1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 offset
, int *icoord
)
136 /* s limited to [0,1) */
137 /* i limited to [0,size-1] */
138 const int i
= util_ifloor(s
* size
);
139 *icoord
= repeat(i
+ offset
, size
);
144 wrap_nearest_clamp(float s
, unsigned size
, int offset
, int *icoord
)
146 /* s limited to [0,1] */
147 /* i limited to [0,size-1] */
155 *icoord
= util_ifloor(s
);
160 wrap_nearest_clamp_to_edge(float s
, unsigned size
, int offset
, int *icoord
)
162 /* s limited to [min,max] */
163 /* i limited to [0, size-1] */
164 const float min
= 0.5F
;
165 const float max
= (float)size
- 0.5F
;
175 *icoord
= util_ifloor(s
);
180 wrap_nearest_clamp_to_border(float s
, unsigned size
, int offset
, int *icoord
)
182 /* s limited to [min,max] */
183 /* i limited to [-1, size] */
184 const float min
= -0.5F
;
185 const float max
= size
+ 0.5F
;
194 *icoord
= util_ifloor(s
);
198 wrap_nearest_mirror_repeat(float s
, unsigned size
, int offset
, int *icoord
)
200 const float min
= 1.0F
/ (2.0F
* size
);
201 const float max
= 1.0F
- min
;
205 s
+= (float)offset
/ size
;
206 flr
= util_ifloor(s
);
215 *icoord
= util_ifloor(u
* size
);
220 wrap_nearest_mirror_clamp(float s
, unsigned size
, int offset
, int *icoord
)
222 /* s limited to [0,1] */
223 /* i limited to [0,size-1] */
224 const float u
= fabsf(s
* size
+ offset
);
230 *icoord
= util_ifloor(u
);
235 wrap_nearest_mirror_clamp_to_edge(float s
, unsigned size
, int offset
, int *icoord
)
237 /* s limited to [min,max] */
238 /* i limited to [0, size-1] */
239 const float min
= 0.5F
;
240 const float max
= (float)size
- 0.5F
;
241 const float u
= fabsf(s
* size
+ offset
);
248 *icoord
= util_ifloor(u
);
253 wrap_nearest_mirror_clamp_to_border(float s
, unsigned size
, int offset
, int *icoord
)
255 /* u limited to [-0.5, size-0.5] */
256 const float min
= -0.5F
;
257 const float max
= (float)size
+ 0.5F
;
258 const float u
= fabsf(s
* size
+ offset
);
265 *icoord
= util_ifloor(u
);
270 * Used to compute texel locations for linear sampling
271 * \param wrapMode PIPE_TEX_WRAP_x
272 * \param s the texcoord
273 * \param size the texture image size
274 * \param icoord0 returns first texture index
275 * \param icoord1 returns second texture index (usually icoord0 + 1)
276 * \param w returns blend factor/weight between texture indices
277 * \param icoord returns the computed integer texture coord
280 wrap_linear_repeat(float s
, unsigned size
, int offset
,
281 int *icoord0
, int *icoord1
, float *w
)
283 const float u
= s
* size
- 0.5F
;
284 *icoord0
= repeat(util_ifloor(u
) + offset
, size
);
285 *icoord1
= repeat(*icoord0
+ 1, size
);
291 wrap_linear_clamp(float s
, unsigned size
, int offset
,
292 int *icoord0
, int *icoord1
, float *w
)
294 const float u
= CLAMP(s
* size
+ offset
, 0.0F
, (float)size
) - 0.5f
;
296 *icoord0
= util_ifloor(u
);
297 *icoord1
= *icoord0
+ 1;
303 wrap_linear_clamp_to_edge(float s
, unsigned size
, int offset
,
304 int *icoord0
, int *icoord1
, float *w
)
306 const float u
= CLAMP(s
* size
+ offset
, 0.0F
, (float)size
) - 0.5f
;
307 *icoord0
= util_ifloor(u
);
308 *icoord1
= *icoord0
+ 1;
311 if (*icoord1
>= (int) size
)
318 wrap_linear_clamp_to_border(float s
, unsigned size
, int offset
,
319 int *icoord0
, int *icoord1
, float *w
)
321 const float min
= -0.5F
;
322 const float max
= (float)size
+ 0.5F
;
323 const float u
= CLAMP(s
* size
+ offset
, min
, max
) - 0.5f
;
324 *icoord0
= util_ifloor(u
);
325 *icoord1
= *icoord0
+ 1;
331 wrap_linear_mirror_repeat(float s
, unsigned size
, int offset
,
332 int *icoord0
, int *icoord1
, float *w
)
337 s
+= (float)offset
/ size
;
338 flr
= util_ifloor(s
);
343 *icoord0
= util_ifloor(u
);
344 *icoord1
= *icoord0
+ 1;
347 if (*icoord1
>= (int) size
)
354 wrap_linear_mirror_clamp(float s
, unsigned size
, int offset
,
355 int *icoord0
, int *icoord1
, float *w
)
357 float u
= fabsf(s
* size
+ offset
);
361 *icoord0
= util_ifloor(u
);
362 *icoord1
= *icoord0
+ 1;
368 wrap_linear_mirror_clamp_to_edge(float s
, unsigned size
, int offset
,
369 int *icoord0
, int *icoord1
, float *w
)
371 float u
= fabsf(s
* size
+ offset
);
375 *icoord0
= util_ifloor(u
);
376 *icoord1
= *icoord0
+ 1;
379 if (*icoord1
>= (int) size
)
386 wrap_linear_mirror_clamp_to_border(float s
, unsigned size
, int offset
,
387 int *icoord0
, int *icoord1
, float *w
)
389 const float min
= -0.5F
;
390 const float max
= size
+ 0.5F
;
391 const float t
= fabsf(s
* size
+ offset
);
392 const float u
= CLAMP(t
, min
, max
) - 0.5F
;
393 *icoord0
= util_ifloor(u
);
394 *icoord1
= *icoord0
+ 1;
400 * PIPE_TEX_WRAP_CLAMP for nearest sampling, unnormalized coords.
403 wrap_nearest_unorm_clamp(float s
, unsigned size
, int offset
, int *icoord
)
405 const int i
= util_ifloor(s
);
406 *icoord
= CLAMP(i
+ offset
, 0, (int) size
-1);
411 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for nearest sampling, unnormalized coords.
414 wrap_nearest_unorm_clamp_to_border(float s
, unsigned size
, int offset
, int *icoord
)
416 *icoord
= util_ifloor( CLAMP(s
+ offset
, -0.5F
, (float) size
+ 0.5F
) );
421 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for nearest sampling, unnormalized coords.
424 wrap_nearest_unorm_clamp_to_edge(float s
, unsigned size
, int offset
, int *icoord
)
426 *icoord
= util_ifloor( CLAMP(s
+ offset
, 0.5F
, (float) size
- 0.5F
) );
431 * PIPE_TEX_WRAP_CLAMP for linear sampling, unnormalized coords.
434 wrap_linear_unorm_clamp(float s
, unsigned size
, int offset
,
435 int *icoord0
, int *icoord1
, float *w
)
437 /* Not exactly what the spec says, but it matches NVIDIA output */
438 const float u
= CLAMP(s
+ offset
- 0.5F
, 0.0f
, (float) size
- 1.0f
);
439 *icoord0
= util_ifloor(u
);
440 *icoord1
= *icoord0
+ 1;
446 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for linear sampling, unnormalized coords.
449 wrap_linear_unorm_clamp_to_border(float s
, unsigned size
, int offset
,
450 int *icoord0
, int *icoord1
, float *w
)
452 const float u
= CLAMP(s
+ offset
, -0.5F
, (float) size
+ 0.5F
) - 0.5F
;
453 *icoord0
= util_ifloor(u
);
454 *icoord1
= *icoord0
+ 1;
455 if (*icoord1
> (int) size
- 1)
462 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for linear sampling, unnormalized coords.
465 wrap_linear_unorm_clamp_to_edge(float s
, unsigned size
, int offset
,
466 int *icoord0
, int *icoord1
, float *w
)
468 const float u
= CLAMP(s
+ offset
, +0.5F
, (float) size
- 0.5F
) - 0.5F
;
469 *icoord0
= util_ifloor(u
);
470 *icoord1
= *icoord0
+ 1;
471 if (*icoord1
> (int) size
- 1)
478 * Do coordinate to array index conversion. For array textures.
481 coord_to_layer(float coord
, unsigned first_layer
, unsigned last_layer
)
483 const int c
= util_ifloor(coord
+ 0.5F
);
484 return CLAMP(c
, (int)first_layer
, (int)last_layer
);
489 * Examine the quad's texture coordinates to compute the partial
490 * derivatives w.r.t X and Y, then compute lambda (level of detail).
493 compute_lambda_1d(const struct sp_sampler_view
*sview
,
494 const float s
[TGSI_QUAD_SIZE
],
495 const float t
[TGSI_QUAD_SIZE
],
496 const float p
[TGSI_QUAD_SIZE
])
498 const struct pipe_resource
*texture
= sview
->base
.texture
;
499 const float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
500 const float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
501 const float rho
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, sview
->base
.u
.tex
.first_level
);
503 return util_fast_log2(rho
);
508 compute_lambda_2d(const struct sp_sampler_view
*sview
,
509 const float s
[TGSI_QUAD_SIZE
],
510 const float t
[TGSI_QUAD_SIZE
],
511 const float p
[TGSI_QUAD_SIZE
])
513 const struct pipe_resource
*texture
= sview
->base
.texture
;
514 const float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
515 const float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
516 const float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
517 const float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
518 const float maxx
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, sview
->base
.u
.tex
.first_level
);
519 const float maxy
= MAX2(dtdx
, dtdy
) * u_minify(texture
->height0
, sview
->base
.u
.tex
.first_level
);
520 const float rho
= MAX2(maxx
, maxy
);
522 return util_fast_log2(rho
);
527 compute_lambda_3d(const struct sp_sampler_view
*sview
,
528 const float s
[TGSI_QUAD_SIZE
],
529 const float t
[TGSI_QUAD_SIZE
],
530 const float p
[TGSI_QUAD_SIZE
])
532 const struct pipe_resource
*texture
= sview
->base
.texture
;
533 const float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
534 const float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
535 const float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
536 const float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
537 const float dpdx
= fabsf(p
[QUAD_BOTTOM_RIGHT
] - p
[QUAD_BOTTOM_LEFT
]);
538 const float dpdy
= fabsf(p
[QUAD_TOP_LEFT
] - p
[QUAD_BOTTOM_LEFT
]);
539 const float maxx
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, sview
->base
.u
.tex
.first_level
);
540 const float maxy
= MAX2(dtdx
, dtdy
) * u_minify(texture
->height0
, sview
->base
.u
.tex
.first_level
);
541 const float maxz
= MAX2(dpdx
, dpdy
) * u_minify(texture
->depth0
, sview
->base
.u
.tex
.first_level
);
542 const float rho
= MAX3(maxx
, maxy
, 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 const 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
);
613 * Here's the complete logic (HOLY CRAP) for finding next face and doing the
614 * corresponding coord wrapping, implemented by get_next_face,
615 * get_next_xcoord, get_next_ycoord.
616 * Read like that (first line):
617 * If face is +x and s coord is below zero, then
618 * new face is +z, new s is max , new t is old t
619 * (max is always cube size - 1).
621 * +x s- -> +z: s = max, t = t
622 * +x s+ -> -z: s = 0, t = t
623 * +x t- -> +y: s = max, t = max-s
624 * +x t+ -> -y: s = max, t = s
626 * -x s- -> -z: s = max, t = t
627 * -x s+ -> +z: s = 0, t = t
628 * -x t- -> +y: s = 0, t = s
629 * -x t+ -> -y: s = 0, t = max-s
631 * +y s- -> -x: s = t, t = 0
632 * +y s+ -> +x: s = max-t, t = 0
633 * +y t- -> -z: s = max-s, t = 0
634 * +y t+ -> +z: s = s, t = 0
636 * -y s- -> -x: s = max-t, t = max
637 * -y s+ -> +x: s = t, t = max
638 * -y t- -> +z: s = s, t = max
639 * -y t+ -> -z: s = max-s, t = max
641 * +z s- -> -x: s = max, t = t
642 * +z s+ -> +x: s = 0, t = t
643 * +z t- -> +y: s = s, t = max
644 * +z t+ -> -y: s = s, t = 0
646 * -z s- -> +x: s = max, t = t
647 * -z s+ -> -x: s = 0, t = t
648 * -z t- -> +y: s = max-s, t = 0
649 * -z t+ -> -y: s = max-s, t = max
654 * seamless cubemap neighbour array.
655 * this array is used to find the adjacent face in each of 4 directions,
656 * left, right, up, down. (or -x, +x, -y, +y).
658 static const unsigned face_array
[PIPE_TEX_FACE_MAX
][4] = {
659 /* pos X first then neg X is Z different, Y the same */
660 /* PIPE_TEX_FACE_POS_X,*/
661 { PIPE_TEX_FACE_POS_Z
, PIPE_TEX_FACE_NEG_Z
,
662 PIPE_TEX_FACE_POS_Y
, PIPE_TEX_FACE_NEG_Y
},
663 /* PIPE_TEX_FACE_NEG_X */
664 { PIPE_TEX_FACE_NEG_Z
, PIPE_TEX_FACE_POS_Z
,
665 PIPE_TEX_FACE_POS_Y
, PIPE_TEX_FACE_NEG_Y
},
667 /* pos Y first then neg Y is X different, X the same */
668 /* PIPE_TEX_FACE_POS_Y */
669 { PIPE_TEX_FACE_NEG_X
, PIPE_TEX_FACE_POS_X
,
670 PIPE_TEX_FACE_NEG_Z
, PIPE_TEX_FACE_POS_Z
},
672 /* PIPE_TEX_FACE_NEG_Y */
673 { PIPE_TEX_FACE_NEG_X
, PIPE_TEX_FACE_POS_X
,
674 PIPE_TEX_FACE_POS_Z
, PIPE_TEX_FACE_NEG_Z
},
676 /* pos Z first then neg Y is X different, X the same */
677 /* PIPE_TEX_FACE_POS_Z */
678 { PIPE_TEX_FACE_NEG_X
, PIPE_TEX_FACE_POS_X
,
679 PIPE_TEX_FACE_POS_Y
, PIPE_TEX_FACE_NEG_Y
},
681 /* PIPE_TEX_FACE_NEG_Z */
682 { PIPE_TEX_FACE_POS_X
, PIPE_TEX_FACE_NEG_X
,
683 PIPE_TEX_FACE_POS_Y
, PIPE_TEX_FACE_NEG_Y
}
686 static inline unsigned
687 get_next_face(unsigned face
, int idx
)
689 return face_array
[face
][idx
];
693 * return a new xcoord based on old face, old coords, cube size
694 * and fall_off_index (0 for x-, 1 for x+, 2 for y-, 3 for y+)
697 get_next_xcoord(unsigned face
, unsigned fall_off_index
, int max
, int xc
, int yc
)
699 if ((face
== 0 && fall_off_index
!= 1) ||
700 (face
== 1 && fall_off_index
== 0) ||
701 (face
== 4 && fall_off_index
== 0) ||
702 (face
== 5 && fall_off_index
== 0)) {
705 if ((face
== 1 && fall_off_index
!= 0) ||
706 (face
== 0 && fall_off_index
== 1) ||
707 (face
== 4 && fall_off_index
== 1) ||
708 (face
== 5 && fall_off_index
== 1)) {
711 if ((face
== 4 && fall_off_index
>= 2) ||
712 (face
== 2 && fall_off_index
== 3) ||
713 (face
== 3 && fall_off_index
== 2)) {
716 if ((face
== 5 && fall_off_index
>= 2) ||
717 (face
== 2 && fall_off_index
== 2) ||
718 (face
== 3 && fall_off_index
== 3)) {
721 if ((face
== 2 && fall_off_index
== 0) ||
722 (face
== 3 && fall_off_index
== 1)) {
725 /* (face == 2 && fall_off_index == 1) ||
726 (face == 3 && fall_off_index == 0)) */
731 * return a new ycoord based on old face, old coords, cube size
732 * and fall_off_index (0 for x-, 1 for x+, 2 for y-, 3 for y+)
735 get_next_ycoord(unsigned face
, unsigned fall_off_index
, int max
, int xc
, int yc
)
737 if ((fall_off_index
<= 1) && (face
<= 1 || face
>= 4)) {
741 (face
== 4 && fall_off_index
== 3) ||
742 (face
== 5 && fall_off_index
== 2)) {
746 (face
== 4 && fall_off_index
== 2) ||
747 (face
== 5 && fall_off_index
== 3)) {
750 if ((face
== 0 && fall_off_index
== 3) ||
751 (face
== 1 && fall_off_index
== 2)) {
754 /* (face == 0 && fall_off_index == 2) ||
755 (face == 1 && fall_off_index == 3) */
760 /* Gather a quad of adjacent texels within a tile:
763 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_view
*sp_sview
,
764 union tex_tile_address addr
,
765 unsigned x
, unsigned y
,
768 const struct softpipe_tex_cached_tile
*tile
;
770 addr
.bits
.x
= x
/ TEX_TILE_SIZE
;
771 addr
.bits
.y
= y
/ TEX_TILE_SIZE
;
775 tile
= sp_get_cached_tile_tex(sp_sview
->cache
, addr
);
777 out
[0] = &tile
->data
.color
[y
][x
][0];
778 out
[1] = &tile
->data
.color
[y
][x
+1][0];
779 out
[2] = &tile
->data
.color
[y
+1][x
][0];
780 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
784 /* Gather a quad of potentially non-adjacent texels:
787 get_texel_quad_2d_no_border(const struct sp_sampler_view
*sp_sview
,
788 union tex_tile_address addr
,
793 out
[0] = get_texel_2d_no_border( sp_sview
, addr
, x0
, y0
);
794 out
[1] = get_texel_2d_no_border( sp_sview
, addr
, x1
, y0
);
795 out
[2] = get_texel_2d_no_border( sp_sview
, addr
, x0
, y1
);
796 out
[3] = get_texel_2d_no_border( sp_sview
, addr
, x1
, y1
);
802 static inline const float *
803 get_texel_3d_no_border(const struct sp_sampler_view
*sp_sview
,
804 union tex_tile_address addr
, int x
, int y
, int z
)
806 const struct softpipe_tex_cached_tile
*tile
;
808 addr
.bits
.x
= x
/ TEX_TILE_SIZE
;
809 addr
.bits
.y
= y
/ TEX_TILE_SIZE
;
814 tile
= sp_get_cached_tile_tex(sp_sview
->cache
, addr
);
816 return &tile
->data
.color
[y
][x
][0];
820 static inline const float *
821 get_texel_3d(const struct sp_sampler_view
*sp_sview
,
822 const struct sp_sampler
*sp_samp
,
823 union tex_tile_address addr
, int x
, int y
, int z
)
825 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
826 const unsigned level
= addr
.bits
.level
;
828 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
829 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
) ||
830 z
< 0 || z
>= (int) u_minify(texture
->depth0
, level
)) {
831 return sp_samp
->base
.border_color
.f
;
834 return get_texel_3d_no_border( sp_sview
, addr
, x
, y
, z
);
839 /* Get texel pointer for 1D array texture */
840 static inline const float *
841 get_texel_1d_array(const struct sp_sampler_view
*sp_sview
,
842 const struct sp_sampler
*sp_samp
,
843 union tex_tile_address addr
, int x
, int y
)
845 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
846 const unsigned level
= addr
.bits
.level
;
848 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
)) {
849 return sp_samp
->base
.border_color
.f
;
852 return get_texel_2d_no_border(sp_sview
, addr
, x
, y
);
857 /* Get texel pointer for 2D array texture */
858 static inline const float *
859 get_texel_2d_array(const struct sp_sampler_view
*sp_sview
,
860 const struct sp_sampler
*sp_samp
,
861 union tex_tile_address addr
, int x
, int y
, int layer
)
863 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
864 const unsigned level
= addr
.bits
.level
;
866 assert(layer
< (int) texture
->array_size
);
869 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
870 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
871 return sp_samp
->base
.border_color
.f
;
874 return get_texel_3d_no_border(sp_sview
, addr
, x
, y
, layer
);
879 static inline const float *
880 get_texel_cube_seamless(const struct sp_sampler_view
*sp_sview
,
881 union tex_tile_address addr
, int x
, int y
,
882 float *corner
, int layer
, unsigned face
)
884 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
885 const unsigned level
= addr
.bits
.level
;
886 int new_x
, new_y
, max_x
;
888 max_x
= (int) u_minify(texture
->width0
, level
);
890 assert(texture
->width0
== texture
->height0
);
894 /* change the face */
897 * Cheat with corners. They are difficult and I believe because we don't get
898 * per-pixel faces we can actually have multiple corner texels per pixel,
899 * which screws things up majorly in any case (as the per spec behavior is
900 * to average the 3 remaining texels, which we might not have).
901 * Hence just make sure that the 2nd coord is clamped, will simply pick the
902 * sample which would have fallen off the x coord, but not y coord.
903 * So the filter weight of the samples will be wrong, but at least this
904 * ensures that only valid texels near the corner are used.
906 if (y
< 0 || y
>= max_x
) {
907 y
= CLAMP(y
, 0, max_x
- 1);
909 new_x
= get_next_xcoord(face
, 0, max_x
-1, x
, y
);
910 new_y
= get_next_ycoord(face
, 0, max_x
-1, x
, y
);
911 face
= get_next_face(face
, 0);
912 } else if (x
>= max_x
) {
913 if (y
< 0 || y
>= max_x
) {
914 y
= CLAMP(y
, 0, max_x
- 1);
916 new_x
= get_next_xcoord(face
, 1, max_x
-1, x
, y
);
917 new_y
= get_next_ycoord(face
, 1, max_x
-1, x
, y
);
918 face
= get_next_face(face
, 1);
920 new_x
= get_next_xcoord(face
, 2, max_x
-1, x
, y
);
921 new_y
= get_next_ycoord(face
, 2, max_x
-1, x
, y
);
922 face
= get_next_face(face
, 2);
923 } else if (y
>= max_x
) {
924 new_x
= get_next_xcoord(face
, 3, max_x
-1, x
, y
);
925 new_y
= get_next_ycoord(face
, 3, max_x
-1, x
, y
);
926 face
= get_next_face(face
, 3);
929 return get_texel_3d_no_border(sp_sview
, addr
, new_x
, new_y
, layer
+ face
);
933 /* Get texel pointer for cube array texture */
934 static inline const float *
935 get_texel_cube_array(const struct sp_sampler_view
*sp_sview
,
936 const struct sp_sampler
*sp_samp
,
937 union tex_tile_address addr
, int x
, int y
, int layer
)
939 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
940 const unsigned level
= addr
.bits
.level
;
942 assert(layer
< (int) texture
->array_size
);
945 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
946 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
947 return sp_samp
->base
.border_color
.f
;
950 return get_texel_3d_no_border(sp_sview
, addr
, x
, y
, layer
);
954 * Given the logbase2 of a mipmap's base level size and a mipmap level,
955 * return the size (in texels) of that mipmap level.
956 * For example, if level[0].width = 256 then base_pot will be 8.
957 * If level = 2, then we'll return 64 (the width at level=2).
958 * Return 1 if level > base_pot.
960 static inline unsigned
961 pot_level_size(unsigned base_pot
, unsigned level
)
963 return (base_pot
>= level
) ? (1 << (base_pot
- level
)) : 1;
968 print_sample(const char *function
, const float *rgba
)
970 debug_printf("%s %g %g %g %g\n",
972 rgba
[0], rgba
[TGSI_NUM_CHANNELS
], rgba
[2*TGSI_NUM_CHANNELS
], rgba
[3*TGSI_NUM_CHANNELS
]);
977 print_sample_4(const char *function
, float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
979 debug_printf("%s %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
981 rgba
[0][0], rgba
[1][0], rgba
[2][0], rgba
[3][0],
982 rgba
[0][1], rgba
[1][1], rgba
[2][1], rgba
[3][1],
983 rgba
[0][2], rgba
[1][2], rgba
[2][2], rgba
[3][2],
984 rgba
[0][3], rgba
[1][3], rgba
[2][3], rgba
[3][3]);
988 /* Some image-filter fastpaths:
991 img_filter_2d_linear_repeat_POT(const struct sp_sampler_view
*sp_sview
,
992 const struct sp_sampler
*sp_samp
,
993 const struct img_filter_args
*args
,
996 const unsigned xpot
= pot_level_size(sp_sview
->xpot
, args
->level
);
997 const unsigned ypot
= pot_level_size(sp_sview
->ypot
, args
->level
);
998 const int xmax
= (xpot
- 1) & (TEX_TILE_SIZE
- 1); /* MIN2(TEX_TILE_SIZE, xpot) - 1; */
999 const int ymax
= (ypot
- 1) & (TEX_TILE_SIZE
- 1); /* MIN2(TEX_TILE_SIZE, ypot) - 1; */
1000 union tex_tile_address addr
;
1003 const float u
= (args
->s
* xpot
- 0.5F
) + args
->offset
[0];
1004 const float v
= (args
->t
* ypot
- 0.5F
) + args
->offset
[1];
1006 const int uflr
= util_ifloor(u
);
1007 const int vflr
= util_ifloor(v
);
1009 const float xw
= u
- (float)uflr
;
1010 const float yw
= v
- (float)vflr
;
1012 const int x0
= uflr
& (xpot
- 1);
1013 const int y0
= vflr
& (ypot
- 1);
1018 addr
.bits
.level
= args
->level
;
1019 addr
.bits
.z
= sp_sview
->base
.u
.tex
.first_layer
;
1021 /* Can we fetch all four at once:
1023 if (x0
< xmax
&& y0
< ymax
) {
1024 get_texel_quad_2d_no_border_single_tile(sp_sview
, addr
, x0
, y0
, tx
);
1027 const unsigned x1
= (x0
+ 1) & (xpot
- 1);
1028 const unsigned y1
= (y0
+ 1) & (ypot
- 1);
1029 get_texel_quad_2d_no_border(sp_sview
, addr
, x0
, y0
, x1
, y1
, tx
);
1032 /* interpolate R, G, B, A */
1033 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++) {
1034 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1036 tx
[2][c
], tx
[3][c
]);
1040 print_sample(__FUNCTION__
, rgba
);
1046 img_filter_2d_nearest_repeat_POT(const struct sp_sampler_view
*sp_sview
,
1047 const struct sp_sampler
*sp_samp
,
1048 const struct img_filter_args
*args
,
1051 const unsigned xpot
= pot_level_size(sp_sview
->xpot
, args
->level
);
1052 const unsigned ypot
= pot_level_size(sp_sview
->ypot
, args
->level
);
1054 union tex_tile_address addr
;
1057 const float u
= args
->s
* xpot
+ args
->offset
[0];
1058 const float v
= args
->t
* ypot
+ args
->offset
[1];
1060 const int uflr
= util_ifloor(u
);
1061 const int vflr
= util_ifloor(v
);
1063 const int x0
= uflr
& (xpot
- 1);
1064 const int y0
= vflr
& (ypot
- 1);
1067 addr
.bits
.level
= args
->level
;
1068 addr
.bits
.z
= sp_sview
->base
.u
.tex
.first_layer
;
1070 out
= get_texel_2d_no_border(sp_sview
, addr
, x0
, y0
);
1071 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1072 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1075 print_sample(__FUNCTION__
, rgba
);
1081 img_filter_2d_nearest_clamp_POT(const struct sp_sampler_view
*sp_sview
,
1082 const struct sp_sampler
*sp_samp
,
1083 const struct img_filter_args
*args
,
1086 const unsigned xpot
= pot_level_size(sp_sview
->xpot
, args
->level
);
1087 const unsigned ypot
= pot_level_size(sp_sview
->ypot
, args
->level
);
1088 union tex_tile_address addr
;
1091 const float u
= args
->s
* xpot
+ args
->offset
[0];
1092 const float v
= args
->t
* ypot
+ args
->offset
[1];
1098 addr
.bits
.level
= args
->level
;
1099 addr
.bits
.z
= sp_sview
->base
.u
.tex
.first_layer
;
1101 x0
= util_ifloor(u
);
1104 else if (x0
> (int) xpot
- 1)
1107 y0
= util_ifloor(v
);
1110 else if (y0
> (int) ypot
- 1)
1113 out
= get_texel_2d_no_border(sp_sview
, addr
, x0
, y0
);
1114 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1115 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1118 print_sample(__FUNCTION__
, rgba
);
1124 img_filter_1d_nearest(const struct sp_sampler_view
*sp_sview
,
1125 const struct sp_sampler
*sp_samp
,
1126 const struct img_filter_args
*args
,
1129 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1130 const int width
= u_minify(texture
->width0
, args
->level
);
1132 union tex_tile_address addr
;
1139 addr
.bits
.level
= args
->level
;
1141 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1143 out
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x
,
1144 sp_sview
->base
.u
.tex
.first_layer
);
1145 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1146 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1149 print_sample(__FUNCTION__
, rgba
);
1155 img_filter_1d_array_nearest(const struct sp_sampler_view
*sp_sview
,
1156 const struct sp_sampler
*sp_samp
,
1157 const struct img_filter_args
*args
,
1160 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1161 const int width
= u_minify(texture
->width0
, args
->level
);
1162 const int layer
= coord_to_layer(args
->t
, sp_sview
->base
.u
.tex
.first_layer
,
1163 sp_sview
->base
.u
.tex
.last_layer
);
1165 union tex_tile_address addr
;
1172 addr
.bits
.level
= args
->level
;
1174 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1176 out
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x
, layer
);
1177 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1178 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1181 print_sample(__FUNCTION__
, rgba
);
1187 img_filter_2d_nearest(const struct sp_sampler_view
*sp_sview
,
1188 const struct sp_sampler
*sp_samp
,
1189 const struct img_filter_args
*args
,
1192 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1193 const int width
= u_minify(texture
->width0
, args
->level
);
1194 const int height
= u_minify(texture
->height0
, args
->level
);
1196 union tex_tile_address addr
;
1204 addr
.bits
.level
= args
->level
;
1205 addr
.bits
.z
= sp_sview
->base
.u
.tex
.first_layer
;
1207 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1208 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1210 out
= get_texel_2d(sp_sview
, sp_samp
, addr
, x
, y
);
1211 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1212 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1215 print_sample(__FUNCTION__
, rgba
);
1221 img_filter_2d_array_nearest(const struct sp_sampler_view
*sp_sview
,
1222 const struct sp_sampler
*sp_samp
,
1223 const struct img_filter_args
*args
,
1226 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1227 const int width
= u_minify(texture
->width0
, args
->level
);
1228 const int height
= u_minify(texture
->height0
, args
->level
);
1229 const int layer
= coord_to_layer(args
->p
, sp_sview
->base
.u
.tex
.first_layer
,
1230 sp_sview
->base
.u
.tex
.last_layer
);
1232 union tex_tile_address addr
;
1240 addr
.bits
.level
= args
->level
;
1242 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1243 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1245 out
= get_texel_2d_array(sp_sview
, sp_samp
, addr
, x
, y
, layer
);
1246 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1247 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1250 print_sample(__FUNCTION__
, rgba
);
1256 img_filter_cube_nearest(const struct sp_sampler_view
*sp_sview
,
1257 const struct sp_sampler
*sp_samp
,
1258 const struct img_filter_args
*args
,
1261 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1262 const int width
= u_minify(texture
->width0
, args
->level
);
1263 const int height
= u_minify(texture
->height0
, args
->level
);
1264 const int layerface
= args
->face_id
+ sp_sview
->base
.u
.tex
.first_layer
;
1266 union tex_tile_address addr
;
1274 addr
.bits
.level
= args
->level
;
1277 * If NEAREST filtering is done within a miplevel, always apply wrap
1278 * mode CLAMP_TO_EDGE.
1280 if (sp_samp
->base
.seamless_cube_map
) {
1281 wrap_nearest_clamp_to_edge(args
->s
, width
, args
->offset
[0], &x
);
1282 wrap_nearest_clamp_to_edge(args
->t
, height
, args
->offset
[1], &y
);
1284 /* Would probably make sense to ignore mode and just do edge clamp */
1285 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1286 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1289 out
= get_texel_cube_array(sp_sview
, sp_samp
, addr
, x
, y
, layerface
);
1290 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1291 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1294 print_sample(__FUNCTION__
, rgba
);
1299 img_filter_cube_array_nearest(const struct sp_sampler_view
*sp_sview
,
1300 const struct sp_sampler
*sp_samp
,
1301 const struct img_filter_args
*args
,
1304 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1305 const int width
= u_minify(texture
->width0
, args
->level
);
1306 const int height
= u_minify(texture
->height0
, args
->level
);
1307 const int layerface
=
1308 coord_to_layer(6 * args
->p
+ sp_sview
->base
.u
.tex
.first_layer
,
1309 sp_sview
->base
.u
.tex
.first_layer
,
1310 sp_sview
->base
.u
.tex
.last_layer
- 5) + args
->face_id
;
1312 union tex_tile_address addr
;
1320 addr
.bits
.level
= args
->level
;
1322 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1323 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1325 out
= get_texel_cube_array(sp_sview
, sp_samp
, addr
, x
, y
, layerface
);
1326 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1327 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1330 print_sample(__FUNCTION__
, rgba
);
1335 img_filter_3d_nearest(const struct sp_sampler_view
*sp_sview
,
1336 const struct sp_sampler
*sp_samp
,
1337 const struct img_filter_args
*args
,
1340 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1341 const int width
= u_minify(texture
->width0
, args
->level
);
1342 const int height
= u_minify(texture
->height0
, args
->level
);
1343 const int depth
= u_minify(texture
->depth0
, args
->level
);
1345 union tex_tile_address addr
;
1353 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1354 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1355 sp_samp
->nearest_texcoord_p(args
->p
, depth
, args
->offset
[2], &z
);
1358 addr
.bits
.level
= args
->level
;
1360 out
= get_texel_3d(sp_sview
, sp_samp
, addr
, x
, y
, z
);
1361 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1362 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1367 img_filter_1d_linear(const struct sp_sampler_view
*sp_sview
,
1368 const struct sp_sampler
*sp_samp
,
1369 const struct img_filter_args
*args
,
1372 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1373 const int width
= u_minify(texture
->width0
, args
->level
);
1375 float xw
; /* weights */
1376 union tex_tile_address addr
;
1377 const float *tx0
, *tx1
;
1383 addr
.bits
.level
= args
->level
;
1385 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1387 tx0
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x0
,
1388 sp_sview
->base
.u
.tex
.first_layer
);
1389 tx1
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x1
,
1390 sp_sview
->base
.u
.tex
.first_layer
);
1392 /* interpolate R, G, B, A */
1393 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1394 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp(xw
, tx0
[c
], tx1
[c
]);
1399 img_filter_1d_array_linear(const struct sp_sampler_view
*sp_sview
,
1400 const struct sp_sampler
*sp_samp
,
1401 const struct img_filter_args
*args
,
1404 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1405 const int width
= u_minify(texture
->width0
, args
->level
);
1406 const int layer
= coord_to_layer(args
->t
, sp_sview
->base
.u
.tex
.first_layer
,
1407 sp_sview
->base
.u
.tex
.last_layer
);
1409 float xw
; /* weights */
1410 union tex_tile_address addr
;
1411 const float *tx0
, *tx1
;
1417 addr
.bits
.level
= args
->level
;
1419 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1421 tx0
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x0
, layer
);
1422 tx1
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x1
, layer
);
1424 /* interpolate R, G, B, A */
1425 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1426 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp(xw
, tx0
[c
], tx1
[c
]);
1430 * Retrieve the gathered value, need to convert to the
1431 * TGSI expected interface, and take component select
1432 * and swizzling into account.
1435 get_gather_value(const struct sp_sampler_view
*sp_sview
,
1436 int chan_in
, int comp_sel
,
1443 * softpipe samples in a different order
1444 * to TGSI expects, so we need to swizzle,
1445 * the samples into the correct slots.
1465 /* pick which component to use for the swizzle */
1468 swizzle
= sp_sview
->base
.swizzle_r
;
1471 swizzle
= sp_sview
->base
.swizzle_g
;
1474 swizzle
= sp_sview
->base
.swizzle_b
;
1477 swizzle
= sp_sview
->base
.swizzle_a
;
1484 /* get correct result using the channel and swizzle */
1486 case PIPE_SWIZZLE_0
:
1488 case PIPE_SWIZZLE_1
:
1491 return tx
[chan
][swizzle
];
1497 img_filter_2d_linear(const struct sp_sampler_view
*sp_sview
,
1498 const struct sp_sampler
*sp_samp
,
1499 const struct img_filter_args
*args
,
1502 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1503 const int width
= u_minify(texture
->width0
, args
->level
);
1504 const int height
= u_minify(texture
->height0
, args
->level
);
1506 float xw
, yw
; /* weights */
1507 union tex_tile_address addr
;
1515 addr
.bits
.level
= args
->level
;
1516 addr
.bits
.z
= sp_sview
->base
.u
.tex
.first_layer
;
1518 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1519 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1521 tx
[0] = get_texel_2d(sp_sview
, sp_samp
, addr
, x0
, y0
);
1522 tx
[1] = get_texel_2d(sp_sview
, sp_samp
, addr
, x1
, y0
);
1523 tx
[2] = get_texel_2d(sp_sview
, sp_samp
, addr
, x0
, y1
);
1524 tx
[3] = get_texel_2d(sp_sview
, sp_samp
, addr
, x1
, y1
);
1526 if (args
->gather_only
) {
1527 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1528 rgba
[TGSI_NUM_CHANNELS
*c
] = get_gather_value(sp_sview
, c
,
1532 /* interpolate R, G, B, A */
1533 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1534 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1536 tx
[2][c
], tx
[3][c
]);
1542 img_filter_2d_array_linear(const struct sp_sampler_view
*sp_sview
,
1543 const struct sp_sampler
*sp_samp
,
1544 const struct img_filter_args
*args
,
1547 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1548 const int width
= u_minify(texture
->width0
, args
->level
);
1549 const int height
= u_minify(texture
->height0
, args
->level
);
1550 const int layer
= coord_to_layer(args
->p
, sp_sview
->base
.u
.tex
.first_layer
,
1551 sp_sview
->base
.u
.tex
.last_layer
);
1553 float xw
, yw
; /* weights */
1554 union tex_tile_address addr
;
1562 addr
.bits
.level
= args
->level
;
1564 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1565 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1567 tx
[0] = get_texel_2d_array(sp_sview
, sp_samp
, addr
, x0
, y0
, layer
);
1568 tx
[1] = get_texel_2d_array(sp_sview
, sp_samp
, addr
, x1
, y0
, layer
);
1569 tx
[2] = get_texel_2d_array(sp_sview
, sp_samp
, addr
, x0
, y1
, layer
);
1570 tx
[3] = get_texel_2d_array(sp_sview
, sp_samp
, addr
, x1
, y1
, layer
);
1572 if (args
->gather_only
) {
1573 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1574 rgba
[TGSI_NUM_CHANNELS
*c
] = get_gather_value(sp_sview
, c
,
1578 /* interpolate R, G, B, A */
1579 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1580 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1582 tx
[2][c
], tx
[3][c
]);
1588 img_filter_cube_linear(const struct sp_sampler_view
*sp_sview
,
1589 const struct sp_sampler
*sp_samp
,
1590 const struct img_filter_args
*args
,
1593 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1594 const int width
= u_minify(texture
->width0
, args
->level
);
1595 const int height
= u_minify(texture
->height0
, args
->level
);
1596 const int layer
= sp_sview
->base
.u
.tex
.first_layer
;
1598 float xw
, yw
; /* weights */
1599 union tex_tile_address addr
;
1601 float corner0
[TGSI_QUAD_SIZE
], corner1
[TGSI_QUAD_SIZE
],
1602 corner2
[TGSI_QUAD_SIZE
], corner3
[TGSI_QUAD_SIZE
];
1609 addr
.bits
.level
= args
->level
;
1612 * For seamless if LINEAR filtering is done within a miplevel,
1613 * always apply wrap mode CLAMP_TO_BORDER.
1615 if (sp_samp
->base
.seamless_cube_map
) {
1616 /* Note this is a bit overkill, actual clamping is not required */
1617 wrap_linear_clamp_to_border(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1618 wrap_linear_clamp_to_border(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1620 /* Would probably make sense to ignore mode and just do edge clamp */
1621 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1622 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1625 if (sp_samp
->base
.seamless_cube_map
) {
1626 tx
[0] = get_texel_cube_seamless(sp_sview
, addr
, x0
, y0
, corner0
, layer
, args
->face_id
);
1627 tx
[1] = get_texel_cube_seamless(sp_sview
, addr
, x1
, y0
, corner1
, layer
, args
->face_id
);
1628 tx
[2] = get_texel_cube_seamless(sp_sview
, addr
, x0
, y1
, corner2
, layer
, args
->face_id
);
1629 tx
[3] = get_texel_cube_seamless(sp_sview
, addr
, x1
, y1
, corner3
, layer
, args
->face_id
);
1631 tx
[0] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x0
, y0
, layer
+ args
->face_id
);
1632 tx
[1] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x1
, y0
, layer
+ args
->face_id
);
1633 tx
[2] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x0
, y1
, layer
+ args
->face_id
);
1634 tx
[3] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x1
, y1
, layer
+ args
->face_id
);
1637 if (args
->gather_only
) {
1638 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1639 rgba
[TGSI_NUM_CHANNELS
*c
] = get_gather_value(sp_sview
, c
,
1643 /* interpolate R, G, B, A */
1644 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1645 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1647 tx
[2][c
], tx
[3][c
]);
1653 img_filter_cube_array_linear(const struct sp_sampler_view
*sp_sview
,
1654 const struct sp_sampler
*sp_samp
,
1655 const struct img_filter_args
*args
,
1658 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1659 const int width
= u_minify(texture
->width0
, args
->level
);
1660 const int height
= u_minify(texture
->height0
, args
->level
);
1662 coord_to_layer(6 * args
->p
+ sp_sview
->base
.u
.tex
.first_layer
,
1663 sp_sview
->base
.u
.tex
.first_layer
,
1664 sp_sview
->base
.u
.tex
.last_layer
- 5);
1666 float xw
, yw
; /* weights */
1667 union tex_tile_address addr
;
1669 float corner0
[TGSI_QUAD_SIZE
], corner1
[TGSI_QUAD_SIZE
],
1670 corner2
[TGSI_QUAD_SIZE
], corner3
[TGSI_QUAD_SIZE
];
1677 addr
.bits
.level
= args
->level
;
1680 * For seamless if LINEAR filtering is done within a miplevel,
1681 * always apply wrap mode CLAMP_TO_BORDER.
1683 if (sp_samp
->base
.seamless_cube_map
) {
1684 /* Note this is a bit overkill, actual clamping is not required */
1685 wrap_linear_clamp_to_border(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1686 wrap_linear_clamp_to_border(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1688 /* Would probably make sense to ignore mode and just do edge clamp */
1689 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1690 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1693 if (sp_samp
->base
.seamless_cube_map
) {
1694 tx
[0] = get_texel_cube_seamless(sp_sview
, addr
, x0
, y0
, corner0
, layer
, args
->face_id
);
1695 tx
[1] = get_texel_cube_seamless(sp_sview
, addr
, x1
, y0
, corner1
, layer
, args
->face_id
);
1696 tx
[2] = get_texel_cube_seamless(sp_sview
, addr
, x0
, y1
, corner2
, layer
, args
->face_id
);
1697 tx
[3] = get_texel_cube_seamless(sp_sview
, addr
, x1
, y1
, corner3
, layer
, args
->face_id
);
1699 tx
[0] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x0
, y0
, layer
+ args
->face_id
);
1700 tx
[1] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x1
, y0
, layer
+ args
->face_id
);
1701 tx
[2] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x0
, y1
, layer
+ args
->face_id
);
1702 tx
[3] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x1
, y1
, layer
+ args
->face_id
);
1705 if (args
->gather_only
) {
1706 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1707 rgba
[TGSI_NUM_CHANNELS
*c
] = get_gather_value(sp_sview
, c
,
1711 /* interpolate R, G, B, A */
1712 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1713 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1715 tx
[2][c
], tx
[3][c
]);
1720 img_filter_3d_linear(const struct sp_sampler_view
*sp_sview
,
1721 const struct sp_sampler
*sp_samp
,
1722 const struct img_filter_args
*args
,
1725 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1726 const int width
= u_minify(texture
->width0
, args
->level
);
1727 const int height
= u_minify(texture
->height0
, args
->level
);
1728 const int depth
= u_minify(texture
->depth0
, args
->level
);
1729 int x0
, x1
, y0
, y1
, z0
, z1
;
1730 float xw
, yw
, zw
; /* interpolation weights */
1731 union tex_tile_address addr
;
1732 const float *tx00
, *tx01
, *tx02
, *tx03
, *tx10
, *tx11
, *tx12
, *tx13
;
1736 addr
.bits
.level
= args
->level
;
1742 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1743 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1744 sp_samp
->linear_texcoord_p(args
->p
, depth
, args
->offset
[2], &z0
, &z1
, &zw
);
1746 tx00
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y0
, z0
);
1747 tx01
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y0
, z0
);
1748 tx02
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y1
, z0
);
1749 tx03
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y1
, z0
);
1751 tx10
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y0
, z1
);
1752 tx11
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y0
, z1
);
1753 tx12
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y1
, z1
);
1754 tx13
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y1
, z1
);
1756 /* interpolate R, G, B, A */
1757 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1758 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_3d(xw
, yw
, zw
,
1766 /* Calculate level of detail for every fragment,
1767 * with lambda already computed.
1768 * Note that lambda has already been biased by global LOD bias.
1769 * \param biased_lambda per-quad lambda.
1770 * \param lod_in per-fragment lod_bias or explicit_lod.
1771 * \param lod returns the per-fragment lod.
1774 compute_lod(const struct pipe_sampler_state
*sampler
,
1775 enum tgsi_sampler_control control
,
1776 const float biased_lambda
,
1777 const float lod_in
[TGSI_QUAD_SIZE
],
1778 float lod
[TGSI_QUAD_SIZE
])
1780 const float min_lod
= sampler
->min_lod
;
1781 const float max_lod
= sampler
->max_lod
;
1785 case TGSI_SAMPLER_LOD_NONE
:
1786 case TGSI_SAMPLER_LOD_ZERO
:
1788 case TGSI_SAMPLER_DERIVS_EXPLICIT
:
1789 lod
[0] = lod
[1] = lod
[2] = lod
[3] = CLAMP(biased_lambda
, min_lod
, max_lod
);
1791 case TGSI_SAMPLER_LOD_BIAS
:
1792 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1793 lod
[i
] = biased_lambda
+ lod_in
[i
];
1794 lod
[i
] = CLAMP(lod
[i
], min_lod
, max_lod
);
1797 case TGSI_SAMPLER_LOD_EXPLICIT
:
1798 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1799 lod
[i
] = CLAMP(lod_in
[i
], min_lod
, max_lod
);
1804 lod
[0] = lod
[1] = lod
[2] = lod
[3] = 0.0f
;
1809 /* Calculate level of detail for every fragment. The computed value is not
1810 * clamped to lod_min and lod_max.
1811 * \param lod_in per-fragment lod_bias or explicit_lod.
1812 * \param lod results per-fragment lod.
1815 compute_lambda_lod_unclamped(const struct sp_sampler_view
*sp_sview
,
1816 const struct sp_sampler
*sp_samp
,
1817 const float s
[TGSI_QUAD_SIZE
],
1818 const float t
[TGSI_QUAD_SIZE
],
1819 const float p
[TGSI_QUAD_SIZE
],
1820 const float lod_in
[TGSI_QUAD_SIZE
],
1821 enum tgsi_sampler_control control
,
1822 float lod
[TGSI_QUAD_SIZE
])
1824 const struct pipe_sampler_state
*sampler
= &sp_samp
->base
;
1825 const float lod_bias
= sampler
->lod_bias
;
1830 case TGSI_SAMPLER_LOD_NONE
:
1832 case TGSI_SAMPLER_DERIVS_EXPLICIT
:
1833 lambda
= sp_sview
->compute_lambda(sp_sview
, s
, t
, p
) + lod_bias
;
1834 lod
[0] = lod
[1] = lod
[2] = lod
[3] = lambda
;
1836 case TGSI_SAMPLER_LOD_BIAS
:
1837 lambda
= sp_sview
->compute_lambda(sp_sview
, s
, t
, p
) + lod_bias
;
1838 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1839 lod
[i
] = lambda
+ lod_in
[i
];
1842 case TGSI_SAMPLER_LOD_EXPLICIT
:
1843 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1844 lod
[i
] = lod_in
[i
] + lod_bias
;
1847 case TGSI_SAMPLER_LOD_ZERO
:
1848 case TGSI_SAMPLER_GATHER
:
1849 lod
[0] = lod
[1] = lod
[2] = lod
[3] = lod_bias
;
1853 lod
[0] = lod
[1] = lod
[2] = lod
[3] = 0.0f
;
1857 /* Calculate level of detail for every fragment.
1858 * \param lod_in per-fragment lod_bias or explicit_lod.
1859 * \param lod results per-fragment lod.
1862 compute_lambda_lod(const struct sp_sampler_view
*sp_sview
,
1863 const struct sp_sampler
*sp_samp
,
1864 const float s
[TGSI_QUAD_SIZE
],
1865 const float t
[TGSI_QUAD_SIZE
],
1866 const float p
[TGSI_QUAD_SIZE
],
1867 const float lod_in
[TGSI_QUAD_SIZE
],
1868 enum tgsi_sampler_control control
,
1869 float lod
[TGSI_QUAD_SIZE
])
1871 const struct pipe_sampler_state
*sampler
= &sp_samp
->base
;
1872 const float min_lod
= sampler
->min_lod
;
1873 const float max_lod
= sampler
->max_lod
;
1876 compute_lambda_lod_unclamped(sp_sview
, sp_samp
,
1877 s
, t
, p
, lod_in
, control
, lod
);
1878 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1879 lod
[i
] = CLAMP(lod
[i
], min_lod
, max_lod
);
1883 static inline unsigned
1884 get_gather_component(const float lod_in
[TGSI_QUAD_SIZE
])
1886 /* gather component is stored in lod_in slot as unsigned */
1887 return (*(unsigned int *)lod_in
) & 0x3;
1891 * Clamps given lod to both lod limits and mip level limits. Clamping to the
1892 * latter limits is done so that lod is relative to the first (base) level.
1895 clamp_lod(const struct sp_sampler_view
*sp_sview
,
1896 const struct sp_sampler
*sp_samp
,
1897 const float lod
[TGSI_QUAD_SIZE
],
1898 float clamped
[TGSI_QUAD_SIZE
])
1900 const float min_lod
= sp_samp
->base
.min_lod
;
1901 const float max_lod
= sp_samp
->base
.max_lod
;
1902 const float min_level
= sp_sview
->base
.u
.tex
.first_level
;
1903 const float max_level
= sp_sview
->base
.u
.tex
.last_level
;
1906 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1909 cl
= CLAMP(cl
, min_lod
, max_lod
);
1910 cl
= CLAMP(cl
, 0, max_level
- min_level
);
1916 * Get mip level relative to base level for linear mip filter
1919 mip_rel_level_linear(const struct sp_sampler_view
*sp_sview
,
1920 const struct sp_sampler
*sp_samp
,
1921 const float lod
[TGSI_QUAD_SIZE
],
1922 float level
[TGSI_QUAD_SIZE
])
1924 clamp_lod(sp_sview
, sp_samp
, lod
, level
);
1928 mip_filter_linear(const struct sp_sampler_view
*sp_sview
,
1929 const struct sp_sampler
*sp_samp
,
1930 img_filter_func min_filter
,
1931 img_filter_func mag_filter
,
1932 const float s
[TGSI_QUAD_SIZE
],
1933 const float t
[TGSI_QUAD_SIZE
],
1934 const float p
[TGSI_QUAD_SIZE
],
1935 const float c0
[TGSI_QUAD_SIZE
],
1936 const float lod_in
[TGSI_QUAD_SIZE
],
1937 const struct filter_args
*filt_args
,
1938 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1940 const struct pipe_sampler_view
*psview
= &sp_sview
->base
;
1942 float lod
[TGSI_QUAD_SIZE
];
1943 struct img_filter_args args
;
1945 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, filt_args
->control
, lod
);
1947 args
.offset
= filt_args
->offset
;
1948 args
.gather_only
= filt_args
->control
== TGSI_SAMPLER_GATHER
;
1949 args
.gather_comp
= get_gather_component(lod_in
);
1951 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1952 const int level0
= psview
->u
.tex
.first_level
+ (int)lod
[j
];
1957 args
.face_id
= filt_args
->faces
[j
];
1960 args
.level
= psview
->u
.tex
.first_level
;
1961 mag_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
1963 else if (level0
>= (int) psview
->u
.tex
.last_level
) {
1964 args
.level
= psview
->u
.tex
.last_level
;
1965 min_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
1968 float levelBlend
= frac(lod
[j
]);
1969 float rgbax
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
1972 args
.level
= level0
;
1973 min_filter(sp_sview
, sp_samp
, &args
, &rgbax
[0][0]);
1974 args
.level
= level0
+1;
1975 min_filter(sp_sview
, sp_samp
, &args
, &rgbax
[0][1]);
1977 for (c
= 0; c
< 4; c
++) {
1978 rgba
[c
][j
] = lerp(levelBlend
, rgbax
[c
][0], rgbax
[c
][1]);
1984 print_sample_4(__FUNCTION__
, rgba
);
1990 * Get mip level relative to base level for nearest mip filter
1993 mip_rel_level_nearest(const struct sp_sampler_view
*sp_sview
,
1994 const struct sp_sampler
*sp_samp
,
1995 const float lod
[TGSI_QUAD_SIZE
],
1996 float level
[TGSI_QUAD_SIZE
])
2000 clamp_lod(sp_sview
, sp_samp
, lod
, level
);
2001 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++)
2002 /* TODO: It should rather be:
2003 * level[j] = ceil(level[j] + 0.5F) - 1.0F;
2005 level
[j
] = (int)(level
[j
] + 0.5F
);
2009 * Compute nearest mipmap level from texcoords.
2010 * Then sample the texture level for four elements of a quad.
2011 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
2014 mip_filter_nearest(const struct sp_sampler_view
*sp_sview
,
2015 const struct sp_sampler
*sp_samp
,
2016 img_filter_func min_filter
,
2017 img_filter_func mag_filter
,
2018 const float s
[TGSI_QUAD_SIZE
],
2019 const float t
[TGSI_QUAD_SIZE
],
2020 const float p
[TGSI_QUAD_SIZE
],
2021 const float c0
[TGSI_QUAD_SIZE
],
2022 const float lod_in
[TGSI_QUAD_SIZE
],
2023 const struct filter_args
*filt_args
,
2024 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2026 const struct pipe_sampler_view
*psview
= &sp_sview
->base
;
2027 float lod
[TGSI_QUAD_SIZE
];
2029 struct img_filter_args args
;
2031 args
.offset
= filt_args
->offset
;
2032 args
.gather_only
= filt_args
->control
== TGSI_SAMPLER_GATHER
;
2033 args
.gather_comp
= get_gather_component(lod_in
);
2035 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, filt_args
->control
, lod
);
2037 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2041 args
.face_id
= filt_args
->faces
[j
];
2044 args
.level
= psview
->u
.tex
.first_level
;
2045 mag_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2047 const int level
= psview
->u
.tex
.first_level
+ (int)(lod
[j
] + 0.5F
);
2048 args
.level
= MIN2(level
, (int)psview
->u
.tex
.last_level
);
2049 min_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2054 print_sample_4(__FUNCTION__
, rgba
);
2060 * Get mip level relative to base level for none mip filter
2063 mip_rel_level_none(const struct sp_sampler_view
*sp_sview
,
2064 const struct sp_sampler
*sp_samp
,
2065 const float lod
[TGSI_QUAD_SIZE
],
2066 float level
[TGSI_QUAD_SIZE
])
2070 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2076 mip_filter_none(const struct sp_sampler_view
*sp_sview
,
2077 const struct sp_sampler
*sp_samp
,
2078 img_filter_func min_filter
,
2079 img_filter_func mag_filter
,
2080 const float s
[TGSI_QUAD_SIZE
],
2081 const float t
[TGSI_QUAD_SIZE
],
2082 const float p
[TGSI_QUAD_SIZE
],
2083 const float c0
[TGSI_QUAD_SIZE
],
2084 const float lod_in
[TGSI_QUAD_SIZE
],
2085 const struct filter_args
*filt_args
,
2086 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2088 float lod
[TGSI_QUAD_SIZE
];
2090 struct img_filter_args args
;
2092 args
.level
= sp_sview
->base
.u
.tex
.first_level
;
2093 args
.offset
= filt_args
->offset
;
2094 args
.gather_only
= filt_args
->control
== TGSI_SAMPLER_GATHER
;
2096 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, filt_args
->control
, lod
);
2098 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2102 args
.face_id
= filt_args
->faces
[j
];
2104 mag_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2107 min_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2114 * Get mip level relative to base level for none mip filter
2117 mip_rel_level_none_no_filter_select(const struct sp_sampler_view
*sp_sview
,
2118 const struct sp_sampler
*sp_samp
,
2119 const float lod
[TGSI_QUAD_SIZE
],
2120 float level
[TGSI_QUAD_SIZE
])
2122 mip_rel_level_none(sp_sview
, sp_samp
, lod
, level
);
2126 mip_filter_none_no_filter_select(const struct sp_sampler_view
*sp_sview
,
2127 const struct sp_sampler
*sp_samp
,
2128 img_filter_func min_filter
,
2129 img_filter_func mag_filter
,
2130 const float s
[TGSI_QUAD_SIZE
],
2131 const float t
[TGSI_QUAD_SIZE
],
2132 const float p
[TGSI_QUAD_SIZE
],
2133 const float c0
[TGSI_QUAD_SIZE
],
2134 const float lod_in
[TGSI_QUAD_SIZE
],
2135 const struct filter_args
*filt_args
,
2136 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2139 struct img_filter_args args
;
2140 args
.level
= sp_sview
->base
.u
.tex
.first_level
;
2141 args
.offset
= filt_args
->offset
;
2142 args
.gather_only
= filt_args
->control
== TGSI_SAMPLER_GATHER
;
2143 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2147 args
.face_id
= filt_args
->faces
[j
];
2148 mag_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2153 /* For anisotropic filtering */
2154 #define WEIGHT_LUT_SIZE 1024
2156 static const float *weightLut
= NULL
;
2159 * Creates the look-up table used to speed-up EWA sampling
2162 create_filter_table(void)
2166 float *lut
= (float *) MALLOC(WEIGHT_LUT_SIZE
* sizeof(float));
2168 for (i
= 0; i
< WEIGHT_LUT_SIZE
; ++i
) {
2169 const float alpha
= 2;
2170 const float r2
= (float) i
/ (float) (WEIGHT_LUT_SIZE
- 1);
2171 const float weight
= (float) exp(-alpha
* r2
);
2180 * Elliptical weighted average (EWA) filter for producing high quality
2181 * anisotropic filtered results.
2182 * Based on the Higher Quality Elliptical Weighted Average Filter
2183 * published by Paul S. Heckbert in his Master's Thesis
2184 * "Fundamentals of Texture Mapping and Image Warping" (1989)
2187 img_filter_2d_ewa(const struct sp_sampler_view
*sp_sview
,
2188 const struct sp_sampler
*sp_samp
,
2189 img_filter_func min_filter
,
2190 img_filter_func mag_filter
,
2191 const float s
[TGSI_QUAD_SIZE
],
2192 const float t
[TGSI_QUAD_SIZE
],
2193 const float p
[TGSI_QUAD_SIZE
],
2194 const uint faces
[TGSI_QUAD_SIZE
],
2195 const int8_t *offset
,
2197 const float dudx
, const float dvdx
,
2198 const float dudy
, const float dvdy
,
2199 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2201 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
2203 // ??? Won't the image filters blow up if level is negative?
2204 const unsigned level0
= level
> 0 ? level
: 0;
2205 const float scaling
= 1.0f
/ (1 << level0
);
2206 const int width
= u_minify(texture
->width0
, level0
);
2207 const int height
= u_minify(texture
->height0
, level0
);
2208 struct img_filter_args args
;
2209 const float ux
= dudx
* scaling
;
2210 const float vx
= dvdx
* scaling
;
2211 const float uy
= dudy
* scaling
;
2212 const float vy
= dvdy
* scaling
;
2214 /* compute ellipse coefficients to bound the region:
2215 * A*x*x + B*x*y + C*y*y = F.
2217 float A
= vx
*vx
+vy
*vy
+1;
2218 float B
= -2*(ux
*vx
+uy
*vy
);
2219 float C
= ux
*ux
+uy
*uy
+1;
2220 float F
= A
*C
-B
*B
/4.0f
;
2222 /* check if it is an ellipse */
2223 /* assert(F > 0.0); */
2225 /* Compute the ellipse's (u,v) bounding box in texture space */
2226 const float d
= -B
*B
+4.0f
*C
*A
;
2227 const float box_u
= 2.0f
/ d
* sqrtf(d
*C
*F
); /* box_u -> half of bbox with */
2228 const float box_v
= 2.0f
/ d
* sqrtf(A
*d
*F
); /* box_v -> half of bbox height */
2230 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2231 float s_buffer
[TGSI_QUAD_SIZE
];
2232 float t_buffer
[TGSI_QUAD_SIZE
];
2233 float weight_buffer
[TGSI_QUAD_SIZE
];
2236 /* For each quad, the du and dx values are the same and so the ellipse is
2237 * also the same. Note that texel/image access can only be performed using
2238 * a quad, i.e. it is not possible to get the pixel value for a single
2239 * tex coord. In order to have a better performance, the access is buffered
2240 * using the s_buffer/t_buffer and weight_buffer. Only when the buffer is
2241 * full, then the pixel values are read from the image.
2243 const float ddq
= 2 * A
;
2245 /* Scale ellipse formula to directly index the Filter Lookup Table.
2246 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
2248 const double formScale
= (double) (WEIGHT_LUT_SIZE
- 1) / F
;
2252 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
2255 args
.offset
= offset
;
2257 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2258 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
2259 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
2260 * value, q, is less than F, we're inside the ellipse
2262 const float tex_u
= -0.5F
+ s
[j
] * texture
->width0
* scaling
;
2263 const float tex_v
= -0.5F
+ t
[j
] * texture
->height0
* scaling
;
2265 const int u0
= (int) floorf(tex_u
- box_u
);
2266 const int u1
= (int) ceilf(tex_u
+ box_u
);
2267 const int v0
= (int) floorf(tex_v
- box_v
);
2268 const int v1
= (int) ceilf(tex_v
+ box_v
);
2269 const float U
= u0
- tex_u
;
2271 float num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
2272 unsigned buffer_next
= 0;
2275 args
.face_id
= faces
[j
];
2277 for (v
= v0
; v
<= v1
; ++v
) {
2278 const float V
= v
- tex_v
;
2279 float dq
= A
* (2 * U
+ 1) + B
* V
;
2280 float q
= (C
* V
+ B
* U
) * V
+ A
* U
* U
;
2283 for (u
= u0
; u
<= u1
; ++u
) {
2284 /* Note that the ellipse has been pre-scaled so F =
2285 * WEIGHT_LUT_SIZE - 1
2287 if (q
< WEIGHT_LUT_SIZE
) {
2288 /* as a LUT is used, q must never be negative;
2289 * should not happen, though
2291 const int qClamped
= q
>= 0.0F
? q
: 0;
2292 const float weight
= weightLut
[qClamped
];
2294 weight_buffer
[buffer_next
] = weight
;
2295 s_buffer
[buffer_next
] = u
/ ((float) width
);
2296 t_buffer
[buffer_next
] = v
/ ((float) height
);
2299 if (buffer_next
== TGSI_QUAD_SIZE
) {
2300 /* 4 texel coords are in the buffer -> read it now */
2302 /* it is assumed that samp->min_img_filter is set to
2303 * img_filter_2d_nearest or one of the
2304 * accelerated img_filter_2d_nearest_XXX functions.
2306 for (jj
= 0; jj
< buffer_next
; jj
++) {
2307 args
.s
= s_buffer
[jj
];
2308 args
.t
= t_buffer
[jj
];
2310 min_filter(sp_sview
, sp_samp
, &args
, &rgba_temp
[0][jj
]);
2311 num
[0] += weight_buffer
[jj
] * rgba_temp
[0][jj
];
2312 num
[1] += weight_buffer
[jj
] * rgba_temp
[1][jj
];
2313 num
[2] += weight_buffer
[jj
] * rgba_temp
[2][jj
];
2314 num
[3] += weight_buffer
[jj
] * rgba_temp
[3][jj
];
2327 /* if the tex coord buffer contains unread values, we will read
2330 if (buffer_next
> 0) {
2332 /* it is assumed that samp->min_img_filter is set to
2333 * img_filter_2d_nearest or one of the
2334 * accelerated img_filter_2d_nearest_XXX functions.
2336 for (jj
= 0; jj
< buffer_next
; jj
++) {
2337 args
.s
= s_buffer
[jj
];
2338 args
.t
= t_buffer
[jj
];
2340 min_filter(sp_sview
, sp_samp
, &args
, &rgba_temp
[0][jj
]);
2341 num
[0] += weight_buffer
[jj
] * rgba_temp
[0][jj
];
2342 num
[1] += weight_buffer
[jj
] * rgba_temp
[1][jj
];
2343 num
[2] += weight_buffer
[jj
] * rgba_temp
[2][jj
];
2344 num
[3] += weight_buffer
[jj
] * rgba_temp
[3][jj
];
2349 /* Reaching this place would mean that no pixels intersected
2350 * the ellipse. This should never happen because the filter
2351 * we use always intersects at least one pixel.
2358 /* not enough pixels in resampling, resort to direct interpolation */
2362 min_filter(sp_sview
, sp_samp
, &args
, &rgba_temp
[0][j
]);
2364 num
[0] = rgba_temp
[0][j
];
2365 num
[1] = rgba_temp
[1][j
];
2366 num
[2] = rgba_temp
[2][j
];
2367 num
[3] = rgba_temp
[3][j
];
2370 rgba
[0][j
] = num
[0] / den
;
2371 rgba
[1][j
] = num
[1] / den
;
2372 rgba
[2][j
] = num
[2] / den
;
2373 rgba
[3][j
] = num
[3] / den
;
2379 * Get mip level relative to base level for linear mip filter
2382 mip_rel_level_linear_aniso(const struct sp_sampler_view
*sp_sview
,
2383 const struct sp_sampler
*sp_samp
,
2384 const float lod
[TGSI_QUAD_SIZE
],
2385 float level
[TGSI_QUAD_SIZE
])
2387 mip_rel_level_linear(sp_sview
, sp_samp
, lod
, level
);
2391 * Sample 2D texture using an anisotropic filter.
2394 mip_filter_linear_aniso(const struct sp_sampler_view
*sp_sview
,
2395 const struct sp_sampler
*sp_samp
,
2396 img_filter_func min_filter
,
2397 img_filter_func mag_filter
,
2398 const float s
[TGSI_QUAD_SIZE
],
2399 const float t
[TGSI_QUAD_SIZE
],
2400 const float p
[TGSI_QUAD_SIZE
],
2401 const float c0
[TGSI_QUAD_SIZE
],
2402 const float lod_in
[TGSI_QUAD_SIZE
],
2403 const struct filter_args
*filt_args
,
2404 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2406 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
2407 const struct pipe_sampler_view
*psview
= &sp_sview
->base
;
2410 float lod
[TGSI_QUAD_SIZE
];
2412 const float s_to_u
= u_minify(texture
->width0
, psview
->u
.tex
.first_level
);
2413 const float t_to_v
= u_minify(texture
->height0
, psview
->u
.tex
.first_level
);
2414 const float dudx
= (s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]) * s_to_u
;
2415 const float dudy
= (s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]) * s_to_u
;
2416 const float dvdx
= (t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]) * t_to_v
;
2417 const float dvdy
= (t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]) * t_to_v
;
2418 struct img_filter_args args
;
2420 args
.offset
= filt_args
->offset
;
2422 if (filt_args
->control
== TGSI_SAMPLER_LOD_BIAS
||
2423 filt_args
->control
== TGSI_SAMPLER_LOD_NONE
||
2425 filt_args
->control
== TGSI_SAMPLER_DERIVS_EXPLICIT
) {
2426 /* note: instead of working with Px and Py, we will use the
2427 * squared length instead, to avoid sqrt.
2429 const float Px2
= dudx
* dudx
+ dvdx
* dvdx
;
2430 const float Py2
= dudy
* dudy
+ dvdy
* dvdy
;
2435 const float maxEccentricity
= sp_samp
->base
.max_anisotropy
* sp_samp
->base
.max_anisotropy
;
2446 /* if the eccentricity of the ellipse is too big, scale up the shorter
2447 * of the two vectors to limit the maximum amount of work per pixel
2450 if (e
> maxEccentricity
) {
2451 /* float s=e / maxEccentricity;
2455 Pmin2
= Pmax2
/ maxEccentricity
;
2458 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
2459 * this since 0.5*log(x) = log(sqrt(x))
2461 lambda
= 0.5F
* util_fast_log2(Pmin2
) + sp_samp
->base
.lod_bias
;
2462 compute_lod(&sp_samp
->base
, filt_args
->control
, lambda
, lod_in
, lod
);
2465 assert(filt_args
->control
== TGSI_SAMPLER_LOD_EXPLICIT
||
2466 filt_args
->control
== TGSI_SAMPLER_LOD_ZERO
);
2467 compute_lod(&sp_samp
->base
, filt_args
->control
, sp_samp
->base
.lod_bias
, lod_in
, lod
);
2470 /* XXX: Take into account all lod values.
2473 level0
= psview
->u
.tex
.first_level
+ (int)lambda
;
2475 /* If the ellipse covers the whole image, we can
2476 * simply return the average of the whole image.
2478 if (level0
>= (int) psview
->u
.tex
.last_level
) {
2480 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2484 args
.level
= psview
->u
.tex
.last_level
;
2485 args
.face_id
= filt_args
->faces
[j
];
2487 * XXX: we overwrote any linear filter with nearest, so this
2488 * isn't right (albeit if last level is 1x1 and no border it
2489 * will work just the same).
2491 min_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2495 /* don't bother interpolating between multiple LODs; it doesn't
2496 * seem to be worth the extra running time.
2498 img_filter_2d_ewa(sp_sview
, sp_samp
, min_filter
, mag_filter
,
2499 s
, t
, p
, filt_args
->faces
, filt_args
->offset
,
2500 level0
, dudx
, dvdx
, dudy
, dvdy
, rgba
);
2504 print_sample_4(__FUNCTION__
, rgba
);
2509 * Get mip level relative to base level for linear mip filter
2512 mip_rel_level_linear_2d_linear_repeat_POT(
2513 const struct sp_sampler_view
*sp_sview
,
2514 const struct sp_sampler
*sp_samp
,
2515 const float lod
[TGSI_QUAD_SIZE
],
2516 float level
[TGSI_QUAD_SIZE
])
2518 mip_rel_level_linear(sp_sview
, sp_samp
, lod
, level
);
2522 * Specialized version of mip_filter_linear with hard-wired calls to
2523 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
2526 mip_filter_linear_2d_linear_repeat_POT(
2527 const struct sp_sampler_view
*sp_sview
,
2528 const struct sp_sampler
*sp_samp
,
2529 img_filter_func min_filter
,
2530 img_filter_func mag_filter
,
2531 const float s
[TGSI_QUAD_SIZE
],
2532 const float t
[TGSI_QUAD_SIZE
],
2533 const float p
[TGSI_QUAD_SIZE
],
2534 const float c0
[TGSI_QUAD_SIZE
],
2535 const float lod_in
[TGSI_QUAD_SIZE
],
2536 const struct filter_args
*filt_args
,
2537 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2539 const struct pipe_sampler_view
*psview
= &sp_sview
->base
;
2541 float lod
[TGSI_QUAD_SIZE
];
2543 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, filt_args
->control
, lod
);
2545 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2546 const int level0
= psview
->u
.tex
.first_level
+ (int)lod
[j
];
2547 struct img_filter_args args
;
2548 /* Catches both negative and large values of level0:
2553 args
.face_id
= filt_args
->faces
[j
];
2554 args
.offset
= filt_args
->offset
;
2555 args
.gather_only
= filt_args
->control
== TGSI_SAMPLER_GATHER
;
2556 if ((unsigned)level0
>= psview
->u
.tex
.last_level
) {
2558 args
.level
= psview
->u
.tex
.first_level
;
2560 args
.level
= psview
->u
.tex
.last_level
;
2561 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, &args
,
2566 const float levelBlend
= frac(lod
[j
]);
2567 float rgbax
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2570 args
.level
= level0
;
2571 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, &args
, &rgbax
[0][0]);
2572 args
.level
= level0
+1;
2573 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, &args
, &rgbax
[0][1]);
2575 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
2576 rgba
[c
][j
] = lerp(levelBlend
, rgbax
[c
][0], rgbax
[c
][1]);
2581 print_sample_4(__FUNCTION__
, rgba
);
2585 static const struct sp_filter_funcs funcs_linear
= {
2586 mip_rel_level_linear
,
2590 static const struct sp_filter_funcs funcs_nearest
= {
2591 mip_rel_level_nearest
,
2595 static const struct sp_filter_funcs funcs_none
= {
2600 static const struct sp_filter_funcs funcs_none_no_filter_select
= {
2601 mip_rel_level_none_no_filter_select
,
2602 mip_filter_none_no_filter_select
2605 static const struct sp_filter_funcs funcs_linear_aniso
= {
2606 mip_rel_level_linear_aniso
,
2607 mip_filter_linear_aniso
2610 static const struct sp_filter_funcs funcs_linear_2d_linear_repeat_POT
= {
2611 mip_rel_level_linear_2d_linear_repeat_POT
,
2612 mip_filter_linear_2d_linear_repeat_POT
2616 * Do shadow/depth comparisons.
2619 sample_compare(const struct sp_sampler_view
*sp_sview
,
2620 const struct sp_sampler
*sp_samp
,
2621 const float s
[TGSI_QUAD_SIZE
],
2622 const float t
[TGSI_QUAD_SIZE
],
2623 const float p
[TGSI_QUAD_SIZE
],
2624 const float c0
[TGSI_QUAD_SIZE
],
2625 const float c1
[TGSI_QUAD_SIZE
],
2626 enum tgsi_sampler_control control
,
2627 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2629 const struct pipe_sampler_state
*sampler
= &sp_samp
->base
;
2631 int k
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2633 const struct util_format_description
*format_desc
=
2634 util_format_description(sp_sview
->base
.format
);
2635 /* not entirely sure we couldn't end up with non-valid swizzle here */
2636 const unsigned chan_type
=
2637 format_desc
->swizzle
[0] <= PIPE_SWIZZLE_W
?
2638 format_desc
->channel
[format_desc
->swizzle
[0]].type
:
2639 UTIL_FORMAT_TYPE_FLOAT
;
2640 const bool is_gather
= (control
== TGSI_SAMPLER_GATHER
);
2643 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
2644 * for 2D Array texture we need to use the 'c0' (aka Q).
2645 * When we sampled the depth texture, the depth value was put into all
2646 * RGBA channels. We look at the red channel here.
2649 if (sp_sview
->base
.target
== PIPE_TEXTURE_2D_ARRAY
||
2650 sp_sview
->base
.target
== PIPE_TEXTURE_CUBE
) {
2655 } else if (sp_sview
->base
.target
== PIPE_TEXTURE_CUBE_ARRAY
) {
2667 if (chan_type
!= UTIL_FORMAT_TYPE_FLOAT
) {
2669 * clamping is a result of conversion to texture format, hence
2670 * doesn't happen with floats. Technically also should do comparison
2671 * in texture format (quantization!).
2673 pc
[0] = CLAMP(pc
[0], 0.0F
, 1.0F
);
2674 pc
[1] = CLAMP(pc
[1], 0.0F
, 1.0F
);
2675 pc
[2] = CLAMP(pc
[2], 0.0F
, 1.0F
);
2676 pc
[3] = CLAMP(pc
[3], 0.0F
, 1.0F
);
2679 for (v
= 0; v
< (is_gather
? TGSI_NUM_CHANNELS
: 1); v
++) {
2680 /* compare four texcoords vs. four texture samples */
2681 switch (sampler
->compare_func
) {
2682 case PIPE_FUNC_LESS
:
2683 k
[v
][0] = pc
[0] < rgba
[v
][0];
2684 k
[v
][1] = pc
[1] < rgba
[v
][1];
2685 k
[v
][2] = pc
[2] < rgba
[v
][2];
2686 k
[v
][3] = pc
[3] < rgba
[v
][3];
2688 case PIPE_FUNC_LEQUAL
:
2689 k
[v
][0] = pc
[0] <= rgba
[v
][0];
2690 k
[v
][1] = pc
[1] <= rgba
[v
][1];
2691 k
[v
][2] = pc
[2] <= rgba
[v
][2];
2692 k
[v
][3] = pc
[3] <= rgba
[v
][3];
2694 case PIPE_FUNC_GREATER
:
2695 k
[v
][0] = pc
[0] > rgba
[v
][0];
2696 k
[v
][1] = pc
[1] > rgba
[v
][1];
2697 k
[v
][2] = pc
[2] > rgba
[v
][2];
2698 k
[v
][3] = pc
[3] > rgba
[v
][3];
2700 case PIPE_FUNC_GEQUAL
:
2701 k
[v
][0] = pc
[0] >= rgba
[v
][0];
2702 k
[v
][1] = pc
[1] >= rgba
[v
][1];
2703 k
[v
][2] = pc
[2] >= rgba
[v
][2];
2704 k
[v
][3] = pc
[3] >= rgba
[v
][3];
2706 case PIPE_FUNC_EQUAL
:
2707 k
[v
][0] = pc
[0] == rgba
[v
][0];
2708 k
[v
][1] = pc
[1] == rgba
[v
][1];
2709 k
[v
][2] = pc
[2] == rgba
[v
][2];
2710 k
[v
][3] = pc
[3] == rgba
[v
][3];
2712 case PIPE_FUNC_NOTEQUAL
:
2713 k
[v
][0] = pc
[0] != rgba
[v
][0];
2714 k
[v
][1] = pc
[1] != rgba
[v
][1];
2715 k
[v
][2] = pc
[2] != rgba
[v
][2];
2716 k
[v
][3] = pc
[3] != rgba
[v
][3];
2718 case PIPE_FUNC_ALWAYS
:
2719 k
[v
][0] = k
[v
][1] = k
[v
][2] = k
[v
][3] = 1;
2721 case PIPE_FUNC_NEVER
:
2722 k
[v
][0] = k
[v
][1] = k
[v
][2] = k
[v
][3] = 0;
2725 k
[v
][0] = k
[v
][1] = k
[v
][2] = k
[v
][3] = 0;
2732 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2733 for (v
= 0; v
< TGSI_NUM_CHANNELS
; v
++) {
2734 rgba
[v
][j
] = k
[v
][j
];
2738 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2739 rgba
[0][j
] = k
[0][j
];
2740 rgba
[1][j
] = k
[0][j
];
2741 rgba
[2][j
] = k
[0][j
];
2748 do_swizzling(const struct pipe_sampler_view
*sview
,
2749 float in
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
],
2750 float out
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2753 const unsigned swizzle_r
= sview
->swizzle_r
;
2754 const unsigned swizzle_g
= sview
->swizzle_g
;
2755 const unsigned swizzle_b
= sview
->swizzle_b
;
2756 const unsigned swizzle_a
= sview
->swizzle_a
;
2757 float oneval
= util_format_is_pure_integer(sview
->format
) ? uif(1) : 1.0f
;
2759 switch (swizzle_r
) {
2760 case PIPE_SWIZZLE_0
:
2761 for (j
= 0; j
< 4; j
++)
2764 case PIPE_SWIZZLE_1
:
2765 for (j
= 0; j
< 4; j
++)
2769 assert(swizzle_r
< 4);
2770 for (j
= 0; j
< 4; j
++)
2771 out
[0][j
] = in
[swizzle_r
][j
];
2774 switch (swizzle_g
) {
2775 case PIPE_SWIZZLE_0
:
2776 for (j
= 0; j
< 4; j
++)
2779 case PIPE_SWIZZLE_1
:
2780 for (j
= 0; j
< 4; j
++)
2784 assert(swizzle_g
< 4);
2785 for (j
= 0; j
< 4; j
++)
2786 out
[1][j
] = in
[swizzle_g
][j
];
2789 switch (swizzle_b
) {
2790 case PIPE_SWIZZLE_0
:
2791 for (j
= 0; j
< 4; j
++)
2794 case PIPE_SWIZZLE_1
:
2795 for (j
= 0; j
< 4; j
++)
2799 assert(swizzle_b
< 4);
2800 for (j
= 0; j
< 4; j
++)
2801 out
[2][j
] = in
[swizzle_b
][j
];
2804 switch (swizzle_a
) {
2805 case PIPE_SWIZZLE_0
:
2806 for (j
= 0; j
< 4; j
++)
2809 case PIPE_SWIZZLE_1
:
2810 for (j
= 0; j
< 4; j
++)
2814 assert(swizzle_a
< 4);
2815 for (j
= 0; j
< 4; j
++)
2816 out
[3][j
] = in
[swizzle_a
][j
];
2821 static wrap_nearest_func
2822 get_nearest_unorm_wrap(unsigned mode
)
2825 case PIPE_TEX_WRAP_CLAMP
:
2826 return wrap_nearest_unorm_clamp
;
2827 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2828 return wrap_nearest_unorm_clamp_to_edge
;
2829 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2830 return wrap_nearest_unorm_clamp_to_border
;
2832 debug_printf("illegal wrap mode %d with non-normalized coords\n", mode
);
2833 return wrap_nearest_unorm_clamp
;
2838 static wrap_nearest_func
2839 get_nearest_wrap(unsigned mode
)
2842 case PIPE_TEX_WRAP_REPEAT
:
2843 return wrap_nearest_repeat
;
2844 case PIPE_TEX_WRAP_CLAMP
:
2845 return wrap_nearest_clamp
;
2846 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2847 return wrap_nearest_clamp_to_edge
;
2848 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2849 return wrap_nearest_clamp_to_border
;
2850 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2851 return wrap_nearest_mirror_repeat
;
2852 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2853 return wrap_nearest_mirror_clamp
;
2854 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2855 return wrap_nearest_mirror_clamp_to_edge
;
2856 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2857 return wrap_nearest_mirror_clamp_to_border
;
2860 return wrap_nearest_repeat
;
2865 static wrap_linear_func
2866 get_linear_unorm_wrap(unsigned mode
)
2869 case PIPE_TEX_WRAP_CLAMP
:
2870 return wrap_linear_unorm_clamp
;
2871 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2872 return wrap_linear_unorm_clamp_to_edge
;
2873 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2874 return wrap_linear_unorm_clamp_to_border
;
2876 debug_printf("illegal wrap mode %d with non-normalized coords\n", mode
);
2877 return wrap_linear_unorm_clamp
;
2882 static wrap_linear_func
2883 get_linear_wrap(unsigned mode
)
2886 case PIPE_TEX_WRAP_REPEAT
:
2887 return wrap_linear_repeat
;
2888 case PIPE_TEX_WRAP_CLAMP
:
2889 return wrap_linear_clamp
;
2890 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2891 return wrap_linear_clamp_to_edge
;
2892 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2893 return wrap_linear_clamp_to_border
;
2894 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2895 return wrap_linear_mirror_repeat
;
2896 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2897 return wrap_linear_mirror_clamp
;
2898 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2899 return wrap_linear_mirror_clamp_to_edge
;
2900 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2901 return wrap_linear_mirror_clamp_to_border
;
2904 return wrap_linear_repeat
;
2910 * Is swizzling needed for the given state key?
2913 any_swizzle(const struct pipe_sampler_view
*view
)
2915 return (view
->swizzle_r
!= PIPE_SWIZZLE_X
||
2916 view
->swizzle_g
!= PIPE_SWIZZLE_Y
||
2917 view
->swizzle_b
!= PIPE_SWIZZLE_Z
||
2918 view
->swizzle_a
!= PIPE_SWIZZLE_W
);
2922 static img_filter_func
2923 get_img_filter(const struct sp_sampler_view
*sp_sview
,
2924 const struct pipe_sampler_state
*sampler
,
2925 unsigned filter
, bool gather
)
2927 switch (sp_sview
->base
.target
) {
2929 case PIPE_TEXTURE_1D
:
2930 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2931 return img_filter_1d_nearest
;
2933 return img_filter_1d_linear
;
2935 case PIPE_TEXTURE_1D_ARRAY
:
2936 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2937 return img_filter_1d_array_nearest
;
2939 return img_filter_1d_array_linear
;
2941 case PIPE_TEXTURE_2D
:
2942 case PIPE_TEXTURE_RECT
:
2943 /* Try for fast path:
2945 if (!gather
&& sp_sview
->pot2d
&&
2946 sampler
->wrap_s
== sampler
->wrap_t
&&
2947 sampler
->normalized_coords
)
2949 switch (sampler
->wrap_s
) {
2950 case PIPE_TEX_WRAP_REPEAT
:
2952 case PIPE_TEX_FILTER_NEAREST
:
2953 return img_filter_2d_nearest_repeat_POT
;
2954 case PIPE_TEX_FILTER_LINEAR
:
2955 return img_filter_2d_linear_repeat_POT
;
2960 case PIPE_TEX_WRAP_CLAMP
:
2962 case PIPE_TEX_FILTER_NEAREST
:
2963 return img_filter_2d_nearest_clamp_POT
;
2969 /* Otherwise use default versions:
2971 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2972 return img_filter_2d_nearest
;
2974 return img_filter_2d_linear
;
2976 case PIPE_TEXTURE_2D_ARRAY
:
2977 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2978 return img_filter_2d_array_nearest
;
2980 return img_filter_2d_array_linear
;
2982 case PIPE_TEXTURE_CUBE
:
2983 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2984 return img_filter_cube_nearest
;
2986 return img_filter_cube_linear
;
2988 case PIPE_TEXTURE_CUBE_ARRAY
:
2989 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2990 return img_filter_cube_array_nearest
;
2992 return img_filter_cube_array_linear
;
2994 case PIPE_TEXTURE_3D
:
2995 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2996 return img_filter_3d_nearest
;
2998 return img_filter_3d_linear
;
3002 return img_filter_1d_nearest
;
3007 * Get mip filter funcs, and optionally both img min filter and img mag
3008 * filter. Note that both img filter function pointers must be either non-NULL
3012 get_filters(const struct sp_sampler_view
*sp_sview
,
3013 const struct sp_sampler
*sp_samp
,
3014 const enum tgsi_sampler_control control
,
3015 const struct sp_filter_funcs
**funcs
,
3016 img_filter_func
*min
,
3017 img_filter_func
*mag
)
3020 if (control
== TGSI_SAMPLER_GATHER
) {
3021 *funcs
= &funcs_nearest
;
3023 *min
= get_img_filter(sp_sview
, &sp_samp
->base
,
3024 PIPE_TEX_FILTER_LINEAR
, true);
3026 } else if (sp_sview
->pot2d
& sp_samp
->min_mag_equal_repeat_linear
) {
3027 *funcs
= &funcs_linear_2d_linear_repeat_POT
;
3029 *funcs
= sp_samp
->filter_funcs
;
3032 *min
= get_img_filter(sp_sview
, &sp_samp
->base
,
3033 sp_samp
->min_img_filter
, false);
3034 if (sp_samp
->min_mag_equal
) {
3037 *mag
= get_img_filter(sp_sview
, &sp_samp
->base
,
3038 sp_samp
->base
.mag_img_filter
, false);
3045 sample_mip(const struct sp_sampler_view
*sp_sview
,
3046 const struct sp_sampler
*sp_samp
,
3047 const float s
[TGSI_QUAD_SIZE
],
3048 const float t
[TGSI_QUAD_SIZE
],
3049 const float p
[TGSI_QUAD_SIZE
],
3050 const float c0
[TGSI_QUAD_SIZE
],
3051 const float lod
[TGSI_QUAD_SIZE
],
3052 const struct filter_args
*filt_args
,
3053 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3055 const struct sp_filter_funcs
*funcs
= NULL
;
3056 img_filter_func min_img_filter
= NULL
;
3057 img_filter_func mag_img_filter
= NULL
;
3059 get_filters(sp_sview
, sp_samp
, filt_args
->control
,
3060 &funcs
, &min_img_filter
, &mag_img_filter
);
3062 funcs
->filter(sp_sview
, sp_samp
, min_img_filter
, mag_img_filter
,
3063 s
, t
, p
, c0
, lod
, filt_args
, rgba
);
3065 if (sp_samp
->base
.compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
3066 sample_compare(sp_sview
, sp_samp
, s
, t
, p
, c0
,
3067 lod
, filt_args
->control
, rgba
);
3070 if (sp_sview
->need_swizzle
&& filt_args
->control
!= TGSI_SAMPLER_GATHER
) {
3071 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
3072 memcpy(rgba_temp
, rgba
, sizeof(rgba_temp
));
3073 do_swizzling(&sp_sview
->base
, rgba_temp
, rgba
);
3080 * This function uses cube texture coordinates to choose a face of a cube and
3081 * computes the 2D cube face coordinates. Puts face info into the sampler
3085 convert_cube(const struct sp_sampler_view
*sp_sview
,
3086 const struct sp_sampler
*sp_samp
,
3087 const float s
[TGSI_QUAD_SIZE
],
3088 const float t
[TGSI_QUAD_SIZE
],
3089 const float p
[TGSI_QUAD_SIZE
],
3090 const float c0
[TGSI_QUAD_SIZE
],
3091 float ssss
[TGSI_QUAD_SIZE
],
3092 float tttt
[TGSI_QUAD_SIZE
],
3093 float pppp
[TGSI_QUAD_SIZE
],
3094 uint faces
[TGSI_QUAD_SIZE
])
3104 direction target sc tc ma
3105 ---------- ------------------------------- --- --- ---
3106 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
3107 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
3108 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
3109 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
3110 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
3111 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
3114 /* Choose the cube face and compute new s/t coords for the 2D face.
3116 * Use the same cube face for all four pixels in the quad.
3118 * This isn't ideal, but if we want to use a different cube face
3119 * per pixel in the quad, we'd have to also compute the per-face
3120 * LOD here too. That's because the four post-face-selection
3121 * texcoords are no longer related to each other (they're
3122 * per-face!) so we can't use subtraction to compute the partial
3123 * deriviates to compute the LOD. Doing so (near cube edges
3124 * anyway) gives us pretty much random values.
3127 /* use the average of the four pixel's texcoords to choose the face */
3128 const float rx
= 0.25F
* (s
[0] + s
[1] + s
[2] + s
[3]);
3129 const float ry
= 0.25F
* (t
[0] + t
[1] + t
[2] + t
[3]);
3130 const float rz
= 0.25F
* (p
[0] + p
[1] + p
[2] + p
[3]);
3131 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
3133 if (arx
>= ary
&& arx
>= arz
) {
3134 const float sign
= (rx
>= 0.0F
) ? 1.0F
: -1.0F
;
3135 const uint face
= (rx
>= 0.0F
) ?
3136 PIPE_TEX_FACE_POS_X
: PIPE_TEX_FACE_NEG_X
;
3137 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3138 const float ima
= -0.5F
/ fabsf(s
[j
]);
3139 ssss
[j
] = sign
* p
[j
] * ima
+ 0.5F
;
3140 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
3144 else if (ary
>= arx
&& ary
>= arz
) {
3145 const float sign
= (ry
>= 0.0F
) ? 1.0F
: -1.0F
;
3146 const uint face
= (ry
>= 0.0F
) ?
3147 PIPE_TEX_FACE_POS_Y
: PIPE_TEX_FACE_NEG_Y
;
3148 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3149 const float ima
= -0.5F
/ fabsf(t
[j
]);
3150 ssss
[j
] = -s
[j
] * ima
+ 0.5F
;
3151 tttt
[j
] = sign
* -p
[j
] * ima
+ 0.5F
;
3156 const float sign
= (rz
>= 0.0F
) ? 1.0F
: -1.0F
;
3157 const uint face
= (rz
>= 0.0F
) ?
3158 PIPE_TEX_FACE_POS_Z
: PIPE_TEX_FACE_NEG_Z
;
3159 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3160 const float ima
= -0.5F
/ fabsf(p
[j
]);
3161 ssss
[j
] = sign
* -s
[j
] * ima
+ 0.5F
;
3162 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
3171 sp_get_dims(const struct sp_sampler_view
*sp_sview
,
3175 const struct pipe_sampler_view
*view
= &sp_sview
->base
;
3176 const struct pipe_resource
*texture
= view
->texture
;
3178 if (view
->target
== PIPE_BUFFER
) {
3179 dims
[0] = view
->u
.buf
.size
/ util_format_get_blocksize(view
->format
);
3180 /* the other values are undefined, but let's avoid potential valgrind
3183 dims
[1] = dims
[2] = dims
[3] = 0;
3187 /* undefined according to EXT_gpu_program */
3188 level
+= view
->u
.tex
.first_level
;
3189 if (level
> view
->u
.tex
.last_level
)
3192 dims
[3] = view
->u
.tex
.last_level
- view
->u
.tex
.first_level
+ 1;
3193 dims
[0] = u_minify(texture
->width0
, level
);
3195 switch (view
->target
) {
3196 case PIPE_TEXTURE_1D_ARRAY
:
3197 dims
[1] = view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
3199 case PIPE_TEXTURE_1D
:
3201 case PIPE_TEXTURE_2D_ARRAY
:
3202 dims
[2] = view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
3204 case PIPE_TEXTURE_2D
:
3205 case PIPE_TEXTURE_CUBE
:
3206 case PIPE_TEXTURE_RECT
:
3207 dims
[1] = u_minify(texture
->height0
, level
);
3209 case PIPE_TEXTURE_3D
:
3210 dims
[1] = u_minify(texture
->height0
, level
);
3211 dims
[2] = u_minify(texture
->depth0
, level
);
3213 case PIPE_TEXTURE_CUBE_ARRAY
:
3214 dims
[1] = u_minify(texture
->height0
, level
);
3215 dims
[2] = (view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1) / 6;
3218 assert(!"unexpected texture target in sp_get_dims()");
3224 * This function is only used for getting unfiltered texels via the
3225 * TXF opcode. The GL spec says that out-of-bounds texel fetches
3226 * produce undefined results. Instead of crashing, lets just clamp
3227 * coords to the texture image size.
3230 sp_get_texels(const struct sp_sampler_view
*sp_sview
,
3231 const int v_i
[TGSI_QUAD_SIZE
],
3232 const int v_j
[TGSI_QUAD_SIZE
],
3233 const int v_k
[TGSI_QUAD_SIZE
],
3234 const int lod
[TGSI_QUAD_SIZE
],
3235 const int8_t offset
[3],
3236 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3238 union tex_tile_address addr
;
3239 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
3242 /* TODO write a better test for LOD */
3243 const unsigned level
=
3244 sp_sview
->base
.target
== PIPE_BUFFER
? 0 :
3245 CLAMP(lod
[0] + sp_sview
->base
.u
.tex
.first_level
,
3246 sp_sview
->base
.u
.tex
.first_level
,
3247 sp_sview
->base
.u
.tex
.last_level
);
3248 const int width
= u_minify(texture
->width0
, level
);
3249 const int height
= u_minify(texture
->height0
, level
);
3250 const int depth
= u_minify(texture
->depth0
, level
);
3251 unsigned elem_size
, first_element
, last_element
;
3254 addr
.bits
.level
= level
;
3256 switch (sp_sview
->base
.target
) {
3258 elem_size
= util_format_get_blocksize(sp_sview
->base
.format
);
3259 first_element
= sp_sview
->base
.u
.buf
.offset
/ elem_size
;
3260 last_element
= (sp_sview
->base
.u
.buf
.offset
+
3261 sp_sview
->base
.u
.buf
.size
) / elem_size
- 1;
3262 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3263 const int x
= CLAMP(v_i
[j
] + offset
[0] +
3267 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
, 0);
3268 for (c
= 0; c
< 4; c
++) {
3273 case PIPE_TEXTURE_1D
:
3274 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3275 const int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3276 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
,
3277 sp_sview
->base
.u
.tex
.first_layer
);
3278 for (c
= 0; c
< 4; c
++) {
3283 case PIPE_TEXTURE_1D_ARRAY
:
3284 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3285 const int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3286 const int y
= CLAMP(v_j
[j
], sp_sview
->base
.u
.tex
.first_layer
,
3287 sp_sview
->base
.u
.tex
.last_layer
);
3288 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
, y
);
3289 for (c
= 0; c
< 4; c
++) {
3294 case PIPE_TEXTURE_2D
:
3295 case PIPE_TEXTURE_RECT
:
3296 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3297 const int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3298 const int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
3299 tx
= get_texel_3d_no_border(sp_sview
, addr
, x
, y
,
3300 sp_sview
->base
.u
.tex
.first_layer
);
3301 for (c
= 0; c
< 4; c
++) {
3306 case PIPE_TEXTURE_2D_ARRAY
:
3307 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3308 const int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3309 const int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
3310 const int layer
= CLAMP(v_k
[j
], sp_sview
->base
.u
.tex
.first_layer
,
3311 sp_sview
->base
.u
.tex
.last_layer
);
3312 tx
= get_texel_3d_no_border(sp_sview
, addr
, x
, y
, layer
);
3313 for (c
= 0; c
< 4; c
++) {
3318 case PIPE_TEXTURE_3D
:
3319 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3320 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3321 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
3322 int z
= CLAMP(v_k
[j
] + offset
[2], 0, depth
- 1);
3323 tx
= get_texel_3d_no_border(sp_sview
, addr
, x
, y
, z
);
3324 for (c
= 0; c
< 4; c
++) {
3329 case PIPE_TEXTURE_CUBE
: /* TXF can't work on CUBE according to spec */
3330 case PIPE_TEXTURE_CUBE_ARRAY
:
3332 assert(!"Unknown or CUBE texture type in TXF processing\n");
3336 if (sp_sview
->need_swizzle
) {
3337 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
3338 memcpy(rgba_temp
, rgba
, sizeof(rgba_temp
));
3339 do_swizzling(&sp_sview
->base
, rgba_temp
, rgba
);
3345 softpipe_create_sampler_state(struct pipe_context
*pipe
,
3346 const struct pipe_sampler_state
*sampler
)
3348 struct sp_sampler
*samp
= CALLOC_STRUCT(sp_sampler
);
3350 samp
->base
= *sampler
;
3352 /* Note that (for instance) linear_texcoord_s and
3353 * nearest_texcoord_s may be active at the same time, if the
3354 * sampler min_img_filter differs from its mag_img_filter.
3356 if (sampler
->normalized_coords
) {
3357 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
3358 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
3359 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
3361 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
3362 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
3363 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
3366 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
3367 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
3368 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
3370 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
3371 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
3372 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
3375 samp
->min_img_filter
= sampler
->min_img_filter
;
3377 switch (sampler
->min_mip_filter
) {
3378 case PIPE_TEX_MIPFILTER_NONE
:
3379 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
3380 samp
->filter_funcs
= &funcs_none_no_filter_select
;
3382 samp
->filter_funcs
= &funcs_none
;
3385 case PIPE_TEX_MIPFILTER_NEAREST
:
3386 samp
->filter_funcs
= &funcs_nearest
;
3389 case PIPE_TEX_MIPFILTER_LINEAR
:
3390 if (sampler
->min_img_filter
== sampler
->mag_img_filter
&&
3391 sampler
->normalized_coords
&&
3392 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
3393 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
3394 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
3395 sampler
->max_anisotropy
<= 1) {
3396 samp
->min_mag_equal_repeat_linear
= TRUE
;
3398 samp
->filter_funcs
= &funcs_linear
;
3400 /* Anisotropic filtering extension. */
3401 if (sampler
->max_anisotropy
> 1) {
3402 samp
->filter_funcs
= &funcs_linear_aniso
;
3404 /* Override min_img_filter:
3405 * min_img_filter needs to be set to NEAREST since we need to access
3406 * each texture pixel as it is and weight it later; using linear
3407 * filters will have incorrect results.
3408 * By setting the filter to NEAREST here, we can avoid calling the
3409 * generic img_filter_2d_nearest in the anisotropic filter function,
3410 * making it possible to use one of the accelerated implementations
3412 samp
->min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
3414 /* on first access create the lookup table containing the filter weights. */
3416 create_filter_table();
3421 if (samp
->min_img_filter
== sampler
->mag_img_filter
) {
3422 samp
->min_mag_equal
= TRUE
;
3425 return (void *)samp
;
3430 softpipe_get_lambda_func(const struct pipe_sampler_view
*view
,
3431 enum pipe_shader_type shader
)
3433 if (shader
!= PIPE_SHADER_FRAGMENT
)
3434 return compute_lambda_vert
;
3436 switch (view
->target
) {
3438 case PIPE_TEXTURE_1D
:
3439 case PIPE_TEXTURE_1D_ARRAY
:
3440 return compute_lambda_1d
;
3441 case PIPE_TEXTURE_2D
:
3442 case PIPE_TEXTURE_2D_ARRAY
:
3443 case PIPE_TEXTURE_RECT
:
3444 case PIPE_TEXTURE_CUBE
:
3445 case PIPE_TEXTURE_CUBE_ARRAY
:
3446 return compute_lambda_2d
;
3447 case PIPE_TEXTURE_3D
:
3448 return compute_lambda_3d
;
3451 return compute_lambda_1d
;
3456 struct pipe_sampler_view
*
3457 softpipe_create_sampler_view(struct pipe_context
*pipe
,
3458 struct pipe_resource
*resource
,
3459 const struct pipe_sampler_view
*templ
)
3461 struct sp_sampler_view
*sview
= CALLOC_STRUCT(sp_sampler_view
);
3462 const struct softpipe_resource
*spr
= (struct softpipe_resource
*)resource
;
3465 struct pipe_sampler_view
*view
= &sview
->base
;
3467 view
->reference
.count
= 1;
3468 view
->texture
= NULL
;
3469 pipe_resource_reference(&view
->texture
, resource
);
3470 view
->context
= pipe
;
3474 * This is possibly too lenient, but the primary reason is just
3475 * to catch state trackers which forget to initialize this, so
3476 * it only catches clearly impossible view targets.
3478 if (view
->target
!= resource
->target
) {
3479 if (view
->target
== PIPE_TEXTURE_1D
)
3480 assert(resource
->target
== PIPE_TEXTURE_1D_ARRAY
);
3481 else if (view
->target
== PIPE_TEXTURE_1D_ARRAY
)
3482 assert(resource
->target
== PIPE_TEXTURE_1D
);
3483 else if (view
->target
== PIPE_TEXTURE_2D
)
3484 assert(resource
->target
== PIPE_TEXTURE_2D_ARRAY
||
3485 resource
->target
== PIPE_TEXTURE_CUBE
||
3486 resource
->target
== PIPE_TEXTURE_CUBE_ARRAY
);
3487 else if (view
->target
== PIPE_TEXTURE_2D_ARRAY
)
3488 assert(resource
->target
== PIPE_TEXTURE_2D
||
3489 resource
->target
== PIPE_TEXTURE_CUBE
||
3490 resource
->target
== PIPE_TEXTURE_CUBE_ARRAY
);
3491 else if (view
->target
== PIPE_TEXTURE_CUBE
)
3492 assert(resource
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
3493 resource
->target
== PIPE_TEXTURE_2D_ARRAY
);
3494 else if (view
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
3495 assert(resource
->target
== PIPE_TEXTURE_CUBE
||
3496 resource
->target
== PIPE_TEXTURE_2D_ARRAY
);
3502 if (any_swizzle(view
)) {
3503 sview
->need_swizzle
= TRUE
;
3506 sview
->need_cube_convert
= (view
->target
== PIPE_TEXTURE_CUBE
||
3507 view
->target
== PIPE_TEXTURE_CUBE_ARRAY
);
3508 sview
->pot2d
= spr
->pot
&&
3509 (view
->target
== PIPE_TEXTURE_2D
||
3510 view
->target
== PIPE_TEXTURE_RECT
);
3512 sview
->xpot
= util_logbase2( resource
->width0
);
3513 sview
->ypot
= util_logbase2( resource
->height0
);
3516 return (struct pipe_sampler_view
*) sview
;
3520 static inline const struct sp_tgsi_sampler
*
3521 sp_tgsi_sampler_cast_c(const struct tgsi_sampler
*sampler
)
3523 return (const struct sp_tgsi_sampler
*)sampler
;
3528 sp_tgsi_get_dims(struct tgsi_sampler
*tgsi_sampler
,
3529 const unsigned sview_index
,
3530 int level
, int dims
[4])
3532 const struct sp_tgsi_sampler
*sp_samp
=
3533 sp_tgsi_sampler_cast_c(tgsi_sampler
);
3535 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3536 /* always have a view here but texture is NULL if no sampler view was set. */
3537 if (!sp_samp
->sp_sview
[sview_index
].base
.texture
) {
3538 dims
[0] = dims
[1] = dims
[2] = dims
[3] = 0;
3541 sp_get_dims(&sp_samp
->sp_sview
[sview_index
], level
, dims
);
3546 sp_tgsi_get_samples(struct tgsi_sampler
*tgsi_sampler
,
3547 const unsigned sview_index
,
3548 const unsigned sampler_index
,
3549 const float s
[TGSI_QUAD_SIZE
],
3550 const float t
[TGSI_QUAD_SIZE
],
3551 const float p
[TGSI_QUAD_SIZE
],
3552 const float c0
[TGSI_QUAD_SIZE
],
3553 const float lod
[TGSI_QUAD_SIZE
],
3554 float derivs
[3][2][TGSI_QUAD_SIZE
],
3555 const int8_t offset
[3],
3556 enum tgsi_sampler_control control
,
3557 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3559 const struct sp_tgsi_sampler
*sp_tgsi_samp
=
3560 sp_tgsi_sampler_cast_c(tgsi_sampler
);
3561 const struct sp_sampler_view
*sp_sview
;
3562 const struct sp_sampler
*sp_samp
;
3563 struct filter_args filt_args
;
3565 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3566 assert(sampler_index
< PIPE_MAX_SAMPLERS
);
3567 assert(sp_tgsi_samp
->sp_sampler
[sampler_index
]);
3569 sp_sview
= &sp_tgsi_samp
->sp_sview
[sview_index
];
3570 sp_samp
= sp_tgsi_samp
->sp_sampler
[sampler_index
];
3571 /* always have a view here but texture is NULL if no sampler view was set. */
3572 if (!sp_sview
->base
.texture
) {
3574 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++) {
3575 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
3582 filt_args
.control
= control
;
3583 filt_args
.offset
= offset
;
3585 if (sp_sview
->need_cube_convert
) {
3586 float cs
[TGSI_QUAD_SIZE
];
3587 float ct
[TGSI_QUAD_SIZE
];
3588 float cp
[TGSI_QUAD_SIZE
];
3589 uint faces
[TGSI_QUAD_SIZE
];
3591 convert_cube(sp_sview
, sp_samp
, s
, t
, p
, c0
, cs
, ct
, cp
, faces
);
3593 filt_args
.faces
= faces
;
3594 sample_mip(sp_sview
, sp_samp
, cs
, ct
, cp
, c0
, lod
, &filt_args
, rgba
);
3596 static const uint zero_faces
[TGSI_QUAD_SIZE
] = {0, 0, 0, 0};
3598 filt_args
.faces
= zero_faces
;
3599 sample_mip(sp_sview
, sp_samp
, s
, t
, p
, c0
, lod
, &filt_args
, rgba
);
3604 sp_tgsi_query_lod(const struct tgsi_sampler
*tgsi_sampler
,
3605 const unsigned sview_index
,
3606 const unsigned sampler_index
,
3607 const float s
[TGSI_QUAD_SIZE
],
3608 const float t
[TGSI_QUAD_SIZE
],
3609 const float p
[TGSI_QUAD_SIZE
],
3610 const float c0
[TGSI_QUAD_SIZE
],
3611 const enum tgsi_sampler_control control
,
3612 float mipmap
[TGSI_QUAD_SIZE
],
3613 float lod
[TGSI_QUAD_SIZE
])
3615 static const float lod_in
[TGSI_QUAD_SIZE
] = { 0.0, 0.0, 0.0, 0.0 };
3617 const struct sp_tgsi_sampler
*sp_tgsi_samp
=
3618 sp_tgsi_sampler_cast_c(tgsi_sampler
);
3619 const struct sp_sampler_view
*sp_sview
;
3620 const struct sp_sampler
*sp_samp
;
3621 const struct sp_filter_funcs
*funcs
;
3624 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3625 assert(sampler_index
< PIPE_MAX_SAMPLERS
);
3626 assert(sp_tgsi_samp
->sp_sampler
[sampler_index
]);
3628 sp_sview
= &sp_tgsi_samp
->sp_sview
[sview_index
];
3629 sp_samp
= sp_tgsi_samp
->sp_sampler
[sampler_index
];
3630 /* always have a view here but texture is NULL if no sampler view was
3632 if (!sp_sview
->base
.texture
) {
3633 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
3640 if (sp_sview
->need_cube_convert
) {
3641 float cs
[TGSI_QUAD_SIZE
];
3642 float ct
[TGSI_QUAD_SIZE
];
3643 float cp
[TGSI_QUAD_SIZE
];
3644 uint unused_faces
[TGSI_QUAD_SIZE
];
3646 convert_cube(sp_sview
, sp_samp
, s
, t
, p
, c0
, cs
, ct
, cp
, unused_faces
);
3647 compute_lambda_lod_unclamped(sp_sview
, sp_samp
,
3648 cs
, ct
, cp
, lod_in
, control
, lod
);
3650 compute_lambda_lod_unclamped(sp_sview
, sp_samp
,
3651 s
, t
, p
, lod_in
, control
, lod
);
3654 get_filters(sp_sview
, sp_samp
, control
, &funcs
, NULL
, NULL
);
3655 funcs
->relative_level(sp_sview
, sp_samp
, lod
, mipmap
);
3659 sp_tgsi_get_texel(struct tgsi_sampler
*tgsi_sampler
,
3660 const unsigned sview_index
,
3661 const int i
[TGSI_QUAD_SIZE
],
3662 const int j
[TGSI_QUAD_SIZE
], const int k
[TGSI_QUAD_SIZE
],
3663 const int lod
[TGSI_QUAD_SIZE
], const int8_t offset
[3],
3664 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3666 const struct sp_tgsi_sampler
*sp_samp
=
3667 sp_tgsi_sampler_cast_c(tgsi_sampler
);
3669 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3670 /* always have a view here but texture is NULL if no sampler view was set. */
3671 if (!sp_samp
->sp_sview
[sview_index
].base
.texture
) {
3673 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++) {
3674 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
3680 sp_get_texels(&sp_samp
->sp_sview
[sview_index
], i
, j
, k
, lod
, offset
, rgba
);
3684 struct sp_tgsi_sampler
*
3685 sp_create_tgsi_sampler(void)
3687 struct sp_tgsi_sampler
*samp
= CALLOC_STRUCT(sp_tgsi_sampler
);
3691 samp
->base
.get_dims
= sp_tgsi_get_dims
;
3692 samp
->base
.get_samples
= sp_tgsi_get_samples
;
3693 samp
->base
.get_texel
= sp_tgsi_get_texel
;
3694 samp
->base
.query_lod
= sp_tgsi_query_lod
;