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 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 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 float u
= CLAMP(s
* size
+ offset
, 0.0F
, (float)size
);
297 *icoord0
= util_ifloor(u
);
298 *icoord1
= *icoord0
+ 1;
304 wrap_linear_clamp_to_edge(float s
, unsigned size
, int offset
,
305 int *icoord0
, int *icoord1
, float *w
)
307 float u
= CLAMP(s
* size
+ offset
, 0.0F
, (float)size
);
309 *icoord0
= util_ifloor(u
);
310 *icoord1
= *icoord0
+ 1;
313 if (*icoord1
>= (int) size
)
320 wrap_linear_clamp_to_border(float s
, unsigned size
, int offset
,
321 int *icoord0
, int *icoord1
, float *w
)
323 const float min
= -0.5F
;
324 const float max
= (float)size
+ 0.5F
;
325 float u
= CLAMP(s
* size
+ offset
, min
, max
);
327 *icoord0
= util_ifloor(u
);
328 *icoord1
= *icoord0
+ 1;
334 wrap_linear_mirror_repeat(float s
, unsigned size
, int offset
,
335 int *icoord0
, int *icoord1
, float *w
)
340 s
+= (float)offset
/ size
;
341 flr
= util_ifloor(s
);
346 *icoord0
= util_ifloor(u
);
347 *icoord1
= *icoord0
+ 1;
350 if (*icoord1
>= (int) size
)
357 wrap_linear_mirror_clamp(float s
, unsigned size
, int offset
,
358 int *icoord0
, int *icoord1
, float *w
)
360 float u
= fabsf(s
* size
+ offset
);
364 *icoord0
= util_ifloor(u
);
365 *icoord1
= *icoord0
+ 1;
371 wrap_linear_mirror_clamp_to_edge(float s
, unsigned size
, int offset
,
372 int *icoord0
, int *icoord1
, float *w
)
374 float u
= fabsf(s
* size
+ offset
);
378 *icoord0
= util_ifloor(u
);
379 *icoord1
= *icoord0
+ 1;
382 if (*icoord1
>= (int) size
)
389 wrap_linear_mirror_clamp_to_border(float s
, unsigned size
, int offset
,
390 int *icoord0
, int *icoord1
, float *w
)
392 const float min
= -0.5F
;
393 const float max
= size
+ 0.5F
;
394 float u
= fabsf(s
* size
+ offset
);
400 *icoord0
= util_ifloor(u
);
401 *icoord1
= *icoord0
+ 1;
407 * PIPE_TEX_WRAP_CLAMP for nearest sampling, unnormalized coords.
410 wrap_nearest_unorm_clamp(float s
, unsigned size
, int offset
, int *icoord
)
412 int i
= util_ifloor(s
);
413 *icoord
= CLAMP(i
+ offset
, 0, (int) size
-1);
418 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for nearest sampling, unnormalized coords.
421 wrap_nearest_unorm_clamp_to_border(float s
, unsigned size
, int offset
, int *icoord
)
423 *icoord
= util_ifloor( CLAMP(s
+ offset
, -0.5F
, (float) size
+ 0.5F
) );
428 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for nearest sampling, unnormalized coords.
431 wrap_nearest_unorm_clamp_to_edge(float s
, unsigned size
, int offset
, int *icoord
)
433 *icoord
= util_ifloor( CLAMP(s
+ offset
, 0.5F
, (float) size
- 0.5F
) );
438 * PIPE_TEX_WRAP_CLAMP for linear sampling, unnormalized coords.
441 wrap_linear_unorm_clamp(float s
, unsigned size
, int offset
,
442 int *icoord0
, int *icoord1
, float *w
)
444 /* Not exactly what the spec says, but it matches NVIDIA output */
445 float u
= CLAMP(s
+ offset
- 0.5F
, 0.0f
, (float) size
- 1.0f
);
446 *icoord0
= util_ifloor(u
);
447 *icoord1
= *icoord0
+ 1;
453 * PIPE_TEX_WRAP_CLAMP_TO_BORDER for linear sampling, unnormalized coords.
456 wrap_linear_unorm_clamp_to_border(float s
, unsigned size
, int offset
,
457 int *icoord0
, int *icoord1
, float *w
)
459 float u
= CLAMP(s
+ offset
, -0.5F
, (float) size
+ 0.5F
);
461 *icoord0
= util_ifloor(u
);
462 *icoord1
= *icoord0
+ 1;
463 if (*icoord1
> (int) size
- 1)
470 * PIPE_TEX_WRAP_CLAMP_TO_EDGE for linear sampling, unnormalized coords.
473 wrap_linear_unorm_clamp_to_edge(float s
, unsigned size
, int offset
,
474 int *icoord0
, int *icoord1
, float *w
)
476 float u
= CLAMP(s
+ offset
, +0.5F
, (float) size
- 0.5F
);
478 *icoord0
= util_ifloor(u
);
479 *icoord1
= *icoord0
+ 1;
480 if (*icoord1
> (int) size
- 1)
487 * Do coordinate to array index conversion. For array textures.
490 coord_to_layer(float coord
, unsigned first_layer
, unsigned last_layer
)
492 int c
= util_ifloor(coord
+ 0.5F
);
493 return CLAMP(c
, (int)first_layer
, (int)last_layer
);
498 * Examine the quad's texture coordinates to compute the partial
499 * derivatives w.r.t X and Y, then compute lambda (level of detail).
502 compute_lambda_1d(const struct sp_sampler_view
*sview
,
503 const float s
[TGSI_QUAD_SIZE
],
504 const float t
[TGSI_QUAD_SIZE
],
505 const float p
[TGSI_QUAD_SIZE
])
507 const struct pipe_resource
*texture
= sview
->base
.texture
;
508 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
509 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
510 float rho
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, sview
->base
.u
.tex
.first_level
);
512 return util_fast_log2(rho
);
517 compute_lambda_2d(const struct sp_sampler_view
*sview
,
518 const float s
[TGSI_QUAD_SIZE
],
519 const float t
[TGSI_QUAD_SIZE
],
520 const float p
[TGSI_QUAD_SIZE
])
522 const struct pipe_resource
*texture
= sview
->base
.texture
;
523 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
524 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
525 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
526 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
527 float maxx
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, sview
->base
.u
.tex
.first_level
);
528 float maxy
= MAX2(dtdx
, dtdy
) * u_minify(texture
->height0
, sview
->base
.u
.tex
.first_level
);
529 float rho
= MAX2(maxx
, maxy
);
531 return util_fast_log2(rho
);
536 compute_lambda_3d(const struct sp_sampler_view
*sview
,
537 const float s
[TGSI_QUAD_SIZE
],
538 const float t
[TGSI_QUAD_SIZE
],
539 const float p
[TGSI_QUAD_SIZE
])
541 const struct pipe_resource
*texture
= sview
->base
.texture
;
542 float dsdx
= fabsf(s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]);
543 float dsdy
= fabsf(s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]);
544 float dtdx
= fabsf(t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]);
545 float dtdy
= fabsf(t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]);
546 float dpdx
= fabsf(p
[QUAD_BOTTOM_RIGHT
] - p
[QUAD_BOTTOM_LEFT
]);
547 float dpdy
= fabsf(p
[QUAD_TOP_LEFT
] - p
[QUAD_BOTTOM_LEFT
]);
548 float maxx
= MAX2(dsdx
, dsdy
) * u_minify(texture
->width0
, sview
->base
.u
.tex
.first_level
);
549 float maxy
= MAX2(dtdx
, dtdy
) * u_minify(texture
->height0
, sview
->base
.u
.tex
.first_level
);
550 float maxz
= MAX2(dpdx
, dpdy
) * u_minify(texture
->depth0
, sview
->base
.u
.tex
.first_level
);
553 rho
= MAX2(maxx
, maxy
);
554 rho
= MAX2(rho
, maxz
);
556 return util_fast_log2(rho
);
561 * Compute lambda for a vertex texture sampler.
562 * Since there aren't derivatives to use, just return 0.
565 compute_lambda_vert(const struct sp_sampler_view
*sview
,
566 const float s
[TGSI_QUAD_SIZE
],
567 const float t
[TGSI_QUAD_SIZE
],
568 const float p
[TGSI_QUAD_SIZE
])
576 * Get a texel from a texture, using the texture tile cache.
578 * \param addr the template tex address containing cube, z, face info.
579 * \param x the x coord of texel within 2D image
580 * \param y the y coord of texel within 2D image
581 * \param rgba the quad to put the texel/color into
583 * XXX maybe move this into sp_tex_tile_cache.c and merge with the
584 * sp_get_cached_tile_tex() function.
590 static inline const float *
591 get_texel_2d_no_border(const struct sp_sampler_view
*sp_sview
,
592 union tex_tile_address addr
, int x
, int y
)
594 const struct softpipe_tex_cached_tile
*tile
;
595 addr
.bits
.x
= x
/ TEX_TILE_SIZE
;
596 addr
.bits
.y
= y
/ TEX_TILE_SIZE
;
600 tile
= sp_get_cached_tile_tex(sp_sview
->cache
, addr
);
602 return &tile
->data
.color
[y
][x
][0];
606 static inline const float *
607 get_texel_2d(const struct sp_sampler_view
*sp_sview
,
608 const struct sp_sampler
*sp_samp
,
609 union tex_tile_address addr
, int x
, int y
)
611 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
612 unsigned level
= addr
.bits
.level
;
614 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
615 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
616 return sp_samp
->base
.border_color
.f
;
619 return get_texel_2d_no_border( sp_sview
, addr
, x
, y
);
625 * Here's the complete logic (HOLY CRAP) for finding next face and doing the
626 * corresponding coord wrapping, implemented by get_next_face,
627 * get_next_xcoord, get_next_ycoord.
628 * Read like that (first line):
629 * If face is +x and s coord is below zero, then
630 * new face is +z, new s is max , new t is old t
631 * (max is always cube size - 1).
633 * +x s- -> +z: s = max, t = t
634 * +x s+ -> -z: s = 0, t = t
635 * +x t- -> +y: s = max, t = max-s
636 * +x t+ -> -y: s = max, t = s
638 * -x s- -> -z: s = max, t = t
639 * -x s+ -> +z: s = 0, t = t
640 * -x t- -> +y: s = 0, t = s
641 * -x t+ -> -y: s = 0, t = max-s
643 * +y s- -> -x: s = t, t = 0
644 * +y s+ -> +x: s = max-t, t = 0
645 * +y t- -> -z: s = max-s, t = 0
646 * +y t+ -> +z: s = s, t = 0
648 * -y s- -> -x: s = max-t, t = max
649 * -y s+ -> +x: s = t, t = max
650 * -y t- -> +z: s = s, t = max
651 * -y t+ -> -z: s = max-s, t = max
653 * +z s- -> -x: s = max, t = t
654 * +z s+ -> +x: s = 0, t = t
655 * +z t- -> +y: s = s, t = max
656 * +z t+ -> -y: s = s, t = 0
658 * -z s- -> +x: s = max, t = t
659 * -z s+ -> -x: s = 0, t = t
660 * -z t- -> +y: s = max-s, t = 0
661 * -z t+ -> -y: s = max-s, t = max
666 * seamless cubemap neighbour array.
667 * this array is used to find the adjacent face in each of 4 directions,
668 * left, right, up, down. (or -x, +x, -y, +y).
670 static const unsigned face_array
[PIPE_TEX_FACE_MAX
][4] = {
671 /* pos X first then neg X is Z different, Y the same */
672 /* PIPE_TEX_FACE_POS_X,*/
673 { PIPE_TEX_FACE_POS_Z
, PIPE_TEX_FACE_NEG_Z
,
674 PIPE_TEX_FACE_POS_Y
, PIPE_TEX_FACE_NEG_Y
},
675 /* PIPE_TEX_FACE_NEG_X */
676 { PIPE_TEX_FACE_NEG_Z
, PIPE_TEX_FACE_POS_Z
,
677 PIPE_TEX_FACE_POS_Y
, PIPE_TEX_FACE_NEG_Y
},
679 /* pos Y first then neg Y is X different, X the same */
680 /* PIPE_TEX_FACE_POS_Y */
681 { PIPE_TEX_FACE_NEG_X
, PIPE_TEX_FACE_POS_X
,
682 PIPE_TEX_FACE_NEG_Z
, PIPE_TEX_FACE_POS_Z
},
684 /* PIPE_TEX_FACE_NEG_Y */
685 { PIPE_TEX_FACE_NEG_X
, PIPE_TEX_FACE_POS_X
,
686 PIPE_TEX_FACE_POS_Z
, PIPE_TEX_FACE_NEG_Z
},
688 /* pos Z first then neg Y is X different, X the same */
689 /* PIPE_TEX_FACE_POS_Z */
690 { PIPE_TEX_FACE_NEG_X
, PIPE_TEX_FACE_POS_X
,
691 PIPE_TEX_FACE_POS_Y
, PIPE_TEX_FACE_NEG_Y
},
693 /* PIPE_TEX_FACE_NEG_Z */
694 { PIPE_TEX_FACE_POS_X
, PIPE_TEX_FACE_NEG_X
,
695 PIPE_TEX_FACE_POS_Y
, PIPE_TEX_FACE_NEG_Y
}
698 static inline unsigned
699 get_next_face(unsigned face
, int idx
)
701 return face_array
[face
][idx
];
705 * return a new xcoord based on old face, old coords, cube size
706 * and fall_off_index (0 for x-, 1 for x+, 2 for y-, 3 for y+)
709 get_next_xcoord(unsigned face
, unsigned fall_off_index
, int max
, int xc
, int yc
)
711 if ((face
== 0 && fall_off_index
!= 1) ||
712 (face
== 1 && fall_off_index
== 0) ||
713 (face
== 4 && fall_off_index
== 0) ||
714 (face
== 5 && fall_off_index
== 0)) {
717 if ((face
== 1 && fall_off_index
!= 0) ||
718 (face
== 0 && fall_off_index
== 1) ||
719 (face
== 4 && fall_off_index
== 1) ||
720 (face
== 5 && fall_off_index
== 1)) {
723 if ((face
== 4 && fall_off_index
>= 2) ||
724 (face
== 2 && fall_off_index
== 3) ||
725 (face
== 3 && fall_off_index
== 2)) {
728 if ((face
== 5 && fall_off_index
>= 2) ||
729 (face
== 2 && fall_off_index
== 2) ||
730 (face
== 3 && fall_off_index
== 3)) {
733 if ((face
== 2 && fall_off_index
== 0) ||
734 (face
== 3 && fall_off_index
== 1)) {
737 /* (face == 2 && fall_off_index == 1) ||
738 (face == 3 && fall_off_index == 0)) */
743 * return a new ycoord based on old face, old coords, cube size
744 * and fall_off_index (0 for x-, 1 for x+, 2 for y-, 3 for y+)
747 get_next_ycoord(unsigned face
, unsigned fall_off_index
, int max
, int xc
, int yc
)
749 if ((fall_off_index
<= 1) && (face
<= 1 || face
>= 4)) {
753 (face
== 4 && fall_off_index
== 3) ||
754 (face
== 5 && fall_off_index
== 2)) {
758 (face
== 4 && fall_off_index
== 2) ||
759 (face
== 5 && fall_off_index
== 3)) {
762 if ((face
== 0 && fall_off_index
== 3) ||
763 (face
== 1 && fall_off_index
== 2)) {
766 /* (face == 0 && fall_off_index == 2) ||
767 (face == 1 && fall_off_index == 3) */
772 /* Gather a quad of adjacent texels within a tile:
775 get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_view
*sp_sview
,
776 union tex_tile_address addr
,
777 unsigned x
, unsigned y
,
780 const struct softpipe_tex_cached_tile
*tile
;
782 addr
.bits
.x
= x
/ TEX_TILE_SIZE
;
783 addr
.bits
.y
= y
/ TEX_TILE_SIZE
;
787 tile
= sp_get_cached_tile_tex(sp_sview
->cache
, addr
);
789 out
[0] = &tile
->data
.color
[y
][x
][0];
790 out
[1] = &tile
->data
.color
[y
][x
+1][0];
791 out
[2] = &tile
->data
.color
[y
+1][x
][0];
792 out
[3] = &tile
->data
.color
[y
+1][x
+1][0];
796 /* Gather a quad of potentially non-adjacent texels:
799 get_texel_quad_2d_no_border(const struct sp_sampler_view
*sp_sview
,
800 union tex_tile_address addr
,
805 out
[0] = get_texel_2d_no_border( sp_sview
, addr
, x0
, y0
);
806 out
[1] = get_texel_2d_no_border( sp_sview
, addr
, x1
, y0
);
807 out
[2] = get_texel_2d_no_border( sp_sview
, addr
, x0
, y1
);
808 out
[3] = get_texel_2d_no_border( sp_sview
, addr
, x1
, y1
);
811 /* Can involve a lot of unnecessary checks for border color:
814 get_texel_quad_2d(const struct sp_sampler_view
*sp_sview
,
815 const struct sp_sampler
*sp_samp
,
816 union tex_tile_address addr
,
821 out
[0] = get_texel_2d( sp_sview
, sp_samp
, addr
, x0
, y0
);
822 out
[1] = get_texel_2d( sp_sview
, sp_samp
, addr
, x1
, y0
);
823 out
[3] = get_texel_2d( sp_sview
, sp_samp
, addr
, x1
, y1
);
824 out
[2] = get_texel_2d( sp_sview
, sp_samp
, addr
, x0
, y1
);
831 static inline const float *
832 get_texel_3d_no_border(const struct sp_sampler_view
*sp_sview
,
833 union tex_tile_address addr
, int x
, int y
, int z
)
835 const struct softpipe_tex_cached_tile
*tile
;
837 addr
.bits
.x
= x
/ TEX_TILE_SIZE
;
838 addr
.bits
.y
= y
/ TEX_TILE_SIZE
;
843 tile
= sp_get_cached_tile_tex(sp_sview
->cache
, addr
);
845 return &tile
->data
.color
[y
][x
][0];
849 static inline const float *
850 get_texel_3d(const struct sp_sampler_view
*sp_sview
,
851 const struct sp_sampler
*sp_samp
,
852 union tex_tile_address addr
, int x
, int y
, int z
)
854 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
855 unsigned level
= addr
.bits
.level
;
857 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
858 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
) ||
859 z
< 0 || z
>= (int) u_minify(texture
->depth0
, level
)) {
860 return sp_samp
->base
.border_color
.f
;
863 return get_texel_3d_no_border( sp_sview
, addr
, x
, y
, z
);
868 /* Get texel pointer for 1D array texture */
869 static inline const float *
870 get_texel_1d_array(const struct sp_sampler_view
*sp_sview
,
871 const struct sp_sampler
*sp_samp
,
872 union tex_tile_address addr
, int x
, int y
)
874 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
875 unsigned level
= addr
.bits
.level
;
877 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
)) {
878 return sp_samp
->base
.border_color
.f
;
881 return get_texel_2d_no_border(sp_sview
, addr
, x
, y
);
886 /* Get texel pointer for 2D array texture */
887 static inline const float *
888 get_texel_2d_array(const struct sp_sampler_view
*sp_sview
,
889 const struct sp_sampler
*sp_samp
,
890 union tex_tile_address addr
, int x
, int y
, int layer
)
892 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
893 unsigned level
= addr
.bits
.level
;
895 assert(layer
< (int) texture
->array_size
);
898 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
899 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
900 return sp_samp
->base
.border_color
.f
;
903 return get_texel_3d_no_border(sp_sview
, addr
, x
, y
, layer
);
908 static inline const float *
909 get_texel_cube_seamless(const struct sp_sampler_view
*sp_sview
,
910 union tex_tile_address addr
, int x
, int y
,
911 float *corner
, int layer
, unsigned face
)
913 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
914 unsigned level
= addr
.bits
.level
;
915 int new_x
, new_y
, max_x
;
917 max_x
= (int) u_minify(texture
->width0
, level
);
919 assert(texture
->width0
== texture
->height0
);
923 /* change the face */
926 * Cheat with corners. They are difficult and I believe because we don't get
927 * per-pixel faces we can actually have multiple corner texels per pixel,
928 * which screws things up majorly in any case (as the per spec behavior is
929 * to average the 3 remaining texels, which we might not have).
930 * Hence just make sure that the 2nd coord is clamped, will simply pick the
931 * sample which would have fallen off the x coord, but not y coord.
932 * So the filter weight of the samples will be wrong, but at least this
933 * ensures that only valid texels near the corner are used.
935 if (y
< 0 || y
>= max_x
) {
936 y
= CLAMP(y
, 0, max_x
- 1);
938 new_x
= get_next_xcoord(face
, 0, max_x
-1, x
, y
);
939 new_y
= get_next_ycoord(face
, 0, max_x
-1, x
, y
);
940 face
= get_next_face(face
, 0);
941 } else if (x
>= max_x
) {
942 if (y
< 0 || y
>= max_x
) {
943 y
= CLAMP(y
, 0, max_x
- 1);
945 new_x
= get_next_xcoord(face
, 1, max_x
-1, x
, y
);
946 new_y
= get_next_ycoord(face
, 1, max_x
-1, x
, y
);
947 face
= get_next_face(face
, 1);
949 new_x
= get_next_xcoord(face
, 2, max_x
-1, x
, y
);
950 new_y
= get_next_ycoord(face
, 2, max_x
-1, x
, y
);
951 face
= get_next_face(face
, 2);
952 } else if (y
>= max_x
) {
953 new_x
= get_next_xcoord(face
, 3, max_x
-1, x
, y
);
954 new_y
= get_next_ycoord(face
, 3, max_x
-1, x
, y
);
955 face
= get_next_face(face
, 3);
958 return get_texel_3d_no_border(sp_sview
, addr
, new_x
, new_y
, layer
+ face
);
962 /* Get texel pointer for cube array texture */
963 static inline const float *
964 get_texel_cube_array(const struct sp_sampler_view
*sp_sview
,
965 const struct sp_sampler
*sp_samp
,
966 union tex_tile_address addr
, int x
, int y
, int layer
)
968 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
969 unsigned level
= addr
.bits
.level
;
971 assert(layer
< (int) texture
->array_size
);
974 if (x
< 0 || x
>= (int) u_minify(texture
->width0
, level
) ||
975 y
< 0 || y
>= (int) u_minify(texture
->height0
, level
)) {
976 return sp_samp
->base
.border_color
.f
;
979 return get_texel_3d_no_border(sp_sview
, addr
, x
, y
, layer
);
983 * Given the logbase2 of a mipmap's base level size and a mipmap level,
984 * return the size (in texels) of that mipmap level.
985 * For example, if level[0].width = 256 then base_pot will be 8.
986 * If level = 2, then we'll return 64 (the width at level=2).
987 * Return 1 if level > base_pot.
989 static inline unsigned
990 pot_level_size(unsigned base_pot
, unsigned level
)
992 return (base_pot
>= level
) ? (1 << (base_pot
- level
)) : 1;
997 print_sample(const char *function
, const float *rgba
)
999 debug_printf("%s %g %g %g %g\n",
1001 rgba
[0], rgba
[TGSI_NUM_CHANNELS
], rgba
[2*TGSI_NUM_CHANNELS
], rgba
[3*TGSI_NUM_CHANNELS
]);
1006 print_sample_4(const char *function
, float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1008 debug_printf("%s %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
1010 rgba
[0][0], rgba
[1][0], rgba
[2][0], rgba
[3][0],
1011 rgba
[0][1], rgba
[1][1], rgba
[2][1], rgba
[3][1],
1012 rgba
[0][2], rgba
[1][2], rgba
[2][2], rgba
[3][2],
1013 rgba
[0][3], rgba
[1][3], rgba
[2][3], rgba
[3][3]);
1017 /* Some image-filter fastpaths:
1020 img_filter_2d_linear_repeat_POT(struct sp_sampler_view
*sp_sview
,
1021 struct sp_sampler
*sp_samp
,
1022 const struct img_filter_args
*args
,
1025 unsigned xpot
= pot_level_size(sp_sview
->xpot
, args
->level
);
1026 unsigned ypot
= pot_level_size(sp_sview
->ypot
, args
->level
);
1027 int xmax
= (xpot
- 1) & (TEX_TILE_SIZE
- 1); /* MIN2(TEX_TILE_SIZE, xpot) - 1; */
1028 int ymax
= (ypot
- 1) & (TEX_TILE_SIZE
- 1); /* MIN2(TEX_TILE_SIZE, ypot) - 1; */
1029 union tex_tile_address addr
;
1032 float u
= (args
->s
* xpot
- 0.5F
) + args
->offset
[0];
1033 float v
= (args
->t
* ypot
- 0.5F
) + args
->offset
[1];
1035 int uflr
= util_ifloor(u
);
1036 int vflr
= util_ifloor(v
);
1038 float xw
= u
- (float)uflr
;
1039 float yw
= v
- (float)vflr
;
1041 int x0
= uflr
& (xpot
- 1);
1042 int y0
= vflr
& (ypot
- 1);
1047 addr
.bits
.level
= args
->level
;
1049 /* Can we fetch all four at once:
1051 if (x0
< xmax
&& y0
< ymax
) {
1052 get_texel_quad_2d_no_border_single_tile(sp_sview
, addr
, x0
, y0
, tx
);
1055 unsigned x1
= (x0
+ 1) & (xpot
- 1);
1056 unsigned y1
= (y0
+ 1) & (ypot
- 1);
1057 get_texel_quad_2d_no_border(sp_sview
, addr
, x0
, y0
, x1
, y1
, tx
);
1060 /* interpolate R, G, B, A */
1061 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++) {
1062 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1064 tx
[2][c
], tx
[3][c
]);
1068 print_sample(__FUNCTION__
, rgba
);
1074 img_filter_2d_nearest_repeat_POT(struct sp_sampler_view
*sp_sview
,
1075 struct sp_sampler
*sp_samp
,
1076 const struct img_filter_args
*args
,
1077 float rgba
[TGSI_QUAD_SIZE
])
1079 unsigned xpot
= pot_level_size(sp_sview
->xpot
, args
->level
);
1080 unsigned ypot
= pot_level_size(sp_sview
->ypot
, args
->level
);
1082 union tex_tile_address addr
;
1085 float u
= args
->s
* xpot
+ args
->offset
[0];
1086 float v
= args
->t
* ypot
+ args
->offset
[1];
1088 int uflr
= util_ifloor(u
);
1089 int vflr
= util_ifloor(v
);
1091 int x0
= uflr
& (xpot
- 1);
1092 int y0
= vflr
& (ypot
- 1);
1095 addr
.bits
.level
= args
->level
;
1097 out
= get_texel_2d_no_border(sp_sview
, addr
, x0
, y0
);
1098 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1099 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1102 print_sample(__FUNCTION__
, rgba
);
1108 img_filter_2d_nearest_clamp_POT(struct sp_sampler_view
*sp_sview
,
1109 struct sp_sampler
*sp_samp
,
1110 const struct img_filter_args
*args
,
1111 float rgba
[TGSI_QUAD_SIZE
])
1113 unsigned xpot
= pot_level_size(sp_sview
->xpot
, args
->level
);
1114 unsigned ypot
= pot_level_size(sp_sview
->ypot
, args
->level
);
1115 union tex_tile_address addr
;
1118 float u
= args
->s
* xpot
+ args
->offset
[0];
1119 float v
= args
->t
* ypot
+ args
->offset
[1];
1125 addr
.bits
.level
= args
->level
;
1127 x0
= util_ifloor(u
);
1130 else if (x0
> (int) xpot
- 1)
1133 y0
= util_ifloor(v
);
1136 else if (y0
> (int) ypot
- 1)
1139 out
= get_texel_2d_no_border(sp_sview
, addr
, x0
, y0
);
1140 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1141 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1144 print_sample(__FUNCTION__
, rgba
);
1150 img_filter_1d_nearest(struct sp_sampler_view
*sp_sview
,
1151 struct sp_sampler
*sp_samp
,
1152 const struct img_filter_args
*args
,
1153 float rgba
[TGSI_QUAD_SIZE
])
1155 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1158 union tex_tile_address addr
;
1162 width
= u_minify(texture
->width0
, args
->level
);
1167 addr
.bits
.level
= args
->level
;
1169 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1171 out
= get_texel_2d(sp_sview
, sp_samp
, addr
, x
, 0);
1172 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1173 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1176 print_sample(__FUNCTION__
, rgba
);
1182 img_filter_1d_array_nearest(struct sp_sampler_view
*sp_sview
,
1183 struct sp_sampler
*sp_samp
,
1184 const struct img_filter_args
*args
,
1187 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1190 union tex_tile_address addr
;
1194 width
= u_minify(texture
->width0
, args
->level
);
1199 addr
.bits
.level
= args
->level
;
1201 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1202 layer
= coord_to_layer(args
->t
, sp_sview
->base
.u
.tex
.first_layer
,
1203 sp_sview
->base
.u
.tex
.last_layer
);
1205 out
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x
, layer
);
1206 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1207 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1210 print_sample(__FUNCTION__
, rgba
);
1216 img_filter_2d_nearest(struct sp_sampler_view
*sp_sview
,
1217 struct sp_sampler
*sp_samp
,
1218 const struct img_filter_args
*args
,
1221 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1224 union tex_tile_address addr
;
1228 width
= u_minify(texture
->width0
, args
->level
);
1229 height
= u_minify(texture
->height0
, args
->level
);
1235 addr
.bits
.level
= args
->level
;
1237 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1238 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1240 out
= get_texel_2d(sp_sview
, sp_samp
, addr
, x
, y
);
1241 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1242 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1245 print_sample(__FUNCTION__
, rgba
);
1251 img_filter_2d_array_nearest(struct sp_sampler_view
*sp_sview
,
1252 struct sp_sampler
*sp_samp
,
1253 const struct img_filter_args
*args
,
1256 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1259 union tex_tile_address addr
;
1263 width
= u_minify(texture
->width0
, args
->level
);
1264 height
= u_minify(texture
->height0
, args
->level
);
1270 addr
.bits
.level
= args
->level
;
1272 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1273 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1274 layer
= coord_to_layer(args
->p
, sp_sview
->base
.u
.tex
.first_layer
,
1275 sp_sview
->base
.u
.tex
.last_layer
);
1277 out
= get_texel_2d_array(sp_sview
, sp_samp
, addr
, x
, y
, layer
);
1278 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1279 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1282 print_sample(__FUNCTION__
, rgba
);
1288 img_filter_cube_nearest(struct sp_sampler_view
*sp_sview
,
1289 struct sp_sampler
*sp_samp
,
1290 const struct img_filter_args
*args
,
1293 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1295 int x
, y
, layerface
;
1296 union tex_tile_address addr
;
1300 width
= u_minify(texture
->width0
, args
->level
);
1301 height
= u_minify(texture
->height0
, args
->level
);
1307 addr
.bits
.level
= args
->level
;
1310 * If NEAREST filtering is done within a miplevel, always apply wrap
1311 * mode CLAMP_TO_EDGE.
1313 if (sp_samp
->base
.seamless_cube_map
) {
1314 wrap_nearest_clamp_to_edge(args
->s
, width
, args
->offset
[0], &x
);
1315 wrap_nearest_clamp_to_edge(args
->t
, height
, args
->offset
[1], &y
);
1317 /* Would probably make sense to ignore mode and just do edge clamp */
1318 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1319 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1322 layerface
= args
->face_id
+ sp_sview
->base
.u
.tex
.first_layer
;
1323 out
= get_texel_cube_array(sp_sview
, sp_samp
, addr
, x
, y
, layerface
);
1324 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1325 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1328 print_sample(__FUNCTION__
, rgba
);
1333 img_filter_cube_array_nearest(struct sp_sampler_view
*sp_sview
,
1334 struct sp_sampler
*sp_samp
,
1335 const struct img_filter_args
*args
,
1338 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1340 int x
, y
, layerface
;
1341 union tex_tile_address addr
;
1345 width
= u_minify(texture
->width0
, args
->level
);
1346 height
= u_minify(texture
->height0
, args
->level
);
1352 addr
.bits
.level
= args
->level
;
1354 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1355 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1356 layerface
= coord_to_layer(6 * args
->p
+ sp_sview
->base
.u
.tex
.first_layer
,
1357 sp_sview
->base
.u
.tex
.first_layer
,
1358 sp_sview
->base
.u
.tex
.last_layer
- 5) + args
->face_id
;
1360 out
= get_texel_cube_array(sp_sview
, sp_samp
, addr
, x
, y
, layerface
);
1361 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1362 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1365 print_sample(__FUNCTION__
, rgba
);
1370 img_filter_3d_nearest(struct sp_sampler_view
*sp_sview
,
1371 struct sp_sampler
*sp_samp
,
1372 const struct img_filter_args
*args
,
1375 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1376 int width
, height
, depth
;
1378 union tex_tile_address addr
;
1382 width
= u_minify(texture
->width0
, args
->level
);
1383 height
= u_minify(texture
->height0
, args
->level
);
1384 depth
= u_minify(texture
->depth0
, args
->level
);
1390 sp_samp
->nearest_texcoord_s(args
->s
, width
, args
->offset
[0], &x
);
1391 sp_samp
->nearest_texcoord_t(args
->t
, height
, args
->offset
[1], &y
);
1392 sp_samp
->nearest_texcoord_p(args
->p
, depth
, args
->offset
[2], &z
);
1395 addr
.bits
.level
= args
->level
;
1397 out
= get_texel_3d(sp_sview
, sp_samp
, addr
, x
, y
, z
);
1398 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1399 rgba
[TGSI_NUM_CHANNELS
*c
] = out
[c
];
1404 img_filter_1d_linear(struct sp_sampler_view
*sp_sview
,
1405 struct sp_sampler
*sp_samp
,
1406 const struct img_filter_args
*args
,
1409 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1412 float xw
; /* weights */
1413 union tex_tile_address addr
;
1414 const float *tx0
, *tx1
;
1417 width
= u_minify(texture
->width0
, args
->level
);
1422 addr
.bits
.level
= args
->level
;
1424 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1426 tx0
= get_texel_2d(sp_sview
, sp_samp
, addr
, x0
, 0);
1427 tx1
= get_texel_2d(sp_sview
, sp_samp
, addr
, x1
, 0);
1429 /* interpolate R, G, B, A */
1430 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1431 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp(xw
, tx0
[c
], tx1
[c
]);
1436 img_filter_1d_array_linear(struct sp_sampler_view
*sp_sview
,
1437 struct sp_sampler
*sp_samp
,
1438 const struct img_filter_args
*args
,
1441 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1444 float xw
; /* weights */
1445 union tex_tile_address addr
;
1446 const float *tx0
, *tx1
;
1449 width
= u_minify(texture
->width0
, args
->level
);
1454 addr
.bits
.level
= args
->level
;
1456 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1457 layer
= coord_to_layer(args
->t
, sp_sview
->base
.u
.tex
.first_layer
,
1458 sp_sview
->base
.u
.tex
.last_layer
);
1460 tx0
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x0
, layer
);
1461 tx1
= get_texel_1d_array(sp_sview
, sp_samp
, addr
, x1
, layer
);
1463 /* interpolate R, G, B, A */
1464 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1465 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp(xw
, tx0
[c
], tx1
[c
]);
1469 * Retrieve the gathered value, need to convert to the
1470 * TGSI expected interface, and take component select
1471 * and swizzling into account.
1474 get_gather_value(const struct sp_sampler_view
*sp_sview
,
1475 int chan_in
, int comp_sel
,
1482 * softpipe samples in a different order
1483 * to TGSI expects, so we need to swizzle,
1484 * the samples into the correct slots.
1504 /* pick which component to use for the swizzle */
1507 swizzle
= sp_sview
->base
.swizzle_r
;
1510 swizzle
= sp_sview
->base
.swizzle_g
;
1513 swizzle
= sp_sview
->base
.swizzle_b
;
1516 swizzle
= sp_sview
->base
.swizzle_a
;
1523 /* get correct result using the channel and swizzle */
1525 case PIPE_SWIZZLE_ZERO
:
1527 case PIPE_SWIZZLE_ONE
:
1530 return tx
[chan
][swizzle
];
1536 img_filter_2d_linear(struct sp_sampler_view
*sp_sview
,
1537 struct sp_sampler
*sp_samp
,
1538 const struct img_filter_args
*args
,
1541 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1544 float xw
, yw
; /* weights */
1545 union tex_tile_address addr
;
1549 width
= u_minify(texture
->width0
, args
->level
);
1550 height
= u_minify(texture
->height0
, args
->level
);
1556 addr
.bits
.level
= args
->level
;
1558 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1559 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1561 tx
[0] = get_texel_2d(sp_sview
, sp_samp
, addr
, x0
, y0
);
1562 tx
[1] = get_texel_2d(sp_sview
, sp_samp
, addr
, x1
, y0
);
1563 tx
[2] = get_texel_2d(sp_sview
, sp_samp
, addr
, x0
, y1
);
1564 tx
[3] = get_texel_2d(sp_sview
, sp_samp
, addr
, x1
, y1
);
1566 if (args
->gather_only
) {
1567 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1568 rgba
[TGSI_NUM_CHANNELS
*c
] = get_gather_value(sp_sview
, c
,
1572 /* interpolate R, G, B, A */
1573 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1574 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1576 tx
[2][c
], tx
[3][c
]);
1582 img_filter_2d_array_linear(struct sp_sampler_view
*sp_sview
,
1583 struct sp_sampler
*sp_samp
,
1584 const struct img_filter_args
*args
,
1587 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1589 int x0
, y0
, x1
, y1
, layer
;
1590 float xw
, yw
; /* weights */
1591 union tex_tile_address addr
;
1595 width
= u_minify(texture
->width0
, args
->level
);
1596 height
= u_minify(texture
->height0
, args
->level
);
1602 addr
.bits
.level
= args
->level
;
1604 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1605 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1606 layer
= coord_to_layer(args
->p
, sp_sview
->base
.u
.tex
.first_layer
,
1607 sp_sview
->base
.u
.tex
.last_layer
);
1609 tx
[0] = get_texel_2d_array(sp_sview
, sp_samp
, addr
, x0
, y0
, layer
);
1610 tx
[1] = get_texel_2d_array(sp_sview
, sp_samp
, addr
, x1
, y0
, layer
);
1611 tx
[2] = get_texel_2d_array(sp_sview
, sp_samp
, addr
, x0
, y1
, layer
);
1612 tx
[3] = get_texel_2d_array(sp_sview
, sp_samp
, addr
, x1
, y1
, layer
);
1614 if (args
->gather_only
) {
1615 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1616 rgba
[TGSI_NUM_CHANNELS
*c
] = get_gather_value(sp_sview
, c
,
1620 /* interpolate R, G, B, A */
1621 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1622 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1624 tx
[2][c
], tx
[3][c
]);
1630 img_filter_cube_linear(struct sp_sampler_view
*sp_sview
,
1631 struct sp_sampler
*sp_samp
,
1632 const struct img_filter_args
*args
,
1635 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1637 int x0
, y0
, x1
, y1
, layer
;
1638 float xw
, yw
; /* weights */
1639 union tex_tile_address addr
;
1641 float corner0
[TGSI_QUAD_SIZE
], corner1
[TGSI_QUAD_SIZE
],
1642 corner2
[TGSI_QUAD_SIZE
], corner3
[TGSI_QUAD_SIZE
];
1645 width
= u_minify(texture
->width0
, args
->level
);
1646 height
= u_minify(texture
->height0
, args
->level
);
1652 addr
.bits
.level
= args
->level
;
1655 * For seamless if LINEAR filtering is done within a miplevel,
1656 * always apply wrap mode CLAMP_TO_BORDER.
1658 if (sp_samp
->base
.seamless_cube_map
) {
1659 /* Note this is a bit overkill, actual clamping is not required */
1660 wrap_linear_clamp_to_border(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1661 wrap_linear_clamp_to_border(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1663 /* Would probably make sense to ignore mode and just do edge clamp */
1664 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1665 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1668 layer
= sp_sview
->base
.u
.tex
.first_layer
;
1670 if (sp_samp
->base
.seamless_cube_map
) {
1671 tx
[0] = get_texel_cube_seamless(sp_sview
, addr
, x0
, y0
, corner0
, layer
, args
->face_id
);
1672 tx
[1] = get_texel_cube_seamless(sp_sview
, addr
, x1
, y0
, corner1
, layer
, args
->face_id
);
1673 tx
[2] = get_texel_cube_seamless(sp_sview
, addr
, x0
, y1
, corner2
, layer
, args
->face_id
);
1674 tx
[3] = get_texel_cube_seamless(sp_sview
, addr
, x1
, y1
, corner3
, layer
, args
->face_id
);
1676 tx
[0] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x0
, y0
, layer
+ args
->face_id
);
1677 tx
[1] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x1
, y0
, layer
+ args
->face_id
);
1678 tx
[2] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x0
, y1
, layer
+ args
->face_id
);
1679 tx
[3] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x1
, y1
, layer
+ args
->face_id
);
1682 if (args
->gather_only
) {
1683 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1684 rgba
[TGSI_NUM_CHANNELS
*c
] = get_gather_value(sp_sview
, c
,
1688 /* interpolate R, G, B, A */
1689 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1690 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1692 tx
[2][c
], tx
[3][c
]);
1698 img_filter_cube_array_linear(struct sp_sampler_view
*sp_sview
,
1699 struct sp_sampler
*sp_samp
,
1700 const struct img_filter_args
*args
,
1703 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1705 int x0
, y0
, x1
, y1
, layer
;
1706 float xw
, yw
; /* weights */
1707 union tex_tile_address addr
;
1709 float corner0
[TGSI_QUAD_SIZE
], corner1
[TGSI_QUAD_SIZE
],
1710 corner2
[TGSI_QUAD_SIZE
], corner3
[TGSI_QUAD_SIZE
];
1713 width
= u_minify(texture
->width0
, args
->level
);
1714 height
= u_minify(texture
->height0
, args
->level
);
1720 addr
.bits
.level
= args
->level
;
1723 * For seamless if LINEAR filtering is done within a miplevel,
1724 * always apply wrap mode CLAMP_TO_BORDER.
1726 if (sp_samp
->base
.seamless_cube_map
) {
1727 /* Note this is a bit overkill, actual clamping is not required */
1728 wrap_linear_clamp_to_border(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1729 wrap_linear_clamp_to_border(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1731 /* Would probably make sense to ignore mode and just do edge clamp */
1732 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1733 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1736 layer
= coord_to_layer(6 * args
->p
+ sp_sview
->base
.u
.tex
.first_layer
,
1737 sp_sview
->base
.u
.tex
.first_layer
,
1738 sp_sview
->base
.u
.tex
.last_layer
- 5);
1740 if (sp_samp
->base
.seamless_cube_map
) {
1741 tx
[0] = get_texel_cube_seamless(sp_sview
, addr
, x0
, y0
, corner0
, layer
, args
->face_id
);
1742 tx
[1] = get_texel_cube_seamless(sp_sview
, addr
, x1
, y0
, corner1
, layer
, args
->face_id
);
1743 tx
[2] = get_texel_cube_seamless(sp_sview
, addr
, x0
, y1
, corner2
, layer
, args
->face_id
);
1744 tx
[3] = get_texel_cube_seamless(sp_sview
, addr
, x1
, y1
, corner3
, layer
, args
->face_id
);
1746 tx
[0] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x0
, y0
, layer
+ args
->face_id
);
1747 tx
[1] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x1
, y0
, layer
+ args
->face_id
);
1748 tx
[2] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x0
, y1
, layer
+ args
->face_id
);
1749 tx
[3] = get_texel_cube_array(sp_sview
, sp_samp
, addr
, x1
, y1
, layer
+ args
->face_id
);
1752 if (args
->gather_only
) {
1753 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1754 rgba
[TGSI_NUM_CHANNELS
*c
] = get_gather_value(sp_sview
, c
,
1758 /* interpolate R, G, B, A */
1759 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1760 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_2d(xw
, yw
,
1762 tx
[2][c
], tx
[3][c
]);
1767 img_filter_3d_linear(struct sp_sampler_view
*sp_sview
,
1768 struct sp_sampler
*sp_samp
,
1769 const struct img_filter_args
*args
,
1772 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
1773 int width
, height
, depth
;
1774 int x0
, x1
, y0
, y1
, z0
, z1
;
1775 float xw
, yw
, zw
; /* interpolation weights */
1776 union tex_tile_address addr
;
1777 const float *tx00
, *tx01
, *tx02
, *tx03
, *tx10
, *tx11
, *tx12
, *tx13
;
1780 width
= u_minify(texture
->width0
, args
->level
);
1781 height
= u_minify(texture
->height0
, args
->level
);
1782 depth
= u_minify(texture
->depth0
, args
->level
);
1785 addr
.bits
.level
= args
->level
;
1791 sp_samp
->linear_texcoord_s(args
->s
, width
, args
->offset
[0], &x0
, &x1
, &xw
);
1792 sp_samp
->linear_texcoord_t(args
->t
, height
, args
->offset
[1], &y0
, &y1
, &yw
);
1793 sp_samp
->linear_texcoord_p(args
->p
, depth
, args
->offset
[2], &z0
, &z1
, &zw
);
1795 tx00
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y0
, z0
);
1796 tx01
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y0
, z0
);
1797 tx02
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y1
, z0
);
1798 tx03
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y1
, z0
);
1800 tx10
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y0
, z1
);
1801 tx11
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y0
, z1
);
1802 tx12
= get_texel_3d(sp_sview
, sp_samp
, addr
, x0
, y1
, z1
);
1803 tx13
= get_texel_3d(sp_sview
, sp_samp
, addr
, x1
, y1
, z1
);
1805 /* interpolate R, G, B, A */
1806 for (c
= 0; c
< TGSI_QUAD_SIZE
; c
++)
1807 rgba
[TGSI_NUM_CHANNELS
*c
] = lerp_3d(xw
, yw
, zw
,
1815 /* Calculate level of detail for every fragment,
1816 * with lambda already computed.
1817 * Note that lambda has already been biased by global LOD bias.
1818 * \param biased_lambda per-quad lambda.
1819 * \param lod_in per-fragment lod_bias or explicit_lod.
1820 * \param lod returns the per-fragment lod.
1823 compute_lod(const struct pipe_sampler_state
*sampler
,
1824 enum tgsi_sampler_control control
,
1825 const float biased_lambda
,
1826 const float lod_in
[TGSI_QUAD_SIZE
],
1827 float lod
[TGSI_QUAD_SIZE
])
1829 float min_lod
= sampler
->min_lod
;
1830 float max_lod
= sampler
->max_lod
;
1834 case tgsi_sampler_lod_none
:
1835 case tgsi_sampler_lod_zero
:
1837 case tgsi_sampler_derivs_explicit
:
1838 lod
[0] = lod
[1] = lod
[2] = lod
[3] = CLAMP(biased_lambda
, min_lod
, max_lod
);
1840 case tgsi_sampler_lod_bias
:
1841 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1842 lod
[i
] = biased_lambda
+ lod_in
[i
];
1843 lod
[i
] = CLAMP(lod
[i
], min_lod
, max_lod
);
1846 case tgsi_sampler_lod_explicit
:
1847 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1848 lod
[i
] = CLAMP(lod_in
[i
], min_lod
, max_lod
);
1853 lod
[0] = lod
[1] = lod
[2] = lod
[3] = 0.0f
;
1858 /* Calculate level of detail for every fragment.
1859 * \param lod_in per-fragment lod_bias or explicit_lod.
1860 * \param lod results per-fragment lod.
1863 compute_lambda_lod(struct sp_sampler_view
*sp_sview
,
1864 struct sp_sampler
*sp_samp
,
1865 const float s
[TGSI_QUAD_SIZE
],
1866 const float t
[TGSI_QUAD_SIZE
],
1867 const float p
[TGSI_QUAD_SIZE
],
1868 const float lod_in
[TGSI_QUAD_SIZE
],
1869 enum tgsi_sampler_control control
,
1870 float lod
[TGSI_QUAD_SIZE
])
1872 const struct pipe_sampler_state
*sampler
= &sp_samp
->base
;
1873 float lod_bias
= sampler
->lod_bias
;
1874 float min_lod
= sampler
->min_lod
;
1875 float max_lod
= sampler
->max_lod
;
1880 case tgsi_sampler_lod_none
:
1882 case tgsi_sampler_derivs_explicit
:
1883 lambda
= sp_sview
->compute_lambda(sp_sview
, s
, t
, p
) + lod_bias
;
1884 lod
[0] = lod
[1] = lod
[2] = lod
[3] = CLAMP(lambda
, min_lod
, max_lod
);
1886 case tgsi_sampler_lod_bias
:
1887 lambda
= sp_sview
->compute_lambda(sp_sview
, s
, t
, p
) + lod_bias
;
1888 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1889 lod
[i
] = lambda
+ lod_in
[i
];
1890 lod
[i
] = CLAMP(lod
[i
], min_lod
, max_lod
);
1893 case tgsi_sampler_lod_explicit
:
1894 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
1895 lod
[i
] = CLAMP(lod_in
[i
], min_lod
, max_lod
);
1898 case tgsi_sampler_lod_zero
:
1899 case tgsi_sampler_gather
:
1900 /* this is all static state in the sampler really need clamp here? */
1901 lod
[0] = lod
[1] = lod
[2] = lod
[3] = CLAMP(lod_bias
, min_lod
, max_lod
);
1905 lod
[0] = lod
[1] = lod
[2] = lod
[3] = 0.0f
;
1909 static inline unsigned
1910 get_gather_component(const float lod_in
[TGSI_QUAD_SIZE
])
1912 /* gather component is stored in lod_in slot as unsigned */
1913 return (*(unsigned int *)lod_in
) & 0x3;
1917 mip_filter_linear(struct sp_sampler_view
*sp_sview
,
1918 struct sp_sampler
*sp_samp
,
1919 img_filter_func min_filter
,
1920 img_filter_func mag_filter
,
1921 const float s
[TGSI_QUAD_SIZE
],
1922 const float t
[TGSI_QUAD_SIZE
],
1923 const float p
[TGSI_QUAD_SIZE
],
1924 const float c0
[TGSI_QUAD_SIZE
],
1925 const float lod_in
[TGSI_QUAD_SIZE
],
1926 const struct filter_args
*filt_args
,
1927 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1929 const struct pipe_sampler_view
*psview
= &sp_sview
->base
;
1931 float lod
[TGSI_QUAD_SIZE
];
1932 struct img_filter_args args
;
1934 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, filt_args
->control
, lod
);
1936 args
.offset
= filt_args
->offset
;
1937 args
.gather_only
= filt_args
->control
== tgsi_sampler_gather
;
1938 args
.gather_comp
= get_gather_component(lod_in
);
1940 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
1941 int level0
= psview
->u
.tex
.first_level
+ (int)lod
[j
];
1946 args
.face_id
= sp_sview
->faces
[j
];
1949 args
.level
= psview
->u
.tex
.first_level
;
1950 mag_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
1952 else if (level0
>= (int) psview
->u
.tex
.last_level
) {
1953 args
.level
= psview
->u
.tex
.last_level
;
1954 min_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
1957 float levelBlend
= frac(lod
[j
]);
1958 float rgbax
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
1961 args
.level
= level0
;
1962 min_filter(sp_sview
, sp_samp
, &args
, &rgbax
[0][0]);
1963 args
.level
= level0
+1;
1964 min_filter(sp_sview
, sp_samp
, &args
, &rgbax
[0][1]);
1966 for (c
= 0; c
< 4; c
++) {
1967 rgba
[c
][j
] = lerp(levelBlend
, rgbax
[c
][0], rgbax
[c
][1]);
1973 print_sample_4(__FUNCTION__
, rgba
);
1979 * Compute nearest mipmap level from texcoords.
1980 * Then sample the texture level for four elements of a quad.
1981 * \param c0 the LOD bias factors, or absolute LODs (depending on control)
1984 mip_filter_nearest(struct sp_sampler_view
*sp_sview
,
1985 struct sp_sampler
*sp_samp
,
1986 img_filter_func min_filter
,
1987 img_filter_func mag_filter
,
1988 const float s
[TGSI_QUAD_SIZE
],
1989 const float t
[TGSI_QUAD_SIZE
],
1990 const float p
[TGSI_QUAD_SIZE
],
1991 const float c0
[TGSI_QUAD_SIZE
],
1992 const float lod_in
[TGSI_QUAD_SIZE
],
1993 const struct filter_args
*filt_args
,
1994 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
1996 const struct pipe_sampler_view
*psview
= &sp_sview
->base
;
1997 float lod
[TGSI_QUAD_SIZE
];
1999 struct img_filter_args args
;
2001 args
.offset
= filt_args
->offset
;
2002 args
.gather_only
= filt_args
->control
== tgsi_sampler_gather
;
2003 args
.gather_comp
= get_gather_component(lod_in
);
2005 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, filt_args
->control
, lod
);
2007 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2011 args
.face_id
= sp_sview
->faces
[j
];
2014 args
.level
= psview
->u
.tex
.first_level
;
2015 mag_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2017 int level
= psview
->u
.tex
.first_level
+ (int)(lod
[j
] + 0.5F
);
2018 args
.level
= MIN2(level
, (int)psview
->u
.tex
.last_level
);
2019 min_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2024 print_sample_4(__FUNCTION__
, rgba
);
2030 mip_filter_none(struct sp_sampler_view
*sp_sview
,
2031 struct sp_sampler
*sp_samp
,
2032 img_filter_func min_filter
,
2033 img_filter_func mag_filter
,
2034 const float s
[TGSI_QUAD_SIZE
],
2035 const float t
[TGSI_QUAD_SIZE
],
2036 const float p
[TGSI_QUAD_SIZE
],
2037 const float c0
[TGSI_QUAD_SIZE
],
2038 const float lod_in
[TGSI_QUAD_SIZE
],
2039 const struct filter_args
*filt_args
,
2040 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2042 float lod
[TGSI_QUAD_SIZE
];
2044 struct img_filter_args args
;
2046 args
.level
= sp_sview
->base
.u
.tex
.first_level
;
2047 args
.offset
= filt_args
->offset
;
2048 args
.gather_only
= filt_args
->control
== tgsi_sampler_gather
;
2050 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, filt_args
->control
, lod
);
2052 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2056 args
.face_id
= sp_sview
->faces
[j
];
2058 mag_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2061 min_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2068 mip_filter_none_no_filter_select(struct sp_sampler_view
*sp_sview
,
2069 struct sp_sampler
*sp_samp
,
2070 img_filter_func min_filter
,
2071 img_filter_func mag_filter
,
2072 const float s
[TGSI_QUAD_SIZE
],
2073 const float t
[TGSI_QUAD_SIZE
],
2074 const float p
[TGSI_QUAD_SIZE
],
2075 const float c0
[TGSI_QUAD_SIZE
],
2076 const float lod_in
[TGSI_QUAD_SIZE
],
2077 const struct filter_args
*filt_args
,
2078 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2081 struct img_filter_args args
;
2082 args
.level
= sp_sview
->base
.u
.tex
.first_level
;
2083 args
.offset
= filt_args
->offset
;
2084 args
.gather_only
= filt_args
->control
== tgsi_sampler_gather
;
2085 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2089 args
.face_id
= sp_sview
->faces
[j
];
2090 mag_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2095 /* For anisotropic filtering */
2096 #define WEIGHT_LUT_SIZE 1024
2098 static float *weightLut
= NULL
;
2101 * Creates the look-up table used to speed-up EWA sampling
2104 create_filter_table(void)
2108 weightLut
= (float *) MALLOC(WEIGHT_LUT_SIZE
* sizeof(float));
2110 for (i
= 0; i
< WEIGHT_LUT_SIZE
; ++i
) {
2112 float r2
= (float) i
/ (float) (WEIGHT_LUT_SIZE
- 1);
2113 float weight
= (float) exp(-alpha
* r2
);
2114 weightLut
[i
] = weight
;
2121 * Elliptical weighted average (EWA) filter for producing high quality
2122 * anisotropic filtered results.
2123 * Based on the Higher Quality Elliptical Weighted Average Filter
2124 * published by Paul S. Heckbert in his Master's Thesis
2125 * "Fundamentals of Texture Mapping and Image Warping" (1989)
2128 img_filter_2d_ewa(struct sp_sampler_view
*sp_sview
,
2129 struct sp_sampler
*sp_samp
,
2130 img_filter_func min_filter
,
2131 img_filter_func mag_filter
,
2132 const float s
[TGSI_QUAD_SIZE
],
2133 const float t
[TGSI_QUAD_SIZE
],
2134 const float p
[TGSI_QUAD_SIZE
],
2136 const float dudx
, const float dvdx
,
2137 const float dudy
, const float dvdy
,
2138 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2140 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
2142 // ??? Won't the image filters blow up if level is negative?
2143 unsigned level0
= level
> 0 ? level
: 0;
2144 float scaling
= 1.0f
/ (1 << level0
);
2145 int width
= u_minify(texture
->width0
, level0
);
2146 int height
= u_minify(texture
->height0
, level0
);
2147 struct img_filter_args args
;
2148 float ux
= dudx
* scaling
;
2149 float vx
= dvdx
* scaling
;
2150 float uy
= dudy
* scaling
;
2151 float vy
= dvdy
* scaling
;
2153 /* compute ellipse coefficients to bound the region:
2154 * A*x*x + B*x*y + C*y*y = F.
2156 float A
= vx
*vx
+vy
*vy
+1;
2157 float B
= -2*(ux
*vx
+uy
*vy
);
2158 float C
= ux
*ux
+uy
*uy
+1;
2159 float F
= A
*C
-B
*B
/4.0f
;
2161 /* check if it is an ellipse */
2162 /* assert(F > 0.0); */
2164 /* Compute the ellipse's (u,v) bounding box in texture space */
2165 float d
= -B
*B
+4.0f
*C
*A
;
2166 float box_u
= 2.0f
/ d
* sqrtf(d
*C
*F
); /* box_u -> half of bbox with */
2167 float box_v
= 2.0f
/ d
* sqrtf(A
*d
*F
); /* box_v -> half of bbox height */
2169 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2170 float s_buffer
[TGSI_QUAD_SIZE
];
2171 float t_buffer
[TGSI_QUAD_SIZE
];
2172 float weight_buffer
[TGSI_QUAD_SIZE
];
2173 unsigned buffer_next
;
2175 float den
; /* = 0.0F; */
2177 float U
; /* = u0 - tex_u; */
2180 /* Scale ellipse formula to directly index the Filter Lookup Table.
2181 * i.e. scale so that F = WEIGHT_LUT_SIZE-1
2183 double formScale
= (double) (WEIGHT_LUT_SIZE
- 1) / F
;
2187 /* F *= formScale; */ /* no need to scale F as we don't use it below here */
2189 /* For each quad, the du and dx values are the same and so the ellipse is
2190 * also the same. Note that texel/image access can only be performed using
2191 * a quad, i.e. it is not possible to get the pixel value for a single
2192 * tex coord. In order to have a better performance, the access is buffered
2193 * using the s_buffer/t_buffer and weight_buffer. Only when the buffer is
2194 * full, then the pixel values are read from the image.
2199 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2200 /* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse
2201 * and incrementally update the value of Ax^2+Bxy*Cy^2; when this
2202 * value, q, is less than F, we're inside the ellipse
2204 float tex_u
= -0.5F
+ s
[j
] * texture
->width0
* scaling
;
2205 float tex_v
= -0.5F
+ t
[j
] * texture
->height0
* scaling
;
2207 int u0
= (int) floorf(tex_u
- box_u
);
2208 int u1
= (int) ceilf(tex_u
+ box_u
);
2209 int v0
= (int) floorf(tex_v
- box_v
);
2210 int v1
= (int) ceilf(tex_v
+ box_v
);
2212 float num
[4] = {0.0F
, 0.0F
, 0.0F
, 0.0F
};
2215 args
.face_id
= sp_sview
->faces
[j
];
2218 for (v
= v0
; v
<= v1
; ++v
) {
2219 float V
= v
- tex_v
;
2220 float dq
= A
* (2 * U
+ 1) + B
* V
;
2221 float q
= (C
* V
+ B
* U
) * V
+ A
* U
* U
;
2224 for (u
= u0
; u
<= u1
; ++u
) {
2225 /* Note that the ellipse has been pre-scaled so F =
2226 * WEIGHT_LUT_SIZE - 1
2228 if (q
< WEIGHT_LUT_SIZE
) {
2229 /* as a LUT is used, q must never be negative;
2230 * should not happen, though
2232 const int qClamped
= q
>= 0.0F
? q
: 0;
2233 float weight
= weightLut
[qClamped
];
2235 weight_buffer
[buffer_next
] = weight
;
2236 s_buffer
[buffer_next
] = u
/ ((float) width
);
2237 t_buffer
[buffer_next
] = v
/ ((float) height
);
2240 if (buffer_next
== TGSI_QUAD_SIZE
) {
2241 /* 4 texel coords are in the buffer -> read it now */
2243 /* it is assumed that samp->min_img_filter is set to
2244 * img_filter_2d_nearest or one of the
2245 * accelerated img_filter_2d_nearest_XXX functions.
2247 for (jj
= 0; jj
< buffer_next
; jj
++) {
2248 args
.s
= s_buffer
[jj
];
2249 args
.t
= t_buffer
[jj
];
2251 min_filter(sp_sview
, sp_samp
, &args
, &rgba_temp
[0][jj
]);
2252 num
[0] += weight_buffer
[jj
] * rgba_temp
[0][jj
];
2253 num
[1] += weight_buffer
[jj
] * rgba_temp
[1][jj
];
2254 num
[2] += weight_buffer
[jj
] * rgba_temp
[2][jj
];
2255 num
[3] += weight_buffer
[jj
] * rgba_temp
[3][jj
];
2268 /* if the tex coord buffer contains unread values, we will read
2271 if (buffer_next
> 0) {
2273 /* it is assumed that samp->min_img_filter is set to
2274 * img_filter_2d_nearest or one of the
2275 * accelerated img_filter_2d_nearest_XXX functions.
2277 for (jj
= 0; jj
< buffer_next
; jj
++) {
2278 args
.s
= s_buffer
[jj
];
2279 args
.t
= t_buffer
[jj
];
2281 min_filter(sp_sview
, sp_samp
, &args
, &rgba_temp
[0][jj
]);
2282 num
[0] += weight_buffer
[jj
] * rgba_temp
[0][jj
];
2283 num
[1] += weight_buffer
[jj
] * rgba_temp
[1][jj
];
2284 num
[2] += weight_buffer
[jj
] * rgba_temp
[2][jj
];
2285 num
[3] += weight_buffer
[jj
] * rgba_temp
[3][jj
];
2290 /* Reaching this place would mean that no pixels intersected
2291 * the ellipse. This should never happen because the filter
2292 * we use always intersects at least one pixel.
2299 /* not enough pixels in resampling, resort to direct interpolation */
2303 min_filter(sp_sview
, sp_samp
, &args
, &rgba_temp
[0][j
]);
2305 num
[0] = rgba_temp
[0][j
];
2306 num
[1] = rgba_temp
[1][j
];
2307 num
[2] = rgba_temp
[2][j
];
2308 num
[3] = rgba_temp
[3][j
];
2311 rgba
[0][j
] = num
[0] / den
;
2312 rgba
[1][j
] = num
[1] / den
;
2313 rgba
[2][j
] = num
[2] / den
;
2314 rgba
[3][j
] = num
[3] / den
;
2320 * Sample 2D texture using an anisotropic filter.
2323 mip_filter_linear_aniso(struct sp_sampler_view
*sp_sview
,
2324 struct sp_sampler
*sp_samp
,
2325 img_filter_func min_filter
,
2326 img_filter_func mag_filter
,
2327 const float s
[TGSI_QUAD_SIZE
],
2328 const float t
[TGSI_QUAD_SIZE
],
2329 const float p
[TGSI_QUAD_SIZE
],
2330 const float c0
[TGSI_QUAD_SIZE
],
2331 const float lod_in
[TGSI_QUAD_SIZE
],
2332 const struct filter_args
*filt_args
,
2333 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2335 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
2336 const struct pipe_sampler_view
*psview
= &sp_sview
->base
;
2339 float lod
[TGSI_QUAD_SIZE
];
2341 float s_to_u
= u_minify(texture
->width0
, psview
->u
.tex
.first_level
);
2342 float t_to_v
= u_minify(texture
->height0
, psview
->u
.tex
.first_level
);
2343 float dudx
= (s
[QUAD_BOTTOM_RIGHT
] - s
[QUAD_BOTTOM_LEFT
]) * s_to_u
;
2344 float dudy
= (s
[QUAD_TOP_LEFT
] - s
[QUAD_BOTTOM_LEFT
]) * s_to_u
;
2345 float dvdx
= (t
[QUAD_BOTTOM_RIGHT
] - t
[QUAD_BOTTOM_LEFT
]) * t_to_v
;
2346 float dvdy
= (t
[QUAD_TOP_LEFT
] - t
[QUAD_BOTTOM_LEFT
]) * t_to_v
;
2347 struct img_filter_args args
;
2349 if (filt_args
->control
== tgsi_sampler_lod_bias
||
2350 filt_args
->control
== tgsi_sampler_lod_none
||
2352 filt_args
->control
== tgsi_sampler_derivs_explicit
) {
2353 /* note: instead of working with Px and Py, we will use the
2354 * squared length instead, to avoid sqrt.
2356 float Px2
= dudx
* dudx
+ dvdx
* dvdx
;
2357 float Py2
= dudy
* dudy
+ dvdy
* dvdy
;
2362 const float maxEccentricity
= sp_samp
->base
.max_anisotropy
* sp_samp
->base
.max_anisotropy
;
2373 /* if the eccentricity of the ellipse is too big, scale up the shorter
2374 * of the two vectors to limit the maximum amount of work per pixel
2377 if (e
> maxEccentricity
) {
2378 /* float s=e / maxEccentricity;
2382 Pmin2
= Pmax2
/ maxEccentricity
;
2385 /* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid
2386 * this since 0.5*log(x) = log(sqrt(x))
2388 lambda
= 0.5F
* util_fast_log2(Pmin2
) + sp_samp
->base
.lod_bias
;
2389 compute_lod(&sp_samp
->base
, filt_args
->control
, lambda
, lod_in
, lod
);
2392 assert(filt_args
->control
== tgsi_sampler_lod_explicit
||
2393 filt_args
->control
== tgsi_sampler_lod_zero
);
2394 compute_lod(&sp_samp
->base
, filt_args
->control
, sp_samp
->base
.lod_bias
, lod_in
, lod
);
2397 /* XXX: Take into account all lod values.
2400 level0
= psview
->u
.tex
.first_level
+ (int)lambda
;
2402 /* If the ellipse covers the whole image, we can
2403 * simply return the average of the whole image.
2405 if (level0
>= (int) psview
->u
.tex
.last_level
) {
2407 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2411 args
.level
= psview
->u
.tex
.last_level
;
2412 args
.face_id
= sp_sview
->faces
[j
];
2413 min_filter(sp_sview
, sp_samp
, &args
, &rgba
[0][j
]);
2417 /* don't bother interpolating between multiple LODs; it doesn't
2418 * seem to be worth the extra running time.
2420 img_filter_2d_ewa(sp_sview
, sp_samp
, min_filter
, mag_filter
,
2422 dudx
, dvdx
, dudy
, dvdy
, rgba
);
2426 print_sample_4(__FUNCTION__
, rgba
);
2432 * Specialized version of mip_filter_linear with hard-wired calls to
2433 * 2d lambda calculation and 2d_linear_repeat_POT img filters.
2436 mip_filter_linear_2d_linear_repeat_POT(
2437 struct sp_sampler_view
*sp_sview
,
2438 struct sp_sampler
*sp_samp
,
2439 img_filter_func min_filter
,
2440 img_filter_func mag_filter
,
2441 const float s
[TGSI_QUAD_SIZE
],
2442 const float t
[TGSI_QUAD_SIZE
],
2443 const float p
[TGSI_QUAD_SIZE
],
2444 const float c0
[TGSI_QUAD_SIZE
],
2445 const float lod_in
[TGSI_QUAD_SIZE
],
2446 const struct filter_args
*filt_args
,
2447 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2449 const struct pipe_sampler_view
*psview
= &sp_sview
->base
;
2451 float lod
[TGSI_QUAD_SIZE
];
2453 compute_lambda_lod(sp_sview
, sp_samp
, s
, t
, p
, lod_in
, filt_args
->control
, lod
);
2455 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2456 int level0
= psview
->u
.tex
.first_level
+ (int)lod
[j
];
2457 struct img_filter_args args
;
2458 /* Catches both negative and large values of level0:
2463 args
.face_id
= sp_sview
->faces
[j
];
2464 args
.offset
= filt_args
->offset
;
2465 args
.gather_only
= filt_args
->control
== tgsi_sampler_gather
;
2466 if ((unsigned)level0
>= psview
->u
.tex
.last_level
) {
2468 args
.level
= psview
->u
.tex
.first_level
;
2470 args
.level
= psview
->u
.tex
.last_level
;
2471 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, &args
,
2476 float levelBlend
= frac(lod
[j
]);
2477 float rgbax
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2480 args
.level
= level0
;
2481 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, &args
, &rgbax
[0][0]);
2482 args
.level
= level0
+1;
2483 img_filter_2d_linear_repeat_POT(sp_sview
, sp_samp
, &args
, &rgbax
[0][1]);
2485 for (c
= 0; c
< TGSI_NUM_CHANNELS
; c
++)
2486 rgba
[c
][j
] = lerp(levelBlend
, rgbax
[c
][0], rgbax
[c
][1]);
2491 print_sample_4(__FUNCTION__
, rgba
);
2497 * Do shadow/depth comparisons.
2500 sample_compare(struct sp_sampler_view
*sp_sview
,
2501 struct sp_sampler
*sp_samp
,
2502 const float s
[TGSI_QUAD_SIZE
],
2503 const float t
[TGSI_QUAD_SIZE
],
2504 const float p
[TGSI_QUAD_SIZE
],
2505 const float c0
[TGSI_QUAD_SIZE
],
2506 const float c1
[TGSI_QUAD_SIZE
],
2507 enum tgsi_sampler_control control
,
2508 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2510 const struct pipe_sampler_state
*sampler
= &sp_samp
->base
;
2512 int k
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2514 const struct util_format_description
*format_desc
;
2516 bool is_gather
= (control
== tgsi_sampler_gather
);
2519 * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
2520 * for 2D Array texture we need to use the 'c0' (aka Q).
2521 * When we sampled the depth texture, the depth value was put into all
2522 * RGBA channels. We look at the red channel here.
2525 if (sp_sview
->base
.target
== PIPE_TEXTURE_2D_ARRAY
||
2526 sp_sview
->base
.target
== PIPE_TEXTURE_CUBE
) {
2531 } else if (sp_sview
->base
.target
== PIPE_TEXTURE_CUBE_ARRAY
) {
2543 format_desc
= util_format_description(sp_sview
->base
.format
);
2544 /* not entirely sure we couldn't end up with non-valid swizzle here */
2545 chan_type
= format_desc
->swizzle
[0] <= UTIL_FORMAT_SWIZZLE_W
?
2546 format_desc
->channel
[format_desc
->swizzle
[0]].type
:
2547 UTIL_FORMAT_TYPE_FLOAT
;
2548 if (chan_type
!= UTIL_FORMAT_TYPE_FLOAT
) {
2550 * clamping is a result of conversion to texture format, hence
2551 * doesn't happen with floats. Technically also should do comparison
2552 * in texture format (quantization!).
2554 pc
[0] = CLAMP(pc
[0], 0.0F
, 1.0F
);
2555 pc
[1] = CLAMP(pc
[1], 0.0F
, 1.0F
);
2556 pc
[2] = CLAMP(pc
[2], 0.0F
, 1.0F
);
2557 pc
[3] = CLAMP(pc
[3], 0.0F
, 1.0F
);
2560 for (v
= 0; v
< (is_gather
? TGSI_NUM_CHANNELS
: 1); v
++) {
2561 /* compare four texcoords vs. four texture samples */
2562 switch (sampler
->compare_func
) {
2563 case PIPE_FUNC_LESS
:
2564 k
[v
][0] = pc
[0] < rgba
[v
][0];
2565 k
[v
][1] = pc
[1] < rgba
[v
][1];
2566 k
[v
][2] = pc
[2] < rgba
[v
][2];
2567 k
[v
][3] = pc
[3] < rgba
[v
][3];
2569 case PIPE_FUNC_LEQUAL
:
2570 k
[v
][0] = pc
[0] <= rgba
[v
][0];
2571 k
[v
][1] = pc
[1] <= rgba
[v
][1];
2572 k
[v
][2] = pc
[2] <= rgba
[v
][2];
2573 k
[v
][3] = pc
[3] <= rgba
[v
][3];
2575 case PIPE_FUNC_GREATER
:
2576 k
[v
][0] = pc
[0] > rgba
[v
][0];
2577 k
[v
][1] = pc
[1] > rgba
[v
][1];
2578 k
[v
][2] = pc
[2] > rgba
[v
][2];
2579 k
[v
][3] = pc
[3] > rgba
[v
][3];
2581 case PIPE_FUNC_GEQUAL
:
2582 k
[v
][0] = pc
[0] >= rgba
[v
][0];
2583 k
[v
][1] = pc
[1] >= rgba
[v
][1];
2584 k
[v
][2] = pc
[2] >= rgba
[v
][2];
2585 k
[v
][3] = pc
[3] >= rgba
[v
][3];
2587 case PIPE_FUNC_EQUAL
:
2588 k
[v
][0] = pc
[0] == rgba
[v
][0];
2589 k
[v
][1] = pc
[1] == rgba
[v
][1];
2590 k
[v
][2] = pc
[2] == rgba
[v
][2];
2591 k
[v
][3] = pc
[3] == rgba
[v
][3];
2593 case PIPE_FUNC_NOTEQUAL
:
2594 k
[v
][0] = pc
[0] != rgba
[v
][0];
2595 k
[v
][1] = pc
[1] != rgba
[v
][1];
2596 k
[v
][2] = pc
[2] != rgba
[v
][2];
2597 k
[v
][3] = pc
[3] != rgba
[v
][3];
2599 case PIPE_FUNC_ALWAYS
:
2600 k
[v
][0] = k
[v
][1] = k
[v
][2] = k
[v
][3] = 1;
2602 case PIPE_FUNC_NEVER
:
2603 k
[v
][0] = k
[v
][1] = k
[v
][2] = k
[v
][3] = 0;
2606 k
[v
][0] = k
[v
][1] = k
[v
][2] = k
[v
][3] = 0;
2613 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2614 for (v
= 0; v
< TGSI_NUM_CHANNELS
; v
++) {
2615 rgba
[v
][j
] = k
[v
][j
];
2619 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2620 rgba
[0][j
] = k
[0][j
];
2621 rgba
[1][j
] = k
[0][j
];
2622 rgba
[2][j
] = k
[0][j
];
2629 do_swizzling(const struct pipe_sampler_view
*sview
,
2630 float in
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
],
2631 float out
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2634 const unsigned swizzle_r
= sview
->swizzle_r
;
2635 const unsigned swizzle_g
= sview
->swizzle_g
;
2636 const unsigned swizzle_b
= sview
->swizzle_b
;
2637 const unsigned swizzle_a
= sview
->swizzle_a
;
2639 switch (swizzle_r
) {
2640 case PIPE_SWIZZLE_ZERO
:
2641 for (j
= 0; j
< 4; j
++)
2644 case PIPE_SWIZZLE_ONE
:
2645 for (j
= 0; j
< 4; j
++)
2649 assert(swizzle_r
< 4);
2650 for (j
= 0; j
< 4; j
++)
2651 out
[0][j
] = in
[swizzle_r
][j
];
2654 switch (swizzle_g
) {
2655 case PIPE_SWIZZLE_ZERO
:
2656 for (j
= 0; j
< 4; j
++)
2659 case PIPE_SWIZZLE_ONE
:
2660 for (j
= 0; j
< 4; j
++)
2664 assert(swizzle_g
< 4);
2665 for (j
= 0; j
< 4; j
++)
2666 out
[1][j
] = in
[swizzle_g
][j
];
2669 switch (swizzle_b
) {
2670 case PIPE_SWIZZLE_ZERO
:
2671 for (j
= 0; j
< 4; j
++)
2674 case PIPE_SWIZZLE_ONE
:
2675 for (j
= 0; j
< 4; j
++)
2679 assert(swizzle_b
< 4);
2680 for (j
= 0; j
< 4; j
++)
2681 out
[2][j
] = in
[swizzle_b
][j
];
2684 switch (swizzle_a
) {
2685 case PIPE_SWIZZLE_ZERO
:
2686 for (j
= 0; j
< 4; j
++)
2689 case PIPE_SWIZZLE_ONE
:
2690 for (j
= 0; j
< 4; j
++)
2694 assert(swizzle_a
< 4);
2695 for (j
= 0; j
< 4; j
++)
2696 out
[3][j
] = in
[swizzle_a
][j
];
2701 static wrap_nearest_func
2702 get_nearest_unorm_wrap(unsigned mode
)
2705 case PIPE_TEX_WRAP_CLAMP
:
2706 return wrap_nearest_unorm_clamp
;
2707 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2708 return wrap_nearest_unorm_clamp_to_edge
;
2709 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2710 return wrap_nearest_unorm_clamp_to_border
;
2712 debug_printf("illegal wrap mode %d with non-normalized coords\n", mode
);
2713 return wrap_nearest_unorm_clamp
;
2718 static wrap_nearest_func
2719 get_nearest_wrap(unsigned mode
)
2722 case PIPE_TEX_WRAP_REPEAT
:
2723 return wrap_nearest_repeat
;
2724 case PIPE_TEX_WRAP_CLAMP
:
2725 return wrap_nearest_clamp
;
2726 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2727 return wrap_nearest_clamp_to_edge
;
2728 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2729 return wrap_nearest_clamp_to_border
;
2730 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2731 return wrap_nearest_mirror_repeat
;
2732 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2733 return wrap_nearest_mirror_clamp
;
2734 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2735 return wrap_nearest_mirror_clamp_to_edge
;
2736 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2737 return wrap_nearest_mirror_clamp_to_border
;
2740 return wrap_nearest_repeat
;
2745 static wrap_linear_func
2746 get_linear_unorm_wrap(unsigned mode
)
2749 case PIPE_TEX_WRAP_CLAMP
:
2750 return wrap_linear_unorm_clamp
;
2751 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2752 return wrap_linear_unorm_clamp_to_edge
;
2753 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2754 return wrap_linear_unorm_clamp_to_border
;
2756 debug_printf("illegal wrap mode %d with non-normalized coords\n", mode
);
2757 return wrap_linear_unorm_clamp
;
2762 static wrap_linear_func
2763 get_linear_wrap(unsigned mode
)
2766 case PIPE_TEX_WRAP_REPEAT
:
2767 return wrap_linear_repeat
;
2768 case PIPE_TEX_WRAP_CLAMP
:
2769 return wrap_linear_clamp
;
2770 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
2771 return wrap_linear_clamp_to_edge
;
2772 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
2773 return wrap_linear_clamp_to_border
;
2774 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
2775 return wrap_linear_mirror_repeat
;
2776 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
2777 return wrap_linear_mirror_clamp
;
2778 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
2779 return wrap_linear_mirror_clamp_to_edge
;
2780 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
2781 return wrap_linear_mirror_clamp_to_border
;
2784 return wrap_linear_repeat
;
2790 * Is swizzling needed for the given state key?
2793 any_swizzle(const struct pipe_sampler_view
*view
)
2795 return (view
->swizzle_r
!= PIPE_SWIZZLE_RED
||
2796 view
->swizzle_g
!= PIPE_SWIZZLE_GREEN
||
2797 view
->swizzle_b
!= PIPE_SWIZZLE_BLUE
||
2798 view
->swizzle_a
!= PIPE_SWIZZLE_ALPHA
);
2802 static img_filter_func
2803 get_img_filter(const struct sp_sampler_view
*sp_sview
,
2804 const struct pipe_sampler_state
*sampler
,
2805 unsigned filter
, bool gather
)
2807 switch (sp_sview
->base
.target
) {
2809 case PIPE_TEXTURE_1D
:
2810 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2811 return img_filter_1d_nearest
;
2813 return img_filter_1d_linear
;
2815 case PIPE_TEXTURE_1D_ARRAY
:
2816 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2817 return img_filter_1d_array_nearest
;
2819 return img_filter_1d_array_linear
;
2821 case PIPE_TEXTURE_2D
:
2822 case PIPE_TEXTURE_RECT
:
2823 /* Try for fast path:
2825 if (!gather
&& sp_sview
->pot2d
&&
2826 sampler
->wrap_s
== sampler
->wrap_t
&&
2827 sampler
->normalized_coords
)
2829 switch (sampler
->wrap_s
) {
2830 case PIPE_TEX_WRAP_REPEAT
:
2832 case PIPE_TEX_FILTER_NEAREST
:
2833 return img_filter_2d_nearest_repeat_POT
;
2834 case PIPE_TEX_FILTER_LINEAR
:
2835 return img_filter_2d_linear_repeat_POT
;
2840 case PIPE_TEX_WRAP_CLAMP
:
2842 case PIPE_TEX_FILTER_NEAREST
:
2843 return img_filter_2d_nearest_clamp_POT
;
2849 /* Otherwise use default versions:
2851 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2852 return img_filter_2d_nearest
;
2854 return img_filter_2d_linear
;
2856 case PIPE_TEXTURE_2D_ARRAY
:
2857 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2858 return img_filter_2d_array_nearest
;
2860 return img_filter_2d_array_linear
;
2862 case PIPE_TEXTURE_CUBE
:
2863 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2864 return img_filter_cube_nearest
;
2866 return img_filter_cube_linear
;
2868 case PIPE_TEXTURE_CUBE_ARRAY
:
2869 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2870 return img_filter_cube_array_nearest
;
2872 return img_filter_cube_array_linear
;
2874 case PIPE_TEXTURE_3D
:
2875 if (filter
== PIPE_TEX_FILTER_NEAREST
)
2876 return img_filter_3d_nearest
;
2878 return img_filter_3d_linear
;
2882 return img_filter_1d_nearest
;
2888 sample_mip(struct sp_sampler_view
*sp_sview
,
2889 struct sp_sampler
*sp_samp
,
2890 const float s
[TGSI_QUAD_SIZE
],
2891 const float t
[TGSI_QUAD_SIZE
],
2892 const float p
[TGSI_QUAD_SIZE
],
2893 const float c0
[TGSI_QUAD_SIZE
],
2894 const float lod
[TGSI_QUAD_SIZE
],
2895 const struct filter_args
*filt_args
,
2896 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2898 mip_filter_func mip_filter
;
2899 img_filter_func min_img_filter
= NULL
;
2900 img_filter_func mag_img_filter
= NULL
;
2902 if (filt_args
->control
== tgsi_sampler_gather
) {
2903 mip_filter
= mip_filter_nearest
;
2904 min_img_filter
= get_img_filter(sp_sview
, &sp_samp
->base
, PIPE_TEX_FILTER_LINEAR
, true);
2905 } else if (sp_sview
->pot2d
& sp_samp
->min_mag_equal_repeat_linear
) {
2906 mip_filter
= mip_filter_linear_2d_linear_repeat_POT
;
2909 mip_filter
= sp_samp
->mip_filter
;
2910 min_img_filter
= get_img_filter(sp_sview
, &sp_samp
->base
, sp_samp
->min_img_filter
, false);
2911 if (sp_samp
->min_mag_equal
) {
2912 mag_img_filter
= min_img_filter
;
2915 mag_img_filter
= get_img_filter(sp_sview
, &sp_samp
->base
, sp_samp
->base
.mag_img_filter
, false);
2919 mip_filter(sp_sview
, sp_samp
, min_img_filter
, mag_img_filter
,
2920 s
, t
, p
, c0
, lod
, filt_args
, rgba
);
2922 if (sp_samp
->base
.compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
2923 sample_compare(sp_sview
, sp_samp
, s
, t
, p
, c0
, lod
, filt_args
->control
, rgba
);
2926 if (sp_sview
->need_swizzle
&& filt_args
->control
!= tgsi_sampler_gather
) {
2927 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
2928 memcpy(rgba_temp
, rgba
, sizeof(rgba_temp
));
2929 do_swizzling(&sp_sview
->base
, rgba_temp
, rgba
);
2936 * Use 3D texcoords to choose a cube face, then sample the 2D cube faces.
2937 * Put face info into the sampler faces[] array.
2940 sample_cube(struct sp_sampler_view
*sp_sview
,
2941 struct sp_sampler
*sp_samp
,
2942 const float s
[TGSI_QUAD_SIZE
],
2943 const float t
[TGSI_QUAD_SIZE
],
2944 const float p
[TGSI_QUAD_SIZE
],
2945 const float c0
[TGSI_QUAD_SIZE
],
2946 const float c1
[TGSI_QUAD_SIZE
],
2947 const struct filter_args
*filt_args
,
2948 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
2951 float ssss
[4], tttt
[4];
2953 /* Not actually used, but the intermediate steps that do the
2954 * dereferencing don't know it.
2956 static float pppp
[4] = { 0, 0, 0, 0 };
2964 direction target sc tc ma
2965 ---------- ------------------------------- --- --- ---
2966 +rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx
2967 -rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx
2968 +ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry
2969 -ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry
2970 +rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz
2971 -rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz
2974 /* Choose the cube face and compute new s/t coords for the 2D face.
2976 * Use the same cube face for all four pixels in the quad.
2978 * This isn't ideal, but if we want to use a different cube face
2979 * per pixel in the quad, we'd have to also compute the per-face
2980 * LOD here too. That's because the four post-face-selection
2981 * texcoords are no longer related to each other (they're
2982 * per-face!) so we can't use subtraction to compute the partial
2983 * deriviates to compute the LOD. Doing so (near cube edges
2984 * anyway) gives us pretty much random values.
2987 /* use the average of the four pixel's texcoords to choose the face */
2988 const float rx
= 0.25F
* (s
[0] + s
[1] + s
[2] + s
[3]);
2989 const float ry
= 0.25F
* (t
[0] + t
[1] + t
[2] + t
[3]);
2990 const float rz
= 0.25F
* (p
[0] + p
[1] + p
[2] + p
[3]);
2991 const float arx
= fabsf(rx
), ary
= fabsf(ry
), arz
= fabsf(rz
);
2993 if (arx
>= ary
&& arx
>= arz
) {
2994 float sign
= (rx
>= 0.0F
) ? 1.0F
: -1.0F
;
2995 uint face
= (rx
>= 0.0F
) ? PIPE_TEX_FACE_POS_X
: PIPE_TEX_FACE_NEG_X
;
2996 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
2997 const float ima
= -0.5F
/ fabsf(s
[j
]);
2998 ssss
[j
] = sign
* p
[j
] * ima
+ 0.5F
;
2999 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
3000 sp_sview
->faces
[j
] = face
;
3003 else if (ary
>= arx
&& ary
>= arz
) {
3004 float sign
= (ry
>= 0.0F
) ? 1.0F
: -1.0F
;
3005 uint face
= (ry
>= 0.0F
) ? PIPE_TEX_FACE_POS_Y
: PIPE_TEX_FACE_NEG_Y
;
3006 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3007 const float ima
= -0.5F
/ fabsf(t
[j
]);
3008 ssss
[j
] = -s
[j
] * ima
+ 0.5F
;
3009 tttt
[j
] = sign
* -p
[j
] * ima
+ 0.5F
;
3010 sp_sview
->faces
[j
] = face
;
3014 float sign
= (rz
>= 0.0F
) ? 1.0F
: -1.0F
;
3015 uint face
= (rz
>= 0.0F
) ? PIPE_TEX_FACE_POS_Z
: PIPE_TEX_FACE_NEG_Z
;
3016 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3017 const float ima
= -0.5F
/ fabsf(p
[j
]);
3018 ssss
[j
] = sign
* -s
[j
] * ima
+ 0.5F
;
3019 tttt
[j
] = t
[j
] * ima
+ 0.5F
;
3020 sp_sview
->faces
[j
] = face
;
3025 sample_mip(sp_sview
, sp_samp
, ssss
, tttt
, pppp
, c0
, c1
, filt_args
, rgba
);
3030 sp_get_dims(struct sp_sampler_view
*sp_sview
, int level
,
3033 const struct pipe_sampler_view
*view
= &sp_sview
->base
;
3034 const struct pipe_resource
*texture
= view
->texture
;
3036 if (view
->target
== PIPE_BUFFER
) {
3037 dims
[0] = (view
->u
.buf
.last_element
- view
->u
.buf
.first_element
) + 1;
3038 /* the other values are undefined, but let's avoid potential valgrind
3041 dims
[1] = dims
[2] = dims
[3] = 0;
3045 /* undefined according to EXT_gpu_program */
3046 level
+= view
->u
.tex
.first_level
;
3047 if (level
> view
->u
.tex
.last_level
)
3050 dims
[3] = view
->u
.tex
.last_level
- view
->u
.tex
.first_level
+ 1;
3051 dims
[0] = u_minify(texture
->width0
, level
);
3053 switch (view
->target
) {
3054 case PIPE_TEXTURE_1D_ARRAY
:
3055 dims
[1] = view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
3057 case PIPE_TEXTURE_1D
:
3059 case PIPE_TEXTURE_2D_ARRAY
:
3060 dims
[2] = view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
3062 case PIPE_TEXTURE_2D
:
3063 case PIPE_TEXTURE_CUBE
:
3064 case PIPE_TEXTURE_RECT
:
3065 dims
[1] = u_minify(texture
->height0
, level
);
3067 case PIPE_TEXTURE_3D
:
3068 dims
[1] = u_minify(texture
->height0
, level
);
3069 dims
[2] = u_minify(texture
->depth0
, level
);
3071 case PIPE_TEXTURE_CUBE_ARRAY
:
3072 dims
[1] = u_minify(texture
->height0
, level
);
3073 dims
[2] = (view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1) / 6;
3076 assert(!"unexpected texture target in sp_get_dims()");
3082 * This function is only used for getting unfiltered texels via the
3083 * TXF opcode. The GL spec says that out-of-bounds texel fetches
3084 * produce undefined results. Instead of crashing, lets just clamp
3085 * coords to the texture image size.
3088 sp_get_texels(struct sp_sampler_view
*sp_sview
,
3089 const int v_i
[TGSI_QUAD_SIZE
],
3090 const int v_j
[TGSI_QUAD_SIZE
],
3091 const int v_k
[TGSI_QUAD_SIZE
],
3092 const int lod
[TGSI_QUAD_SIZE
],
3093 const int8_t offset
[3],
3094 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3096 union tex_tile_address addr
;
3097 const struct pipe_resource
*texture
= sp_sview
->base
.texture
;
3100 int width
, height
, depth
;
3103 /* TODO write a better test for LOD */
3104 addr
.bits
.level
= sp_sview
->base
.target
== PIPE_BUFFER
? 0 :
3105 CLAMP(lod
[0] + sp_sview
->base
.u
.tex
.first_level
,
3106 sp_sview
->base
.u
.tex
.first_level
,
3107 sp_sview
->base
.u
.tex
.last_level
);
3109 width
= u_minify(texture
->width0
, addr
.bits
.level
);
3110 height
= u_minify(texture
->height0
, addr
.bits
.level
);
3111 depth
= u_minify(texture
->depth0
, addr
.bits
.level
);
3113 switch (sp_sview
->base
.target
) {
3115 case PIPE_TEXTURE_1D
:
3116 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3117 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3118 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
, 0);
3119 for (c
= 0; c
< 4; c
++) {
3124 case PIPE_TEXTURE_1D_ARRAY
:
3125 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3126 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3127 int y
= CLAMP(v_j
[j
], sp_sview
->base
.u
.tex
.first_layer
,
3128 sp_sview
->base
.u
.tex
.last_layer
);
3129 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
, y
);
3130 for (c
= 0; c
< 4; c
++) {
3135 case PIPE_TEXTURE_2D
:
3136 case PIPE_TEXTURE_RECT
:
3137 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3138 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3139 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
3140 tx
= get_texel_2d_no_border(sp_sview
, addr
, x
, y
);
3141 for (c
= 0; c
< 4; c
++) {
3146 case PIPE_TEXTURE_2D_ARRAY
:
3147 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3148 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3149 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
3150 int layer
= CLAMP(v_k
[j
], sp_sview
->base
.u
.tex
.first_layer
,
3151 sp_sview
->base
.u
.tex
.last_layer
);
3152 tx
= get_texel_3d_no_border(sp_sview
, addr
, x
, y
, layer
);
3153 for (c
= 0; c
< 4; c
++) {
3158 case PIPE_TEXTURE_3D
:
3159 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++) {
3160 int x
= CLAMP(v_i
[j
] + offset
[0], 0, width
- 1);
3161 int y
= CLAMP(v_j
[j
] + offset
[1], 0, height
- 1);
3162 int z
= CLAMP(v_k
[j
] + offset
[2], 0, depth
- 1);
3163 tx
= get_texel_3d_no_border(sp_sview
, addr
, x
, y
, z
);
3164 for (c
= 0; c
< 4; c
++) {
3169 case PIPE_TEXTURE_CUBE
: /* TXF can't work on CUBE according to spec */
3171 assert(!"Unknown or CUBE texture type in TXF processing\n");
3175 if (sp_sview
->need_swizzle
) {
3176 float rgba_temp
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
];
3177 memcpy(rgba_temp
, rgba
, sizeof(rgba_temp
));
3178 do_swizzling(&sp_sview
->base
, rgba_temp
, rgba
);
3184 softpipe_create_sampler_state(struct pipe_context
*pipe
,
3185 const struct pipe_sampler_state
*sampler
)
3187 struct sp_sampler
*samp
= CALLOC_STRUCT(sp_sampler
);
3189 samp
->base
= *sampler
;
3191 /* Note that (for instance) linear_texcoord_s and
3192 * nearest_texcoord_s may be active at the same time, if the
3193 * sampler min_img_filter differs from its mag_img_filter.
3195 if (sampler
->normalized_coords
) {
3196 samp
->linear_texcoord_s
= get_linear_wrap( sampler
->wrap_s
);
3197 samp
->linear_texcoord_t
= get_linear_wrap( sampler
->wrap_t
);
3198 samp
->linear_texcoord_p
= get_linear_wrap( sampler
->wrap_r
);
3200 samp
->nearest_texcoord_s
= get_nearest_wrap( sampler
->wrap_s
);
3201 samp
->nearest_texcoord_t
= get_nearest_wrap( sampler
->wrap_t
);
3202 samp
->nearest_texcoord_p
= get_nearest_wrap( sampler
->wrap_r
);
3205 samp
->linear_texcoord_s
= get_linear_unorm_wrap( sampler
->wrap_s
);
3206 samp
->linear_texcoord_t
= get_linear_unorm_wrap( sampler
->wrap_t
);
3207 samp
->linear_texcoord_p
= get_linear_unorm_wrap( sampler
->wrap_r
);
3209 samp
->nearest_texcoord_s
= get_nearest_unorm_wrap( sampler
->wrap_s
);
3210 samp
->nearest_texcoord_t
= get_nearest_unorm_wrap( sampler
->wrap_t
);
3211 samp
->nearest_texcoord_p
= get_nearest_unorm_wrap( sampler
->wrap_r
);
3214 samp
->min_img_filter
= sampler
->min_img_filter
;
3216 switch (sampler
->min_mip_filter
) {
3217 case PIPE_TEX_MIPFILTER_NONE
:
3218 if (sampler
->min_img_filter
== sampler
->mag_img_filter
)
3219 samp
->mip_filter
= mip_filter_none_no_filter_select
;
3221 samp
->mip_filter
= mip_filter_none
;
3224 case PIPE_TEX_MIPFILTER_NEAREST
:
3225 samp
->mip_filter
= mip_filter_nearest
;
3228 case PIPE_TEX_MIPFILTER_LINEAR
:
3229 if (sampler
->min_img_filter
== sampler
->mag_img_filter
&&
3230 sampler
->normalized_coords
&&
3231 sampler
->wrap_s
== PIPE_TEX_WRAP_REPEAT
&&
3232 sampler
->wrap_t
== PIPE_TEX_WRAP_REPEAT
&&
3233 sampler
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
3234 sampler
->max_anisotropy
<= 1) {
3235 samp
->min_mag_equal_repeat_linear
= TRUE
;
3237 samp
->mip_filter
= mip_filter_linear
;
3239 /* Anisotropic filtering extension. */
3240 if (sampler
->max_anisotropy
> 1) {
3241 samp
->mip_filter
= mip_filter_linear_aniso
;
3243 /* Override min_img_filter:
3244 * min_img_filter needs to be set to NEAREST since we need to access
3245 * each texture pixel as it is and weight it later; using linear
3246 * filters will have incorrect results.
3247 * By setting the filter to NEAREST here, we can avoid calling the
3248 * generic img_filter_2d_nearest in the anisotropic filter function,
3249 * making it possible to use one of the accelerated implementations
3251 samp
->min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
3253 /* on first access create the lookup table containing the filter weights. */
3255 create_filter_table();
3260 if (samp
->min_img_filter
== sampler
->mag_img_filter
) {
3261 samp
->min_mag_equal
= TRUE
;
3264 return (void *)samp
;
3269 softpipe_get_lambda_func(const struct pipe_sampler_view
*view
, unsigned shader
)
3271 if (shader
!= PIPE_SHADER_FRAGMENT
)
3272 return compute_lambda_vert
;
3274 switch (view
->target
) {
3276 case PIPE_TEXTURE_1D
:
3277 case PIPE_TEXTURE_1D_ARRAY
:
3278 return compute_lambda_1d
;
3279 case PIPE_TEXTURE_2D
:
3280 case PIPE_TEXTURE_2D_ARRAY
:
3281 case PIPE_TEXTURE_RECT
:
3282 case PIPE_TEXTURE_CUBE
:
3283 case PIPE_TEXTURE_CUBE_ARRAY
:
3284 return compute_lambda_2d
;
3285 case PIPE_TEXTURE_3D
:
3286 return compute_lambda_3d
;
3289 return compute_lambda_1d
;
3294 struct pipe_sampler_view
*
3295 softpipe_create_sampler_view(struct pipe_context
*pipe
,
3296 struct pipe_resource
*resource
,
3297 const struct pipe_sampler_view
*templ
)
3299 struct sp_sampler_view
*sview
= CALLOC_STRUCT(sp_sampler_view
);
3300 struct softpipe_resource
*spr
= (struct softpipe_resource
*)resource
;
3303 struct pipe_sampler_view
*view
= &sview
->base
;
3305 view
->reference
.count
= 1;
3306 view
->texture
= NULL
;
3307 pipe_resource_reference(&view
->texture
, resource
);
3308 view
->context
= pipe
;
3312 * This is possibly too lenient, but the primary reason is just
3313 * to catch state trackers which forget to initialize this, so
3314 * it only catches clearly impossible view targets.
3316 if (view
->target
!= resource
->target
) {
3317 if (view
->target
== PIPE_TEXTURE_1D
)
3318 assert(resource
->target
== PIPE_TEXTURE_1D_ARRAY
);
3319 else if (view
->target
== PIPE_TEXTURE_1D_ARRAY
)
3320 assert(resource
->target
== PIPE_TEXTURE_1D
);
3321 else if (view
->target
== PIPE_TEXTURE_2D
)
3322 assert(resource
->target
== PIPE_TEXTURE_2D_ARRAY
||
3323 resource
->target
== PIPE_TEXTURE_CUBE
||
3324 resource
->target
== PIPE_TEXTURE_CUBE_ARRAY
);
3325 else if (view
->target
== PIPE_TEXTURE_2D_ARRAY
)
3326 assert(resource
->target
== PIPE_TEXTURE_2D
||
3327 resource
->target
== PIPE_TEXTURE_CUBE
||
3328 resource
->target
== PIPE_TEXTURE_CUBE_ARRAY
);
3329 else if (view
->target
== PIPE_TEXTURE_CUBE
)
3330 assert(resource
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
3331 resource
->target
== PIPE_TEXTURE_2D_ARRAY
);
3332 else if (view
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
3333 assert(resource
->target
== PIPE_TEXTURE_CUBE
||
3334 resource
->target
== PIPE_TEXTURE_2D_ARRAY
);
3340 if (any_swizzle(view
)) {
3341 sview
->need_swizzle
= TRUE
;
3344 if (view
->target
== PIPE_TEXTURE_CUBE
||
3345 view
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
3346 sview
->get_samples
= sample_cube
;
3348 sview
->get_samples
= sample_mip
;
3350 sview
->pot2d
= spr
->pot
&&
3351 (view
->target
== PIPE_TEXTURE_2D
||
3352 view
->target
== PIPE_TEXTURE_RECT
);
3354 sview
->xpot
= util_logbase2( resource
->width0
);
3355 sview
->ypot
= util_logbase2( resource
->height0
);
3358 return (struct pipe_sampler_view
*) sview
;
3363 sp_tgsi_get_dims(struct tgsi_sampler
*tgsi_sampler
,
3364 const unsigned sview_index
,
3365 int level
, int dims
[4])
3367 struct sp_tgsi_sampler
*sp_samp
= (struct sp_tgsi_sampler
*)tgsi_sampler
;
3369 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3370 /* always have a view here but texture is NULL if no sampler view was set. */
3371 if (!sp_samp
->sp_sview
[sview_index
].base
.texture
) {
3372 dims
[0] = dims
[1] = dims
[2] = dims
[3] = 0;
3375 sp_get_dims(&sp_samp
->sp_sview
[sview_index
], level
, dims
);
3380 sp_tgsi_get_samples(struct tgsi_sampler
*tgsi_sampler
,
3381 const unsigned sview_index
,
3382 const unsigned sampler_index
,
3383 const float s
[TGSI_QUAD_SIZE
],
3384 const float t
[TGSI_QUAD_SIZE
],
3385 const float p
[TGSI_QUAD_SIZE
],
3386 const float c0
[TGSI_QUAD_SIZE
],
3387 const float lod
[TGSI_QUAD_SIZE
],
3388 float derivs
[3][2][TGSI_QUAD_SIZE
],
3389 const int8_t offset
[3],
3390 enum tgsi_sampler_control control
,
3391 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3393 struct sp_tgsi_sampler
*sp_samp
= (struct sp_tgsi_sampler
*)tgsi_sampler
;
3394 struct filter_args filt_args
;
3395 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3396 assert(sampler_index
< PIPE_MAX_SAMPLERS
);
3397 assert(sp_samp
->sp_sampler
[sampler_index
]);
3398 /* always have a view here but texture is NULL if no sampler view was set. */
3399 if (!sp_samp
->sp_sview
[sview_index
].base
.texture
) {
3401 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++) {
3402 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
3409 filt_args
.control
= control
;
3410 filt_args
.offset
= offset
;
3411 sp_samp
->sp_sview
[sview_index
].get_samples(&sp_samp
->sp_sview
[sview_index
],
3412 sp_samp
->sp_sampler
[sampler_index
],
3413 s
, t
, p
, c0
, lod
, &filt_args
, rgba
);
3418 sp_tgsi_get_texel(struct tgsi_sampler
*tgsi_sampler
,
3419 const unsigned sview_index
,
3420 const int i
[TGSI_QUAD_SIZE
],
3421 const int j
[TGSI_QUAD_SIZE
], const int k
[TGSI_QUAD_SIZE
],
3422 const int lod
[TGSI_QUAD_SIZE
], const int8_t offset
[3],
3423 float rgba
[TGSI_NUM_CHANNELS
][TGSI_QUAD_SIZE
])
3425 struct sp_tgsi_sampler
*sp_samp
= (struct sp_tgsi_sampler
*)tgsi_sampler
;
3427 assert(sview_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
3428 /* always have a view here but texture is NULL if no sampler view was set. */
3429 if (!sp_samp
->sp_sview
[sview_index
].base
.texture
) {
3431 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++) {
3432 for (i
= 0; i
< TGSI_QUAD_SIZE
; i
++) {
3438 sp_get_texels(&sp_samp
->sp_sview
[sview_index
], i
, j
, k
, lod
, offset
, rgba
);
3442 struct sp_tgsi_sampler
*
3443 sp_create_tgsi_sampler(void)
3445 struct sp_tgsi_sampler
*samp
= CALLOC_STRUCT(sp_tgsi_sampler
);
3449 samp
->base
.get_dims
= sp_tgsi_get_dims
;
3450 samp
->base
.get_samples
= sp_tgsi_get_samples
;
3451 samp
->base
.get_texel
= sp_tgsi_get_texel
;