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_quad.h"
61 * Does the given texture wrap mode allow sampling the texture border color?
62 * XXX maybe move this into gallium util code.
65 wrap_mode_uses_border_color(unsigned mode
)
68 case PIPE_TEX_WRAP_REPEAT
:
69 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
70 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
71 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
73 case PIPE_TEX_WRAP_CLAMP
:
74 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
75 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
76 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
79 assert(0 && "unexpected wrap mode");
86 * Generate code to fetch a texel from a texture at int coords (x, y, z).
87 * The computation depends on whether the texture is 1D, 2D or 3D.
88 * The result, texel, will be float vectors:
89 * texel[0] = red values
90 * texel[1] = green values
91 * texel[2] = blue values
92 * texel[3] = alpha values
95 lp_build_sample_texel_soa(struct lp_build_sample_context
*bld
,
102 LLVMValueRef y_stride
,
103 LLVMValueRef z_stride
,
104 LLVMValueRef data_ptr
,
105 LLVMValueRef texel_out
[4])
107 const int dims
= texture_dims(bld
->static_state
->target
);
108 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
111 LLVMValueRef use_border
= NULL
;
113 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
114 if (wrap_mode_uses_border_color(bld
->static_state
->wrap_s
)) {
116 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
117 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
118 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
121 if (dims
>= 2 && wrap_mode_uses_border_color(bld
->static_state
->wrap_t
)) {
123 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
124 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
126 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
127 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
130 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
134 if (dims
== 3 && wrap_mode_uses_border_color(bld
->static_state
->wrap_r
)) {
136 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
137 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
139 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
140 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
143 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
147 /* convert x,y,z coords to linear offset from start of texture, in bytes */
148 lp_build_sample_offset(&bld
->uint_coord_bld
,
150 x
, y
, z
, y_stride
, z_stride
,
154 /* If we can sample the border color, it means that texcoords may
155 * lie outside the bounds of the texture image. We need to do
156 * something to prevent reading out of bounds and causing a segfault.
158 * Simply AND the texture coords with !use_border. This will cause
159 * coords which are out of bounds to become zero. Zero's guaranteed
160 * to be inside the texture image.
162 offset
= lp_build_andnot(&bld
->uint_coord_bld
, offset
, use_border
);
165 lp_build_fetch_rgba_soa(bld
->builder
,
172 apply_sampler_swizzle(bld
, texel_out
);
175 * Note: if we find an app which frequently samples the texture border
176 * we might want to implement a true conditional here to avoid sampling
177 * the texture whenever possible (since that's quite a bit of code).
180 * texel = border_color;
183 * texel = sample_texture(coord);
185 * As it is now, we always sample the texture, then selectively replace
186 * the texel color results with the border color.
190 /* select texel color or border color depending on use_border */
192 for (chan
= 0; chan
< 4; chan
++) {
193 LLVMValueRef border_chan
=
194 lp_build_const_vec(bld
->texel_type
,
195 bld
->static_state
->border_color
[chan
]);
196 texel_out
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
197 border_chan
, texel_out
[chan
]);
204 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
207 lp_build_coord_mirror(struct lp_build_sample_context
*bld
,
210 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
211 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
212 LLVMValueRef fract
, flr
, isOdd
;
214 /* fract = coord - floor(coord) */
215 fract
= lp_build_sub(coord_bld
, coord
, lp_build_floor(coord_bld
, coord
));
217 /* flr = ifloor(coord); */
218 flr
= lp_build_ifloor(coord_bld
, coord
);
220 /* isOdd = flr & 1 */
221 isOdd
= LLVMBuildAnd(bld
->builder
, flr
, int_coord_bld
->one
, "");
223 /* make coord positive or negative depending on isOdd */
224 coord
= lp_build_set_sign(coord_bld
, fract
, isOdd
);
226 /* convert isOdd to float */
227 isOdd
= lp_build_int_to_float(coord_bld
, isOdd
);
229 /* add isOdd to coord */
230 coord
= lp_build_add(coord_bld
, coord
, isOdd
);
237 * Build LLVM code for texture wrap mode for linear filtering.
238 * \param x0_out returns first integer texcoord
239 * \param x1_out returns second integer texcoord
240 * \param weight_out returns linear interpolation weight
243 lp_build_sample_wrap_linear(struct lp_build_sample_context
*bld
,
248 LLVMValueRef
*x0_out
,
249 LLVMValueRef
*x1_out
,
250 LLVMValueRef
*weight_out
)
252 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
253 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
254 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
255 LLVMValueRef half
= lp_build_const_vec(coord_bld
->type
, 0.5);
256 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
257 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
258 LLVMValueRef coord0
, coord1
, weight
;
261 case PIPE_TEX_WRAP_REPEAT
:
262 /* mul by size and subtract 0.5 */
263 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
264 coord
= lp_build_sub(coord_bld
, coord
, half
);
266 coord0
= lp_build_ifloor(coord_bld
, coord
);
267 coord1
= lp_build_add(uint_coord_bld
, coord0
, uint_coord_bld
->one
);
268 /* compute lerp weight */
269 weight
= lp_build_fract(coord_bld
, coord
);
272 coord0
= LLVMBuildAnd(bld
->builder
, coord0
, length_minus_one
, "");
273 coord1
= LLVMBuildAnd(bld
->builder
, coord1
, length_minus_one
, "");
276 /* Signed remainder won't give the right results for negative
277 * dividends but unsigned remainder does.*/
278 coord0
= LLVMBuildURem(bld
->builder
, coord0
, length
, "");
279 coord1
= LLVMBuildURem(bld
->builder
, coord1
, length
, "");
283 case PIPE_TEX_WRAP_CLAMP
:
284 if (bld
->static_state
->normalized_coords
) {
285 /* scale coord to length */
286 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
289 /* clamp to [0, length] */
290 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, length_f
);
292 coord
= lp_build_sub(coord_bld
, coord
, half
);
294 weight
= lp_build_fract(coord_bld
, coord
);
295 coord0
= lp_build_ifloor(coord_bld
, coord
);
296 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
299 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
300 if (bld
->static_state
->normalized_coords
) {
302 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, coord_bld
->one
);
303 /* mul by tex size and subtract 0.5 */
304 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
305 coord
= lp_build_sub(coord_bld
, coord
, half
);
308 LLVMValueRef min
, max
;
309 /* clamp to [0.5, length - 0.5] */
311 max
= lp_build_sub(coord_bld
, length_f
, min
);
312 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
314 /* compute lerp weight */
315 weight
= lp_build_fract(coord_bld
, coord
);
316 /* coord0 = floor(coord); */
317 coord0
= lp_build_ifloor(coord_bld
, coord
);
318 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
319 /* coord0 = max(coord0, 0) */
320 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
321 /* coord1 = min(coord1, length-1) */
322 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
325 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
327 LLVMValueRef min
, max
;
328 if (bld
->static_state
->normalized_coords
) {
329 /* scale coord to length */
330 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
332 /* clamp to [-0.5, length + 0.5] */
333 min
= lp_build_const_vec(coord_bld
->type
, -0.5F
);
334 max
= lp_build_sub(coord_bld
, length_f
, min
);
335 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
336 coord
= lp_build_sub(coord_bld
, coord
, half
);
337 /* compute lerp weight */
338 weight
= lp_build_fract(coord_bld
, coord
);
340 coord0
= lp_build_ifloor(coord_bld
, coord
);
341 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
345 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
346 /* compute mirror function */
347 coord
= lp_build_coord_mirror(bld
, coord
);
349 /* scale coord to length */
350 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
351 coord
= lp_build_sub(coord_bld
, coord
, half
);
353 /* compute lerp weight */
354 weight
= lp_build_fract(coord_bld
, coord
);
356 /* convert to int coords */
357 coord0
= lp_build_ifloor(coord_bld
, coord
);
358 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
360 /* coord0 = max(coord0, 0) */
361 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
362 /* coord1 = min(coord1, length-1) */
363 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
366 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
367 coord
= lp_build_abs(coord_bld
, coord
);
369 if (bld
->static_state
->normalized_coords
) {
370 /* scale coord to length */
371 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
374 /* clamp to [0, length] */
375 coord
= lp_build_min(coord_bld
, coord
, length_f
);
377 coord
= lp_build_sub(coord_bld
, coord
, half
);
379 weight
= lp_build_fract(coord_bld
, coord
);
380 coord0
= lp_build_ifloor(coord_bld
, coord
);
381 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
384 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
386 LLVMValueRef min
, max
;
388 coord
= lp_build_abs(coord_bld
, coord
);
390 if (bld
->static_state
->normalized_coords
) {
391 /* scale coord to length */
392 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
395 /* clamp to [0.5, length - 0.5] */
397 max
= lp_build_sub(coord_bld
, length_f
, min
);
398 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
400 coord
= lp_build_sub(coord_bld
, coord
, half
);
402 weight
= lp_build_fract(coord_bld
, coord
);
403 coord0
= lp_build_ifloor(coord_bld
, coord
);
404 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
408 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
410 LLVMValueRef min
, max
;
412 coord
= lp_build_abs(coord_bld
, coord
);
414 if (bld
->static_state
->normalized_coords
) {
415 /* scale coord to length */
416 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
419 /* clamp to [-0.5, length + 0.5] */
420 min
= lp_build_negate(coord_bld
, half
);
421 max
= lp_build_sub(coord_bld
, length_f
, min
);
422 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
424 coord
= lp_build_sub(coord_bld
, coord
, half
);
426 weight
= lp_build_fract(coord_bld
, coord
);
427 coord0
= lp_build_ifloor(coord_bld
, coord
);
428 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
441 *weight_out
= weight
;
446 * Build LLVM code for texture wrap mode for nearest filtering.
447 * \param coord the incoming texcoord (nominally in [0,1])
448 * \param length the texture size along one dimension, as int vector
449 * \param is_pot if TRUE, length is a power of two
450 * \param wrap_mode one of PIPE_TEX_WRAP_x
453 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
459 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
460 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
461 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
462 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
463 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
467 case PIPE_TEX_WRAP_REPEAT
:
468 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
469 icoord
= lp_build_ifloor(coord_bld
, coord
);
471 icoord
= LLVMBuildAnd(bld
->builder
, icoord
, length_minus_one
, "");
473 /* Signed remainder won't give the right results for negative
474 * dividends but unsigned remainder does.*/
475 icoord
= LLVMBuildURem(bld
->builder
, icoord
, length
, "");
478 case PIPE_TEX_WRAP_CLAMP
:
479 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
480 if (bld
->static_state
->normalized_coords
) {
481 /* scale coord to length */
482 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
486 icoord
= lp_build_ifloor(coord_bld
, coord
);
488 /* clamp to [0, length - 1]. */
489 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
493 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
494 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
496 LLVMValueRef min
, max
;
498 if (bld
->static_state
->normalized_coords
) {
499 /* scale coord to length */
500 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
503 icoord
= lp_build_ifloor(coord_bld
, coord
);
505 /* clamp to [-1, length] */
506 min
= lp_build_negate(int_coord_bld
, int_coord_bld
->one
);
508 icoord
= lp_build_clamp(int_coord_bld
, icoord
, min
, max
);
512 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
513 /* compute mirror function */
514 coord
= lp_build_coord_mirror(bld
, coord
);
516 /* scale coord to length */
517 assert(bld
->static_state
->normalized_coords
);
518 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
520 icoord
= lp_build_ifloor(coord_bld
, coord
);
522 /* clamp to [0, length - 1] */
523 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
526 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
527 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
528 coord
= lp_build_abs(coord_bld
, coord
);
530 if (bld
->static_state
->normalized_coords
) {
531 /* scale coord to length */
532 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
535 icoord
= lp_build_ifloor(coord_bld
, coord
);
537 /* clamp to [0, length - 1] */
538 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
541 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
542 coord
= lp_build_abs(coord_bld
, coord
);
544 if (bld
->static_state
->normalized_coords
) {
545 /* scale coord to length */
546 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
549 icoord
= lp_build_ifloor(coord_bld
, coord
);
551 /* clamp to [0, length] */
552 icoord
= lp_build_min(int_coord_bld
, icoord
, length
);
565 * Generate code to sample a mipmap level with nearest filtering.
566 * If sampling a cube texture, r = cube face in [0,5].
569 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
570 LLVMValueRef width_vec
,
571 LLVMValueRef height_vec
,
572 LLVMValueRef depth_vec
,
573 LLVMValueRef row_stride_vec
,
574 LLVMValueRef img_stride_vec
,
575 LLVMValueRef data_ptr
,
579 LLVMValueRef colors_out
[4])
581 const int dims
= texture_dims(bld
->static_state
->target
);
582 LLVMValueRef x
, y
, z
;
585 * Compute integer texcoords.
587 x
= lp_build_sample_wrap_nearest(bld
, s
, width_vec
,
588 bld
->static_state
->pot_width
,
589 bld
->static_state
->wrap_s
);
590 lp_build_name(x
, "tex.x.wrapped");
593 y
= lp_build_sample_wrap_nearest(bld
, t
, height_vec
,
594 bld
->static_state
->pot_height
,
595 bld
->static_state
->wrap_t
);
596 lp_build_name(y
, "tex.y.wrapped");
599 z
= lp_build_sample_wrap_nearest(bld
, r
, depth_vec
,
600 bld
->static_state
->pot_height
,
601 bld
->static_state
->wrap_r
);
602 lp_build_name(z
, "tex.z.wrapped");
604 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
616 * Get texture colors.
618 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
620 row_stride_vec
, img_stride_vec
,
621 data_ptr
, colors_out
);
626 * Generate code to sample a mipmap level with linear filtering.
627 * If sampling a cube texture, r = cube face in [0,5].
630 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
631 LLVMValueRef width_vec
,
632 LLVMValueRef height_vec
,
633 LLVMValueRef depth_vec
,
634 LLVMValueRef row_stride_vec
,
635 LLVMValueRef img_stride_vec
,
636 LLVMValueRef data_ptr
,
640 LLVMValueRef colors_out
[4])
642 const int dims
= texture_dims(bld
->static_state
->target
);
643 LLVMValueRef x0
, y0
, z0
, x1
, y1
, z1
;
644 LLVMValueRef s_fpart
, t_fpart
, r_fpart
;
645 LLVMValueRef neighbors
[2][2][4];
649 * Compute integer texcoords.
651 lp_build_sample_wrap_linear(bld
, s
, width_vec
,
652 bld
->static_state
->pot_width
,
653 bld
->static_state
->wrap_s
,
655 lp_build_name(x0
, "tex.x0.wrapped");
656 lp_build_name(x1
, "tex.x1.wrapped");
659 lp_build_sample_wrap_linear(bld
, t
, height_vec
,
660 bld
->static_state
->pot_height
,
661 bld
->static_state
->wrap_t
,
663 lp_build_name(y0
, "tex.y0.wrapped");
664 lp_build_name(y1
, "tex.y1.wrapped");
667 lp_build_sample_wrap_linear(bld
, r
, depth_vec
,
668 bld
->static_state
->pot_depth
,
669 bld
->static_state
->wrap_r
,
671 lp_build_name(z0
, "tex.z0.wrapped");
672 lp_build_name(z1
, "tex.z1.wrapped");
674 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
675 z0
= z1
= r
; /* cube face */
684 y0
= y1
= t_fpart
= NULL
;
685 z0
= z1
= r_fpart
= NULL
;
689 * Get texture colors.
691 /* get x0/x1 texels */
692 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
694 row_stride_vec
, img_stride_vec
,
695 data_ptr
, neighbors
[0][0]);
696 lp_build_sample_texel_soa(bld
, 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
, width_vec
, height_vec
, depth_vec
,
716 row_stride_vec
, img_stride_vec
,
717 data_ptr
, neighbors
[1][0]);
718 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
720 row_stride_vec
, img_stride_vec
,
721 data_ptr
, neighbors
[1][1]);
723 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
724 for (chan
= 0; chan
< 4; chan
++) {
725 colors0
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
727 neighbors
[0][0][chan
],
728 neighbors
[0][1][chan
],
729 neighbors
[1][0][chan
],
730 neighbors
[1][1][chan
]);
734 LLVMValueRef neighbors1
[2][2][4];
735 LLVMValueRef colors1
[4];
737 /* get x0/x1/y0/y1 texels at z1 */
738 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
740 row_stride_vec
, img_stride_vec
,
741 data_ptr
, neighbors1
[0][0]);
742 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
744 row_stride_vec
, img_stride_vec
,
745 data_ptr
, neighbors1
[0][1]);
746 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
748 row_stride_vec
, img_stride_vec
,
749 data_ptr
, neighbors1
[1][0]);
750 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
752 row_stride_vec
, img_stride_vec
,
753 data_ptr
, neighbors1
[1][1]);
755 /* Bilinear interpolate the four samples from the second Z slice */
756 for (chan
= 0; chan
< 4; chan
++) {
757 colors1
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
759 neighbors1
[0][0][chan
],
760 neighbors1
[0][1][chan
],
761 neighbors1
[1][0][chan
],
762 neighbors1
[1][1][chan
]);
765 /* Linearly interpolate the two samples from the two 3D slices */
766 for (chan
= 0; chan
< 4; chan
++) {
767 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
,
769 colors0
[chan
], colors1
[chan
]);
774 for (chan
= 0; chan
< 4; chan
++) {
775 colors_out
[chan
] = colors0
[chan
];
783 * Sample the texture/mipmap using given image filter and mip filter.
784 * data0_ptr and data1_ptr point to the two mipmap levels to sample
785 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
786 * If we're using nearest miplevel sampling the '1' values will be null/unused.
789 lp_build_sample_mipmap(struct lp_build_sample_context
*bld
,
795 LLVMValueRef lod_fpart
,
796 LLVMValueRef width0_vec
,
797 LLVMValueRef width1_vec
,
798 LLVMValueRef height0_vec
,
799 LLVMValueRef height1_vec
,
800 LLVMValueRef depth0_vec
,
801 LLVMValueRef depth1_vec
,
802 LLVMValueRef row_stride0_vec
,
803 LLVMValueRef row_stride1_vec
,
804 LLVMValueRef img_stride0_vec
,
805 LLVMValueRef img_stride1_vec
,
806 LLVMValueRef data_ptr0
,
807 LLVMValueRef data_ptr1
,
808 LLVMValueRef
*colors_out
)
810 LLVMValueRef colors0
[4], colors1
[4];
813 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
814 /* sample the first mipmap level */
815 lp_build_sample_image_nearest(bld
,
816 width0_vec
, height0_vec
, depth0_vec
,
817 row_stride0_vec
, img_stride0_vec
,
818 data_ptr0
, s
, t
, r
, colors0
);
820 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
821 /* sample the second mipmap level */
822 lp_build_sample_image_nearest(bld
,
823 width1_vec
, height1_vec
, depth1_vec
,
824 row_stride1_vec
, img_stride1_vec
,
825 data_ptr1
, s
, t
, r
, colors1
);
829 assert(img_filter
== PIPE_TEX_FILTER_LINEAR
);
831 /* sample the first mipmap level */
832 lp_build_sample_image_linear(bld
,
833 width0_vec
, height0_vec
, depth0_vec
,
834 row_stride0_vec
, img_stride0_vec
,
835 data_ptr0
, s
, t
, r
, colors0
);
837 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
838 /* sample the second mipmap level */
839 lp_build_sample_image_linear(bld
,
840 width1_vec
, height1_vec
, depth1_vec
,
841 row_stride1_vec
, img_stride1_vec
,
842 data_ptr1
, s
, t
, r
, colors1
);
846 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
847 /* interpolate samples from the two mipmap levels */
848 for (chan
= 0; chan
< 4; chan
++) {
849 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
850 colors0
[chan
], colors1
[chan
]);
854 /* use first/only level's colors */
855 for (chan
= 0; chan
< 4; chan
++) {
856 colors_out
[chan
] = colors0
[chan
];
864 * General texture sampling codegen.
865 * This function handles texture sampling for all texture targets (1D,
866 * 2D, 3D, cube) and all filtering modes.
869 lp_build_sample_general(struct lp_build_sample_context
*bld
,
874 const LLVMValueRef
*ddx
,
875 const LLVMValueRef
*ddy
,
876 LLVMValueRef lod_bias
, /* optional */
877 LLVMValueRef explicit_lod
, /* optional */
881 LLVMValueRef width_vec
,
882 LLVMValueRef height_vec
,
883 LLVMValueRef depth_vec
,
884 LLVMValueRef row_stride_array
,
885 LLVMValueRef img_stride_array
,
886 LLVMValueRef data_array
,
887 LLVMValueRef
*colors_out
)
889 struct lp_build_context
*float_bld
= &bld
->float_bld
;
890 const unsigned mip_filter
= bld
->static_state
->min_mip_filter
;
891 const unsigned min_filter
= bld
->static_state
->min_img_filter
;
892 const unsigned mag_filter
= bld
->static_state
->mag_img_filter
;
893 const int dims
= texture_dims(bld
->static_state
->target
);
894 LLVMValueRef lod
= NULL
, lod_fpart
= NULL
;
895 LLVMValueRef ilevel0
, ilevel1
= NULL
;
896 LLVMValueRef width0_vec
= NULL
, height0_vec
= NULL
, depth0_vec
= NULL
;
897 LLVMValueRef width1_vec
= NULL
, height1_vec
= NULL
, depth1_vec
= NULL
;
898 LLVMValueRef row_stride0_vec
= NULL
, row_stride1_vec
= NULL
;
899 LLVMValueRef img_stride0_vec
= NULL
, img_stride1_vec
= NULL
;
900 LLVMValueRef data_ptr0
, data_ptr1
= NULL
;
901 LLVMValueRef face_ddx
[4], face_ddy
[4];
904 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
905 mip_filter, min_filter, mag_filter);
909 * Choose cube face, recompute texcoords and derivatives for the chosen face.
911 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
912 LLVMValueRef face
, face_s
, face_t
;
913 lp_build_cube_lookup(bld
, s
, t
, r
, &face
, &face_s
, &face_t
);
914 s
= face_s
; /* vec */
915 t
= face_t
; /* vec */
916 /* use 'r' to indicate cube face */
917 r
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, face
); /* vec */
919 /* recompute ddx, ddy using the new (s,t) face texcoords */
920 face_ddx
[0] = lp_build_ddx(&bld
->coord_bld
, s
);
921 face_ddx
[1] = lp_build_ddx(&bld
->coord_bld
, t
);
924 face_ddy
[0] = lp_build_ddy(&bld
->coord_bld
, s
);
925 face_ddy
[1] = lp_build_ddy(&bld
->coord_bld
, t
);
933 * Compute the level of detail (float).
935 if (min_filter
!= mag_filter
||
936 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
937 /* Need to compute lod either to choose mipmap levels or to
938 * distinguish between minification/magnification with one mipmap level.
940 lod
= lp_build_lod_selector(bld
, unit
, ddx
, ddy
,
941 lod_bias
, explicit_lod
,
942 width
, height
, depth
);
946 * Compute integer mipmap level(s) to fetch texels from.
948 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
949 /* always use mip level 0 */
950 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
951 /* XXX this is a work-around for an apparent bug in LLVM 2.7.
952 * We should be able to set ilevel0 = const(0) but that causes
953 * bad x86 code to be emitted.
955 lod
= lp_build_const_elem(bld
->coord_bld
.type
, 0.0);
956 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
959 ilevel0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
964 if (mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
965 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
968 assert(mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
);
969 lp_build_linear_mip_levels(bld
, unit
, lod
, &ilevel0
, &ilevel1
,
971 lod_fpart
= lp_build_broadcast_scalar(&bld
->coord_bld
, lod_fpart
);
975 /* compute image size(s) of source mipmap level(s) */
976 lp_build_mipmap_level_sizes(bld
, dims
, width_vec
, height_vec
, depth_vec
,
978 row_stride_array
, img_stride_array
,
979 &width0_vec
, &width1_vec
,
980 &height0_vec
, &height1_vec
,
981 &depth0_vec
, &depth1_vec
,
982 &row_stride0_vec
, &row_stride1_vec
,
983 &img_stride0_vec
, &img_stride1_vec
);
986 * Get pointer(s) to image data for mipmap level(s).
988 data_ptr0
= lp_build_get_mipmap_level(bld
, data_array
, ilevel0
);
989 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
990 data_ptr1
= lp_build_get_mipmap_level(bld
, data_array
, ilevel1
);
994 * Get/interpolate texture colors.
996 if (min_filter
== mag_filter
) {
997 /* no need to distinquish between minification and magnification */
998 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
, s
, t
, r
, lod_fpart
,
999 width0_vec
, width1_vec
,
1000 height0_vec
, height1_vec
,
1001 depth0_vec
, depth1_vec
,
1002 row_stride0_vec
, row_stride1_vec
,
1003 img_stride0_vec
, img_stride1_vec
,
1004 data_ptr0
, data_ptr1
,
1008 /* Emit conditional to choose min image filter or mag image filter
1009 * depending on the lod being >0 or <= 0, respectively.
1011 struct lp_build_flow_context
*flow_ctx
;
1012 struct lp_build_if_state if_ctx
;
1013 LLVMValueRef minify
;
1015 flow_ctx
= lp_build_flow_create(bld
->builder
);
1016 lp_build_flow_scope_begin(flow_ctx
);
1018 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[0]);
1019 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[1]);
1020 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[2]);
1021 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[3]);
1023 /* minify = lod > 0.0 */
1024 minify
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
1025 lod
, float_bld
->zero
, "");
1027 lp_build_if(&if_ctx
, flow_ctx
, bld
->builder
, minify
);
1029 /* Use the minification filter */
1030 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
,
1032 width0_vec
, width1_vec
,
1033 height0_vec
, height1_vec
,
1034 depth0_vec
, depth1_vec
,
1035 row_stride0_vec
, row_stride1_vec
,
1036 img_stride0_vec
, img_stride1_vec
,
1037 data_ptr0
, data_ptr1
,
1040 lp_build_else(&if_ctx
);
1042 /* Use the magnification filter */
1043 lp_build_sample_mipmap(bld
, mag_filter
, mip_filter
,
1045 width0_vec
, width1_vec
,
1046 height0_vec
, height1_vec
,
1047 depth0_vec
, depth1_vec
,
1048 row_stride0_vec
, row_stride1_vec
,
1049 img_stride0_vec
, img_stride1_vec
,
1050 data_ptr0
, data_ptr1
,
1053 lp_build_endif(&if_ctx
);
1055 lp_build_flow_scope_end(flow_ctx
);
1056 lp_build_flow_destroy(flow_ctx
);
1062 * Do shadow test/comparison.
1063 * \param p the texcoord Z (aka R, aka P) component
1064 * \param texel the texel to compare against (use the X channel)
1067 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
1069 LLVMValueRef texel
[4])
1071 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1073 const unsigned chan
= 0;
1075 if (bld
->static_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
1080 LLVMValueRef indx
= lp_build_const_int32(0);
1081 LLVMValueRef coord
= LLVMBuildExtractElement(bld
->builder
, p
, indx
, "");
1082 LLVMValueRef tex
= LLVMBuildExtractElement(bld
->builder
,
1083 texel
[chan
], indx
, "");
1084 lp_build_printf(bld
->builder
, "shadow compare coord %f to texture %f\n",
1088 /* result = (p FUNC texel) ? 1 : 0 */
1089 res
= lp_build_cmp(texel_bld
, bld
->static_state
->compare_func
,
1091 res
= lp_build_select(texel_bld
, res
, texel_bld
->one
, texel_bld
->zero
);
1093 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1097 texel
[3] = texel_bld
->one
;
1102 * Just set texels to white instead of actually sampling the texture.
1106 lp_build_sample_nop(struct lp_type type
,
1107 LLVMValueRef texel_out
[4])
1109 LLVMValueRef one
= lp_build_one(type
);
1112 for (chan
= 0; chan
< 4; chan
++) {
1113 texel_out
[chan
] = one
;
1119 * Build texture sampling code.
1120 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1122 * \param type vector float type to use for coords, etc.
1123 * \param ddx partial derivatives of (s,t,r,q) with respect to x
1124 * \param ddy partial derivatives of (s,t,r,q) with respect to y
1127 lp_build_sample_soa(LLVMBuilderRef builder
,
1128 const struct lp_sampler_static_state
*static_state
,
1129 struct lp_sampler_dynamic_state
*dynamic_state
,
1130 struct lp_type type
,
1132 unsigned num_coords
,
1133 const LLVMValueRef
*coords
,
1134 const LLVMValueRef ddx
[4],
1135 const LLVMValueRef ddy
[4],
1136 LLVMValueRef lod_bias
, /* optional */
1137 LLVMValueRef explicit_lod
, /* optional */
1138 LLVMValueRef texel_out
[4])
1140 struct lp_build_sample_context bld
;
1141 LLVMValueRef width
, width_vec
;
1142 LLVMValueRef height
, height_vec
;
1143 LLVMValueRef depth
, depth_vec
;
1144 LLVMValueRef row_stride_array
, img_stride_array
;
1145 LLVMValueRef data_array
;
1151 enum pipe_format fmt
= static_state
->format
;
1152 debug_printf("Sample from %s\n", util_format_name(fmt
));
1155 assert(type
.floating
);
1157 /* Setup our build context */
1158 memset(&bld
, 0, sizeof bld
);
1159 bld
.builder
= builder
;
1160 bld
.static_state
= static_state
;
1161 bld
.dynamic_state
= dynamic_state
;
1162 bld
.format_desc
= util_format_description(static_state
->format
);
1164 bld
.float_type
= lp_type_float(32);
1165 bld
.int_type
= lp_type_int(32);
1166 bld
.coord_type
= type
;
1167 bld
.uint_coord_type
= lp_uint_type(type
);
1168 bld
.int_coord_type
= lp_int_type(type
);
1169 bld
.texel_type
= type
;
1171 lp_build_context_init(&bld
.float_bld
, builder
, bld
.float_type
);
1172 lp_build_context_init(&bld
.int_bld
, builder
, bld
.int_type
);
1173 lp_build_context_init(&bld
.coord_bld
, builder
, bld
.coord_type
);
1174 lp_build_context_init(&bld
.uint_coord_bld
, builder
, bld
.uint_coord_type
);
1175 lp_build_context_init(&bld
.int_coord_bld
, builder
, bld
.int_coord_type
);
1176 lp_build_context_init(&bld
.texel_bld
, builder
, bld
.texel_type
);
1178 /* Get the dynamic state */
1179 width
= dynamic_state
->width(dynamic_state
, builder
, unit
);
1180 height
= dynamic_state
->height(dynamic_state
, builder
, unit
);
1181 depth
= dynamic_state
->depth(dynamic_state
, builder
, unit
);
1182 row_stride_array
= dynamic_state
->row_stride(dynamic_state
, builder
, unit
);
1183 img_stride_array
= dynamic_state
->img_stride(dynamic_state
, builder
, unit
);
1184 data_array
= dynamic_state
->data_ptr(dynamic_state
, builder
, unit
);
1185 /* Note that data_array is an array[level] of pointers to texture images */
1191 /* width, height, depth as uint vectors */
1192 width_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, width
);
1193 height_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, height
);
1194 depth_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, depth
);
1197 /* For debug: no-op texture sampling */
1198 lp_build_sample_nop(bld
.texel_type
, texel_out
);
1200 else if (util_format_fits_8unorm(bld
.format_desc
) &&
1201 lp_is_simple_wrap_mode(static_state
->wrap_s
) &&
1202 lp_is_simple_wrap_mode(static_state
->wrap_t
)) {
1203 /* do sampling/filtering with fixed pt arithmetic */
1204 lp_build_sample_aos(&bld
, unit
, s
, t
, r
, ddx
, ddy
,
1205 lod_bias
, explicit_lod
,
1206 width
, height
, depth
,
1207 width_vec
, height_vec
, depth_vec
,
1208 row_stride_array
, img_stride_array
,
1209 data_array
, texel_out
);
1213 if ((gallivm_debug
& GALLIVM_DEBUG_PERF
) &&
1214 util_format_fits_8unorm(bld
.format_desc
)) {
1215 debug_printf("%s: using floating point linear filtering for %s\n",
1216 __FUNCTION__
, bld
.format_desc
->short_name
);
1217 debug_printf(" min_img %d mag_img %d mip %d wraps %d wrapt %d\n",
1218 static_state
->min_img_filter
,
1219 static_state
->mag_img_filter
,
1220 static_state
->min_mip_filter
,
1221 static_state
->wrap_s
,
1222 static_state
->wrap_t
);
1225 lp_build_sample_general(&bld
, unit
, s
, t
, r
, ddx
, ddy
,
1226 lod_bias
, explicit_lod
,
1227 width
, height
, depth
,
1228 width_vec
, height_vec
, depth_vec
,
1229 row_stride_array
, img_stride_array
,
1234 lp_build_sample_compare(&bld
, r
, texel_out
);