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 "util/u_format_rgb9e5.h"
46 #include "lp_bld_debug.h"
47 #include "lp_bld_type.h"
48 #include "lp_bld_const.h"
49 #include "lp_bld_conv.h"
50 #include "lp_bld_arit.h"
51 #include "lp_bld_bitarit.h"
52 #include "lp_bld_logic.h"
53 #include "lp_bld_printf.h"
54 #include "lp_bld_swizzle.h"
55 #include "lp_bld_flow.h"
56 #include "lp_bld_gather.h"
57 #include "lp_bld_format.h"
58 #include "lp_bld_sample.h"
59 #include "lp_bld_sample_aos.h"
60 #include "lp_bld_struct.h"
61 #include "lp_bld_quad.h"
62 #include "lp_bld_pack.h"
66 * Generate code to fetch a texel from a texture at int coords (x, y, z).
67 * The computation depends on whether the texture is 1D, 2D or 3D.
68 * The result, texel, will be float vectors:
69 * texel[0] = red values
70 * texel[1] = green values
71 * texel[2] = blue values
72 * texel[3] = alpha values
75 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_static_sampler_state
*static_state
= bld
->static_sampler_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 const struct util_format_description
*format_desc
= bld
->format_desc
;
185 struct lp_type border_type
= bld
->texel_type
;
186 border_type
.length
= 4;
188 * Only replace channels which are actually present. The others should
189 * get optimized away eventually by sampler_view swizzle anyway but it's
192 for (chan
= 0; chan
< 4; chan
++) {
194 /* reverse-map channel... */
195 for (chan_s
= 0; chan_s
< 4; chan_s
++) {
196 if (chan_s
== format_desc
->swizzle
[chan
]) {
201 /* use the already clamped color */
202 LLVMValueRef idx
= lp_build_const_int32(bld
->gallivm
, chan
);
203 LLVMValueRef border_chan
;
205 border_chan
= lp_build_extract_broadcast(bld
->gallivm
,
208 bld
->border_color_clamped
,
210 texel_out
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
211 border_chan
, texel_out
[chan
]);
219 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
222 lp_build_coord_mirror(struct lp_build_sample_context
*bld
,
225 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
226 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
227 LLVMValueRef fract
, flr
, isOdd
;
229 lp_build_ifloor_fract(coord_bld
, coord
, &flr
, &fract
);
231 /* isOdd = flr & 1 */
232 isOdd
= LLVMBuildAnd(bld
->gallivm
->builder
, flr
, int_coord_bld
->one
, "");
234 /* make coord positive or negative depending on isOdd */
235 coord
= lp_build_set_sign(coord_bld
, fract
, isOdd
);
237 /* convert isOdd to float */
238 isOdd
= lp_build_int_to_float(coord_bld
, isOdd
);
240 /* add isOdd to coord */
241 coord
= lp_build_add(coord_bld
, coord
, isOdd
);
248 * Helper to compute the first coord and the weight for
249 * linear wrap repeat npot textures
252 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
253 LLVMValueRef coord_f
,
254 LLVMValueRef length_i
,
255 LLVMValueRef length_f
,
256 LLVMValueRef
*coord0_i
,
257 LLVMValueRef
*weight_f
)
259 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
260 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
261 LLVMValueRef half
= lp_build_const_vec(bld
->gallivm
, coord_bld
->type
, 0.5);
262 LLVMValueRef length_minus_one
= lp_build_sub(int_coord_bld
, length_i
,
265 /* wrap with normalized floats is just fract */
266 coord_f
= lp_build_fract(coord_bld
, coord_f
);
267 /* mul by size and subtract 0.5 */
268 coord_f
= lp_build_mul(coord_bld
, coord_f
, length_f
);
269 coord_f
= lp_build_sub(coord_bld
, coord_f
, half
);
271 * we avoided the 0.5/length division before the repeat wrap,
272 * now need to fix up edge cases with selects
274 /* convert to int, compute lerp weight */
275 lp_build_ifloor_fract(coord_bld
, coord_f
, coord0_i
, weight_f
);
276 mask
= lp_build_compare(int_coord_bld
->gallivm
, int_coord_bld
->type
,
277 PIPE_FUNC_LESS
, *coord0_i
, int_coord_bld
->zero
);
278 *coord0_i
= lp_build_select(int_coord_bld
, mask
, length_minus_one
, *coord0_i
);
283 * Build LLVM code for texture wrap mode for linear filtering.
284 * \param x0_out returns first integer texcoord
285 * \param x1_out returns second integer texcoord
286 * \param weight_out returns linear interpolation weight
289 lp_build_sample_wrap_linear(struct lp_build_sample_context
*bld
,
292 LLVMValueRef length_f
,
296 LLVMValueRef
*x0_out
,
297 LLVMValueRef
*x1_out
,
298 LLVMValueRef
*weight_out
)
300 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
301 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
302 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
303 LLVMValueRef half
= lp_build_const_vec(bld
->gallivm
, coord_bld
->type
, 0.5);
304 LLVMValueRef length_minus_one
= lp_build_sub(int_coord_bld
, length
, int_coord_bld
->one
);
305 LLVMValueRef coord0
, coord1
, weight
;
308 case PIPE_TEX_WRAP_REPEAT
:
310 /* mul by size and subtract 0.5 */
311 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
312 coord
= lp_build_sub(coord_bld
, coord
, half
);
314 offset
= lp_build_int_to_float(coord_bld
, offset
);
315 coord
= lp_build_add(coord_bld
, coord
, offset
);
317 /* convert to int, compute lerp weight */
318 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
319 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
321 coord0
= LLVMBuildAnd(builder
, coord0
, length_minus_one
, "");
322 coord1
= LLVMBuildAnd(builder
, coord1
, length_minus_one
, "");
327 offset
= lp_build_int_to_float(coord_bld
, offset
);
328 offset
= lp_build_div(coord_bld
, offset
, length_f
);
329 coord
= lp_build_add(coord_bld
, coord
, offset
);
331 lp_build_coord_repeat_npot_linear(bld
, coord
,
334 mask
= lp_build_compare(int_coord_bld
->gallivm
, int_coord_bld
->type
,
335 PIPE_FUNC_NOTEQUAL
, coord0
, length_minus_one
);
336 coord1
= LLVMBuildAnd(builder
,
337 lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
),
342 case PIPE_TEX_WRAP_CLAMP
:
343 if (bld
->static_sampler_state
->normalized_coords
) {
344 /* scale coord to length */
345 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
348 offset
= lp_build_int_to_float(coord_bld
, offset
);
349 coord
= lp_build_add(coord_bld
, coord
, offset
);
352 /* clamp to [0, length] */
353 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, length_f
);
355 coord
= lp_build_sub(coord_bld
, coord
, half
);
357 /* convert to int, compute lerp weight */
358 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
359 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
362 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
364 struct lp_build_context abs_coord_bld
= bld
->coord_bld
;
365 abs_coord_bld
.type
.sign
= FALSE
;
367 if (bld
->static_sampler_state
->normalized_coords
) {
368 /* mul by tex size */
369 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
372 offset
= lp_build_int_to_float(coord_bld
, offset
);
373 coord
= lp_build_add(coord_bld
, coord
, offset
);
376 /* clamp to length max */
377 coord
= lp_build_min(coord_bld
, coord
, length_f
);
379 coord
= lp_build_sub(coord_bld
, coord
, half
);
380 /* clamp to [0, length - 0.5] */
381 coord
= lp_build_max(coord_bld
, coord
, coord_bld
->zero
);
382 /* convert to int, compute lerp weight */
383 lp_build_ifloor_fract(&abs_coord_bld
, coord
, &coord0
, &weight
);
384 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
385 /* coord1 = min(coord1, length-1) */
386 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
390 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
391 if (bld
->static_sampler_state
->normalized_coords
) {
392 /* scale coord to length */
393 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
396 offset
= lp_build_int_to_float(coord_bld
, offset
);
397 coord
= lp_build_add(coord_bld
, coord
, offset
);
399 /* was: clamp to [-0.5, length + 0.5], then sub 0.5 */
400 /* can skip clamp (though might not work for very large coord values */
401 coord
= lp_build_sub(coord_bld
, coord
, half
);
402 /* convert to int, compute lerp weight */
403 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
404 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
407 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
408 /* compute mirror function */
409 coord
= lp_build_coord_mirror(bld
, coord
);
411 /* scale coord to length */
412 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
413 coord
= lp_build_sub(coord_bld
, coord
, half
);
415 offset
= lp_build_int_to_float(coord_bld
, offset
);
416 coord
= lp_build_add(coord_bld
, coord
, offset
);
419 /* convert to int, compute lerp weight */
420 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
421 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
423 /* coord0 = max(coord0, 0) */
424 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
425 /* coord1 = min(coord1, length-1) */
426 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
429 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
430 if (bld
->static_sampler_state
->normalized_coords
) {
431 /* scale coord to length */
432 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
435 offset
= lp_build_int_to_float(coord_bld
, offset
);
436 coord
= lp_build_add(coord_bld
, coord
, offset
);
438 coord
= lp_build_abs(coord_bld
, coord
);
440 /* clamp to [0, length] */
441 coord
= lp_build_min(coord_bld
, coord
, length_f
);
443 coord
= lp_build_sub(coord_bld
, coord
, half
);
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
);
450 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
452 struct lp_build_context abs_coord_bld
= bld
->coord_bld
;
453 abs_coord_bld
.type
.sign
= FALSE
;
455 if (bld
->static_sampler_state
->normalized_coords
) {
456 /* scale coord to length */
457 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
460 offset
= lp_build_int_to_float(coord_bld
, offset
);
461 coord
= lp_build_add(coord_bld
, coord
, offset
);
463 coord
= lp_build_abs(coord_bld
, coord
);
465 /* clamp to length max */
466 coord
= lp_build_min(coord_bld
, coord
, length_f
);
468 coord
= lp_build_sub(coord_bld
, coord
, half
);
469 /* clamp to [0, length - 0.5] */
470 coord
= lp_build_max(coord_bld
, coord
, coord_bld
->zero
);
472 /* convert to int, compute lerp weight */
473 lp_build_ifloor_fract(&abs_coord_bld
, coord
, &coord0
, &weight
);
474 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
475 /* coord1 = min(coord1, length-1) */
476 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
480 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
482 if (bld
->static_sampler_state
->normalized_coords
) {
483 /* scale coord to length */
484 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
487 offset
= lp_build_int_to_float(coord_bld
, offset
);
488 coord
= lp_build_add(coord_bld
, coord
, offset
);
490 coord
= lp_build_abs(coord_bld
, coord
);
492 /* was: clamp to [-0.5, length + 0.5] then sub 0.5 */
493 /* skip clamp - always positive, and other side
494 only potentially matters for very large coords */
495 coord
= lp_build_sub(coord_bld
, coord
, half
);
497 /* convert to int, compute lerp weight */
498 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
499 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
512 *weight_out
= weight
;
517 * Build LLVM code for texture wrap mode for nearest filtering.
518 * \param coord the incoming texcoord (nominally in [0,1])
519 * \param length the texture size along one dimension, as int vector
520 * \param length_f the texture size along one dimension, as float vector
521 * \param offset texel offset along one dimension (as int vector)
522 * \param is_pot if TRUE, length is a power of two
523 * \param wrap_mode one of PIPE_TEX_WRAP_x
526 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
529 LLVMValueRef length_f
,
534 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
535 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
536 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
537 LLVMValueRef length_minus_one
= lp_build_sub(int_coord_bld
, length
, int_coord_bld
->one
);
541 case PIPE_TEX_WRAP_REPEAT
:
543 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
544 icoord
= lp_build_ifloor(coord_bld
, coord
);
546 icoord
= lp_build_add(int_coord_bld
, icoord
, offset
);
548 icoord
= LLVMBuildAnd(builder
, icoord
, length_minus_one
, "");
552 offset
= lp_build_int_to_float(coord_bld
, offset
);
553 offset
= lp_build_div(coord_bld
, offset
, length_f
);
554 coord
= lp_build_add(coord_bld
, coord
, offset
);
556 /* take fraction, unnormalize */
557 coord
= lp_build_fract_safe(coord_bld
, coord
);
558 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
559 icoord
= lp_build_itrunc(coord_bld
, coord
);
563 case PIPE_TEX_WRAP_CLAMP
:
564 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
565 if (bld
->static_sampler_state
->normalized_coords
) {
566 /* scale coord to length */
567 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
571 /* use itrunc instead since we clamp to 0 anyway */
572 icoord
= lp_build_itrunc(coord_bld
, coord
);
574 icoord
= lp_build_add(int_coord_bld
, icoord
, offset
);
577 /* clamp to [0, length - 1]. */
578 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
582 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
583 if (bld
->static_sampler_state
->normalized_coords
) {
584 /* scale coord to length */
585 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
587 /* no clamp necessary, border masking will handle this */
588 icoord
= lp_build_ifloor(coord_bld
, coord
);
590 icoord
= lp_build_add(int_coord_bld
, icoord
, offset
);
594 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
596 offset
= lp_build_int_to_float(coord_bld
, offset
);
597 offset
= lp_build_div(coord_bld
, offset
, length_f
);
598 coord
= lp_build_add(coord_bld
, coord
, offset
);
600 /* compute mirror function */
601 coord
= lp_build_coord_mirror(bld
, coord
);
603 /* scale coord to length */
604 assert(bld
->static_sampler_state
->normalized_coords
);
605 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
607 /* itrunc == ifloor here */
608 icoord
= lp_build_itrunc(coord_bld
, coord
);
610 /* clamp to [0, length - 1] */
611 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
614 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
615 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
616 if (bld
->static_sampler_state
->normalized_coords
) {
617 /* scale coord to length */
618 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
621 offset
= lp_build_int_to_float(coord_bld
, offset
);
622 coord
= lp_build_add(coord_bld
, coord
, offset
);
624 coord
= lp_build_abs(coord_bld
, coord
);
626 /* itrunc == ifloor here */
627 icoord
= lp_build_itrunc(coord_bld
, coord
);
629 /* clamp to [0, length - 1] */
630 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
633 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
634 if (bld
->static_sampler_state
->normalized_coords
) {
635 /* scale coord to length */
636 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
639 offset
= lp_build_int_to_float(coord_bld
, offset
);
640 coord
= lp_build_add(coord_bld
, coord
, offset
);
642 coord
= lp_build_abs(coord_bld
, coord
);
644 /* itrunc == ifloor here */
645 icoord
= lp_build_itrunc(coord_bld
, coord
);
658 * Do shadow test/comparison.
659 * \param p shadow ref value
660 * \param texel the texel to compare against
663 lp_build_sample_comparefunc(struct lp_build_sample_context
*bld
,
667 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
671 //lp_build_print_value(bld->gallivm, "shadow cmp coord", p);
672 lp_build_print_value(bld
->gallivm
, "shadow cmp texel", texel
);
675 /* result = (p FUNC texel) ? 1 : 0 */
677 * honor d3d10 floating point rules here, which state that comparisons
678 * are ordered except NOT_EQUAL which is unordered.
680 if (bld
->static_sampler_state
->compare_func
!= PIPE_FUNC_NOTEQUAL
) {
681 res
= lp_build_cmp_ordered(texel_bld
, bld
->static_sampler_state
->compare_func
,
685 res
= lp_build_cmp(texel_bld
, bld
->static_sampler_state
->compare_func
,
693 * Generate code to sample a mipmap level with nearest filtering.
694 * If sampling a cube texture, r = cube face in [0,5].
697 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
699 LLVMValueRef row_stride_vec
,
700 LLVMValueRef img_stride_vec
,
701 LLVMValueRef data_ptr
,
702 LLVMValueRef mipoffsets
,
703 LLVMValueRef
*coords
,
704 const LLVMValueRef
*offsets
,
705 LLVMValueRef colors_out
[4])
707 const unsigned dims
= bld
->dims
;
708 LLVMValueRef width_vec
;
709 LLVMValueRef height_vec
;
710 LLVMValueRef depth_vec
;
711 LLVMValueRef flt_size
;
712 LLVMValueRef flt_width_vec
;
713 LLVMValueRef flt_height_vec
;
714 LLVMValueRef flt_depth_vec
;
715 LLVMValueRef x
, y
= NULL
, z
= NULL
;
717 lp_build_extract_image_sizes(bld
,
721 &width_vec
, &height_vec
, &depth_vec
);
723 flt_size
= lp_build_int_to_float(&bld
->float_size_bld
, size
);
725 lp_build_extract_image_sizes(bld
,
726 &bld
->float_size_bld
,
729 &flt_width_vec
, &flt_height_vec
, &flt_depth_vec
);
732 * Compute integer texcoords.
734 x
= lp_build_sample_wrap_nearest(bld
, coords
[0], width_vec
,
735 flt_width_vec
, offsets
[0],
736 bld
->static_texture_state
->pot_width
,
737 bld
->static_sampler_state
->wrap_s
);
738 lp_build_name(x
, "tex.x.wrapped");
741 y
= lp_build_sample_wrap_nearest(bld
, coords
[1], height_vec
,
742 flt_height_vec
, offsets
[1],
743 bld
->static_texture_state
->pot_height
,
744 bld
->static_sampler_state
->wrap_t
);
745 lp_build_name(y
, "tex.y.wrapped");
748 z
= lp_build_sample_wrap_nearest(bld
, coords
[2], depth_vec
,
749 flt_depth_vec
, offsets
[2],
750 bld
->static_texture_state
->pot_depth
,
751 bld
->static_sampler_state
->wrap_r
);
752 lp_build_name(z
, "tex.z.wrapped");
755 if (bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE
||
756 bld
->static_texture_state
->target
== PIPE_TEXTURE_1D_ARRAY
||
757 bld
->static_texture_state
->target
== PIPE_TEXTURE_2D_ARRAY
) {
759 lp_build_name(z
, "tex.z.layer");
763 * Get texture colors.
765 lp_build_sample_texel_soa(bld
,
766 width_vec
, height_vec
, depth_vec
,
768 row_stride_vec
, img_stride_vec
,
769 data_ptr
, mipoffsets
, colors_out
);
771 if (bld
->static_sampler_state
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
773 cmpval
= lp_build_sample_comparefunc(bld
, coords
[4], colors_out
[0]);
774 /* this is really just a AND 1.0, cmpval but llvm is clever enough */
775 colors_out
[0] = lp_build_select(&bld
->texel_bld
, cmpval
,
776 bld
->texel_bld
.one
, bld
->texel_bld
.zero
);
777 colors_out
[1] = colors_out
[2] = colors_out
[3] = colors_out
[0];
784 * Like a lerp, but inputs are 0/~0 masks, so can simplify slightly.
787 lp_build_masklerp(struct lp_build_context
*bld
,
792 struct gallivm_state
*gallivm
= bld
->gallivm
;
793 LLVMBuilderRef builder
= gallivm
->builder
;
794 LLVMValueRef weight2
;
796 weight2
= lp_build_sub(bld
, bld
->one
, weight
);
797 weight
= LLVMBuildBitCast(builder
, weight
,
798 lp_build_int_vec_type(gallivm
, bld
->type
), "");
799 weight2
= LLVMBuildBitCast(builder
, weight2
,
800 lp_build_int_vec_type(gallivm
, bld
->type
), "");
801 weight
= LLVMBuildAnd(builder
, weight
, mask1
, "");
802 weight2
= LLVMBuildAnd(builder
, weight2
, mask0
, "");
803 weight
= LLVMBuildBitCast(builder
, weight
, bld
->vec_type
, "");
804 weight2
= LLVMBuildBitCast(builder
, weight2
, bld
->vec_type
, "");
805 return lp_build_add(bld
, weight
, weight2
);
809 * Like a 2d lerp, but inputs are 0/~0 masks, so can simplify slightly.
812 lp_build_masklerp2d(struct lp_build_context
*bld
,
813 LLVMValueRef weight0
,
814 LLVMValueRef weight1
,
820 LLVMValueRef val0
= lp_build_masklerp(bld
, weight0
, mask00
, mask01
);
821 LLVMValueRef val1
= lp_build_masklerp(bld
, weight0
, mask10
, mask11
);
822 return lp_build_lerp(bld
, weight1
, val0
, val1
, 0);
826 * Generate code to sample a mipmap level with linear filtering.
827 * If sampling a cube texture, r = cube face in [0,5].
828 * If linear_mask is present, only pixels having their mask set
829 * will receive linear filtering, the rest will use nearest.
832 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
834 LLVMValueRef linear_mask
,
835 LLVMValueRef row_stride_vec
,
836 LLVMValueRef img_stride_vec
,
837 LLVMValueRef data_ptr
,
838 LLVMValueRef mipoffsets
,
839 LLVMValueRef
*coords
,
840 const LLVMValueRef
*offsets
,
841 LLVMValueRef colors_out
[4])
843 const unsigned dims
= bld
->dims
;
844 LLVMValueRef width_vec
;
845 LLVMValueRef height_vec
;
846 LLVMValueRef depth_vec
;
847 LLVMValueRef flt_size
;
848 LLVMValueRef flt_width_vec
;
849 LLVMValueRef flt_height_vec
;
850 LLVMValueRef flt_depth_vec
;
851 LLVMValueRef z1
= NULL
;
852 LLVMValueRef z00
= NULL
, z01
= NULL
, z10
= NULL
, z11
= NULL
;
853 LLVMValueRef x00
= NULL
, x01
= NULL
, x10
= NULL
, x11
= NULL
;
854 LLVMValueRef y00
= NULL
, y01
= NULL
, y10
= NULL
, y11
= NULL
;
855 LLVMValueRef s_fpart
, t_fpart
= NULL
, r_fpart
= NULL
;
856 LLVMValueRef xs
[4], ys
[4], zs
[4];
857 LLVMValueRef neighbors
[2][2][4];
858 int chan
, texel_index
;
860 lp_build_extract_image_sizes(bld
,
864 &width_vec
, &height_vec
, &depth_vec
);
866 flt_size
= lp_build_int_to_float(&bld
->float_size_bld
, size
);
868 lp_build_extract_image_sizes(bld
,
869 &bld
->float_size_bld
,
872 &flt_width_vec
, &flt_height_vec
, &flt_depth_vec
);
875 * Compute integer texcoords.
878 if (bld
->static_texture_state
->target
!= PIPE_TEXTURE_CUBE
||
879 !bld
->static_sampler_state
->seamless_cube_map
) {
880 lp_build_sample_wrap_linear(bld
, coords
[0], width_vec
,
881 flt_width_vec
, offsets
[0],
882 bld
->static_texture_state
->pot_width
,
883 bld
->static_sampler_state
->wrap_s
,
884 &x00
, &x01
, &s_fpart
);
885 lp_build_name(x00
, "tex.x0.wrapped");
886 lp_build_name(x01
, "tex.x1.wrapped");
891 lp_build_sample_wrap_linear(bld
, coords
[1], height_vec
,
892 flt_height_vec
, offsets
[1],
893 bld
->static_texture_state
->pot_height
,
894 bld
->static_sampler_state
->wrap_t
,
895 &y00
, &y10
, &t_fpart
);
896 lp_build_name(y00
, "tex.y0.wrapped");
897 lp_build_name(y10
, "tex.y1.wrapped");
902 lp_build_sample_wrap_linear(bld
, coords
[2], depth_vec
,
903 flt_depth_vec
, offsets
[2],
904 bld
->static_texture_state
->pot_depth
,
905 bld
->static_sampler_state
->wrap_r
,
906 &z00
, &z1
, &r_fpart
);
907 z01
= z10
= z11
= z00
;
908 lp_build_name(z00
, "tex.z0.wrapped");
909 lp_build_name(z1
, "tex.z1.wrapped");
912 if (bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE
||
913 bld
->static_texture_state
->target
== PIPE_TEXTURE_1D_ARRAY
||
914 bld
->static_texture_state
->target
== PIPE_TEXTURE_2D_ARRAY
) {
915 z00
= z01
= z10
= z11
= z1
= coords
[2]; /* cube face or layer */
916 lp_build_name(z00
, "tex.z0.layer");
917 lp_build_name(z1
, "tex.z1.layer");
921 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
922 struct lp_build_context
*ivec_bld
= &bld
->int_coord_bld
;
923 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
924 struct lp_build_if_state edge_if
;
925 LLVMValueRef new_faces
[4], new_xcoords
[4][2], new_ycoords
[4][2];
926 LLVMValueRef fall_off
[4], coord
, have_edge
;
927 LLVMValueRef fall_off_ym_notxm
, fall_off_ym_notxp
;
928 LLVMValueRef fall_off_yp_notxm
, fall_off_yp_notxp
;
929 LLVMValueRef x0
, x1
, y0
, y1
, y0_clamped
, y1_clamped
;
930 LLVMValueRef face
= coords
[2];
931 LLVMValueRef half
= lp_build_const_vec(bld
->gallivm
, coord_bld
->type
, 0.5f
);
932 LLVMValueRef length_minus_one
= lp_build_sub(ivec_bld
, width_vec
, ivec_bld
->one
);
933 /* XXX drop height calcs. Could (should) do this without seamless filtering too */
934 height_vec
= width_vec
;
935 flt_height_vec
= flt_width_vec
;
937 /* XXX the overflow logic is actually sort of duplicated with trilinear,
938 * since an overflow in one mip should also have a corresponding overflow
941 /* should always have normalized coords, and offsets are undefined */
942 assert(bld
->static_sampler_state
->normalized_coords
);
943 coord
= lp_build_mul(coord_bld
, coords
[0], flt_width_vec
);
944 /* instead of clamp, build mask if overflowed */
945 coord
= lp_build_sub(coord_bld
, coord
, half
);
946 /* convert to int, compute lerp weight */
947 /* not ideal with AVX (and no AVX2) */
948 lp_build_ifloor_fract(coord_bld
, coord
, &x0
, &s_fpart
);
949 x1
= lp_build_add(ivec_bld
, x0
, ivec_bld
->one
);
950 coord
= lp_build_mul(coord_bld
, coords
[1], flt_height_vec
);
951 coord
= lp_build_sub(coord_bld
, coord
, half
);
952 lp_build_ifloor_fract(coord_bld
, coord
, &y0
, &t_fpart
);
953 y1
= lp_build_add(ivec_bld
, y0
, ivec_bld
->one
);
955 fall_off
[0] = lp_build_cmp(ivec_bld
, PIPE_FUNC_LESS
, x0
, ivec_bld
->zero
);
956 fall_off
[1] = lp_build_cmp(ivec_bld
, PIPE_FUNC_GREATER
, x1
, length_minus_one
);
957 fall_off
[2] = lp_build_cmp(ivec_bld
, PIPE_FUNC_LESS
, y0
, ivec_bld
->zero
);
958 fall_off
[3] = lp_build_cmp(ivec_bld
, PIPE_FUNC_GREATER
, y1
, length_minus_one
);
960 have_edge
= lp_build_or(ivec_bld
, fall_off
[0], fall_off
[1]);
961 have_edge
= lp_build_or(ivec_bld
, have_edge
, fall_off
[2]);
962 have_edge
= lp_build_or(ivec_bld
, have_edge
, fall_off
[3]);
964 have_edge
= lp_build_any_true_range(ivec_bld
, ivec_bld
->type
.length
, have_edge
);
966 for (texel_index
= 0; texel_index
< 4; texel_index
++) {
967 xs
[texel_index
] = lp_build_alloca(bld
->gallivm
, ivec_bld
->vec_type
, "xs");
968 ys
[texel_index
] = lp_build_alloca(bld
->gallivm
, ivec_bld
->vec_type
, "ys");
969 zs
[texel_index
] = lp_build_alloca(bld
->gallivm
, ivec_bld
->vec_type
, "zs");
972 lp_build_if(&edge_if
, bld
->gallivm
, have_edge
);
975 * Need to feed clamped values here for cheap corner handling,
976 * but only for y coord (as when falling off both edges we only
977 * fall off the x one) - this should be sufficient.
979 y0_clamped
= lp_build_max(ivec_bld
, y0
, ivec_bld
->zero
);
980 y1_clamped
= lp_build_min(ivec_bld
, y1
, length_minus_one
);
983 * Get all possible new coords.
985 lp_build_cube_new_coords(ivec_bld
, face
,
986 x0
, x1
, y0_clamped
, y1_clamped
,
988 new_faces
, new_xcoords
, new_ycoords
);
990 /* handle fall off x-, x+ direction */
991 /* determine new coords, face (not both fall_off vars can be true at same time) */
992 x00
= lp_build_select(ivec_bld
, fall_off
[0], new_xcoords
[0][0], x0
);
993 y00
= lp_build_select(ivec_bld
, fall_off
[0], new_ycoords
[0][0], y0_clamped
);
994 x10
= lp_build_select(ivec_bld
, fall_off
[0], new_xcoords
[0][1], x0
);
995 y10
= lp_build_select(ivec_bld
, fall_off
[0], new_ycoords
[0][1], y1_clamped
);
996 x01
= lp_build_select(ivec_bld
, fall_off
[1], new_xcoords
[1][0], x1
);
997 y01
= lp_build_select(ivec_bld
, fall_off
[1], new_ycoords
[1][0], y0_clamped
);
998 x11
= lp_build_select(ivec_bld
, fall_off
[1], new_xcoords
[1][1], x1
);
999 y11
= lp_build_select(ivec_bld
, fall_off
[1], new_ycoords
[1][1], y1_clamped
);
1001 z00
= z10
= lp_build_select(ivec_bld
, fall_off
[0], new_faces
[0], face
);
1002 z01
= z11
= lp_build_select(ivec_bld
, fall_off
[1], new_faces
[1], face
);
1004 /* handle fall off y-, y+ direction */
1006 * Cheap corner logic: just hack up things so a texel doesn't fall
1007 * off both sides (which means filter weights will be wrong but we'll only
1008 * use valid texels in the filter).
1009 * This means however (y) coords must additionally be clamped (see above).
1010 * This corner handling should be fully OpenGL (but not d3d10) compliant.
1012 fall_off_ym_notxm
= lp_build_andnot(ivec_bld
, fall_off
[2], fall_off
[0]);
1013 fall_off_ym_notxp
= lp_build_andnot(ivec_bld
, fall_off
[2], fall_off
[1]);
1014 fall_off_yp_notxm
= lp_build_andnot(ivec_bld
, fall_off
[3], fall_off
[0]);
1015 fall_off_yp_notxp
= lp_build_andnot(ivec_bld
, fall_off
[3], fall_off
[1]);
1017 x00
= lp_build_select(ivec_bld
, fall_off_ym_notxm
, new_xcoords
[2][0], x00
);
1018 y00
= lp_build_select(ivec_bld
, fall_off_ym_notxm
, new_ycoords
[2][0], y00
);
1019 x01
= lp_build_select(ivec_bld
, fall_off_ym_notxp
, new_xcoords
[2][1], x01
);
1020 y01
= lp_build_select(ivec_bld
, fall_off_ym_notxp
, new_ycoords
[2][1], y01
);
1021 x10
= lp_build_select(ivec_bld
, fall_off_yp_notxm
, new_xcoords
[3][0], x10
);
1022 y10
= lp_build_select(ivec_bld
, fall_off_yp_notxm
, new_ycoords
[3][0], y10
);
1023 x11
= lp_build_select(ivec_bld
, fall_off_yp_notxp
, new_xcoords
[3][1], x11
);
1024 y11
= lp_build_select(ivec_bld
, fall_off_yp_notxp
, new_ycoords
[3][1], y11
);
1026 z00
= lp_build_select(ivec_bld
, fall_off_ym_notxm
, new_faces
[2], z00
);
1027 z01
= lp_build_select(ivec_bld
, fall_off_ym_notxp
, new_faces
[2], z01
);
1028 z10
= lp_build_select(ivec_bld
, fall_off_yp_notxm
, new_faces
[3], z10
);
1029 z11
= lp_build_select(ivec_bld
, fall_off_yp_notxp
, new_faces
[3], z11
);
1031 LLVMBuildStore(builder
, x00
, xs
[0]);
1032 LLVMBuildStore(builder
, x01
, xs
[1]);
1033 LLVMBuildStore(builder
, x10
, xs
[2]);
1034 LLVMBuildStore(builder
, x11
, xs
[3]);
1035 LLVMBuildStore(builder
, y00
, ys
[0]);
1036 LLVMBuildStore(builder
, y01
, ys
[1]);
1037 LLVMBuildStore(builder
, y10
, ys
[2]);
1038 LLVMBuildStore(builder
, y11
, ys
[3]);
1039 LLVMBuildStore(builder
, z00
, zs
[0]);
1040 LLVMBuildStore(builder
, z01
, zs
[1]);
1041 LLVMBuildStore(builder
, z10
, zs
[2]);
1042 LLVMBuildStore(builder
, z11
, zs
[3]);
1044 lp_build_else(&edge_if
);
1046 LLVMBuildStore(builder
, x0
, xs
[0]);
1047 LLVMBuildStore(builder
, x1
, xs
[1]);
1048 LLVMBuildStore(builder
, x0
, xs
[2]);
1049 LLVMBuildStore(builder
, x1
, xs
[3]);
1050 LLVMBuildStore(builder
, y0
, ys
[0]);
1051 LLVMBuildStore(builder
, y0
, ys
[1]);
1052 LLVMBuildStore(builder
, y1
, ys
[2]);
1053 LLVMBuildStore(builder
, y1
, ys
[3]);
1054 LLVMBuildStore(builder
, face
, zs
[0]);
1055 LLVMBuildStore(builder
, face
, zs
[1]);
1056 LLVMBuildStore(builder
, face
, zs
[2]);
1057 LLVMBuildStore(builder
, face
, zs
[3]);
1059 lp_build_endif(&edge_if
);
1061 x00
= LLVMBuildLoad(builder
, xs
[0], "");
1062 x01
= LLVMBuildLoad(builder
, xs
[1], "");
1063 x10
= LLVMBuildLoad(builder
, xs
[2], "");
1064 x11
= LLVMBuildLoad(builder
, xs
[3], "");
1065 y00
= LLVMBuildLoad(builder
, ys
[0], "");
1066 y01
= LLVMBuildLoad(builder
, ys
[1], "");
1067 y10
= LLVMBuildLoad(builder
, ys
[2], "");
1068 y11
= LLVMBuildLoad(builder
, ys
[3], "");
1069 z00
= LLVMBuildLoad(builder
, zs
[0], "");
1070 z01
= LLVMBuildLoad(builder
, zs
[1], "");
1071 z10
= LLVMBuildLoad(builder
, zs
[2], "");
1072 z11
= LLVMBuildLoad(builder
, zs
[3], "");
1077 * Whack filter weights into place. Whatever pixel had more weight is
1078 * the one which should have been selected by nearest filtering hence
1079 * just use 100% weight for it.
1081 struct lp_build_context
*c_bld
= &bld
->coord_bld
;
1082 LLVMValueRef w1_mask
, w1_weight
;
1083 LLVMValueRef half
= lp_build_const_vec(bld
->gallivm
, c_bld
->type
, 0.5f
);
1085 w1_mask
= lp_build_cmp(c_bld
, PIPE_FUNC_GREATER
, s_fpart
, half
);
1086 /* this select is really just a "and" */
1087 w1_weight
= lp_build_select(c_bld
, w1_mask
, c_bld
->one
, c_bld
->zero
);
1088 s_fpart
= lp_build_select(c_bld
, linear_mask
, s_fpart
, w1_weight
);
1090 w1_mask
= lp_build_cmp(c_bld
, PIPE_FUNC_GREATER
, t_fpart
, half
);
1091 w1_weight
= lp_build_select(c_bld
, w1_mask
, c_bld
->one
, c_bld
->zero
);
1092 t_fpart
= lp_build_select(c_bld
, linear_mask
, t_fpart
, w1_weight
);
1094 w1_mask
= lp_build_cmp(c_bld
, PIPE_FUNC_GREATER
, r_fpart
, half
);
1095 w1_weight
= lp_build_select(c_bld
, w1_mask
, c_bld
->one
, c_bld
->zero
);
1096 r_fpart
= lp_build_select(c_bld
, linear_mask
, r_fpart
, w1_weight
);
1102 * Get texture colors.
1104 /* get x0/x1 texels */
1105 lp_build_sample_texel_soa(bld
,
1106 width_vec
, height_vec
, depth_vec
,
1108 row_stride_vec
, img_stride_vec
,
1109 data_ptr
, mipoffsets
, neighbors
[0][0]);
1110 lp_build_sample_texel_soa(bld
,
1111 width_vec
, height_vec
, depth_vec
,
1113 row_stride_vec
, img_stride_vec
,
1114 data_ptr
, mipoffsets
, neighbors
[0][1]);
1117 if (bld
->static_sampler_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
) {
1118 /* Interpolate two samples from 1D image to produce one color */
1119 for (chan
= 0; chan
< 4; chan
++) {
1120 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, s_fpart
,
1121 neighbors
[0][0][chan
],
1122 neighbors
[0][1][chan
],
1127 LLVMValueRef cmpval0
, cmpval1
;
1128 cmpval0
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[0][0][0]);
1129 cmpval1
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[0][1][0]);
1130 /* simplified lerp, AND mask with weight and add */
1131 colors_out
[0] = lp_build_masklerp(&bld
->texel_bld
, s_fpart
,
1133 colors_out
[1] = colors_out
[2] = colors_out
[3] = colors_out
[0];
1138 LLVMValueRef colors0
[4];
1140 /* get x0/x1 texels at y1 */
1141 lp_build_sample_texel_soa(bld
,
1142 width_vec
, height_vec
, depth_vec
,
1144 row_stride_vec
, img_stride_vec
,
1145 data_ptr
, mipoffsets
, neighbors
[1][0]);
1146 lp_build_sample_texel_soa(bld
,
1147 width_vec
, height_vec
, depth_vec
,
1149 row_stride_vec
, img_stride_vec
,
1150 data_ptr
, mipoffsets
, neighbors
[1][1]);
1152 if (bld
->static_sampler_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
) {
1153 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
1154 for (chan
= 0; chan
< 4; chan
++) {
1155 colors0
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1157 neighbors
[0][0][chan
],
1158 neighbors
[0][1][chan
],
1159 neighbors
[1][0][chan
],
1160 neighbors
[1][1][chan
],
1165 LLVMValueRef cmpval00
, cmpval01
, cmpval10
, cmpval11
;
1166 cmpval00
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[0][0][0]);
1167 cmpval01
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[0][1][0]);
1168 cmpval10
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[1][0][0]);
1169 cmpval11
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[1][1][0]);
1170 colors0
[0] = lp_build_masklerp2d(&bld
->texel_bld
, s_fpart
, t_fpart
,
1171 cmpval00
, cmpval01
, cmpval10
, cmpval11
);
1172 colors0
[1] = colors0
[2] = colors0
[3] = colors0
[0];
1176 LLVMValueRef neighbors1
[2][2][4];
1177 LLVMValueRef colors1
[4];
1179 /* get x0/x1/y0/y1 texels at z1 */
1180 lp_build_sample_texel_soa(bld
,
1181 width_vec
, height_vec
, depth_vec
,
1183 row_stride_vec
, img_stride_vec
,
1184 data_ptr
, mipoffsets
, neighbors1
[0][0]);
1185 lp_build_sample_texel_soa(bld
,
1186 width_vec
, height_vec
, depth_vec
,
1188 row_stride_vec
, img_stride_vec
,
1189 data_ptr
, mipoffsets
, neighbors1
[0][1]);
1190 lp_build_sample_texel_soa(bld
,
1191 width_vec
, height_vec
, depth_vec
,
1193 row_stride_vec
, img_stride_vec
,
1194 data_ptr
, mipoffsets
, neighbors1
[1][0]);
1195 lp_build_sample_texel_soa(bld
,
1196 width_vec
, height_vec
, depth_vec
,
1198 row_stride_vec
, img_stride_vec
,
1199 data_ptr
, mipoffsets
, neighbors1
[1][1]);
1201 if (bld
->static_sampler_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
) {
1202 /* Bilinear interpolate the four samples from the second Z slice */
1203 for (chan
= 0; chan
< 4; chan
++) {
1204 colors1
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1206 neighbors1
[0][0][chan
],
1207 neighbors1
[0][1][chan
],
1208 neighbors1
[1][0][chan
],
1209 neighbors1
[1][1][chan
],
1212 /* Linearly interpolate the two samples from the two 3D slices */
1213 for (chan
= 0; chan
< 4; chan
++) {
1214 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
,
1216 colors0
[chan
], colors1
[chan
],
1221 LLVMValueRef cmpval00
, cmpval01
, cmpval10
, cmpval11
;
1222 cmpval00
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[0][0][0]);
1223 cmpval01
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[0][1][0]);
1224 cmpval10
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[1][0][0]);
1225 cmpval11
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[1][1][0]);
1226 colors1
[0] = lp_build_masklerp2d(&bld
->texel_bld
, s_fpart
, t_fpart
,
1227 cmpval00
, cmpval01
, cmpval10
, cmpval11
);
1228 /* Linearly interpolate the two samples from the two 3D slices */
1229 colors_out
[0] = lp_build_lerp(&bld
->texel_bld
,
1231 colors0
[0], colors1
[0],
1233 colors_out
[1] = colors_out
[2] = colors_out
[3] = colors_out
[0];
1238 for (chan
= 0; chan
< 4; chan
++) {
1239 colors_out
[chan
] = colors0
[chan
];
1247 * Sample the texture/mipmap using given image filter and mip filter.
1248 * ilevel0 and ilevel1 indicate the two mipmap levels to sample
1249 * from (vectors or scalars).
1250 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1253 lp_build_sample_mipmap(struct lp_build_sample_context
*bld
,
1254 unsigned img_filter
,
1255 unsigned mip_filter
,
1256 LLVMValueRef
*coords
,
1257 const LLVMValueRef
*offsets
,
1258 LLVMValueRef ilevel0
,
1259 LLVMValueRef ilevel1
,
1260 LLVMValueRef lod_fpart
,
1261 LLVMValueRef
*colors_out
)
1263 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1264 LLVMValueRef size0
= NULL
;
1265 LLVMValueRef size1
= NULL
;
1266 LLVMValueRef row_stride0_vec
= NULL
;
1267 LLVMValueRef row_stride1_vec
= NULL
;
1268 LLVMValueRef img_stride0_vec
= NULL
;
1269 LLVMValueRef img_stride1_vec
= NULL
;
1270 LLVMValueRef data_ptr0
= NULL
;
1271 LLVMValueRef data_ptr1
= NULL
;
1272 LLVMValueRef mipoff0
= NULL
;
1273 LLVMValueRef mipoff1
= NULL
;
1274 LLVMValueRef colors0
[4], colors1
[4];
1277 /* sample the first mipmap level */
1278 lp_build_mipmap_level_sizes(bld
, ilevel0
,
1280 &row_stride0_vec
, &img_stride0_vec
);
1281 if (bld
->num_mips
== 1) {
1282 data_ptr0
= lp_build_get_mipmap_level(bld
, ilevel0
);
1285 /* This path should work for num_lods 1 too but slightly less efficient */
1286 data_ptr0
= bld
->base_ptr
;
1287 mipoff0
= lp_build_get_mip_offsets(bld
, ilevel0
);
1289 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
1290 lp_build_sample_image_nearest(bld
, size0
,
1291 row_stride0_vec
, img_stride0_vec
,
1292 data_ptr0
, mipoff0
, coords
, offsets
,
1296 assert(img_filter
== PIPE_TEX_FILTER_LINEAR
);
1297 lp_build_sample_image_linear(bld
, size0
, NULL
,
1298 row_stride0_vec
, img_stride0_vec
,
1299 data_ptr0
, mipoff0
, coords
, offsets
,
1303 /* Store the first level's colors in the output variables */
1304 for (chan
= 0; chan
< 4; chan
++) {
1305 LLVMBuildStore(builder
, colors0
[chan
], colors_out
[chan
]);
1308 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1309 struct lp_build_if_state if_ctx
;
1310 LLVMValueRef need_lerp
;
1312 /* need_lerp = lod_fpart > 0 */
1313 if (bld
->num_lods
== 1) {
1314 need_lerp
= LLVMBuildFCmp(builder
, LLVMRealUGT
,
1315 lod_fpart
, bld
->lodf_bld
.zero
,
1320 * We'll do mip filtering if any of the quads (or individual
1321 * pixel in case of per-pixel lod) need it.
1322 * It might be better to split the vectors here and only fetch/filter
1323 * quads which need it (if there's one lod per quad).
1325 need_lerp
= lp_build_compare(bld
->gallivm
, bld
->lodf_bld
.type
,
1327 lod_fpart
, bld
->lodf_bld
.zero
);
1328 need_lerp
= lp_build_any_true_range(&bld
->lodi_bld
, bld
->num_lods
, need_lerp
);
1331 lp_build_if(&if_ctx
, bld
->gallivm
, need_lerp
);
1334 * We unfortunately need to clamp lod_fpart here since we can get
1335 * negative values which would screw up filtering if not all
1336 * lod_fpart values have same sign.
1338 lod_fpart
= lp_build_max(&bld
->lodf_bld
, lod_fpart
,
1339 bld
->lodf_bld
.zero
);
1340 /* sample the second mipmap level */
1341 lp_build_mipmap_level_sizes(bld
, ilevel1
,
1343 &row_stride1_vec
, &img_stride1_vec
);
1344 if (bld
->num_mips
== 1) {
1345 data_ptr1
= lp_build_get_mipmap_level(bld
, ilevel1
);
1348 data_ptr1
= bld
->base_ptr
;
1349 mipoff1
= lp_build_get_mip_offsets(bld
, ilevel1
);
1351 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
1352 lp_build_sample_image_nearest(bld
, size1
,
1353 row_stride1_vec
, img_stride1_vec
,
1354 data_ptr1
, mipoff1
, coords
, offsets
,
1358 lp_build_sample_image_linear(bld
, size1
, NULL
,
1359 row_stride1_vec
, img_stride1_vec
,
1360 data_ptr1
, mipoff1
, coords
, offsets
,
1364 /* interpolate samples from the two mipmap levels */
1366 if (bld
->num_lods
!= bld
->coord_type
.length
)
1367 lod_fpart
= lp_build_unpack_broadcast_aos_scalars(bld
->gallivm
,
1369 bld
->texel_bld
.type
,
1372 for (chan
= 0; chan
< 4; chan
++) {
1373 colors0
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
1374 colors0
[chan
], colors1
[chan
],
1376 LLVMBuildStore(builder
, colors0
[chan
], colors_out
[chan
]);
1379 lp_build_endif(&if_ctx
);
1385 * Sample the texture/mipmap using given mip filter, and using
1386 * both nearest and linear filtering at the same time depending
1388 * lod can be per quad but linear_mask is always per pixel.
1389 * ilevel0 and ilevel1 indicate the two mipmap levels to sample
1390 * from (vectors or scalars).
1391 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1394 lp_build_sample_mipmap_both(struct lp_build_sample_context
*bld
,
1395 LLVMValueRef linear_mask
,
1396 unsigned mip_filter
,
1397 LLVMValueRef
*coords
,
1398 const LLVMValueRef
*offsets
,
1399 LLVMValueRef ilevel0
,
1400 LLVMValueRef ilevel1
,
1401 LLVMValueRef lod_fpart
,
1402 LLVMValueRef lod_positive
,
1403 LLVMValueRef
*colors_out
)
1405 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1406 LLVMValueRef size0
= NULL
;
1407 LLVMValueRef size1
= NULL
;
1408 LLVMValueRef row_stride0_vec
= NULL
;
1409 LLVMValueRef row_stride1_vec
= NULL
;
1410 LLVMValueRef img_stride0_vec
= NULL
;
1411 LLVMValueRef img_stride1_vec
= NULL
;
1412 LLVMValueRef data_ptr0
= NULL
;
1413 LLVMValueRef data_ptr1
= NULL
;
1414 LLVMValueRef mipoff0
= NULL
;
1415 LLVMValueRef mipoff1
= NULL
;
1416 LLVMValueRef colors0
[4], colors1
[4];
1419 /* sample the first mipmap level */
1420 lp_build_mipmap_level_sizes(bld
, ilevel0
,
1422 &row_stride0_vec
, &img_stride0_vec
);
1423 if (bld
->num_mips
== 1) {
1424 data_ptr0
= lp_build_get_mipmap_level(bld
, ilevel0
);
1427 /* This path should work for num_lods 1 too but slightly less efficient */
1428 data_ptr0
= bld
->base_ptr
;
1429 mipoff0
= lp_build_get_mip_offsets(bld
, ilevel0
);
1432 lp_build_sample_image_linear(bld
, size0
, linear_mask
,
1433 row_stride0_vec
, img_stride0_vec
,
1434 data_ptr0
, mipoff0
, coords
, offsets
,
1437 /* Store the first level's colors in the output variables */
1438 for (chan
= 0; chan
< 4; chan
++) {
1439 LLVMBuildStore(builder
, colors0
[chan
], colors_out
[chan
]);
1442 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1443 struct lp_build_if_state if_ctx
;
1444 LLVMValueRef need_lerp
;
1447 * We'll do mip filtering if any of the quads (or individual
1448 * pixel in case of per-pixel lod) need it.
1449 * Note using lod_positive here not lod_fpart since it may be the same
1450 * condition as that used in the outer "if" in the caller hence llvm
1451 * should be able to merge the branches in this case.
1453 need_lerp
= lp_build_any_true_range(&bld
->lodi_bld
, bld
->num_lods
, lod_positive
);
1455 lp_build_if(&if_ctx
, bld
->gallivm
, need_lerp
);
1458 * We unfortunately need to clamp lod_fpart here since we can get
1459 * negative values which would screw up filtering if not all
1460 * lod_fpart values have same sign.
1462 lod_fpart
= lp_build_max(&bld
->lodf_bld
, lod_fpart
,
1463 bld
->lodf_bld
.zero
);
1464 /* sample the second mipmap level */
1465 lp_build_mipmap_level_sizes(bld
, ilevel1
,
1467 &row_stride1_vec
, &img_stride1_vec
);
1468 if (bld
->num_mips
== 1) {
1469 data_ptr1
= lp_build_get_mipmap_level(bld
, ilevel1
);
1472 data_ptr1
= bld
->base_ptr
;
1473 mipoff1
= lp_build_get_mip_offsets(bld
, ilevel1
);
1476 lp_build_sample_image_linear(bld
, size1
, linear_mask
,
1477 row_stride1_vec
, img_stride1_vec
,
1478 data_ptr1
, mipoff1
, coords
, offsets
,
1481 /* interpolate samples from the two mipmap levels */
1483 if (bld
->num_lods
!= bld
->coord_type
.length
)
1484 lod_fpart
= lp_build_unpack_broadcast_aos_scalars(bld
->gallivm
,
1486 bld
->texel_bld
.type
,
1489 for (chan
= 0; chan
< 4; chan
++) {
1490 colors0
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
1491 colors0
[chan
], colors1
[chan
],
1493 LLVMBuildStore(builder
, colors0
[chan
], colors_out
[chan
]);
1496 lp_build_endif(&if_ctx
);
1502 * Build (per-coord) layer value.
1503 * Either clamp layer to valid values or fill in optional out_of_bounds
1504 * value and just return value unclamped.
1507 lp_build_layer_coord(struct lp_build_sample_context
*bld
,
1508 unsigned texture_unit
,
1510 LLVMValueRef
*out_of_bounds
)
1512 LLVMValueRef num_layers
;
1513 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
1515 num_layers
= bld
->dynamic_state
->depth(bld
->dynamic_state
,
1516 bld
->gallivm
, texture_unit
);
1518 if (out_of_bounds
) {
1519 LLVMValueRef out1
, out
;
1520 num_layers
= lp_build_broadcast_scalar(int_coord_bld
, num_layers
);
1521 out
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, layer
, int_coord_bld
->zero
);
1522 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, layer
, num_layers
);
1523 *out_of_bounds
= lp_build_or(int_coord_bld
, out
, out1
);
1527 LLVMValueRef maxlayer
;
1528 maxlayer
= lp_build_sub(&bld
->int_bld
, num_layers
, bld
->int_bld
.one
);
1529 maxlayer
= lp_build_broadcast_scalar(int_coord_bld
, maxlayer
);
1530 return lp_build_clamp(int_coord_bld
, layer
, int_coord_bld
->zero
, maxlayer
);
1536 * Calculate cube face, lod, mip levels.
1539 lp_build_sample_common(struct lp_build_sample_context
*bld
,
1540 unsigned texture_index
,
1541 unsigned sampler_index
,
1542 LLVMValueRef
*coords
,
1543 const struct lp_derivatives
*derivs
, /* optional */
1544 LLVMValueRef lod_bias
, /* optional */
1545 LLVMValueRef explicit_lod
, /* optional */
1546 LLVMValueRef
*lod_pos_or_zero
,
1547 LLVMValueRef
*lod_fpart
,
1548 LLVMValueRef
*ilevel0
,
1549 LLVMValueRef
*ilevel1
)
1551 const unsigned mip_filter
= bld
->static_sampler_state
->min_mip_filter
;
1552 const unsigned min_filter
= bld
->static_sampler_state
->min_img_filter
;
1553 const unsigned mag_filter
= bld
->static_sampler_state
->mag_img_filter
;
1554 const unsigned target
= bld
->static_texture_state
->target
;
1555 LLVMValueRef first_level
, cube_rho
= NULL
;
1556 LLVMValueRef lod_ipart
= NULL
;
1557 struct lp_derivatives cube_derivs
;
1560 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1561 mip_filter, min_filter, mag_filter);
1565 * Choose cube face, recompute texcoords for the chosen face and
1566 * compute rho here too (as it requires transform of derivatives).
1568 if (target
== PIPE_TEXTURE_CUBE
) {
1569 boolean need_derivs
;
1570 need_derivs
= ((min_filter
!= mag_filter
||
1571 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) &&
1572 !bld
->static_sampler_state
->min_max_lod_equal
&&
1574 lp_build_cube_lookup(bld
, coords
, derivs
, &cube_rho
, &cube_derivs
, need_derivs
);
1575 derivs
= &cube_derivs
;
1577 else if (target
== PIPE_TEXTURE_1D_ARRAY
||
1578 target
== PIPE_TEXTURE_2D_ARRAY
) {
1579 coords
[2] = lp_build_iround(&bld
->coord_bld
, coords
[2]);
1580 coords
[2] = lp_build_layer_coord(bld
, texture_index
, coords
[2], NULL
);
1583 if (bld
->static_sampler_state
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
1585 * Clamp p coords to [0,1] for fixed function depth texture format here.
1586 * Technically this is not entirely correct for unorm depth as the ref value
1587 * should be converted to the depth format (quantization!) and comparison
1588 * then done in texture format. This would actually help performance (since
1589 * only need to do it once and could save the per-sample conversion of texels
1590 * to floats instead), but it would need more messy code (would need to push
1591 * at least some bits down to actual fetch so conversion could be skipped,
1592 * and would have ugly interaction with border color, would need to convert
1593 * border color to that format too or do some other tricks to make it work).
1595 const struct util_format_description
*format_desc
= bld
->format_desc
;
1597 /* not entirely sure we couldn't end up with non-valid swizzle here */
1598 chan_type
= format_desc
->swizzle
[0] <= UTIL_FORMAT_SWIZZLE_W
?
1599 format_desc
->channel
[format_desc
->swizzle
[0]].type
:
1600 UTIL_FORMAT_TYPE_FLOAT
;
1601 if (chan_type
!= UTIL_FORMAT_TYPE_FLOAT
) {
1602 coords
[4] = lp_build_clamp(&bld
->coord_bld
, coords
[4],
1603 bld
->coord_bld
.zero
, bld
->coord_bld
.one
);
1608 * Compute the level of detail (float).
1610 if (min_filter
!= mag_filter
||
1611 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
1612 /* Need to compute lod either to choose mipmap levels or to
1613 * distinguish between minification/magnification with one mipmap level.
1615 lp_build_lod_selector(bld
, texture_index
, sampler_index
,
1616 coords
[0], coords
[1], coords
[2], cube_rho
,
1617 derivs
, lod_bias
, explicit_lod
,
1619 &lod_ipart
, lod_fpart
, lod_pos_or_zero
);
1621 lod_ipart
= bld
->lodi_bld
.zero
;
1622 *lod_pos_or_zero
= bld
->lodi_bld
.zero
;
1625 if (bld
->num_lods
!= bld
->num_mips
) {
1626 /* only makes sense if there's just a single mip level */
1627 assert(bld
->num_mips
== 1);
1628 lod_ipart
= lp_build_extract_range(bld
->gallivm
, lod_ipart
, 0, 1);
1632 * Compute integer mipmap level(s) to fetch texels from: ilevel0, ilevel1
1634 switch (mip_filter
) {
1636 assert(0 && "bad mip_filter value in lp_build_sample_soa()");
1638 case PIPE_TEX_MIPFILTER_NONE
:
1639 /* always use mip level 0 */
1640 if (HAVE_LLVM
== 0x0207 && target
== PIPE_TEXTURE_CUBE
) {
1641 /* XXX this is a work-around for an apparent bug in LLVM 2.7.
1642 * We should be able to set ilevel0 = const(0) but that causes
1643 * bad x86 code to be emitted.
1646 lp_build_nearest_mip_level(bld
, texture_index
, lod_ipart
, ilevel0
, NULL
);
1649 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
1650 bld
->gallivm
, texture_index
);
1651 first_level
= lp_build_broadcast_scalar(&bld
->leveli_bld
, first_level
);
1652 *ilevel0
= first_level
;
1655 case PIPE_TEX_MIPFILTER_NEAREST
:
1657 lp_build_nearest_mip_level(bld
, texture_index
, lod_ipart
, ilevel0
, NULL
);
1659 case PIPE_TEX_MIPFILTER_LINEAR
:
1662 lp_build_linear_mip_levels(bld
, texture_index
,
1663 lod_ipart
, lod_fpart
,
1670 lp_build_clamp_border_color(struct lp_build_sample_context
*bld
,
1671 unsigned sampler_unit
)
1673 struct gallivm_state
*gallivm
= bld
->gallivm
;
1674 LLVMBuilderRef builder
= gallivm
->builder
;
1675 LLVMValueRef border_color_ptr
=
1676 bld
->dynamic_state
->border_color(bld
->dynamic_state
,
1677 gallivm
, sampler_unit
);
1678 LLVMValueRef border_color
;
1679 const struct util_format_description
*format_desc
= bld
->format_desc
;
1680 struct lp_type vec4_type
= bld
->texel_type
;
1681 struct lp_build_context vec4_bld
;
1682 LLVMValueRef min_clamp
= NULL
;
1683 LLVMValueRef max_clamp
= NULL
;
1686 * For normalized format need to clamp border color (technically
1687 * probably should also quantize the data). Really sucks doing this
1688 * here but can't avoid at least for now since this is part of
1689 * sampler state and texture format is part of sampler_view state.
1690 * GL expects also expects clamping for uint/sint formats too so
1691 * do that as well (d3d10 can't end up here with uint/sint since it
1692 * only supports them with ld).
1694 vec4_type
.length
= 4;
1695 lp_build_context_init(&vec4_bld
, gallivm
, vec4_type
);
1698 * Vectorized clamping of border color. Loading is a bit of a hack since
1699 * we just cast the pointer to float array to pointer to vec4
1702 border_color_ptr
= lp_build_array_get_ptr(gallivm
, border_color_ptr
,
1703 lp_build_const_int32(gallivm
, 0));
1704 border_color_ptr
= LLVMBuildBitCast(builder
, border_color_ptr
,
1705 LLVMPointerType(vec4_bld
.vec_type
, 0), "");
1706 border_color
= LLVMBuildLoad(builder
, border_color_ptr
, "");
1707 /* we don't have aligned type in the dynamic state unfortunately */
1708 lp_set_load_alignment(border_color
, 4);
1711 * Instead of having some incredibly complex logic which will try to figure out
1712 * clamping necessary for each channel, simply use the first channel, and treat
1713 * mixed signed/unsigned normalized formats specially.
1714 * (Mixed non-normalized, which wouldn't work at all here, do not exist for a
1717 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
) {
1719 /* d/s needs special handling because both present means just sampling depth */
1720 if (util_format_is_depth_and_stencil(format_desc
->format
)) {
1721 chan
= format_desc
->swizzle
[0];
1724 chan
= util_format_get_first_non_void_channel(format_desc
->format
);
1726 if (chan
>= 0 && chan
<= UTIL_FORMAT_SWIZZLE_W
) {
1727 unsigned chan_type
= format_desc
->channel
[chan
].type
;
1728 unsigned chan_norm
= format_desc
->channel
[chan
].normalized
;
1729 unsigned chan_pure
= format_desc
->channel
[chan
].pure_integer
;
1730 if (chan_type
== UTIL_FORMAT_TYPE_SIGNED
) {
1732 min_clamp
= lp_build_const_vec(gallivm
, vec4_type
, -1.0F
);
1733 max_clamp
= vec4_bld
.one
;
1735 else if (chan_pure
) {
1737 * Border color was stored as int, hence need min/max clamp
1738 * only if chan has less than 32 bits..
1740 unsigned chan_size
= format_desc
->channel
[chan
].size
;
1741 if (chan_size
< 32) {
1742 min_clamp
= lp_build_const_int_vec(gallivm
, vec4_type
,
1743 0 - (1 << (chan_size
- 1)));
1744 max_clamp
= lp_build_const_int_vec(gallivm
, vec4_type
,
1745 (1 << (chan_size
- 1)) - 1);
1748 /* TODO: no idea about non-pure, non-normalized! */
1750 else if (chan_type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
1752 min_clamp
= vec4_bld
.zero
;
1753 max_clamp
= vec4_bld
.one
;
1756 * Need a ugly hack here, because we don't have Z32_FLOAT_X8X24
1757 * we use Z32_FLOAT_S8X24 to imply sampling depth component
1758 * and ignoring stencil, which will blow up here if we try to
1759 * do a uint clamp in a float texel build...
1760 * And even if we had that format, mesa st also thinks using z24s8
1761 * means depth sampling ignoring stencil.
1763 else if (chan_pure
) {
1765 * Border color was stored as uint, hence never need min
1766 * clamp, and only need max clamp if chan has less than 32 bits.
1768 unsigned chan_size
= format_desc
->channel
[chan
].size
;
1769 if (chan_size
< 32) {
1770 max_clamp
= lp_build_const_int_vec(gallivm
, vec4_type
,
1771 (1 << chan_size
) - 1);
1773 /* TODO: no idea about non-pure, non-normalized! */
1776 else if (chan_type
== UTIL_FORMAT_TYPE_FIXED
) {
1777 /* TODO: I have no idea what clamp this would need if any! */
1780 /* mixed plain formats (or different pure size) */
1781 switch (format_desc
->format
) {
1782 case PIPE_FORMAT_B10G10R10A2_UINT
:
1783 case PIPE_FORMAT_R10G10B10A2_UINT
:
1785 unsigned max10
= (1 << 10) - 1;
1786 max_clamp
= lp_build_const_aos(gallivm
, vec4_type
, max10
, max10
,
1787 max10
, (1 << 2) - 1, NULL
);
1790 case PIPE_FORMAT_R10SG10SB10SA2U_NORM
:
1791 min_clamp
= lp_build_const_aos(gallivm
, vec4_type
, -1.0F
, -1.0F
,
1793 max_clamp
= vec4_bld
.one
;
1795 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
1796 case PIPE_FORMAT_R5SG5SB6U_NORM
:
1797 min_clamp
= lp_build_const_aos(gallivm
, vec4_type
, -1.0F
, -1.0F
,
1799 max_clamp
= vec4_bld
.one
;
1806 /* cannot figure this out from format description */
1807 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
1808 /* s3tc formats are always unorm */
1809 min_clamp
= vec4_bld
.zero
;
1810 max_clamp
= vec4_bld
.one
;
1812 else if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
||
1813 format_desc
->layout
== UTIL_FORMAT_LAYOUT_ETC
) {
1814 switch (format_desc
->format
) {
1815 case PIPE_FORMAT_RGTC1_UNORM
:
1816 case PIPE_FORMAT_RGTC2_UNORM
:
1817 case PIPE_FORMAT_LATC1_UNORM
:
1818 case PIPE_FORMAT_LATC2_UNORM
:
1819 case PIPE_FORMAT_ETC1_RGB8
:
1820 min_clamp
= vec4_bld
.zero
;
1821 max_clamp
= vec4_bld
.one
;
1823 case PIPE_FORMAT_RGTC1_SNORM
:
1824 case PIPE_FORMAT_RGTC2_SNORM
:
1825 case PIPE_FORMAT_LATC1_SNORM
:
1826 case PIPE_FORMAT_LATC2_SNORM
:
1827 min_clamp
= lp_build_const_vec(gallivm
, vec4_type
, -1.0F
);
1828 max_clamp
= vec4_bld
.one
;
1836 * all others from subsampled/other group, though we don't care
1837 * about yuv (and should not have any from zs here)
1839 else if (format_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_YUV
){
1840 switch (format_desc
->format
) {
1841 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
1842 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
1843 case PIPE_FORMAT_G8R8_B8R8_UNORM
:
1844 case PIPE_FORMAT_R8G8_R8B8_UNORM
:
1845 case PIPE_FORMAT_R1_UNORM
: /* doesn't make sense but ah well */
1846 min_clamp
= vec4_bld
.zero
;
1847 max_clamp
= vec4_bld
.one
;
1849 case PIPE_FORMAT_R8G8Bx_SNORM
:
1850 min_clamp
= lp_build_const_vec(gallivm
, vec4_type
, -1.0F
);
1851 max_clamp
= vec4_bld
.one
;
1854 * Note smallfloat formats usually don't need clamping
1855 * (they still have infinite range) however this is not
1856 * true for r11g11b10 and r9g9b9e5, which can't represent
1857 * negative numbers (and additionally r9g9b9e5 can't represent
1858 * very large numbers). d3d10 seems happy without clamping in
1859 * this case, but gl spec is pretty clear: "for floating
1860 * point and integer formats, border values are clamped to
1861 * the representable range of the format" so do that here.
1863 case PIPE_FORMAT_R11G11B10_FLOAT
:
1864 min_clamp
= vec4_bld
.zero
;
1866 case PIPE_FORMAT_R9G9B9E5_FLOAT
:
1867 min_clamp
= vec4_bld
.zero
;
1868 max_clamp
= lp_build_const_vec(gallivm
, vec4_type
, MAX_RGB9E5
);
1878 border_color
= lp_build_max(&vec4_bld
, border_color
, min_clamp
);
1881 border_color
= lp_build_min(&vec4_bld
, border_color
, max_clamp
);
1884 bld
->border_color_clamped
= border_color
;
1889 * General texture sampling codegen.
1890 * This function handles texture sampling for all texture targets (1D,
1891 * 2D, 3D, cube) and all filtering modes.
1894 lp_build_sample_general(struct lp_build_sample_context
*bld
,
1895 unsigned sampler_unit
,
1896 LLVMValueRef
*coords
,
1897 const LLVMValueRef
*offsets
,
1898 LLVMValueRef lod_positive
,
1899 LLVMValueRef lod_fpart
,
1900 LLVMValueRef ilevel0
,
1901 LLVMValueRef ilevel1
,
1902 LLVMValueRef
*colors_out
)
1904 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1905 const struct lp_static_sampler_state
*sampler_state
= bld
->static_sampler_state
;
1906 const unsigned mip_filter
= sampler_state
->min_mip_filter
;
1907 const unsigned min_filter
= sampler_state
->min_img_filter
;
1908 const unsigned mag_filter
= sampler_state
->mag_img_filter
;
1909 LLVMValueRef texels
[4];
1912 /* if we need border color, (potentially) clamp it now */
1913 if (lp_sampler_wrap_mode_uses_border_color(sampler_state
->wrap_s
,
1917 lp_sampler_wrap_mode_uses_border_color(sampler_state
->wrap_t
,
1921 lp_sampler_wrap_mode_uses_border_color(sampler_state
->wrap_r
,
1924 lp_build_clamp_border_color(bld
, sampler_unit
);
1929 * Get/interpolate texture colors.
1932 for (chan
= 0; chan
< 4; ++chan
) {
1933 texels
[chan
] = lp_build_alloca(bld
->gallivm
, bld
->texel_bld
.vec_type
, "");
1934 lp_build_name(texels
[chan
], "sampler%u_texel_%c_var", sampler_unit
, "xyzw"[chan
]);
1937 if (min_filter
== mag_filter
) {
1938 /* no need to distinguish between minification and magnification */
1939 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
,
1941 ilevel0
, ilevel1
, lod_fpart
,
1946 * Could also get rid of the if-logic and always use mipmap_both, both
1947 * for the single lod and multi-lod case if nothing really uses this.
1949 if (bld
->num_lods
== 1) {
1950 /* Emit conditional to choose min image filter or mag image filter
1951 * depending on the lod being > 0 or <= 0, respectively.
1953 struct lp_build_if_state if_ctx
;
1955 lod_positive
= LLVMBuildTrunc(builder
, lod_positive
,
1956 LLVMInt1TypeInContext(bld
->gallivm
->context
), "");
1958 lp_build_if(&if_ctx
, bld
->gallivm
, lod_positive
);
1960 /* Use the minification filter */
1961 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
,
1963 ilevel0
, ilevel1
, lod_fpart
,
1966 lp_build_else(&if_ctx
);
1968 /* Use the magnification filter */
1969 lp_build_sample_mipmap(bld
, mag_filter
, PIPE_TEX_MIPFILTER_NONE
,
1971 ilevel0
, NULL
, NULL
,
1974 lp_build_endif(&if_ctx
);
1977 LLVMValueRef need_linear
, linear_mask
;
1978 unsigned mip_filter_for_nearest
;
1979 struct lp_build_if_state if_ctx
;
1981 if (min_filter
== PIPE_TEX_FILTER_LINEAR
) {
1982 linear_mask
= lod_positive
;
1983 mip_filter_for_nearest
= PIPE_TEX_MIPFILTER_NONE
;
1986 linear_mask
= lp_build_not(&bld
->lodi_bld
, lod_positive
);
1987 mip_filter_for_nearest
= mip_filter
;
1989 need_linear
= lp_build_any_true_range(&bld
->lodi_bld
, bld
->num_lods
,
1992 if (bld
->num_lods
!= bld
->coord_type
.length
) {
1993 linear_mask
= lp_build_unpack_broadcast_aos_scalars(bld
->gallivm
,
1995 bld
->int_coord_type
,
1999 lp_build_if(&if_ctx
, bld
->gallivm
, need_linear
);
2002 * Do sampling with both filters simultaneously. This means using
2003 * a linear filter and doing some tricks (with weights) for the pixels
2004 * which need nearest filter.
2005 * Note that it's probably rare some pixels need nearest and some
2006 * linear filter but the fixups required for the nearest pixels
2007 * aren't all that complicated so just always run a combined path
2008 * if at least some pixels require linear.
2010 lp_build_sample_mipmap_both(bld
, linear_mask
, mip_filter
,
2013 lod_fpart
, lod_positive
,
2016 lp_build_else(&if_ctx
);
2019 * All pixels require just nearest filtering, which is way
2020 * cheaper than linear, hence do a separate path for that.
2022 lp_build_sample_mipmap(bld
, PIPE_TEX_FILTER_NEAREST
,
2023 mip_filter_for_nearest
,
2025 ilevel0
, ilevel1
, lod_fpart
,
2028 lp_build_endif(&if_ctx
);
2032 for (chan
= 0; chan
< 4; ++chan
) {
2033 colors_out
[chan
] = LLVMBuildLoad(builder
, texels
[chan
], "");
2034 lp_build_name(colors_out
[chan
], "sampler%u_texel_%c", sampler_unit
, "xyzw"[chan
]);
2040 * Texel fetch function.
2041 * In contrast to general sampling there is no filtering, no coord minification,
2042 * lod (if any) is always explicit uint, coords are uints (in terms of texel units)
2043 * directly to be applied to the selected mip level (after adding texel offsets).
2044 * This function handles texel fetch for all targets where texel fetch is supported
2045 * (no cube maps, but 1d, 2d, 3d are supported, arrays and buffers should be too).
2048 lp_build_fetch_texel(struct lp_build_sample_context
*bld
,
2049 unsigned texture_unit
,
2050 const LLVMValueRef
*coords
,
2051 LLVMValueRef explicit_lod
,
2052 const LLVMValueRef
*offsets
,
2053 LLVMValueRef
*colors_out
)
2055 struct lp_build_context
*perquadi_bld
= &bld
->lodi_bld
;
2056 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
2057 unsigned dims
= bld
->dims
, chan
;
2058 unsigned target
= bld
->static_texture_state
->target
;
2059 boolean out_of_bound_ret_zero
= TRUE
;
2060 LLVMValueRef size
, ilevel
;
2061 LLVMValueRef row_stride_vec
= NULL
, img_stride_vec
= NULL
;
2062 LLVMValueRef x
= coords
[0], y
= coords
[1], z
= coords
[2];
2063 LLVMValueRef width
, height
, depth
, i
, j
;
2064 LLVMValueRef offset
, out_of_bounds
, out1
;
2066 out_of_bounds
= int_coord_bld
->zero
;
2068 if (explicit_lod
&& bld
->static_texture_state
->target
!= PIPE_BUFFER
) {
2069 if (bld
->num_mips
!= int_coord_bld
->type
.length
) {
2070 ilevel
= lp_build_pack_aos_scalars(bld
->gallivm
, int_coord_bld
->type
,
2071 perquadi_bld
->type
, explicit_lod
, 0);
2074 ilevel
= explicit_lod
;
2076 lp_build_nearest_mip_level(bld
, texture_unit
, ilevel
, &ilevel
,
2077 out_of_bound_ret_zero
? &out_of_bounds
: NULL
);
2080 assert(bld
->num_mips
== 1);
2081 if (bld
->static_texture_state
->target
!= PIPE_BUFFER
) {
2082 ilevel
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
2083 bld
->gallivm
, texture_unit
);
2086 ilevel
= lp_build_const_int32(bld
->gallivm
, 0);
2089 lp_build_mipmap_level_sizes(bld
, ilevel
,
2091 &row_stride_vec
, &img_stride_vec
);
2092 lp_build_extract_image_sizes(bld
, &bld
->int_size_bld
, int_coord_bld
->type
,
2093 size
, &width
, &height
, &depth
);
2095 if (target
== PIPE_TEXTURE_1D_ARRAY
||
2096 target
== PIPE_TEXTURE_2D_ARRAY
) {
2097 if (out_of_bound_ret_zero
) {
2098 z
= lp_build_layer_coord(bld
, texture_unit
, z
, &out1
);
2099 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
2102 z
= lp_build_layer_coord(bld
, texture_unit
, z
, NULL
);
2106 /* This is a lot like border sampling */
2109 * coords are really unsigned, offsets are signed, but I don't think
2110 * exceeding 31 bits is possible
2112 x
= lp_build_add(int_coord_bld
, x
, offsets
[0]);
2114 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
2115 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
2116 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
2117 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
2121 y
= lp_build_add(int_coord_bld
, y
, offsets
[1]);
2123 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
2124 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
2125 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
2126 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
2130 z
= lp_build_add(int_coord_bld
, z
, offsets
[2]);
2132 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
2133 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
2134 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
2135 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
2139 lp_build_sample_offset(int_coord_bld
,
2141 x
, y
, z
, row_stride_vec
, img_stride_vec
,
2144 if (bld
->static_texture_state
->target
!= PIPE_BUFFER
) {
2145 offset
= lp_build_add(int_coord_bld
, offset
,
2146 lp_build_get_mip_offsets(bld
, ilevel
));
2149 offset
= lp_build_andnot(int_coord_bld
, offset
, out_of_bounds
);
2151 lp_build_fetch_rgba_soa(bld
->gallivm
,
2154 bld
->base_ptr
, offset
,
2158 if (out_of_bound_ret_zero
) {
2160 * Only needed for ARB_robust_buffer_access_behavior and d3d10.
2161 * Could use min/max above instead of out-of-bounds comparisons
2162 * if we don't care about the result returned for out-of-bounds.
2164 for (chan
= 0; chan
< 4; chan
++) {
2165 colors_out
[chan
] = lp_build_select(&bld
->texel_bld
, out_of_bounds
,
2166 bld
->texel_bld
.zero
, colors_out
[chan
]);
2173 * Just set texels to white instead of actually sampling the texture.
2177 lp_build_sample_nop(struct gallivm_state
*gallivm
,
2178 struct lp_type type
,
2179 const LLVMValueRef
*coords
,
2180 LLVMValueRef texel_out
[4])
2182 LLVMValueRef one
= lp_build_one(gallivm
, type
);
2185 for (chan
= 0; chan
< 4; chan
++) {
2186 texel_out
[chan
] = one
;
2192 * Build texture sampling code.
2193 * 'texel' will return a vector of four LLVMValueRefs corresponding to
2195 * \param type vector float type to use for coords, etc.
2196 * \param is_fetch if this is a texel fetch instruction.
2197 * \param derivs partial derivatives of (s,t,r,q) with respect to x and y
2200 lp_build_sample_soa(struct gallivm_state
*gallivm
,
2201 const struct lp_static_texture_state
*static_texture_state
,
2202 const struct lp_static_sampler_state
*static_sampler_state
,
2203 struct lp_sampler_dynamic_state
*dynamic_state
,
2204 struct lp_type type
,
2206 unsigned texture_index
,
2207 unsigned sampler_index
,
2208 const LLVMValueRef
*coords
,
2209 const LLVMValueRef
*offsets
,
2210 const struct lp_derivatives
*derivs
, /* optional */
2211 LLVMValueRef lod_bias
, /* optional */
2212 LLVMValueRef explicit_lod
, /* optional */
2213 enum lp_sampler_lod_property lod_property
,
2214 LLVMValueRef texel_out
[4])
2216 unsigned target
= static_texture_state
->target
;
2217 unsigned dims
= texture_dims(target
);
2218 unsigned num_quads
= type
.length
/ 4;
2219 unsigned mip_filter
, min_img_filter
, mag_img_filter
, i
;
2220 struct lp_build_sample_context bld
;
2221 struct lp_static_sampler_state derived_sampler_state
= *static_sampler_state
;
2222 LLVMTypeRef i32t
= LLVMInt32TypeInContext(gallivm
->context
);
2223 LLVMBuilderRef builder
= gallivm
->builder
;
2224 LLVMValueRef tex_width
, newcoords
[5];
2227 enum pipe_format fmt
= static_texture_state
->format
;
2228 debug_printf("Sample from %s\n", util_format_name(fmt
));
2231 if (static_texture_state
->format
== PIPE_FORMAT_NONE
) {
2233 * If there's nothing bound, format is NONE, and we must return
2234 * all zero as mandated by d3d10 in this case.
2237 LLVMValueRef zero
= lp_build_const_vec(gallivm
, type
, 0.0F
);
2238 for (chan
= 0; chan
< 4; chan
++) {
2239 texel_out
[chan
] = zero
;
2244 assert(type
.floating
);
2246 /* Setup our build context */
2247 memset(&bld
, 0, sizeof bld
);
2248 bld
.gallivm
= gallivm
;
2249 bld
.static_sampler_state
= &derived_sampler_state
;
2250 bld
.static_texture_state
= static_texture_state
;
2251 bld
.dynamic_state
= dynamic_state
;
2252 bld
.format_desc
= util_format_description(static_texture_state
->format
);
2255 bld
.vector_width
= lp_type_width(type
);
2257 bld
.float_type
= lp_type_float(32);
2258 bld
.int_type
= lp_type_int(32);
2259 bld
.coord_type
= type
;
2260 bld
.int_coord_type
= lp_int_type(type
);
2261 bld
.float_size_in_type
= lp_type_float(32);
2262 bld
.float_size_in_type
.length
= dims
> 1 ? 4 : 1;
2263 bld
.int_size_in_type
= lp_int_type(bld
.float_size_in_type
);
2264 bld
.texel_type
= type
;
2266 /* always using the first channel hopefully should be safe,
2267 * if not things WILL break in other places anyway.
2269 if (bld
.format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
&&
2270 bld
.format_desc
->channel
[0].pure_integer
) {
2271 if (bld
.format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_SIGNED
) {
2272 bld
.texel_type
= lp_type_int_vec(type
.width
, type
.width
* type
.length
);
2274 else if (bld
.format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
2275 bld
.texel_type
= lp_type_uint_vec(type
.width
, type
.width
* type
.length
);
2278 else if (util_format_has_stencil(bld
.format_desc
) &&
2279 !util_format_has_depth(bld
.format_desc
)) {
2280 /* for stencil only formats, sample stencil (uint) */
2281 bld
.texel_type
= lp_type_int_vec(type
.width
, type
.width
* type
.length
);
2284 if (!static_texture_state
->level_zero_only
) {
2285 derived_sampler_state
.min_mip_filter
= static_sampler_state
->min_mip_filter
;
2287 derived_sampler_state
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
2289 mip_filter
= derived_sampler_state
.min_mip_filter
;
2292 debug_printf(" .min_mip_filter = %u\n", derived_sampler_state
.min_mip_filter
);
2295 if (static_texture_state
->target
== PIPE_TEXTURE_CUBE
||
2296 static_texture_state
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
2299 * Seamless filtering ignores wrap modes.
2300 * Setting to CLAMP_TO_EDGE is correct for nearest filtering, for
2301 * bilinear it's not correct but way better than using for instance repeat.
2302 * Note we even set this for non-seamless. Technically GL allows any wrap
2303 * mode, which made sense when supporting true borders (can get seamless
2304 * effect with border and CLAMP_TO_BORDER), but gallium doesn't support
2305 * borders and d3d9 requires wrap modes to be ignored and it's a pain to fix
2306 * up the sampler state (as it makes it texture dependent).
2308 derived_sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
2309 derived_sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
2312 min_img_filter
= derived_sampler_state
.min_img_filter
;
2313 mag_img_filter
= derived_sampler_state
.mag_img_filter
;
2317 * This is all a bit complicated different paths are chosen for performance
2319 * Essentially, there can be 1 lod per element, 1 lod per quad or 1 lod for
2320 * everything (the last two options are equivalent for 4-wide case).
2321 * If there's per-quad lod but we split to 4-wide so we can use AoS, per-quad
2322 * lod is calculated then the lod value extracted afterwards so making this
2323 * case basically the same as far as lod handling is concerned for the
2324 * further sample/filter code as the 1 lod for everything case.
2325 * Different lod handling mostly shows up when building mipmap sizes
2326 * (lp_build_mipmap_level_sizes() and friends) and also in filtering
2327 * (getting the fractional part of the lod to the right texels).
2331 * There are other situations where at least the multiple int lods could be
2332 * avoided like min and max lod being equal.
2334 bld
.num_mips
= bld
.num_lods
= 1;
2336 if ((gallivm_debug
& GALLIVM_DEBUG_NO_QUAD_LOD
) &&
2337 (gallivm_debug
& GALLIVM_DEBUG_NO_RHO_APPROX
) &&
2338 (static_texture_state
->target
== PIPE_TEXTURE_CUBE
) &&
2339 (!is_fetch
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)) {
2341 * special case for using per-pixel lod even for implicit lod,
2342 * which is generally never required (ok by APIs) except to please
2343 * some (somewhat broken imho) tests (because per-pixel face selection
2344 * can cause derivatives to be different for pixels outside the primitive
2345 * due to the major axis division even if pre-project derivatives are
2348 bld
.num_mips
= type
.length
;
2349 bld
.num_lods
= type
.length
;
2351 else if (lod_property
== LP_SAMPLER_LOD_PER_ELEMENT
||
2352 (explicit_lod
|| lod_bias
|| derivs
)) {
2353 if ((is_fetch
&& target
!= PIPE_BUFFER
) ||
2354 (!is_fetch
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)) {
2355 bld
.num_mips
= type
.length
;
2356 bld
.num_lods
= type
.length
;
2358 else if (!is_fetch
&& min_img_filter
!= mag_img_filter
) {
2360 bld
.num_lods
= type
.length
;
2363 /* TODO: for true scalar_lod should only use 1 lod value */
2364 else if ((is_fetch
&& explicit_lod
&& target
!= PIPE_BUFFER
) ||
2365 (!is_fetch
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)) {
2366 bld
.num_mips
= num_quads
;
2367 bld
.num_lods
= num_quads
;
2369 else if (!is_fetch
&& min_img_filter
!= mag_img_filter
) {
2371 bld
.num_lods
= num_quads
;
2375 bld
.lodf_type
= type
;
2376 /* we want native vector size to be able to use our intrinsics */
2377 if (bld
.num_lods
!= type
.length
) {
2378 /* TODO: this currently always has to be per-quad or per-element */
2379 bld
.lodf_type
.length
= type
.length
> 4 ? ((type
.length
+ 15) / 16) * 4 : 1;
2381 bld
.lodi_type
= lp_int_type(bld
.lodf_type
);
2382 bld
.levelf_type
= bld
.lodf_type
;
2383 if (bld
.num_mips
== 1) {
2384 bld
.levelf_type
.length
= 1;
2386 bld
.leveli_type
= lp_int_type(bld
.levelf_type
);
2387 bld
.float_size_type
= bld
.float_size_in_type
;
2388 /* Note: size vectors may not be native. They contain minified w/h/d/_ values,
2389 * with per-element lod that is w0/h0/d0/_/w1/h1/d1_/... so up to 8x4f32 */
2390 if (bld
.num_mips
> 1) {
2391 bld
.float_size_type
.length
= bld
.num_mips
== type
.length
?
2392 bld
.num_mips
* bld
.float_size_in_type
.length
:
2395 bld
.int_size_type
= lp_int_type(bld
.float_size_type
);
2397 lp_build_context_init(&bld
.float_bld
, gallivm
, bld
.float_type
);
2398 lp_build_context_init(&bld
.float_vec_bld
, gallivm
, type
);
2399 lp_build_context_init(&bld
.int_bld
, gallivm
, bld
.int_type
);
2400 lp_build_context_init(&bld
.coord_bld
, gallivm
, bld
.coord_type
);
2401 lp_build_context_init(&bld
.int_coord_bld
, gallivm
, bld
.int_coord_type
);
2402 lp_build_context_init(&bld
.int_size_in_bld
, gallivm
, bld
.int_size_in_type
);
2403 lp_build_context_init(&bld
.float_size_in_bld
, gallivm
, bld
.float_size_in_type
);
2404 lp_build_context_init(&bld
.int_size_bld
, gallivm
, bld
.int_size_type
);
2405 lp_build_context_init(&bld
.float_size_bld
, gallivm
, bld
.float_size_type
);
2406 lp_build_context_init(&bld
.texel_bld
, gallivm
, bld
.texel_type
);
2407 lp_build_context_init(&bld
.levelf_bld
, gallivm
, bld
.levelf_type
);
2408 lp_build_context_init(&bld
.leveli_bld
, gallivm
, bld
.leveli_type
);
2409 lp_build_context_init(&bld
.lodf_bld
, gallivm
, bld
.lodf_type
);
2410 lp_build_context_init(&bld
.lodi_bld
, gallivm
, bld
.lodi_type
);
2412 /* Get the dynamic state */
2413 tex_width
= dynamic_state
->width(dynamic_state
, gallivm
, texture_index
);
2414 bld
.row_stride_array
= dynamic_state
->row_stride(dynamic_state
, gallivm
, texture_index
);
2415 bld
.img_stride_array
= dynamic_state
->img_stride(dynamic_state
, gallivm
, texture_index
);
2416 bld
.base_ptr
= dynamic_state
->base_ptr(dynamic_state
, gallivm
, texture_index
);
2417 bld
.mip_offsets
= dynamic_state
->mip_offsets(dynamic_state
, gallivm
, texture_index
);
2418 /* Note that mip_offsets is an array[level] of offsets to texture images */
2420 /* width, height, depth as single int vector */
2422 bld
.int_size
= tex_width
;
2425 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size_in_bld
.undef
,
2426 tex_width
, LLVMConstInt(i32t
, 0, 0), "");
2428 LLVMValueRef tex_height
=
2429 dynamic_state
->height(dynamic_state
, gallivm
, texture_index
);
2430 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size
,
2431 tex_height
, LLVMConstInt(i32t
, 1, 0), "");
2433 LLVMValueRef tex_depth
=
2434 dynamic_state
->depth(dynamic_state
, gallivm
, texture_index
);
2435 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size
,
2436 tex_depth
, LLVMConstInt(i32t
, 2, 0), "");
2441 for (i
= 0; i
< 5; i
++) {
2442 newcoords
[i
] = coords
[i
];
2446 /* For debug: no-op texture sampling */
2447 lp_build_sample_nop(gallivm
,
2453 else if (is_fetch
) {
2454 lp_build_fetch_texel(&bld
, texture_index
, newcoords
,
2455 explicit_lod
, offsets
,
2460 LLVMValueRef lod_fpart
= NULL
, lod_positive
= NULL
;
2461 LLVMValueRef ilevel0
= NULL
, ilevel1
= NULL
;
2462 boolean use_aos
= util_format_fits_8unorm(bld
.format_desc
) &&
2463 /* not sure this is strictly needed or simply impossible */
2464 derived_sampler_state
.compare_mode
== PIPE_TEX_COMPARE_NONE
&&
2465 lp_is_simple_wrap_mode(derived_sampler_state
.wrap_s
);
2467 use_aos
&= bld
.num_lods
<= num_quads
||
2468 derived_sampler_state
.min_img_filter
==
2469 derived_sampler_state
.mag_img_filter
;
2471 use_aos
&= lp_is_simple_wrap_mode(derived_sampler_state
.wrap_t
);
2473 use_aos
&= lp_is_simple_wrap_mode(derived_sampler_state
.wrap_r
);
2476 if (static_texture_state
->target
== PIPE_TEXTURE_CUBE
&&
2477 derived_sampler_state
.seamless_cube_map
&&
2478 (derived_sampler_state
.min_img_filter
== PIPE_TEX_FILTER_LINEAR
||
2479 derived_sampler_state
.mag_img_filter
== PIPE_TEX_FILTER_LINEAR
)) {
2480 /* theoretically possible with AoS filtering but not implemented (complex!) */
2484 if ((gallivm_debug
& GALLIVM_DEBUG_PERF
) &&
2485 !use_aos
&& util_format_fits_8unorm(bld
.format_desc
)) {
2486 debug_printf("%s: using floating point linear filtering for %s\n",
2487 __FUNCTION__
, bld
.format_desc
->short_name
);
2488 debug_printf(" min_img %d mag_img %d mip %d target %d seamless %d"
2489 " wraps %d wrapt %d wrapr %d\n",
2490 derived_sampler_state
.min_img_filter
,
2491 derived_sampler_state
.mag_img_filter
,
2492 derived_sampler_state
.min_mip_filter
,
2493 static_texture_state
->target
,
2494 derived_sampler_state
.seamless_cube_map
,
2495 derived_sampler_state
.wrap_s
,
2496 derived_sampler_state
.wrap_t
,
2497 derived_sampler_state
.wrap_r
);
2500 lp_build_sample_common(&bld
, texture_index
, sampler_index
,
2502 derivs
, lod_bias
, explicit_lod
,
2503 &lod_positive
, &lod_fpart
,
2504 &ilevel0
, &ilevel1
);
2507 * we only try 8-wide sampling with soa as it appears to
2508 * be a loss with aos with AVX (but it should work, except
2509 * for conformance if min_filter != mag_filter if num_lods > 1).
2510 * (It should be faster if we'd support avx2)
2512 if (num_quads
== 1 || !use_aos
) {
2514 /* do sampling/filtering with fixed pt arithmetic */
2515 lp_build_sample_aos(&bld
, sampler_index
,
2516 newcoords
[0], newcoords
[1],
2518 offsets
, lod_positive
, lod_fpart
,
2524 lp_build_sample_general(&bld
, sampler_index
,
2526 lod_positive
, lod_fpart
,
2533 struct lp_build_sample_context bld4
;
2534 struct lp_type type4
= type
;
2536 LLVMValueRef texelout4
[4];
2537 LLVMValueRef texelouttmp
[4][LP_MAX_VECTOR_LENGTH
/16];
2541 /* Setup our build context */
2542 memset(&bld4
, 0, sizeof bld4
);
2543 bld4
.gallivm
= bld
.gallivm
;
2544 bld4
.static_texture_state
= bld
.static_texture_state
;
2545 bld4
.static_sampler_state
= bld
.static_sampler_state
;
2546 bld4
.dynamic_state
= bld
.dynamic_state
;
2547 bld4
.format_desc
= bld
.format_desc
;
2548 bld4
.dims
= bld
.dims
;
2549 bld4
.row_stride_array
= bld
.row_stride_array
;
2550 bld4
.img_stride_array
= bld
.img_stride_array
;
2551 bld4
.base_ptr
= bld
.base_ptr
;
2552 bld4
.mip_offsets
= bld
.mip_offsets
;
2553 bld4
.int_size
= bld
.int_size
;
2555 bld4
.vector_width
= lp_type_width(type4
);
2557 bld4
.float_type
= lp_type_float(32);
2558 bld4
.int_type
= lp_type_int(32);
2559 bld4
.coord_type
= type4
;
2560 bld4
.int_coord_type
= lp_int_type(type4
);
2561 bld4
.float_size_in_type
= lp_type_float(32);
2562 bld4
.float_size_in_type
.length
= dims
> 1 ? 4 : 1;
2563 bld4
.int_size_in_type
= lp_int_type(bld4
.float_size_in_type
);
2564 bld4
.texel_type
= bld
.texel_type
;
2565 bld4
.texel_type
.length
= 4;
2567 bld4
.num_mips
= bld4
.num_lods
= 1;
2568 if ((gallivm_debug
& GALLIVM_DEBUG_NO_QUAD_LOD
) &&
2569 (gallivm_debug
& GALLIVM_DEBUG_NO_RHO_APPROX
) &&
2570 (static_texture_state
->target
== PIPE_TEXTURE_CUBE
) &&
2571 (!is_fetch
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)) {
2572 bld4
.num_mips
= type4
.length
;
2573 bld4
.num_lods
= type4
.length
;
2575 if (lod_property
== LP_SAMPLER_LOD_PER_ELEMENT
&&
2576 (explicit_lod
|| lod_bias
|| derivs
)) {
2577 if ((is_fetch
&& target
!= PIPE_BUFFER
) ||
2578 (!is_fetch
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)) {
2579 bld4
.num_mips
= type4
.length
;
2580 bld4
.num_lods
= type4
.length
;
2582 else if (!is_fetch
&& min_img_filter
!= mag_img_filter
) {
2584 bld4
.num_lods
= type4
.length
;
2588 /* we want native vector size to be able to use our intrinsics */
2589 bld4
.lodf_type
= type4
;
2590 if (bld4
.num_lods
!= type4
.length
) {
2591 bld4
.lodf_type
.length
= 1;
2593 bld4
.lodi_type
= lp_int_type(bld4
.lodf_type
);
2594 bld4
.levelf_type
= type4
;
2595 if (bld4
.num_mips
!= type4
.length
) {
2596 bld4
.levelf_type
.length
= 1;
2598 bld4
.leveli_type
= lp_int_type(bld4
.levelf_type
);
2599 bld4
.float_size_type
= bld4
.float_size_in_type
;
2600 if (bld4
.num_mips
> 1) {
2601 bld4
.float_size_type
.length
= bld4
.num_mips
== type4
.length
?
2602 bld4
.num_mips
* bld4
.float_size_in_type
.length
:
2605 bld4
.int_size_type
= lp_int_type(bld4
.float_size_type
);
2607 lp_build_context_init(&bld4
.float_bld
, gallivm
, bld4
.float_type
);
2608 lp_build_context_init(&bld4
.float_vec_bld
, gallivm
, type4
);
2609 lp_build_context_init(&bld4
.int_bld
, gallivm
, bld4
.int_type
);
2610 lp_build_context_init(&bld4
.coord_bld
, gallivm
, bld4
.coord_type
);
2611 lp_build_context_init(&bld4
.int_coord_bld
, gallivm
, bld4
.int_coord_type
);
2612 lp_build_context_init(&bld4
.int_size_in_bld
, gallivm
, bld4
.int_size_in_type
);
2613 lp_build_context_init(&bld4
.float_size_in_bld
, gallivm
, bld4
.float_size_in_type
);
2614 lp_build_context_init(&bld4
.int_size_bld
, gallivm
, bld4
.int_size_type
);
2615 lp_build_context_init(&bld4
.float_size_bld
, gallivm
, bld4
.float_size_type
);
2616 lp_build_context_init(&bld4
.texel_bld
, gallivm
, bld4
.texel_type
);
2617 lp_build_context_init(&bld4
.levelf_bld
, gallivm
, bld4
.levelf_type
);
2618 lp_build_context_init(&bld4
.leveli_bld
, gallivm
, bld4
.leveli_type
);
2619 lp_build_context_init(&bld4
.lodf_bld
, gallivm
, bld4
.lodf_type
);
2620 lp_build_context_init(&bld4
.lodi_bld
, gallivm
, bld4
.lodi_type
);
2622 for (i
= 0; i
< num_quads
; i
++) {
2623 LLVMValueRef s4
, t4
, r4
;
2624 LLVMValueRef lod_positive4
, lod_fpart4
= NULL
;
2625 LLVMValueRef ilevel04
, ilevel14
= NULL
;
2626 LLVMValueRef offsets4
[4] = { NULL
};
2627 unsigned num_lods
= bld4
.num_lods
;
2629 s4
= lp_build_extract_range(gallivm
, newcoords
[0], 4*i
, 4);
2630 t4
= lp_build_extract_range(gallivm
, newcoords
[1], 4*i
, 4);
2631 r4
= lp_build_extract_range(gallivm
, newcoords
[2], 4*i
, 4);
2634 offsets4
[0] = lp_build_extract_range(gallivm
, offsets
[0], 4*i
, 4);
2636 offsets4
[1] = lp_build_extract_range(gallivm
, offsets
[1], 4*i
, 4);
2638 offsets4
[2] = lp_build_extract_range(gallivm
, offsets
[2], 4*i
, 4);
2642 lod_positive4
= lp_build_extract_range(gallivm
, lod_positive
, num_lods
* i
, num_lods
);
2643 ilevel04
= bld
.num_mips
== 1 ? ilevel0
:
2644 lp_build_extract_range(gallivm
, ilevel0
, num_lods
* i
, num_lods
);
2645 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
2646 ilevel14
= lp_build_extract_range(gallivm
, ilevel1
, num_lods
* i
, num_lods
);
2647 lod_fpart4
= lp_build_extract_range(gallivm
, lod_fpart
, num_lods
* i
, num_lods
);
2651 /* do sampling/filtering with fixed pt arithmetic */
2652 lp_build_sample_aos(&bld4
, sampler_index
,
2653 s4
, t4
, r4
, offsets4
,
2654 lod_positive4
, lod_fpart4
,
2660 /* this path is currently unreachable and hence might break easily... */
2661 LLVMValueRef newcoords4
[5];
2665 newcoords4
[3] = lp_build_extract_range(gallivm
, newcoords
[3], 4*i
, 4);
2666 newcoords4
[4] = lp_build_extract_range(gallivm
, newcoords
[4], 4*i
, 4);
2668 lp_build_sample_general(&bld4
, sampler_index
,
2669 newcoords4
, offsets4
,
2670 lod_positive4
, lod_fpart4
,
2674 for (j
= 0; j
< 4; j
++) {
2675 texelouttmp
[j
][i
] = texelout4
[j
];
2679 for (j
= 0; j
< 4; j
++) {
2680 texel_out
[j
] = lp_build_concat(gallivm
, texelouttmp
[j
], type4
, num_quads
);
2685 if (target
!= PIPE_BUFFER
) {
2686 apply_sampler_swizzle(&bld
, texel_out
);
2690 * texel type can be a (32bit) int/uint (for pure int formats only),
2691 * however we are expected to always return floats (storage is untyped).
2693 if (!bld
.texel_type
.floating
) {
2695 for (chan
= 0; chan
< 4; chan
++) {
2696 texel_out
[chan
] = LLVMBuildBitCast(builder
, texel_out
[chan
],
2697 lp_build_vec_type(gallivm
, type
), "");
2703 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
2704 const struct lp_static_texture_state
*static_state
,
2705 struct lp_sampler_dynamic_state
*dynamic_state
,
2706 struct lp_type int_type
,
2707 unsigned texture_unit
,
2709 boolean is_sviewinfo
,
2710 enum lp_sampler_lod_property lod_property
,
2711 LLVMValueRef explicit_lod
,
2712 LLVMValueRef
*sizes_out
)
2714 LLVMValueRef lod
, level
, size
;
2715 LLVMValueRef first_level
= NULL
;
2718 unsigned num_lods
= 1;
2719 struct lp_build_context bld_int_vec4
;
2721 if (static_state
->format
== PIPE_FORMAT_NONE
) {
2723 * If there's nothing bound, format is NONE, and we must return
2724 * all zero as mandated by d3d10 in this case.
2727 LLVMValueRef zero
= lp_build_const_vec(gallivm
, int_type
, 0.0F
);
2728 for (chan
= 0; chan
< 4; chan
++) {
2729 sizes_out
[chan
] = zero
;
2735 * Do some sanity verification about bound texture and shader dcl target.
2736 * Not entirely sure what's possible but assume array/non-array
2737 * always compatible (probably not ok for OpenGL but d3d10 has no
2738 * distinction of arrays at the resource level).
2739 * Everything else looks bogus (though not entirely sure about rect/2d).
2740 * Currently disabled because it causes assertion failures if there's
2741 * nothing bound (or rather a dummy texture, not that this case would
2742 * return the right values).
2744 if (0 && static_state
->target
!= target
) {
2745 if (static_state
->target
== PIPE_TEXTURE_1D
)
2746 assert(target
== PIPE_TEXTURE_1D_ARRAY
);
2747 else if (static_state
->target
== PIPE_TEXTURE_1D_ARRAY
)
2748 assert(target
== PIPE_TEXTURE_1D
);
2749 else if (static_state
->target
== PIPE_TEXTURE_2D
)
2750 assert(target
== PIPE_TEXTURE_2D_ARRAY
);
2751 else if (static_state
->target
== PIPE_TEXTURE_2D_ARRAY
)
2752 assert(target
== PIPE_TEXTURE_2D
);
2753 else if (static_state
->target
== PIPE_TEXTURE_CUBE
)
2754 assert(target
== PIPE_TEXTURE_CUBE_ARRAY
);
2755 else if (static_state
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
2756 assert(target
== PIPE_TEXTURE_CUBE
);
2761 dims
= texture_dims(target
);
2764 case PIPE_TEXTURE_1D_ARRAY
:
2765 case PIPE_TEXTURE_2D_ARRAY
:
2773 assert(!int_type
.floating
);
2775 lp_build_context_init(&bld_int_vec4
, gallivm
, lp_type_int_vec(32, 128));
2778 /* FIXME: this needs to honor per-element lod */
2779 lod
= LLVMBuildExtractElement(gallivm
->builder
, explicit_lod
, lp_build_const_int32(gallivm
, 0), "");
2780 first_level
= dynamic_state
->first_level(dynamic_state
, gallivm
, texture_unit
);
2781 level
= LLVMBuildAdd(gallivm
->builder
, lod
, first_level
, "level");
2782 lod
= lp_build_broadcast_scalar(&bld_int_vec4
, level
);
2784 lod
= bld_int_vec4
.zero
;
2787 size
= bld_int_vec4
.undef
;
2789 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
2790 dynamic_state
->width(dynamic_state
, gallivm
, texture_unit
),
2791 lp_build_const_int32(gallivm
, 0), "");
2794 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
2795 dynamic_state
->height(dynamic_state
, gallivm
, texture_unit
),
2796 lp_build_const_int32(gallivm
, 1), "");
2800 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
2801 dynamic_state
->depth(dynamic_state
, gallivm
, texture_unit
),
2802 lp_build_const_int32(gallivm
, 2), "");
2805 size
= lp_build_minify(&bld_int_vec4
, size
, lod
);
2808 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
2809 dynamic_state
->depth(dynamic_state
, gallivm
, texture_unit
),
2810 lp_build_const_int32(gallivm
, dims
), "");
2813 * d3d10 requires zero for x/y/z values (but not w, i.e. mip levels)
2814 * if level is out of bounds (note this can't cover unbound texture
2815 * here, which also requires returning zero).
2817 if (explicit_lod
&& is_sviewinfo
) {
2818 LLVMValueRef last_level
, out
, out1
;
2819 struct lp_build_context leveli_bld
;
2821 /* everything is scalar for now */
2822 lp_build_context_init(&leveli_bld
, gallivm
, lp_type_int_vec(32, 32));
2823 last_level
= dynamic_state
->last_level(dynamic_state
, gallivm
, texture_unit
);
2825 out
= lp_build_cmp(&leveli_bld
, PIPE_FUNC_LESS
, level
, first_level
);
2826 out1
= lp_build_cmp(&leveli_bld
, PIPE_FUNC_GREATER
, level
, last_level
);
2827 out
= lp_build_or(&leveli_bld
, out
, out1
);
2828 if (num_lods
== 1) {
2829 out
= lp_build_broadcast_scalar(&bld_int_vec4
, out
);
2835 size
= lp_build_andnot(&bld_int_vec4
, size
, out
);
2837 for (i
= 0; i
< dims
+ (has_array
? 1 : 0); i
++) {
2838 sizes_out
[i
] = lp_build_extract_broadcast(gallivm
, bld_int_vec4
.type
, int_type
,
2840 lp_build_const_int32(gallivm
, i
));
2843 for (; i
< 4; i
++) {
2844 sizes_out
[i
] = lp_build_const_vec(gallivm
, int_type
, 0.0);
2849 * if there's no explicit_lod (buffers, rects) queries requiring nr of
2850 * mips would be illegal.
2852 if (is_sviewinfo
&& explicit_lod
) {
2853 struct lp_build_context bld_int_scalar
;
2854 LLVMValueRef num_levels
;
2855 lp_build_context_init(&bld_int_scalar
, gallivm
, lp_type_int(32));
2857 if (static_state
->level_zero_only
) {
2858 num_levels
= bld_int_scalar
.one
;
2861 LLVMValueRef last_level
;
2863 last_level
= dynamic_state
->last_level(dynamic_state
, gallivm
, texture_unit
);
2864 num_levels
= lp_build_sub(&bld_int_scalar
, last_level
, first_level
);
2865 num_levels
= lp_build_add(&bld_int_scalar
, num_levels
, bld_int_scalar
.one
);
2867 sizes_out
[3] = lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, int_type
),