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
);
799 /* Can involve a lot of unnecessary checks for border color:
802 get_texel_quad_2d(const struct sp_sampler_view
*sp_sview
,
803 const struct sp_sampler
*sp_samp
,
804 union tex_tile_address addr
,
809 out
[0] = get_texel_2d( sp_sview
, sp_samp
, addr
, x0
, y0
);
810 out
[1] = get_texel_2d( sp_sview
, sp_samp
, addr
, x1
, y0
);
811 out
[3] = get_texel_2d( sp_sview
, sp_samp
, addr
, x1
, y1
);
812 out
[2] = get_texel_2d( sp_sview
, sp_samp
, addr
, x0
, y1
);
819 static inline const float *
820 get_texel_3d_no_border(const struct sp_sampler_view
*sp_sview
,
821 union tex_tile_address addr
, int x
, int y
, int z
)
823 const struct softpipe_tex_cached_tile
*tile
;
825 addr
.bits
.x
= x
/ TEX_TILE_SIZE
;
826 addr
.bits
.y
= y
/ TEX_TILE_SIZE
;
831 tile
= sp_get_cached_tile_tex(sp_sview
->cache
, addr
);
833 return &tile
->data
.color
[y
][x
][0];
837 static inline const float *
838 get_texel_3d(const struct sp_sampler_view
*sp_sview
,
839 const struct sp_sampler
*sp_samp
,
840 union tex_tile_address addr
, int x
, int y
, int z
)
842 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
843 const unsigned level
= addr
.bits
.level
;
845 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
846 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
) ||
847 z
< 0 || z
>= (int) u_minify(texture
->depth0
, level
)) {
848 return sp_samp
->base
.border_color
.f
;
851 return get_texel_3d_no_border( sp_sview
, addr
, x
, y
, z
);
856 /* Get texel pointer for 1D array texture */
857 static inline const float *
858 get_texel_1d_array(const struct sp_sampler_view
*sp_sview
,
859 const struct sp_sampler
*sp_samp
,
860 union tex_tile_address addr
, int x
, int y
)
862 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
863 const unsigned level
= addr
.bits
.level
;
865 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
)) {
866 return sp_samp
->base
.border_color
.f
;
869 return get_texel_2d_no_border(sp_sview
, addr
, x
, y
);
874 /* Get texel pointer for 2D array texture */
875 static inline const float *
876 get_texel_2d_array(const struct sp_sampler_view
*sp_sview
,
877 const struct sp_sampler
*sp_samp
,
878 union tex_tile_address addr
, int x
, int y
, int layer
)
880 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
881 const unsigned level
= addr
.bits
.level
;
883 assert(layer
< (int) texture
->array_size
);
886 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
887 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
888 return sp_samp
->base
.border_color
.f
;
891 return get_texel_3d_no_border(sp_sview
, addr
, x
, y
, layer
);
896 static inline const float *
897 get_texel_cube_seamless(const struct sp_sampler_view
*sp_sview
,
898 union tex_tile_address addr
, int x
, int y
,
899 float *corner
, int layer
, unsigned face
)
901 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
902 const unsigned level
= addr
.bits
.level
;
903 int new_x
, new_y
, max_x
;
905 max_x
= (int) u_minify(texture
->width0
, level
);
907 assert(texture
->width0
== texture
->height0
);
911 /* change the face */
914 * Cheat with corners. They are difficult and I believe because we don't get
915 * per-pixel faces we can actually have multiple corner texels per pixel,
916 * which screws things up majorly in any case (as the per spec behavior is
917 * to average the 3 remaining texels, which we might not have).
918 * Hence just make sure that the 2nd coord is clamped, will simply pick the
919 * sample which would have fallen off the x coord, but not y coord.
920 * So the filter weight of the samples will be wrong, but at least this
921 * ensures that only valid texels near the corner are used.
923 if (y
< 0 || y
>= max_x
) {
924 y
= CLAMP(y
, 0, max_x
- 1);
926 new_x
= get_next_xcoord(face
, 0, max_x
-1, x
, y
);
927 new_y
= get_next_ycoord(face
, 0, max_x
-1, x
, y
);
928 face
= get_next_face(face
, 0);
929 } else if (x
>= max_x
) {
930 if (y
< 0 || y
>= max_x
) {
931 y
= CLAMP(y
, 0, max_x
- 1);
933 new_x
= get_next_xcoord(face
, 1, max_x
-1, x
, y
);
934 new_y
= get_next_ycoord(face
, 1, max_x
-1, x
, y
);
935 face
= get_next_face(face
, 1);
937 new_x
= get_next_xcoord(face
, 2, max_x
-1, x
, y
);
938 new_y
= get_next_ycoord(face
, 2, max_x
-1, x
, y
);
939 face
= get_next_face(face
, 2);
940 } else if (y
>= max_x
) {
941 new_x
= get_next_xcoord(face
, 3, max_x
-1, x
, y
);
942 new_y
= get_next_ycoord(face
, 3, max_x
-1, x
, y
);
943 face
= get_next_face(face
, 3);
946 return get_texel_3d_no_border(sp_sview
, addr
, new_x
, new_y
, layer
+ face
);
950 /* Get texel pointer for cube array texture */
951 static inline const float *
952 get_texel_cube_array(const struct sp_sampler_view
*sp_sview
,
953 const struct sp_sampler
*sp_samp
,
954 union tex_tile_address addr
, int x
, int y
, int layer
)
956 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
957 const unsigned level
= addr
.bits
.level
;
959 assert(layer
< (int) texture
->array_size
);
962 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
963 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
964 return sp_samp
->base
.border_color
.f
;
967 return get_texel_3d_no_border(sp_sview
, addr
, x
, y
, layer
);
971 * Given the logbase2 of a mipmap's base level size and a mipmap level,
972 * return the size (in texels) of that mipmap level.
973 * For example, if level[0].width = 256 then base_pot will be 8.
974 * If level = 2, then we'll return 64 (the width at level=2).
975 * Return 1 if level > base_pot.
977 static inline unsigned
978 pot_level_size(unsigned base_pot
, unsigned level
)
980 return (base_pot
>= level
) ? (1 << (base_pot
- level
)) : 1;
985 print_sample(const char *function
, const float *rgba
)
987 debug_printf("%s %g %g %g %g\n",
989 rgba
[0], rgba
[TGSI_NUM_CHANNELS
], rgba
[2*TGSI_NUM_CHANNELS
], rgba
[3*TGSI_NUM_CHANNELS
]);
994 print_sample_4(const char *function
, float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
996 debug_printf("%s %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
998 rgba
[0][0], rgba
[1][0], rgba
[2][0], rgba
[3][0],
999 rgba
[0][1], rgba
[1][1], rgba
[2][1], rgba
[3][1],
1000 rgba
[0][2], rgba
[1][2], rgba
[2][2], rgba
[3][2],
1001 rgba
[0][3], rgba
[1][3], rgba
[2][3], rgba
[3][3]);
1005 /* Some image-filter fastpaths:
1008 img_filter_2d_linear_repeat_POT(const struct sp_sampler_view
*sp_sview
,
1009 const struct sp_sampler
*sp_samp
,
1010 const struct img_filter_args
*args
,
1013 const unsigned xpot
= pot_level_size(sp_sview
->xpot
, args
->level
);
1014 const unsigned ypot
= pot_level_size(sp_sview
->ypot
, args
->level
);
1015 const int xmax
= (xpot
- 1) & (TEX_TILE_SIZE
- 1); /* MIN2(TEX_TILE_SIZE, xpot) - 1; */
1016 const int ymax
= (ypot
- 1) & (TEX_TILE_SIZE
- 1); /* MIN2(TEX_TILE_SIZE, ypot) - 1; */
1017 union tex_tile_address addr
;
1020 const float u
= (args
->s
* xpot
- 0.5F
) + args
->offset
[0];
1021 const float v
= (args
->t
* ypot
- 0.5F
) + args
->offset
[1];
1023 const int uflr
= util_ifloor(u
);
1024 const int vflr
= util_ifloor(v
);
1026 const float xw
= u
- (float)uflr
;
1027 const float yw
= v
- (float)vflr
;
1029 const int x0
= uflr
& (xpot
- 1);
1030 const int y0
= vflr
& (ypot
- 1);
1035 addr
.bits
.level
= args
->level
;
1036 addr
.bits
.z
= sp_sview
->base
.u
.tex
.first_layer
;
1038 /* Can we fetch all four at once:
1040 if (x0
< xmax
&& y0
< ymax
) {
1041 get_texel_quad_2d_no_border_single_tile(sp_sview
, addr
, x0
, y0
, tx
);
1044 const unsigned x1
= (x0
+ 1) & (xpot
- 1);
1045 const unsigned y1
= (y0
+ 1) & (ypot
- 1);
1046 get_texel_quad_2d_no_border(sp_sview
, addr
, x0
, y0
, x1
, y1
, tx
);
1049 /* interpolate R, G, B, A */
1050 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++) {
1051 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1053 tx
[2][c
], tx
[3][c
]);
1057 print_sample(__FUNCTION__
, rgba
);
1063 img_filter_2d_nearest_repeat_POT(const struct sp_sampler_view
*sp_sview
,
1064 const struct sp_sampler
*sp_samp
,
1065 const struct img_filter_args
*args
,
1068 const unsigned xpot
= pot_level_size(sp_sview
->xpot
, args
->level
);
1069 const unsigned ypot
= pot_level_size(sp_sview
->ypot
, args
->level
);
1071 union tex_tile_address addr
;
1074 const float u
= args
->s
* xpot
+ args
->offset
[0];
1075 const float v
= args
->t
* ypot
+ args
->offset
[1];
1077 const int uflr
= util_ifloor(u
);
1078 const int vflr
= util_ifloor(v
);
1080 const int x0
= uflr
& (xpot
- 1);
1081 const int y0
= vflr
& (ypot
- 1);
1084 addr
.bits
.level
= args
->level
;
1085 addr
.bits
.z
= sp_sview
->base
.u
.tex
.first_layer
;
1087 out
= get_texel_2d_no_border(sp_sview
, addr
, x0
, y0
);
1088 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1089 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1092 print_sample(__FUNCTION__
, rgba
);
1098 img_filter_2d_nearest_clamp_POT(const struct sp_sampler_view
*sp_sview
,
1099 const struct sp_sampler
*sp_samp
,
1100 const struct img_filter_args
*args
,
1103 const unsigned xpot
= pot_level_size(sp_sview
->xpot
, args
->level
);
1104 const unsigned ypot
= pot_level_size(sp_sview
->ypot
, args
->level
);
1105 union tex_tile_address addr
;
1108 const float u
= args
->s
* xpot
+ args
->offset
[0];
1109 const float v
= args
->t
* ypot
+ args
->offset
[1];
1115 addr
.bits
.level
= args
->level
;
1116 addr
.bits
.z
= sp_sview
->base
.u
.tex
.first_layer
;
1118 x0
= util_ifloor(u
);
1121 else if (x0
> (int) xpot
- 1)
1124 y0
= util_ifloor(v
);
1127 else if (y0
> (int) ypot
- 1)
1130 out
= get_texel_2d_no_border(sp_sview
, addr
, x0
, y0
);
1131 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1132 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1135 print_sample(__FUNCTION__
, rgba
);
1141 img_filter_1d_nearest(const struct sp_sampler_view
*sp_sview
,
1142 const struct sp_sampler
*sp_samp
,
1143 const struct img_filter_args
*args
,
1146 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1147 const int width
= u_minify(texture
->width0
, args
->level
);
1149 union tex_tile_address addr
;
1156 addr
.bits
.level
= args
->level
;
1158 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1160 out
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x
,
1161 sp_sview
->base
.u
.tex
.first_layer
);
1162 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1163 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1166 print_sample(__FUNCTION__
, rgba
);
1172 img_filter_1d_array_nearest(const struct sp_sampler_view
*sp_sview
,
1173 const struct sp_sampler
*sp_samp
,
1174 const struct img_filter_args
*args
,
1177 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1178 const int width
= u_minify(texture
->width0
, args
->level
);
1179 const int layer
= coord_to_layer(args
->t
, sp_sview
->base
.u
.tex
.first_layer
,
1180 sp_sview
->base
.u
.tex
.last_layer
);
1182 union tex_tile_address addr
;
1189 addr
.bits
.level
= args
->level
;
1191 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1193 out
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x
, layer
);
1194 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1195 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1198 print_sample(__FUNCTION__
, rgba
);
1204 img_filter_2d_nearest(const struct sp_sampler_view
*sp_sview
,
1205 const struct sp_sampler
*sp_samp
,
1206 const struct img_filter_args
*args
,
1209 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1210 const int width
= u_minify(texture
->width0
, args
->level
);
1211 const int height
= u_minify(texture
->height0
, args
->level
);
1213 union tex_tile_address addr
;
1221 addr
.bits
.level
= args
->level
;
1222 addr
.bits
.z
= sp_sview
->base
.u
.tex
.first_layer
;
1224 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1225 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1227 out
= get_texel_2d(sp_sview
, sp_samp
, addr
, x
, y
);
1228 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1229 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1232 print_sample(__FUNCTION__
, rgba
);
1238 img_filter_2d_array_nearest(const struct sp_sampler_view
*sp_sview
,
1239 const struct sp_sampler
*sp_samp
,
1240 const struct img_filter_args
*args
,
1243 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1244 const int width
= u_minify(texture
->width0
, args
->level
);
1245 const int height
= u_minify(texture
->height0
, args
->level
);
1246 const int layer
= coord_to_layer(args
->p
, sp_sview
->base
.u
.tex
.first_layer
,
1247 sp_sview
->base
.u
.tex
.last_layer
);
1249 union tex_tile_address addr
;
1257 addr
.bits
.level
= args
->level
;
1259 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1260 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1262 out
= get_texel_2d_array(sp_sview
, sp_samp
, addr
, x
, y
, layer
);
1263 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1264 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1267 print_sample(__FUNCTION__
, rgba
);
1273 img_filter_cube_nearest(const struct sp_sampler_view
*sp_sview
,
1274 const struct sp_sampler
*sp_samp
,
1275 const struct img_filter_args
*args
,
1278 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1279 const int width
= u_minify(texture
->width0
, args
->level
);
1280 const int height
= u_minify(texture
->height0
, args
->level
);
1281 const int layerface
= args
->face_id
+ sp_sview
->base
.u
.tex
.first_layer
;
1283 union tex_tile_address addr
;
1291 addr
.bits
.level
= args
->level
;
1294 * If NEAREST filtering is done within a miplevel, always apply wrap
1295 * mode CLAMP_TO_EDGE.
1297 if (sp_samp
->base
.seamless_cube_map
) {
1298 wrap_nearest_clamp_to_edge(args
->s
, width
, args
->offset
[0], &x
);
1299 wrap_nearest_clamp_to_edge(args
->t
, height
, args
->offset
[1], &y
);
1301 /* Would probably make sense to ignore mode and just do edge clamp */
1302 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1303 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1306 out
= get_texel_cube_array(sp_sview
, sp_samp
, addr
, x
, y
, layerface
);
1307 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1308 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1311 print_sample(__FUNCTION__
, rgba
);
1316 img_filter_cube_array_nearest(const struct sp_sampler_view
*sp_sview
,
1317 const struct sp_sampler
*sp_samp
,
1318 const struct img_filter_args
*args
,
1321 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1322 const int width
= u_minify(texture
->width0
, args
->level
);
1323 const int height
= u_minify(texture
->height0
, args
->level
);
1324 const int layerface
=
1325 coord_to_layer(6 * args
->p
+ sp_sview
->base
.u
.tex
.first_layer
,
1326 sp_sview
->base
.u
.tex
.first_layer
,
1327 sp_sview
->base
.u
.tex
.last_layer
- 5) + args
->face_id
;
1329 union tex_tile_address addr
;
1337 addr
.bits
.level
= args
->level
;
1339 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1340 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1342 out
= get_texel_cube_array(sp_sview
, sp_samp
, addr
, x
, y
, layerface
);
1343 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1344 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1347 print_sample(__FUNCTION__
, rgba
);
1352 img_filter_3d_nearest(const struct sp_sampler_view
*sp_sview
,
1353 const struct sp_sampler
*sp_samp
,
1354 const struct img_filter_args
*args
,
1357 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1358 const int width
= u_minify(texture
->width0
, args
->level
);
1359 const int height
= u_minify(texture
->height0
, args
->level
);
1360 const int depth
= u_minify(texture
->depth0
, args
->level
);
1362 union tex_tile_address addr
;
1370 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1371 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1372 sp_samp
->nearest_texcoord_p(args
->p
, depth
, args
->offset
[2], &z
);
1375 addr
.bits
.level
= args
->level
;
1377 out
= get_texel_3d(sp_sview
, sp_samp
, addr
, x
, y
, z
);
1378 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1379 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1384 img_filter_1d_linear(const struct sp_sampler_view
*sp_sview
,
1385 const struct sp_sampler
*sp_samp
,
1386 const struct img_filter_args
*args
,
1389 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1390 const int width
= u_minify(texture
->width0
, args
->level
);
1392 float xw
; /* weights */
1393 union tex_tile_address addr
;
1394 const float *tx0
, *tx1
;
1400 addr
.bits
.level
= args
->level
;
1402 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1404 tx0
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x0
,
1405 sp_sview
->base
.u
.tex
.first_layer
);
1406 tx1
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x1
,
1407 sp_sview
->base
.u
.tex
.first_layer
);
1409 /* interpolate R, G, B, A */
1410 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1411 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp(xw
, tx0
[c
], tx1
[c
]);
1416 img_filter_1d_array_linear(const struct sp_sampler_view
*sp_sview
,
1417 const struct sp_sampler
*sp_samp
,
1418 const struct img_filter_args
*args
,
1421 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1422 const int width
= u_minify(texture
->width0
, args
->level
);
1423 const int layer
= coord_to_layer(args
->t
, sp_sview
->base
.u
.tex
.first_layer
,
1424 sp_sview
->base
.u
.tex
.last_layer
);
1426 float xw
; /* weights */
1427 union tex_tile_address addr
;
1428 const float *tx0
, *tx1
;
1434 addr
.bits
.level
= args
->level
;
1436 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1438 tx0
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x0
, layer
);
1439 tx1
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x1
, layer
);
1441 /* interpolate R, G, B, A */
1442 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1443 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp(xw
, tx0
[c
], tx1
[c
]);
1447 * Retrieve the gathered value, need to convert to the
1448 * TGSI expected interface, and take component select
1449 * and swizzling into account.
1452 get_gather_value(const struct sp_sampler_view
*sp_sview
,
1453 int chan_in
, int comp_sel
,
1460 * softpipe samples in a different order
1461 * to TGSI expects, so we need to swizzle,
1462 * the samples into the correct slots.
1482 /* pick which component to use for the swizzle */
1485 swizzle
= sp_sview
->base
.swizzle_r
;
1488 swizzle
= sp_sview
->base
.swizzle_g
;
1491 swizzle
= sp_sview
->base
.swizzle_b
;
1494 swizzle
= sp_sview
->base
.swizzle_a
;
1501 /* get correct result using the channel and swizzle */
1503 case PIPE_SWIZZLE_0
:
1505 case PIPE_SWIZZLE_1
:
1508 return tx
[chan
][swizzle
];
1514 img_filter_2d_linear(const struct sp_sampler_view
*sp_sview
,
1515 const struct sp_sampler
*sp_samp
,
1516 const struct img_filter_args
*args
,
1519 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1520 const int width
= u_minify(texture
->width0
, args
->level
);
1521 const int height
= u_minify(texture
->height0
, args
->level
);
1523 float xw
, yw
; /* weights */
1524 union tex_tile_address addr
;
1532 addr
.bits
.level
= args
->level
;
1533 addr
.bits
.z
= sp_sview
->base
.u
.tex
.first_layer
;
1535 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1536 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1538 tx
[0] = get_texel_2d(sp_sview
, sp_samp
, addr
, x0
, y0
);
1539 tx
[1] = get_texel_2d(sp_sview
, sp_samp
, addr
, x1
, y0
);
1540 tx
[2] = get_texel_2d(sp_sview
, sp_samp
, addr
, x0
, y1
);
1541 tx
[3] = get_texel_2d(sp_sview
, sp_samp
, addr
, x1
, y1
);
1543 if (args
->gather_only
) {
1544 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1545 rgba
[TGSI_NUM_CHANNELS
*c
] = get_gather_value(sp_sview
, c
,
1549 /* interpolate R, G, B, A */
1550 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1551 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1553 tx
[2][c
], tx
[3][c
]);
1559 img_filter_2d_array_linear(const struct sp_sampler_view
*sp_sview
,
1560 const struct sp_sampler
*sp_samp
,
1561 const struct img_filter_args
*args
,
1564 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1565 const int width
= u_minify(texture
->width0
, args
->level
);
1566 const int height
= u_minify(texture
->height0
, args
->level
);
1567 const int layer
= coord_to_layer(args
->p
, sp_sview
->base
.u
.tex
.first_layer
,
1568 sp_sview
->base
.u
.tex
.last_layer
);
1570 float xw
, yw
; /* weights */
1571 union tex_tile_address addr
;
1579 addr
.bits
.level
= args
->level
;
1581 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1582 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1584 tx
[0] = get_texel_2d_array(sp_sview
, sp_samp
, addr
, x0
, y0
, layer
);
1585 tx
[1] = get_texel_2d_array(sp_sview
, sp_samp
, addr
, x1
, y0
, layer
);
1586 tx
[2] = get_texel_2d_array(sp_sview
, sp_samp
, addr
, x0
, y1
, layer
);
1587 tx
[3] = get_texel_2d_array(sp_sview
, sp_samp
, addr
, x1
, y1
, layer
);
1589 if (args
->gather_only
) {
1590 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1591 rgba
[TGSI_NUM_CHANNELS
*c
] = get_gather_value(sp_sview
, c
,
1595 /* interpolate R, G, B, A */
1596 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1597 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1599 tx
[2][c
], tx
[3][c
]);
1605 img_filter_cube_linear(const struct sp_sampler_view
*sp_sview
,
1606 const struct sp_sampler
*sp_samp
,
1607 const struct img_filter_args
*args
,
1610 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1611 const int width
= u_minify(texture
->width0
, args
->level
);
1612 const int height
= u_minify(texture
->height0
, args
->level
);
1613 const int layer
= sp_sview
->base
.u
.tex
.first_layer
;
1615 float xw
, yw
; /* weights */
1616 union tex_tile_address addr
;
1618 float corner0
[TGSI_QUAD_SIZE
], corner1
[TGSI_QUAD_SIZE
],
1619 corner2
[TGSI_QUAD_SIZE
], corner3
[TGSI_QUAD_SIZE
];
1626 addr
.bits
.level
= args
->level
;
1629 * For seamless if LINEAR filtering is done within a miplevel,
1630 * always apply wrap mode CLAMP_TO_BORDER.
1632 if (sp_samp
->base
.seamless_cube_map
) {
1633 /* Note this is a bit overkill, actual clamping is not required */
1634 wrap_linear_clamp_to_border(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1635 wrap_linear_clamp_to_border(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1637 /* Would probably make sense to ignore mode and just do edge clamp */
1638 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1639 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1642 if (sp_samp
->base
.seamless_cube_map
) {
1643 tx
[0] = get_texel_cube_seamless(sp_sview
, addr
, x0
, y0
, corner0
, layer
, args
->face_id
);
1644 tx
[1] = get_texel_cube_seamless(sp_sview
, addr
, x1
, y0
, corner1
, layer
, args
->face_id
);
1645 tx
[2] = get_texel_cube_seamless(sp_sview
, addr
, x0
, y1
, corner2
, layer
, args
->face_id
);
1646 tx
[3] = get_texel_cube_seamless(sp_sview
, addr
, x1
, y1
, corner3
, layer
, args
->face_id
);
1648 tx
[0] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x0
, y0
, layer
+ args
->face_id
);
1649 tx
[1] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x1
, y0
, layer
+ args
->face_id
);
1650 tx
[2] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x0
, y1
, layer
+ args
->face_id
);
1651 tx
[3] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x1
, y1
, layer
+ args
->face_id
);
1654 if (args
->gather_only
) {
1655 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1656 rgba
[TGSI_NUM_CHANNELS
*c
] = get_gather_value(sp_sview
, c
,
1660 /* interpolate R, G, B, A */
1661 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1662 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1664 tx
[2][c
], tx
[3][c
]);
1670 img_filter_cube_array_linear(const struct sp_sampler_view
*sp_sview
,
1671 const struct sp_sampler
*sp_samp
,
1672 const struct img_filter_args
*args
,
1675 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1676 const int width
= u_minify(texture
->width0
, args
->level
);
1677 const int height
= u_minify(texture
->height0
, args
->level
);
1679 coord_to_layer(6 * args
->p
+ sp_sview
->base
.u
.tex
.first_layer
,
1680 sp_sview
->base
.u
.tex
.first_layer
,
1681 sp_sview
->base
.u
.tex
.last_layer
- 5);
1683 float xw
, yw
; /* weights */
1684 union tex_tile_address addr
;
1686 float corner0
[TGSI_QUAD_SIZE
], corner1
[TGSI_QUAD_SIZE
],
1687 corner2
[TGSI_QUAD_SIZE
], corner3
[TGSI_QUAD_SIZE
];
1694 addr
.bits
.level
= args
->level
;
1697 * For seamless if LINEAR filtering is done within a miplevel,
1698 * always apply wrap mode CLAMP_TO_BORDER.
1700 if (sp_samp
->base
.seamless_cube_map
) {
1701 /* Note this is a bit overkill, actual clamping is not required */
1702 wrap_linear_clamp_to_border(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1703 wrap_linear_clamp_to_border(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1705 /* Would probably make sense to ignore mode and just do edge clamp */
1706 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1707 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1710 if (sp_samp
->base
.seamless_cube_map
) {
1711 tx
[0] = get_texel_cube_seamless(sp_sview
, addr
, x0
, y0
, corner0
, layer
, args
->face_id
);
1712 tx
[1] = get_texel_cube_seamless(sp_sview
, addr
, x1
, y0
, corner1
, layer
, args
->face_id
);
1713 tx
[2] = get_texel_cube_seamless(sp_sview
, addr
, x0
, y1
, corner2
, layer
, args
->face_id
);
1714 tx
[3] = get_texel_cube_seamless(sp_sview
, addr
, x1
, y1
, corner3
, layer
, args
->face_id
);
1716 tx
[0] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x0
, y0
, layer
+ args
->face_id
);
1717 tx
[1] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x1
, y0
, layer
+ args
->face_id
);
1718 tx
[2] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x0
, y1
, layer
+ args
->face_id
);
1719 tx
[3] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x1
, y1
, layer
+ args
->face_id
);
1722 if (args
->gather_only
) {
1723 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1724 rgba
[TGSI_NUM_CHANNELS
*c
] = get_gather_value(sp_sview
, c
,
1728 /* interpolate R, G, B, A */
1729 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1730 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1732 tx
[2][c
], tx
[3][c
]);
1737 img_filter_3d_linear(const struct sp_sampler_view
*sp_sview
,
1738 const struct sp_sampler
*sp_samp
,
1739 const struct img_filter_args
*args
,
1742 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1743 const int width
= u_minify(texture
->width0
, args
->level
);
1744 const int height
= u_minify(texture
->height0
, args
->level
);
1745 const int depth
= u_minify(texture
->depth0
, args
->level
);
1746 int x0
, x1
, y0
, y1
, z0
, z1
;
1747 float xw
, yw
, zw
; /* interpolation weights */
1748 union tex_tile_address addr
;
1749 const float *tx00
, *tx01
, *tx02
, *tx03
, *tx10
, *tx11
, *tx12
, *tx13
;
1753 addr
.bits
.level
= args
->level
;
1759 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1760 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1761 sp_samp
->linear_texcoord_p(args
->p
, depth
, args
->offset
[2], &z0
, &z1
, &zw
);
1763 tx00
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y0
, z0
);
1764 tx01
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y0
, z0
);
1765 tx02
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y1
, z0
);
1766 tx03
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y1
, z0
);
1768 tx10
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y0
, z1
);
1769 tx11
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y0
, z1
);
1770 tx12
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y1
, z1
);
1771 tx13
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y1
, z1
);
1773 /* interpolate R, G, B, A */
1774 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
1775 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_3d(xw
, yw
, zw
,
1783 /* Calculate level of detail for every fragment,
1784 * with lambda already computed.
1785 * Note that lambda has already been biased by global LOD bias.
1786 * \param biased_lambda per-quad lambda.
1787 * \param lod_in per-fragment lod_bias or explicit_lod.
1788 * \param lod returns the per-fragment lod.
1791 compute_lod(const struct pipe_sampler_state
*sampler
,
1792 enum tgsi_sampler_control control
,
1793 const float biased_lambda
,
1794 const float lod_in
[TGSI_QUAD_SIZE
],
1795 float lod
[TGSI_QUAD_SIZE
])
1797 const float min_lod
= sampler
->min_lod
;
1798 const float max_lod
= sampler
->max_lod
;
1802 case TGSI_SAMPLER_LOD_NONE
:
1803 case TGSI_SAMPLER_LOD_ZERO
:
1805 case TGSI_SAMPLER_DERIVS_EXPLICIT
:
1806 lod
[0] = lod
[1] = lod
[2] = lod
[3] = CLAMP(biased_lambda
, min_lod
, max_lod
);
1808 case TGSI_SAMPLER_LOD_BIAS
:
1809 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1810 lod
[i
] = biased_lambda
+ lod_in
[i
];
1811 lod
[i
] = CLAMP(lod
[i
], min_lod
, max_lod
);
1814 case TGSI_SAMPLER_LOD_EXPLICIT
:
1815 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1816 lod
[i
] = CLAMP(lod_in
[i
], min_lod
, max_lod
);
1821 lod
[0] = lod
[1] = lod
[2] = lod
[3] = 0.0f
;
1826 /* Calculate level of detail for every fragment. The computed value is not
1827 * clamped to lod_min and lod_max.
1828 * \param lod_in per-fragment lod_bias or explicit_lod.
1829 * \param lod results per-fragment lod.
1832 compute_lambda_lod_unclamped(const struct sp_sampler_view
*sp_sview
,
1833 const struct sp_sampler
*sp_samp
,
1834 const float s
[TGSI_QUAD_SIZE
],
1835 const float t
[TGSI_QUAD_SIZE
],
1836 const float p
[TGSI_QUAD_SIZE
],
1837 const float lod_in
[TGSI_QUAD_SIZE
],
1838 enum tgsi_sampler_control control
,
1839 float lod
[TGSI_QUAD_SIZE
])
1841 const struct pipe_sampler_state
*sampler
= &sp_samp
->base
;
1842 const float lod_bias
= sampler
->lod_bias
;
1847 case TGSI_SAMPLER_LOD_NONE
:
1849 case TGSI_SAMPLER_DERIVS_EXPLICIT
:
1850 lambda
= sp_sview
->compute_lambda(sp_sview
, s
, t
, p
) + lod_bias
;
1851 lod
[0] = lod
[1] = lod
[2] = lod
[3] = lambda
;
1853 case TGSI_SAMPLER_LOD_BIAS
:
1854 lambda
= sp_sview
->compute_lambda(sp_sview
, s
, t
, p
) + lod_bias
;
1855 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1856 lod
[i
] = lambda
+ lod_in
[i
];
1859 case TGSI_SAMPLER_LOD_EXPLICIT
:
1860 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1861 lod
[i
] = lod_in
[i
] + lod_bias
;
1864 case TGSI_SAMPLER_LOD_ZERO
:
1865 case TGSI_SAMPLER_GATHER
:
1866 lod
[0] = lod
[1] = lod
[2] = lod
[3] = lod_bias
;
1870 lod
[0] = lod
[1] = lod
[2] = lod
[3] = 0.0f
;
1874 /* Calculate level of detail for every fragment.
1875 * \param lod_in per-fragment lod_bias or explicit_lod.
1876 * \param lod results per-fragment lod.
1879 compute_lambda_lod(const struct sp_sampler_view
*sp_sview
,
1880 const struct sp_sampler
*sp_samp
,
1881 const float s
[TGSI_QUAD_SIZE
],
1882 const float t
[TGSI_QUAD_SIZE
],
1883 const float p
[TGSI_QUAD_SIZE
],
1884 const float lod_in
[TGSI_QUAD_SIZE
],
1885 enum tgsi_sampler_control control
,
1886 float lod
[TGSI_QUAD_SIZE
])
1888 const struct pipe_sampler_state
*sampler
= &sp_samp
->base
;
1889 const float min_lod
= sampler
->min_lod
;
1890 const float max_lod
= sampler
->max_lod
;
1893 compute_lambda_lod_unclamped(sp_sview
, sp_samp
,
1894 s
, t
, p
, lod_in
, control
, lod
);
1895 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1896 lod
[i
] = CLAMP(lod
[i
], min_lod
, max_lod
);
1900 static inline unsigned
1901 get_gather_component(const float lod_in
[TGSI_QUAD_SIZE
])
1903 /* gather component is stored in lod_in slot as unsigned */
1904 return (*(unsigned int *)lod_in
) & 0x3;
1908 * Clamps given lod to both lod limits and mip level limits. Clamping to the
1909 * latter limits is done so that lod is relative to the first (base) level.
1912 clamp_lod(const struct sp_sampler_view
*sp_sview
,
1913 const struct sp_sampler
*sp_samp
,
1914 const float lod
[TGSI_QUAD_SIZE
],
1915 float clamped
[TGSI_QUAD_SIZE
])
1917 const float min_lod
= sp_samp
->base
.min_lod
;
1918 const float max_lod
= sp_samp
->base
.max_lod
;
1919 const float min_level
= sp_sview
->base
.u
.tex
.first_level
;
1920 const float max_level
= sp_sview
->base
.u
.tex
.last_level
;
1923 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1926 cl
= CLAMP(cl
, min_lod
, max_lod
);
1927 cl
= CLAMP(cl
, 0, max_level
- min_level
);
1933 * Get mip level relative to base level for linear mip filter
1936 mip_rel_level_linear(const struct sp_sampler_view
*sp_sview
,
1937 const struct sp_sampler
*sp_samp
,
1938 const float lod
[TGSI_QUAD_SIZE
],
1939 float level
[TGSI_QUAD_SIZE
])
1941 clamp_lod(sp_sview
, sp_samp
, lod
, level
);
1945 mip_filter_linear(const struct sp_sampler_view
*sp_sview
,
1946 const struct sp_sampler
*sp_samp
,
1947 img_filter_func min_filter
,
1948 img_filter_func mag_filter
,
1949 const float s
[TGSI_QUAD_SIZE
],
1950 const float t
[TGSI_QUAD_SIZE
],
1951 const float p
[TGSI_QUAD_SIZE
],
1952 const float c0
[TGSI_QUAD_SIZE
],
1953 const float lod_in
[TGSI_QUAD_SIZE
],
1954 const struct filter_args
*filt_args
,
1955 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1957 const struct pipe_sampler_view
*psview
= &sp_sview
->base
;
1959 float lod
[TGSI_QUAD_SIZE
];
1960 struct img_filter_args args
;
1962 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, filt_args
->control
, lod
);
1964 args
.offset
= filt_args
->offset
;
1965 args
.gather_only
= filt_args
->control
== TGSI_SAMPLER_GATHER
;
1966 args
.gather_comp
= get_gather_component(lod_in
);
1968 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1969 const int level0
= psview
->u
.tex
.first_level
+ (int)lod
[j
];
1974 args
.face_id
= filt_args
->faces
[j
];
1977 args
.level
= psview
->u
.tex
.first_level
;
1978 mag_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
1980 else if (level0
>= (int) psview
->u
.tex
.last_level
) {
1981 args
.level
= psview
->u
.tex
.last_level
;
1982 min_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
1985 float levelBlend
= frac(lod
[j
]);
1986 float rgbax
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
1989 args
.level
= level0
;
1990 min_filter(sp_sview
, sp_samp
, &args
, &rgbax
[0][0]);
1991 args
.level
= level0
+1;
1992 min_filter(sp_sview
, sp_samp
, &args
, &rgbax
[0][1]);
1994 for (c
= 0; c
< 4; c
++) {
1995 rgba
[c
][j
] = lerp(levelBlend
, rgbax
[c
][0], rgbax
[c
][1]);
2001 print_sample_4(__FUNCTION__
, rgba
);
2007 * Get mip level relative to base level for nearest mip filter
2010 mip_rel_level_nearest(const struct sp_sampler_view
*sp_sview
,
2011 const struct sp_sampler
*sp_samp
,
2012 const float lod
[TGSI_QUAD_SIZE
],
2013 float level
[TGSI_QUAD_SIZE
])
2017 clamp_lod(sp_sview
, sp_samp
, lod
, level
);
2018 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++)
2019 /* TODO: It should rather be:
2020 * level[j] = ceil(level[j] + 0.5F) - 1.0F;
2022 level
[j
] = (int)(level
[j
] + 0.5F
);
2026 * Compute nearest mipmap level from texcoords.
2027 * Then sample the texture level for four elements of a quad.
2028 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
2031 mip_filter_nearest(const struct sp_sampler_view
*sp_sview
,
2032 const struct sp_sampler
*sp_samp
,
2033 img_filter_func min_filter
,
2034 img_filter_func mag_filter
,
2035 const float s
[TGSI_QUAD_SIZE
],
2036 const float t
[TGSI_QUAD_SIZE
],
2037 const float p
[TGSI_QUAD_SIZE
],
2038 const float c0
[TGSI_QUAD_SIZE
],
2039 const float lod_in
[TGSI_QUAD_SIZE
],
2040 const struct filter_args
*filt_args
,
2041 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2043 const struct pipe_sampler_view
*psview
= &sp_sview
->base
;
2044 float lod
[TGSI_QUAD_SIZE
];
2046 struct img_filter_args args
;
2048 args
.offset
= filt_args
->offset
;
2049 args
.gather_only
= filt_args
->control
== TGSI_SAMPLER_GATHER
;
2050 args
.gather_comp
= get_gather_component(lod_in
);
2052 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, filt_args
->control
, lod
);
2054 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2058 args
.face_id
= filt_args
->faces
[j
];
2061 args
.level
= psview
->u
.tex
.first_level
;
2062 mag_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2064 const int level
= psview
->u
.tex
.first_level
+ (int)(lod
[j
] + 0.5F
);
2065 args
.level
= MIN2(level
, (int)psview
->u
.tex
.last_level
);
2066 min_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2071 print_sample_4(__FUNCTION__
, rgba
);
2077 * Get mip level relative to base level for none mip filter
2080 mip_rel_level_none(const struct sp_sampler_view
*sp_sview
,
2081 const struct sp_sampler
*sp_samp
,
2082 const float lod
[TGSI_QUAD_SIZE
],
2083 float level
[TGSI_QUAD_SIZE
])
2087 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2093 mip_filter_none(const struct sp_sampler_view
*sp_sview
,
2094 const struct sp_sampler
*sp_samp
,
2095 img_filter_func min_filter
,
2096 img_filter_func mag_filter
,
2097 const float s
[TGSI_QUAD_SIZE
],
2098 const float t
[TGSI_QUAD_SIZE
],
2099 const float p
[TGSI_QUAD_SIZE
],
2100 const float c0
[TGSI_QUAD_SIZE
],
2101 const float lod_in
[TGSI_QUAD_SIZE
],
2102 const struct filter_args
*filt_args
,
2103 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2105 float lod
[TGSI_QUAD_SIZE
];
2107 struct img_filter_args args
;
2109 args
.level
= sp_sview
->base
.u
.tex
.first_level
;
2110 args
.offset
= filt_args
->offset
;
2111 args
.gather_only
= filt_args
->control
== TGSI_SAMPLER_GATHER
;
2113 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, filt_args
->control
, lod
);
2115 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2119 args
.face_id
= filt_args
->faces
[j
];
2121 mag_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2124 min_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2131 * Get mip level relative to base level for none mip filter
2134 mip_rel_level_none_no_filter_select(const struct sp_sampler_view
*sp_sview
,
2135 const struct sp_sampler
*sp_samp
,
2136 const float lod
[TGSI_QUAD_SIZE
],
2137 float level
[TGSI_QUAD_SIZE
])
2139 mip_rel_level_none(sp_sview
, sp_samp
, lod
, level
);
2143 mip_filter_none_no_filter_select(const struct sp_sampler_view
*sp_sview
,
2144 const struct sp_sampler
*sp_samp
,
2145 img_filter_func min_filter
,
2146 img_filter_func mag_filter
,
2147 const float s
[TGSI_QUAD_SIZE
],
2148 const float t
[TGSI_QUAD_SIZE
],
2149 const float p
[TGSI_QUAD_SIZE
],
2150 const float c0
[TGSI_QUAD_SIZE
],
2151 const float lod_in
[TGSI_QUAD_SIZE
],
2152 const struct filter_args
*filt_args
,
2153 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2156 struct img_filter_args args
;
2157 args
.level
= sp_sview
->base
.u
.tex
.first_level
;
2158 args
.offset
= filt_args
->offset
;
2159 args
.gather_only
= filt_args
->control
== TGSI_SAMPLER_GATHER
;
2160 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2164 args
.face_id
= filt_args
->faces
[j
];
2165 mag_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2170 /* For anisotropic filtering */
2171 #define WEIGHT_LUT_SIZE 1024
2173 static const float *weightLut
= NULL
;
2176 * Creates the look-up table used to speed-up EWA sampling
2179 create_filter_table(void)
2183 float *lut
= (float *) MALLOC(WEIGHT_LUT_SIZE
* sizeof(float));
2185 for (i
= 0; i
< WEIGHT_LUT_SIZE
; ++i
) {
2186 const float alpha
= 2;
2187 const float r2
= (float) i
/ (float) (WEIGHT_LUT_SIZE
- 1);
2188 const float weight
= (float) exp(-alpha
* r2
);
2197 * Elliptical weighted average (EWA) filter for producing high quality
2198 * anisotropic filtered results.
2199 * Based on the Higher Quality Elliptical Weighted Average Filter
2200 * published by Paul S. Heckbert in his Master's Thesis
2201 * "Fundamentals of Texture Mapping and Image Warping" (1989)
2204 img_filter_2d_ewa(const struct sp_sampler_view
*sp_sview
,
2205 const struct sp_sampler
*sp_samp
,
2206 img_filter_func min_filter
,
2207 img_filter_func mag_filter
,
2208 const float s
[TGSI_QUAD_SIZE
],
2209 const float t
[TGSI_QUAD_SIZE
],
2210 const float p
[TGSI_QUAD_SIZE
],
2211 const uint faces
[TGSI_QUAD_SIZE
],
2212 const int8_t *offset
,
2214 const float dudx
, const float dvdx
,
2215 const float dudy
, const float dvdy
,
2216 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2218 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
2220 // ??? Won't the image filters blow up if level is negative?
2221 const unsigned level0
= level
> 0 ? level
: 0;
2222 const float scaling
= 1.0f
/ (1 << level0
);
2223 const int width
= u_minify(texture
->width0
, level0
);
2224 const int height
= u_minify(texture
->height0
, level0
);
2225 struct img_filter_args args
;
2226 const float ux
= dudx
* scaling
;
2227 const float vx
= dvdx
* scaling
;
2228 const float uy
= dudy
* scaling
;
2229 const float vy
= dvdy
* scaling
;
2231 /* compute ellipse coefficients to bound the region:
2232 * A*x*x + B*x*y + C*y*y = F.
2234 float A
= vx
*vx
+vy
*vy
+1;
2235 float B
= -2*(ux
*vx
+uy
*vy
);
2236 float C
= ux
*ux
+uy
*uy
+1;
2237 float F
= A
*C
-B
*B
/4.0f
;
2239 /* check if it is an ellipse */
2240 /* assert(F > 0.0); */
2242 /* Compute the ellipse's (u,v) bounding box in texture space */
2243 const float d
= -B
*B
+4.0f
*C
*A
;
2244 const float box_u
= 2.0f
/ d
* sqrtf(d
*C
*F
); /* box_u -> half of bbox with */
2245 const float box_v
= 2.0f
/ d
* sqrtf(A
*d
*F
); /* box_v -> half of bbox height */
2247 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2248 float s_buffer
[TGSI_QUAD_SIZE
];
2249 float t_buffer
[TGSI_QUAD_SIZE
];
2250 float weight_buffer
[TGSI_QUAD_SIZE
];
2253 /* For each quad, the du and dx values are the same and so the ellipse is
2254 * also the same. Note that texel/image access can only be performed using
2255 * a quad, i.e. it is not possible to get the pixel value for a single
2256 * tex coord. In order to have a better performance, the access is buffered
2257 * using the s_buffer/t_buffer and weight_buffer. Only when the buffer is
2258 * full, then the pixel values are read from the image.
2260 const float ddq
= 2 * A
;
2262 /* Scale ellipse formula to directly index the Filter Lookup Table.
2263 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
2265 const double formScale
= (double) (WEIGHT_LUT_SIZE
- 1) / F
;
2269 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
2272 args
.offset
= offset
;
2274 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2275 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
2276 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
2277 * value, q, is less than F, we're inside the ellipse
2279 const float tex_u
= -0.5F
+ s
[j
] * texture
->width0
* scaling
;
2280 const float tex_v
= -0.5F
+ t
[j
] * texture
->height0
* scaling
;
2282 const int u0
= (int) floorf(tex_u
- box_u
);
2283 const int u1
= (int) ceilf(tex_u
+ box_u
);
2284 const int v0
= (int) floorf(tex_v
- box_v
);
2285 const int v1
= (int) ceilf(tex_v
+ box_v
);
2286 const float U
= u0
- tex_u
;
2288 float num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
2289 unsigned buffer_next
= 0;
2292 args
.face_id
= faces
[j
];
2294 for (v
= v0
; v
<= v1
; ++v
) {
2295 const float V
= v
- tex_v
;
2296 float dq
= A
* (2 * U
+ 1) + B
* V
;
2297 float q
= (C
* V
+ B
* U
) * V
+ A
* U
* U
;
2300 for (u
= u0
; u
<= u1
; ++u
) {
2301 /* Note that the ellipse has been pre-scaled so F =
2302 * WEIGHT_LUT_SIZE - 1
2304 if (q
< WEIGHT_LUT_SIZE
) {
2305 /* as a LUT is used, q must never be negative;
2306 * should not happen, though
2308 const int qClamped
= q
>= 0.0F
? q
: 0;
2309 const float weight
= weightLut
[qClamped
];
2311 weight_buffer
[buffer_next
] = weight
;
2312 s_buffer
[buffer_next
] = u
/ ((float) width
);
2313 t_buffer
[buffer_next
] = v
/ ((float) height
);
2316 if (buffer_next
== TGSI_QUAD_SIZE
) {
2317 /* 4 texel coords are in the buffer -> read it now */
2319 /* it is assumed that samp->min_img_filter is set to
2320 * img_filter_2d_nearest or one of the
2321 * accelerated img_filter_2d_nearest_XXX functions.
2323 for (jj
= 0; jj
< buffer_next
; jj
++) {
2324 args
.s
= s_buffer
[jj
];
2325 args
.t
= t_buffer
[jj
];
2327 min_filter(sp_sview
, sp_samp
, &args
, &rgba_temp
[0][jj
]);
2328 num
[0] += weight_buffer
[jj
] * rgba_temp
[0][jj
];
2329 num
[1] += weight_buffer
[jj
] * rgba_temp
[1][jj
];
2330 num
[2] += weight_buffer
[jj
] * rgba_temp
[2][jj
];
2331 num
[3] += weight_buffer
[jj
] * rgba_temp
[3][jj
];
2344 /* if the tex coord buffer contains unread values, we will read
2347 if (buffer_next
> 0) {
2349 /* it is assumed that samp->min_img_filter is set to
2350 * img_filter_2d_nearest or one of the
2351 * accelerated img_filter_2d_nearest_XXX functions.
2353 for (jj
= 0; jj
< buffer_next
; jj
++) {
2354 args
.s
= s_buffer
[jj
];
2355 args
.t
= t_buffer
[jj
];
2357 min_filter(sp_sview
, sp_samp
, &args
, &rgba_temp
[0][jj
]);
2358 num
[0] += weight_buffer
[jj
] * rgba_temp
[0][jj
];
2359 num
[1] += weight_buffer
[jj
] * rgba_temp
[1][jj
];
2360 num
[2] += weight_buffer
[jj
] * rgba_temp
[2][jj
];
2361 num
[3] += weight_buffer
[jj
] * rgba_temp
[3][jj
];
2366 /* Reaching this place would mean that no pixels intersected
2367 * the ellipse. This should never happen because the filter
2368 * we use always intersects at least one pixel.
2375 /* not enough pixels in resampling, resort to direct interpolation */
2379 min_filter(sp_sview
, sp_samp
, &args
, &rgba_temp
[0][j
]);
2381 num
[0] = rgba_temp
[0][j
];
2382 num
[1] = rgba_temp
[1][j
];
2383 num
[2] = rgba_temp
[2][j
];
2384 num
[3] = rgba_temp
[3][j
];
2387 rgba
[0][j
] = num
[0] / den
;
2388 rgba
[1][j
] = num
[1] / den
;
2389 rgba
[2][j
] = num
[2] / den
;
2390 rgba
[3][j
] = num
[3] / den
;
2396 * Get mip level relative to base level for linear mip filter
2399 mip_rel_level_linear_aniso(const struct sp_sampler_view
*sp_sview
,
2400 const struct sp_sampler
*sp_samp
,
2401 const float lod
[TGSI_QUAD_SIZE
],
2402 float level
[TGSI_QUAD_SIZE
])
2404 mip_rel_level_linear(sp_sview
, sp_samp
, lod
, level
);
2408 * Sample 2D texture using an anisotropic filter.
2411 mip_filter_linear_aniso(const struct sp_sampler_view
*sp_sview
,
2412 const struct sp_sampler
*sp_samp
,
2413 img_filter_func min_filter
,
2414 img_filter_func mag_filter
,
2415 const float s
[TGSI_QUAD_SIZE
],
2416 const float t
[TGSI_QUAD_SIZE
],
2417 const float p
[TGSI_QUAD_SIZE
],
2418 const float c0
[TGSI_QUAD_SIZE
],
2419 const float lod_in
[TGSI_QUAD_SIZE
],
2420 const struct filter_args
*filt_args
,
2421 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2423 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
2424 const struct pipe_sampler_view
*psview
= &sp_sview
->base
;
2427 float lod
[TGSI_QUAD_SIZE
];
2429 const float s_to_u
= u_minify(texture
->width0
, psview
->u
.tex
.first_level
);
2430 const float t_to_v
= u_minify(texture
->height0
, psview
->u
.tex
.first_level
);
2431 const float dudx
= (s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]) * s_to_u
;
2432 const float dudy
= (s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]) * s_to_u
;
2433 const float dvdx
= (t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]) * t_to_v
;
2434 const float dvdy
= (t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]) * t_to_v
;
2435 struct img_filter_args args
;
2437 args
.offset
= filt_args
->offset
;
2439 if (filt_args
->control
== TGSI_SAMPLER_LOD_BIAS
||
2440 filt_args
->control
== TGSI_SAMPLER_LOD_NONE
||
2442 filt_args
->control
== TGSI_SAMPLER_DERIVS_EXPLICIT
) {
2443 /* note: instead of working with Px and Py, we will use the
2444 * squared length instead, to avoid sqrt.
2446 const float Px2
= dudx
* dudx
+ dvdx
* dvdx
;
2447 const float Py2
= dudy
* dudy
+ dvdy
* dvdy
;
2452 const float maxEccentricity
= sp_samp
->base
.max_anisotropy
* sp_samp
->base
.max_anisotropy
;
2463 /* if the eccentricity of the ellipse is too big, scale up the shorter
2464 * of the two vectors to limit the maximum amount of work per pixel
2467 if (e
> maxEccentricity
) {
2468 /* float s=e / maxEccentricity;
2472 Pmin2
= Pmax2
/ maxEccentricity
;
2475 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
2476 * this since 0.5*log(x) = log(sqrt(x))
2478 lambda
= 0.5F
* util_fast_log2(Pmin2
) + sp_samp
->base
.lod_bias
;
2479 compute_lod(&sp_samp
->base
, filt_args
->control
, lambda
, lod_in
, lod
);
2482 assert(filt_args
->control
== TGSI_SAMPLER_LOD_EXPLICIT
||
2483 filt_args
->control
== TGSI_SAMPLER_LOD_ZERO
);
2484 compute_lod(&sp_samp
->base
, filt_args
->control
, sp_samp
->base
.lod_bias
, lod_in
, lod
);
2487 /* XXX: Take into account all lod values.
2490 level0
= psview
->u
.tex
.first_level
+ (int)lambda
;
2492 /* If the ellipse covers the whole image, we can
2493 * simply return the average of the whole image.
2495 if (level0
>= (int) psview
->u
.tex
.last_level
) {
2497 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2501 args
.level
= psview
->u
.tex
.last_level
;
2502 args
.face_id
= filt_args
->faces
[j
];
2504 * XXX: we overwrote any linear filter with nearest, so this
2505 * isn't right (albeit if last level is 1x1 and no border it
2506 * will work just the same).
2508 min_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2512 /* don't bother interpolating between multiple LODs; it doesn't
2513 * seem to be worth the extra running time.
2515 img_filter_2d_ewa(sp_sview
, sp_samp
, min_filter
, mag_filter
,
2516 s
, t
, p
, filt_args
->faces
, filt_args
->offset
,
2517 level0
, dudx
, dvdx
, dudy
, dvdy
, rgba
);
2521 print_sample_4(__FUNCTION__
, rgba
);
2526 * Get mip level relative to base level for linear mip filter
2529 mip_rel_level_linear_2d_linear_repeat_POT(
2530 const struct sp_sampler_view
*sp_sview
,
2531 const struct sp_sampler
*sp_samp
,
2532 const float lod
[TGSI_QUAD_SIZE
],
2533 float level
[TGSI_QUAD_SIZE
])
2535 mip_rel_level_linear(sp_sview
, sp_samp
, lod
, level
);
2539 * Specialized version of mip_filter_linear with hard-wired calls to
2540 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
2543 mip_filter_linear_2d_linear_repeat_POT(
2544 const struct sp_sampler_view
*sp_sview
,
2545 const struct sp_sampler
*sp_samp
,
2546 img_filter_func min_filter
,
2547 img_filter_func mag_filter
,
2548 const float s
[TGSI_QUAD_SIZE
],
2549 const float t
[TGSI_QUAD_SIZE
],
2550 const float p
[TGSI_QUAD_SIZE
],
2551 const float c0
[TGSI_QUAD_SIZE
],
2552 const float lod_in
[TGSI_QUAD_SIZE
],
2553 const struct filter_args
*filt_args
,
2554 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2556 const struct pipe_sampler_view
*psview
= &sp_sview
->base
;
2558 float lod
[TGSI_QUAD_SIZE
];
2560 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, filt_args
->control
, lod
);
2562 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2563 const int level0
= psview
->u
.tex
.first_level
+ (int)lod
[j
];
2564 struct img_filter_args args
;
2565 /* Catches both negative and large values of level0:
2570 args
.face_id
= filt_args
->faces
[j
];
2571 args
.offset
= filt_args
->offset
;
2572 args
.gather_only
= filt_args
->control
== TGSI_SAMPLER_GATHER
;
2573 if ((unsigned)level0
>= psview
->u
.tex
.last_level
) {
2575 args
.level
= psview
->u
.tex
.first_level
;
2577 args
.level
= psview
->u
.tex
.last_level
;
2578 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, &args
,
2583 const float levelBlend
= frac(lod
[j
]);
2584 float rgbax
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2587 args
.level
= level0
;
2588 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, &args
, &rgbax
[0][0]);
2589 args
.level
= level0
+1;
2590 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, &args
, &rgbax
[0][1]);
2592 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
2593 rgba
[c
][j
] = lerp(levelBlend
, rgbax
[c
][0], rgbax
[c
][1]);
2598 print_sample_4(__FUNCTION__
, rgba
);
2602 static const struct sp_filter_funcs funcs_linear
= {
2603 mip_rel_level_linear
,
2607 static const struct sp_filter_funcs funcs_nearest
= {
2608 mip_rel_level_nearest
,
2612 static const struct sp_filter_funcs funcs_none
= {
2617 static const struct sp_filter_funcs funcs_none_no_filter_select
= {
2618 mip_rel_level_none_no_filter_select
,
2619 mip_filter_none_no_filter_select
2622 static const struct sp_filter_funcs funcs_linear_aniso
= {
2623 mip_rel_level_linear_aniso
,
2624 mip_filter_linear_aniso
2627 static const struct sp_filter_funcs funcs_linear_2d_linear_repeat_POT
= {
2628 mip_rel_level_linear_2d_linear_repeat_POT
,
2629 mip_filter_linear_2d_linear_repeat_POT
2633 * Do shadow/depth comparisons.
2636 sample_compare(const struct sp_sampler_view
*sp_sview
,
2637 const struct sp_sampler
*sp_samp
,
2638 const float s
[TGSI_QUAD_SIZE
],
2639 const float t
[TGSI_QUAD_SIZE
],
2640 const float p
[TGSI_QUAD_SIZE
],
2641 const float c0
[TGSI_QUAD_SIZE
],
2642 const float c1
[TGSI_QUAD_SIZE
],
2643 enum tgsi_sampler_control control
,
2644 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2646 const struct pipe_sampler_state
*sampler
= &sp_samp
->base
;
2648 int k
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2650 const struct util_format_description
*format_desc
=
2651 util_format_description(sp_sview
->base
.format
);
2652 /* not entirely sure we couldn't end up with non-valid swizzle here */
2653 const unsigned chan_type
=
2654 format_desc
->swizzle
[0] <= PIPE_SWIZZLE_W
?
2655 format_desc
->channel
[format_desc
->swizzle
[0]].type
:
2656 UTIL_FORMAT_TYPE_FLOAT
;
2657 const bool is_gather
= (control
== TGSI_SAMPLER_GATHER
);
2660 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
2661 * for 2D Array texture we need to use the 'c0' (aka Q).
2662 * When we sampled the depth texture, the depth value was put into all
2663 * RGBA channels. We look at the red channel here.
2666 if (sp_sview
->base
.target
== PIPE_TEXTURE_2D_ARRAY
||
2667 sp_sview
->base
.target
== PIPE_TEXTURE_CUBE
) {
2672 } else if (sp_sview
->base
.target
== PIPE_TEXTURE_CUBE_ARRAY
) {
2684 if (chan_type
!= UTIL_FORMAT_TYPE_FLOAT
) {
2686 * clamping is a result of conversion to texture format, hence
2687 * doesn't happen with floats. Technically also should do comparison
2688 * in texture format (quantization!).
2690 pc
[0] = CLAMP(pc
[0], 0.0F
, 1.0F
);
2691 pc
[1] = CLAMP(pc
[1], 0.0F
, 1.0F
);
2692 pc
[2] = CLAMP(pc
[2], 0.0F
, 1.0F
);
2693 pc
[3] = CLAMP(pc
[3], 0.0F
, 1.0F
);
2696 for (v
= 0; v
< (is_gather
? TGSI_NUM_CHANNELS
: 1); v
++) {
2697 /* compare four texcoords vs. four texture samples */
2698 switch (sampler
->compare_func
) {
2699 case PIPE_FUNC_LESS
:
2700 k
[v
][0] = pc
[0] < rgba
[v
][0];
2701 k
[v
][1] = pc
[1] < rgba
[v
][1];
2702 k
[v
][2] = pc
[2] < rgba
[v
][2];
2703 k
[v
][3] = pc
[3] < rgba
[v
][3];
2705 case PIPE_FUNC_LEQUAL
:
2706 k
[v
][0] = pc
[0] <= rgba
[v
][0];
2707 k
[v
][1] = pc
[1] <= rgba
[v
][1];
2708 k
[v
][2] = pc
[2] <= rgba
[v
][2];
2709 k
[v
][3] = pc
[3] <= rgba
[v
][3];
2711 case PIPE_FUNC_GREATER
:
2712 k
[v
][0] = pc
[0] > rgba
[v
][0];
2713 k
[v
][1] = pc
[1] > rgba
[v
][1];
2714 k
[v
][2] = pc
[2] > rgba
[v
][2];
2715 k
[v
][3] = pc
[3] > rgba
[v
][3];
2717 case PIPE_FUNC_GEQUAL
:
2718 k
[v
][0] = pc
[0] >= rgba
[v
][0];
2719 k
[v
][1] = pc
[1] >= rgba
[v
][1];
2720 k
[v
][2] = pc
[2] >= rgba
[v
][2];
2721 k
[v
][3] = pc
[3] >= rgba
[v
][3];
2723 case PIPE_FUNC_EQUAL
:
2724 k
[v
][0] = pc
[0] == rgba
[v
][0];
2725 k
[v
][1] = pc
[1] == rgba
[v
][1];
2726 k
[v
][2] = pc
[2] == rgba
[v
][2];
2727 k
[v
][3] = pc
[3] == rgba
[v
][3];
2729 case PIPE_FUNC_NOTEQUAL
:
2730 k
[v
][0] = pc
[0] != rgba
[v
][0];
2731 k
[v
][1] = pc
[1] != rgba
[v
][1];
2732 k
[v
][2] = pc
[2] != rgba
[v
][2];
2733 k
[v
][3] = pc
[3] != rgba
[v
][3];
2735 case PIPE_FUNC_ALWAYS
:
2736 k
[v
][0] = k
[v
][1] = k
[v
][2] = k
[v
][3] = 1;
2738 case PIPE_FUNC_NEVER
:
2739 k
[v
][0] = k
[v
][1] = k
[v
][2] = k
[v
][3] = 0;
2742 k
[v
][0] = k
[v
][1] = k
[v
][2] = k
[v
][3] = 0;
2749 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2750 for (v
= 0; v
< TGSI_NUM_CHANNELS
; v
++) {
2751 rgba
[v
][j
] = k
[v
][j
];
2755 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2756 rgba
[0][j
] = k
[0][j
];
2757 rgba
[1][j
] = k
[0][j
];
2758 rgba
[2][j
] = k
[0][j
];
2765 do_swizzling(const struct pipe_sampler_view
*sview
,
2766 float in
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
],
2767 float out
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2770 const unsigned swizzle_r
= sview
->swizzle_r
;
2771 const unsigned swizzle_g
= sview
->swizzle_g
;
2772 const unsigned swizzle_b
= sview
->swizzle_b
;
2773 const unsigned swizzle_a
= sview
->swizzle_a
;
2775 switch (swizzle_r
) {
2776 case PIPE_SWIZZLE_0
:
2777 for (j
= 0; j
< 4; j
++)
2780 case PIPE_SWIZZLE_1
:
2781 for (j
= 0; j
< 4; j
++)
2785 assert(swizzle_r
< 4);
2786 for (j
= 0; j
< 4; j
++)
2787 out
[0][j
] = in
[swizzle_r
][j
];
2790 switch (swizzle_g
) {
2791 case PIPE_SWIZZLE_0
:
2792 for (j
= 0; j
< 4; j
++)
2795 case PIPE_SWIZZLE_1
:
2796 for (j
= 0; j
< 4; j
++)
2800 assert(swizzle_g
< 4);
2801 for (j
= 0; j
< 4; j
++)
2802 out
[1][j
] = in
[swizzle_g
][j
];
2805 switch (swizzle_b
) {
2806 case PIPE_SWIZZLE_0
:
2807 for (j
= 0; j
< 4; j
++)
2810 case PIPE_SWIZZLE_1
:
2811 for (j
= 0; j
< 4; j
++)
2815 assert(swizzle_b
< 4);
2816 for (j
= 0; j
< 4; j
++)
2817 out
[2][j
] = in
[swizzle_b
][j
];
2820 switch (swizzle_a
) {
2821 case PIPE_SWIZZLE_0
:
2822 for (j
= 0; j
< 4; j
++)
2825 case PIPE_SWIZZLE_1
:
2826 for (j
= 0; j
< 4; j
++)
2830 assert(swizzle_a
< 4);
2831 for (j
= 0; j
< 4; j
++)
2832 out
[3][j
] = in
[swizzle_a
][j
];
2837 static wrap_nearest_func
2838 get_nearest_unorm_wrap(unsigned mode
)
2841 case PIPE_TEX_WRAP_CLAMP
:
2842 return wrap_nearest_unorm_clamp
;
2843 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2844 return wrap_nearest_unorm_clamp_to_edge
;
2845 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2846 return wrap_nearest_unorm_clamp_to_border
;
2848 debug_printf("illegal wrap mode %d with non-normalized coords\n", mode
);
2849 return wrap_nearest_unorm_clamp
;
2854 static wrap_nearest_func
2855 get_nearest_wrap(unsigned mode
)
2858 case PIPE_TEX_WRAP_REPEAT
:
2859 return wrap_nearest_repeat
;
2860 case PIPE_TEX_WRAP_CLAMP
:
2861 return wrap_nearest_clamp
;
2862 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2863 return wrap_nearest_clamp_to_edge
;
2864 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2865 return wrap_nearest_clamp_to_border
;
2866 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2867 return wrap_nearest_mirror_repeat
;
2868 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2869 return wrap_nearest_mirror_clamp
;
2870 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2871 return wrap_nearest_mirror_clamp_to_edge
;
2872 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2873 return wrap_nearest_mirror_clamp_to_border
;
2876 return wrap_nearest_repeat
;
2881 static wrap_linear_func
2882 get_linear_unorm_wrap(unsigned mode
)
2885 case PIPE_TEX_WRAP_CLAMP
:
2886 return wrap_linear_unorm_clamp
;
2887 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2888 return wrap_linear_unorm_clamp_to_edge
;
2889 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2890 return wrap_linear_unorm_clamp_to_border
;
2892 debug_printf("illegal wrap mode %d with non-normalized coords\n", mode
);
2893 return wrap_linear_unorm_clamp
;
2898 static wrap_linear_func
2899 get_linear_wrap(unsigned mode
)
2902 case PIPE_TEX_WRAP_REPEAT
:
2903 return wrap_linear_repeat
;
2904 case PIPE_TEX_WRAP_CLAMP
:
2905 return wrap_linear_clamp
;
2906 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2907 return wrap_linear_clamp_to_edge
;
2908 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2909 return wrap_linear_clamp_to_border
;
2910 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2911 return wrap_linear_mirror_repeat
;
2912 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2913 return wrap_linear_mirror_clamp
;
2914 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2915 return wrap_linear_mirror_clamp_to_edge
;
2916 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2917 return wrap_linear_mirror_clamp_to_border
;
2920 return wrap_linear_repeat
;
2926 * Is swizzling needed for the given state key?
2929 any_swizzle(const struct pipe_sampler_view
*view
)
2931 return (view
->swizzle_r
!= PIPE_SWIZZLE_X
||
2932 view
->swizzle_g
!= PIPE_SWIZZLE_Y
||
2933 view
->swizzle_b
!= PIPE_SWIZZLE_Z
||
2934 view
->swizzle_a
!= PIPE_SWIZZLE_W
);
2938 static img_filter_func
2939 get_img_filter(const struct sp_sampler_view
*sp_sview
,
2940 const struct pipe_sampler_state
*sampler
,
2941 unsigned filter
, bool gather
)
2943 switch (sp_sview
->base
.target
) {
2945 case PIPE_TEXTURE_1D
:
2946 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2947 return img_filter_1d_nearest
;
2949 return img_filter_1d_linear
;
2951 case PIPE_TEXTURE_1D_ARRAY
:
2952 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2953 return img_filter_1d_array_nearest
;
2955 return img_filter_1d_array_linear
;
2957 case PIPE_TEXTURE_2D
:
2958 case PIPE_TEXTURE_RECT
:
2959 /* Try for fast path:
2961 if (!gather
&& sp_sview
->pot2d
&&
2962 sampler
->wrap_s
== sampler
->wrap_t
&&
2963 sampler
->normalized_coords
)
2965 switch (sampler
->wrap_s
) {
2966 case PIPE_TEX_WRAP_REPEAT
:
2968 case PIPE_TEX_FILTER_NEAREST
:
2969 return img_filter_2d_nearest_repeat_POT
;
2970 case PIPE_TEX_FILTER_LINEAR
:
2971 return img_filter_2d_linear_repeat_POT
;
2976 case PIPE_TEX_WRAP_CLAMP
:
2978 case PIPE_TEX_FILTER_NEAREST
:
2979 return img_filter_2d_nearest_clamp_POT
;
2985 /* Otherwise use default versions:
2987 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2988 return img_filter_2d_nearest
;
2990 return img_filter_2d_linear
;
2992 case PIPE_TEXTURE_2D_ARRAY
:
2993 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2994 return img_filter_2d_array_nearest
;
2996 return img_filter_2d_array_linear
;
2998 case PIPE_TEXTURE_CUBE
:
2999 if (filter
== PIPE_TEX_FILTER_NEAREST
)
3000 return img_filter_cube_nearest
;
3002 return img_filter_cube_linear
;
3004 case PIPE_TEXTURE_CUBE_ARRAY
:
3005 if (filter
== PIPE_TEX_FILTER_NEAREST
)
3006 return img_filter_cube_array_nearest
;
3008 return img_filter_cube_array_linear
;
3010 case PIPE_TEXTURE_3D
:
3011 if (filter
== PIPE_TEX_FILTER_NEAREST
)
3012 return img_filter_3d_nearest
;
3014 return img_filter_3d_linear
;
3018 return img_filter_1d_nearest
;
3023 * Get mip filter funcs, and optionally both img min filter and img mag
3024 * filter. Note that both img filter function pointers must be either non-NULL
3028 get_filters(const struct sp_sampler_view
*sp_sview
,
3029 const struct sp_sampler
*sp_samp
,
3030 const enum tgsi_sampler_control control
,
3031 const struct sp_filter_funcs
**funcs
,
3032 img_filter_func
*min
,
3033 img_filter_func
*mag
)
3036 if (control
== TGSI_SAMPLER_GATHER
) {
3037 *funcs
= &funcs_nearest
;
3039 *min
= get_img_filter(sp_sview
, &sp_samp
->base
,
3040 PIPE_TEX_FILTER_LINEAR
, true);
3042 } else if (sp_sview
->pot2d
& sp_samp
->min_mag_equal_repeat_linear
) {
3043 *funcs
= &funcs_linear_2d_linear_repeat_POT
;
3045 *funcs
= sp_samp
->filter_funcs
;
3048 *min
= get_img_filter(sp_sview
, &sp_samp
->base
,
3049 sp_samp
->min_img_filter
, false);
3050 if (sp_samp
->min_mag_equal
) {
3053 *mag
= get_img_filter(sp_sview
, &sp_samp
->base
,
3054 sp_samp
->base
.mag_img_filter
, false);
3061 sample_mip(const struct sp_sampler_view
*sp_sview
,
3062 const struct sp_sampler
*sp_samp
,
3063 const float s
[TGSI_QUAD_SIZE
],
3064 const float t
[TGSI_QUAD_SIZE
],
3065 const float p
[TGSI_QUAD_SIZE
],
3066 const float c0
[TGSI_QUAD_SIZE
],
3067 const float lod
[TGSI_QUAD_SIZE
],
3068 const struct filter_args
*filt_args
,
3069 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3071 const struct sp_filter_funcs
*funcs
= NULL
;
3072 img_filter_func min_img_filter
= NULL
;
3073 img_filter_func mag_img_filter
= NULL
;
3075 get_filters(sp_sview
, sp_samp
, filt_args
->control
,
3076 &funcs
, &min_img_filter
, &mag_img_filter
);
3078 funcs
->filter(sp_sview
, sp_samp
, min_img_filter
, mag_img_filter
,
3079 s
, t
, p
, c0
, lod
, filt_args
, rgba
);
3081 if (sp_samp
->base
.compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
3082 sample_compare(sp_sview
, sp_samp
, s
, t
, p
, c0
,
3083 lod
, filt_args
->control
, rgba
);
3086 if (sp_sview
->need_swizzle
&& filt_args
->control
!= TGSI_SAMPLER_GATHER
) {
3087 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
3088 memcpy(rgba_temp
, rgba
, sizeof(rgba_temp
));
3089 do_swizzling(&sp_sview
->base
, rgba_temp
, rgba
);
3096 * This function uses cube texture coordinates to choose a face of a cube and
3097 * computes the 2D cube face coordinates. Puts face info into the sampler
3101 convert_cube(const struct sp_sampler_view
*sp_sview
,
3102 const struct sp_sampler
*sp_samp
,
3103 const float s
[TGSI_QUAD_SIZE
],
3104 const float t
[TGSI_QUAD_SIZE
],
3105 const float p
[TGSI_QUAD_SIZE
],
3106 const float c0
[TGSI_QUAD_SIZE
],
3107 float ssss
[TGSI_QUAD_SIZE
],
3108 float tttt
[TGSI_QUAD_SIZE
],
3109 float pppp
[TGSI_QUAD_SIZE
],
3110 uint faces
[TGSI_QUAD_SIZE
])
3120 direction target sc tc ma
3121 ---------- ------------------------------- --- --- ---
3122 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
3123 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
3124 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
3125 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
3126 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
3127 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
3130 /* Choose the cube face and compute new s/t coords for the 2D face.
3132 * Use the same cube face for all four pixels in the quad.
3134 * This isn't ideal, but if we want to use a different cube face
3135 * per pixel in the quad, we'd have to also compute the per-face
3136 * LOD here too. That's because the four post-face-selection
3137 * texcoords are no longer related to each other (they're
3138 * per-face!) so we can't use subtraction to compute the partial
3139 * deriviates to compute the LOD. Doing so (near cube edges
3140 * anyway) gives us pretty much random values.
3143 /* use the average of the four pixel's texcoords to choose the face */
3144 const float rx
= 0.25F
* (s
[0] + s
[1] + s
[2] + s
[3]);
3145 const float ry
= 0.25F
* (t
[0] + t
[1] + t
[2] + t
[3]);
3146 const float rz
= 0.25F
* (p
[0] + p
[1] + p
[2] + p
[3]);
3147 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
3149 if (arx
>= ary
&& arx
>= arz
) {
3150 const float sign
= (rx
>= 0.0F
) ? 1.0F
: -1.0F
;
3151 const uint face
= (rx
>= 0.0F
) ?
3152 PIPE_TEX_FACE_POS_X
: PIPE_TEX_FACE_NEG_X
;
3153 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3154 const float ima
= -0.5F
/ fabsf(s
[j
]);
3155 ssss
[j
] = sign
* p
[j
] * ima
+ 0.5F
;
3156 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
3160 else if (ary
>= arx
&& ary
>= arz
) {
3161 const float sign
= (ry
>= 0.0F
) ? 1.0F
: -1.0F
;
3162 const uint face
= (ry
>= 0.0F
) ?
3163 PIPE_TEX_FACE_POS_Y
: PIPE_TEX_FACE_NEG_Y
;
3164 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3165 const float ima
= -0.5F
/ fabsf(t
[j
]);
3166 ssss
[j
] = -s
[j
] * ima
+ 0.5F
;
3167 tttt
[j
] = sign
* -p
[j
] * ima
+ 0.5F
;
3172 const float sign
= (rz
>= 0.0F
) ? 1.0F
: -1.0F
;
3173 const uint face
= (rz
>= 0.0F
) ?
3174 PIPE_TEX_FACE_POS_Z
: PIPE_TEX_FACE_NEG_Z
;
3175 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3176 const float ima
= -0.5F
/ fabsf(p
[j
]);
3177 ssss
[j
] = sign
* -s
[j
] * ima
+ 0.5F
;
3178 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
3187 sp_get_dims(const struct sp_sampler_view
*sp_sview
,
3191 const struct pipe_sampler_view
*view
= &sp_sview
->base
;
3192 const struct pipe_resource
*texture
= view
->texture
;
3194 if (view
->target
== PIPE_BUFFER
) {
3195 dims
[0] = view
->u
.buf
.size
/ util_format_get_blocksize(view
->format
);
3196 /* the other values are undefined, but let's avoid potential valgrind
3199 dims
[1] = dims
[2] = dims
[3] = 0;
3203 /* undefined according to EXT_gpu_program */
3204 level
+= view
->u
.tex
.first_level
;
3205 if (level
> view
->u
.tex
.last_level
)
3208 dims
[3] = view
->u
.tex
.last_level
- view
->u
.tex
.first_level
+ 1;
3209 dims
[0] = u_minify(texture
->width0
, level
);
3211 switch (view
->target
) {
3212 case PIPE_TEXTURE_1D_ARRAY
:
3213 dims
[1] = view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
3215 case PIPE_TEXTURE_1D
:
3217 case PIPE_TEXTURE_2D_ARRAY
:
3218 dims
[2] = view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
3220 case PIPE_TEXTURE_2D
:
3221 case PIPE_TEXTURE_CUBE
:
3222 case PIPE_TEXTURE_RECT
:
3223 dims
[1] = u_minify(texture
->height0
, level
);
3225 case PIPE_TEXTURE_3D
:
3226 dims
[1] = u_minify(texture
->height0
, level
);
3227 dims
[2] = u_minify(texture
->depth0
, level
);
3229 case PIPE_TEXTURE_CUBE_ARRAY
:
3230 dims
[1] = u_minify(texture
->height0
, level
);
3231 dims
[2] = (view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1) / 6;
3234 assert(!"unexpected texture target in sp_get_dims()");
3240 * This function is only used for getting unfiltered texels via the
3241 * TXF opcode. The GL spec says that out-of-bounds texel fetches
3242 * produce undefined results. Instead of crashing, lets just clamp
3243 * coords to the texture image size.
3246 sp_get_texels(const struct sp_sampler_view
*sp_sview
,
3247 const int v_i
[TGSI_QUAD_SIZE
],
3248 const int v_j
[TGSI_QUAD_SIZE
],
3249 const int v_k
[TGSI_QUAD_SIZE
],
3250 const int lod
[TGSI_QUAD_SIZE
],
3251 const int8_t offset
[3],
3252 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3254 union tex_tile_address addr
;
3255 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
3258 /* TODO write a better test for LOD */
3259 const unsigned level
=
3260 sp_sview
->base
.target
== PIPE_BUFFER
? 0 :
3261 CLAMP(lod
[0] + sp_sview
->base
.u
.tex
.first_level
,
3262 sp_sview
->base
.u
.tex
.first_level
,
3263 sp_sview
->base
.u
.tex
.last_level
);
3264 const int width
= u_minify(texture
->width0
, level
);
3265 const int height
= u_minify(texture
->height0
, level
);
3266 const int depth
= u_minify(texture
->depth0
, level
);
3267 unsigned elem_size
, first_element
, last_element
;
3270 addr
.bits
.level
= level
;
3272 switch (sp_sview
->base
.target
) {
3274 elem_size
= util_format_get_blocksize(sp_sview
->base
.format
);
3275 first_element
= sp_sview
->base
.u
.buf
.offset
/ elem_size
;
3276 last_element
= (sp_sview
->base
.u
.buf
.offset
+
3277 sp_sview
->base
.u
.buf
.size
) / elem_size
- 1;
3278 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3279 const int x
= CLAMP(v_i
[j
] + offset
[0] +
3283 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
, 0);
3284 for (c
= 0; c
< 4; c
++) {
3289 case PIPE_TEXTURE_1D
:
3290 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3291 const int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3292 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
,
3293 sp_sview
->base
.u
.tex
.first_layer
);
3294 for (c
= 0; c
< 4; c
++) {
3299 case PIPE_TEXTURE_1D_ARRAY
:
3300 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3301 const int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3302 const int y
= CLAMP(v_j
[j
], sp_sview
->base
.u
.tex
.first_layer
,
3303 sp_sview
->base
.u
.tex
.last_layer
);
3304 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
, y
);
3305 for (c
= 0; c
< 4; c
++) {
3310 case PIPE_TEXTURE_2D
:
3311 case PIPE_TEXTURE_RECT
:
3312 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3313 const int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3314 const int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
3315 tx
= get_texel_3d_no_border(sp_sview
, addr
, x
, y
,
3316 sp_sview
->base
.u
.tex
.first_layer
);
3317 for (c
= 0; c
< 4; c
++) {
3322 case PIPE_TEXTURE_2D_ARRAY
:
3323 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3324 const int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3325 const int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
3326 const int layer
= CLAMP(v_k
[j
], sp_sview
->base
.u
.tex
.first_layer
,
3327 sp_sview
->base
.u
.tex
.last_layer
);
3328 tx
= get_texel_3d_no_border(sp_sview
, addr
, x
, y
, layer
);
3329 for (c
= 0; c
< 4; c
++) {
3334 case PIPE_TEXTURE_3D
:
3335 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3336 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3337 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
3338 int z
= CLAMP(v_k
[j
] + offset
[2], 0, depth
- 1);
3339 tx
= get_texel_3d_no_border(sp_sview
, addr
, x
, y
, z
);
3340 for (c
= 0; c
< 4; c
++) {
3345 case PIPE_TEXTURE_CUBE
: /* TXF can't work on CUBE according to spec */
3346 case PIPE_TEXTURE_CUBE_ARRAY
:
3348 assert(!"Unknown or CUBE texture type in TXF processing\n");
3352 if (sp_sview
->need_swizzle
) {
3353 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
3354 memcpy(rgba_temp
, rgba
, sizeof(rgba_temp
));
3355 do_swizzling(&sp_sview
->base
, rgba_temp
, rgba
);
3361 softpipe_create_sampler_state(struct pipe_context
*pipe
,
3362 const struct pipe_sampler_state
*sampler
)
3364 struct sp_sampler
*samp
= CALLOC_STRUCT(sp_sampler
);
3366 samp
->base
= *sampler
;
3368 /* Note that (for instance) linear_texcoord_s and
3369 * nearest_texcoord_s may be active at the same time, if the
3370 * sampler min_img_filter differs from its mag_img_filter.
3372 if (sampler
->normalized_coords
) {
3373 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
3374 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
3375 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
3377 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
3378 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
3379 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
3382 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
3383 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
3384 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
3386 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
3387 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
3388 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
3391 samp
->min_img_filter
= sampler
->min_img_filter
;
3393 switch (sampler
->min_mip_filter
) {
3394 case PIPE_TEX_MIPFILTER_NONE
:
3395 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
3396 samp
->filter_funcs
= &funcs_none_no_filter_select
;
3398 samp
->filter_funcs
= &funcs_none
;
3401 case PIPE_TEX_MIPFILTER_NEAREST
:
3402 samp
->filter_funcs
= &funcs_nearest
;
3405 case PIPE_TEX_MIPFILTER_LINEAR
:
3406 if (sampler
->min_img_filter
== sampler
->mag_img_filter
&&
3407 sampler
->normalized_coords
&&
3408 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
3409 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
3410 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
3411 sampler
->max_anisotropy
<= 1) {
3412 samp
->min_mag_equal_repeat_linear
= TRUE
;
3414 samp
->filter_funcs
= &funcs_linear
;
3416 /* Anisotropic filtering extension. */
3417 if (sampler
->max_anisotropy
> 1) {
3418 samp
->filter_funcs
= &funcs_linear_aniso
;
3420 /* Override min_img_filter:
3421 * min_img_filter needs to be set to NEAREST since we need to access
3422 * each texture pixel as it is and weight it later; using linear
3423 * filters will have incorrect results.
3424 * By setting the filter to NEAREST here, we can avoid calling the
3425 * generic img_filter_2d_nearest in the anisotropic filter function,
3426 * making it possible to use one of the accelerated implementations
3428 samp
->min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
3430 /* on first access create the lookup table containing the filter weights. */
3432 create_filter_table();
3437 if (samp
->min_img_filter
== sampler
->mag_img_filter
) {
3438 samp
->min_mag_equal
= TRUE
;
3441 return (void *)samp
;
3446 softpipe_get_lambda_func(const struct pipe_sampler_view
*view
, unsigned shader
)
3448 if (shader
!= PIPE_SHADER_FRAGMENT
)
3449 return compute_lambda_vert
;
3451 switch (view
->target
) {
3453 case PIPE_TEXTURE_1D
:
3454 case PIPE_TEXTURE_1D_ARRAY
:
3455 return compute_lambda_1d
;
3456 case PIPE_TEXTURE_2D
:
3457 case PIPE_TEXTURE_2D_ARRAY
:
3458 case PIPE_TEXTURE_RECT
:
3459 case PIPE_TEXTURE_CUBE
:
3460 case PIPE_TEXTURE_CUBE_ARRAY
:
3461 return compute_lambda_2d
;
3462 case PIPE_TEXTURE_3D
:
3463 return compute_lambda_3d
;
3466 return compute_lambda_1d
;
3471 struct pipe_sampler_view
*
3472 softpipe_create_sampler_view(struct pipe_context
*pipe
,
3473 struct pipe_resource
*resource
,
3474 const struct pipe_sampler_view
*templ
)
3476 struct sp_sampler_view
*sview
= CALLOC_STRUCT(sp_sampler_view
);
3477 const struct softpipe_resource
*spr
= (struct softpipe_resource
*)resource
;
3480 struct pipe_sampler_view
*view
= &sview
->base
;
3482 view
->reference
.count
= 1;
3483 view
->texture
= NULL
;
3484 pipe_resource_reference(&view
->texture
, resource
);
3485 view
->context
= pipe
;
3489 * This is possibly too lenient, but the primary reason is just
3490 * to catch state trackers which forget to initialize this, so
3491 * it only catches clearly impossible view targets.
3493 if (view
->target
!= resource
->target
) {
3494 if (view
->target
== PIPE_TEXTURE_1D
)
3495 assert(resource
->target
== PIPE_TEXTURE_1D_ARRAY
);
3496 else if (view
->target
== PIPE_TEXTURE_1D_ARRAY
)
3497 assert(resource
->target
== PIPE_TEXTURE_1D
);
3498 else if (view
->target
== PIPE_TEXTURE_2D
)
3499 assert(resource
->target
== PIPE_TEXTURE_2D_ARRAY
||
3500 resource
->target
== PIPE_TEXTURE_CUBE
||
3501 resource
->target
== PIPE_TEXTURE_CUBE_ARRAY
);
3502 else if (view
->target
== PIPE_TEXTURE_2D_ARRAY
)
3503 assert(resource
->target
== PIPE_TEXTURE_2D
||
3504 resource
->target
== PIPE_TEXTURE_CUBE
||
3505 resource
->target
== PIPE_TEXTURE_CUBE_ARRAY
);
3506 else if (view
->target
== PIPE_TEXTURE_CUBE
)
3507 assert(resource
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
3508 resource
->target
== PIPE_TEXTURE_2D_ARRAY
);
3509 else if (view
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
3510 assert(resource
->target
== PIPE_TEXTURE_CUBE
||
3511 resource
->target
== PIPE_TEXTURE_2D_ARRAY
);
3517 if (any_swizzle(view
)) {
3518 sview
->need_swizzle
= TRUE
;
3521 sview
->need_cube_convert
= (view
->target
== PIPE_TEXTURE_CUBE
||
3522 view
->target
== PIPE_TEXTURE_CUBE_ARRAY
);
3523 sview
->pot2d
= spr
->pot
&&
3524 (view
->target
== PIPE_TEXTURE_2D
||
3525 view
->target
== PIPE_TEXTURE_RECT
);
3527 sview
->xpot
= util_logbase2( resource
->width0
);
3528 sview
->ypot
= util_logbase2( resource
->height0
);
3531 return (struct pipe_sampler_view
*) sview
;
3535 static inline const struct sp_tgsi_sampler
*
3536 sp_tgsi_sampler_cast_c(const struct tgsi_sampler
*sampler
)
3538 return (const struct sp_tgsi_sampler
*)sampler
;
3543 sp_tgsi_get_dims(struct tgsi_sampler
*tgsi_sampler
,
3544 const unsigned sview_index
,
3545 int level
, int dims
[4])
3547 const struct sp_tgsi_sampler
*sp_samp
=
3548 sp_tgsi_sampler_cast_c(tgsi_sampler
);
3550 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3551 /* always have a view here but texture is NULL if no sampler view was set. */
3552 if (!sp_samp
->sp_sview
[sview_index
].base
.texture
) {
3553 dims
[0] = dims
[1] = dims
[2] = dims
[3] = 0;
3556 sp_get_dims(&sp_samp
->sp_sview
[sview_index
], level
, dims
);
3561 sp_tgsi_get_samples(struct tgsi_sampler
*tgsi_sampler
,
3562 const unsigned sview_index
,
3563 const unsigned sampler_index
,
3564 const float s
[TGSI_QUAD_SIZE
],
3565 const float t
[TGSI_QUAD_SIZE
],
3566 const float p
[TGSI_QUAD_SIZE
],
3567 const float c0
[TGSI_QUAD_SIZE
],
3568 const float lod
[TGSI_QUAD_SIZE
],
3569 float derivs
[3][2][TGSI_QUAD_SIZE
],
3570 const int8_t offset
[3],
3571 enum tgsi_sampler_control control
,
3572 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3574 const struct sp_tgsi_sampler
*sp_tgsi_samp
=
3575 sp_tgsi_sampler_cast_c(tgsi_sampler
);
3576 const struct sp_sampler_view
*sp_sview
;
3577 const struct sp_sampler
*sp_samp
;
3578 struct filter_args filt_args
;
3580 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3581 assert(sampler_index
< PIPE_MAX_SAMPLERS
);
3582 assert(sp_tgsi_samp
->sp_sampler
[sampler_index
]);
3584 sp_sview
= &sp_tgsi_samp
->sp_sview
[sview_index
];
3585 sp_samp
= sp_tgsi_samp
->sp_sampler
[sampler_index
];
3586 /* always have a view here but texture is NULL if no sampler view was set. */
3587 if (!sp_sview
->base
.texture
) {
3589 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++) {
3590 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
3597 filt_args
.control
= control
;
3598 filt_args
.offset
= offset
;
3600 if (sp_sview
->need_cube_convert
) {
3601 float cs
[TGSI_QUAD_SIZE
];
3602 float ct
[TGSI_QUAD_SIZE
];
3603 float cp
[TGSI_QUAD_SIZE
];
3604 uint faces
[TGSI_QUAD_SIZE
];
3606 convert_cube(sp_sview
, sp_samp
, s
, t
, p
, c0
, cs
, ct
, cp
, faces
);
3608 filt_args
.faces
= faces
;
3609 sample_mip(sp_sview
, sp_samp
, cs
, ct
, cp
, c0
, lod
, &filt_args
, rgba
);
3611 static const uint zero_faces
[TGSI_QUAD_SIZE
] = {0, 0, 0, 0};
3613 filt_args
.faces
= zero_faces
;
3614 sample_mip(sp_sview
, sp_samp
, s
, t
, p
, c0
, lod
, &filt_args
, rgba
);
3619 sp_tgsi_query_lod(const struct tgsi_sampler
*tgsi_sampler
,
3620 const unsigned sview_index
,
3621 const unsigned sampler_index
,
3622 const float s
[TGSI_QUAD_SIZE
],
3623 const float t
[TGSI_QUAD_SIZE
],
3624 const float p
[TGSI_QUAD_SIZE
],
3625 const float c0
[TGSI_QUAD_SIZE
],
3626 const enum tgsi_sampler_control control
,
3627 float mipmap
[TGSI_QUAD_SIZE
],
3628 float lod
[TGSI_QUAD_SIZE
])
3630 static const float lod_in
[TGSI_QUAD_SIZE
] = { 0.0, 0.0, 0.0, 0.0 };
3632 const struct sp_tgsi_sampler
*sp_tgsi_samp
=
3633 sp_tgsi_sampler_cast_c(tgsi_sampler
);
3634 const struct sp_sampler_view
*sp_sview
;
3635 const struct sp_sampler
*sp_samp
;
3636 const struct sp_filter_funcs
*funcs
;
3639 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3640 assert(sampler_index
< PIPE_MAX_SAMPLERS
);
3641 assert(sp_tgsi_samp
->sp_sampler
[sampler_index
]);
3643 sp_sview
= &sp_tgsi_samp
->sp_sview
[sview_index
];
3644 sp_samp
= sp_tgsi_samp
->sp_sampler
[sampler_index
];
3645 /* always have a view here but texture is NULL if no sampler view was
3647 if (!sp_sview
->base
.texture
) {
3648 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
3655 if (sp_sview
->need_cube_convert
) {
3656 float cs
[TGSI_QUAD_SIZE
];
3657 float ct
[TGSI_QUAD_SIZE
];
3658 float cp
[TGSI_QUAD_SIZE
];
3659 uint unused_faces
[TGSI_QUAD_SIZE
];
3661 convert_cube(sp_sview
, sp_samp
, s
, t
, p
, c0
, cs
, ct
, cp
, unused_faces
);
3662 compute_lambda_lod_unclamped(sp_sview
, sp_samp
,
3663 cs
, ct
, cp
, lod_in
, control
, lod
);
3665 compute_lambda_lod_unclamped(sp_sview
, sp_samp
,
3666 s
, t
, p
, lod_in
, control
, lod
);
3669 get_filters(sp_sview
, sp_samp
, control
, &funcs
, NULL
, NULL
);
3670 funcs
->relative_level(sp_sview
, sp_samp
, lod
, mipmap
);
3674 sp_tgsi_get_texel(struct tgsi_sampler
*tgsi_sampler
,
3675 const unsigned sview_index
,
3676 const int i
[TGSI_QUAD_SIZE
],
3677 const int j
[TGSI_QUAD_SIZE
], const int k
[TGSI_QUAD_SIZE
],
3678 const int lod
[TGSI_QUAD_SIZE
], const int8_t offset
[3],
3679 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3681 const struct sp_tgsi_sampler
*sp_samp
=
3682 sp_tgsi_sampler_cast_c(tgsi_sampler
);
3684 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3685 /* always have a view here but texture is NULL if no sampler view was set. */
3686 if (!sp_samp
->sp_sview
[sview_index
].base
.texture
) {
3688 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++) {
3689 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
3695 sp_get_texels(&sp_samp
->sp_sview
[sview_index
], i
, j
, k
, lod
, offset
, rgba
);
3699 struct sp_tgsi_sampler
*
3700 sp_create_tgsi_sampler(void)
3702 struct sp_tgsi_sampler
*samp
= CALLOC_STRUCT(sp_tgsi_sampler
);
3706 samp
->base
.get_dims
= sp_tgsi_get_dims
;
3707 samp
->base
.get_samples
= sp_tgsi_get_samples
;
3708 samp
->base
.get_texel
= sp_tgsi_get_texel
;
3709 samp
->base
.query_lod
= sp_tgsi_query_lod
;