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 "pipe/p_shader_tokens.h"
39 #include "util/u_debug.h"
40 #include "util/u_dump.h"
41 #include "util/u_memory.h"
42 #include "util/u_math.h"
43 #include "util/u_format.h"
44 #include "util/u_cpu_detect.h"
45 #include "lp_bld_debug.h"
46 #include "lp_bld_type.h"
47 #include "lp_bld_const.h"
48 #include "lp_bld_conv.h"
49 #include "lp_bld_arit.h"
50 #include "lp_bld_bitarit.h"
51 #include "lp_bld_logic.h"
52 #include "lp_bld_printf.h"
53 #include "lp_bld_swizzle.h"
54 #include "lp_bld_flow.h"
55 #include "lp_bld_gather.h"
56 #include "lp_bld_format.h"
57 #include "lp_bld_sample.h"
58 #include "lp_bld_sample_aos.h"
59 #include "lp_bld_struct.h"
60 #include "lp_bld_quad.h"
61 #include "lp_bld_pack.h"
65 * Generate code to fetch a texel from a texture at int coords (x, y, z).
66 * The computation depends on whether the texture is 1D, 2D or 3D.
67 * The result, texel, will be float vectors:
68 * texel[0] = red values
69 * texel[1] = green values
70 * texel[2] = blue values
71 * texel[3] = alpha values
74 lp_build_sample_texel_soa(struct lp_build_sample_context
*bld
,
82 LLVMValueRef y_stride
,
83 LLVMValueRef z_stride
,
84 LLVMValueRef data_ptr
,
85 LLVMValueRef mipoffsets
,
86 LLVMValueRef texel_out
[4])
88 const struct lp_sampler_static_state
*static_state
= bld
->static_state
;
89 const unsigned dims
= bld
->dims
;
90 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
91 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
94 LLVMValueRef use_border
= NULL
;
96 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
97 if (lp_sampler_wrap_mode_uses_border_color(static_state
->wrap_s
,
98 static_state
->min_img_filter
,
99 static_state
->mag_img_filter
)) {
101 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
102 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
103 use_border
= LLVMBuildOr(builder
, b1
, b2
, "b1_or_b2");
107 lp_sampler_wrap_mode_uses_border_color(static_state
->wrap_t
,
108 static_state
->min_img_filter
,
109 static_state
->mag_img_filter
)) {
111 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
112 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
114 use_border
= LLVMBuildOr(builder
, use_border
, b1
, "ub_or_b1");
115 use_border
= LLVMBuildOr(builder
, use_border
, b2
, "ub_or_b2");
118 use_border
= LLVMBuildOr(builder
, b1
, b2
, "b1_or_b2");
123 lp_sampler_wrap_mode_uses_border_color(static_state
->wrap_r
,
124 static_state
->min_img_filter
,
125 static_state
->mag_img_filter
)) {
127 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
128 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
130 use_border
= LLVMBuildOr(builder
, use_border
, b1
, "ub_or_b1");
131 use_border
= LLVMBuildOr(builder
, use_border
, b2
, "ub_or_b2");
134 use_border
= LLVMBuildOr(builder
, b1
, b2
, "b1_or_b2");
138 /* convert x,y,z coords to linear offset from start of texture, in bytes */
139 lp_build_sample_offset(&bld
->int_coord_bld
,
141 x
, y
, z
, y_stride
, z_stride
,
144 offset
= lp_build_add(&bld
->int_coord_bld
, offset
, mipoffsets
);
148 /* If we can sample the border color, it means that texcoords may
149 * lie outside the bounds of the texture image. We need to do
150 * something to prevent reading out of bounds and causing a segfault.
152 * Simply AND the texture coords with !use_border. This will cause
153 * coords which are out of bounds to become zero. Zero's guaranteed
154 * to be inside the texture image.
156 offset
= lp_build_andnot(&bld
->int_coord_bld
, offset
, use_border
);
159 lp_build_fetch_rgba_soa(bld
->gallivm
,
167 * Note: if we find an app which frequently samples the texture border
168 * we might want to implement a true conditional here to avoid sampling
169 * the texture whenever possible (since that's quite a bit of code).
172 * texel = border_color;
175 * texel = sample_texture(coord);
177 * As it is now, we always sample the texture, then selectively replace
178 * the texel color results with the border color.
182 /* select texel color or border color depending on use_border */
183 LLVMValueRef border_color_ptr
=
184 bld
->dynamic_state
->border_color(bld
->dynamic_state
,
187 for (chan
= 0; chan
< 4; chan
++) {
188 LLVMValueRef border_chan
=
189 lp_build_array_get(bld
->gallivm
, border_color_ptr
,
190 lp_build_const_int32(bld
->gallivm
, chan
));
191 LLVMValueRef border_chan_vec
=
192 lp_build_broadcast_scalar(&bld
->float_vec_bld
, border_chan
);
193 texel_out
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
194 border_chan_vec
, texel_out
[chan
]);
201 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
204 lp_build_coord_mirror(struct lp_build_sample_context
*bld
,
207 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
208 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
209 LLVMValueRef fract
, flr
, isOdd
;
211 lp_build_ifloor_fract(coord_bld
, coord
, &flr
, &fract
);
213 /* isOdd = flr & 1 */
214 isOdd
= LLVMBuildAnd(bld
->gallivm
->builder
, flr
, int_coord_bld
->one
, "");
216 /* make coord positive or negative depending on isOdd */
217 coord
= lp_build_set_sign(coord_bld
, fract
, isOdd
);
219 /* convert isOdd to float */
220 isOdd
= lp_build_int_to_float(coord_bld
, isOdd
);
222 /* add isOdd to coord */
223 coord
= lp_build_add(coord_bld
, coord
, isOdd
);
230 * Helper to compute the first coord and the weight for
231 * linear wrap repeat npot textures
234 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
235 LLVMValueRef coord_f
,
236 LLVMValueRef length_i
,
237 LLVMValueRef length_f
,
238 LLVMValueRef
*coord0_i
,
239 LLVMValueRef
*weight_f
)
241 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
242 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
243 LLVMValueRef half
= lp_build_const_vec(bld
->gallivm
, coord_bld
->type
, 0.5);
244 LLVMValueRef length_minus_one
= lp_build_sub(int_coord_bld
, length_i
,
247 /* wrap with normalized floats is just fract */
248 coord_f
= lp_build_fract(coord_bld
, coord_f
);
249 /* mul by size and subtract 0.5 */
250 coord_f
= lp_build_mul(coord_bld
, coord_f
, length_f
);
251 coord_f
= lp_build_sub(coord_bld
, coord_f
, half
);
253 * we avoided the 0.5/length division before the repeat wrap,
254 * now need to fix up edge cases with selects
256 /* convert to int, compute lerp weight */
257 lp_build_ifloor_fract(coord_bld
, coord_f
, coord0_i
, weight_f
);
258 mask
= lp_build_compare(int_coord_bld
->gallivm
, int_coord_bld
->type
,
259 PIPE_FUNC_LESS
, *coord0_i
, int_coord_bld
->zero
);
260 *coord0_i
= lp_build_select(int_coord_bld
, mask
, length_minus_one
, *coord0_i
);
265 * Build LLVM code for texture wrap mode for linear filtering.
266 * \param x0_out returns first integer texcoord
267 * \param x1_out returns second integer texcoord
268 * \param weight_out returns linear interpolation weight
271 lp_build_sample_wrap_linear(struct lp_build_sample_context
*bld
,
274 LLVMValueRef length_f
,
277 LLVMValueRef
*x0_out
,
278 LLVMValueRef
*x1_out
,
279 LLVMValueRef
*weight_out
)
281 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
282 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
283 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
284 LLVMValueRef half
= lp_build_const_vec(bld
->gallivm
, coord_bld
->type
, 0.5);
285 LLVMValueRef length_minus_one
= lp_build_sub(int_coord_bld
, length
, int_coord_bld
->one
);
286 LLVMValueRef coord0
, coord1
, weight
;
289 case PIPE_TEX_WRAP_REPEAT
:
291 /* mul by size and subtract 0.5 */
292 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
293 coord
= lp_build_sub(coord_bld
, coord
, half
);
294 /* convert to int, compute lerp weight */
295 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
296 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
298 coord0
= LLVMBuildAnd(builder
, coord0
, length_minus_one
, "");
299 coord1
= LLVMBuildAnd(builder
, coord1
, length_minus_one
, "");
303 lp_build_coord_repeat_npot_linear(bld
, coord
,
306 mask
= lp_build_compare(int_coord_bld
->gallivm
, int_coord_bld
->type
,
307 PIPE_FUNC_NOTEQUAL
, coord0
, length_minus_one
);
308 coord1
= LLVMBuildAnd(builder
,
309 lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
),
314 case PIPE_TEX_WRAP_CLAMP
:
315 if (bld
->static_state
->normalized_coords
) {
316 /* scale coord to length */
317 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
320 /* clamp to [0, length] */
321 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, length_f
);
323 coord
= lp_build_sub(coord_bld
, coord
, half
);
325 /* convert to int, compute lerp weight */
326 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
327 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
330 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
332 struct lp_build_context abs_coord_bld
= bld
->coord_bld
;
333 abs_coord_bld
.type
.sign
= FALSE
;
335 if (bld
->static_state
->normalized_coords
) {
336 /* mul by tex size */
337 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
339 /* clamp to length max */
340 coord
= lp_build_min(coord_bld
, coord
, length_f
);
342 coord
= lp_build_sub(coord_bld
, coord
, half
);
343 /* clamp to [0, length - 0.5] */
344 coord
= lp_build_max(coord_bld
, coord
, coord_bld
->zero
);
345 /* convert to int, compute lerp weight */
346 lp_build_ifloor_fract(&abs_coord_bld
, coord
, &coord0
, &weight
);
347 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
348 /* coord1 = min(coord1, length-1) */
349 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
353 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
356 if (bld
->static_state
->normalized_coords
) {
357 /* scale coord to length */
358 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
360 /* was: clamp to [-0.5, length + 0.5], then sub 0.5 */
361 coord
= lp_build_sub(coord_bld
, coord
, half
);
362 min
= lp_build_const_vec(bld
->gallivm
, coord_bld
->type
, -1.0F
);
363 coord
= lp_build_clamp(coord_bld
, coord
, min
, length_f
);
364 /* convert to int, compute lerp weight */
365 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
366 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
370 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
371 /* compute mirror function */
372 coord
= lp_build_coord_mirror(bld
, coord
);
374 /* scale coord to length */
375 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
376 coord
= lp_build_sub(coord_bld
, coord
, half
);
378 /* convert to int, compute lerp weight */
379 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
380 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
382 /* coord0 = max(coord0, 0) */
383 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
384 /* coord1 = min(coord1, length-1) */
385 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
388 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
389 coord
= lp_build_abs(coord_bld
, coord
);
391 if (bld
->static_state
->normalized_coords
) {
392 /* scale coord to length */
393 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
396 /* clamp to [0, length] */
397 coord
= lp_build_min(coord_bld
, coord
, length_f
);
399 coord
= lp_build_sub(coord_bld
, coord
, half
);
401 /* convert to int, compute lerp weight */
402 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
403 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
406 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
408 LLVMValueRef min
, max
;
409 struct lp_build_context abs_coord_bld
= bld
->coord_bld
;
410 abs_coord_bld
.type
.sign
= FALSE
;
411 coord
= lp_build_abs(coord_bld
, coord
);
413 if (bld
->static_state
->normalized_coords
) {
414 /* scale coord to length */
415 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
418 /* clamp to [0.5, length - 0.5] */
420 max
= lp_build_sub(coord_bld
, length_f
, min
);
421 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
423 coord
= lp_build_sub(coord_bld
, coord
, half
);
425 /* convert to int, compute lerp weight */
426 lp_build_ifloor_fract(&abs_coord_bld
, coord
, &coord0
, &weight
);
427 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
431 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
433 coord
= lp_build_abs(coord_bld
, coord
);
435 if (bld
->static_state
->normalized_coords
) {
436 /* scale coord to length */
437 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
440 /* was: clamp to [-0.5, length + 0.5] then sub 0.5 */
441 /* skip -0.5 clamp (always positive), do sub first */
442 coord
= lp_build_sub(coord_bld
, coord
, half
);
443 coord
= lp_build_min(coord_bld
, coord
, length_f
);
445 /* convert to int, compute lerp weight */
446 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
447 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
460 *weight_out
= weight
;
465 * Build LLVM code for texture wrap mode for nearest filtering.
466 * \param coord the incoming texcoord (nominally in [0,1])
467 * \param length the texture size along one dimension, as int vector
468 * \param is_pot if TRUE, length is a power of two
469 * \param wrap_mode one of PIPE_TEX_WRAP_x
472 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
475 LLVMValueRef length_f
,
479 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
480 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
481 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
482 LLVMValueRef length_minus_one
= lp_build_sub(int_coord_bld
, length
, int_coord_bld
->one
);
486 case PIPE_TEX_WRAP_REPEAT
:
488 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
489 icoord
= lp_build_ifloor(coord_bld
, coord
);
490 icoord
= LLVMBuildAnd(builder
, icoord
, length_minus_one
, "");
493 /* take fraction, unnormalize */
494 coord
= lp_build_fract_safe(coord_bld
, coord
);
495 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
496 icoord
= lp_build_itrunc(coord_bld
, coord
);
500 case PIPE_TEX_WRAP_CLAMP
:
501 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
502 if (bld
->static_state
->normalized_coords
) {
503 /* scale coord to length */
504 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
508 /* use itrunc instead since we clamp to 0 anyway */
509 icoord
= lp_build_itrunc(coord_bld
, coord
);
511 /* clamp to [0, length - 1]. */
512 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
516 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
517 /* Note: this is the same as CLAMP_TO_EDGE, except min = -1 */
519 LLVMValueRef min
, max
;
521 if (bld
->static_state
->normalized_coords
) {
522 /* scale coord to length */
523 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
526 icoord
= lp_build_ifloor(coord_bld
, coord
);
528 /* clamp to [-1, length] */
529 min
= lp_build_negate(int_coord_bld
, int_coord_bld
->one
);
531 icoord
= lp_build_clamp(int_coord_bld
, icoord
, min
, max
);
535 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
536 /* compute mirror function */
537 coord
= lp_build_coord_mirror(bld
, coord
);
539 /* scale coord to length */
540 assert(bld
->static_state
->normalized_coords
);
541 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
543 /* itrunc == ifloor here */
544 icoord
= lp_build_itrunc(coord_bld
, coord
);
546 /* clamp to [0, length - 1] */
547 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
550 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
551 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
552 coord
= lp_build_abs(coord_bld
, coord
);
554 if (bld
->static_state
->normalized_coords
) {
555 /* scale coord to length */
556 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
559 /* itrunc == ifloor here */
560 icoord
= lp_build_itrunc(coord_bld
, coord
);
562 /* clamp to [0, length - 1] */
563 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
566 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
567 coord
= lp_build_abs(coord_bld
, coord
);
569 if (bld
->static_state
->normalized_coords
) {
570 /* scale coord to length */
571 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
574 /* itrunc == ifloor here */
575 icoord
= lp_build_itrunc(coord_bld
, coord
);
577 /* clamp to [0, length] */
578 icoord
= lp_build_min(int_coord_bld
, icoord
, length
);
591 * Generate code to sample a mipmap level with nearest filtering.
592 * If sampling a cube texture, r = cube face in [0,5].
595 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
598 LLVMValueRef row_stride_vec
,
599 LLVMValueRef img_stride_vec
,
600 LLVMValueRef data_ptr
,
601 LLVMValueRef mipoffsets
,
605 LLVMValueRef colors_out
[4])
607 const unsigned dims
= bld
->dims
;
608 LLVMValueRef width_vec
;
609 LLVMValueRef height_vec
;
610 LLVMValueRef depth_vec
;
611 LLVMValueRef flt_size
;
612 LLVMValueRef flt_width_vec
;
613 LLVMValueRef flt_height_vec
;
614 LLVMValueRef flt_depth_vec
;
615 LLVMValueRef x
, y
, z
;
617 lp_build_extract_image_sizes(bld
,
621 &width_vec
, &height_vec
, &depth_vec
);
623 flt_size
= lp_build_int_to_float(&bld
->float_size_bld
, size
);
625 lp_build_extract_image_sizes(bld
,
626 &bld
->float_size_bld
,
629 &flt_width_vec
, &flt_height_vec
, &flt_depth_vec
);
632 * Compute integer texcoords.
634 x
= lp_build_sample_wrap_nearest(bld
, s
, width_vec
, flt_width_vec
,
635 bld
->static_state
->pot_width
,
636 bld
->static_state
->wrap_s
);
637 lp_build_name(x
, "tex.x.wrapped");
640 y
= lp_build_sample_wrap_nearest(bld
, t
, height_vec
, flt_height_vec
,
641 bld
->static_state
->pot_height
,
642 bld
->static_state
->wrap_t
);
643 lp_build_name(y
, "tex.y.wrapped");
646 z
= lp_build_sample_wrap_nearest(bld
, r
, depth_vec
, flt_depth_vec
,
647 bld
->static_state
->pot_depth
,
648 bld
->static_state
->wrap_r
);
649 lp_build_name(z
, "tex.z.wrapped");
651 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
663 * Get texture colors.
665 lp_build_sample_texel_soa(bld
, unit
,
666 width_vec
, height_vec
, depth_vec
,
668 row_stride_vec
, img_stride_vec
,
669 data_ptr
, mipoffsets
, colors_out
);
674 * Generate code to sample a mipmap level with linear filtering.
675 * If sampling a cube texture, r = cube face in [0,5].
678 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
681 LLVMValueRef row_stride_vec
,
682 LLVMValueRef img_stride_vec
,
683 LLVMValueRef data_ptr
,
684 LLVMValueRef mipoffsets
,
688 LLVMValueRef colors_out
[4])
690 const unsigned dims
= bld
->dims
;
691 LLVMValueRef width_vec
;
692 LLVMValueRef height_vec
;
693 LLVMValueRef depth_vec
;
694 LLVMValueRef flt_size
;
695 LLVMValueRef flt_width_vec
;
696 LLVMValueRef flt_height_vec
;
697 LLVMValueRef flt_depth_vec
;
698 LLVMValueRef x0
, y0
, z0
, x1
, y1
, z1
;
699 LLVMValueRef s_fpart
, t_fpart
, r_fpart
;
700 LLVMValueRef neighbors
[2][2][4];
703 lp_build_extract_image_sizes(bld
,
707 &width_vec
, &height_vec
, &depth_vec
);
709 flt_size
= lp_build_int_to_float(&bld
->float_size_bld
, size
);
711 lp_build_extract_image_sizes(bld
,
712 &bld
->float_size_bld
,
715 &flt_width_vec
, &flt_height_vec
, &flt_depth_vec
);
718 * Compute integer texcoords.
720 lp_build_sample_wrap_linear(bld
, s
, width_vec
, flt_width_vec
,
721 bld
->static_state
->pot_width
,
722 bld
->static_state
->wrap_s
,
724 lp_build_name(x0
, "tex.x0.wrapped");
725 lp_build_name(x1
, "tex.x1.wrapped");
728 lp_build_sample_wrap_linear(bld
, t
, height_vec
, flt_height_vec
,
729 bld
->static_state
->pot_height
,
730 bld
->static_state
->wrap_t
,
732 lp_build_name(y0
, "tex.y0.wrapped");
733 lp_build_name(y1
, "tex.y1.wrapped");
736 lp_build_sample_wrap_linear(bld
, r
, depth_vec
, flt_depth_vec
,
737 bld
->static_state
->pot_depth
,
738 bld
->static_state
->wrap_r
,
740 lp_build_name(z0
, "tex.z0.wrapped");
741 lp_build_name(z1
, "tex.z1.wrapped");
743 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
744 z0
= z1
= r
; /* cube face */
753 y0
= y1
= t_fpart
= NULL
;
754 z0
= z1
= r_fpart
= NULL
;
758 * Get texture colors.
760 /* get x0/x1 texels */
761 lp_build_sample_texel_soa(bld
, unit
,
762 width_vec
, height_vec
, depth_vec
,
764 row_stride_vec
, img_stride_vec
,
765 data_ptr
, mipoffsets
, neighbors
[0][0]);
766 lp_build_sample_texel_soa(bld
, unit
,
767 width_vec
, height_vec
, depth_vec
,
769 row_stride_vec
, img_stride_vec
,
770 data_ptr
, mipoffsets
, neighbors
[0][1]);
773 /* Interpolate two samples from 1D image to produce one color */
774 for (chan
= 0; chan
< 4; chan
++) {
775 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, s_fpart
,
776 neighbors
[0][0][chan
],
777 neighbors
[0][1][chan
]);
782 LLVMValueRef colors0
[4];
784 /* get x0/x1 texels at y1 */
785 lp_build_sample_texel_soa(bld
, unit
,
786 width_vec
, height_vec
, depth_vec
,
788 row_stride_vec
, img_stride_vec
,
789 data_ptr
, mipoffsets
, neighbors
[1][0]);
790 lp_build_sample_texel_soa(bld
, unit
,
791 width_vec
, height_vec
, depth_vec
,
793 row_stride_vec
, img_stride_vec
,
794 data_ptr
, mipoffsets
, neighbors
[1][1]);
796 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
797 for (chan
= 0; chan
< 4; chan
++) {
798 colors0
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
800 neighbors
[0][0][chan
],
801 neighbors
[0][1][chan
],
802 neighbors
[1][0][chan
],
803 neighbors
[1][1][chan
]);
807 LLVMValueRef neighbors1
[2][2][4];
808 LLVMValueRef colors1
[4];
810 /* get x0/x1/y0/y1 texels at z1 */
811 lp_build_sample_texel_soa(bld
, unit
,
812 width_vec
, height_vec
, depth_vec
,
814 row_stride_vec
, img_stride_vec
,
815 data_ptr
, mipoffsets
, neighbors1
[0][0]);
816 lp_build_sample_texel_soa(bld
, unit
,
817 width_vec
, height_vec
, depth_vec
,
819 row_stride_vec
, img_stride_vec
,
820 data_ptr
, mipoffsets
, neighbors1
[0][1]);
821 lp_build_sample_texel_soa(bld
, unit
,
822 width_vec
, height_vec
, depth_vec
,
824 row_stride_vec
, img_stride_vec
,
825 data_ptr
, mipoffsets
, neighbors1
[1][0]);
826 lp_build_sample_texel_soa(bld
, unit
,
827 width_vec
, height_vec
, depth_vec
,
829 row_stride_vec
, img_stride_vec
,
830 data_ptr
, mipoffsets
, neighbors1
[1][1]);
832 /* Bilinear interpolate the four samples from the second Z slice */
833 for (chan
= 0; chan
< 4; chan
++) {
834 colors1
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
836 neighbors1
[0][0][chan
],
837 neighbors1
[0][1][chan
],
838 neighbors1
[1][0][chan
],
839 neighbors1
[1][1][chan
]);
842 /* Linearly interpolate the two samples from the two 3D slices */
843 for (chan
= 0; chan
< 4; chan
++) {
844 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
,
846 colors0
[chan
], colors1
[chan
]);
851 for (chan
= 0; chan
< 4; chan
++) {
852 colors_out
[chan
] = colors0
[chan
];
860 * Sample the texture/mipmap using given image filter and mip filter.
861 * data0_ptr and data1_ptr point to the two mipmap levels to sample
862 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
863 * If we're using nearest miplevel sampling the '1' values will be null/unused.
866 lp_build_sample_mipmap(struct lp_build_sample_context
*bld
,
873 LLVMValueRef ilevel0
,
874 LLVMValueRef ilevel1
,
875 LLVMValueRef lod_fpart
,
876 LLVMValueRef
*colors_out
)
878 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
879 LLVMValueRef size0
= NULL
;
880 LLVMValueRef size1
= NULL
;
881 LLVMValueRef row_stride0_vec
= NULL
;
882 LLVMValueRef row_stride1_vec
= NULL
;
883 LLVMValueRef img_stride0_vec
= NULL
;
884 LLVMValueRef img_stride1_vec
= NULL
;
885 LLVMValueRef data_ptr0
= NULL
;
886 LLVMValueRef data_ptr1
= NULL
;
887 LLVMValueRef mipoff0
= NULL
;
888 LLVMValueRef mipoff1
= NULL
;
889 LLVMValueRef colors0
[4], colors1
[4];
892 /* sample the first mipmap level */
893 lp_build_mipmap_level_sizes(bld
, ilevel0
,
895 &row_stride0_vec
, &img_stride0_vec
);
896 if (bld
->num_lods
== 1) {
897 data_ptr0
= lp_build_get_mipmap_level(bld
, ilevel0
);
900 /* This path should work for num_lods 1 too but slightly less efficient */
901 data_ptr0
= bld
->base_ptr
;
902 mipoff0
= lp_build_get_mip_offsets(bld
, ilevel0
);
904 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
905 lp_build_sample_image_nearest(bld
, unit
,
907 row_stride0_vec
, img_stride0_vec
,
908 data_ptr0
, mipoff0
, s
, t
, r
,
912 assert(img_filter
== PIPE_TEX_FILTER_LINEAR
);
913 lp_build_sample_image_linear(bld
, unit
,
915 row_stride0_vec
, img_stride0_vec
,
916 data_ptr0
, mipoff0
, s
, t
, r
,
920 /* Store the first level's colors in the output variables */
921 for (chan
= 0; chan
< 4; chan
++) {
922 LLVMBuildStore(builder
, colors0
[chan
], colors_out
[chan
]);
925 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
926 struct lp_build_if_state if_ctx
;
927 LLVMValueRef need_lerp
;
928 unsigned num_quads
= bld
->coord_bld
.type
.length
/ 4;
930 /* need_lerp = lod_fpart > 0 */
931 if (num_quads
== 1) {
932 need_lerp
= LLVMBuildFCmp(builder
, LLVMRealUGT
,
933 lod_fpart
, bld
->perquadf_bld
.zero
,
938 * We'll do mip filtering if any of the quads need it.
939 * It might be better to split the vectors here and only fetch/filter
940 * quads which need it.
943 * We unfortunately need to clamp lod_fpart here since we can get
944 * negative values which would screw up filtering if not all
945 * lod_fpart values have same sign.
947 lod_fpart
= lp_build_max(&bld
->perquadf_bld
, lod_fpart
,
948 bld
->perquadf_bld
.zero
);
949 need_lerp
= lp_build_compare(bld
->gallivm
, bld
->perquadf_bld
.type
,
951 lod_fpart
, bld
->perquadf_bld
.zero
);
952 need_lerp
= lp_build_any_true_range(&bld
->perquadi_bld
, num_quads
, need_lerp
);
955 lp_build_if(&if_ctx
, bld
->gallivm
, need_lerp
);
957 /* sample the second mipmap level */
958 lp_build_mipmap_level_sizes(bld
, ilevel1
,
960 &row_stride1_vec
, &img_stride1_vec
);
961 if (bld
->num_lods
== 1) {
962 data_ptr1
= lp_build_get_mipmap_level(bld
, ilevel1
);
965 data_ptr1
= bld
->base_ptr
;
966 mipoff1
= lp_build_get_mip_offsets(bld
, ilevel1
);
968 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
969 lp_build_sample_image_nearest(bld
, unit
,
971 row_stride1_vec
, img_stride1_vec
,
972 data_ptr1
, mipoff1
, s
, t
, r
,
976 lp_build_sample_image_linear(bld
, unit
,
978 row_stride1_vec
, img_stride1_vec
,
979 data_ptr1
, mipoff1
, s
, t
, r
,
983 /* interpolate samples from the two mipmap levels */
985 lod_fpart
= lp_build_unpack_broadcast_aos_scalars(bld
->gallivm
,
986 bld
->perquadf_bld
.type
,
990 for (chan
= 0; chan
< 4; chan
++) {
991 colors0
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
992 colors0
[chan
], colors1
[chan
]);
993 LLVMBuildStore(builder
, colors0
[chan
], colors_out
[chan
]);
996 lp_build_endif(&if_ctx
);
1001 * Calculate cube face, lod, mip levels.
1004 lp_build_sample_common(struct lp_build_sample_context
*bld
,
1009 const struct lp_derivatives
*derivs
,
1010 LLVMValueRef lod_bias
, /* optional */
1011 LLVMValueRef explicit_lod
, /* optional */
1012 LLVMValueRef
*lod_ipart
,
1013 LLVMValueRef
*lod_fpart
,
1014 LLVMValueRef
*ilevel0
,
1015 LLVMValueRef
*ilevel1
)
1017 const unsigned mip_filter
= bld
->static_state
->min_mip_filter
;
1018 const unsigned min_filter
= bld
->static_state
->min_img_filter
;
1019 const unsigned mag_filter
= bld
->static_state
->mag_img_filter
;
1020 LLVMValueRef first_level
;
1021 struct lp_derivatives face_derivs
;
1024 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1025 mip_filter, min_filter, mag_filter);
1029 * Choose cube face, recompute texcoords and derivatives for the chosen face.
1031 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1032 LLVMValueRef face
, face_s
, face_t
;
1033 lp_build_cube_lookup(bld
, *s
, *t
, *r
, &face
, &face_s
, &face_t
);
1034 *s
= face_s
; /* vec */
1035 *t
= face_t
; /* vec */
1036 /* use 'r' to indicate cube face */
1037 *r
= face
; /* vec */
1039 /* recompute ddx, ddy using the new (s,t) face texcoords */
1040 face_derivs
.ddx_ddy
[0] = lp_build_packed_ddx_ddy_twocoord(&bld
->coord_bld
, *s
, *t
);
1041 face_derivs
.ddx_ddy
[1] = NULL
;
1042 derivs
= &face_derivs
;
1046 * Compute the level of detail (float).
1048 if (min_filter
!= mag_filter
||
1049 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
1050 /* Need to compute lod either to choose mipmap levels or to
1051 * distinguish between minification/magnification with one mipmap level.
1053 lp_build_lod_selector(bld
, unit
, derivs
,
1054 lod_bias
, explicit_lod
,
1056 lod_ipart
, lod_fpart
);
1058 *lod_ipart
= bld
->perquadi_bld
.zero
;
1062 * Compute integer mipmap level(s) to fetch texels from: ilevel0, ilevel1
1064 switch (mip_filter
) {
1066 assert(0 && "bad mip_filter value in lp_build_sample_soa()");
1068 case PIPE_TEX_MIPFILTER_NONE
:
1069 /* always use mip level 0 */
1070 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1071 /* XXX this is a work-around for an apparent bug in LLVM 2.7.
1072 * We should be able to set ilevel0 = const(0) but that causes
1073 * bad x86 code to be emitted.
1074 * XXX should probably disable that on other llvm versions.
1077 lp_build_nearest_mip_level(bld
, unit
, *lod_ipart
, ilevel0
);
1080 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
1081 bld
->gallivm
, unit
);
1082 first_level
= lp_build_broadcast_scalar(&bld
->perquadi_bld
, first_level
);
1083 *ilevel0
= first_level
;
1086 case PIPE_TEX_MIPFILTER_NEAREST
:
1088 lp_build_nearest_mip_level(bld
, unit
, *lod_ipart
, ilevel0
);
1090 case PIPE_TEX_MIPFILTER_LINEAR
:
1093 lp_build_linear_mip_levels(bld
, unit
,
1094 *lod_ipart
, lod_fpart
,
1101 * General texture sampling codegen.
1102 * This function handles texture sampling for all texture targets (1D,
1103 * 2D, 3D, cube) and all filtering modes.
1106 lp_build_sample_general(struct lp_build_sample_context
*bld
,
1111 LLVMValueRef lod_ipart
,
1112 LLVMValueRef lod_fpart
,
1113 LLVMValueRef ilevel0
,
1114 LLVMValueRef ilevel1
,
1115 LLVMValueRef
*colors_out
)
1117 struct lp_build_context
*int_bld
= &bld
->int_bld
;
1118 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1119 const unsigned mip_filter
= bld
->static_state
->min_mip_filter
;
1120 const unsigned min_filter
= bld
->static_state
->min_img_filter
;
1121 const unsigned mag_filter
= bld
->static_state
->mag_img_filter
;
1122 LLVMValueRef texels
[4];
1126 * Get/interpolate texture colors.
1129 for (chan
= 0; chan
< 4; ++chan
) {
1130 texels
[chan
] = lp_build_alloca(bld
->gallivm
, bld
->texel_bld
.vec_type
, "");
1131 lp_build_name(texels
[chan
], "sampler%u_texel_%c_var", unit
, "xyzw"[chan
]);
1134 if (min_filter
== mag_filter
) {
1135 /* no need to distinguish between minification and magnification */
1136 lp_build_sample_mipmap(bld
, unit
,
1137 min_filter
, mip_filter
,
1139 ilevel0
, ilevel1
, lod_fpart
,
1143 /* Emit conditional to choose min image filter or mag image filter
1144 * depending on the lod being > 0 or <= 0, respectively.
1146 struct lp_build_if_state if_ctx
;
1147 LLVMValueRef minify
;
1150 * XXX this should to all lods into account, if some are min
1151 * some max probably could hack up the coords/weights in the linear
1152 * path with selects to work for nearest.
1153 * If that's just two quads sitting next to each other it seems
1154 * quite ok to do the same filtering method on both though, at
1155 * least unless we have explicit lod (and who uses different
1156 * min/mag filter with that?)
1158 if (bld
->num_lods
> 1)
1159 lod_ipart
= LLVMBuildExtractElement(builder
, lod_ipart
,
1160 lp_build_const_int32(bld
->gallivm
, 0), "");
1162 /* minify = lod >= 0.0 */
1163 minify
= LLVMBuildICmp(builder
, LLVMIntSGE
,
1164 lod_ipart
, int_bld
->zero
, "");
1166 lp_build_if(&if_ctx
, bld
->gallivm
, minify
);
1168 /* Use the minification filter */
1169 lp_build_sample_mipmap(bld
, unit
,
1170 min_filter
, mip_filter
,
1172 ilevel0
, ilevel1
, lod_fpart
,
1175 lp_build_else(&if_ctx
);
1177 /* Use the magnification filter */
1178 lp_build_sample_mipmap(bld
, unit
,
1179 mag_filter
, PIPE_TEX_MIPFILTER_NONE
,
1181 ilevel0
, NULL
, NULL
,
1184 lp_build_endif(&if_ctx
);
1187 for (chan
= 0; chan
< 4; ++chan
) {
1188 colors_out
[chan
] = LLVMBuildLoad(builder
, texels
[chan
], "");
1189 lp_build_name(colors_out
[chan
], "sampler%u_texel_%c", unit
, "xyzw"[chan
]);
1195 * Texel fetch function.
1196 * In contrast to general sampling there is no filtering, no coord minification,
1197 * lod (if any) is always explicit uint, coords are uints (in terms of texel units)
1198 * directly to be applied to the selected mip level (after adding texel offsets).
1199 * This function handles texel fetch for all targets where texel fetch is supported
1200 * (no cube maps, but 1d, 2d, 3d are supported, arrays and buffers should be too).
1203 lp_build_fetch_texel(struct lp_build_sample_context
*bld
,
1205 const LLVMValueRef
*coords
,
1206 LLVMValueRef explicit_lod
,
1207 const LLVMValueRef
*offsets
,
1208 LLVMValueRef
*colors_out
)
1210 struct lp_build_context
*perquadi_bld
= &bld
->perquadi_bld
;
1211 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
1212 unsigned dims
= bld
->dims
, chan
;
1213 LLVMValueRef size
, ilevel
;
1214 LLVMValueRef row_stride_vec
= NULL
, img_stride_vec
= NULL
;
1215 LLVMValueRef x
= coords
[0], y
= coords
[1], z
= coords
[2];
1216 LLVMValueRef width
, height
, depth
, i
, j
;
1217 LLVMValueRef offset
, out_of_bounds
, out1
;
1219 /* XXX just like ordinary sampling, we don't handle per-pixel lod (yet). */
1220 if (explicit_lod
&& bld
->static_state
->target
!= PIPE_BUFFER
) {
1221 ilevel
= lp_build_pack_aos_scalars(bld
->gallivm
, int_coord_bld
->type
,
1222 perquadi_bld
->type
, explicit_lod
, 0);
1223 lp_build_nearest_mip_level(bld
, unit
, ilevel
, &ilevel
);
1227 ilevel
= lp_build_const_int32(bld
->gallivm
, 0);
1229 lp_build_mipmap_level_sizes(bld
, ilevel
,
1231 &row_stride_vec
, &img_stride_vec
);
1232 lp_build_extract_image_sizes(bld
, &bld
->int_size_bld
, int_coord_bld
->type
,
1233 size
, &width
, &height
, &depth
);
1235 /* This is a lot like border sampling */
1237 /* XXX coords are really unsigned, offsets are signed */
1238 x
= lp_build_add(int_coord_bld
, x
, offsets
[0]);
1240 out_of_bounds
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
1241 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
1242 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1246 y
= lp_build_add(int_coord_bld
, y
, offsets
[1]);
1248 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
1249 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1250 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
1251 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1255 z
= lp_build_add(int_coord_bld
, z
, offsets
[2]);
1257 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
1258 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1259 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
1260 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1264 lp_build_sample_offset(int_coord_bld
,
1266 x
, y
, z
, row_stride_vec
, img_stride_vec
,
1269 if (bld
->static_state
->target
!= PIPE_BUFFER
) {
1270 offset
= lp_build_add(int_coord_bld
, offset
,
1271 lp_build_get_mip_offsets(bld
, ilevel
));
1274 offset
= lp_build_andnot(int_coord_bld
, offset
, out_of_bounds
);
1276 lp_build_fetch_rgba_soa(bld
->gallivm
,
1279 bld
->base_ptr
, offset
,
1285 * Not needed except for ARB_robust_buffer_access_behavior.
1286 * Could use min/max above instead of out-of-bounds comparisons
1287 * (in fact cast to unsigned and min only is sufficient)
1288 * if we don't care about the result returned for out-of-bounds.
1290 for (chan
= 0; chan
< 4; chan
++) {
1291 colors_out
[chan
] = lp_build_select(&bld
->texel_bld
, out_of_bounds
,
1292 bld
->texel_bld
.zero
, colors_out
[chan
]);
1299 * Do shadow test/comparison.
1300 * \param p the texcoord Z (aka R, aka P) component
1301 * \param texel the texel to compare against (use the X channel)
1302 * Ideally this should really be done per-sample.
1305 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
1307 LLVMValueRef texel
[4])
1309 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1310 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1312 const unsigned chan
= 0;
1314 if (bld
->static_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
1319 LLVMValueRef indx
= lp_build_const_int32(bld
->gallivm
, 0);
1320 LLVMValueRef coord
= LLVMBuildExtractElement(builder
, p
, indx
, "");
1321 LLVMValueRef tex
= LLVMBuildExtractElement(builder
, texel
[chan
], indx
, "");
1322 lp_build_printf(bld
->gallivm
, "shadow compare coord %f to texture %f\n",
1326 /* Clamp p coords to [0,1] */
1327 p
= lp_build_clamp(&bld
->coord_bld
, p
,
1328 bld
->coord_bld
.zero
,
1329 bld
->coord_bld
.one
);
1331 /* result = (p FUNC texel) ? 1 : 0 */
1332 res
= lp_build_cmp(texel_bld
, bld
->static_state
->compare_func
,
1334 res
= lp_build_select(texel_bld
, res
, texel_bld
->one
, texel_bld
->zero
);
1336 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1340 texel
[3] = texel_bld
->one
;
1345 * Just set texels to white instead of actually sampling the texture.
1349 lp_build_sample_nop(struct gallivm_state
*gallivm
,
1350 struct lp_type type
,
1351 const LLVMValueRef
*coords
,
1352 LLVMValueRef texel_out
[4])
1354 LLVMValueRef one
= lp_build_one(gallivm
, type
);
1357 for (chan
= 0; chan
< 4; chan
++) {
1358 texel_out
[chan
] = one
;
1364 * Build texture sampling code.
1365 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1367 * \param type vector float type to use for coords, etc.
1368 * \param is_fetch if this is a texel fetch instruction.
1369 * \param derivs partial derivatives of (s,t,r,q) with respect to x and y
1372 lp_build_sample_soa(struct gallivm_state
*gallivm
,
1373 const struct lp_sampler_static_state
*static_state
,
1374 struct lp_sampler_dynamic_state
*dynamic_state
,
1375 struct lp_type type
,
1378 const LLVMValueRef
*coords
,
1379 const LLVMValueRef
*offsets
,
1380 const struct lp_derivatives
*derivs
,
1381 LLVMValueRef lod_bias
, /* optional */
1382 LLVMValueRef explicit_lod
, /* optional */
1383 LLVMValueRef texel_out
[4])
1385 unsigned dims
= texture_dims(static_state
->target
);
1386 unsigned num_quads
= type
.length
/ 4;
1387 unsigned mip_filter
= static_state
->min_mip_filter
;
1388 struct lp_build_sample_context bld
;
1389 LLVMTypeRef i32t
= LLVMInt32TypeInContext(gallivm
->context
);
1390 LLVMBuilderRef builder
= gallivm
->builder
;
1391 LLVMValueRef tex_width
, tex_height
, tex_depth
;
1397 enum pipe_format fmt
= static_state
->format
;
1398 debug_printf("Sample from %s\n", util_format_name(fmt
));
1401 assert(type
.floating
);
1403 /* Setup our build context */
1404 memset(&bld
, 0, sizeof bld
);
1405 bld
.gallivm
= gallivm
;
1406 bld
.static_state
= static_state
;
1407 bld
.dynamic_state
= dynamic_state
;
1408 bld
.format_desc
= util_format_description(static_state
->format
);
1411 bld
.vector_width
= lp_type_width(type
);
1413 bld
.float_type
= lp_type_float(32);
1414 bld
.int_type
= lp_type_int(32);
1415 bld
.coord_type
= type
;
1416 bld
.int_coord_type
= lp_int_type(type
);
1417 bld
.float_size_in_type
= lp_type_float(32);
1418 bld
.float_size_in_type
.length
= dims
> 1 ? 4 : 1;
1419 bld
.int_size_in_type
= lp_int_type(bld
.float_size_in_type
);
1420 bld
.texel_type
= type
;
1421 bld
.perquadf_type
= type
;
1422 /* we want native vector size to be able to use our intrinsics */
1423 bld
.perquadf_type
.length
= type
.length
> 4 ? ((type
.length
+ 15) / 16) * 4 : 1;
1424 bld
.perquadi_type
= lp_int_type(bld
.perquadf_type
);
1427 * There are other situations where at least the multiple int lods could be
1428 * avoided like min and max lod being equal.
1430 if ((is_fetch
&& explicit_lod
&& bld
.static_state
->target
!= PIPE_BUFFER
) ||
1431 (!is_fetch
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)) {
1432 bld
.num_lods
= num_quads
;
1438 bld
.float_size_type
= bld
.float_size_in_type
;
1439 bld
.float_size_type
.length
= bld
.num_lods
> 1 ? type
.length
:
1440 bld
.float_size_in_type
.length
;
1441 bld
.int_size_type
= lp_int_type(bld
.float_size_type
);
1443 lp_build_context_init(&bld
.float_bld
, gallivm
, bld
.float_type
);
1444 lp_build_context_init(&bld
.float_vec_bld
, gallivm
, type
);
1445 lp_build_context_init(&bld
.int_bld
, gallivm
, bld
.int_type
);
1446 lp_build_context_init(&bld
.coord_bld
, gallivm
, bld
.coord_type
);
1447 lp_build_context_init(&bld
.int_coord_bld
, gallivm
, bld
.int_coord_type
);
1448 lp_build_context_init(&bld
.int_size_in_bld
, gallivm
, bld
.int_size_in_type
);
1449 lp_build_context_init(&bld
.float_size_in_bld
, gallivm
, bld
.float_size_in_type
);
1450 lp_build_context_init(&bld
.int_size_bld
, gallivm
, bld
.int_size_type
);
1451 lp_build_context_init(&bld
.float_size_bld
, gallivm
, bld
.float_size_type
);
1452 lp_build_context_init(&bld
.texel_bld
, gallivm
, bld
.texel_type
);
1453 lp_build_context_init(&bld
.perquadf_bld
, gallivm
, bld
.perquadf_type
);
1454 lp_build_context_init(&bld
.perquadi_bld
, gallivm
, bld
.perquadi_type
);
1456 /* Get the dynamic state */
1457 tex_width
= dynamic_state
->width(dynamic_state
, gallivm
, unit
);
1458 tex_height
= dynamic_state
->height(dynamic_state
, gallivm
, unit
);
1459 tex_depth
= dynamic_state
->depth(dynamic_state
, gallivm
, unit
);
1460 bld
.row_stride_array
= dynamic_state
->row_stride(dynamic_state
, gallivm
, unit
);
1461 bld
.img_stride_array
= dynamic_state
->img_stride(dynamic_state
, gallivm
, unit
);
1462 bld
.base_ptr
= dynamic_state
->base_ptr(dynamic_state
, gallivm
, unit
);
1463 bld
.mip_offsets
= dynamic_state
->mip_offsets(dynamic_state
, gallivm
, unit
);
1464 /* Note that mip_offsets is an array[level] of offsets to texture images */
1470 /* width, height, depth as single int vector */
1472 bld
.int_size
= tex_width
;
1475 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size_in_bld
.undef
,
1476 tex_width
, LLVMConstInt(i32t
, 0, 0), "");
1478 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size
,
1479 tex_height
, LLVMConstInt(i32t
, 1, 0), "");
1481 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size
,
1482 tex_depth
, LLVMConstInt(i32t
, 2, 0), "");
1488 /* For debug: no-op texture sampling */
1489 lp_build_sample_nop(gallivm
,
1495 LLVMValueRef lod_ipart
= NULL
, lod_fpart
= NULL
;
1496 LLVMValueRef ilevel0
= NULL
, ilevel1
= NULL
;
1497 boolean use_aos
= util_format_fits_8unorm(bld
.format_desc
) &&
1498 lp_is_simple_wrap_mode(static_state
->wrap_s
) &&
1499 lp_is_simple_wrap_mode(static_state
->wrap_t
);
1501 if ((gallivm_debug
& GALLIVM_DEBUG_PERF
) &&
1502 !use_aos
&& util_format_fits_8unorm(bld
.format_desc
)) {
1503 debug_printf("%s: using floating point linear filtering for %s\n",
1504 __FUNCTION__
, bld
.format_desc
->short_name
);
1505 debug_printf(" min_img %d mag_img %d mip %d wraps %d wrapt %d\n",
1506 static_state
->min_img_filter
,
1507 static_state
->mag_img_filter
,
1508 static_state
->min_mip_filter
,
1509 static_state
->wrap_s
,
1510 static_state
->wrap_t
);
1514 lp_build_fetch_texel(&bld
, unit
, coords
,
1515 explicit_lod
, offsets
,
1518 if (static_state
->target
!= PIPE_BUFFER
) {
1519 apply_sampler_swizzle(&bld
, texel_out
);
1525 lp_build_sample_common(&bld
, unit
,
1527 derivs
, lod_bias
, explicit_lod
,
1528 &lod_ipart
, &lod_fpart
,
1529 &ilevel0
, &ilevel1
);
1532 * we only try 8-wide sampling with soa as it appears to
1533 * be a loss with aos with AVX (but it should work).
1534 * (It should be faster if we'd support avx2)
1536 if (num_quads
== 1 || !use_aos
) {
1538 if (num_quads
> 1) {
1539 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
1540 LLVMValueRef index0
= lp_build_const_int32(gallivm
, 0);
1542 * These parameters are the same for all quads,
1543 * could probably simplify.
1545 lod_ipart
= LLVMBuildExtractElement(builder
, lod_ipart
, index0
, "");
1546 ilevel0
= LLVMBuildExtractElement(builder
, ilevel0
, index0
, "");
1550 /* do sampling/filtering with fixed pt arithmetic */
1551 lp_build_sample_aos(&bld
, unit
,
1553 lod_ipart
, lod_fpart
,
1559 lp_build_sample_general(&bld
, unit
,
1561 lod_ipart
, lod_fpart
,
1568 struct lp_build_sample_context bld4
;
1569 struct lp_type type4
= type
;
1571 LLVMValueRef texelout4
[4];
1572 LLVMValueRef texelouttmp
[4][LP_MAX_VECTOR_LENGTH
/16];
1576 /* Setup our build context */
1577 memset(&bld4
, 0, sizeof bld4
);
1578 bld4
.gallivm
= bld
.gallivm
;
1579 bld4
.static_state
= bld
.static_state
;
1580 bld4
.dynamic_state
= bld
.dynamic_state
;
1581 bld4
.format_desc
= bld
.format_desc
;
1582 bld4
.dims
= bld
.dims
;
1583 bld4
.row_stride_array
= bld
.row_stride_array
;
1584 bld4
.img_stride_array
= bld
.img_stride_array
;
1585 bld4
.base_ptr
= bld
.base_ptr
;
1586 bld4
.mip_offsets
= bld
.mip_offsets
;
1587 bld4
.int_size
= bld
.int_size
;
1589 bld4
.vector_width
= lp_type_width(type4
);
1591 bld4
.float_type
= lp_type_float(32);
1592 bld4
.int_type
= lp_type_int(32);
1593 bld4
.coord_type
= type4
;
1594 bld4
.int_coord_type
= lp_int_type(type4
);
1595 bld4
.float_size_in_type
= lp_type_float(32);
1596 bld4
.float_size_in_type
.length
= dims
> 1 ? 4 : 1;
1597 bld4
.int_size_in_type
= lp_int_type(bld4
.float_size_in_type
);
1598 bld4
.texel_type
= type4
;
1599 bld4
.perquadf_type
= type4
;
1600 /* we want native vector size to be able to use our intrinsics */
1601 bld4
.perquadf_type
.length
= 1;
1602 bld4
.perquadi_type
= lp_int_type(bld4
.perquadf_type
);
1605 bld4
.int_size_type
= bld4
.int_size_in_type
;
1606 bld4
.float_size_type
= bld4
.float_size_in_type
;
1608 lp_build_context_init(&bld4
.float_bld
, gallivm
, bld4
.float_type
);
1609 lp_build_context_init(&bld4
.float_vec_bld
, gallivm
, type4
);
1610 lp_build_context_init(&bld4
.int_bld
, gallivm
, bld4
.int_type
);
1611 lp_build_context_init(&bld4
.coord_bld
, gallivm
, bld4
.coord_type
);
1612 lp_build_context_init(&bld4
.int_coord_bld
, gallivm
, bld4
.int_coord_type
);
1613 lp_build_context_init(&bld4
.int_size_in_bld
, gallivm
, bld4
.int_size_in_type
);
1614 lp_build_context_init(&bld4
.float_size_in_bld
, gallivm
, bld4
.float_size_in_type
);
1615 lp_build_context_init(&bld4
.int_size_bld
, gallivm
, bld4
.int_size_type
);
1616 lp_build_context_init(&bld4
.float_size_bld
, gallivm
, bld4
.float_size_type
);
1617 lp_build_context_init(&bld4
.texel_bld
, gallivm
, bld4
.texel_type
);
1618 lp_build_context_init(&bld4
.perquadf_bld
, gallivm
, bld4
.perquadf_type
);
1619 lp_build_context_init(&bld4
.perquadi_bld
, gallivm
, bld4
.perquadi_type
);
1621 for (i
= 0; i
< num_quads
; i
++) {
1622 LLVMValueRef s4
, t4
, r4
;
1623 LLVMValueRef lod_iparts
, lod_fparts
= NULL
;
1624 LLVMValueRef ilevel0s
, ilevel1s
= NULL
;
1625 LLVMValueRef indexi
= lp_build_const_int32(gallivm
, i
);
1627 s4
= lp_build_extract_range(gallivm
, s
, 4*i
, 4);
1628 t4
= lp_build_extract_range(gallivm
, t
, 4*i
, 4);
1629 r4
= lp_build_extract_range(gallivm
, r
, 4*i
, 4);
1630 lod_iparts
= LLVMBuildExtractElement(builder
, lod_ipart
, indexi
, "");
1631 ilevel0s
= LLVMBuildExtractElement(builder
, ilevel0
, indexi
, "");
1632 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1633 ilevel1s
= LLVMBuildExtractElement(builder
, ilevel1
, indexi
, "");
1634 lod_fparts
= LLVMBuildExtractElement(builder
, lod_fpart
, indexi
, "");
1638 /* do sampling/filtering with fixed pt arithmetic */
1639 lp_build_sample_aos(&bld4
, unit
,
1641 lod_iparts
, lod_fparts
,
1647 lp_build_sample_general(&bld4
, unit
,
1649 lod_iparts
, lod_fparts
,
1653 for (j
= 0; j
< 4; j
++) {
1654 texelouttmp
[j
][i
] = texelout4
[j
];
1658 for (j
= 0; j
< 4; j
++) {
1659 texel_out
[j
] = lp_build_concat(gallivm
, texelouttmp
[j
], type4
, num_quads
);
1664 lp_build_sample_compare(&bld
, r
, texel_out
);
1666 apply_sampler_swizzle(&bld
, texel_out
);
1670 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
1671 const struct lp_sampler_static_state
*static_state
,
1672 struct lp_sampler_dynamic_state
*dynamic_state
,
1673 struct lp_type int_type
,
1675 LLVMValueRef explicit_lod
,
1676 LLVMValueRef
*sizes_out
)
1681 struct lp_build_context bld_int_vec
;
1683 switch (static_state
->target
) {
1684 case PIPE_TEXTURE_1D
:
1688 case PIPE_TEXTURE_2D
:
1689 case PIPE_TEXTURE_CUBE
:
1690 case PIPE_TEXTURE_RECT
:
1693 case PIPE_TEXTURE_3D
:
1702 assert(!int_type
.floating
);
1704 lp_build_context_init(&bld_int_vec
, gallivm
, lp_type_int_vec(32, 128));
1707 LLVMValueRef first_level
;
1708 lod
= LLVMBuildExtractElement(gallivm
->builder
, explicit_lod
, lp_build_const_int32(gallivm
, 0), "");
1709 first_level
= dynamic_state
->first_level(dynamic_state
, gallivm
, unit
);
1710 lod
= lp_build_broadcast_scalar(&bld_int_vec
,
1711 LLVMBuildAdd(gallivm
->builder
, lod
, first_level
, "lod"));
1714 lod
= bld_int_vec
.zero
;
1717 size
= bld_int_vec
.undef
;
1719 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
1720 dynamic_state
->width(dynamic_state
, gallivm
, unit
),
1721 lp_build_const_int32(gallivm
, 0), "");
1724 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
1725 dynamic_state
->height(dynamic_state
, gallivm
, unit
),
1726 lp_build_const_int32(gallivm
, 1), "");
1730 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
1731 dynamic_state
->depth(dynamic_state
, gallivm
, unit
),
1732 lp_build_const_int32(gallivm
, 2), "");
1735 size
= lp_build_minify(&bld_int_vec
, size
, lod
);
1737 for (i
=0; i
< dims
; i
++) {
1738 sizes_out
[i
] = lp_build_extract_broadcast(gallivm
, bld_int_vec
.type
, int_type
,
1740 lp_build_const_int32(gallivm
, i
));