1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Texture sampling -- SoA.
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 * @author Brian Paul <brianp@vmware.com>
36 #include "pipe/p_defines.h"
37 #include "pipe/p_state.h"
38 #include "util/u_debug.h"
39 #include "util/u_dump.h"
40 #include "util/u_memory.h"
41 #include "util/u_math.h"
42 #include "util/u_format.h"
43 #include "lp_bld_debug.h"
44 #include "lp_bld_type.h"
45 #include "lp_bld_const.h"
46 #include "lp_bld_conv.h"
47 #include "lp_bld_arit.h"
48 #include "lp_bld_bitarit.h"
49 #include "lp_bld_logic.h"
50 #include "lp_bld_printf.h"
51 #include "lp_bld_swizzle.h"
52 #include "lp_bld_flow.h"
53 #include "lp_bld_gather.h"
54 #include "lp_bld_format.h"
55 #include "lp_bld_sample.h"
56 #include "lp_bld_sample_aos.h"
57 #include "lp_bld_struct.h"
58 #include "lp_bld_quad.h"
62 * Generate code to fetch a texel from a texture at int coords (x, y, z).
63 * The computation depends on whether the texture is 1D, 2D or 3D.
64 * The result, texel, will be float vectors:
65 * texel[0] = red values
66 * texel[1] = green values
67 * texel[2] = blue values
68 * texel[3] = alpha values
71 lp_build_sample_texel_soa(struct lp_build_sample_context
*bld
,
79 LLVMValueRef y_stride
,
80 LLVMValueRef z_stride
,
81 LLVMValueRef data_ptr
,
82 LLVMValueRef texel_out
[4])
84 const struct lp_sampler_static_state
*static_state
= bld
->static_state
;
85 const int dims
= texture_dims(static_state
->target
);
86 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
89 LLVMValueRef use_border
= NULL
;
91 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
92 if (lp_sampler_wrap_mode_uses_border_color(static_state
->wrap_s
,
93 static_state
->min_img_filter
,
94 static_state
->mag_img_filter
)) {
96 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
97 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
98 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
102 lp_sampler_wrap_mode_uses_border_color(static_state
->wrap_t
,
103 static_state
->min_img_filter
,
104 static_state
->mag_img_filter
)) {
106 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
107 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
109 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
110 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
113 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
118 lp_sampler_wrap_mode_uses_border_color(static_state
->wrap_r
,
119 static_state
->min_img_filter
,
120 static_state
->mag_img_filter
)) {
122 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
123 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
125 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
126 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
129 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
133 /* convert x,y,z coords to linear offset from start of texture, in bytes */
134 lp_build_sample_offset(&bld
->uint_coord_bld
,
136 x
, y
, z
, y_stride
, z_stride
,
140 /* If we can sample the border color, it means that texcoords may
141 * lie outside the bounds of the texture image. We need to do
142 * something to prevent reading out of bounds and causing a segfault.
144 * Simply AND the texture coords with !use_border. This will cause
145 * coords which are out of bounds to become zero. Zero's guaranteed
146 * to be inside the texture image.
148 offset
= lp_build_andnot(&bld
->uint_coord_bld
, offset
, use_border
);
151 lp_build_fetch_rgba_soa(bld
->builder
,
159 * Note: if we find an app which frequently samples the texture border
160 * we might want to implement a true conditional here to avoid sampling
161 * the texture whenever possible (since that's quite a bit of code).
164 * texel = border_color;
167 * texel = sample_texture(coord);
169 * As it is now, we always sample the texture, then selectively replace
170 * the texel color results with the border color.
174 /* select texel color or border color depending on use_border */
175 LLVMValueRef border_color_ptr
=
176 bld
->dynamic_state
->border_color(bld
->dynamic_state
,
179 for (chan
= 0; chan
< 4; chan
++) {
180 LLVMValueRef border_chan
=
181 lp_build_array_get(bld
->builder
, border_color_ptr
,
182 lp_build_const_int32(chan
));
183 LLVMValueRef border_chan_vec
=
184 lp_build_broadcast_scalar(&bld
->float_vec_bld
, border_chan
);
185 texel_out
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
186 border_chan_vec
, texel_out
[chan
]);
190 apply_sampler_swizzle(bld
, texel_out
);
195 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
198 lp_build_coord_mirror(struct lp_build_sample_context
*bld
,
201 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
202 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
203 LLVMValueRef fract
, flr
, isOdd
;
205 /* fract = coord - floor(coord) */
206 fract
= lp_build_sub(coord_bld
, coord
, lp_build_floor(coord_bld
, coord
));
208 /* flr = ifloor(coord); */
209 flr
= lp_build_ifloor(coord_bld
, coord
);
211 /* isOdd = flr & 1 */
212 isOdd
= LLVMBuildAnd(bld
->builder
, flr
, int_coord_bld
->one
, "");
214 /* make coord positive or negative depending on isOdd */
215 coord
= lp_build_set_sign(coord_bld
, fract
, isOdd
);
217 /* convert isOdd to float */
218 isOdd
= lp_build_int_to_float(coord_bld
, isOdd
);
220 /* add isOdd to coord */
221 coord
= lp_build_add(coord_bld
, coord
, isOdd
);
228 * Build LLVM code for texture wrap mode for linear filtering.
229 * \param x0_out returns first integer texcoord
230 * \param x1_out returns second integer texcoord
231 * \param weight_out returns linear interpolation weight
234 lp_build_sample_wrap_linear(struct lp_build_sample_context
*bld
,
239 LLVMValueRef
*x0_out
,
240 LLVMValueRef
*x1_out
,
241 LLVMValueRef
*weight_out
)
243 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
244 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
245 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
246 LLVMValueRef half
= lp_build_const_vec(coord_bld
->type
, 0.5);
247 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
248 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
249 LLVMValueRef coord0
, coord1
, weight
;
252 case PIPE_TEX_WRAP_REPEAT
:
253 /* mul by size and subtract 0.5 */
254 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
255 coord
= lp_build_sub(coord_bld
, coord
, half
);
257 coord0
= lp_build_ifloor(coord_bld
, coord
);
258 coord1
= lp_build_add(uint_coord_bld
, coord0
, uint_coord_bld
->one
);
259 /* compute lerp weight */
260 weight
= lp_build_fract(coord_bld
, coord
);
263 coord0
= LLVMBuildAnd(bld
->builder
, coord0
, length_minus_one
, "");
264 coord1
= LLVMBuildAnd(bld
->builder
, coord1
, length_minus_one
, "");
267 /* Add a bias to the texcoord to handle negative coords */
268 LLVMValueRef bias
= lp_build_mul_imm(uint_coord_bld
, length
, 1024);
269 coord0
= LLVMBuildAdd(bld
->builder
, coord0
, bias
, "");
270 coord1
= LLVMBuildAdd(bld
->builder
, coord1
, bias
, "");
271 coord0
= LLVMBuildURem(bld
->builder
, coord0
, length
, "");
272 coord1
= LLVMBuildURem(bld
->builder
, coord1
, length
, "");
276 case PIPE_TEX_WRAP_CLAMP
:
277 if (bld
->static_state
->normalized_coords
) {
278 /* scale coord to length */
279 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
282 /* clamp to [0, length] */
283 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, length_f
);
285 coord
= lp_build_sub(coord_bld
, coord
, half
);
287 weight
= lp_build_fract(coord_bld
, coord
);
288 coord0
= lp_build_ifloor(coord_bld
, coord
);
289 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
292 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
293 if (bld
->static_state
->normalized_coords
) {
295 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, coord_bld
->one
);
296 /* mul by tex size and subtract 0.5 */
297 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
298 coord
= lp_build_sub(coord_bld
, coord
, half
);
301 LLVMValueRef min
, max
;
302 /* clamp to [0.5, length - 0.5] */
304 max
= lp_build_sub(coord_bld
, length_f
, min
);
305 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
307 /* compute lerp weight */
308 weight
= lp_build_fract(coord_bld
, coord
);
309 /* coord0 = floor(coord); */
310 coord0
= lp_build_ifloor(coord_bld
, coord
);
311 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
312 /* coord0 = max(coord0, 0) */
313 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
314 /* coord1 = min(coord1, length-1) */
315 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
318 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
320 LLVMValueRef min
, max
;
321 if (bld
->static_state
->normalized_coords
) {
322 /* scale coord to length */
323 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
325 /* clamp to [-0.5, length + 0.5] */
326 min
= lp_build_const_vec(coord_bld
->type
, -0.5F
);
327 max
= lp_build_sub(coord_bld
, length_f
, min
);
328 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
329 coord
= lp_build_sub(coord_bld
, coord
, half
);
330 /* compute lerp weight */
331 weight
= lp_build_fract(coord_bld
, coord
);
333 coord0
= lp_build_ifloor(coord_bld
, coord
);
334 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
338 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
339 /* compute mirror function */
340 coord
= lp_build_coord_mirror(bld
, coord
);
342 /* scale coord to length */
343 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
344 coord
= lp_build_sub(coord_bld
, coord
, half
);
346 /* compute lerp weight */
347 weight
= lp_build_fract(coord_bld
, coord
);
349 /* convert to int coords */
350 coord0
= lp_build_ifloor(coord_bld
, coord
);
351 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
353 /* coord0 = max(coord0, 0) */
354 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
355 /* coord1 = min(coord1, length-1) */
356 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
359 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
360 coord
= lp_build_abs(coord_bld
, coord
);
362 if (bld
->static_state
->normalized_coords
) {
363 /* scale coord to length */
364 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
367 /* clamp to [0, length] */
368 coord
= lp_build_min(coord_bld
, coord
, length_f
);
370 coord
= lp_build_sub(coord_bld
, coord
, half
);
372 weight
= lp_build_fract(coord_bld
, coord
);
373 coord0
= lp_build_ifloor(coord_bld
, coord
);
374 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
377 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
379 LLVMValueRef min
, max
;
381 coord
= lp_build_abs(coord_bld
, coord
);
383 if (bld
->static_state
->normalized_coords
) {
384 /* scale coord to length */
385 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
388 /* clamp to [0.5, length - 0.5] */
390 max
= lp_build_sub(coord_bld
, length_f
, min
);
391 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
393 coord
= lp_build_sub(coord_bld
, coord
, half
);
395 weight
= lp_build_fract(coord_bld
, coord
);
396 coord0
= lp_build_ifloor(coord_bld
, coord
);
397 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
401 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
403 LLVMValueRef min
, max
;
405 coord
= lp_build_abs(coord_bld
, coord
);
407 if (bld
->static_state
->normalized_coords
) {
408 /* scale coord to length */
409 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
412 /* clamp to [-0.5, length + 0.5] */
413 min
= lp_build_negate(coord_bld
, half
);
414 max
= lp_build_sub(coord_bld
, length_f
, min
);
415 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
417 coord
= lp_build_sub(coord_bld
, coord
, half
);
419 weight
= lp_build_fract(coord_bld
, coord
);
420 coord0
= lp_build_ifloor(coord_bld
, coord
);
421 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
434 *weight_out
= weight
;
439 * Build LLVM code for texture wrap mode for nearest filtering.
440 * \param coord the incoming texcoord (nominally in [0,1])
441 * \param length the texture size along one dimension, as int vector
442 * \param is_pot if TRUE, length is a power of two
443 * \param wrap_mode one of PIPE_TEX_WRAP_x
446 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
452 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
453 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
454 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
455 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
456 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
460 case PIPE_TEX_WRAP_REPEAT
:
461 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
462 icoord
= lp_build_ifloor(coord_bld
, coord
);
464 icoord
= LLVMBuildAnd(bld
->builder
, icoord
, length_minus_one
, "");
466 /* Add a bias to the texcoord to handle negative coords */
467 LLVMValueRef bias
= lp_build_mul_imm(uint_coord_bld
, length
, 1024);
468 icoord
= LLVMBuildAdd(bld
->builder
, icoord
, bias
, "");
469 icoord
= LLVMBuildURem(bld
->builder
, icoord
, length
, "");
473 case PIPE_TEX_WRAP_CLAMP
:
474 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
475 if (bld
->static_state
->normalized_coords
) {
476 /* scale coord to length */
477 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
481 icoord
= lp_build_ifloor(coord_bld
, coord
);
483 /* clamp to [0, length - 1]. */
484 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
488 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
489 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
491 LLVMValueRef min
, max
;
493 if (bld
->static_state
->normalized_coords
) {
494 /* scale coord to length */
495 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
498 icoord
= lp_build_ifloor(coord_bld
, coord
);
500 /* clamp to [-1, length] */
501 min
= lp_build_negate(int_coord_bld
, int_coord_bld
->one
);
503 icoord
= lp_build_clamp(int_coord_bld
, icoord
, min
, max
);
507 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
508 /* compute mirror function */
509 coord
= lp_build_coord_mirror(bld
, coord
);
511 /* scale coord to length */
512 assert(bld
->static_state
->normalized_coords
);
513 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
515 icoord
= lp_build_ifloor(coord_bld
, coord
);
517 /* clamp to [0, length - 1] */
518 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
521 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
522 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
523 coord
= lp_build_abs(coord_bld
, coord
);
525 if (bld
->static_state
->normalized_coords
) {
526 /* scale coord to length */
527 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
530 icoord
= lp_build_ifloor(coord_bld
, coord
);
532 /* clamp to [0, length - 1] */
533 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
536 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
537 coord
= lp_build_abs(coord_bld
, coord
);
539 if (bld
->static_state
->normalized_coords
) {
540 /* scale coord to length */
541 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
544 icoord
= lp_build_ifloor(coord_bld
, coord
);
546 /* clamp to [0, length] */
547 icoord
= lp_build_min(int_coord_bld
, icoord
, length
);
560 * Generate code to sample a mipmap level with nearest filtering.
561 * If sampling a cube texture, r = cube face in [0,5].
564 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
566 LLVMValueRef width_vec
,
567 LLVMValueRef height_vec
,
568 LLVMValueRef depth_vec
,
569 LLVMValueRef row_stride_vec
,
570 LLVMValueRef img_stride_vec
,
571 LLVMValueRef data_ptr
,
575 LLVMValueRef colors_out
[4])
577 const int dims
= texture_dims(bld
->static_state
->target
);
578 LLVMValueRef x
, y
, z
;
581 * Compute integer texcoords.
583 x
= lp_build_sample_wrap_nearest(bld
, s
, width_vec
,
584 bld
->static_state
->pot_width
,
585 bld
->static_state
->wrap_s
);
586 lp_build_name(x
, "tex.x.wrapped");
589 y
= lp_build_sample_wrap_nearest(bld
, t
, height_vec
,
590 bld
->static_state
->pot_height
,
591 bld
->static_state
->wrap_t
);
592 lp_build_name(y
, "tex.y.wrapped");
595 z
= lp_build_sample_wrap_nearest(bld
, r
, depth_vec
,
596 bld
->static_state
->pot_depth
,
597 bld
->static_state
->wrap_r
);
598 lp_build_name(z
, "tex.z.wrapped");
600 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
612 * Get texture colors.
614 lp_build_sample_texel_soa(bld
, unit
,
615 width_vec
, height_vec
, depth_vec
,
617 row_stride_vec
, img_stride_vec
,
618 data_ptr
, colors_out
);
623 * Generate code to sample a mipmap level with linear filtering.
624 * If sampling a cube texture, r = cube face in [0,5].
627 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
629 LLVMValueRef width_vec
,
630 LLVMValueRef height_vec
,
631 LLVMValueRef depth_vec
,
632 LLVMValueRef row_stride_vec
,
633 LLVMValueRef img_stride_vec
,
634 LLVMValueRef data_ptr
,
638 LLVMValueRef colors_out
[4])
640 const int dims
= texture_dims(bld
->static_state
->target
);
641 LLVMValueRef x0
, y0
, z0
, x1
, y1
, z1
;
642 LLVMValueRef s_fpart
, t_fpart
, r_fpart
;
643 LLVMValueRef neighbors
[2][2][4];
647 * Compute integer texcoords.
649 lp_build_sample_wrap_linear(bld
, s
, width_vec
,
650 bld
->static_state
->pot_width
,
651 bld
->static_state
->wrap_s
,
653 lp_build_name(x0
, "tex.x0.wrapped");
654 lp_build_name(x1
, "tex.x1.wrapped");
657 lp_build_sample_wrap_linear(bld
, t
, height_vec
,
658 bld
->static_state
->pot_height
,
659 bld
->static_state
->wrap_t
,
661 lp_build_name(y0
, "tex.y0.wrapped");
662 lp_build_name(y1
, "tex.y1.wrapped");
665 lp_build_sample_wrap_linear(bld
, r
, depth_vec
,
666 bld
->static_state
->pot_depth
,
667 bld
->static_state
->wrap_r
,
669 lp_build_name(z0
, "tex.z0.wrapped");
670 lp_build_name(z1
, "tex.z1.wrapped");
672 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
673 z0
= z1
= r
; /* cube face */
682 y0
= y1
= t_fpart
= NULL
;
683 z0
= z1
= r_fpart
= NULL
;
687 * Get texture colors.
689 /* get x0/x1 texels */
690 lp_build_sample_texel_soa(bld
, unit
,
691 width_vec
, height_vec
, depth_vec
,
693 row_stride_vec
, img_stride_vec
,
694 data_ptr
, neighbors
[0][0]);
695 lp_build_sample_texel_soa(bld
, unit
,
696 width_vec
, height_vec
, depth_vec
,
698 row_stride_vec
, img_stride_vec
,
699 data_ptr
, neighbors
[0][1]);
702 /* Interpolate two samples from 1D image to produce one color */
703 for (chan
= 0; chan
< 4; chan
++) {
704 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, s_fpart
,
705 neighbors
[0][0][chan
],
706 neighbors
[0][1][chan
]);
711 LLVMValueRef colors0
[4];
713 /* get x0/x1 texels at y1 */
714 lp_build_sample_texel_soa(bld
, unit
,
715 width_vec
, height_vec
, depth_vec
,
717 row_stride_vec
, img_stride_vec
,
718 data_ptr
, neighbors
[1][0]);
719 lp_build_sample_texel_soa(bld
, unit
,
720 width_vec
, height_vec
, depth_vec
,
722 row_stride_vec
, img_stride_vec
,
723 data_ptr
, neighbors
[1][1]);
725 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
726 for (chan
= 0; chan
< 4; chan
++) {
727 colors0
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
729 neighbors
[0][0][chan
],
730 neighbors
[0][1][chan
],
731 neighbors
[1][0][chan
],
732 neighbors
[1][1][chan
]);
736 LLVMValueRef neighbors1
[2][2][4];
737 LLVMValueRef colors1
[4];
739 /* get x0/x1/y0/y1 texels at z1 */
740 lp_build_sample_texel_soa(bld
, unit
,
741 width_vec
, height_vec
, depth_vec
,
743 row_stride_vec
, img_stride_vec
,
744 data_ptr
, neighbors1
[0][0]);
745 lp_build_sample_texel_soa(bld
, unit
,
746 width_vec
, height_vec
, depth_vec
,
748 row_stride_vec
, img_stride_vec
,
749 data_ptr
, neighbors1
[0][1]);
750 lp_build_sample_texel_soa(bld
, unit
,
751 width_vec
, height_vec
, depth_vec
,
753 row_stride_vec
, img_stride_vec
,
754 data_ptr
, neighbors1
[1][0]);
755 lp_build_sample_texel_soa(bld
, unit
,
756 width_vec
, height_vec
, depth_vec
,
758 row_stride_vec
, img_stride_vec
,
759 data_ptr
, neighbors1
[1][1]);
761 /* Bilinear interpolate the four samples from the second Z slice */
762 for (chan
= 0; chan
< 4; chan
++) {
763 colors1
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
765 neighbors1
[0][0][chan
],
766 neighbors1
[0][1][chan
],
767 neighbors1
[1][0][chan
],
768 neighbors1
[1][1][chan
]);
771 /* Linearly interpolate the two samples from the two 3D slices */
772 for (chan
= 0; chan
< 4; chan
++) {
773 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
,
775 colors0
[chan
], colors1
[chan
]);
780 for (chan
= 0; chan
< 4; chan
++) {
781 colors_out
[chan
] = colors0
[chan
];
789 * Sample the texture/mipmap using given image filter and mip filter.
790 * data0_ptr and data1_ptr point to the two mipmap levels to sample
791 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
792 * If we're using nearest miplevel sampling the '1' values will be null/unused.
795 lp_build_sample_mipmap(struct lp_build_sample_context
*bld
,
802 LLVMValueRef lod_fpart
,
803 LLVMValueRef width0_vec
,
804 LLVMValueRef width1_vec
,
805 LLVMValueRef height0_vec
,
806 LLVMValueRef height1_vec
,
807 LLVMValueRef depth0_vec
,
808 LLVMValueRef depth1_vec
,
809 LLVMValueRef row_stride0_vec
,
810 LLVMValueRef row_stride1_vec
,
811 LLVMValueRef img_stride0_vec
,
812 LLVMValueRef img_stride1_vec
,
813 LLVMValueRef data_ptr0
,
814 LLVMValueRef data_ptr1
,
815 LLVMValueRef
*colors_out
)
817 LLVMValueRef colors0
[4], colors1
[4];
820 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
821 /* sample the first mipmap level */
822 lp_build_sample_image_nearest(bld
, unit
,
823 width0_vec
, height0_vec
, depth0_vec
,
824 row_stride0_vec
, img_stride0_vec
,
825 data_ptr0
, s
, t
, r
, colors0
);
827 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
828 /* sample the second mipmap level */
829 lp_build_sample_image_nearest(bld
, unit
,
830 width1_vec
, height1_vec
, depth1_vec
,
831 row_stride1_vec
, img_stride1_vec
,
832 data_ptr1
, s
, t
, r
, colors1
);
836 assert(img_filter
== PIPE_TEX_FILTER_LINEAR
);
838 /* sample the first mipmap level */
839 lp_build_sample_image_linear(bld
, unit
,
840 width0_vec
, height0_vec
, depth0_vec
,
841 row_stride0_vec
, img_stride0_vec
,
842 data_ptr0
, s
, t
, r
, colors0
);
844 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
845 /* sample the second mipmap level */
846 lp_build_sample_image_linear(bld
, unit
,
847 width1_vec
, height1_vec
, depth1_vec
,
848 row_stride1_vec
, img_stride1_vec
,
849 data_ptr1
, s
, t
, r
, colors1
);
853 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
854 /* interpolate samples from the two mipmap levels */
855 for (chan
= 0; chan
< 4; chan
++) {
856 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
857 colors0
[chan
], colors1
[chan
]);
861 /* use first/only level's colors */
862 for (chan
= 0; chan
< 4; chan
++) {
863 colors_out
[chan
] = colors0
[chan
];
871 * General texture sampling codegen.
872 * This function handles texture sampling for all texture targets (1D,
873 * 2D, 3D, cube) and all filtering modes.
876 lp_build_sample_general(struct lp_build_sample_context
*bld
,
881 const LLVMValueRef
*ddx
,
882 const LLVMValueRef
*ddy
,
883 LLVMValueRef lod_bias
, /* optional */
884 LLVMValueRef explicit_lod
, /* optional */
888 LLVMValueRef width_vec
,
889 LLVMValueRef height_vec
,
890 LLVMValueRef depth_vec
,
891 LLVMValueRef row_stride_array
,
892 LLVMValueRef img_stride_array
,
893 LLVMValueRef data_array
,
894 LLVMValueRef
*colors_out
)
896 struct lp_build_context
*float_bld
= &bld
->float_bld
;
897 const unsigned mip_filter
= bld
->static_state
->min_mip_filter
;
898 const unsigned min_filter
= bld
->static_state
->min_img_filter
;
899 const unsigned mag_filter
= bld
->static_state
->mag_img_filter
;
900 const int dims
= texture_dims(bld
->static_state
->target
);
901 LLVMValueRef lod
= NULL
, lod_fpart
= NULL
;
902 LLVMValueRef ilevel0
, ilevel1
= NULL
;
903 LLVMValueRef width0_vec
= NULL
, height0_vec
= NULL
, depth0_vec
= NULL
;
904 LLVMValueRef width1_vec
= NULL
, height1_vec
= NULL
, depth1_vec
= NULL
;
905 LLVMValueRef row_stride0_vec
= NULL
, row_stride1_vec
= NULL
;
906 LLVMValueRef img_stride0_vec
= NULL
, img_stride1_vec
= NULL
;
907 LLVMValueRef data_ptr0
, data_ptr1
= NULL
;
908 LLVMValueRef face_ddx
[4], face_ddy
[4];
911 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
912 mip_filter, min_filter, mag_filter);
916 * Choose cube face, recompute texcoords and derivatives for the chosen face.
918 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
919 LLVMValueRef face
, face_s
, face_t
;
920 lp_build_cube_lookup(bld
, s
, t
, r
, &face
, &face_s
, &face_t
);
921 s
= face_s
; /* vec */
922 t
= face_t
; /* vec */
923 /* use 'r' to indicate cube face */
924 r
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, face
); /* vec */
926 /* recompute ddx, ddy using the new (s,t) face texcoords */
927 face_ddx
[0] = lp_build_ddx(&bld
->coord_bld
, s
);
928 face_ddx
[1] = lp_build_ddx(&bld
->coord_bld
, t
);
931 face_ddy
[0] = lp_build_ddy(&bld
->coord_bld
, s
);
932 face_ddy
[1] = lp_build_ddy(&bld
->coord_bld
, t
);
940 * Compute the level of detail (float).
942 if (min_filter
!= mag_filter
||
943 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
944 /* Need to compute lod either to choose mipmap levels or to
945 * distinguish between minification/magnification with one mipmap level.
947 lod
= lp_build_lod_selector(bld
, unit
, ddx
, ddy
,
948 lod_bias
, explicit_lod
,
949 width
, height
, depth
);
953 * Compute integer mipmap level(s) to fetch texels from.
955 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
956 /* always use mip level 0 */
957 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
958 /* XXX this is a work-around for an apparent bug in LLVM 2.7.
959 * We should be able to set ilevel0 = const(0) but that causes
960 * bad x86 code to be emitted.
962 lod
= lp_build_const_elem(bld
->coord_bld
.type
, 0.0);
963 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
966 ilevel0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
971 if (mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
972 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
975 assert(mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
);
976 lp_build_linear_mip_levels(bld
, unit
, lod
, &ilevel0
, &ilevel1
,
978 lod_fpart
= lp_build_broadcast_scalar(&bld
->coord_bld
, lod_fpart
);
982 /* compute image size(s) of source mipmap level(s) */
983 lp_build_mipmap_level_sizes(bld
, dims
, width_vec
, height_vec
, depth_vec
,
985 row_stride_array
, img_stride_array
,
986 &width0_vec
, &width1_vec
,
987 &height0_vec
, &height1_vec
,
988 &depth0_vec
, &depth1_vec
,
989 &row_stride0_vec
, &row_stride1_vec
,
990 &img_stride0_vec
, &img_stride1_vec
);
993 * Get pointer(s) to image data for mipmap level(s).
995 data_ptr0
= lp_build_get_mipmap_level(bld
, data_array
, ilevel0
);
996 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
997 data_ptr1
= lp_build_get_mipmap_level(bld
, data_array
, ilevel1
);
1001 * Get/interpolate texture colors.
1003 if (min_filter
== mag_filter
) {
1004 /* no need to distinquish between minification and magnification */
1005 lp_build_sample_mipmap(bld
, unit
,
1006 min_filter
, mip_filter
, s
, t
, r
, lod_fpart
,
1007 width0_vec
, width1_vec
,
1008 height0_vec
, height1_vec
,
1009 depth0_vec
, depth1_vec
,
1010 row_stride0_vec
, row_stride1_vec
,
1011 img_stride0_vec
, img_stride1_vec
,
1012 data_ptr0
, data_ptr1
,
1016 /* Emit conditional to choose min image filter or mag image filter
1017 * depending on the lod being >0 or <= 0, respectively.
1019 struct lp_build_flow_context
*flow_ctx
;
1020 struct lp_build_if_state if_ctx
;
1021 LLVMValueRef minify
;
1023 flow_ctx
= lp_build_flow_create(bld
->builder
);
1024 lp_build_flow_scope_begin(flow_ctx
);
1026 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[0]);
1027 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[1]);
1028 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[2]);
1029 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[3]);
1031 /* minify = lod > 0.0 */
1032 minify
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
1033 lod
, float_bld
->zero
, "");
1035 lp_build_if(&if_ctx
, flow_ctx
, bld
->builder
, minify
);
1037 /* Use the minification filter */
1038 lp_build_sample_mipmap(bld
, unit
,
1039 min_filter
, mip_filter
,
1041 width0_vec
, width1_vec
,
1042 height0_vec
, height1_vec
,
1043 depth0_vec
, depth1_vec
,
1044 row_stride0_vec
, row_stride1_vec
,
1045 img_stride0_vec
, img_stride1_vec
,
1046 data_ptr0
, data_ptr1
,
1049 lp_build_else(&if_ctx
);
1051 /* Use the magnification filter */
1052 lp_build_sample_mipmap(bld
, unit
,
1053 mag_filter
, mip_filter
,
1055 width0_vec
, width1_vec
,
1056 height0_vec
, height1_vec
,
1057 depth0_vec
, depth1_vec
,
1058 row_stride0_vec
, row_stride1_vec
,
1059 img_stride0_vec
, img_stride1_vec
,
1060 data_ptr0
, data_ptr1
,
1063 lp_build_endif(&if_ctx
);
1065 lp_build_flow_scope_end(flow_ctx
);
1066 lp_build_flow_destroy(flow_ctx
);
1072 * Do shadow test/comparison.
1073 * \param p the texcoord Z (aka R, aka P) component
1074 * \param texel the texel to compare against (use the X channel)
1077 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
1079 LLVMValueRef texel
[4])
1081 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1083 const unsigned chan
= 0;
1085 if (bld
->static_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
1090 LLVMValueRef indx
= lp_build_const_int32(0);
1091 LLVMValueRef coord
= LLVMBuildExtractElement(bld
->builder
, p
, indx
, "");
1092 LLVMValueRef tex
= LLVMBuildExtractElement(bld
->builder
,
1093 texel
[chan
], indx
, "");
1094 lp_build_printf(bld
->builder
, "shadow compare coord %f to texture %f\n",
1098 /* result = (p FUNC texel) ? 1 : 0 */
1099 res
= lp_build_cmp(texel_bld
, bld
->static_state
->compare_func
,
1101 res
= lp_build_select(texel_bld
, res
, texel_bld
->one
, texel_bld
->zero
);
1103 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1107 texel
[3] = texel_bld
->one
;
1112 * Just set texels to white instead of actually sampling the texture.
1116 lp_build_sample_nop(struct lp_type type
,
1117 LLVMValueRef texel_out
[4])
1119 LLVMValueRef one
= lp_build_one(type
);
1122 for (chan
= 0; chan
< 4; chan
++) {
1123 texel_out
[chan
] = one
;
1129 * Build texture sampling code.
1130 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1132 * \param type vector float type to use for coords, etc.
1133 * \param ddx partial derivatives of (s,t,r,q) with respect to x
1134 * \param ddy partial derivatives of (s,t,r,q) with respect to y
1137 lp_build_sample_soa(LLVMBuilderRef builder
,
1138 const struct lp_sampler_static_state
*static_state
,
1139 struct lp_sampler_dynamic_state
*dynamic_state
,
1140 struct lp_type type
,
1142 unsigned num_coords
,
1143 const LLVMValueRef
*coords
,
1144 const LLVMValueRef ddx
[4],
1145 const LLVMValueRef ddy
[4],
1146 LLVMValueRef lod_bias
, /* optional */
1147 LLVMValueRef explicit_lod
, /* optional */
1148 LLVMValueRef texel_out
[4])
1150 struct lp_build_sample_context bld
;
1151 LLVMValueRef width
, width_vec
;
1152 LLVMValueRef height
, height_vec
;
1153 LLVMValueRef depth
, depth_vec
;
1154 LLVMValueRef row_stride_array
, img_stride_array
;
1155 LLVMValueRef data_array
;
1159 struct lp_type float_vec_type
;
1162 enum pipe_format fmt
= static_state
->format
;
1163 debug_printf("Sample from %s\n", util_format_name(fmt
));
1166 assert(type
.floating
);
1168 /* Setup our build context */
1169 memset(&bld
, 0, sizeof bld
);
1170 bld
.builder
= builder
;
1171 bld
.static_state
= static_state
;
1172 bld
.dynamic_state
= dynamic_state
;
1173 bld
.format_desc
= util_format_description(static_state
->format
);
1175 bld
.float_type
= lp_type_float(32);
1176 bld
.int_type
= lp_type_int(32);
1177 bld
.coord_type
= type
;
1178 bld
.uint_coord_type
= lp_uint_type(type
);
1179 bld
.int_coord_type
= lp_int_type(type
);
1180 bld
.texel_type
= type
;
1182 float_vec_type
= lp_type_float_vec(32);
1184 lp_build_context_init(&bld
.float_bld
, builder
, bld
.float_type
);
1185 lp_build_context_init(&bld
.float_vec_bld
, builder
, float_vec_type
);
1186 lp_build_context_init(&bld
.int_bld
, builder
, bld
.int_type
);
1187 lp_build_context_init(&bld
.coord_bld
, builder
, bld
.coord_type
);
1188 lp_build_context_init(&bld
.uint_coord_bld
, builder
, bld
.uint_coord_type
);
1189 lp_build_context_init(&bld
.int_coord_bld
, builder
, bld
.int_coord_type
);
1190 lp_build_context_init(&bld
.texel_bld
, builder
, bld
.texel_type
);
1192 /* Get the dynamic state */
1193 width
= dynamic_state
->width(dynamic_state
, builder
, unit
);
1194 height
= dynamic_state
->height(dynamic_state
, builder
, unit
);
1195 depth
= dynamic_state
->depth(dynamic_state
, builder
, unit
);
1196 row_stride_array
= dynamic_state
->row_stride(dynamic_state
, builder
, unit
);
1197 img_stride_array
= dynamic_state
->img_stride(dynamic_state
, builder
, unit
);
1198 data_array
= dynamic_state
->data_ptr(dynamic_state
, builder
, unit
);
1199 /* Note that data_array is an array[level] of pointers to texture images */
1205 /* width, height, depth as uint vectors */
1206 width_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, width
);
1207 height_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, height
);
1208 depth_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, depth
);
1211 /* For debug: no-op texture sampling */
1212 lp_build_sample_nop(bld
.texel_type
, texel_out
);
1214 else if (util_format_fits_8unorm(bld
.format_desc
) &&
1215 lp_is_simple_wrap_mode(static_state
->wrap_s
) &&
1216 lp_is_simple_wrap_mode(static_state
->wrap_t
)) {
1217 /* do sampling/filtering with fixed pt arithmetic */
1218 lp_build_sample_aos(&bld
, unit
, s
, t
, r
, ddx
, ddy
,
1219 lod_bias
, explicit_lod
,
1220 width
, height
, depth
,
1221 width_vec
, height_vec
, depth_vec
,
1222 row_stride_array
, img_stride_array
,
1223 data_array
, texel_out
);
1227 if ((gallivm_debug
& GALLIVM_DEBUG_PERF
) &&
1228 util_format_fits_8unorm(bld
.format_desc
)) {
1229 debug_printf("%s: using floating point linear filtering for %s\n",
1230 __FUNCTION__
, bld
.format_desc
->short_name
);
1231 debug_printf(" min_img %d mag_img %d mip %d wraps %d wrapt %d\n",
1232 static_state
->min_img_filter
,
1233 static_state
->mag_img_filter
,
1234 static_state
->min_mip_filter
,
1235 static_state
->wrap_s
,
1236 static_state
->wrap_t
);
1239 lp_build_sample_general(&bld
, unit
, s
, t
, r
, ddx
, ddy
,
1240 lod_bias
, explicit_lod
,
1241 width
, height
, depth
,
1242 width_vec
, height_vec
, depth_vec
,
1243 row_stride_array
, img_stride_array
,
1248 lp_build_sample_compare(&bld
, r
, texel_out
);