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 struct lp_build_context abs_coord_bld
= bld
->coord_bld
;
440 abs_coord_bld
.type
.sign
= FALSE
;
442 if (bld
->static_sampler_state
->normalized_coords
) {
443 /* scale coord to length */
444 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
447 offset
= lp_build_int_to_float(coord_bld
, offset
);
448 coord
= lp_build_add(coord_bld
, coord
, offset
);
450 coord
= lp_build_abs(coord_bld
, coord
);
452 /* clamp to length max */
453 coord
= lp_build_min(coord_bld
, coord
, length_f
);
455 coord
= lp_build_sub(coord_bld
, coord
, half
);
456 /* clamp to [0, length - 0.5] */
457 coord
= lp_build_max(coord_bld
, coord
, coord_bld
->zero
);
459 /* convert to int, compute lerp weight */
460 lp_build_ifloor_fract(&abs_coord_bld
, coord
, &coord0
, &weight
);
461 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
462 /* coord1 = min(coord1, length-1) */
463 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
467 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
469 if (bld
->static_sampler_state
->normalized_coords
) {
470 /* scale coord to length */
471 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
474 offset
= lp_build_int_to_float(coord_bld
, offset
);
475 coord
= lp_build_add(coord_bld
, coord
, offset
);
477 coord
= lp_build_abs(coord_bld
, coord
);
479 /* was: clamp to [-0.5, length + 0.5] then sub 0.5 */
480 /* skip clamp - always positive, and other side
481 only potentially matters for very large coords */
482 coord
= lp_build_sub(coord_bld
, coord
, half
);
484 /* convert to int, compute lerp weight */
485 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
486 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
499 *weight_out
= weight
;
504 * Build LLVM code for texture wrap mode for nearest filtering.
505 * \param coord the incoming texcoord (nominally in [0,1])
506 * \param length the texture size along one dimension, as int vector
507 * \param length_f the texture size along one dimension, as float vector
508 * \param offset texel offset along one dimension (as int vector)
509 * \param is_pot if TRUE, length is a power of two
510 * \param wrap_mode one of PIPE_TEX_WRAP_x
513 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
516 LLVMValueRef length_f
,
521 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
522 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
523 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
524 LLVMValueRef length_minus_one
= lp_build_sub(int_coord_bld
, length
, int_coord_bld
->one
);
528 case PIPE_TEX_WRAP_REPEAT
:
530 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
531 icoord
= lp_build_ifloor(coord_bld
, coord
);
533 icoord
= lp_build_add(int_coord_bld
, icoord
, offset
);
535 icoord
= LLVMBuildAnd(builder
, icoord
, length_minus_one
, "");
539 offset
= lp_build_int_to_float(coord_bld
, offset
);
540 offset
= lp_build_div(coord_bld
, offset
, length_f
);
541 coord
= lp_build_add(coord_bld
, coord
, offset
);
543 /* take fraction, unnormalize */
544 coord
= lp_build_fract_safe(coord_bld
, coord
);
545 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
546 icoord
= lp_build_itrunc(coord_bld
, coord
);
550 case PIPE_TEX_WRAP_CLAMP
:
551 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
552 if (bld
->static_sampler_state
->normalized_coords
) {
553 /* scale coord to length */
554 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
558 /* use itrunc instead since we clamp to 0 anyway */
559 icoord
= lp_build_itrunc(coord_bld
, coord
);
561 icoord
= lp_build_add(int_coord_bld
, icoord
, offset
);
564 /* clamp to [0, length - 1]. */
565 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
569 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
570 if (bld
->static_sampler_state
->normalized_coords
) {
571 /* scale coord to length */
572 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
574 /* no clamp necessary, border masking will handle this */
575 icoord
= lp_build_ifloor(coord_bld
, coord
);
577 icoord
= lp_build_add(int_coord_bld
, icoord
, offset
);
581 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
583 offset
= lp_build_int_to_float(coord_bld
, offset
);
584 offset
= lp_build_div(coord_bld
, offset
, length_f
);
585 coord
= lp_build_add(coord_bld
, coord
, offset
);
587 /* compute mirror function */
588 coord
= lp_build_coord_mirror(bld
, coord
);
590 /* scale coord to length */
591 assert(bld
->static_sampler_state
->normalized_coords
);
592 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
594 /* itrunc == ifloor here */
595 icoord
= lp_build_itrunc(coord_bld
, coord
);
597 /* clamp to [0, length - 1] */
598 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
601 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
602 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
603 if (bld
->static_sampler_state
->normalized_coords
) {
604 /* scale coord to length */
605 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
608 offset
= lp_build_int_to_float(coord_bld
, offset
);
609 coord
= lp_build_add(coord_bld
, coord
, offset
);
611 coord
= lp_build_abs(coord_bld
, coord
);
613 /* itrunc == ifloor here */
614 icoord
= lp_build_itrunc(coord_bld
, coord
);
616 /* clamp to [0, length - 1] */
617 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
620 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
621 if (bld
->static_sampler_state
->normalized_coords
) {
622 /* scale coord to length */
623 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
626 offset
= lp_build_int_to_float(coord_bld
, offset
);
627 coord
= lp_build_add(coord_bld
, coord
, offset
);
629 coord
= lp_build_abs(coord_bld
, coord
);
631 /* itrunc == ifloor here */
632 icoord
= lp_build_itrunc(coord_bld
, coord
);
645 * Generate code to sample a mipmap level with nearest filtering.
646 * If sampling a cube texture, r = cube face in [0,5].
649 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
650 unsigned sampler_unit
,
652 LLVMValueRef row_stride_vec
,
653 LLVMValueRef img_stride_vec
,
654 LLVMValueRef data_ptr
,
655 LLVMValueRef mipoffsets
,
659 const LLVMValueRef
*offsets
,
660 LLVMValueRef colors_out
[4])
662 const unsigned dims
= bld
->dims
;
663 LLVMValueRef width_vec
;
664 LLVMValueRef height_vec
;
665 LLVMValueRef depth_vec
;
666 LLVMValueRef flt_size
;
667 LLVMValueRef flt_width_vec
;
668 LLVMValueRef flt_height_vec
;
669 LLVMValueRef flt_depth_vec
;
670 LLVMValueRef x
, y
= NULL
, z
= NULL
;
672 lp_build_extract_image_sizes(bld
,
676 &width_vec
, &height_vec
, &depth_vec
);
678 flt_size
= lp_build_int_to_float(&bld
->float_size_bld
, size
);
680 lp_build_extract_image_sizes(bld
,
681 &bld
->float_size_bld
,
684 &flt_width_vec
, &flt_height_vec
, &flt_depth_vec
);
687 * Compute integer texcoords.
689 x
= lp_build_sample_wrap_nearest(bld
, s
, width_vec
, flt_width_vec
, offsets
[0],
690 bld
->static_texture_state
->pot_width
,
691 bld
->static_sampler_state
->wrap_s
);
692 lp_build_name(x
, "tex.x.wrapped");
695 y
= lp_build_sample_wrap_nearest(bld
, t
, height_vec
, flt_height_vec
, offsets
[1],
696 bld
->static_texture_state
->pot_height
,
697 bld
->static_sampler_state
->wrap_t
);
698 lp_build_name(y
, "tex.y.wrapped");
701 z
= lp_build_sample_wrap_nearest(bld
, r
, depth_vec
, flt_depth_vec
, offsets
[2],
702 bld
->static_texture_state
->pot_depth
,
703 bld
->static_sampler_state
->wrap_r
);
704 lp_build_name(z
, "tex.z.wrapped");
707 if (bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE
||
708 bld
->static_texture_state
->target
== PIPE_TEXTURE_1D_ARRAY
||
709 bld
->static_texture_state
->target
== PIPE_TEXTURE_2D_ARRAY
) {
711 lp_build_name(z
, "tex.z.layer");
715 * Get texture colors.
717 lp_build_sample_texel_soa(bld
, sampler_unit
,
718 width_vec
, height_vec
, depth_vec
,
720 row_stride_vec
, img_stride_vec
,
721 data_ptr
, mipoffsets
, colors_out
);
726 * Generate code to sample a mipmap level with linear filtering.
727 * If sampling a cube texture, r = cube face in [0,5].
730 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
731 unsigned sampler_unit
,
733 LLVMValueRef row_stride_vec
,
734 LLVMValueRef img_stride_vec
,
735 LLVMValueRef data_ptr
,
736 LLVMValueRef mipoffsets
,
740 const LLVMValueRef
*offsets
,
741 LLVMValueRef colors_out
[4])
743 const unsigned dims
= bld
->dims
;
744 LLVMValueRef width_vec
;
745 LLVMValueRef height_vec
;
746 LLVMValueRef depth_vec
;
747 LLVMValueRef flt_size
;
748 LLVMValueRef flt_width_vec
;
749 LLVMValueRef flt_height_vec
;
750 LLVMValueRef flt_depth_vec
;
751 LLVMValueRef x0
, y0
= NULL
, z0
= NULL
, x1
, y1
= NULL
, z1
= NULL
;
752 LLVMValueRef s_fpart
, t_fpart
= NULL
, r_fpart
= NULL
;
753 LLVMValueRef neighbors
[2][2][4];
756 lp_build_extract_image_sizes(bld
,
760 &width_vec
, &height_vec
, &depth_vec
);
762 flt_size
= lp_build_int_to_float(&bld
->float_size_bld
, size
);
764 lp_build_extract_image_sizes(bld
,
765 &bld
->float_size_bld
,
768 &flt_width_vec
, &flt_height_vec
, &flt_depth_vec
);
771 * Compute integer texcoords.
773 lp_build_sample_wrap_linear(bld
, s
, width_vec
, flt_width_vec
, offsets
[0],
774 bld
->static_texture_state
->pot_width
,
775 bld
->static_sampler_state
->wrap_s
,
777 lp_build_name(x0
, "tex.x0.wrapped");
778 lp_build_name(x1
, "tex.x1.wrapped");
781 lp_build_sample_wrap_linear(bld
, t
, height_vec
, flt_height_vec
, offsets
[1],
782 bld
->static_texture_state
->pot_height
,
783 bld
->static_sampler_state
->wrap_t
,
785 lp_build_name(y0
, "tex.y0.wrapped");
786 lp_build_name(y1
, "tex.y1.wrapped");
789 lp_build_sample_wrap_linear(bld
, r
, depth_vec
, flt_depth_vec
, offsets
[2],
790 bld
->static_texture_state
->pot_depth
,
791 bld
->static_sampler_state
->wrap_r
,
793 lp_build_name(z0
, "tex.z0.wrapped");
794 lp_build_name(z1
, "tex.z1.wrapped");
797 if (bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE
||
798 bld
->static_texture_state
->target
== PIPE_TEXTURE_1D_ARRAY
||
799 bld
->static_texture_state
->target
== PIPE_TEXTURE_2D_ARRAY
) {
800 z0
= z1
= r
; /* cube face or array layer */
801 lp_build_name(z0
, "tex.z0.layer");
802 lp_build_name(z1
, "tex.z1.layer");
807 * Get texture colors.
809 /* get x0/x1 texels */
810 lp_build_sample_texel_soa(bld
, sampler_unit
,
811 width_vec
, height_vec
, depth_vec
,
813 row_stride_vec
, img_stride_vec
,
814 data_ptr
, mipoffsets
, neighbors
[0][0]);
815 lp_build_sample_texel_soa(bld
, sampler_unit
,
816 width_vec
, height_vec
, depth_vec
,
818 row_stride_vec
, img_stride_vec
,
819 data_ptr
, mipoffsets
, neighbors
[0][1]);
822 /* Interpolate two samples from 1D image to produce one color */
823 for (chan
= 0; chan
< 4; chan
++) {
824 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, s_fpart
,
825 neighbors
[0][0][chan
],
826 neighbors
[0][1][chan
],
832 LLVMValueRef colors0
[4];
834 /* get x0/x1 texels at y1 */
835 lp_build_sample_texel_soa(bld
, sampler_unit
,
836 width_vec
, height_vec
, depth_vec
,
838 row_stride_vec
, img_stride_vec
,
839 data_ptr
, mipoffsets
, neighbors
[1][0]);
840 lp_build_sample_texel_soa(bld
, sampler_unit
,
841 width_vec
, height_vec
, depth_vec
,
843 row_stride_vec
, img_stride_vec
,
844 data_ptr
, mipoffsets
, neighbors
[1][1]);
846 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
847 for (chan
= 0; chan
< 4; chan
++) {
848 colors0
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
850 neighbors
[0][0][chan
],
851 neighbors
[0][1][chan
],
852 neighbors
[1][0][chan
],
853 neighbors
[1][1][chan
],
858 LLVMValueRef neighbors1
[2][2][4];
859 LLVMValueRef colors1
[4];
861 /* get x0/x1/y0/y1 texels at z1 */
862 lp_build_sample_texel_soa(bld
, sampler_unit
,
863 width_vec
, height_vec
, depth_vec
,
865 row_stride_vec
, img_stride_vec
,
866 data_ptr
, mipoffsets
, neighbors1
[0][0]);
867 lp_build_sample_texel_soa(bld
, sampler_unit
,
868 width_vec
, height_vec
, depth_vec
,
870 row_stride_vec
, img_stride_vec
,
871 data_ptr
, mipoffsets
, neighbors1
[0][1]);
872 lp_build_sample_texel_soa(bld
, sampler_unit
,
873 width_vec
, height_vec
, depth_vec
,
875 row_stride_vec
, img_stride_vec
,
876 data_ptr
, mipoffsets
, neighbors1
[1][0]);
877 lp_build_sample_texel_soa(bld
, sampler_unit
,
878 width_vec
, height_vec
, depth_vec
,
880 row_stride_vec
, img_stride_vec
,
881 data_ptr
, mipoffsets
, neighbors1
[1][1]);
883 /* Bilinear interpolate the four samples from the second Z slice */
884 for (chan
= 0; chan
< 4; chan
++) {
885 colors1
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
887 neighbors1
[0][0][chan
],
888 neighbors1
[0][1][chan
],
889 neighbors1
[1][0][chan
],
890 neighbors1
[1][1][chan
],
894 /* Linearly interpolate the two samples from the two 3D slices */
895 for (chan
= 0; chan
< 4; chan
++) {
896 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
,
898 colors0
[chan
], colors1
[chan
],
904 for (chan
= 0; chan
< 4; chan
++) {
905 colors_out
[chan
] = colors0
[chan
];
913 * Sample the texture/mipmap using given image filter and mip filter.
914 * data0_ptr and data1_ptr point to the two mipmap levels to sample
915 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
916 * If we're using nearest miplevel sampling the '1' values will be null/unused.
919 lp_build_sample_mipmap(struct lp_build_sample_context
*bld
,
920 unsigned sampler_unit
,
926 const LLVMValueRef
*offsets
,
927 LLVMValueRef ilevel0
,
928 LLVMValueRef ilevel1
,
929 LLVMValueRef lod_fpart
,
930 LLVMValueRef
*colors_out
)
932 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
933 LLVMValueRef size0
= NULL
;
934 LLVMValueRef size1
= NULL
;
935 LLVMValueRef row_stride0_vec
= NULL
;
936 LLVMValueRef row_stride1_vec
= NULL
;
937 LLVMValueRef img_stride0_vec
= NULL
;
938 LLVMValueRef img_stride1_vec
= NULL
;
939 LLVMValueRef data_ptr0
= NULL
;
940 LLVMValueRef data_ptr1
= NULL
;
941 LLVMValueRef mipoff0
= NULL
;
942 LLVMValueRef mipoff1
= NULL
;
943 LLVMValueRef colors0
[4], colors1
[4];
946 /* sample the first mipmap level */
947 lp_build_mipmap_level_sizes(bld
, ilevel0
,
949 &row_stride0_vec
, &img_stride0_vec
);
950 if (bld
->num_lods
== 1) {
951 data_ptr0
= lp_build_get_mipmap_level(bld
, ilevel0
);
954 /* This path should work for num_lods 1 too but slightly less efficient */
955 data_ptr0
= bld
->base_ptr
;
956 mipoff0
= lp_build_get_mip_offsets(bld
, ilevel0
);
958 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
959 lp_build_sample_image_nearest(bld
, sampler_unit
,
961 row_stride0_vec
, img_stride0_vec
,
962 data_ptr0
, mipoff0
, s
, t
, r
, offsets
,
966 assert(img_filter
== PIPE_TEX_FILTER_LINEAR
);
967 lp_build_sample_image_linear(bld
, sampler_unit
,
969 row_stride0_vec
, img_stride0_vec
,
970 data_ptr0
, mipoff0
, s
, t
, r
, offsets
,
974 /* Store the first level's colors in the output variables */
975 for (chan
= 0; chan
< 4; chan
++) {
976 LLVMBuildStore(builder
, colors0
[chan
], colors_out
[chan
]);
979 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
980 struct lp_build_if_state if_ctx
;
981 LLVMValueRef need_lerp
;
983 /* need_lerp = lod_fpart > 0 */
984 if (bld
->num_lods
== 1) {
985 need_lerp
= LLVMBuildFCmp(builder
, LLVMRealUGT
,
986 lod_fpart
, bld
->levelf_bld
.zero
,
991 * We'll do mip filtering if any of the quads (or individual
992 * pixel in case of per-pixel lod) need it.
993 * It might be better to split the vectors here and only fetch/filter
994 * quads which need it.
997 * We unfortunately need to clamp lod_fpart here since we can get
998 * negative values which would screw up filtering if not all
999 * lod_fpart values have same sign.
1001 lod_fpart
= lp_build_max(&bld
->levelf_bld
, lod_fpart
,
1002 bld
->levelf_bld
.zero
);
1003 need_lerp
= lp_build_compare(bld
->gallivm
, bld
->levelf_bld
.type
,
1005 lod_fpart
, bld
->levelf_bld
.zero
);
1006 need_lerp
= lp_build_any_true_range(&bld
->leveli_bld
, bld
->num_lods
, need_lerp
);
1009 lp_build_if(&if_ctx
, bld
->gallivm
, need_lerp
);
1011 /* sample the second mipmap level */
1012 lp_build_mipmap_level_sizes(bld
, ilevel1
,
1014 &row_stride1_vec
, &img_stride1_vec
);
1015 if (bld
->num_lods
== 1) {
1016 data_ptr1
= lp_build_get_mipmap_level(bld
, ilevel1
);
1019 data_ptr1
= bld
->base_ptr
;
1020 mipoff1
= lp_build_get_mip_offsets(bld
, ilevel1
);
1022 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
1023 lp_build_sample_image_nearest(bld
, sampler_unit
,
1025 row_stride1_vec
, img_stride1_vec
,
1026 data_ptr1
, mipoff1
, s
, t
, r
, offsets
,
1030 lp_build_sample_image_linear(bld
, sampler_unit
,
1032 row_stride1_vec
, img_stride1_vec
,
1033 data_ptr1
, mipoff1
, s
, t
, r
, offsets
,
1037 /* interpolate samples from the two mipmap levels */
1039 if (bld
->num_lods
!= bld
->coord_type
.length
)
1040 lod_fpart
= lp_build_unpack_broadcast_aos_scalars(bld
->gallivm
,
1041 bld
->levelf_bld
.type
,
1042 bld
->texel_bld
.type
,
1045 for (chan
= 0; chan
< 4; chan
++) {
1046 colors0
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
1047 colors0
[chan
], colors1
[chan
],
1049 LLVMBuildStore(builder
, colors0
[chan
], colors_out
[chan
]);
1052 lp_build_endif(&if_ctx
);
1058 * Build (per-coord) layer value.
1059 * Either clamp layer to valid values or fill in optional out_of_bounds
1060 * value and just return value unclamped.
1063 lp_build_layer_coord(struct lp_build_sample_context
*bld
,
1064 unsigned texture_unit
,
1066 LLVMValueRef
*out_of_bounds
)
1068 LLVMValueRef num_layers
;
1069 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
1071 num_layers
= bld
->dynamic_state
->depth(bld
->dynamic_state
,
1072 bld
->gallivm
, texture_unit
);
1074 if (out_of_bounds
) {
1075 LLVMValueRef out1
, out
;
1076 num_layers
= lp_build_broadcast_scalar(int_coord_bld
, num_layers
);
1077 out
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, layer
, int_coord_bld
->zero
);
1078 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, layer
, num_layers
);
1079 *out_of_bounds
= lp_build_or(int_coord_bld
, out
, out1
);
1083 LLVMValueRef maxlayer
;
1084 maxlayer
= lp_build_sub(&bld
->int_bld
, num_layers
, bld
->int_bld
.one
);
1085 maxlayer
= lp_build_broadcast_scalar(int_coord_bld
, maxlayer
);
1086 return lp_build_clamp(int_coord_bld
, layer
, int_coord_bld
->zero
, maxlayer
);
1092 * Calculate cube face, lod, mip levels.
1095 lp_build_sample_common(struct lp_build_sample_context
*bld
,
1096 unsigned texture_index
,
1097 unsigned sampler_index
,
1101 const struct lp_derivatives
*derivs
, /* optional */
1102 LLVMValueRef lod_bias
, /* optional */
1103 LLVMValueRef explicit_lod
, /* optional */
1104 LLVMValueRef
*lod_ipart
,
1105 LLVMValueRef
*lod_fpart
,
1106 LLVMValueRef
*ilevel0
,
1107 LLVMValueRef
*ilevel1
)
1109 const unsigned mip_filter
= bld
->static_sampler_state
->min_mip_filter
;
1110 const unsigned min_filter
= bld
->static_sampler_state
->min_img_filter
;
1111 const unsigned mag_filter
= bld
->static_sampler_state
->mag_img_filter
;
1112 const unsigned target
= bld
->static_texture_state
->target
;
1113 LLVMValueRef first_level
, cube_rho
= NULL
;
1116 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1117 mip_filter, min_filter, mag_filter);
1121 * Choose cube face, recompute texcoords for the chosen face and
1122 * compute rho here too (as it requires transform of derivatives).
1124 if (target
== PIPE_TEXTURE_CUBE
) {
1125 LLVMValueRef face
, face_s
, face_t
;
1126 boolean need_derivs
;
1127 need_derivs
= ((min_filter
!= mag_filter
||
1128 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) &&
1129 !bld
->static_sampler_state
->min_max_lod_equal
&&
1131 lp_build_cube_lookup(bld
, *s
, *t
, *r
, derivs
, &face
, &face_s
, &face_t
,
1132 &cube_rho
, need_derivs
);
1133 *s
= face_s
; /* vec */
1134 *t
= face_t
; /* vec */
1135 /* use 'r' to indicate cube face */
1136 *r
= face
; /* vec */
1138 else if (target
== PIPE_TEXTURE_1D_ARRAY
) {
1139 *r
= lp_build_iround(&bld
->coord_bld
, *t
);
1140 *r
= lp_build_layer_coord(bld
, texture_index
, *r
, NULL
);
1142 else if (target
== PIPE_TEXTURE_2D_ARRAY
) {
1143 *r
= lp_build_iround(&bld
->coord_bld
, *r
);
1144 *r
= lp_build_layer_coord(bld
, texture_index
, *r
, NULL
);
1148 * Compute the level of detail (float).
1150 if (min_filter
!= mag_filter
||
1151 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
1152 /* Need to compute lod either to choose mipmap levels or to
1153 * distinguish between minification/magnification with one mipmap level.
1155 lp_build_lod_selector(bld
, texture_index
, sampler_index
,
1156 *s
, *t
, *r
, cube_rho
,
1157 derivs
, lod_bias
, explicit_lod
,
1159 lod_ipart
, lod_fpart
);
1161 *lod_ipart
= bld
->leveli_bld
.zero
;
1165 * Compute integer mipmap level(s) to fetch texels from: ilevel0, ilevel1
1167 switch (mip_filter
) {
1169 assert(0 && "bad mip_filter value in lp_build_sample_soa()");
1171 case PIPE_TEX_MIPFILTER_NONE
:
1172 /* always use mip level 0 */
1173 if (HAVE_LLVM
== 0x0207 && target
== PIPE_TEXTURE_CUBE
) {
1174 /* XXX this is a work-around for an apparent bug in LLVM 2.7.
1175 * We should be able to set ilevel0 = const(0) but that causes
1176 * bad x86 code to be emitted.
1179 lp_build_nearest_mip_level(bld
, texture_index
, *lod_ipart
, ilevel0
, NULL
);
1182 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
1183 bld
->gallivm
, texture_index
);
1184 first_level
= lp_build_broadcast_scalar(&bld
->leveli_bld
, first_level
);
1185 *ilevel0
= first_level
;
1188 case PIPE_TEX_MIPFILTER_NEAREST
:
1190 lp_build_nearest_mip_level(bld
, texture_index
, *lod_ipart
, ilevel0
, NULL
);
1192 case PIPE_TEX_MIPFILTER_LINEAR
:
1195 lp_build_linear_mip_levels(bld
, texture_index
,
1196 *lod_ipart
, lod_fpart
,
1203 * General texture sampling codegen.
1204 * This function handles texture sampling for all texture targets (1D,
1205 * 2D, 3D, cube) and all filtering modes.
1208 lp_build_sample_general(struct lp_build_sample_context
*bld
,
1209 unsigned sampler_unit
,
1213 const LLVMValueRef
*offsets
,
1214 LLVMValueRef lod_ipart
,
1215 LLVMValueRef lod_fpart
,
1216 LLVMValueRef ilevel0
,
1217 LLVMValueRef ilevel1
,
1218 LLVMValueRef
*colors_out
)
1220 struct lp_build_context
*int_bld
= &bld
->int_bld
;
1221 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1222 const unsigned mip_filter
= bld
->static_sampler_state
->min_mip_filter
;
1223 const unsigned min_filter
= bld
->static_sampler_state
->min_img_filter
;
1224 const unsigned mag_filter
= bld
->static_sampler_state
->mag_img_filter
;
1225 LLVMValueRef texels
[4];
1229 * Get/interpolate texture colors.
1232 for (chan
= 0; chan
< 4; ++chan
) {
1233 texels
[chan
] = lp_build_alloca(bld
->gallivm
, bld
->texel_bld
.vec_type
, "");
1234 lp_build_name(texels
[chan
], "sampler%u_texel_%c_var", sampler_unit
, "xyzw"[chan
]);
1237 if (min_filter
== mag_filter
) {
1238 /* no need to distinguish between minification and magnification */
1239 lp_build_sample_mipmap(bld
, sampler_unit
,
1240 min_filter
, mip_filter
,
1242 ilevel0
, ilevel1
, lod_fpart
,
1246 /* Emit conditional to choose min image filter or mag image filter
1247 * depending on the lod being > 0 or <= 0, respectively.
1249 struct lp_build_if_state if_ctx
;
1250 LLVMValueRef minify
;
1253 * XXX this should to all lods into account, if some are min
1254 * some max probably could hack up the coords/weights in the linear
1255 * path with selects to work for nearest.
1256 * If that's just two quads sitting next to each other it seems
1257 * quite ok to do the same filtering method on both though, at
1258 * least unless we have explicit lod (and who uses different
1259 * min/mag filter with that?)
1261 if (bld
->num_lods
> 1)
1262 lod_ipart
= LLVMBuildExtractElement(builder
, lod_ipart
,
1263 lp_build_const_int32(bld
->gallivm
, 0), "");
1265 /* minify = lod >= 0.0 */
1266 minify
= LLVMBuildICmp(builder
, LLVMIntSGE
,
1267 lod_ipart
, int_bld
->zero
, "");
1269 lp_build_if(&if_ctx
, bld
->gallivm
, minify
);
1271 /* Use the minification filter */
1272 lp_build_sample_mipmap(bld
, sampler_unit
,
1273 min_filter
, mip_filter
,
1275 ilevel0
, ilevel1
, lod_fpart
,
1278 lp_build_else(&if_ctx
);
1280 /* Use the magnification filter */
1281 lp_build_sample_mipmap(bld
, sampler_unit
,
1282 mag_filter
, PIPE_TEX_MIPFILTER_NONE
,
1284 ilevel0
, NULL
, NULL
,
1287 lp_build_endif(&if_ctx
);
1290 for (chan
= 0; chan
< 4; ++chan
) {
1291 colors_out
[chan
] = LLVMBuildLoad(builder
, texels
[chan
], "");
1292 lp_build_name(colors_out
[chan
], "sampler%u_texel_%c", sampler_unit
, "xyzw"[chan
]);
1298 * Texel fetch function.
1299 * In contrast to general sampling there is no filtering, no coord minification,
1300 * lod (if any) is always explicit uint, coords are uints (in terms of texel units)
1301 * directly to be applied to the selected mip level (after adding texel offsets).
1302 * This function handles texel fetch for all targets where texel fetch is supported
1303 * (no cube maps, but 1d, 2d, 3d are supported, arrays and buffers should be too).
1306 lp_build_fetch_texel(struct lp_build_sample_context
*bld
,
1307 unsigned texture_unit
,
1308 const LLVMValueRef
*coords
,
1309 LLVMValueRef explicit_lod
,
1310 const LLVMValueRef
*offsets
,
1311 LLVMValueRef
*colors_out
)
1313 struct lp_build_context
*perquadi_bld
= &bld
->leveli_bld
;
1314 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
1315 unsigned dims
= bld
->dims
, chan
;
1316 unsigned target
= bld
->static_texture_state
->target
;
1317 boolean out_of_bound_ret_zero
= TRUE
;
1318 LLVMValueRef size
, ilevel
;
1319 LLVMValueRef row_stride_vec
= NULL
, img_stride_vec
= NULL
;
1320 LLVMValueRef x
= coords
[0], y
= coords
[1], z
= coords
[2];
1321 LLVMValueRef width
, height
, depth
, i
, j
;
1322 LLVMValueRef offset
, out_of_bounds
, out1
;
1324 out_of_bounds
= int_coord_bld
->zero
;
1326 if (explicit_lod
&& bld
->static_texture_state
->target
!= PIPE_BUFFER
) {
1327 if (bld
->num_lods
!= int_coord_bld
->type
.length
) {
1328 ilevel
= lp_build_pack_aos_scalars(bld
->gallivm
, int_coord_bld
->type
,
1329 perquadi_bld
->type
, explicit_lod
, 0);
1332 ilevel
= explicit_lod
;
1334 lp_build_nearest_mip_level(bld
, texture_unit
, ilevel
, &ilevel
,
1335 out_of_bound_ret_zero
? &out_of_bounds
: NULL
);
1338 assert(bld
->num_lods
== 1);
1339 if (bld
->static_texture_state
->target
!= PIPE_BUFFER
) {
1340 ilevel
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
1341 bld
->gallivm
, texture_unit
);
1344 ilevel
= lp_build_const_int32(bld
->gallivm
, 0);
1347 lp_build_mipmap_level_sizes(bld
, ilevel
,
1349 &row_stride_vec
, &img_stride_vec
);
1350 lp_build_extract_image_sizes(bld
, &bld
->int_size_bld
, int_coord_bld
->type
,
1351 size
, &width
, &height
, &depth
);
1353 if (target
== PIPE_TEXTURE_1D_ARRAY
||
1354 target
== PIPE_TEXTURE_2D_ARRAY
) {
1355 if (target
== PIPE_TEXTURE_1D_ARRAY
) {
1358 if (out_of_bound_ret_zero
) {
1359 z
= lp_build_layer_coord(bld
, texture_unit
, z
, &out1
);
1360 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1363 z
= lp_build_layer_coord(bld
, texture_unit
, z
, NULL
);
1367 /* This is a lot like border sampling */
1370 * coords are really unsigned, offsets are signed, but I don't think
1371 * exceeding 31 bits is possible
1373 x
= lp_build_add(int_coord_bld
, x
, offsets
[0]);
1375 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
1376 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1377 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
1378 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1382 y
= lp_build_add(int_coord_bld
, y
, offsets
[1]);
1384 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
1385 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1386 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
1387 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1391 z
= lp_build_add(int_coord_bld
, z
, offsets
[2]);
1393 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
1394 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1395 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
1396 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
1400 lp_build_sample_offset(int_coord_bld
,
1402 x
, y
, z
, row_stride_vec
, img_stride_vec
,
1405 if (bld
->static_texture_state
->target
!= PIPE_BUFFER
) {
1406 offset
= lp_build_add(int_coord_bld
, offset
,
1407 lp_build_get_mip_offsets(bld
, ilevel
));
1410 offset
= lp_build_andnot(int_coord_bld
, offset
, out_of_bounds
);
1412 lp_build_fetch_rgba_soa(bld
->gallivm
,
1415 bld
->base_ptr
, offset
,
1419 if (out_of_bound_ret_zero
) {
1421 * Only needed for ARB_robust_buffer_access_behavior and d3d10.
1422 * Could use min/max above instead of out-of-bounds comparisons
1423 * if we don't care about the result returned for out-of-bounds.
1425 for (chan
= 0; chan
< 4; chan
++) {
1426 colors_out
[chan
] = lp_build_select(&bld
->texel_bld
, out_of_bounds
,
1427 bld
->texel_bld
.zero
, colors_out
[chan
]);
1434 * Do shadow test/comparison.
1435 * \param coords incoming texcoords
1436 * \param texel the texel to compare against (use the X channel)
1437 * Ideally this should really be done per-sample.
1440 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
1441 const LLVMValueRef
*coords
,
1442 LLVMValueRef texel
[4])
1444 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1445 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1446 LLVMValueRef res
, p
;
1447 const unsigned chan
= 0;
1449 const struct util_format_description
*format_desc
;
1451 if (bld
->static_sampler_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
1454 if (bld
->static_texture_state
->target
== PIPE_TEXTURE_2D_ARRAY
||
1455 bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE
) {
1464 LLVMValueRef indx
= lp_build_const_int32(bld
->gallivm
, 0);
1465 LLVMValueRef coord
= LLVMBuildExtractElement(builder
, p
, indx
, "");
1466 LLVMValueRef tex
= LLVMBuildExtractElement(builder
, texel
[chan
], indx
, "");
1467 lp_build_printf(bld
->gallivm
, "shadow compare coord %f to texture %f\n",
1471 /* Clamp p coords to [0,1] for fixed function depth texture format */
1472 format_desc
= util_format_description(bld
->static_texture_state
->format
);
1473 /* not entirely sure we couldn't end up with non-valid swizzle here */
1474 chan_type
= format_desc
->swizzle
[0] <= UTIL_FORMAT_SWIZZLE_W
?
1475 format_desc
->channel
[format_desc
->swizzle
[0]].type
:
1476 UTIL_FORMAT_TYPE_FLOAT
;
1477 if (chan_type
!= UTIL_FORMAT_TYPE_FLOAT
) {
1478 p
= lp_build_clamp(&bld
->coord_bld
, p
,
1479 bld
->coord_bld
.zero
, bld
->coord_bld
.one
);
1483 * technically this is not entirely correct for unorm depth as the ref value
1484 * should be converted to the depth format (quantization!) and comparison
1485 * then done in texture format.
1488 /* result = (p FUNC texel) ? 1 : 0 */
1490 * honor d3d10 floating point rules here, which state that comparisons
1491 * are ordered except NOT_EQUAL which is unordered.
1493 if (bld
->static_sampler_state
->compare_func
!= PIPE_FUNC_NOTEQUAL
) {
1494 res
= lp_build_cmp_ordered(texel_bld
, bld
->static_sampler_state
->compare_func
,
1498 res
= lp_build_cmp(texel_bld
, bld
->static_sampler_state
->compare_func
,
1501 res
= lp_build_select(texel_bld
, res
, texel_bld
->one
, texel_bld
->zero
);
1504 * returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE.
1505 * This should be ok because sampler swizzle is applied on top of it.
1510 texel
[3] = texel_bld
->one
;
1515 * Just set texels to white instead of actually sampling the texture.
1519 lp_build_sample_nop(struct gallivm_state
*gallivm
,
1520 struct lp_type type
,
1521 const LLVMValueRef
*coords
,
1522 LLVMValueRef texel_out
[4])
1524 LLVMValueRef one
= lp_build_one(gallivm
, type
);
1527 for (chan
= 0; chan
< 4; chan
++) {
1528 texel_out
[chan
] = one
;
1534 * Build texture sampling code.
1535 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1537 * \param type vector float type to use for coords, etc.
1538 * \param is_fetch if this is a texel fetch instruction.
1539 * \param derivs partial derivatives of (s,t,r,q) with respect to x and y
1542 lp_build_sample_soa(struct gallivm_state
*gallivm
,
1543 const struct lp_static_texture_state
*static_texture_state
,
1544 const struct lp_static_sampler_state
*static_sampler_state
,
1545 struct lp_sampler_dynamic_state
*dynamic_state
,
1546 struct lp_type type
,
1548 unsigned texture_index
,
1549 unsigned sampler_index
,
1550 const LLVMValueRef
*coords
,
1551 const LLVMValueRef
*offsets
,
1552 const struct lp_derivatives
*derivs
, /* optional */
1553 LLVMValueRef lod_bias
, /* optional */
1554 LLVMValueRef explicit_lod
, /* optional */
1556 LLVMValueRef texel_out
[4])
1558 unsigned dims
= texture_dims(static_texture_state
->target
);
1559 unsigned num_quads
= type
.length
/ 4;
1560 unsigned mip_filter
;
1561 struct lp_build_sample_context bld
;
1562 struct lp_static_sampler_state derived_sampler_state
= *static_sampler_state
;
1563 LLVMTypeRef i32t
= LLVMInt32TypeInContext(gallivm
->context
);
1564 LLVMBuilderRef builder
= gallivm
->builder
;
1565 LLVMValueRef tex_width
;
1571 enum pipe_format fmt
= static_texture_state
->format
;
1572 debug_printf("Sample from %s\n", util_format_name(fmt
));
1575 assert(type
.floating
);
1577 /* Setup our build context */
1578 memset(&bld
, 0, sizeof bld
);
1579 bld
.gallivm
= gallivm
;
1580 bld
.static_sampler_state
= &derived_sampler_state
;
1581 bld
.static_texture_state
= static_texture_state
;
1582 bld
.dynamic_state
= dynamic_state
;
1583 bld
.format_desc
= util_format_description(static_texture_state
->format
);
1586 bld
.vector_width
= lp_type_width(type
);
1588 bld
.float_type
= lp_type_float(32);
1589 bld
.int_type
= lp_type_int(32);
1590 bld
.coord_type
= type
;
1591 bld
.int_coord_type
= lp_int_type(type
);
1592 bld
.float_size_in_type
= lp_type_float(32);
1593 bld
.float_size_in_type
.length
= dims
> 1 ? 4 : 1;
1594 bld
.int_size_in_type
= lp_int_type(bld
.float_size_in_type
);
1595 bld
.texel_type
= type
;
1597 /* always using the first channel hopefully should be safe,
1598 * if not things WILL break in other places anyway.
1600 if (bld
.format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
&&
1601 bld
.format_desc
->channel
[0].pure_integer
) {
1602 if (bld
.format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_SIGNED
) {
1603 bld
.texel_type
= lp_type_int_vec(type
.width
, type
.width
* type
.length
);
1605 else if (bld
.format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
1606 bld
.texel_type
= lp_type_uint_vec(type
.width
, type
.width
* type
.length
);
1609 else if (util_format_has_stencil(bld
.format_desc
) &&
1610 !util_format_has_depth(bld
.format_desc
)) {
1611 /* for stencil only formats, sample stencil (uint) */
1612 bld
.texel_type
= lp_type_int_vec(type
.width
, type
.width
* type
.length
);
1615 if (!static_texture_state
->level_zero_only
) {
1616 derived_sampler_state
.min_mip_filter
= static_sampler_state
->min_mip_filter
;
1618 derived_sampler_state
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
1620 mip_filter
= derived_sampler_state
.min_mip_filter
;
1623 debug_printf(" .min_mip_filter = %u\n", derived_sampler_state
.min_mip_filter
);
1627 * This is all a bit complicated different paths are chosen for performance
1629 * Essentially, there can be 1 lod per element, 1 lod per quad or 1 lod for
1630 * everything (the last two options are equivalent for 4-wide case).
1631 * If there's per-quad lod but we split to 4-wide so we can use AoS, per-quad
1632 * lod is calculated then the lod value extracted afterwards so making this
1633 * case basically the same as far as lod handling is concerned for the
1634 * further sample/filter code as the 1 lod for everything case.
1635 * Different lod handling mostly shows up when building mipmap sizes
1636 * (lp_build_mipmap_level_sizes() and friends) and also in filtering
1637 * (getting the fractional part of the lod to the right texels).
1641 * There are other situations where at least the multiple int lods could be
1642 * avoided like min and max lod being equal.
1644 if (explicit_lod
&& !scalar_lod
&&
1645 ((is_fetch
&& bld
.static_texture_state
->target
!= PIPE_BUFFER
) ||
1646 (!is_fetch
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)))
1647 bld
.num_lods
= type
.length
;
1648 /* TODO: for true scalar_lod should only use 1 lod value */
1649 else if ((is_fetch
&& explicit_lod
&& bld
.static_texture_state
->target
!= PIPE_BUFFER
) ||
1650 (!is_fetch
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)) {
1651 bld
.num_lods
= num_quads
;
1657 bld
.levelf_type
= type
;
1658 /* we want native vector size to be able to use our intrinsics */
1659 if (bld
.num_lods
!= type
.length
) {
1660 bld
.levelf_type
.length
= type
.length
> 4 ? ((type
.length
+ 15) / 16) * 4 : 1;
1662 bld
.leveli_type
= lp_int_type(bld
.levelf_type
);
1663 bld
.float_size_type
= bld
.float_size_in_type
;
1664 /* Note: size vectors may not be native. They contain minified w/h/d/_ values,
1665 * with per-element lod that is w0/h0/d0/_/w1/h1/d1_/... so up to 8x4f32 */
1666 if (bld
.num_lods
> 1) {
1667 bld
.float_size_type
.length
= bld
.num_lods
== type
.length
?
1668 bld
.num_lods
* bld
.float_size_in_type
.length
:
1671 bld
.int_size_type
= lp_int_type(bld
.float_size_type
);
1673 lp_build_context_init(&bld
.float_bld
, gallivm
, bld
.float_type
);
1674 lp_build_context_init(&bld
.float_vec_bld
, gallivm
, type
);
1675 lp_build_context_init(&bld
.int_bld
, gallivm
, bld
.int_type
);
1676 lp_build_context_init(&bld
.coord_bld
, gallivm
, bld
.coord_type
);
1677 lp_build_context_init(&bld
.int_coord_bld
, gallivm
, bld
.int_coord_type
);
1678 lp_build_context_init(&bld
.int_size_in_bld
, gallivm
, bld
.int_size_in_type
);
1679 lp_build_context_init(&bld
.float_size_in_bld
, gallivm
, bld
.float_size_in_type
);
1680 lp_build_context_init(&bld
.int_size_bld
, gallivm
, bld
.int_size_type
);
1681 lp_build_context_init(&bld
.float_size_bld
, gallivm
, bld
.float_size_type
);
1682 lp_build_context_init(&bld
.texel_bld
, gallivm
, bld
.texel_type
);
1683 lp_build_context_init(&bld
.levelf_bld
, gallivm
, bld
.levelf_type
);
1684 lp_build_context_init(&bld
.leveli_bld
, gallivm
, bld
.leveli_type
);
1686 /* Get the dynamic state */
1687 tex_width
= dynamic_state
->width(dynamic_state
, gallivm
, texture_index
);
1688 bld
.row_stride_array
= dynamic_state
->row_stride(dynamic_state
, gallivm
, texture_index
);
1689 bld
.img_stride_array
= dynamic_state
->img_stride(dynamic_state
, gallivm
, texture_index
);
1690 bld
.base_ptr
= dynamic_state
->base_ptr(dynamic_state
, gallivm
, texture_index
);
1691 bld
.mip_offsets
= dynamic_state
->mip_offsets(dynamic_state
, gallivm
, texture_index
);
1692 /* Note that mip_offsets is an array[level] of offsets to texture images */
1698 /* width, height, depth as single int vector */
1700 bld
.int_size
= tex_width
;
1703 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size_in_bld
.undef
,
1704 tex_width
, LLVMConstInt(i32t
, 0, 0), "");
1706 LLVMValueRef tex_height
=
1707 dynamic_state
->height(dynamic_state
, gallivm
, texture_index
);
1708 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size
,
1709 tex_height
, LLVMConstInt(i32t
, 1, 0), "");
1711 LLVMValueRef tex_depth
=
1712 dynamic_state
->depth(dynamic_state
, gallivm
, texture_index
);
1713 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size
,
1714 tex_depth
, LLVMConstInt(i32t
, 2, 0), "");
1720 /* For debug: no-op texture sampling */
1721 lp_build_sample_nop(gallivm
,
1727 else if (is_fetch
) {
1728 lp_build_fetch_texel(&bld
, texture_index
, coords
,
1729 explicit_lod
, offsets
,
1734 LLVMValueRef lod_ipart
= NULL
, lod_fpart
= NULL
;
1735 LLVMValueRef ilevel0
= NULL
, ilevel1
= NULL
;
1736 boolean use_aos
= util_format_fits_8unorm(bld
.format_desc
) &&
1737 lp_is_simple_wrap_mode(static_sampler_state
->wrap_s
) &&
1738 lp_is_simple_wrap_mode(static_sampler_state
->wrap_t
);
1740 if ((gallivm_debug
& GALLIVM_DEBUG_PERF
) &&
1741 !use_aos
&& util_format_fits_8unorm(bld
.format_desc
)) {
1742 debug_printf("%s: using floating point linear filtering for %s\n",
1743 __FUNCTION__
, bld
.format_desc
->short_name
);
1744 debug_printf(" min_img %d mag_img %d mip %d wraps %d wrapt %d\n",
1745 static_sampler_state
->min_img_filter
,
1746 static_sampler_state
->mag_img_filter
,
1747 static_sampler_state
->min_mip_filter
,
1748 static_sampler_state
->wrap_s
,
1749 static_sampler_state
->wrap_t
);
1752 lp_build_sample_common(&bld
, texture_index
, sampler_index
,
1754 derivs
, lod_bias
, explicit_lod
,
1755 &lod_ipart
, &lod_fpart
,
1756 &ilevel0
, &ilevel1
);
1759 * we only try 8-wide sampling with soa as it appears to
1760 * be a loss with aos with AVX (but it should work).
1761 * (It should be faster if we'd support avx2)
1763 if (num_quads
== 1 || !use_aos
) {
1765 if (num_quads
> 1) {
1766 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
1767 LLVMValueRef index0
= lp_build_const_int32(gallivm
, 0);
1769 * These parameters are the same for all quads,
1770 * could probably simplify.
1772 lod_ipart
= LLVMBuildExtractElement(builder
, lod_ipart
, index0
, "");
1773 ilevel0
= LLVMBuildExtractElement(builder
, ilevel0
, index0
, "");
1777 /* do sampling/filtering with fixed pt arithmetic */
1778 lp_build_sample_aos(&bld
, sampler_index
,
1780 lod_ipart
, lod_fpart
,
1786 lp_build_sample_general(&bld
, sampler_index
,
1788 lod_ipart
, lod_fpart
,
1795 struct lp_build_sample_context bld4
;
1796 struct lp_type type4
= type
;
1798 LLVMValueRef texelout4
[4];
1799 LLVMValueRef texelouttmp
[4][LP_MAX_VECTOR_LENGTH
/16];
1803 /* Setup our build context */
1804 memset(&bld4
, 0, sizeof bld4
);
1805 bld4
.gallivm
= bld
.gallivm
;
1806 bld4
.static_texture_state
= bld
.static_texture_state
;
1807 bld4
.static_sampler_state
= bld
.static_sampler_state
;
1808 bld4
.dynamic_state
= bld
.dynamic_state
;
1809 bld4
.format_desc
= bld
.format_desc
;
1810 bld4
.dims
= bld
.dims
;
1811 bld4
.row_stride_array
= bld
.row_stride_array
;
1812 bld4
.img_stride_array
= bld
.img_stride_array
;
1813 bld4
.base_ptr
= bld
.base_ptr
;
1814 bld4
.mip_offsets
= bld
.mip_offsets
;
1815 bld4
.int_size
= bld
.int_size
;
1817 bld4
.vector_width
= lp_type_width(type4
);
1819 bld4
.float_type
= lp_type_float(32);
1820 bld4
.int_type
= lp_type_int(32);
1821 bld4
.coord_type
= type4
;
1822 bld4
.int_coord_type
= lp_int_type(type4
);
1823 bld4
.float_size_in_type
= lp_type_float(32);
1824 bld4
.float_size_in_type
.length
= dims
> 1 ? 4 : 1;
1825 bld4
.int_size_in_type
= lp_int_type(bld4
.float_size_in_type
);
1826 bld4
.texel_type
= bld
.texel_type
;
1827 bld4
.texel_type
.length
= 4;
1828 bld4
.levelf_type
= type4
;
1829 /* we want native vector size to be able to use our intrinsics */
1830 bld4
.levelf_type
.length
= 1;
1831 bld4
.leveli_type
= lp_int_type(bld4
.levelf_type
);
1833 if (explicit_lod
&& !scalar_lod
&&
1834 ((is_fetch
&& bld
.static_texture_state
->target
!= PIPE_BUFFER
) ||
1835 (!is_fetch
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)))
1836 bld4
.num_lods
= type4
.length
;
1840 bld4
.levelf_type
= type4
;
1841 /* we want native vector size to be able to use our intrinsics */
1842 if (bld4
.num_lods
!= type4
.length
) {
1843 bld4
.levelf_type
.length
= 1;
1845 bld4
.leveli_type
= lp_int_type(bld4
.levelf_type
);
1846 bld4
.float_size_type
= bld4
.float_size_in_type
;
1847 if (bld4
.num_lods
> 1) {
1848 bld4
.float_size_type
.length
= bld4
.num_lods
== type4
.length
?
1849 bld4
.num_lods
* bld4
.float_size_in_type
.length
:
1852 bld4
.int_size_type
= lp_int_type(bld4
.float_size_type
);
1854 lp_build_context_init(&bld4
.float_bld
, gallivm
, bld4
.float_type
);
1855 lp_build_context_init(&bld4
.float_vec_bld
, gallivm
, type4
);
1856 lp_build_context_init(&bld4
.int_bld
, gallivm
, bld4
.int_type
);
1857 lp_build_context_init(&bld4
.coord_bld
, gallivm
, bld4
.coord_type
);
1858 lp_build_context_init(&bld4
.int_coord_bld
, gallivm
, bld4
.int_coord_type
);
1859 lp_build_context_init(&bld4
.int_size_in_bld
, gallivm
, bld4
.int_size_in_type
);
1860 lp_build_context_init(&bld4
.float_size_in_bld
, gallivm
, bld4
.float_size_in_type
);
1861 lp_build_context_init(&bld4
.int_size_bld
, gallivm
, bld4
.int_size_type
);
1862 lp_build_context_init(&bld4
.float_size_bld
, gallivm
, bld4
.float_size_type
);
1863 lp_build_context_init(&bld4
.texel_bld
, gallivm
, bld4
.texel_type
);
1864 lp_build_context_init(&bld4
.levelf_bld
, gallivm
, bld4
.levelf_type
);
1865 lp_build_context_init(&bld4
.leveli_bld
, gallivm
, bld4
.leveli_type
);
1867 for (i
= 0; i
< num_quads
; i
++) {
1868 LLVMValueRef s4
, t4
, r4
;
1869 LLVMValueRef lod_ipart4
, lod_fpart4
= NULL
;
1870 LLVMValueRef ilevel04
, ilevel14
= NULL
;
1871 LLVMValueRef offsets4
[4] = { NULL
};
1872 unsigned num_lods
= bld4
.num_lods
;
1874 s4
= lp_build_extract_range(gallivm
, s
, 4*i
, 4);
1875 t4
= lp_build_extract_range(gallivm
, t
, 4*i
, 4);
1876 r4
= lp_build_extract_range(gallivm
, r
, 4*i
, 4);
1879 offsets4
[0] = lp_build_extract_range(gallivm
, offsets
[0], 4*i
, 4);
1881 offsets4
[1] = lp_build_extract_range(gallivm
, offsets
[1], 4*i
, 4);
1883 offsets4
[2] = lp_build_extract_range(gallivm
, offsets
[2], 4*i
, 4);
1887 lod_ipart4
= lp_build_extract_range(gallivm
, lod_ipart
, num_lods
* i
, num_lods
);
1888 ilevel04
= lp_build_extract_range(gallivm
, ilevel0
, num_lods
* i
, num_lods
);
1889 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1890 ilevel14
= lp_build_extract_range(gallivm
, ilevel1
, num_lods
* i
, num_lods
);
1891 lod_fpart4
= lp_build_extract_range(gallivm
, lod_fpart
, num_lods
* i
, num_lods
);
1895 /* do sampling/filtering with fixed pt arithmetic */
1896 lp_build_sample_aos(&bld4
, sampler_index
,
1897 s4
, t4
, r4
, offsets4
,
1898 lod_ipart4
, lod_fpart4
,
1904 lp_build_sample_general(&bld4
, sampler_index
,
1905 s4
, t4
, r4
, offsets4
,
1906 lod_ipart4
, lod_fpart4
,
1910 for (j
= 0; j
< 4; j
++) {
1911 texelouttmp
[j
][i
] = texelout4
[j
];
1915 for (j
= 0; j
< 4; j
++) {
1916 texel_out
[j
] = lp_build_concat(gallivm
, texelouttmp
[j
], type4
, num_quads
);
1920 lp_build_sample_compare(&bld
, coords
, texel_out
);
1923 if (static_texture_state
->target
!= PIPE_BUFFER
) {
1924 apply_sampler_swizzle(&bld
, texel_out
);
1928 * texel type can be a (32bit) int/uint (for pure int formats only),
1929 * however we are expected to always return floats (storage is untyped).
1931 if (!bld
.texel_type
.floating
) {
1933 for (chan
= 0; chan
< 4; chan
++) {
1934 texel_out
[chan
] = LLVMBuildBitCast(builder
, texel_out
[chan
],
1935 lp_build_vec_type(gallivm
, type
), "");
1941 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
1942 const struct lp_static_texture_state
*static_state
,
1943 struct lp_sampler_dynamic_state
*dynamic_state
,
1944 struct lp_type int_type
,
1945 unsigned texture_unit
,
1946 boolean need_nr_mips
,
1948 LLVMValueRef explicit_lod
,
1949 LLVMValueRef
*sizes_out
)
1953 LLVMValueRef first_level
= NULL
;
1956 struct lp_build_context bld_int_vec
;
1958 dims
= texture_dims(static_state
->target
);
1960 switch (static_state
->target
) {
1961 case PIPE_TEXTURE_1D_ARRAY
:
1962 case PIPE_TEXTURE_2D_ARRAY
:
1970 assert(!int_type
.floating
);
1972 lp_build_context_init(&bld_int_vec
, gallivm
, lp_type_int_vec(32, 128));
1975 /* FIXME: this needs to honor per-element lod */
1976 lod
= LLVMBuildExtractElement(gallivm
->builder
, explicit_lod
, lp_build_const_int32(gallivm
, 0), "");
1977 first_level
= dynamic_state
->first_level(dynamic_state
, gallivm
, texture_unit
);
1978 lod
= lp_build_broadcast_scalar(&bld_int_vec
,
1979 LLVMBuildAdd(gallivm
->builder
, lod
, first_level
, "lod"));
1982 lod
= bld_int_vec
.zero
;
1986 size
= bld_int_vec
.zero
;
1989 size
= bld_int_vec
.undef
;
1992 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
1993 dynamic_state
->width(dynamic_state
, gallivm
, texture_unit
),
1994 lp_build_const_int32(gallivm
, 0), "");
1997 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
1998 dynamic_state
->height(dynamic_state
, gallivm
, texture_unit
),
1999 lp_build_const_int32(gallivm
, 1), "");
2003 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
2004 dynamic_state
->depth(dynamic_state
, gallivm
, texture_unit
),
2005 lp_build_const_int32(gallivm
, 2), "");
2008 size
= lp_build_minify(&bld_int_vec
, size
, lod
);
2011 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
2012 dynamic_state
->depth(dynamic_state
, gallivm
, texture_unit
),
2013 lp_build_const_int32(gallivm
, dims
), "");
2016 * XXX for out-of-bounds lod, should set size to zero vector here
2017 * (for dx10-style only, i.e. need_nr_mips)
2020 for (i
= 0; i
< dims
+ (has_array
? 1 : 0); i
++) {
2021 sizes_out
[i
] = lp_build_extract_broadcast(gallivm
, bld_int_vec
.type
, int_type
,
2023 lp_build_const_int32(gallivm
, i
));
2027 * if there's no explicit_lod (buffers, rects) queries requiring nr of
2028 * mips would be illegal.
2030 if (need_nr_mips
&& explicit_lod
) {
2031 struct lp_build_context bld_int_scalar
;
2032 LLVMValueRef num_levels
;
2033 lp_build_context_init(&bld_int_scalar
, gallivm
, lp_type_int(32));
2035 if (static_state
->level_zero_only
) {
2036 num_levels
= bld_int_scalar
.one
;
2039 LLVMValueRef last_level
;
2041 last_level
= dynamic_state
->last_level(dynamic_state
, gallivm
, texture_unit
);
2042 num_levels
= lp_build_sub(&bld_int_scalar
, last_level
, first_level
);
2043 num_levels
= lp_build_add(&bld_int_scalar
, num_levels
, bld_int_scalar
.one
);
2045 sizes_out
[3] = lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, int_type
),