1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Texture sampling -- SoA.
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 * @author Brian Paul <brianp@vmware.com>
36 #include "pipe/p_defines.h"
37 #include "pipe/p_state.h"
38 #include "pipe/p_shader_tokens.h"
39 #include "util/u_debug.h"
40 #include "util/u_dump.h"
41 #include "util/u_memory.h"
42 #include "util/u_math.h"
43 #include "util/u_format.h"
44 #include "util/u_cpu_detect.h"
45 #include "lp_bld_debug.h"
46 #include "lp_bld_type.h"
47 #include "lp_bld_const.h"
48 #include "lp_bld_conv.h"
49 #include "lp_bld_arit.h"
50 #include "lp_bld_bitarit.h"
51 #include "lp_bld_logic.h"
52 #include "lp_bld_printf.h"
53 #include "lp_bld_swizzle.h"
54 #include "lp_bld_flow.h"
55 #include "lp_bld_gather.h"
56 #include "lp_bld_format.h"
57 #include "lp_bld_sample.h"
58 #include "lp_bld_sample_aos.h"
59 #include "lp_bld_struct.h"
60 #include "lp_bld_quad.h"
61 #include "lp_bld_pack.h"
65 * Generate code to fetch a texel from a texture at int coords (x, y, z).
66 * The computation depends on whether the texture is 1D, 2D or 3D.
67 * The result, texel, will be float vectors:
68 * texel[0] = red values
69 * texel[1] = green values
70 * texel[2] = blue values
71 * texel[3] = alpha values
74 lp_build_sample_texel_soa(struct lp_build_sample_context
*bld
,
75 unsigned sampler_unit
,
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 LLVMValueRef border_color_ptr
=
184 bld
->dynamic_state
->border_color(bld
->dynamic_state
,
185 bld
->gallivm
, sampler_unit
);
187 for (chan
= 0; chan
< 4; chan
++) {
188 LLVMValueRef border_chan
=
189 lp_build_array_get(bld
->gallivm
, border_color_ptr
,
190 lp_build_const_int32(bld
->gallivm
, chan
));
191 LLVMValueRef border_chan_vec
=
192 lp_build_broadcast_scalar(&bld
->float_vec_bld
, border_chan
);
194 if (!bld
->texel_type
.floating
) {
195 border_chan_vec
= LLVMBuildBitCast(builder
, border_chan_vec
,
196 bld
->texel_bld
.vec_type
, "");
198 texel_out
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
199 border_chan_vec
, texel_out
[chan
]);
206 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
209 lp_build_coord_mirror(struct lp_build_sample_context
*bld
,
212 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
213 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
214 LLVMValueRef fract
, flr
, isOdd
;
216 lp_build_ifloor_fract(coord_bld
, coord
, &flr
, &fract
);
218 /* isOdd = flr & 1 */
219 isOdd
= LLVMBuildAnd(bld
->gallivm
->builder
, flr
, int_coord_bld
->one
, "");
221 /* make coord positive or negative depending on isOdd */
222 coord
= lp_build_set_sign(coord_bld
, fract
, isOdd
);
224 /* convert isOdd to float */
225 isOdd
= lp_build_int_to_float(coord_bld
, isOdd
);
227 /* add isOdd to coord */
228 coord
= lp_build_add(coord_bld
, coord
, isOdd
);
235 * Helper to compute the first coord and the weight for
236 * linear wrap repeat npot textures
239 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
240 LLVMValueRef coord_f
,
241 LLVMValueRef length_i
,
242 LLVMValueRef length_f
,
243 LLVMValueRef
*coord0_i
,
244 LLVMValueRef
*weight_f
)
246 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
247 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
248 LLVMValueRef half
= lp_build_const_vec(bld
->gallivm
, coord_bld
->type
, 0.5);
249 LLVMValueRef length_minus_one
= lp_build_sub(int_coord_bld
, length_i
,
252 /* wrap with normalized floats is just fract */
253 coord_f
= lp_build_fract(coord_bld
, coord_f
);
254 /* mul by size and subtract 0.5 */
255 coord_f
= lp_build_mul(coord_bld
, coord_f
, length_f
);
256 coord_f
= lp_build_sub(coord_bld
, coord_f
, half
);
258 * we avoided the 0.5/length division before the repeat wrap,
259 * now need to fix up edge cases with selects
261 /* convert to int, compute lerp weight */
262 lp_build_ifloor_fract(coord_bld
, coord_f
, coord0_i
, weight_f
);
263 mask
= lp_build_compare(int_coord_bld
->gallivm
, int_coord_bld
->type
,
264 PIPE_FUNC_LESS
, *coord0_i
, int_coord_bld
->zero
);
265 *coord0_i
= lp_build_select(int_coord_bld
, mask
, length_minus_one
, *coord0_i
);
270 * Build LLVM code for texture wrap mode for linear filtering.
271 * \param x0_out returns first integer texcoord
272 * \param x1_out returns second integer texcoord
273 * \param weight_out returns linear interpolation weight
276 lp_build_sample_wrap_linear(struct lp_build_sample_context
*bld
,
279 LLVMValueRef length_f
,
283 LLVMValueRef
*x0_out
,
284 LLVMValueRef
*x1_out
,
285 LLVMValueRef
*weight_out
)
287 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
288 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
289 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
290 LLVMValueRef half
= lp_build_const_vec(bld
->gallivm
, coord_bld
->type
, 0.5);
291 LLVMValueRef length_minus_one
= lp_build_sub(int_coord_bld
, length
, int_coord_bld
->one
);
292 LLVMValueRef coord0
, coord1
, weight
;
295 case PIPE_TEX_WRAP_REPEAT
:
297 /* mul by size and subtract 0.5 */
298 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
299 coord
= lp_build_sub(coord_bld
, coord
, half
);
301 offset
= lp_build_int_to_float(coord_bld
, offset
);
302 coord
= lp_build_add(coord_bld
, coord
, offset
);
304 /* convert to int, compute lerp weight */
305 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
306 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
308 coord0
= LLVMBuildAnd(builder
, coord0
, length_minus_one
, "");
309 coord1
= LLVMBuildAnd(builder
, coord1
, length_minus_one
, "");
314 offset
= lp_build_int_to_float(coord_bld
, offset
);
315 offset
= lp_build_div(coord_bld
, offset
, length_f
);
316 coord
= lp_build_add(coord_bld
, coord
, offset
);
318 lp_build_coord_repeat_npot_linear(bld
, coord
,
321 mask
= lp_build_compare(int_coord_bld
->gallivm
, int_coord_bld
->type
,
322 PIPE_FUNC_NOTEQUAL
, coord0
, length_minus_one
);
323 coord1
= LLVMBuildAnd(builder
,
324 lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
),
329 case PIPE_TEX_WRAP_CLAMP
:
330 if (bld
->static_sampler_state
->normalized_coords
) {
331 /* scale coord to length */
332 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
335 offset
= lp_build_int_to_float(coord_bld
, offset
);
336 coord
= lp_build_add(coord_bld
, coord
, offset
);
339 /* clamp to [0, length] */
340 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, length_f
);
342 coord
= lp_build_sub(coord_bld
, coord
, half
);
344 /* convert to int, compute lerp weight */
345 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
346 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
349 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
351 struct lp_build_context abs_coord_bld
= bld
->coord_bld
;
352 abs_coord_bld
.type
.sign
= FALSE
;
354 if (bld
->static_sampler_state
->normalized_coords
) {
355 /* mul by tex size */
356 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
359 offset
= lp_build_int_to_float(coord_bld
, offset
);
360 coord
= lp_build_add(coord_bld
, coord
, offset
);
363 /* clamp to length max */
364 coord
= lp_build_min(coord_bld
, coord
, length_f
);
366 coord
= lp_build_sub(coord_bld
, coord
, half
);
367 /* clamp to [0, length - 0.5] */
368 coord
= lp_build_max(coord_bld
, coord
, coord_bld
->zero
);
369 /* convert to int, compute lerp weight */
370 lp_build_ifloor_fract(&abs_coord_bld
, coord
, &coord0
, &weight
);
371 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
372 /* coord1 = min(coord1, length-1) */
373 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
377 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
378 if (bld
->static_sampler_state
->normalized_coords
) {
379 /* scale coord to length */
380 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
383 offset
= lp_build_int_to_float(coord_bld
, offset
);
384 coord
= lp_build_add(coord_bld
, coord
, offset
);
386 /* was: clamp to [-0.5, length + 0.5], then sub 0.5 */
387 /* can skip clamp (though might not work for very large coord values */
388 coord
= lp_build_sub(coord_bld
, coord
, half
);
389 /* convert to int, compute lerp weight */
390 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
391 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
394 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
395 /* compute mirror function */
396 coord
= lp_build_coord_mirror(bld
, coord
);
398 /* scale coord to length */
399 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
400 coord
= lp_build_sub(coord_bld
, coord
, half
);
402 offset
= lp_build_int_to_float(coord_bld
, offset
);
403 coord
= lp_build_add(coord_bld
, coord
, offset
);
406 /* convert to int, compute lerp weight */
407 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
408 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
410 /* coord0 = max(coord0, 0) */
411 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
412 /* coord1 = min(coord1, length-1) */
413 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
416 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
417 if (bld
->static_sampler_state
->normalized_coords
) {
418 /* scale coord to length */
419 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
422 offset
= lp_build_int_to_float(coord_bld
, offset
);
423 coord
= lp_build_add(coord_bld
, coord
, offset
);
425 coord
= lp_build_abs(coord_bld
, coord
);
427 /* clamp to [0, length] */
428 coord
= lp_build_min(coord_bld
, coord
, length_f
);
430 coord
= lp_build_sub(coord_bld
, coord
, half
);
432 /* convert to int, compute lerp weight */
433 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
434 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
437 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
439 LLVMValueRef min
, max
;
440 struct lp_build_context abs_coord_bld
= bld
->coord_bld
;
441 abs_coord_bld
.type
.sign
= FALSE
;
443 if (bld
->static_sampler_state
->normalized_coords
) {
444 /* scale coord to length */
445 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
448 offset
= lp_build_int_to_float(coord_bld
, offset
);
449 coord
= lp_build_add(coord_bld
, coord
, offset
);
451 coord
= lp_build_abs(coord_bld
, coord
);
453 /* clamp to [0.5, length - 0.5] */
455 max
= lp_build_sub(coord_bld
, length_f
, min
);
456 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
458 coord
= lp_build_sub(coord_bld
, coord
, half
);
460 /* convert to int, compute lerp weight */
461 lp_build_ifloor_fract(&abs_coord_bld
, coord
, &coord0
, &weight
);
462 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
466 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
468 if (bld
->static_sampler_state
->normalized_coords
) {
469 /* scale coord to length */
470 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
473 offset
= lp_build_int_to_float(coord_bld
, offset
);
474 coord
= lp_build_add(coord_bld
, coord
, offset
);
476 coord
= lp_build_abs(coord_bld
, coord
);
478 /* was: clamp to [-0.5, length + 0.5] then sub 0.5 */
479 /* skip clamp - always positive, and other side
480 only potentially matters for very large coords */
481 coord
= lp_build_sub(coord_bld
, coord
, half
);
483 /* convert to int, compute lerp weight */
484 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
485 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
498 *weight_out
= weight
;
503 * Build LLVM code for texture wrap mode for nearest filtering.
504 * \param coord the incoming texcoord (nominally in [0,1])
505 * \param length the texture size along one dimension, as int vector
506 * \param length_f the texture size along one dimension, as float vector
507 * \param offset texel offset along one dimension (as int vector)
508 * \param is_pot if TRUE, length is a power of two
509 * \param wrap_mode one of PIPE_TEX_WRAP_x
512 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
515 LLVMValueRef length_f
,
520 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
521 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
522 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
523 LLVMValueRef length_minus_one
= lp_build_sub(int_coord_bld
, length
, int_coord_bld
->one
);
527 case PIPE_TEX_WRAP_REPEAT
:
529 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
530 icoord
= lp_build_ifloor(coord_bld
, coord
);
532 icoord
= lp_build_add(int_coord_bld
, icoord
, offset
);
534 icoord
= LLVMBuildAnd(builder
, icoord
, length_minus_one
, "");
538 offset
= lp_build_int_to_float(coord_bld
, offset
);
539 offset
= lp_build_div(coord_bld
, offset
, length_f
);
540 coord
= lp_build_add(coord_bld
, coord
, offset
);
542 /* take fraction, unnormalize */
543 coord
= lp_build_fract_safe(coord_bld
, coord
);
544 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
545 icoord
= lp_build_itrunc(coord_bld
, coord
);
549 case PIPE_TEX_WRAP_CLAMP
:
550 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
551 if (bld
->static_sampler_state
->normalized_coords
) {
552 /* scale coord to length */
553 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
557 /* use itrunc instead since we clamp to 0 anyway */
558 icoord
= lp_build_itrunc(coord_bld
, coord
);
560 icoord
= lp_build_add(int_coord_bld
, icoord
, offset
);
563 /* clamp to [0, length - 1]. */
564 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
568 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
569 if (bld
->static_sampler_state
->normalized_coords
) {
570 /* scale coord to length */
571 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
573 /* no clamp necessary, border masking will handle this */
574 icoord
= lp_build_ifloor(coord_bld
, coord
);
576 icoord
= lp_build_add(int_coord_bld
, icoord
, offset
);
580 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
582 offset
= lp_build_int_to_float(coord_bld
, offset
);
583 offset
= lp_build_div(coord_bld
, offset
, length_f
);
584 coord
= lp_build_add(coord_bld
, coord
, offset
);
586 /* compute mirror function */
587 coord
= lp_build_coord_mirror(bld
, coord
);
589 /* scale coord to length */
590 assert(bld
->static_sampler_state
->normalized_coords
);
591 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
593 /* itrunc == ifloor here */
594 icoord
= lp_build_itrunc(coord_bld
, coord
);
596 /* clamp to [0, length - 1] */
597 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
600 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
601 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
602 if (bld
->static_sampler_state
->normalized_coords
) {
603 /* scale coord to length */
604 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
607 offset
= lp_build_int_to_float(coord_bld
, offset
);
608 coord
= lp_build_add(coord_bld
, coord
, offset
);
610 coord
= lp_build_abs(coord_bld
, coord
);
612 /* itrunc == ifloor here */
613 icoord
= lp_build_itrunc(coord_bld
, coord
);
615 /* clamp to [0, length - 1] */
616 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
619 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
620 if (bld
->static_sampler_state
->normalized_coords
) {
621 /* scale coord to length */
622 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
625 offset
= lp_build_int_to_float(coord_bld
, offset
);
626 coord
= lp_build_add(coord_bld
, coord
, offset
);
628 coord
= lp_build_abs(coord_bld
, coord
);
630 /* itrunc == ifloor here */
631 icoord
= lp_build_itrunc(coord_bld
, coord
);
644 * Generate code to sample a mipmap level with nearest filtering.
645 * If sampling a cube texture, r = cube face in [0,5].
648 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
649 unsigned sampler_unit
,
651 LLVMValueRef row_stride_vec
,
652 LLVMValueRef img_stride_vec
,
653 LLVMValueRef data_ptr
,
654 LLVMValueRef mipoffsets
,
658 const LLVMValueRef
*offsets
,
659 LLVMValueRef colors_out
[4])
661 const unsigned dims
= bld
->dims
;
662 LLVMValueRef width_vec
;
663 LLVMValueRef height_vec
;
664 LLVMValueRef depth_vec
;
665 LLVMValueRef flt_size
;
666 LLVMValueRef flt_width_vec
;
667 LLVMValueRef flt_height_vec
;
668 LLVMValueRef flt_depth_vec
;
669 LLVMValueRef x
, y
= NULL
, z
= NULL
;
671 lp_build_extract_image_sizes(bld
,
675 &width_vec
, &height_vec
, &depth_vec
);
677 flt_size
= lp_build_int_to_float(&bld
->float_size_bld
, size
);
679 lp_build_extract_image_sizes(bld
,
680 &bld
->float_size_bld
,
683 &flt_width_vec
, &flt_height_vec
, &flt_depth_vec
);
686 * Compute integer texcoords.
688 x
= lp_build_sample_wrap_nearest(bld
, s
, width_vec
, flt_width_vec
, offsets
[0],
689 bld
->static_texture_state
->pot_width
,
690 bld
->static_sampler_state
->wrap_s
);
691 lp_build_name(x
, "tex.x.wrapped");
694 y
= lp_build_sample_wrap_nearest(bld
, t
, height_vec
, flt_height_vec
, offsets
[1],
695 bld
->static_texture_state
->pot_height
,
696 bld
->static_sampler_state
->wrap_t
);
697 lp_build_name(y
, "tex.y.wrapped");
700 z
= lp_build_sample_wrap_nearest(bld
, r
, depth_vec
, flt_depth_vec
, offsets
[2],
701 bld
->static_texture_state
->pot_depth
,
702 bld
->static_sampler_state
->wrap_r
);
703 lp_build_name(z
, "tex.z.wrapped");
706 if (bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE
||
707 bld
->static_texture_state
->target
== PIPE_TEXTURE_1D_ARRAY
||
708 bld
->static_texture_state
->target
== PIPE_TEXTURE_2D_ARRAY
) {
710 lp_build_name(z
, "tex.z.layer");
714 * Get texture colors.
716 lp_build_sample_texel_soa(bld
, sampler_unit
,
717 width_vec
, height_vec
, depth_vec
,
719 row_stride_vec
, img_stride_vec
,
720 data_ptr
, mipoffsets
, colors_out
);
725 * Generate code to sample a mipmap level with linear filtering.
726 * If sampling a cube texture, r = cube face in [0,5].
729 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
730 unsigned sampler_unit
,
732 LLVMValueRef row_stride_vec
,
733 LLVMValueRef img_stride_vec
,
734 LLVMValueRef data_ptr
,
735 LLVMValueRef mipoffsets
,
739 const LLVMValueRef
*offsets
,
740 LLVMValueRef colors_out
[4])
742 const unsigned dims
= bld
->dims
;
743 LLVMValueRef width_vec
;
744 LLVMValueRef height_vec
;
745 LLVMValueRef depth_vec
;
746 LLVMValueRef flt_size
;
747 LLVMValueRef flt_width_vec
;
748 LLVMValueRef flt_height_vec
;
749 LLVMValueRef flt_depth_vec
;
750 LLVMValueRef x0
, y0
= NULL
, z0
= NULL
, x1
, y1
= NULL
, z1
= NULL
;
751 LLVMValueRef s_fpart
, t_fpart
= NULL
, r_fpart
= NULL
;
752 LLVMValueRef neighbors
[2][2][4];
755 lp_build_extract_image_sizes(bld
,
759 &width_vec
, &height_vec
, &depth_vec
);
761 flt_size
= lp_build_int_to_float(&bld
->float_size_bld
, size
);
763 lp_build_extract_image_sizes(bld
,
764 &bld
->float_size_bld
,
767 &flt_width_vec
, &flt_height_vec
, &flt_depth_vec
);
770 * Compute integer texcoords.
772 lp_build_sample_wrap_linear(bld
, s
, width_vec
, flt_width_vec
, offsets
[0],
773 bld
->static_texture_state
->pot_width
,
774 bld
->static_sampler_state
->wrap_s
,
776 lp_build_name(x0
, "tex.x0.wrapped");
777 lp_build_name(x1
, "tex.x1.wrapped");
780 lp_build_sample_wrap_linear(bld
, t
, height_vec
, flt_height_vec
, offsets
[1],
781 bld
->static_texture_state
->pot_height
,
782 bld
->static_sampler_state
->wrap_t
,
784 lp_build_name(y0
, "tex.y0.wrapped");
785 lp_build_name(y1
, "tex.y1.wrapped");
788 lp_build_sample_wrap_linear(bld
, r
, depth_vec
, flt_depth_vec
, offsets
[2],
789 bld
->static_texture_state
->pot_depth
,
790 bld
->static_sampler_state
->wrap_r
,
792 lp_build_name(z0
, "tex.z0.wrapped");
793 lp_build_name(z1
, "tex.z1.wrapped");
796 if (bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE
||
797 bld
->static_texture_state
->target
== PIPE_TEXTURE_1D_ARRAY
||
798 bld
->static_texture_state
->target
== PIPE_TEXTURE_2D_ARRAY
) {
799 z0
= z1
= r
; /* cube face or array layer */
800 lp_build_name(z0
, "tex.z0.layer");
801 lp_build_name(z1
, "tex.z1.layer");
806 * Get texture colors.
808 /* get x0/x1 texels */
809 lp_build_sample_texel_soa(bld
, sampler_unit
,
810 width_vec
, height_vec
, depth_vec
,
812 row_stride_vec
, img_stride_vec
,
813 data_ptr
, mipoffsets
, neighbors
[0][0]);
814 lp_build_sample_texel_soa(bld
, sampler_unit
,
815 width_vec
, height_vec
, depth_vec
,
817 row_stride_vec
, img_stride_vec
,
818 data_ptr
, mipoffsets
, neighbors
[0][1]);
821 /* Interpolate two samples from 1D image to produce one color */
822 for (chan
= 0; chan
< 4; chan
++) {
823 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, s_fpart
,
824 neighbors
[0][0][chan
],
825 neighbors
[0][1][chan
],
831 LLVMValueRef colors0
[4];
833 /* get x0/x1 texels at y1 */
834 lp_build_sample_texel_soa(bld
, sampler_unit
,
835 width_vec
, height_vec
, depth_vec
,
837 row_stride_vec
, img_stride_vec
,
838 data_ptr
, mipoffsets
, neighbors
[1][0]);
839 lp_build_sample_texel_soa(bld
, sampler_unit
,
840 width_vec
, height_vec
, depth_vec
,
842 row_stride_vec
, img_stride_vec
,
843 data_ptr
, mipoffsets
, neighbors
[1][1]);
845 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
846 for (chan
= 0; chan
< 4; chan
++) {
847 colors0
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
849 neighbors
[0][0][chan
],
850 neighbors
[0][1][chan
],
851 neighbors
[1][0][chan
],
852 neighbors
[1][1][chan
],
857 LLVMValueRef neighbors1
[2][2][4];
858 LLVMValueRef colors1
[4];
860 /* get x0/x1/y0/y1 texels at z1 */
861 lp_build_sample_texel_soa(bld
, sampler_unit
,
862 width_vec
, height_vec
, depth_vec
,
864 row_stride_vec
, img_stride_vec
,
865 data_ptr
, mipoffsets
, neighbors1
[0][0]);
866 lp_build_sample_texel_soa(bld
, sampler_unit
,
867 width_vec
, height_vec
, depth_vec
,
869 row_stride_vec
, img_stride_vec
,
870 data_ptr
, mipoffsets
, neighbors1
[0][1]);
871 lp_build_sample_texel_soa(bld
, sampler_unit
,
872 width_vec
, height_vec
, depth_vec
,
874 row_stride_vec
, img_stride_vec
,
875 data_ptr
, mipoffsets
, neighbors1
[1][0]);
876 lp_build_sample_texel_soa(bld
, sampler_unit
,
877 width_vec
, height_vec
, depth_vec
,
879 row_stride_vec
, img_stride_vec
,
880 data_ptr
, mipoffsets
, neighbors1
[1][1]);
882 /* Bilinear interpolate the four samples from the second Z slice */
883 for (chan
= 0; chan
< 4; chan
++) {
884 colors1
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
886 neighbors1
[0][0][chan
],
887 neighbors1
[0][1][chan
],
888 neighbors1
[1][0][chan
],
889 neighbors1
[1][1][chan
],
893 /* Linearly interpolate the two samples from the two 3D slices */
894 for (chan
= 0; chan
< 4; chan
++) {
895 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
,
897 colors0
[chan
], colors1
[chan
],
903 for (chan
= 0; chan
< 4; chan
++) {
904 colors_out
[chan
] = colors0
[chan
];
912 * Sample the texture/mipmap using given image filter and mip filter.
913 * data0_ptr and data1_ptr point to the two mipmap levels to sample
914 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
915 * If we're using nearest miplevel sampling the '1' values will be null/unused.
918 lp_build_sample_mipmap(struct lp_build_sample_context
*bld
,
919 unsigned sampler_unit
,
925 const LLVMValueRef
*offsets
,
926 LLVMValueRef ilevel0
,
927 LLVMValueRef ilevel1
,
928 LLVMValueRef lod_fpart
,
929 LLVMValueRef
*colors_out
)
931 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
932 LLVMValueRef size0
= NULL
;
933 LLVMValueRef size1
= NULL
;
934 LLVMValueRef row_stride0_vec
= NULL
;
935 LLVMValueRef row_stride1_vec
= NULL
;
936 LLVMValueRef img_stride0_vec
= NULL
;
937 LLVMValueRef img_stride1_vec
= NULL
;
938 LLVMValueRef data_ptr0
= NULL
;
939 LLVMValueRef data_ptr1
= NULL
;
940 LLVMValueRef mipoff0
= NULL
;
941 LLVMValueRef mipoff1
= NULL
;
942 LLVMValueRef colors0
[4], colors1
[4];
945 /* sample the first mipmap level */
946 lp_build_mipmap_level_sizes(bld
, ilevel0
,
948 &row_stride0_vec
, &img_stride0_vec
);
949 if (bld
->num_lods
== 1) {
950 data_ptr0
= lp_build_get_mipmap_level(bld
, ilevel0
);
953 /* This path should work for num_lods 1 too but slightly less efficient */
954 data_ptr0
= bld
->base_ptr
;
955 mipoff0
= lp_build_get_mip_offsets(bld
, ilevel0
);
957 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
958 lp_build_sample_image_nearest(bld
, sampler_unit
,
960 row_stride0_vec
, img_stride0_vec
,
961 data_ptr0
, mipoff0
, s
, t
, r
, offsets
,
965 assert(img_filter
== PIPE_TEX_FILTER_LINEAR
);
966 lp_build_sample_image_linear(bld
, sampler_unit
,
968 row_stride0_vec
, img_stride0_vec
,
969 data_ptr0
, mipoff0
, s
, t
, r
, offsets
,
973 /* Store the first level's colors in the output variables */
974 for (chan
= 0; chan
< 4; chan
++) {
975 LLVMBuildStore(builder
, colors0
[chan
], colors_out
[chan
]);
978 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
979 struct lp_build_if_state if_ctx
;
980 LLVMValueRef need_lerp
;
981 unsigned num_quads
= bld
->coord_bld
.type
.length
/ 4;
983 /* need_lerp = lod_fpart > 0 */
984 if (num_quads
== 1) {
985 need_lerp
= LLVMBuildFCmp(builder
, LLVMRealUGT
,
986 lod_fpart
, bld
->perquadf_bld
.zero
,
991 * We'll do mip filtering if any of the quads need it.
992 * It might be better to split the vectors here and only fetch/filter
993 * quads which need it.
996 * We unfortunately need to clamp lod_fpart here since we can get
997 * negative values which would screw up filtering if not all
998 * lod_fpart values have same sign.
1000 lod_fpart
= lp_build_max(&bld
->perquadf_bld
, lod_fpart
,
1001 bld
->perquadf_bld
.zero
);
1002 need_lerp
= lp_build_compare(bld
->gallivm
, bld
->perquadf_bld
.type
,
1004 lod_fpart
, bld
->perquadf_bld
.zero
);
1005 need_lerp
= lp_build_any_true_range(&bld
->perquadi_bld
, num_quads
, need_lerp
);
1008 lp_build_if(&if_ctx
, bld
->gallivm
, need_lerp
);
1010 /* sample the second mipmap level */
1011 lp_build_mipmap_level_sizes(bld
, ilevel1
,
1013 &row_stride1_vec
, &img_stride1_vec
);
1014 if (bld
->num_lods
== 1) {
1015 data_ptr1
= lp_build_get_mipmap_level(bld
, ilevel1
);
1018 data_ptr1
= bld
->base_ptr
;
1019 mipoff1
= lp_build_get_mip_offsets(bld
, ilevel1
);
1021 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
1022 lp_build_sample_image_nearest(bld
, sampler_unit
,
1024 row_stride1_vec
, img_stride1_vec
,
1025 data_ptr1
, mipoff1
, s
, t
, r
, offsets
,
1029 lp_build_sample_image_linear(bld
, sampler_unit
,
1031 row_stride1_vec
, img_stride1_vec
,
1032 data_ptr1
, mipoff1
, s
, t
, r
, offsets
,
1036 /* interpolate samples from the two mipmap levels */
1038 lod_fpart
= lp_build_unpack_broadcast_aos_scalars(bld
->gallivm
,
1039 bld
->perquadf_bld
.type
,
1040 bld
->texel_bld
.type
,
1043 for (chan
= 0; chan
< 4; chan
++) {
1044 colors0
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
1045 colors0
[chan
], colors1
[chan
],
1047 LLVMBuildStore(builder
, colors0
[chan
], colors_out
[chan
]);
1050 lp_build_endif(&if_ctx
);
1056 * Clamp layer coord to valid values.
1059 lp_build_layer_coord(struct lp_build_sample_context
*bld
,
1060 unsigned texture_unit
,
1063 LLVMValueRef maxlayer
;
1065 maxlayer
= bld
->dynamic_state
->depth(bld
->dynamic_state
,
1066 bld
->gallivm
, texture_unit
);
1067 maxlayer
= lp_build_sub(&bld
->int_bld
, maxlayer
, bld
->int_bld
.one
);
1068 maxlayer
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, maxlayer
);
1069 return lp_build_clamp(&bld
->int_coord_bld
, layer
,
1070 bld
->int_coord_bld
.zero
, maxlayer
);
1076 * Calculate cube face, lod, mip levels.
1079 lp_build_sample_common(struct lp_build_sample_context
*bld
,
1080 unsigned texture_index
,
1081 unsigned sampler_index
,
1085 const struct lp_derivatives
*derivs
, /* optional */
1086 LLVMValueRef lod_bias
, /* optional */
1087 LLVMValueRef explicit_lod
, /* optional */
1088 LLVMValueRef
*lod_ipart
,
1089 LLVMValueRef
*lod_fpart
,
1090 LLVMValueRef
*ilevel0
,
1091 LLVMValueRef
*ilevel1
)
1093 const unsigned mip_filter
= bld
->static_sampler_state
->min_mip_filter
;
1094 const unsigned min_filter
= bld
->static_sampler_state
->min_img_filter
;
1095 const unsigned mag_filter
= bld
->static_sampler_state
->mag_img_filter
;
1096 const unsigned target
= bld
->static_texture_state
->target
;
1097 LLVMValueRef first_level
, cube_rho
= NULL
;
1100 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1101 mip_filter, min_filter, mag_filter);
1105 * Choose cube face, recompute texcoords for the chosen face and
1106 * compute rho here too (as it requires transform of derivatives).
1108 if (target
== PIPE_TEXTURE_CUBE
) {
1109 LLVMValueRef face
, face_s
, face_t
;
1110 boolean need_derivs
;
1111 need_derivs
= ((min_filter
!= mag_filter
||
1112 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) &&
1113 !bld
->static_sampler_state
->min_max_lod_equal
&&
1115 lp_build_cube_lookup(bld
, *s
, *t
, *r
, derivs
, &face
, &face_s
, &face_t
,
1116 &cube_rho
, need_derivs
);
1117 *s
= face_s
; /* vec */
1118 *t
= face_t
; /* vec */
1119 /* use 'r' to indicate cube face */
1120 *r
= face
; /* vec */
1122 else if (target
== PIPE_TEXTURE_1D_ARRAY
) {
1123 *r
= lp_build_iround(&bld
->coord_bld
, *t
);
1124 *r
= lp_build_layer_coord(bld
, texture_index
, *r
);
1126 else if (target
== PIPE_TEXTURE_2D_ARRAY
) {
1127 *r
= lp_build_iround(&bld
->coord_bld
, *r
);
1128 *r
= lp_build_layer_coord(bld
, texture_index
, *r
);
1132 * Compute the level of detail (float).
1134 if (min_filter
!= mag_filter
||
1135 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
1136 /* Need to compute lod either to choose mipmap levels or to
1137 * distinguish between minification/magnification with one mipmap level.
1139 lp_build_lod_selector(bld
, texture_index
, sampler_index
,
1140 *s
, *t
, *r
, cube_rho
,
1141 derivs
, lod_bias
, explicit_lod
,
1143 lod_ipart
, lod_fpart
);
1145 *lod_ipart
= bld
->perquadi_bld
.zero
;
1149 * Compute integer mipmap level(s) to fetch texels from: ilevel0, ilevel1
1151 switch (mip_filter
) {
1153 assert(0 && "bad mip_filter value in lp_build_sample_soa()");
1155 case PIPE_TEX_MIPFILTER_NONE
:
1156 /* always use mip level 0 */
1157 if (HAVE_LLVM
== 0x0207 && target
== PIPE_TEXTURE_CUBE
) {
1158 /* XXX this is a work-around for an apparent bug in LLVM 2.7.
1159 * We should be able to set ilevel0 = const(0) but that causes
1160 * bad x86 code to be emitted.
1163 lp_build_nearest_mip_level(bld
, texture_index
, *lod_ipart
, ilevel0
);
1166 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
1167 bld
->gallivm
, texture_index
);
1168 first_level
= lp_build_broadcast_scalar(&bld
->perquadi_bld
, first_level
);
1169 *ilevel0
= first_level
;
1172 case PIPE_TEX_MIPFILTER_NEAREST
:
1174 lp_build_nearest_mip_level(bld
, texture_index
, *lod_ipart
, ilevel0
);
1176 case PIPE_TEX_MIPFILTER_LINEAR
:
1179 lp_build_linear_mip_levels(bld
, texture_index
,
1180 *lod_ipart
, lod_fpart
,
1187 * General texture sampling codegen.
1188 * This function handles texture sampling for all texture targets (1D,
1189 * 2D, 3D, cube) and all filtering modes.
1192 lp_build_sample_general(struct lp_build_sample_context
*bld
,
1193 unsigned sampler_unit
,
1197 const LLVMValueRef
*offsets
,
1198 LLVMValueRef lod_ipart
,
1199 LLVMValueRef lod_fpart
,
1200 LLVMValueRef ilevel0
,
1201 LLVMValueRef ilevel1
,
1202 LLVMValueRef
*colors_out
)
1204 struct lp_build_context
*int_bld
= &bld
->int_bld
;
1205 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1206 const unsigned mip_filter
= bld
->static_sampler_state
->min_mip_filter
;
1207 const unsigned min_filter
= bld
->static_sampler_state
->min_img_filter
;
1208 const unsigned mag_filter
= bld
->static_sampler_state
->mag_img_filter
;
1209 LLVMValueRef texels
[4];
1213 * Get/interpolate texture colors.
1216 for (chan
= 0; chan
< 4; ++chan
) {
1217 texels
[chan
] = lp_build_alloca(bld
->gallivm
, bld
->texel_bld
.vec_type
, "");
1218 lp_build_name(texels
[chan
], "sampler%u_texel_%c_var", sampler_unit
, "xyzw"[chan
]);
1221 if (min_filter
== mag_filter
) {
1222 /* no need to distinguish between minification and magnification */
1223 lp_build_sample_mipmap(bld
, sampler_unit
,
1224 min_filter
, mip_filter
,
1226 ilevel0
, ilevel1
, lod_fpart
,
1230 /* Emit conditional to choose min image filter or mag image filter
1231 * depending on the lod being > 0 or <= 0, respectively.
1233 struct lp_build_if_state if_ctx
;
1234 LLVMValueRef minify
;
1237 * XXX this should to all lods into account, if some are min
1238 * some max probably could hack up the coords/weights in the linear
1239 * path with selects to work for nearest.
1240 * If that's just two quads sitting next to each other it seems
1241 * quite ok to do the same filtering method on both though, at
1242 * least unless we have explicit lod (and who uses different
1243 * min/mag filter with that?)
1245 if (bld
->num_lods
> 1)
1246 lod_ipart
= LLVMBuildExtractElement(builder
, lod_ipart
,
1247 lp_build_const_int32(bld
->gallivm
, 0), "");
1249 /* minify = lod >= 0.0 */
1250 minify
= LLVMBuildICmp(builder
, LLVMIntSGE
,
1251 lod_ipart
, int_bld
->zero
, "");
1253 lp_build_if(&if_ctx
, bld
->gallivm
, minify
);
1255 /* Use the minification filter */
1256 lp_build_sample_mipmap(bld
, sampler_unit
,
1257 min_filter
, mip_filter
,
1259 ilevel0
, ilevel1
, lod_fpart
,
1262 lp_build_else(&if_ctx
);
1264 /* Use the magnification filter */
1265 lp_build_sample_mipmap(bld
, sampler_unit
,
1266 mag_filter
, PIPE_TEX_MIPFILTER_NONE
,
1268 ilevel0
, NULL
, NULL
,
1271 lp_build_endif(&if_ctx
);
1274 for (chan
= 0; chan
< 4; ++chan
) {
1275 colors_out
[chan
] = LLVMBuildLoad(builder
, texels
[chan
], "");
1276 lp_build_name(colors_out
[chan
], "sampler%u_texel_%c", sampler_unit
, "xyzw"[chan
]);
1282 * Texel fetch function.
1283 * In contrast to general sampling there is no filtering, no coord minification,
1284 * lod (if any) is always explicit uint, coords are uints (in terms of texel units)
1285 * directly to be applied to the selected mip level (after adding texel offsets).
1286 * This function handles texel fetch for all targets where texel fetch is supported
1287 * (no cube maps, but 1d, 2d, 3d are supported, arrays and buffers should be too).
1290 lp_build_fetch_texel(struct lp_build_sample_context
*bld
,
1291 unsigned texture_unit
,
1292 const LLVMValueRef
*coords
,
1293 LLVMValueRef explicit_lod
,
1294 const LLVMValueRef
*offsets
,
1295 LLVMValueRef
*colors_out
)
1297 struct lp_build_context
*perquadi_bld
= &bld
->perquadi_bld
;
1298 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
1299 unsigned dims
= bld
->dims
, chan
;
1300 unsigned target
= bld
->static_texture_state
->target
;
1301 LLVMValueRef size
, ilevel
;
1302 LLVMValueRef row_stride_vec
= NULL
, img_stride_vec
= NULL
;
1303 LLVMValueRef x
= coords
[0], y
= coords
[1], z
= coords
[2];
1304 LLVMValueRef width
, height
, depth
, i
, j
;
1305 LLVMValueRef offset
, out_of_bounds
, out1
;
1307 /* XXX just like ordinary sampling, we don't handle per-pixel lod (yet). */
1308 if (explicit_lod
&& bld
->static_texture_state
->target
!= PIPE_BUFFER
) {
1309 ilevel
= lp_build_pack_aos_scalars(bld
->gallivm
, int_coord_bld
->type
,
1310 perquadi_bld
->type
, explicit_lod
, 0);
1311 lp_build_nearest_mip_level(bld
, texture_unit
, ilevel
, &ilevel
);
1315 ilevel
= lp_build_const_int32(bld
->gallivm
, 0);
1317 lp_build_mipmap_level_sizes(bld
, ilevel
,
1319 &row_stride_vec
, &img_stride_vec
);
1320 lp_build_extract_image_sizes(bld
, &bld
->int_size_bld
, int_coord_bld
->type
,
1321 size
, &width
, &height
, &depth
);
1323 if (target
== PIPE_TEXTURE_1D_ARRAY
||
1324 target
== PIPE_TEXTURE_2D_ARRAY
) {
1325 if (target
== PIPE_TEXTURE_1D_ARRAY
) {
1326 z
= lp_build_layer_coord(bld
, texture_unit
, y
);
1329 z
= lp_build_layer_coord(bld
, texture_unit
, z
);
1333 /* This is a lot like border sampling */
1335 /* XXX coords are really unsigned, offsets are signed */
1336 x
= lp_build_add(int_coord_bld
, x
, offsets
[0]);
1338 out_of_bounds
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
1339 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
1340 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1344 y
= lp_build_add(int_coord_bld
, y
, offsets
[1]);
1346 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
1347 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1348 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
1349 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1353 z
= lp_build_add(int_coord_bld
, z
, offsets
[2]);
1355 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
1356 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1357 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
1358 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1362 lp_build_sample_offset(int_coord_bld
,
1364 x
, y
, z
, row_stride_vec
, img_stride_vec
,
1367 if (bld
->static_texture_state
->target
!= PIPE_BUFFER
) {
1368 offset
= lp_build_add(int_coord_bld
, offset
,
1369 lp_build_get_mip_offsets(bld
, ilevel
));
1372 offset
= lp_build_andnot(int_coord_bld
, offset
, out_of_bounds
);
1374 lp_build_fetch_rgba_soa(bld
->gallivm
,
1377 bld
->base_ptr
, offset
,
1383 * Not needed except for ARB_robust_buffer_access_behavior.
1384 * Could use min/max above instead of out-of-bounds comparisons
1385 * (in fact cast to unsigned and min only is sufficient)
1386 * if we don't care about the result returned for out-of-bounds.
1388 for (chan
= 0; chan
< 4; chan
++) {
1389 colors_out
[chan
] = lp_build_select(&bld
->texel_bld
, out_of_bounds
,
1390 bld
->texel_bld
.zero
, colors_out
[chan
]);
1397 * Do shadow test/comparison.
1398 * \param coords incoming texcoords
1399 * \param texel the texel to compare against (use the X channel)
1400 * Ideally this should really be done per-sample.
1403 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
1404 const LLVMValueRef
*coords
,
1405 LLVMValueRef texel
[4])
1407 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1408 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1409 LLVMValueRef res
, p
;
1410 const unsigned chan
= 0;
1412 if (bld
->static_sampler_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
1415 if (bld
->static_texture_state
->target
== PIPE_TEXTURE_2D_ARRAY
||
1416 bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE
) {
1425 LLVMValueRef indx
= lp_build_const_int32(bld
->gallivm
, 0);
1426 LLVMValueRef coord
= LLVMBuildExtractElement(builder
, p
, indx
, "");
1427 LLVMValueRef tex
= LLVMBuildExtractElement(builder
, texel
[chan
], indx
, "");
1428 lp_build_printf(bld
->gallivm
, "shadow compare coord %f to texture %f\n",
1432 /* Clamp p coords to [0,1] */
1433 p
= lp_build_clamp(&bld
->coord_bld
, p
,
1434 bld
->coord_bld
.zero
,
1435 bld
->coord_bld
.one
);
1437 /* result = (p FUNC texel) ? 1 : 0 */
1438 res
= lp_build_cmp(texel_bld
, bld
->static_sampler_state
->compare_func
,
1440 res
= lp_build_select(texel_bld
, res
, texel_bld
->one
, texel_bld
->zero
);
1442 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1446 texel
[3] = texel_bld
->one
;
1451 * Just set texels to white instead of actually sampling the texture.
1455 lp_build_sample_nop(struct gallivm_state
*gallivm
,
1456 struct lp_type type
,
1457 const LLVMValueRef
*coords
,
1458 LLVMValueRef texel_out
[4])
1460 LLVMValueRef one
= lp_build_one(gallivm
, type
);
1463 for (chan
= 0; chan
< 4; chan
++) {
1464 texel_out
[chan
] = one
;
1470 * Build texture sampling code.
1471 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1473 * \param type vector float type to use for coords, etc.
1474 * \param is_fetch if this is a texel fetch instruction.
1475 * \param derivs partial derivatives of (s,t,r,q) with respect to x and y
1478 lp_build_sample_soa(struct gallivm_state
*gallivm
,
1479 const struct lp_static_texture_state
*static_texture_state
,
1480 const struct lp_static_sampler_state
*static_sampler_state
,
1481 struct lp_sampler_dynamic_state
*dynamic_state
,
1482 struct lp_type type
,
1484 unsigned texture_index
,
1485 unsigned sampler_index
,
1486 const LLVMValueRef
*coords
,
1487 const LLVMValueRef
*offsets
,
1488 const struct lp_derivatives
*derivs
, /* optional */
1489 LLVMValueRef lod_bias
, /* optional */
1490 LLVMValueRef explicit_lod
, /* optional */
1491 LLVMValueRef texel_out
[4])
1493 unsigned dims
= texture_dims(static_texture_state
->target
);
1494 unsigned num_quads
= type
.length
/ 4;
1495 unsigned mip_filter
;
1496 struct lp_build_sample_context bld
;
1497 struct lp_static_sampler_state derived_sampler_state
= *static_sampler_state
;
1498 LLVMTypeRef i32t
= LLVMInt32TypeInContext(gallivm
->context
);
1499 LLVMBuilderRef builder
= gallivm
->builder
;
1500 LLVMValueRef tex_width
;
1506 enum pipe_format fmt
= static_texture_state
->format
;
1507 debug_printf("Sample from %s\n", util_format_name(fmt
));
1510 assert(type
.floating
);
1512 /* Setup our build context */
1513 memset(&bld
, 0, sizeof bld
);
1514 bld
.gallivm
= gallivm
;
1515 bld
.static_sampler_state
= &derived_sampler_state
;
1516 bld
.static_texture_state
= static_texture_state
;
1517 bld
.dynamic_state
= dynamic_state
;
1518 bld
.format_desc
= util_format_description(static_texture_state
->format
);
1521 bld
.vector_width
= lp_type_width(type
);
1523 bld
.float_type
= lp_type_float(32);
1524 bld
.int_type
= lp_type_int(32);
1525 bld
.coord_type
= type
;
1526 bld
.int_coord_type
= lp_int_type(type
);
1527 bld
.float_size_in_type
= lp_type_float(32);
1528 bld
.float_size_in_type
.length
= dims
> 1 ? 4 : 1;
1529 bld
.int_size_in_type
= lp_int_type(bld
.float_size_in_type
);
1530 bld
.texel_type
= type
;
1531 bld
.perquadf_type
= type
;
1532 /* we want native vector size to be able to use our intrinsics */
1533 bld
.perquadf_type
.length
= type
.length
> 4 ? ((type
.length
+ 15) / 16) * 4 : 1;
1534 bld
.perquadi_type
= lp_int_type(bld
.perquadf_type
);
1536 /* always using the first channel hopefully should be safe,
1537 * if not things WILL break in other places anyway.
1539 if (bld
.format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
&&
1540 bld
.format_desc
->channel
[0].pure_integer
) {
1541 if (bld
.format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_SIGNED
) {
1542 bld
.texel_type
= lp_type_int_vec(type
.width
, type
.width
* type
.length
);
1544 else if (bld
.format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
1545 bld
.texel_type
= lp_type_uint_vec(type
.width
, type
.width
* type
.length
);
1549 if (!static_texture_state
->level_zero_only
) {
1550 derived_sampler_state
.min_mip_filter
= static_sampler_state
->min_mip_filter
;
1552 derived_sampler_state
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
1554 mip_filter
= derived_sampler_state
.min_mip_filter
;
1557 debug_printf(" .min_mip_filter = %u\n", derived_sampler_state
.min_mip_filter
);
1561 * There are other situations where at least the multiple int lods could be
1562 * avoided like min and max lod being equal.
1564 if ((is_fetch
&& explicit_lod
&& bld
.static_texture_state
->target
!= PIPE_BUFFER
) ||
1565 (!is_fetch
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)) {
1566 bld
.num_lods
= num_quads
;
1572 bld
.float_size_type
= bld
.float_size_in_type
;
1573 bld
.float_size_type
.length
= bld
.num_lods
> 1 ? type
.length
:
1574 bld
.float_size_in_type
.length
;
1575 bld
.int_size_type
= lp_int_type(bld
.float_size_type
);
1577 lp_build_context_init(&bld
.float_bld
, gallivm
, bld
.float_type
);
1578 lp_build_context_init(&bld
.float_vec_bld
, gallivm
, type
);
1579 lp_build_context_init(&bld
.int_bld
, gallivm
, bld
.int_type
);
1580 lp_build_context_init(&bld
.coord_bld
, gallivm
, bld
.coord_type
);
1581 lp_build_context_init(&bld
.int_coord_bld
, gallivm
, bld
.int_coord_type
);
1582 lp_build_context_init(&bld
.int_size_in_bld
, gallivm
, bld
.int_size_in_type
);
1583 lp_build_context_init(&bld
.float_size_in_bld
, gallivm
, bld
.float_size_in_type
);
1584 lp_build_context_init(&bld
.int_size_bld
, gallivm
, bld
.int_size_type
);
1585 lp_build_context_init(&bld
.float_size_bld
, gallivm
, bld
.float_size_type
);
1586 lp_build_context_init(&bld
.texel_bld
, gallivm
, bld
.texel_type
);
1587 lp_build_context_init(&bld
.perquadf_bld
, gallivm
, bld
.perquadf_type
);
1588 lp_build_context_init(&bld
.perquadi_bld
, gallivm
, bld
.perquadi_type
);
1590 /* Get the dynamic state */
1591 tex_width
= dynamic_state
->width(dynamic_state
, gallivm
, texture_index
);
1592 bld
.row_stride_array
= dynamic_state
->row_stride(dynamic_state
, gallivm
, texture_index
);
1593 bld
.img_stride_array
= dynamic_state
->img_stride(dynamic_state
, gallivm
, texture_index
);
1594 bld
.base_ptr
= dynamic_state
->base_ptr(dynamic_state
, gallivm
, texture_index
);
1595 bld
.mip_offsets
= dynamic_state
->mip_offsets(dynamic_state
, gallivm
, texture_index
);
1596 /* Note that mip_offsets is an array[level] of offsets to texture images */
1602 /* width, height, depth as single int vector */
1604 bld
.int_size
= tex_width
;
1607 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size_in_bld
.undef
,
1608 tex_width
, LLVMConstInt(i32t
, 0, 0), "");
1610 LLVMValueRef tex_height
=
1611 dynamic_state
->height(dynamic_state
, gallivm
, texture_index
);
1612 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size
,
1613 tex_height
, LLVMConstInt(i32t
, 1, 0), "");
1615 LLVMValueRef tex_depth
=
1616 dynamic_state
->depth(dynamic_state
, gallivm
, texture_index
);
1617 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size
,
1618 tex_depth
, LLVMConstInt(i32t
, 2, 0), "");
1624 /* For debug: no-op texture sampling */
1625 lp_build_sample_nop(gallivm
,
1631 else if (is_fetch
) {
1632 lp_build_fetch_texel(&bld
, texture_index
, coords
,
1633 explicit_lod
, offsets
,
1638 LLVMValueRef lod_ipart
= NULL
, lod_fpart
= NULL
;
1639 LLVMValueRef ilevel0
= NULL
, ilevel1
= NULL
;
1640 boolean use_aos
= util_format_fits_8unorm(bld
.format_desc
) &&
1641 lp_is_simple_wrap_mode(static_sampler_state
->wrap_s
) &&
1642 lp_is_simple_wrap_mode(static_sampler_state
->wrap_t
);
1644 if ((gallivm_debug
& GALLIVM_DEBUG_PERF
) &&
1645 !use_aos
&& util_format_fits_8unorm(bld
.format_desc
)) {
1646 debug_printf("%s: using floating point linear filtering for %s\n",
1647 __FUNCTION__
, bld
.format_desc
->short_name
);
1648 debug_printf(" min_img %d mag_img %d mip %d wraps %d wrapt %d\n",
1649 static_sampler_state
->min_img_filter
,
1650 static_sampler_state
->mag_img_filter
,
1651 static_sampler_state
->min_mip_filter
,
1652 static_sampler_state
->wrap_s
,
1653 static_sampler_state
->wrap_t
);
1656 lp_build_sample_common(&bld
, texture_index
, sampler_index
,
1658 derivs
, lod_bias
, explicit_lod
,
1659 &lod_ipart
, &lod_fpart
,
1660 &ilevel0
, &ilevel1
);
1663 * we only try 8-wide sampling with soa as it appears to
1664 * be a loss with aos with AVX (but it should work).
1665 * (It should be faster if we'd support avx2)
1667 if (num_quads
== 1 || !use_aos
) {
1669 if (num_quads
> 1) {
1670 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
1671 LLVMValueRef index0
= lp_build_const_int32(gallivm
, 0);
1673 * These parameters are the same for all quads,
1674 * could probably simplify.
1676 lod_ipart
= LLVMBuildExtractElement(builder
, lod_ipart
, index0
, "");
1677 ilevel0
= LLVMBuildExtractElement(builder
, ilevel0
, index0
, "");
1681 /* do sampling/filtering with fixed pt arithmetic */
1682 lp_build_sample_aos(&bld
, sampler_index
,
1684 lod_ipart
, lod_fpart
,
1690 lp_build_sample_general(&bld
, sampler_index
,
1692 lod_ipart
, lod_fpart
,
1699 struct lp_build_sample_context bld4
;
1700 struct lp_type type4
= type
;
1702 LLVMValueRef texelout4
[4];
1703 LLVMValueRef texelouttmp
[4][LP_MAX_VECTOR_LENGTH
/16];
1707 /* Setup our build context */
1708 memset(&bld4
, 0, sizeof bld4
);
1709 bld4
.gallivm
= bld
.gallivm
;
1710 bld4
.static_texture_state
= bld
.static_texture_state
;
1711 bld4
.static_sampler_state
= bld
.static_sampler_state
;
1712 bld4
.dynamic_state
= bld
.dynamic_state
;
1713 bld4
.format_desc
= bld
.format_desc
;
1714 bld4
.dims
= bld
.dims
;
1715 bld4
.row_stride_array
= bld
.row_stride_array
;
1716 bld4
.img_stride_array
= bld
.img_stride_array
;
1717 bld4
.base_ptr
= bld
.base_ptr
;
1718 bld4
.mip_offsets
= bld
.mip_offsets
;
1719 bld4
.int_size
= bld
.int_size
;
1721 bld4
.vector_width
= lp_type_width(type4
);
1723 bld4
.float_type
= lp_type_float(32);
1724 bld4
.int_type
= lp_type_int(32);
1725 bld4
.coord_type
= type4
;
1726 bld4
.int_coord_type
= lp_int_type(type4
);
1727 bld4
.float_size_in_type
= lp_type_float(32);
1728 bld4
.float_size_in_type
.length
= dims
> 1 ? 4 : 1;
1729 bld4
.int_size_in_type
= lp_int_type(bld4
.float_size_in_type
);
1730 bld4
.texel_type
= bld
.texel_type
;
1731 bld4
.texel_type
.length
= 4;
1732 bld4
.perquadf_type
= type4
;
1733 /* we want native vector size to be able to use our intrinsics */
1734 bld4
.perquadf_type
.length
= 1;
1735 bld4
.perquadi_type
= lp_int_type(bld4
.perquadf_type
);
1738 bld4
.int_size_type
= bld4
.int_size_in_type
;
1739 bld4
.float_size_type
= bld4
.float_size_in_type
;
1741 lp_build_context_init(&bld4
.float_bld
, gallivm
, bld4
.float_type
);
1742 lp_build_context_init(&bld4
.float_vec_bld
, gallivm
, type4
);
1743 lp_build_context_init(&bld4
.int_bld
, gallivm
, bld4
.int_type
);
1744 lp_build_context_init(&bld4
.coord_bld
, gallivm
, bld4
.coord_type
);
1745 lp_build_context_init(&bld4
.int_coord_bld
, gallivm
, bld4
.int_coord_type
);
1746 lp_build_context_init(&bld4
.int_size_in_bld
, gallivm
, bld4
.int_size_in_type
);
1747 lp_build_context_init(&bld4
.float_size_in_bld
, gallivm
, bld4
.float_size_in_type
);
1748 lp_build_context_init(&bld4
.int_size_bld
, gallivm
, bld4
.int_size_type
);
1749 lp_build_context_init(&bld4
.float_size_bld
, gallivm
, bld4
.float_size_type
);
1750 lp_build_context_init(&bld4
.texel_bld
, gallivm
, bld4
.texel_type
);
1751 lp_build_context_init(&bld4
.perquadf_bld
, gallivm
, bld4
.perquadf_type
);
1752 lp_build_context_init(&bld4
.perquadi_bld
, gallivm
, bld4
.perquadi_type
);
1754 for (i
= 0; i
< num_quads
; i
++) {
1755 LLVMValueRef s4
, t4
, r4
;
1756 LLVMValueRef lod_iparts
, lod_fparts
= NULL
;
1757 LLVMValueRef ilevel0s
, ilevel1s
= NULL
;
1758 LLVMValueRef indexi
= lp_build_const_int32(gallivm
, i
);
1759 LLVMValueRef offsets4
[4] = { NULL
};
1761 s4
= lp_build_extract_range(gallivm
, s
, 4*i
, 4);
1762 t4
= lp_build_extract_range(gallivm
, t
, 4*i
, 4);
1763 r4
= lp_build_extract_range(gallivm
, r
, 4*i
, 4);
1766 offsets4
[0] = lp_build_extract_range(gallivm
, offsets
[0], 4*i
, 4);
1768 offsets4
[1] = lp_build_extract_range(gallivm
, offsets
[1], 4*i
, 4);
1770 offsets4
[2] = lp_build_extract_range(gallivm
, offsets
[2], 4*i
, 4);
1774 lod_iparts
= LLVMBuildExtractElement(builder
, lod_ipart
, indexi
, "");
1775 ilevel0s
= LLVMBuildExtractElement(builder
, ilevel0
, indexi
, "");
1776 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1777 ilevel1s
= LLVMBuildExtractElement(builder
, ilevel1
, indexi
, "");
1778 lod_fparts
= LLVMBuildExtractElement(builder
, lod_fpart
, indexi
, "");
1782 /* do sampling/filtering with fixed pt arithmetic */
1783 lp_build_sample_aos(&bld4
, sampler_index
,
1784 s4
, t4
, r4
, offsets4
,
1785 lod_iparts
, lod_fparts
,
1791 lp_build_sample_general(&bld4
, sampler_index
,
1792 s4
, t4
, r4
, offsets4
,
1793 lod_iparts
, lod_fparts
,
1797 for (j
= 0; j
< 4; j
++) {
1798 texelouttmp
[j
][i
] = texelout4
[j
];
1802 for (j
= 0; j
< 4; j
++) {
1803 texel_out
[j
] = lp_build_concat(gallivm
, texelouttmp
[j
], type4
, num_quads
);
1807 lp_build_sample_compare(&bld
, coords
, texel_out
);
1810 if (static_texture_state
->target
!= PIPE_BUFFER
) {
1811 apply_sampler_swizzle(&bld
, texel_out
);
1815 * texel type can be a (32bit) int/uint (for pure int formats only),
1816 * however we are expected to always return floats (storage is untyped).
1818 if (!bld
.texel_type
.floating
) {
1820 for (chan
= 0; chan
< 4; chan
++) {
1821 texel_out
[chan
] = LLVMBuildBitCast(builder
, texel_out
[chan
],
1822 lp_build_vec_type(gallivm
, type
), "");
1828 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
1829 const struct lp_static_texture_state
*static_state
,
1830 struct lp_sampler_dynamic_state
*dynamic_state
,
1831 struct lp_type int_type
,
1832 unsigned texture_unit
,
1833 boolean need_nr_mips
,
1834 LLVMValueRef explicit_lod
,
1835 LLVMValueRef
*sizes_out
)
1839 LLVMValueRef first_level
= NULL
;
1842 struct lp_build_context bld_int_vec
;
1844 dims
= texture_dims(static_state
->target
);
1846 switch (static_state
->target
) {
1847 case PIPE_TEXTURE_1D_ARRAY
:
1848 case PIPE_TEXTURE_2D_ARRAY
:
1856 assert(!int_type
.floating
);
1858 lp_build_context_init(&bld_int_vec
, gallivm
, lp_type_int_vec(32, 128));
1861 lod
= LLVMBuildExtractElement(gallivm
->builder
, explicit_lod
, lp_build_const_int32(gallivm
, 0), "");
1862 first_level
= dynamic_state
->first_level(dynamic_state
, gallivm
, texture_unit
);
1863 lod
= lp_build_broadcast_scalar(&bld_int_vec
,
1864 LLVMBuildAdd(gallivm
->builder
, lod
, first_level
, "lod"));
1867 lod
= bld_int_vec
.zero
;
1871 size
= bld_int_vec
.zero
;
1874 size
= bld_int_vec
.undef
;
1877 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
1878 dynamic_state
->width(dynamic_state
, gallivm
, texture_unit
),
1879 lp_build_const_int32(gallivm
, 0), "");
1882 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
1883 dynamic_state
->height(dynamic_state
, gallivm
, texture_unit
),
1884 lp_build_const_int32(gallivm
, 1), "");
1888 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
1889 dynamic_state
->depth(dynamic_state
, gallivm
, texture_unit
),
1890 lp_build_const_int32(gallivm
, 2), "");
1893 size
= lp_build_minify(&bld_int_vec
, size
, lod
);
1896 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
1897 dynamic_state
->depth(dynamic_state
, gallivm
, texture_unit
),
1898 lp_build_const_int32(gallivm
, dims
), "");
1901 * XXX for out-of-bounds lod, should set size to zero vector here
1902 * (for dx10-style only, i.e. need_nr_mips)
1905 for (i
= 0; i
< dims
+ (has_array
? 1 : 0); i
++) {
1906 sizes_out
[i
] = lp_build_extract_broadcast(gallivm
, bld_int_vec
.type
, int_type
,
1908 lp_build_const_int32(gallivm
, i
));
1912 * if there's no explicit_lod (buffers, rects) queries requiring nr of
1913 * mips would be illegal.
1915 if (need_nr_mips
&& explicit_lod
) {
1916 struct lp_build_context bld_int_scalar
;
1917 LLVMValueRef num_levels
;
1918 lp_build_context_init(&bld_int_scalar
, gallivm
, lp_type_int(32));
1920 if (static_state
->level_zero_only
) {
1921 num_levels
= bld_int_scalar
.one
;
1924 LLVMValueRef last_level
;
1926 last_level
= dynamic_state
->last_level(dynamic_state
, gallivm
, texture_unit
);
1927 num_levels
= lp_build_sub(&bld_int_scalar
, last_level
, first_level
);
1928 num_levels
= lp_build_add(&bld_int_scalar
, num_levels
, bld_int_scalar
.one
);
1930 sizes_out
[3] = lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, int_type
),