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_QUAD_SIZE
; 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
,
1066 float rgba
[TGSI_QUAD_SIZE
])
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_QUAD_SIZE
; 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
,
1101 float rgba
[TGSI_QUAD_SIZE
])
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_QUAD_SIZE
; 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
,
1144 float rgba
[TGSI_QUAD_SIZE
])
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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_ZERO
:
1505 case PIPE_SWIZZLE_ONE
:
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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_QUAD_SIZE
; 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_QUAD_SIZE
; 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
],
2213 const float dudx
, const float dvdx
,
2214 const float dudy
, const float dvdy
,
2215 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2217 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
2219 // ??? Won't the image filters blow up if level is negative?
2220 const unsigned level0
= level
> 0 ? level
: 0;
2221 const float scaling
= 1.0f
/ (1 << level0
);
2222 const int width
= u_minify(texture
->width0
, level0
);
2223 const int height
= u_minify(texture
->height0
, level0
);
2224 struct img_filter_args args
;
2225 const float ux
= dudx
* scaling
;
2226 const float vx
= dvdx
* scaling
;
2227 const float uy
= dudy
* scaling
;
2228 const float vy
= dvdy
* scaling
;
2230 /* compute ellipse coefficients to bound the region:
2231 * A*x*x + B*x*y + C*y*y = F.
2233 float A
= vx
*vx
+vy
*vy
+1;
2234 float B
= -2*(ux
*vx
+uy
*vy
);
2235 float C
= ux
*ux
+uy
*uy
+1;
2236 float F
= A
*C
-B
*B
/4.0f
;
2238 /* check if it is an ellipse */
2239 /* assert(F > 0.0); */
2241 /* Compute the ellipse's (u,v) bounding box in texture space */
2242 const float d
= -B
*B
+4.0f
*C
*A
;
2243 const float box_u
= 2.0f
/ d
* sqrtf(d
*C
*F
); /* box_u -> half of bbox with */
2244 const float box_v
= 2.0f
/ d
* sqrtf(A
*d
*F
); /* box_v -> half of bbox height */
2246 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2247 float s_buffer
[TGSI_QUAD_SIZE
];
2248 float t_buffer
[TGSI_QUAD_SIZE
];
2249 float weight_buffer
[TGSI_QUAD_SIZE
];
2252 /* For each quad, the du and dx values are the same and so the ellipse is
2253 * also the same. Note that texel/image access can only be performed using
2254 * a quad, i.e. it is not possible to get the pixel value for a single
2255 * tex coord. In order to have a better performance, the access is buffered
2256 * using the s_buffer/t_buffer and weight_buffer. Only when the buffer is
2257 * full, then the pixel values are read from the image.
2259 const float ddq
= 2 * A
;
2261 /* Scale ellipse formula to directly index the Filter Lookup Table.
2262 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
2264 const double formScale
= (double) (WEIGHT_LUT_SIZE
- 1) / F
;
2268 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
2271 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2272 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
2273 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
2274 * value, q, is less than F, we're inside the ellipse
2276 const float tex_u
= -0.5F
+ s
[j
] * texture
->width0
* scaling
;
2277 const float tex_v
= -0.5F
+ t
[j
] * texture
->height0
* scaling
;
2279 const int u0
= (int) floorf(tex_u
- box_u
);
2280 const int u1
= (int) ceilf(tex_u
+ box_u
);
2281 const int v0
= (int) floorf(tex_v
- box_v
);
2282 const int v1
= (int) ceilf(tex_v
+ box_v
);
2283 const float U
= u0
- tex_u
;
2285 float num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
2286 unsigned buffer_next
= 0;
2289 args
.face_id
= faces
[j
];
2291 for (v
= v0
; v
<= v1
; ++v
) {
2292 const float V
= v
- tex_v
;
2293 float dq
= A
* (2 * U
+ 1) + B
* V
;
2294 float q
= (C
* V
+ B
* U
) * V
+ A
* U
* U
;
2297 for (u
= u0
; u
<= u1
; ++u
) {
2298 /* Note that the ellipse has been pre-scaled so F =
2299 * WEIGHT_LUT_SIZE - 1
2301 if (q
< WEIGHT_LUT_SIZE
) {
2302 /* as a LUT is used, q must never be negative;
2303 * should not happen, though
2305 const int qClamped
= q
>= 0.0F
? q
: 0;
2306 const float weight
= weightLut
[qClamped
];
2308 weight_buffer
[buffer_next
] = weight
;
2309 s_buffer
[buffer_next
] = u
/ ((float) width
);
2310 t_buffer
[buffer_next
] = v
/ ((float) height
);
2313 if (buffer_next
== TGSI_QUAD_SIZE
) {
2314 /* 4 texel coords are in the buffer -> read it now */
2316 /* it is assumed that samp->min_img_filter is set to
2317 * img_filter_2d_nearest or one of the
2318 * accelerated img_filter_2d_nearest_XXX functions.
2320 for (jj
= 0; jj
< buffer_next
; jj
++) {
2321 args
.s
= s_buffer
[jj
];
2322 args
.t
= t_buffer
[jj
];
2324 min_filter(sp_sview
, sp_samp
, &args
, &rgba_temp
[0][jj
]);
2325 num
[0] += weight_buffer
[jj
] * rgba_temp
[0][jj
];
2326 num
[1] += weight_buffer
[jj
] * rgba_temp
[1][jj
];
2327 num
[2] += weight_buffer
[jj
] * rgba_temp
[2][jj
];
2328 num
[3] += weight_buffer
[jj
] * rgba_temp
[3][jj
];
2341 /* if the tex coord buffer contains unread values, we will read
2344 if (buffer_next
> 0) {
2346 /* it is assumed that samp->min_img_filter is set to
2347 * img_filter_2d_nearest or one of the
2348 * accelerated img_filter_2d_nearest_XXX functions.
2350 for (jj
= 0; jj
< buffer_next
; jj
++) {
2351 args
.s
= s_buffer
[jj
];
2352 args
.t
= t_buffer
[jj
];
2354 min_filter(sp_sview
, sp_samp
, &args
, &rgba_temp
[0][jj
]);
2355 num
[0] += weight_buffer
[jj
] * rgba_temp
[0][jj
];
2356 num
[1] += weight_buffer
[jj
] * rgba_temp
[1][jj
];
2357 num
[2] += weight_buffer
[jj
] * rgba_temp
[2][jj
];
2358 num
[3] += weight_buffer
[jj
] * rgba_temp
[3][jj
];
2363 /* Reaching this place would mean that no pixels intersected
2364 * the ellipse. This should never happen because the filter
2365 * we use always intersects at least one pixel.
2372 /* not enough pixels in resampling, resort to direct interpolation */
2376 min_filter(sp_sview
, sp_samp
, &args
, &rgba_temp
[0][j
]);
2378 num
[0] = rgba_temp
[0][j
];
2379 num
[1] = rgba_temp
[1][j
];
2380 num
[2] = rgba_temp
[2][j
];
2381 num
[3] = rgba_temp
[3][j
];
2384 rgba
[0][j
] = num
[0] / den
;
2385 rgba
[1][j
] = num
[1] / den
;
2386 rgba
[2][j
] = num
[2] / den
;
2387 rgba
[3][j
] = num
[3] / den
;
2393 * Get mip level relative to base level for linear mip filter
2396 mip_rel_level_linear_aniso(const struct sp_sampler_view
*sp_sview
,
2397 const struct sp_sampler
*sp_samp
,
2398 const float lod
[TGSI_QUAD_SIZE
],
2399 float level
[TGSI_QUAD_SIZE
])
2401 mip_rel_level_linear(sp_sview
, sp_samp
, lod
, level
);
2405 * Sample 2D texture using an anisotropic filter.
2408 mip_filter_linear_aniso(const struct sp_sampler_view
*sp_sview
,
2409 const struct sp_sampler
*sp_samp
,
2410 img_filter_func min_filter
,
2411 img_filter_func mag_filter
,
2412 const float s
[TGSI_QUAD_SIZE
],
2413 const float t
[TGSI_QUAD_SIZE
],
2414 const float p
[TGSI_QUAD_SIZE
],
2415 const float c0
[TGSI_QUAD_SIZE
],
2416 const float lod_in
[TGSI_QUAD_SIZE
],
2417 const struct filter_args
*filt_args
,
2418 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2420 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
2421 const struct pipe_sampler_view
*psview
= &sp_sview
->base
;
2424 float lod
[TGSI_QUAD_SIZE
];
2426 const float s_to_u
= u_minify(texture
->width0
, psview
->u
.tex
.first_level
);
2427 const float t_to_v
= u_minify(texture
->height0
, psview
->u
.tex
.first_level
);
2428 const float dudx
= (s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]) * s_to_u
;
2429 const float dudy
= (s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]) * s_to_u
;
2430 const float dvdx
= (t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]) * t_to_v
;
2431 const float dvdy
= (t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]) * t_to_v
;
2432 struct img_filter_args args
;
2434 if (filt_args
->control
== TGSI_SAMPLER_LOD_BIAS
||
2435 filt_args
->control
== TGSI_SAMPLER_LOD_NONE
||
2437 filt_args
->control
== TGSI_SAMPLER_DERIVS_EXPLICIT
) {
2438 /* note: instead of working with Px and Py, we will use the
2439 * squared length instead, to avoid sqrt.
2441 const float Px2
= dudx
* dudx
+ dvdx
* dvdx
;
2442 const float Py2
= dudy
* dudy
+ dvdy
* dvdy
;
2447 const float maxEccentricity
= sp_samp
->base
.max_anisotropy
* sp_samp
->base
.max_anisotropy
;
2458 /* if the eccentricity of the ellipse is too big, scale up the shorter
2459 * of the two vectors to limit the maximum amount of work per pixel
2462 if (e
> maxEccentricity
) {
2463 /* float s=e / maxEccentricity;
2467 Pmin2
= Pmax2
/ maxEccentricity
;
2470 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
2471 * this since 0.5*log(x) = log(sqrt(x))
2473 lambda
= 0.5F
* util_fast_log2(Pmin2
) + sp_samp
->base
.lod_bias
;
2474 compute_lod(&sp_samp
->base
, filt_args
->control
, lambda
, lod_in
, lod
);
2477 assert(filt_args
->control
== TGSI_SAMPLER_LOD_EXPLICIT
||
2478 filt_args
->control
== TGSI_SAMPLER_LOD_ZERO
);
2479 compute_lod(&sp_samp
->base
, filt_args
->control
, sp_samp
->base
.lod_bias
, lod_in
, lod
);
2482 /* XXX: Take into account all lod values.
2485 level0
= psview
->u
.tex
.first_level
+ (int)lambda
;
2487 /* If the ellipse covers the whole image, we can
2488 * simply return the average of the whole image.
2490 if (level0
>= (int) psview
->u
.tex
.last_level
) {
2492 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2496 args
.level
= psview
->u
.tex
.last_level
;
2497 args
.face_id
= filt_args
->faces
[j
];
2498 min_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2502 /* don't bother interpolating between multiple LODs; it doesn't
2503 * seem to be worth the extra running time.
2505 img_filter_2d_ewa(sp_sview
, sp_samp
, min_filter
, mag_filter
,
2506 s
, t
, p
, filt_args
->faces
, level0
,
2507 dudx
, dvdx
, dudy
, dvdy
, rgba
);
2511 print_sample_4(__FUNCTION__
, rgba
);
2516 * Get mip level relative to base level for linear mip filter
2519 mip_rel_level_linear_2d_linear_repeat_POT(
2520 const struct sp_sampler_view
*sp_sview
,
2521 const struct sp_sampler
*sp_samp
,
2522 const float lod
[TGSI_QUAD_SIZE
],
2523 float level
[TGSI_QUAD_SIZE
])
2525 mip_rel_level_linear(sp_sview
, sp_samp
, lod
, level
);
2529 * Specialized version of mip_filter_linear with hard-wired calls to
2530 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
2533 mip_filter_linear_2d_linear_repeat_POT(
2534 const struct sp_sampler_view
*sp_sview
,
2535 const struct sp_sampler
*sp_samp
,
2536 img_filter_func min_filter
,
2537 img_filter_func mag_filter
,
2538 const float s
[TGSI_QUAD_SIZE
],
2539 const float t
[TGSI_QUAD_SIZE
],
2540 const float p
[TGSI_QUAD_SIZE
],
2541 const float c0
[TGSI_QUAD_SIZE
],
2542 const float lod_in
[TGSI_QUAD_SIZE
],
2543 const struct filter_args
*filt_args
,
2544 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2546 const struct pipe_sampler_view
*psview
= &sp_sview
->base
;
2548 float lod
[TGSI_QUAD_SIZE
];
2550 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, filt_args
->control
, lod
);
2552 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2553 const int level0
= psview
->u
.tex
.first_level
+ (int)lod
[j
];
2554 struct img_filter_args args
;
2555 /* Catches both negative and large values of level0:
2560 args
.face_id
= filt_args
->faces
[j
];
2561 args
.offset
= filt_args
->offset
;
2562 args
.gather_only
= filt_args
->control
== TGSI_SAMPLER_GATHER
;
2563 if ((unsigned)level0
>= psview
->u
.tex
.last_level
) {
2565 args
.level
= psview
->u
.tex
.first_level
;
2567 args
.level
= psview
->u
.tex
.last_level
;
2568 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, &args
,
2573 const float levelBlend
= frac(lod
[j
]);
2574 float rgbax
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2577 args
.level
= level0
;
2578 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, &args
, &rgbax
[0][0]);
2579 args
.level
= level0
+1;
2580 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, &args
, &rgbax
[0][1]);
2582 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
2583 rgba
[c
][j
] = lerp(levelBlend
, rgbax
[c
][0], rgbax
[c
][1]);
2588 print_sample_4(__FUNCTION__
, rgba
);
2592 static const struct sp_filter_funcs funcs_linear
= {
2593 mip_rel_level_linear
,
2597 static const struct sp_filter_funcs funcs_nearest
= {
2598 mip_rel_level_nearest
,
2602 static const struct sp_filter_funcs funcs_none
= {
2607 static const struct sp_filter_funcs funcs_none_no_filter_select
= {
2608 mip_rel_level_none_no_filter_select
,
2609 mip_filter_none_no_filter_select
2612 static const struct sp_filter_funcs funcs_linear_aniso
= {
2613 mip_rel_level_linear_aniso
,
2614 mip_filter_linear_aniso
2617 static const struct sp_filter_funcs funcs_linear_2d_linear_repeat_POT
= {
2618 mip_rel_level_linear_2d_linear_repeat_POT
,
2619 mip_filter_linear_2d_linear_repeat_POT
2623 * Do shadow/depth comparisons.
2626 sample_compare(const struct sp_sampler_view
*sp_sview
,
2627 const struct sp_sampler
*sp_samp
,
2628 const float s
[TGSI_QUAD_SIZE
],
2629 const float t
[TGSI_QUAD_SIZE
],
2630 const float p
[TGSI_QUAD_SIZE
],
2631 const float c0
[TGSI_QUAD_SIZE
],
2632 const float c1
[TGSI_QUAD_SIZE
],
2633 enum tgsi_sampler_control control
,
2634 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2636 const struct pipe_sampler_state
*sampler
= &sp_samp
->base
;
2638 int k
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2640 const struct util_format_description
*format_desc
=
2641 util_format_description(sp_sview
->base
.format
);
2642 /* not entirely sure we couldn't end up with non-valid swizzle here */
2643 const unsigned chan_type
=
2644 format_desc
->swizzle
[0] <= UTIL_FORMAT_SWIZZLE_W
?
2645 format_desc
->channel
[format_desc
->swizzle
[0]].type
:
2646 UTIL_FORMAT_TYPE_FLOAT
;
2647 const bool is_gather
= (control
== TGSI_SAMPLER_GATHER
);
2650 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
2651 * for 2D Array texture we need to use the 'c0' (aka Q).
2652 * When we sampled the depth texture, the depth value was put into all
2653 * RGBA channels. We look at the red channel here.
2656 if (sp_sview
->base
.target
== PIPE_TEXTURE_2D_ARRAY
||
2657 sp_sview
->base
.target
== PIPE_TEXTURE_CUBE
) {
2662 } else if (sp_sview
->base
.target
== PIPE_TEXTURE_CUBE_ARRAY
) {
2674 if (chan_type
!= UTIL_FORMAT_TYPE_FLOAT
) {
2676 * clamping is a result of conversion to texture format, hence
2677 * doesn't happen with floats. Technically also should do comparison
2678 * in texture format (quantization!).
2680 pc
[0] = CLAMP(pc
[0], 0.0F
, 1.0F
);
2681 pc
[1] = CLAMP(pc
[1], 0.0F
, 1.0F
);
2682 pc
[2] = CLAMP(pc
[2], 0.0F
, 1.0F
);
2683 pc
[3] = CLAMP(pc
[3], 0.0F
, 1.0F
);
2686 for (v
= 0; v
< (is_gather
? TGSI_NUM_CHANNELS
: 1); v
++) {
2687 /* compare four texcoords vs. four texture samples */
2688 switch (sampler
->compare_func
) {
2689 case PIPE_FUNC_LESS
:
2690 k
[v
][0] = pc
[0] < rgba
[v
][0];
2691 k
[v
][1] = pc
[1] < rgba
[v
][1];
2692 k
[v
][2] = pc
[2] < rgba
[v
][2];
2693 k
[v
][3] = pc
[3] < rgba
[v
][3];
2695 case PIPE_FUNC_LEQUAL
:
2696 k
[v
][0] = pc
[0] <= rgba
[v
][0];
2697 k
[v
][1] = pc
[1] <= rgba
[v
][1];
2698 k
[v
][2] = pc
[2] <= rgba
[v
][2];
2699 k
[v
][3] = pc
[3] <= rgba
[v
][3];
2701 case PIPE_FUNC_GREATER
:
2702 k
[v
][0] = pc
[0] > rgba
[v
][0];
2703 k
[v
][1] = pc
[1] > rgba
[v
][1];
2704 k
[v
][2] = pc
[2] > rgba
[v
][2];
2705 k
[v
][3] = pc
[3] > rgba
[v
][3];
2707 case PIPE_FUNC_GEQUAL
:
2708 k
[v
][0] = pc
[0] >= rgba
[v
][0];
2709 k
[v
][1] = pc
[1] >= rgba
[v
][1];
2710 k
[v
][2] = pc
[2] >= rgba
[v
][2];
2711 k
[v
][3] = pc
[3] >= rgba
[v
][3];
2713 case PIPE_FUNC_EQUAL
:
2714 k
[v
][0] = pc
[0] == rgba
[v
][0];
2715 k
[v
][1] = pc
[1] == rgba
[v
][1];
2716 k
[v
][2] = pc
[2] == rgba
[v
][2];
2717 k
[v
][3] = pc
[3] == rgba
[v
][3];
2719 case PIPE_FUNC_NOTEQUAL
:
2720 k
[v
][0] = pc
[0] != rgba
[v
][0];
2721 k
[v
][1] = pc
[1] != rgba
[v
][1];
2722 k
[v
][2] = pc
[2] != rgba
[v
][2];
2723 k
[v
][3] = pc
[3] != rgba
[v
][3];
2725 case PIPE_FUNC_ALWAYS
:
2726 k
[v
][0] = k
[v
][1] = k
[v
][2] = k
[v
][3] = 1;
2728 case PIPE_FUNC_NEVER
:
2729 k
[v
][0] = k
[v
][1] = k
[v
][2] = k
[v
][3] = 0;
2732 k
[v
][0] = k
[v
][1] = k
[v
][2] = k
[v
][3] = 0;
2739 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2740 for (v
= 0; v
< TGSI_NUM_CHANNELS
; v
++) {
2741 rgba
[v
][j
] = k
[v
][j
];
2745 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2746 rgba
[0][j
] = k
[0][j
];
2747 rgba
[1][j
] = k
[0][j
];
2748 rgba
[2][j
] = k
[0][j
];
2755 do_swizzling(const struct pipe_sampler_view
*sview
,
2756 float in
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
],
2757 float out
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2760 const unsigned swizzle_r
= sview
->swizzle_r
;
2761 const unsigned swizzle_g
= sview
->swizzle_g
;
2762 const unsigned swizzle_b
= sview
->swizzle_b
;
2763 const unsigned swizzle_a
= sview
->swizzle_a
;
2765 switch (swizzle_r
) {
2766 case PIPE_SWIZZLE_ZERO
:
2767 for (j
= 0; j
< 4; j
++)
2770 case PIPE_SWIZZLE_ONE
:
2771 for (j
= 0; j
< 4; j
++)
2775 assert(swizzle_r
< 4);
2776 for (j
= 0; j
< 4; j
++)
2777 out
[0][j
] = in
[swizzle_r
][j
];
2780 switch (swizzle_g
) {
2781 case PIPE_SWIZZLE_ZERO
:
2782 for (j
= 0; j
< 4; j
++)
2785 case PIPE_SWIZZLE_ONE
:
2786 for (j
= 0; j
< 4; j
++)
2790 assert(swizzle_g
< 4);
2791 for (j
= 0; j
< 4; j
++)
2792 out
[1][j
] = in
[swizzle_g
][j
];
2795 switch (swizzle_b
) {
2796 case PIPE_SWIZZLE_ZERO
:
2797 for (j
= 0; j
< 4; j
++)
2800 case PIPE_SWIZZLE_ONE
:
2801 for (j
= 0; j
< 4; j
++)
2805 assert(swizzle_b
< 4);
2806 for (j
= 0; j
< 4; j
++)
2807 out
[2][j
] = in
[swizzle_b
][j
];
2810 switch (swizzle_a
) {
2811 case PIPE_SWIZZLE_ZERO
:
2812 for (j
= 0; j
< 4; j
++)
2815 case PIPE_SWIZZLE_ONE
:
2816 for (j
= 0; j
< 4; j
++)
2820 assert(swizzle_a
< 4);
2821 for (j
= 0; j
< 4; j
++)
2822 out
[3][j
] = in
[swizzle_a
][j
];
2827 static wrap_nearest_func
2828 get_nearest_unorm_wrap(unsigned mode
)
2831 case PIPE_TEX_WRAP_CLAMP
:
2832 return wrap_nearest_unorm_clamp
;
2833 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2834 return wrap_nearest_unorm_clamp_to_edge
;
2835 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2836 return wrap_nearest_unorm_clamp_to_border
;
2838 debug_printf("illegal wrap mode %d with non-normalized coords\n", mode
);
2839 return wrap_nearest_unorm_clamp
;
2844 static wrap_nearest_func
2845 get_nearest_wrap(unsigned mode
)
2848 case PIPE_TEX_WRAP_REPEAT
:
2849 return wrap_nearest_repeat
;
2850 case PIPE_TEX_WRAP_CLAMP
:
2851 return wrap_nearest_clamp
;
2852 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2853 return wrap_nearest_clamp_to_edge
;
2854 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2855 return wrap_nearest_clamp_to_border
;
2856 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2857 return wrap_nearest_mirror_repeat
;
2858 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2859 return wrap_nearest_mirror_clamp
;
2860 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2861 return wrap_nearest_mirror_clamp_to_edge
;
2862 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2863 return wrap_nearest_mirror_clamp_to_border
;
2866 return wrap_nearest_repeat
;
2871 static wrap_linear_func
2872 get_linear_unorm_wrap(unsigned mode
)
2875 case PIPE_TEX_WRAP_CLAMP
:
2876 return wrap_linear_unorm_clamp
;
2877 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2878 return wrap_linear_unorm_clamp_to_edge
;
2879 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2880 return wrap_linear_unorm_clamp_to_border
;
2882 debug_printf("illegal wrap mode %d with non-normalized coords\n", mode
);
2883 return wrap_linear_unorm_clamp
;
2888 static wrap_linear_func
2889 get_linear_wrap(unsigned mode
)
2892 case PIPE_TEX_WRAP_REPEAT
:
2893 return wrap_linear_repeat
;
2894 case PIPE_TEX_WRAP_CLAMP
:
2895 return wrap_linear_clamp
;
2896 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2897 return wrap_linear_clamp_to_edge
;
2898 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2899 return wrap_linear_clamp_to_border
;
2900 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2901 return wrap_linear_mirror_repeat
;
2902 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2903 return wrap_linear_mirror_clamp
;
2904 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2905 return wrap_linear_mirror_clamp_to_edge
;
2906 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2907 return wrap_linear_mirror_clamp_to_border
;
2910 return wrap_linear_repeat
;
2916 * Is swizzling needed for the given state key?
2919 any_swizzle(const struct pipe_sampler_view
*view
)
2921 return (view
->swizzle_r
!= PIPE_SWIZZLE_RED
||
2922 view
->swizzle_g
!= PIPE_SWIZZLE_GREEN
||
2923 view
->swizzle_b
!= PIPE_SWIZZLE_BLUE
||
2924 view
->swizzle_a
!= PIPE_SWIZZLE_ALPHA
);
2928 static img_filter_func
2929 get_img_filter(const struct sp_sampler_view
*sp_sview
,
2930 const struct pipe_sampler_state
*sampler
,
2931 unsigned filter
, bool gather
)
2933 switch (sp_sview
->base
.target
) {
2935 case PIPE_TEXTURE_1D
:
2936 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2937 return img_filter_1d_nearest
;
2939 return img_filter_1d_linear
;
2941 case PIPE_TEXTURE_1D_ARRAY
:
2942 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2943 return img_filter_1d_array_nearest
;
2945 return img_filter_1d_array_linear
;
2947 case PIPE_TEXTURE_2D
:
2948 case PIPE_TEXTURE_RECT
:
2949 /* Try for fast path:
2951 if (!gather
&& sp_sview
->pot2d
&&
2952 sampler
->wrap_s
== sampler
->wrap_t
&&
2953 sampler
->normalized_coords
)
2955 switch (sampler
->wrap_s
) {
2956 case PIPE_TEX_WRAP_REPEAT
:
2958 case PIPE_TEX_FILTER_NEAREST
:
2959 return img_filter_2d_nearest_repeat_POT
;
2960 case PIPE_TEX_FILTER_LINEAR
:
2961 return img_filter_2d_linear_repeat_POT
;
2966 case PIPE_TEX_WRAP_CLAMP
:
2968 case PIPE_TEX_FILTER_NEAREST
:
2969 return img_filter_2d_nearest_clamp_POT
;
2975 /* Otherwise use default versions:
2977 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2978 return img_filter_2d_nearest
;
2980 return img_filter_2d_linear
;
2982 case PIPE_TEXTURE_2D_ARRAY
:
2983 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2984 return img_filter_2d_array_nearest
;
2986 return img_filter_2d_array_linear
;
2988 case PIPE_TEXTURE_CUBE
:
2989 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2990 return img_filter_cube_nearest
;
2992 return img_filter_cube_linear
;
2994 case PIPE_TEXTURE_CUBE_ARRAY
:
2995 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2996 return img_filter_cube_array_nearest
;
2998 return img_filter_cube_array_linear
;
3000 case PIPE_TEXTURE_3D
:
3001 if (filter
== PIPE_TEX_FILTER_NEAREST
)
3002 return img_filter_3d_nearest
;
3004 return img_filter_3d_linear
;
3008 return img_filter_1d_nearest
;
3013 * Get mip filter funcs, and optionally both img min filter and img mag
3014 * filter. Note that both img filter function pointers must be either non-NULL
3018 get_filters(const struct sp_sampler_view
*sp_sview
,
3019 const struct sp_sampler
*sp_samp
,
3020 const enum tgsi_sampler_control control
,
3021 const struct sp_filter_funcs
**funcs
,
3022 img_filter_func
*min
,
3023 img_filter_func
*mag
)
3026 if (control
== TGSI_SAMPLER_GATHER
) {
3027 *funcs
= &funcs_nearest
;
3029 *min
= get_img_filter(sp_sview
, &sp_samp
->base
,
3030 PIPE_TEX_FILTER_LINEAR
, true);
3032 } else if (sp_sview
->pot2d
& sp_samp
->min_mag_equal_repeat_linear
) {
3033 *funcs
= &funcs_linear_2d_linear_repeat_POT
;
3035 *funcs
= sp_samp
->filter_funcs
;
3038 *min
= get_img_filter(sp_sview
, &sp_samp
->base
,
3039 sp_samp
->min_img_filter
, false);
3040 if (sp_samp
->min_mag_equal
) {
3043 *mag
= get_img_filter(sp_sview
, &sp_samp
->base
,
3044 sp_samp
->base
.mag_img_filter
, false);
3051 sample_mip(const struct sp_sampler_view
*sp_sview
,
3052 const struct sp_sampler
*sp_samp
,
3053 const float s
[TGSI_QUAD_SIZE
],
3054 const float t
[TGSI_QUAD_SIZE
],
3055 const float p
[TGSI_QUAD_SIZE
],
3056 const float c0
[TGSI_QUAD_SIZE
],
3057 const float lod
[TGSI_QUAD_SIZE
],
3058 const struct filter_args
*filt_args
,
3059 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3061 const struct sp_filter_funcs
*funcs
= NULL
;
3062 img_filter_func min_img_filter
= NULL
;
3063 img_filter_func mag_img_filter
= NULL
;
3065 get_filters(sp_sview
, sp_samp
, filt_args
->control
,
3066 &funcs
, &min_img_filter
, &mag_img_filter
);
3068 funcs
->filter(sp_sview
, sp_samp
, min_img_filter
, mag_img_filter
,
3069 s
, t
, p
, c0
, lod
, filt_args
, rgba
);
3071 if (sp_samp
->base
.compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
3072 sample_compare(sp_sview
, sp_samp
, s
, t
, p
, c0
,
3073 lod
, filt_args
->control
, rgba
);
3076 if (sp_sview
->need_swizzle
&& filt_args
->control
!= TGSI_SAMPLER_GATHER
) {
3077 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
3078 memcpy(rgba_temp
, rgba
, sizeof(rgba_temp
));
3079 do_swizzling(&sp_sview
->base
, rgba_temp
, rgba
);
3086 * This function uses cube texture coordinates to choose a face of a cube and
3087 * computes the 2D cube face coordinates. Puts face info into the sampler
3091 convert_cube(const struct sp_sampler_view
*sp_sview
,
3092 const struct sp_sampler
*sp_samp
,
3093 const float s
[TGSI_QUAD_SIZE
],
3094 const float t
[TGSI_QUAD_SIZE
],
3095 const float p
[TGSI_QUAD_SIZE
],
3096 const float c0
[TGSI_QUAD_SIZE
],
3097 float ssss
[TGSI_QUAD_SIZE
],
3098 float tttt
[TGSI_QUAD_SIZE
],
3099 float pppp
[TGSI_QUAD_SIZE
],
3100 uint faces
[TGSI_QUAD_SIZE
])
3110 direction target sc tc ma
3111 ---------- ------------------------------- --- --- ---
3112 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
3113 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
3114 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
3115 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
3116 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
3117 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
3120 /* Choose the cube face and compute new s/t coords for the 2D face.
3122 * Use the same cube face for all four pixels in the quad.
3124 * This isn't ideal, but if we want to use a different cube face
3125 * per pixel in the quad, we'd have to also compute the per-face
3126 * LOD here too. That's because the four post-face-selection
3127 * texcoords are no longer related to each other (they're
3128 * per-face!) so we can't use subtraction to compute the partial
3129 * deriviates to compute the LOD. Doing so (near cube edges
3130 * anyway) gives us pretty much random values.
3133 /* use the average of the four pixel's texcoords to choose the face */
3134 const float rx
= 0.25F
* (s
[0] + s
[1] + s
[2] + s
[3]);
3135 const float ry
= 0.25F
* (t
[0] + t
[1] + t
[2] + t
[3]);
3136 const float rz
= 0.25F
* (p
[0] + p
[1] + p
[2] + p
[3]);
3137 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
3139 if (arx
>= ary
&& arx
>= arz
) {
3140 const float sign
= (rx
>= 0.0F
) ? 1.0F
: -1.0F
;
3141 const uint face
= (rx
>= 0.0F
) ?
3142 PIPE_TEX_FACE_POS_X
: PIPE_TEX_FACE_NEG_X
;
3143 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3144 const float ima
= -0.5F
/ fabsf(s
[j
]);
3145 ssss
[j
] = sign
* p
[j
] * ima
+ 0.5F
;
3146 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
3150 else if (ary
>= arx
&& ary
>= arz
) {
3151 const float sign
= (ry
>= 0.0F
) ? 1.0F
: -1.0F
;
3152 const uint face
= (ry
>= 0.0F
) ?
3153 PIPE_TEX_FACE_POS_Y
: PIPE_TEX_FACE_NEG_Y
;
3154 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3155 const float ima
= -0.5F
/ fabsf(t
[j
]);
3156 ssss
[j
] = -s
[j
] * ima
+ 0.5F
;
3157 tttt
[j
] = sign
* -p
[j
] * ima
+ 0.5F
;
3162 const float sign
= (rz
>= 0.0F
) ? 1.0F
: -1.0F
;
3163 const uint face
= (rz
>= 0.0F
) ?
3164 PIPE_TEX_FACE_POS_Z
: PIPE_TEX_FACE_NEG_Z
;
3165 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3166 const float ima
= -0.5F
/ fabsf(p
[j
]);
3167 ssss
[j
] = sign
* -s
[j
] * ima
+ 0.5F
;
3168 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
3177 sp_get_dims(const struct sp_sampler_view
*sp_sview
,
3181 const struct pipe_sampler_view
*view
= &sp_sview
->base
;
3182 const struct pipe_resource
*texture
= view
->texture
;
3184 if (view
->target
== PIPE_BUFFER
) {
3185 dims
[0] = (view
->u
.buf
.last_element
- view
->u
.buf
.first_element
) + 1;
3186 /* the other values are undefined, but let's avoid potential valgrind
3189 dims
[1] = dims
[2] = dims
[3] = 0;
3193 /* undefined according to EXT_gpu_program */
3194 level
+= view
->u
.tex
.first_level
;
3195 if (level
> view
->u
.tex
.last_level
)
3198 dims
[3] = view
->u
.tex
.last_level
- view
->u
.tex
.first_level
+ 1;
3199 dims
[0] = u_minify(texture
->width0
, level
);
3201 switch (view
->target
) {
3202 case PIPE_TEXTURE_1D_ARRAY
:
3203 dims
[1] = view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
3205 case PIPE_TEXTURE_1D
:
3207 case PIPE_TEXTURE_2D_ARRAY
:
3208 dims
[2] = view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
3210 case PIPE_TEXTURE_2D
:
3211 case PIPE_TEXTURE_CUBE
:
3212 case PIPE_TEXTURE_RECT
:
3213 dims
[1] = u_minify(texture
->height0
, level
);
3215 case PIPE_TEXTURE_3D
:
3216 dims
[1] = u_minify(texture
->height0
, level
);
3217 dims
[2] = u_minify(texture
->depth0
, level
);
3219 case PIPE_TEXTURE_CUBE_ARRAY
:
3220 dims
[1] = u_minify(texture
->height0
, level
);
3221 dims
[2] = (view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1) / 6;
3224 assert(!"unexpected texture target in sp_get_dims()");
3230 * This function is only used for getting unfiltered texels via the
3231 * TXF opcode. The GL spec says that out-of-bounds texel fetches
3232 * produce undefined results. Instead of crashing, lets just clamp
3233 * coords to the texture image size.
3236 sp_get_texels(const struct sp_sampler_view
*sp_sview
,
3237 const int v_i
[TGSI_QUAD_SIZE
],
3238 const int v_j
[TGSI_QUAD_SIZE
],
3239 const int v_k
[TGSI_QUAD_SIZE
],
3240 const int lod
[TGSI_QUAD_SIZE
],
3241 const int8_t offset
[3],
3242 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3244 union tex_tile_address addr
;
3245 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
3248 /* TODO write a better test for LOD */
3249 const unsigned level
=
3250 sp_sview
->base
.target
== PIPE_BUFFER
? 0 :
3251 CLAMP(lod
[0] + sp_sview
->base
.u
.tex
.first_level
,
3252 sp_sview
->base
.u
.tex
.first_level
,
3253 sp_sview
->base
.u
.tex
.last_level
);
3254 const int width
= u_minify(texture
->width0
, level
);
3255 const int height
= u_minify(texture
->height0
, level
);
3256 const int depth
= u_minify(texture
->depth0
, level
);
3259 addr
.bits
.level
= level
;
3261 switch (sp_sview
->base
.target
) {
3263 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3264 const int x
= CLAMP(v_i
[j
] + offset
[0] +
3265 sp_sview
->base
.u
.buf
.first_element
,
3266 sp_sview
->base
.u
.buf
.first_element
,
3267 sp_sview
->base
.u
.buf
.last_element
);
3268 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
, 0);
3269 for (c
= 0; c
< 4; c
++) {
3274 case PIPE_TEXTURE_1D
:
3275 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3276 const int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3277 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
,
3278 sp_sview
->base
.u
.tex
.first_layer
);
3279 for (c
= 0; c
< 4; c
++) {
3284 case PIPE_TEXTURE_1D_ARRAY
:
3285 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3286 const int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3287 const int y
= CLAMP(v_j
[j
], sp_sview
->base
.u
.tex
.first_layer
,
3288 sp_sview
->base
.u
.tex
.last_layer
);
3289 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
, y
);
3290 for (c
= 0; c
< 4; c
++) {
3295 case PIPE_TEXTURE_2D
:
3296 case PIPE_TEXTURE_RECT
:
3297 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3298 const int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3299 const int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
3300 tx
= get_texel_3d_no_border(sp_sview
, addr
, x
, y
,
3301 sp_sview
->base
.u
.tex
.first_layer
);
3302 for (c
= 0; c
< 4; c
++) {
3307 case PIPE_TEXTURE_2D_ARRAY
:
3308 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3309 const int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3310 const int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
3311 const int layer
= CLAMP(v_k
[j
], sp_sview
->base
.u
.tex
.first_layer
,
3312 sp_sview
->base
.u
.tex
.last_layer
);
3313 tx
= get_texel_3d_no_border(sp_sview
, addr
, x
, y
, layer
);
3314 for (c
= 0; c
< 4; c
++) {
3319 case PIPE_TEXTURE_3D
:
3320 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3321 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3322 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
3323 int z
= CLAMP(v_k
[j
] + offset
[2], 0, depth
- 1);
3324 tx
= get_texel_3d_no_border(sp_sview
, addr
, x
, y
, z
);
3325 for (c
= 0; c
< 4; c
++) {
3330 case PIPE_TEXTURE_CUBE
: /* TXF can't work on CUBE according to spec */
3331 case PIPE_TEXTURE_CUBE_ARRAY
:
3333 assert(!"Unknown or CUBE texture type in TXF processing\n");
3337 if (sp_sview
->need_swizzle
) {
3338 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
3339 memcpy(rgba_temp
, rgba
, sizeof(rgba_temp
));
3340 do_swizzling(&sp_sview
->base
, rgba_temp
, rgba
);
3346 softpipe_create_sampler_state(struct pipe_context
*pipe
,
3347 const struct pipe_sampler_state
*sampler
)
3349 struct sp_sampler
*samp
= CALLOC_STRUCT(sp_sampler
);
3351 samp
->base
= *sampler
;
3353 /* Note that (for instance) linear_texcoord_s and
3354 * nearest_texcoord_s may be active at the same time, if the
3355 * sampler min_img_filter differs from its mag_img_filter.
3357 if (sampler
->normalized_coords
) {
3358 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
3359 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
3360 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
3362 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
3363 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
3364 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
3367 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
3368 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
3369 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
3371 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
3372 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
3373 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
3376 samp
->min_img_filter
= sampler
->min_img_filter
;
3378 switch (sampler
->min_mip_filter
) {
3379 case PIPE_TEX_MIPFILTER_NONE
:
3380 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
3381 samp
->filter_funcs
= &funcs_none_no_filter_select
;
3383 samp
->filter_funcs
= &funcs_none
;
3386 case PIPE_TEX_MIPFILTER_NEAREST
:
3387 samp
->filter_funcs
= &funcs_nearest
;
3390 case PIPE_TEX_MIPFILTER_LINEAR
:
3391 if (sampler
->min_img_filter
== sampler
->mag_img_filter
&&
3392 sampler
->normalized_coords
&&
3393 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
3394 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
3395 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
3396 sampler
->max_anisotropy
<= 1) {
3397 samp
->min_mag_equal_repeat_linear
= TRUE
;
3399 samp
->filter_funcs
= &funcs_linear
;
3401 /* Anisotropic filtering extension. */
3402 if (sampler
->max_anisotropy
> 1) {
3403 samp
->filter_funcs
= &funcs_linear_aniso
;
3405 /* Override min_img_filter:
3406 * min_img_filter needs to be set to NEAREST since we need to access
3407 * each texture pixel as it is and weight it later; using linear
3408 * filters will have incorrect results.
3409 * By setting the filter to NEAREST here, we can avoid calling the
3410 * generic img_filter_2d_nearest in the anisotropic filter function,
3411 * making it possible to use one of the accelerated implementations
3413 samp
->min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
3415 /* on first access create the lookup table containing the filter weights. */
3417 create_filter_table();
3422 if (samp
->min_img_filter
== sampler
->mag_img_filter
) {
3423 samp
->min_mag_equal
= TRUE
;
3426 return (void *)samp
;
3431 softpipe_get_lambda_func(const struct pipe_sampler_view
*view
, unsigned shader
)
3433 if (shader
!= PIPE_SHADER_FRAGMENT
)
3434 return compute_lambda_vert
;
3436 switch (view
->target
) {
3438 case PIPE_TEXTURE_1D
:
3439 case PIPE_TEXTURE_1D_ARRAY
:
3440 return compute_lambda_1d
;
3441 case PIPE_TEXTURE_2D
:
3442 case PIPE_TEXTURE_2D_ARRAY
:
3443 case PIPE_TEXTURE_RECT
:
3444 case PIPE_TEXTURE_CUBE
:
3445 case PIPE_TEXTURE_CUBE_ARRAY
:
3446 return compute_lambda_2d
;
3447 case PIPE_TEXTURE_3D
:
3448 return compute_lambda_3d
;
3451 return compute_lambda_1d
;
3456 struct pipe_sampler_view
*
3457 softpipe_create_sampler_view(struct pipe_context
*pipe
,
3458 struct pipe_resource
*resource
,
3459 const struct pipe_sampler_view
*templ
)
3461 struct sp_sampler_view
*sview
= CALLOC_STRUCT(sp_sampler_view
);
3462 const struct softpipe_resource
*spr
= (struct softpipe_resource
*)resource
;
3465 struct pipe_sampler_view
*view
= &sview
->base
;
3467 view
->reference
.count
= 1;
3468 view
->texture
= NULL
;
3469 pipe_resource_reference(&view
->texture
, resource
);
3470 view
->context
= pipe
;
3474 * This is possibly too lenient, but the primary reason is just
3475 * to catch state trackers which forget to initialize this, so
3476 * it only catches clearly impossible view targets.
3478 if (view
->target
!= resource
->target
) {
3479 if (view
->target
== PIPE_TEXTURE_1D
)
3480 assert(resource
->target
== PIPE_TEXTURE_1D_ARRAY
);
3481 else if (view
->target
== PIPE_TEXTURE_1D_ARRAY
)
3482 assert(resource
->target
== PIPE_TEXTURE_1D
);
3483 else if (view
->target
== PIPE_TEXTURE_2D
)
3484 assert(resource
->target
== PIPE_TEXTURE_2D_ARRAY
||
3485 resource
->target
== PIPE_TEXTURE_CUBE
||
3486 resource
->target
== PIPE_TEXTURE_CUBE_ARRAY
);
3487 else if (view
->target
== PIPE_TEXTURE_2D_ARRAY
)
3488 assert(resource
->target
== PIPE_TEXTURE_2D
||
3489 resource
->target
== PIPE_TEXTURE_CUBE
||
3490 resource
->target
== PIPE_TEXTURE_CUBE_ARRAY
);
3491 else if (view
->target
== PIPE_TEXTURE_CUBE
)
3492 assert(resource
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
3493 resource
->target
== PIPE_TEXTURE_2D_ARRAY
);
3494 else if (view
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
3495 assert(resource
->target
== PIPE_TEXTURE_CUBE
||
3496 resource
->target
== PIPE_TEXTURE_2D_ARRAY
);
3502 if (any_swizzle(view
)) {
3503 sview
->need_swizzle
= TRUE
;
3506 sview
->need_cube_convert
= (view
->target
== PIPE_TEXTURE_CUBE
||
3507 view
->target
== PIPE_TEXTURE_CUBE_ARRAY
);
3508 sview
->pot2d
= spr
->pot
&&
3509 (view
->target
== PIPE_TEXTURE_2D
||
3510 view
->target
== PIPE_TEXTURE_RECT
);
3512 sview
->xpot
= util_logbase2( resource
->width0
);
3513 sview
->ypot
= util_logbase2( resource
->height0
);
3516 return (struct pipe_sampler_view
*) sview
;
3520 static inline const struct sp_tgsi_sampler
*
3521 sp_tgsi_sampler_cast_c(const struct tgsi_sampler
*sampler
)
3523 return (const struct sp_tgsi_sampler
*)sampler
;
3528 sp_tgsi_get_dims(struct tgsi_sampler
*tgsi_sampler
,
3529 const unsigned sview_index
,
3530 int level
, int dims
[4])
3532 const struct sp_tgsi_sampler
*sp_samp
=
3533 sp_tgsi_sampler_cast_c(tgsi_sampler
);
3535 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3536 /* always have a view here but texture is NULL if no sampler view was set. */
3537 if (!sp_samp
->sp_sview
[sview_index
].base
.texture
) {
3538 dims
[0] = dims
[1] = dims
[2] = dims
[3] = 0;
3541 sp_get_dims(&sp_samp
->sp_sview
[sview_index
], level
, dims
);
3546 sp_tgsi_get_samples(struct tgsi_sampler
*tgsi_sampler
,
3547 const unsigned sview_index
,
3548 const unsigned sampler_index
,
3549 const float s
[TGSI_QUAD_SIZE
],
3550 const float t
[TGSI_QUAD_SIZE
],
3551 const float p
[TGSI_QUAD_SIZE
],
3552 const float c0
[TGSI_QUAD_SIZE
],
3553 const float lod
[TGSI_QUAD_SIZE
],
3554 float derivs
[3][2][TGSI_QUAD_SIZE
],
3555 const int8_t offset
[3],
3556 enum tgsi_sampler_control control
,
3557 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3559 const struct sp_tgsi_sampler
*sp_tgsi_samp
=
3560 sp_tgsi_sampler_cast_c(tgsi_sampler
);
3561 const struct sp_sampler_view
*sp_sview
;
3562 const struct sp_sampler
*sp_samp
;
3563 struct filter_args filt_args
;
3565 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3566 assert(sampler_index
< PIPE_MAX_SAMPLERS
);
3567 assert(sp_tgsi_samp
->sp_sampler
[sampler_index
]);
3569 sp_sview
= &sp_tgsi_samp
->sp_sview
[sview_index
];
3570 sp_samp
= sp_tgsi_samp
->sp_sampler
[sampler_index
];
3571 /* always have a view here but texture is NULL if no sampler view was set. */
3572 if (!sp_sview
->base
.texture
) {
3574 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++) {
3575 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
3582 filt_args
.control
= control
;
3583 filt_args
.offset
= offset
;
3585 if (sp_sview
->need_cube_convert
) {
3586 float cs
[TGSI_QUAD_SIZE
];
3587 float ct
[TGSI_QUAD_SIZE
];
3588 float cp
[TGSI_QUAD_SIZE
];
3589 uint faces
[TGSI_QUAD_SIZE
];
3591 convert_cube(sp_sview
, sp_samp
, s
, t
, p
, c0
, cs
, ct
, cp
, faces
);
3593 filt_args
.faces
= faces
;
3594 sample_mip(sp_sview
, sp_samp
, cs
, ct
, cp
, c0
, lod
, &filt_args
, rgba
);
3596 static const uint zero_faces
[TGSI_QUAD_SIZE
] = {0, 0, 0, 0};
3598 filt_args
.faces
= zero_faces
;
3599 sample_mip(sp_sview
, sp_samp
, s
, t
, p
, c0
, lod
, &filt_args
, rgba
);
3604 sp_tgsi_query_lod(const struct tgsi_sampler
*tgsi_sampler
,
3605 const unsigned sview_index
,
3606 const unsigned sampler_index
,
3607 const float s
[TGSI_QUAD_SIZE
],
3608 const float t
[TGSI_QUAD_SIZE
],
3609 const float p
[TGSI_QUAD_SIZE
],
3610 const float c0
[TGSI_QUAD_SIZE
],
3611 const enum tgsi_sampler_control control
,
3612 float mipmap
[TGSI_QUAD_SIZE
],
3613 float lod
[TGSI_QUAD_SIZE
])
3615 static const float lod_in
[TGSI_QUAD_SIZE
] = { 0.0, 0.0, 0.0, 0.0 };
3617 const struct sp_tgsi_sampler
*sp_tgsi_samp
=
3618 sp_tgsi_sampler_cast_c(tgsi_sampler
);
3619 const struct sp_sampler_view
*sp_sview
;
3620 const struct sp_sampler
*sp_samp
;
3621 const struct sp_filter_funcs
*funcs
;
3624 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3625 assert(sampler_index
< PIPE_MAX_SAMPLERS
);
3626 assert(sp_tgsi_samp
->sp_sampler
[sampler_index
]);
3628 sp_sview
= &sp_tgsi_samp
->sp_sview
[sview_index
];
3629 sp_samp
= sp_tgsi_samp
->sp_sampler
[sampler_index
];
3630 /* always have a view here but texture is NULL if no sampler view was
3632 if (!sp_sview
->base
.texture
) {
3633 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
3640 if (sp_sview
->need_cube_convert
) {
3641 float cs
[TGSI_QUAD_SIZE
];
3642 float ct
[TGSI_QUAD_SIZE
];
3643 float cp
[TGSI_QUAD_SIZE
];
3644 uint unused_faces
[TGSI_QUAD_SIZE
];
3646 convert_cube(sp_sview
, sp_samp
, s
, t
, p
, c0
, cs
, ct
, cp
, unused_faces
);
3647 compute_lambda_lod_unclamped(sp_sview
, sp_samp
,
3648 cs
, ct
, cp
, lod_in
, control
, lod
);
3650 compute_lambda_lod_unclamped(sp_sview
, sp_samp
,
3651 s
, t
, p
, lod_in
, control
, lod
);
3654 get_filters(sp_sview
, sp_samp
, control
, &funcs
, NULL
, NULL
);
3655 funcs
->relative_level(sp_sview
, sp_samp
, lod
, mipmap
);
3659 sp_tgsi_get_texel(struct tgsi_sampler
*tgsi_sampler
,
3660 const unsigned sview_index
,
3661 const int i
[TGSI_QUAD_SIZE
],
3662 const int j
[TGSI_QUAD_SIZE
], const int k
[TGSI_QUAD_SIZE
],
3663 const int lod
[TGSI_QUAD_SIZE
], const int8_t offset
[3],
3664 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3666 const struct sp_tgsi_sampler
*sp_samp
=
3667 sp_tgsi_sampler_cast_c(tgsi_sampler
);
3669 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3670 /* always have a view here but texture is NULL if no sampler view was set. */
3671 if (!sp_samp
->sp_sview
[sview_index
].base
.texture
) {
3673 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++) {
3674 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
3680 sp_get_texels(&sp_samp
->sp_sview
[sview_index
], i
, j
, k
, lod
, offset
, rgba
);
3684 struct sp_tgsi_sampler
*
3685 sp_create_tgsi_sampler(void)
3687 struct sp_tgsi_sampler
*samp
= CALLOC_STRUCT(sp_tgsi_sampler
);
3691 samp
->base
.get_dims
= sp_tgsi_get_dims
;
3692 samp
->base
.get_samples
= sp_tgsi_get_samples
;
3693 samp
->base
.get_texel
= sp_tgsi_get_texel
;
3694 samp
->base
.query_lod
= sp_tgsi_query_lod
;