1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Texture sampling -- SoA.
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 * @author Brian Paul <brianp@vmware.com>
36 #include "pipe/p_defines.h"
37 #include "pipe/p_state.h"
38 #include "pipe/p_shader_tokens.h"
39 #include "util/u_debug.h"
40 #include "util/u_dump.h"
41 #include "util/u_memory.h"
42 #include "util/u_math.h"
43 #include "util/u_format.h"
44 #include "util/u_cpu_detect.h"
45 #include "util/u_format_rgb9e5.h"
46 #include "lp_bld_debug.h"
47 #include "lp_bld_type.h"
48 #include "lp_bld_const.h"
49 #include "lp_bld_conv.h"
50 #include "lp_bld_arit.h"
51 #include "lp_bld_bitarit.h"
52 #include "lp_bld_logic.h"
53 #include "lp_bld_printf.h"
54 #include "lp_bld_swizzle.h"
55 #include "lp_bld_flow.h"
56 #include "lp_bld_gather.h"
57 #include "lp_bld_format.h"
58 #include "lp_bld_sample.h"
59 #include "lp_bld_sample_aos.h"
60 #include "lp_bld_struct.h"
61 #include "lp_bld_quad.h"
62 #include "lp_bld_pack.h"
66 * Generate code to fetch a texel from a texture at int coords (x, y, z).
67 * The computation depends on whether the texture is 1D, 2D or 3D.
68 * The result, texel, will be float vectors:
69 * texel[0] = red values
70 * texel[1] = green values
71 * texel[2] = blue values
72 * texel[3] = alpha values
75 lp_build_sample_texel_soa(struct lp_build_sample_context
*bld
,
82 LLVMValueRef y_stride
,
83 LLVMValueRef z_stride
,
84 LLVMValueRef data_ptr
,
85 LLVMValueRef mipoffsets
,
86 LLVMValueRef texel_out
[4])
88 const struct lp_static_sampler_state
*static_state
= bld
->static_sampler_state
;
89 const unsigned dims
= bld
->dims
;
90 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
91 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
94 LLVMValueRef use_border
= NULL
;
96 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
97 if (lp_sampler_wrap_mode_uses_border_color(static_state
->wrap_s
,
98 static_state
->min_img_filter
,
99 static_state
->mag_img_filter
)) {
101 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
102 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
103 use_border
= LLVMBuildOr(builder
, b1
, b2
, "b1_or_b2");
107 lp_sampler_wrap_mode_uses_border_color(static_state
->wrap_t
,
108 static_state
->min_img_filter
,
109 static_state
->mag_img_filter
)) {
111 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
112 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
114 use_border
= LLVMBuildOr(builder
, use_border
, b1
, "ub_or_b1");
115 use_border
= LLVMBuildOr(builder
, use_border
, b2
, "ub_or_b2");
118 use_border
= LLVMBuildOr(builder
, b1
, b2
, "b1_or_b2");
123 lp_sampler_wrap_mode_uses_border_color(static_state
->wrap_r
,
124 static_state
->min_img_filter
,
125 static_state
->mag_img_filter
)) {
127 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
128 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
130 use_border
= LLVMBuildOr(builder
, use_border
, b1
, "ub_or_b1");
131 use_border
= LLVMBuildOr(builder
, use_border
, b2
, "ub_or_b2");
134 use_border
= LLVMBuildOr(builder
, b1
, b2
, "b1_or_b2");
138 /* convert x,y,z coords to linear offset from start of texture, in bytes */
139 lp_build_sample_offset(&bld
->int_coord_bld
,
141 x
, y
, z
, y_stride
, z_stride
,
144 offset
= lp_build_add(&bld
->int_coord_bld
, offset
, mipoffsets
);
148 /* If we can sample the border color, it means that texcoords may
149 * lie outside the bounds of the texture image. We need to do
150 * something to prevent reading out of bounds and causing a segfault.
152 * Simply AND the texture coords with !use_border. This will cause
153 * coords which are out of bounds to become zero. Zero's guaranteed
154 * to be inside the texture image.
156 offset
= lp_build_andnot(&bld
->int_coord_bld
, offset
, use_border
);
159 lp_build_fetch_rgba_soa(bld
->gallivm
,
168 * Note: if we find an app which frequently samples the texture border
169 * we might want to implement a true conditional here to avoid sampling
170 * the texture whenever possible (since that's quite a bit of code).
173 * texel = border_color;
176 * texel = sample_texture(coord);
178 * As it is now, we always sample the texture, then selectively replace
179 * the texel color results with the border color.
183 /* select texel color or border color depending on use_border. */
184 const struct util_format_description
*format_desc
= bld
->format_desc
;
186 struct lp_type border_type
= bld
->texel_type
;
187 border_type
.length
= 4;
189 * Only replace channels which are actually present. The others should
190 * get optimized away eventually by sampler_view swizzle anyway but it's
193 for (chan
= 0; chan
< 4; chan
++) {
195 /* reverse-map channel... */
196 for (chan_s
= 0; chan_s
< 4; chan_s
++) {
197 if (chan_s
== format_desc
->swizzle
[chan
]) {
202 /* use the already clamped color */
203 LLVMValueRef idx
= lp_build_const_int32(bld
->gallivm
, chan
);
204 LLVMValueRef border_chan
;
206 border_chan
= lp_build_extract_broadcast(bld
->gallivm
,
209 bld
->border_color_clamped
,
211 texel_out
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
212 border_chan
, texel_out
[chan
]);
220 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
223 lp_build_coord_mirror(struct lp_build_sample_context
*bld
,
226 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
227 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
228 LLVMValueRef fract
, flr
, isOdd
;
230 lp_build_ifloor_fract(coord_bld
, coord
, &flr
, &fract
);
232 /* isOdd = flr & 1 */
233 isOdd
= LLVMBuildAnd(bld
->gallivm
->builder
, flr
, int_coord_bld
->one
, "");
235 /* make coord positive or negative depending on isOdd */
236 coord
= lp_build_set_sign(coord_bld
, fract
, isOdd
);
238 /* convert isOdd to float */
239 isOdd
= lp_build_int_to_float(coord_bld
, isOdd
);
241 /* add isOdd to coord */
242 coord
= lp_build_add(coord_bld
, coord
, isOdd
);
249 * Helper to compute the first coord and the weight for
250 * linear wrap repeat npot textures
253 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
254 LLVMValueRef coord_f
,
255 LLVMValueRef length_i
,
256 LLVMValueRef length_f
,
257 LLVMValueRef
*coord0_i
,
258 LLVMValueRef
*weight_f
)
260 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
261 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
262 LLVMValueRef half
= lp_build_const_vec(bld
->gallivm
, coord_bld
->type
, 0.5);
263 LLVMValueRef length_minus_one
= lp_build_sub(int_coord_bld
, length_i
,
266 /* wrap with normalized floats is just fract */
267 coord_f
= lp_build_fract(coord_bld
, coord_f
);
268 /* mul by size and subtract 0.5 */
269 coord_f
= lp_build_mul(coord_bld
, coord_f
, length_f
);
270 coord_f
= lp_build_sub(coord_bld
, coord_f
, half
);
272 * we avoided the 0.5/length division before the repeat wrap,
273 * now need to fix up edge cases with selects
275 /* convert to int, compute lerp weight */
276 lp_build_ifloor_fract(coord_bld
, coord_f
, coord0_i
, weight_f
);
277 mask
= lp_build_compare(int_coord_bld
->gallivm
, int_coord_bld
->type
,
278 PIPE_FUNC_LESS
, *coord0_i
, int_coord_bld
->zero
);
279 *coord0_i
= lp_build_select(int_coord_bld
, mask
, length_minus_one
, *coord0_i
);
284 * Build LLVM code for texture wrap mode for linear filtering.
285 * \param x0_out returns first integer texcoord
286 * \param x1_out returns second integer texcoord
287 * \param weight_out returns linear interpolation weight
290 lp_build_sample_wrap_linear(struct lp_build_sample_context
*bld
,
293 LLVMValueRef length_f
,
297 LLVMValueRef
*x0_out
,
298 LLVMValueRef
*x1_out
,
299 LLVMValueRef
*weight_out
)
301 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
302 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
303 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
304 LLVMValueRef half
= lp_build_const_vec(bld
->gallivm
, coord_bld
->type
, 0.5);
305 LLVMValueRef length_minus_one
= lp_build_sub(int_coord_bld
, length
, int_coord_bld
->one
);
306 LLVMValueRef coord0
, coord1
, weight
;
309 case PIPE_TEX_WRAP_REPEAT
:
311 /* mul by size and subtract 0.5 */
312 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
313 coord
= lp_build_sub(coord_bld
, coord
, half
);
315 offset
= lp_build_int_to_float(coord_bld
, offset
);
316 coord
= lp_build_add(coord_bld
, coord
, offset
);
318 /* convert to int, compute lerp weight */
319 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
320 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
322 coord0
= LLVMBuildAnd(builder
, coord0
, length_minus_one
, "");
323 coord1
= LLVMBuildAnd(builder
, coord1
, length_minus_one
, "");
328 offset
= lp_build_int_to_float(coord_bld
, offset
);
329 offset
= lp_build_div(coord_bld
, offset
, length_f
);
330 coord
= lp_build_add(coord_bld
, coord
, offset
);
332 lp_build_coord_repeat_npot_linear(bld
, coord
,
335 mask
= lp_build_compare(int_coord_bld
->gallivm
, int_coord_bld
->type
,
336 PIPE_FUNC_NOTEQUAL
, coord0
, length_minus_one
);
337 coord1
= LLVMBuildAnd(builder
,
338 lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
),
343 case PIPE_TEX_WRAP_CLAMP
:
344 if (bld
->static_sampler_state
->normalized_coords
) {
345 /* scale coord to length */
346 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
349 offset
= lp_build_int_to_float(coord_bld
, offset
);
350 coord
= lp_build_add(coord_bld
, coord
, offset
);
353 /* clamp to [0, length] */
354 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, length_f
);
356 coord
= lp_build_sub(coord_bld
, coord
, half
);
358 /* convert to int, compute lerp weight */
359 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
360 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
363 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
365 struct lp_build_context abs_coord_bld
= bld
->coord_bld
;
366 abs_coord_bld
.type
.sign
= FALSE
;
368 if (bld
->static_sampler_state
->normalized_coords
) {
369 /* mul by tex size */
370 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
373 offset
= lp_build_int_to_float(coord_bld
, offset
);
374 coord
= lp_build_add(coord_bld
, coord
, offset
);
377 /* clamp to length max */
378 coord
= lp_build_min(coord_bld
, coord
, length_f
);
380 coord
= lp_build_sub(coord_bld
, coord
, half
);
381 /* clamp to [0, length - 0.5] */
382 coord
= lp_build_max(coord_bld
, coord
, coord_bld
->zero
);
383 /* convert to int, compute lerp weight */
384 lp_build_ifloor_fract(&abs_coord_bld
, coord
, &coord0
, &weight
);
385 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
386 /* coord1 = min(coord1, length-1) */
387 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
391 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
392 if (bld
->static_sampler_state
->normalized_coords
) {
393 /* scale coord to length */
394 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
397 offset
= lp_build_int_to_float(coord_bld
, offset
);
398 coord
= lp_build_add(coord_bld
, coord
, offset
);
400 /* was: clamp to [-0.5, length + 0.5], then sub 0.5 */
401 /* can skip clamp (though might not work for very large coord values */
402 coord
= lp_build_sub(coord_bld
, coord
, half
);
403 /* convert to int, compute lerp weight */
404 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
405 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
408 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
410 offset
= lp_build_int_to_float(coord_bld
, offset
);
411 offset
= lp_build_div(coord_bld
, offset
, length_f
);
412 coord
= lp_build_add(coord_bld
, coord
, offset
);
414 /* compute mirror function */
415 coord
= lp_build_coord_mirror(bld
, coord
);
417 /* scale coord to length */
418 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
419 coord
= lp_build_sub(coord_bld
, coord
, half
);
421 /* convert to int, compute lerp weight */
422 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
423 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
425 /* coord0 = max(coord0, 0) */
426 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
427 /* coord1 = min(coord1, length-1) */
428 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
431 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
432 if (bld
->static_sampler_state
->normalized_coords
) {
433 /* scale coord to length */
434 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
437 offset
= lp_build_int_to_float(coord_bld
, offset
);
438 coord
= lp_build_add(coord_bld
, coord
, offset
);
440 coord
= lp_build_abs(coord_bld
, coord
);
442 /* clamp to [0, length] */
443 coord
= lp_build_min(coord_bld
, coord
, length_f
);
445 coord
= lp_build_sub(coord_bld
, coord
, half
);
447 /* convert to int, compute lerp weight */
448 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
449 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
452 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
454 struct lp_build_context abs_coord_bld
= bld
->coord_bld
;
455 abs_coord_bld
.type
.sign
= FALSE
;
457 if (bld
->static_sampler_state
->normalized_coords
) {
458 /* scale coord to length */
459 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
462 offset
= lp_build_int_to_float(coord_bld
, offset
);
463 coord
= lp_build_add(coord_bld
, coord
, offset
);
465 coord
= lp_build_abs(coord_bld
, coord
);
467 /* clamp to length max */
468 coord
= lp_build_min(coord_bld
, coord
, length_f
);
470 coord
= lp_build_sub(coord_bld
, coord
, half
);
471 /* clamp to [0, length - 0.5] */
472 coord
= lp_build_max(coord_bld
, coord
, coord_bld
->zero
);
474 /* convert to int, compute lerp weight */
475 lp_build_ifloor_fract(&abs_coord_bld
, coord
, &coord0
, &weight
);
476 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
477 /* coord1 = min(coord1, length-1) */
478 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
482 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
484 if (bld
->static_sampler_state
->normalized_coords
) {
485 /* scale coord to length */
486 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
489 offset
= lp_build_int_to_float(coord_bld
, offset
);
490 coord
= lp_build_add(coord_bld
, coord
, offset
);
492 coord
= lp_build_abs(coord_bld
, coord
);
494 /* was: clamp to [-0.5, length + 0.5] then sub 0.5 */
495 /* skip clamp - always positive, and other side
496 only potentially matters for very large coords */
497 coord
= lp_build_sub(coord_bld
, coord
, half
);
499 /* convert to int, compute lerp weight */
500 lp_build_ifloor_fract(coord_bld
, coord
, &coord0
, &weight
);
501 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
514 *weight_out
= weight
;
519 * Build LLVM code for texture wrap mode for nearest filtering.
520 * \param coord the incoming texcoord (nominally in [0,1])
521 * \param length the texture size along one dimension, as int vector
522 * \param length_f the texture size along one dimension, as float vector
523 * \param offset texel offset along one dimension (as int vector)
524 * \param is_pot if TRUE, length is a power of two
525 * \param wrap_mode one of PIPE_TEX_WRAP_x
528 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
531 LLVMValueRef length_f
,
536 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
537 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
538 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
539 LLVMValueRef length_minus_one
= lp_build_sub(int_coord_bld
, length
, int_coord_bld
->one
);
543 case PIPE_TEX_WRAP_REPEAT
:
545 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
546 icoord
= lp_build_ifloor(coord_bld
, coord
);
548 icoord
= lp_build_add(int_coord_bld
, icoord
, offset
);
550 icoord
= LLVMBuildAnd(builder
, icoord
, length_minus_one
, "");
554 offset
= lp_build_int_to_float(coord_bld
, offset
);
555 offset
= lp_build_div(coord_bld
, offset
, length_f
);
556 coord
= lp_build_add(coord_bld
, coord
, offset
);
558 /* take fraction, unnormalize */
559 coord
= lp_build_fract_safe(coord_bld
, coord
);
560 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
561 icoord
= lp_build_itrunc(coord_bld
, coord
);
565 case PIPE_TEX_WRAP_CLAMP
:
566 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
567 if (bld
->static_sampler_state
->normalized_coords
) {
568 /* scale coord to length */
569 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
573 offset
= lp_build_int_to_float(coord_bld
, offset
);
574 coord
= lp_build_add(coord_bld
, coord
, offset
);
577 /* use itrunc instead since we clamp to 0 anyway */
578 icoord
= lp_build_itrunc(coord_bld
, coord
);
580 /* clamp to [0, length - 1]. */
581 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
585 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
586 if (bld
->static_sampler_state
->normalized_coords
) {
587 /* scale coord to length */
588 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
590 /* no clamp necessary, border masking will handle this */
591 icoord
= lp_build_ifloor(coord_bld
, coord
);
593 icoord
= lp_build_add(int_coord_bld
, icoord
, offset
);
597 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
599 offset
= lp_build_int_to_float(coord_bld
, offset
);
600 offset
= lp_build_div(coord_bld
, offset
, length_f
);
601 coord
= lp_build_add(coord_bld
, coord
, offset
);
603 /* compute mirror function */
604 coord
= lp_build_coord_mirror(bld
, coord
);
606 /* scale coord to length */
607 assert(bld
->static_sampler_state
->normalized_coords
);
608 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
610 /* itrunc == ifloor here */
611 icoord
= lp_build_itrunc(coord_bld
, coord
);
613 /* clamp to [0, length - 1] */
614 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
617 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
618 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
619 if (bld
->static_sampler_state
->normalized_coords
) {
620 /* scale coord to length */
621 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
624 offset
= lp_build_int_to_float(coord_bld
, offset
);
625 coord
= lp_build_add(coord_bld
, coord
, offset
);
627 coord
= lp_build_abs(coord_bld
, coord
);
629 /* itrunc == ifloor here */
630 icoord
= lp_build_itrunc(coord_bld
, coord
);
632 /* clamp to [0, length - 1] */
633 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
636 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
637 if (bld
->static_sampler_state
->normalized_coords
) {
638 /* scale coord to length */
639 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
642 offset
= lp_build_int_to_float(coord_bld
, offset
);
643 coord
= lp_build_add(coord_bld
, coord
, offset
);
645 coord
= lp_build_abs(coord_bld
, coord
);
647 /* itrunc == ifloor here */
648 icoord
= lp_build_itrunc(coord_bld
, coord
);
661 * Do shadow test/comparison.
662 * \param p shadow ref value
663 * \param texel the texel to compare against
666 lp_build_sample_comparefunc(struct lp_build_sample_context
*bld
,
670 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
674 //lp_build_print_value(bld->gallivm, "shadow cmp coord", p);
675 lp_build_print_value(bld
->gallivm
, "shadow cmp texel", texel
);
678 /* result = (p FUNC texel) ? 1 : 0 */
680 * honor d3d10 floating point rules here, which state that comparisons
681 * are ordered except NOT_EQUAL which is unordered.
683 if (bld
->static_sampler_state
->compare_func
!= PIPE_FUNC_NOTEQUAL
) {
684 res
= lp_build_cmp_ordered(texel_bld
, bld
->static_sampler_state
->compare_func
,
688 res
= lp_build_cmp(texel_bld
, bld
->static_sampler_state
->compare_func
,
696 * Generate code to sample a mipmap level with nearest filtering.
697 * If sampling a cube texture, r = cube face in [0,5].
700 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
702 LLVMValueRef row_stride_vec
,
703 LLVMValueRef img_stride_vec
,
704 LLVMValueRef data_ptr
,
705 LLVMValueRef mipoffsets
,
706 LLVMValueRef
*coords
,
707 const LLVMValueRef
*offsets
,
708 LLVMValueRef colors_out
[4])
710 const unsigned dims
= bld
->dims
;
711 LLVMValueRef width_vec
;
712 LLVMValueRef height_vec
;
713 LLVMValueRef depth_vec
;
714 LLVMValueRef flt_size
;
715 LLVMValueRef flt_width_vec
;
716 LLVMValueRef flt_height_vec
;
717 LLVMValueRef flt_depth_vec
;
718 LLVMValueRef x
, y
= NULL
, z
= NULL
;
720 lp_build_extract_image_sizes(bld
,
724 &width_vec
, &height_vec
, &depth_vec
);
726 flt_size
= lp_build_int_to_float(&bld
->float_size_bld
, size
);
728 lp_build_extract_image_sizes(bld
,
729 &bld
->float_size_bld
,
732 &flt_width_vec
, &flt_height_vec
, &flt_depth_vec
);
735 * Compute integer texcoords.
737 x
= lp_build_sample_wrap_nearest(bld
, coords
[0], width_vec
,
738 flt_width_vec
, offsets
[0],
739 bld
->static_texture_state
->pot_width
,
740 bld
->static_sampler_state
->wrap_s
);
741 lp_build_name(x
, "tex.x.wrapped");
744 y
= lp_build_sample_wrap_nearest(bld
, coords
[1], height_vec
,
745 flt_height_vec
, offsets
[1],
746 bld
->static_texture_state
->pot_height
,
747 bld
->static_sampler_state
->wrap_t
);
748 lp_build_name(y
, "tex.y.wrapped");
751 z
= lp_build_sample_wrap_nearest(bld
, coords
[2], depth_vec
,
752 flt_depth_vec
, offsets
[2],
753 bld
->static_texture_state
->pot_depth
,
754 bld
->static_sampler_state
->wrap_r
);
755 lp_build_name(z
, "tex.z.wrapped");
758 if (has_layer_coord(bld
->static_texture_state
->target
)) {
759 if (bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
760 /* add cube layer to face */
761 z
= lp_build_add(&bld
->int_coord_bld
, coords
[2], coords
[3]);
766 lp_build_name(z
, "tex.z.layer");
770 * Get texture colors.
772 lp_build_sample_texel_soa(bld
,
773 width_vec
, height_vec
, depth_vec
,
775 row_stride_vec
, img_stride_vec
,
776 data_ptr
, mipoffsets
, colors_out
);
778 if (bld
->static_sampler_state
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
780 cmpval
= lp_build_sample_comparefunc(bld
, coords
[4], colors_out
[0]);
781 /* this is really just a AND 1.0, cmpval but llvm is clever enough */
782 colors_out
[0] = lp_build_select(&bld
->texel_bld
, cmpval
,
783 bld
->texel_bld
.one
, bld
->texel_bld
.zero
);
784 colors_out
[1] = colors_out
[2] = colors_out
[3] = colors_out
[0];
791 * Like a lerp, but inputs are 0/~0 masks, so can simplify slightly.
794 lp_build_masklerp(struct lp_build_context
*bld
,
799 struct gallivm_state
*gallivm
= bld
->gallivm
;
800 LLVMBuilderRef builder
= gallivm
->builder
;
801 LLVMValueRef weight2
;
803 weight2
= lp_build_sub(bld
, bld
->one
, weight
);
804 weight
= LLVMBuildBitCast(builder
, weight
,
805 lp_build_int_vec_type(gallivm
, bld
->type
), "");
806 weight2
= LLVMBuildBitCast(builder
, weight2
,
807 lp_build_int_vec_type(gallivm
, bld
->type
), "");
808 weight
= LLVMBuildAnd(builder
, weight
, mask1
, "");
809 weight2
= LLVMBuildAnd(builder
, weight2
, mask0
, "");
810 weight
= LLVMBuildBitCast(builder
, weight
, bld
->vec_type
, "");
811 weight2
= LLVMBuildBitCast(builder
, weight2
, bld
->vec_type
, "");
812 return lp_build_add(bld
, weight
, weight2
);
816 * Like a 2d lerp, but inputs are 0/~0 masks, so can simplify slightly.
819 lp_build_masklerp2d(struct lp_build_context
*bld
,
820 LLVMValueRef weight0
,
821 LLVMValueRef weight1
,
827 LLVMValueRef val0
= lp_build_masklerp(bld
, weight0
, mask00
, mask01
);
828 LLVMValueRef val1
= lp_build_masklerp(bld
, weight0
, mask10
, mask11
);
829 return lp_build_lerp(bld
, weight1
, val0
, val1
, 0);
833 * this is a bit excessive code for something OpenGL just recommends
834 * but does not require.
836 #define ACCURATE_CUBE_CORNERS 1
839 * Generate code to sample a mipmap level with linear filtering.
840 * If sampling a cube texture, r = cube face in [0,5].
841 * If linear_mask is present, only pixels having their mask set
842 * will receive linear filtering, the rest will use nearest.
845 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
848 LLVMValueRef linear_mask
,
849 LLVMValueRef row_stride_vec
,
850 LLVMValueRef img_stride_vec
,
851 LLVMValueRef data_ptr
,
852 LLVMValueRef mipoffsets
,
853 LLVMValueRef
*coords
,
854 const LLVMValueRef
*offsets
,
855 LLVMValueRef colors_out
[4])
857 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
858 struct lp_build_context
*ivec_bld
= &bld
->int_coord_bld
;
859 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
860 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
861 const unsigned dims
= bld
->dims
;
862 LLVMValueRef width_vec
;
863 LLVMValueRef height_vec
;
864 LLVMValueRef depth_vec
;
865 LLVMValueRef flt_size
;
866 LLVMValueRef flt_width_vec
;
867 LLVMValueRef flt_height_vec
;
868 LLVMValueRef flt_depth_vec
;
869 LLVMValueRef fall_off
[4], have_corners
;
870 LLVMValueRef z1
= NULL
;
871 LLVMValueRef z00
= NULL
, z01
= NULL
, z10
= NULL
, z11
= NULL
;
872 LLVMValueRef x00
= NULL
, x01
= NULL
, x10
= NULL
, x11
= NULL
;
873 LLVMValueRef y00
= NULL
, y01
= NULL
, y10
= NULL
, y11
= NULL
;
874 LLVMValueRef s_fpart
, t_fpart
= NULL
, r_fpart
= NULL
;
875 LLVMValueRef xs
[4], ys
[4], zs
[4];
876 LLVMValueRef neighbors
[2][2][4];
877 int chan
, texel_index
;
878 boolean seamless_cube_filter
, accurate_cube_corners
;
880 seamless_cube_filter
= (bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE
||
881 bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE_ARRAY
) &&
882 bld
->static_sampler_state
->seamless_cube_map
;
884 * XXX I don't know how this is really supposed to work with gather. From GL
885 * spec wording (not gather specific) it sounds like the 4th missing texel
886 * should be an average of the other 3, hence for gather could return this.
887 * This is however NOT how the code here works, which just fixes up the
888 * weights used for filtering instead. And of course for gather there is
889 * no filter to tweak...
891 accurate_cube_corners
= ACCURATE_CUBE_CORNERS
&& seamless_cube_filter
&&
894 lp_build_extract_image_sizes(bld
,
898 &width_vec
, &height_vec
, &depth_vec
);
900 flt_size
= lp_build_int_to_float(&bld
->float_size_bld
, size
);
902 lp_build_extract_image_sizes(bld
,
903 &bld
->float_size_bld
,
906 &flt_width_vec
, &flt_height_vec
, &flt_depth_vec
);
909 * Compute integer texcoords.
912 if (!seamless_cube_filter
) {
913 lp_build_sample_wrap_linear(bld
, coords
[0], width_vec
,
914 flt_width_vec
, offsets
[0],
915 bld
->static_texture_state
->pot_width
,
916 bld
->static_sampler_state
->wrap_s
,
917 &x00
, &x01
, &s_fpart
);
918 lp_build_name(x00
, "tex.x0.wrapped");
919 lp_build_name(x01
, "tex.x1.wrapped");
924 lp_build_sample_wrap_linear(bld
, coords
[1], height_vec
,
925 flt_height_vec
, offsets
[1],
926 bld
->static_texture_state
->pot_height
,
927 bld
->static_sampler_state
->wrap_t
,
928 &y00
, &y10
, &t_fpart
);
929 lp_build_name(y00
, "tex.y0.wrapped");
930 lp_build_name(y10
, "tex.y1.wrapped");
935 lp_build_sample_wrap_linear(bld
, coords
[2], depth_vec
,
936 flt_depth_vec
, offsets
[2],
937 bld
->static_texture_state
->pot_depth
,
938 bld
->static_sampler_state
->wrap_r
,
939 &z00
, &z1
, &r_fpart
);
940 z01
= z10
= z11
= z00
;
941 lp_build_name(z00
, "tex.z0.wrapped");
942 lp_build_name(z1
, "tex.z1.wrapped");
945 if (has_layer_coord(bld
->static_texture_state
->target
)) {
946 if (bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
947 /* add cube layer to face */
948 z00
= z01
= z10
= z11
= z1
=
949 lp_build_add(&bld
->int_coord_bld
, coords
[2], coords
[3]);
952 z00
= z01
= z10
= z11
= z1
= coords
[2]; /* cube face or layer */
954 lp_build_name(z00
, "tex.z0.layer");
955 lp_build_name(z1
, "tex.z1.layer");
959 struct lp_build_if_state edge_if
;
961 LLVMValueRef new_faces
[4], new_xcoords
[4][2], new_ycoords
[4][2];
962 LLVMValueRef coord
, have_edge
, have_corner
;
963 LLVMValueRef fall_off_ym_notxm
, fall_off_ym_notxp
, fall_off_x
, fall_off_y
;
964 LLVMValueRef fall_off_yp_notxm
, fall_off_yp_notxp
;
965 LLVMValueRef x0
, x1
, y0
, y1
, y0_clamped
, y1_clamped
;
966 LLVMValueRef face
= coords
[2];
967 LLVMValueRef half
= lp_build_const_vec(bld
->gallivm
, coord_bld
->type
, 0.5f
);
968 LLVMValueRef length_minus_one
= lp_build_sub(ivec_bld
, width_vec
, ivec_bld
->one
);
969 /* XXX drop height calcs. Could (should) do this without seamless filtering too */
970 height_vec
= width_vec
;
971 flt_height_vec
= flt_width_vec
;
973 /* XXX the overflow logic is actually sort of duplicated with trilinear,
974 * since an overflow in one mip should also have a corresponding overflow
977 /* should always have normalized coords, and offsets are undefined */
978 assert(bld
->static_sampler_state
->normalized_coords
);
979 coord
= lp_build_mul(coord_bld
, coords
[0], flt_width_vec
);
980 /* instead of clamp, build mask if overflowed */
981 coord
= lp_build_sub(coord_bld
, coord
, half
);
982 /* convert to int, compute lerp weight */
983 /* not ideal with AVX (and no AVX2) */
984 lp_build_ifloor_fract(coord_bld
, coord
, &x0
, &s_fpart
);
985 x1
= lp_build_add(ivec_bld
, x0
, ivec_bld
->one
);
986 coord
= lp_build_mul(coord_bld
, coords
[1], flt_height_vec
);
987 coord
= lp_build_sub(coord_bld
, coord
, half
);
988 lp_build_ifloor_fract(coord_bld
, coord
, &y0
, &t_fpart
);
989 y1
= lp_build_add(ivec_bld
, y0
, ivec_bld
->one
);
991 fall_off
[0] = lp_build_cmp(ivec_bld
, PIPE_FUNC_LESS
, x0
, ivec_bld
->zero
);
992 fall_off
[1] = lp_build_cmp(ivec_bld
, PIPE_FUNC_GREATER
, x1
, length_minus_one
);
993 fall_off
[2] = lp_build_cmp(ivec_bld
, PIPE_FUNC_LESS
, y0
, ivec_bld
->zero
);
994 fall_off
[3] = lp_build_cmp(ivec_bld
, PIPE_FUNC_GREATER
, y1
, length_minus_one
);
996 fall_off_x
= lp_build_or(ivec_bld
, fall_off
[0], fall_off
[1]);
997 fall_off_y
= lp_build_or(ivec_bld
, fall_off
[2], fall_off
[3]);
998 have_edge
= lp_build_or(ivec_bld
, fall_off_x
, fall_off_y
);
999 have_edge
= lp_build_any_true_range(ivec_bld
, ivec_bld
->type
.length
, have_edge
);
1001 /* needed for accurate corner filtering branch later, rely on 0 init */
1002 int1t
= LLVMInt1TypeInContext(bld
->gallivm
->context
);
1003 have_corners
= lp_build_alloca(bld
->gallivm
, int1t
, "have_corner");
1005 for (texel_index
= 0; texel_index
< 4; texel_index
++) {
1006 xs
[texel_index
] = lp_build_alloca(bld
->gallivm
, ivec_bld
->vec_type
, "xs");
1007 ys
[texel_index
] = lp_build_alloca(bld
->gallivm
, ivec_bld
->vec_type
, "ys");
1008 zs
[texel_index
] = lp_build_alloca(bld
->gallivm
, ivec_bld
->vec_type
, "zs");
1011 lp_build_if(&edge_if
, bld
->gallivm
, have_edge
);
1013 have_corner
= lp_build_and(ivec_bld
, fall_off_x
, fall_off_y
);
1014 have_corner
= lp_build_any_true_range(ivec_bld
, ivec_bld
->type
.length
, have_corner
);
1015 LLVMBuildStore(builder
, have_corner
, have_corners
);
1018 * Need to feed clamped values here for cheap corner handling,
1019 * but only for y coord (as when falling off both edges we only
1020 * fall off the x one) - this should be sufficient.
1022 y0_clamped
= lp_build_max(ivec_bld
, y0
, ivec_bld
->zero
);
1023 y1_clamped
= lp_build_min(ivec_bld
, y1
, length_minus_one
);
1026 * Get all possible new coords.
1028 lp_build_cube_new_coords(ivec_bld
, face
,
1029 x0
, x1
, y0_clamped
, y1_clamped
,
1031 new_faces
, new_xcoords
, new_ycoords
);
1033 /* handle fall off x-, x+ direction */
1034 /* determine new coords, face (not both fall_off vars can be true at same time) */
1035 x00
= lp_build_select(ivec_bld
, fall_off
[0], new_xcoords
[0][0], x0
);
1036 y00
= lp_build_select(ivec_bld
, fall_off
[0], new_ycoords
[0][0], y0_clamped
);
1037 x10
= lp_build_select(ivec_bld
, fall_off
[0], new_xcoords
[0][1], x0
);
1038 y10
= lp_build_select(ivec_bld
, fall_off
[0], new_ycoords
[0][1], y1_clamped
);
1039 x01
= lp_build_select(ivec_bld
, fall_off
[1], new_xcoords
[1][0], x1
);
1040 y01
= lp_build_select(ivec_bld
, fall_off
[1], new_ycoords
[1][0], y0_clamped
);
1041 x11
= lp_build_select(ivec_bld
, fall_off
[1], new_xcoords
[1][1], x1
);
1042 y11
= lp_build_select(ivec_bld
, fall_off
[1], new_ycoords
[1][1], y1_clamped
);
1044 z00
= z10
= lp_build_select(ivec_bld
, fall_off
[0], new_faces
[0], face
);
1045 z01
= z11
= lp_build_select(ivec_bld
, fall_off
[1], new_faces
[1], face
);
1047 /* handle fall off y-, y+ direction */
1049 * Cheap corner logic: just hack up things so a texel doesn't fall
1050 * off both sides (which means filter weights will be wrong but we'll only
1051 * use valid texels in the filter).
1052 * This means however (y) coords must additionally be clamped (see above).
1053 * This corner handling should be fully OpenGL (but not d3d10) compliant.
1055 fall_off_ym_notxm
= lp_build_andnot(ivec_bld
, fall_off
[2], fall_off
[0]);
1056 fall_off_ym_notxp
= lp_build_andnot(ivec_bld
, fall_off
[2], fall_off
[1]);
1057 fall_off_yp_notxm
= lp_build_andnot(ivec_bld
, fall_off
[3], fall_off
[0]);
1058 fall_off_yp_notxp
= lp_build_andnot(ivec_bld
, fall_off
[3], fall_off
[1]);
1060 x00
= lp_build_select(ivec_bld
, fall_off_ym_notxm
, new_xcoords
[2][0], x00
);
1061 y00
= lp_build_select(ivec_bld
, fall_off_ym_notxm
, new_ycoords
[2][0], y00
);
1062 x01
= lp_build_select(ivec_bld
, fall_off_ym_notxp
, new_xcoords
[2][1], x01
);
1063 y01
= lp_build_select(ivec_bld
, fall_off_ym_notxp
, new_ycoords
[2][1], y01
);
1064 x10
= lp_build_select(ivec_bld
, fall_off_yp_notxm
, new_xcoords
[3][0], x10
);
1065 y10
= lp_build_select(ivec_bld
, fall_off_yp_notxm
, new_ycoords
[3][0], y10
);
1066 x11
= lp_build_select(ivec_bld
, fall_off_yp_notxp
, new_xcoords
[3][1], x11
);
1067 y11
= lp_build_select(ivec_bld
, fall_off_yp_notxp
, new_ycoords
[3][1], y11
);
1069 z00
= lp_build_select(ivec_bld
, fall_off_ym_notxm
, new_faces
[2], z00
);
1070 z01
= lp_build_select(ivec_bld
, fall_off_ym_notxp
, new_faces
[2], z01
);
1071 z10
= lp_build_select(ivec_bld
, fall_off_yp_notxm
, new_faces
[3], z10
);
1072 z11
= lp_build_select(ivec_bld
, fall_off_yp_notxp
, new_faces
[3], z11
);
1074 if (bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
1075 /* now can add cube layer to face (per sample) */
1076 z00
= lp_build_add(ivec_bld
, z00
, coords
[3]);
1077 z01
= lp_build_add(ivec_bld
, z01
, coords
[3]);
1078 z10
= lp_build_add(ivec_bld
, z10
, coords
[3]);
1079 z11
= lp_build_add(ivec_bld
, z11
, coords
[3]);
1082 LLVMBuildStore(builder
, x00
, xs
[0]);
1083 LLVMBuildStore(builder
, x01
, xs
[1]);
1084 LLVMBuildStore(builder
, x10
, xs
[2]);
1085 LLVMBuildStore(builder
, x11
, xs
[3]);
1086 LLVMBuildStore(builder
, y00
, ys
[0]);
1087 LLVMBuildStore(builder
, y01
, ys
[1]);
1088 LLVMBuildStore(builder
, y10
, ys
[2]);
1089 LLVMBuildStore(builder
, y11
, ys
[3]);
1090 LLVMBuildStore(builder
, z00
, zs
[0]);
1091 LLVMBuildStore(builder
, z01
, zs
[1]);
1092 LLVMBuildStore(builder
, z10
, zs
[2]);
1093 LLVMBuildStore(builder
, z11
, zs
[3]);
1095 lp_build_else(&edge_if
);
1097 LLVMBuildStore(builder
, x0
, xs
[0]);
1098 LLVMBuildStore(builder
, x1
, xs
[1]);
1099 LLVMBuildStore(builder
, x0
, xs
[2]);
1100 LLVMBuildStore(builder
, x1
, xs
[3]);
1101 LLVMBuildStore(builder
, y0
, ys
[0]);
1102 LLVMBuildStore(builder
, y0
, ys
[1]);
1103 LLVMBuildStore(builder
, y1
, ys
[2]);
1104 LLVMBuildStore(builder
, y1
, ys
[3]);
1105 if (bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
1106 LLVMValueRef cube_layer
= lp_build_add(ivec_bld
, face
, coords
[3]);
1107 LLVMBuildStore(builder
, cube_layer
, zs
[0]);
1108 LLVMBuildStore(builder
, cube_layer
, zs
[1]);
1109 LLVMBuildStore(builder
, cube_layer
, zs
[2]);
1110 LLVMBuildStore(builder
, cube_layer
, zs
[3]);
1113 LLVMBuildStore(builder
, face
, zs
[0]);
1114 LLVMBuildStore(builder
, face
, zs
[1]);
1115 LLVMBuildStore(builder
, face
, zs
[2]);
1116 LLVMBuildStore(builder
, face
, zs
[3]);
1119 lp_build_endif(&edge_if
);
1121 x00
= LLVMBuildLoad(builder
, xs
[0], "");
1122 x01
= LLVMBuildLoad(builder
, xs
[1], "");
1123 x10
= LLVMBuildLoad(builder
, xs
[2], "");
1124 x11
= LLVMBuildLoad(builder
, xs
[3], "");
1125 y00
= LLVMBuildLoad(builder
, ys
[0], "");
1126 y01
= LLVMBuildLoad(builder
, ys
[1], "");
1127 y10
= LLVMBuildLoad(builder
, ys
[2], "");
1128 y11
= LLVMBuildLoad(builder
, ys
[3], "");
1129 z00
= LLVMBuildLoad(builder
, zs
[0], "");
1130 z01
= LLVMBuildLoad(builder
, zs
[1], "");
1131 z10
= LLVMBuildLoad(builder
, zs
[2], "");
1132 z11
= LLVMBuildLoad(builder
, zs
[3], "");
1137 * Whack filter weights into place. Whatever texel had more weight is
1138 * the one which should have been selected by nearest filtering hence
1139 * just use 100% weight for it.
1141 struct lp_build_context
*c_bld
= &bld
->coord_bld
;
1142 LLVMValueRef w1_mask
, w1_weight
;
1143 LLVMValueRef half
= lp_build_const_vec(bld
->gallivm
, c_bld
->type
, 0.5f
);
1145 w1_mask
= lp_build_cmp(c_bld
, PIPE_FUNC_GREATER
, s_fpart
, half
);
1146 /* this select is really just a "and" */
1147 w1_weight
= lp_build_select(c_bld
, w1_mask
, c_bld
->one
, c_bld
->zero
);
1148 s_fpart
= lp_build_select(c_bld
, linear_mask
, s_fpart
, w1_weight
);
1150 w1_mask
= lp_build_cmp(c_bld
, PIPE_FUNC_GREATER
, t_fpart
, half
);
1151 w1_weight
= lp_build_select(c_bld
, w1_mask
, c_bld
->one
, c_bld
->zero
);
1152 t_fpart
= lp_build_select(c_bld
, linear_mask
, t_fpart
, w1_weight
);
1154 w1_mask
= lp_build_cmp(c_bld
, PIPE_FUNC_GREATER
, r_fpart
, half
);
1155 w1_weight
= lp_build_select(c_bld
, w1_mask
, c_bld
->one
, c_bld
->zero
);
1156 r_fpart
= lp_build_select(c_bld
, linear_mask
, r_fpart
, w1_weight
);
1162 * Get texture colors.
1164 /* get x0/x1 texels */
1165 lp_build_sample_texel_soa(bld
,
1166 width_vec
, height_vec
, depth_vec
,
1168 row_stride_vec
, img_stride_vec
,
1169 data_ptr
, mipoffsets
, neighbors
[0][0]);
1170 lp_build_sample_texel_soa(bld
,
1171 width_vec
, height_vec
, depth_vec
,
1173 row_stride_vec
, img_stride_vec
,
1174 data_ptr
, mipoffsets
, neighbors
[0][1]);
1178 if (bld
->static_sampler_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
) {
1179 /* Interpolate two samples from 1D image to produce one color */
1180 for (chan
= 0; chan
< 4; chan
++) {
1181 colors_out
[chan
] = lp_build_lerp(texel_bld
, s_fpart
,
1182 neighbors
[0][0][chan
],
1183 neighbors
[0][1][chan
],
1188 LLVMValueRef cmpval0
, cmpval1
;
1189 cmpval0
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[0][0][0]);
1190 cmpval1
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[0][1][0]);
1191 /* simplified lerp, AND mask with weight and add */
1192 colors_out
[0] = lp_build_masklerp(texel_bld
, s_fpart
,
1194 colors_out
[1] = colors_out
[2] = colors_out
[3] = colors_out
[0];
1199 struct lp_build_if_state corner_if
;
1200 LLVMValueRef colors0
[4], colorss
[4];
1202 /* get x0/x1 texels at y1 */
1203 lp_build_sample_texel_soa(bld
,
1204 width_vec
, height_vec
, depth_vec
,
1206 row_stride_vec
, img_stride_vec
,
1207 data_ptr
, mipoffsets
, neighbors
[1][0]);
1208 lp_build_sample_texel_soa(bld
,
1209 width_vec
, height_vec
, depth_vec
,
1211 row_stride_vec
, img_stride_vec
,
1212 data_ptr
, mipoffsets
, neighbors
[1][1]);
1215 * To avoid having to duplicate linear_mask / fetch code use
1216 * another branch (with corner condition though edge would work
1219 if (accurate_cube_corners
) {
1220 LLVMValueRef w00
, w01
, w10
, w11
, wx0
, wy0
;
1221 LLVMValueRef c_weight
, c00
, c01
, c10
, c11
;
1222 LLVMValueRef have_corner
, one_third
, tmp
;
1224 colorss
[0] = lp_build_alloca(bld
->gallivm
, coord_bld
->vec_type
, "cs");
1225 colorss
[1] = lp_build_alloca(bld
->gallivm
, coord_bld
->vec_type
, "cs");
1226 colorss
[2] = lp_build_alloca(bld
->gallivm
, coord_bld
->vec_type
, "cs");
1227 colorss
[3] = lp_build_alloca(bld
->gallivm
, coord_bld
->vec_type
, "cs");
1229 have_corner
= LLVMBuildLoad(builder
, have_corners
, "");
1231 lp_build_if(&corner_if
, bld
->gallivm
, have_corner
);
1234 * we can't use standard 2d lerp as we need per-element weight
1235 * in case of corners, so just calculate bilinear result as
1236 * w00*s00 + w01*s01 + w10*s10 + w11*s11.
1237 * (This is actually less work than using 2d lerp, 7 vs. 9 instructions,
1238 * however calculating the weights needs another 6, so actually probably
1239 * not slower than 2d lerp only for 4 channels as weights only need
1240 * to be calculated once - of course fixing the weights has additional cost.)
1242 wx0
= lp_build_sub(coord_bld
, coord_bld
->one
, s_fpart
);
1243 wy0
= lp_build_sub(coord_bld
, coord_bld
->one
, t_fpart
);
1244 w00
= lp_build_mul(coord_bld
, wx0
, wy0
);
1245 w01
= lp_build_mul(coord_bld
, s_fpart
, wy0
);
1246 w10
= lp_build_mul(coord_bld
, wx0
, t_fpart
);
1247 w11
= lp_build_mul(coord_bld
, s_fpart
, t_fpart
);
1249 /* find corner weight */
1250 c00
= lp_build_and(ivec_bld
, fall_off
[0], fall_off
[2]);
1251 c_weight
= lp_build_select(coord_bld
, c00
, w00
, coord_bld
->zero
);
1252 c01
= lp_build_and(ivec_bld
, fall_off
[1], fall_off
[2]);
1253 c_weight
= lp_build_select(coord_bld
, c01
, w01
, c_weight
);
1254 c10
= lp_build_and(ivec_bld
, fall_off
[0], fall_off
[3]);
1255 c_weight
= lp_build_select(coord_bld
, c10
, w10
, c_weight
);
1256 c11
= lp_build_and(ivec_bld
, fall_off
[1], fall_off
[3]);
1257 c_weight
= lp_build_select(coord_bld
, c11
, w11
, c_weight
);
1260 * add 1/3 of the corner weight to each of the 3 other samples
1261 * and null out corner weight
1263 one_third
= lp_build_const_vec(bld
->gallivm
, coord_bld
->type
, 1.0f
/3.0f
);
1264 c_weight
= lp_build_mul(coord_bld
, c_weight
, one_third
);
1265 w00
= lp_build_add(coord_bld
, w00
, c_weight
);
1266 c00
= LLVMBuildBitCast(builder
, c00
, coord_bld
->vec_type
, "");
1267 w00
= lp_build_andnot(coord_bld
, w00
, c00
);
1268 w01
= lp_build_add(coord_bld
, w01
, c_weight
);
1269 c01
= LLVMBuildBitCast(builder
, c01
, coord_bld
->vec_type
, "");
1270 w01
= lp_build_andnot(coord_bld
, w01
, c01
);
1271 w10
= lp_build_add(coord_bld
, w10
, c_weight
);
1272 c10
= LLVMBuildBitCast(builder
, c10
, coord_bld
->vec_type
, "");
1273 w10
= lp_build_andnot(coord_bld
, w10
, c10
);
1274 w11
= lp_build_add(coord_bld
, w11
, c_weight
);
1275 c11
= LLVMBuildBitCast(builder
, c11
, coord_bld
->vec_type
, "");
1276 w11
= lp_build_andnot(coord_bld
, w11
, c11
);
1278 if (bld
->static_sampler_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
) {
1279 for (chan
= 0; chan
< 4; chan
++) {
1280 colors0
[chan
] = lp_build_mul(coord_bld
, w00
, neighbors
[0][0][chan
]);
1281 tmp
= lp_build_mul(coord_bld
, w01
, neighbors
[0][1][chan
]);
1282 colors0
[chan
] = lp_build_add(coord_bld
, tmp
, colors0
[chan
]);
1283 tmp
= lp_build_mul(coord_bld
, w10
, neighbors
[1][0][chan
]);
1284 colors0
[chan
] = lp_build_add(coord_bld
, tmp
, colors0
[chan
]);
1285 tmp
= lp_build_mul(coord_bld
, w11
, neighbors
[1][1][chan
]);
1286 colors0
[chan
] = lp_build_add(coord_bld
, tmp
, colors0
[chan
]);
1290 LLVMValueRef cmpval00
, cmpval01
, cmpval10
, cmpval11
;
1291 cmpval00
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[0][0][0]);
1292 cmpval01
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[0][1][0]);
1293 cmpval10
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[1][0][0]);
1294 cmpval11
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[1][1][0]);
1295 /* inputs to interpolation are just masks so just add masked weights together */
1296 cmpval00
= LLVMBuildBitCast(builder
, cmpval00
, coord_bld
->vec_type
, "");
1297 cmpval01
= LLVMBuildBitCast(builder
, cmpval01
, coord_bld
->vec_type
, "");
1298 cmpval10
= LLVMBuildBitCast(builder
, cmpval10
, coord_bld
->vec_type
, "");
1299 cmpval11
= LLVMBuildBitCast(builder
, cmpval11
, coord_bld
->vec_type
, "");
1300 colors0
[0] = lp_build_and(coord_bld
, w00
, cmpval00
);
1301 tmp
= lp_build_and(coord_bld
, w01
, cmpval01
);
1302 colors0
[0] = lp_build_add(coord_bld
, tmp
, colors0
[0]);
1303 tmp
= lp_build_and(coord_bld
, w10
, cmpval10
);
1304 colors0
[0] = lp_build_add(coord_bld
, tmp
, colors0
[0]);
1305 tmp
= lp_build_and(coord_bld
, w11
, cmpval11
);
1306 colors0
[0] = lp_build_add(coord_bld
, tmp
, colors0
[0]);
1307 colors0
[1] = colors0
[2] = colors0
[3] = colors0
[0];
1310 LLVMBuildStore(builder
, colors0
[0], colorss
[0]);
1311 LLVMBuildStore(builder
, colors0
[1], colorss
[1]);
1312 LLVMBuildStore(builder
, colors0
[2], colorss
[2]);
1313 LLVMBuildStore(builder
, colors0
[3], colorss
[3]);
1315 lp_build_else(&corner_if
);
1318 if (bld
->static_sampler_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
) {
1321 * Just assign the red channel (no component selection yet).
1322 * This is a bit hackish, we usually do the swizzle at the
1323 * end of sampling (much less values to swizzle), but this
1324 * obviously cannot work when using gather.
1326 unsigned chan_swiz
= bld
->static_texture_state
->swizzle_r
;
1327 colors0
[0] = lp_build_swizzle_soa_channel(texel_bld
,
1330 colors0
[1] = lp_build_swizzle_soa_channel(texel_bld
,
1333 colors0
[2] = lp_build_swizzle_soa_channel(texel_bld
,
1336 colors0
[3] = lp_build_swizzle_soa_channel(texel_bld
,
1341 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
1342 for (chan
= 0; chan
< 4; chan
++) {
1343 colors0
[chan
] = lp_build_lerp_2d(texel_bld
,
1345 neighbors
[0][0][chan
],
1346 neighbors
[0][1][chan
],
1347 neighbors
[1][0][chan
],
1348 neighbors
[1][1][chan
],
1354 LLVMValueRef cmpval00
, cmpval01
, cmpval10
, cmpval11
;
1355 cmpval00
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[0][0][0]);
1356 cmpval01
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[0][1][0]);
1357 cmpval10
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[1][0][0]);
1358 cmpval11
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[1][1][0]);
1361 /* more hacks for swizzling, should be X, ONE or ZERO... */
1362 unsigned chan_swiz
= bld
->static_texture_state
->swizzle_r
;
1363 if (chan_swiz
<= PIPE_SWIZZLE_ALPHA
) {
1364 colors0
[0] = lp_build_select(texel_bld
, cmpval10
,
1365 texel_bld
->one
, texel_bld
->zero
);
1366 colors0
[1] = lp_build_select(texel_bld
, cmpval11
,
1367 texel_bld
->one
, texel_bld
->zero
);
1368 colors0
[2] = lp_build_select(texel_bld
, cmpval01
,
1369 texel_bld
->one
, texel_bld
->zero
);
1370 colors0
[3] = lp_build_select(texel_bld
, cmpval00
,
1371 texel_bld
->one
, texel_bld
->zero
);
1373 else if (chan_swiz
== PIPE_SWIZZLE_ZERO
) {
1374 colors0
[0] = colors0
[1] = colors0
[2] = colors0
[3] =
1378 colors0
[0] = colors0
[1] = colors0
[2] = colors0
[3] =
1383 colors0
[0] = lp_build_masklerp2d(texel_bld
, s_fpart
, t_fpart
,
1384 cmpval00
, cmpval01
, cmpval10
, cmpval11
);
1385 colors0
[1] = colors0
[2] = colors0
[3] = colors0
[0];
1389 if (accurate_cube_corners
) {
1390 LLVMBuildStore(builder
, colors0
[0], colorss
[0]);
1391 LLVMBuildStore(builder
, colors0
[1], colorss
[1]);
1392 LLVMBuildStore(builder
, colors0
[2], colorss
[2]);
1393 LLVMBuildStore(builder
, colors0
[3], colorss
[3]);
1395 lp_build_endif(&corner_if
);
1397 colors0
[0] = LLVMBuildLoad(builder
, colorss
[0], "");
1398 colors0
[1] = LLVMBuildLoad(builder
, colorss
[1], "");
1399 colors0
[2] = LLVMBuildLoad(builder
, colorss
[2], "");
1400 colors0
[3] = LLVMBuildLoad(builder
, colorss
[3], "");
1404 LLVMValueRef neighbors1
[2][2][4];
1405 LLVMValueRef colors1
[4];
1409 /* get x0/x1/y0/y1 texels at z1 */
1410 lp_build_sample_texel_soa(bld
,
1411 width_vec
, height_vec
, depth_vec
,
1413 row_stride_vec
, img_stride_vec
,
1414 data_ptr
, mipoffsets
, neighbors1
[0][0]);
1415 lp_build_sample_texel_soa(bld
,
1416 width_vec
, height_vec
, depth_vec
,
1418 row_stride_vec
, img_stride_vec
,
1419 data_ptr
, mipoffsets
, neighbors1
[0][1]);
1420 lp_build_sample_texel_soa(bld
,
1421 width_vec
, height_vec
, depth_vec
,
1423 row_stride_vec
, img_stride_vec
,
1424 data_ptr
, mipoffsets
, neighbors1
[1][0]);
1425 lp_build_sample_texel_soa(bld
,
1426 width_vec
, height_vec
, depth_vec
,
1428 row_stride_vec
, img_stride_vec
,
1429 data_ptr
, mipoffsets
, neighbors1
[1][1]);
1431 if (bld
->static_sampler_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
) {
1432 /* Bilinear interpolate the four samples from the second Z slice */
1433 for (chan
= 0; chan
< 4; chan
++) {
1434 colors1
[chan
] = lp_build_lerp_2d(texel_bld
,
1436 neighbors1
[0][0][chan
],
1437 neighbors1
[0][1][chan
],
1438 neighbors1
[1][0][chan
],
1439 neighbors1
[1][1][chan
],
1442 /* Linearly interpolate the two samples from the two 3D slices */
1443 for (chan
= 0; chan
< 4; chan
++) {
1444 colors_out
[chan
] = lp_build_lerp(texel_bld
,
1446 colors0
[chan
], colors1
[chan
],
1451 LLVMValueRef cmpval00
, cmpval01
, cmpval10
, cmpval11
;
1452 cmpval00
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[0][0][0]);
1453 cmpval01
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[0][1][0]);
1454 cmpval10
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[1][0][0]);
1455 cmpval11
= lp_build_sample_comparefunc(bld
, coords
[4], neighbors
[1][1][0]);
1456 colors1
[0] = lp_build_masklerp2d(texel_bld
, s_fpart
, t_fpart
,
1457 cmpval00
, cmpval01
, cmpval10
, cmpval11
);
1458 /* Linearly interpolate the two samples from the two 3D slices */
1459 colors_out
[0] = lp_build_lerp(texel_bld
,
1461 colors0
[0], colors1
[0],
1463 colors_out
[1] = colors_out
[2] = colors_out
[3] = colors_out
[0];
1468 for (chan
= 0; chan
< 4; chan
++) {
1469 colors_out
[chan
] = colors0
[chan
];
1477 * Sample the texture/mipmap using given image filter and mip filter.
1478 * ilevel0 and ilevel1 indicate the two mipmap levels to sample
1479 * from (vectors or scalars).
1480 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1483 lp_build_sample_mipmap(struct lp_build_sample_context
*bld
,
1484 unsigned img_filter
,
1485 unsigned mip_filter
,
1487 LLVMValueRef
*coords
,
1488 const LLVMValueRef
*offsets
,
1489 LLVMValueRef ilevel0
,
1490 LLVMValueRef ilevel1
,
1491 LLVMValueRef lod_fpart
,
1492 LLVMValueRef
*colors_out
)
1494 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1495 LLVMValueRef size0
= NULL
;
1496 LLVMValueRef size1
= NULL
;
1497 LLVMValueRef row_stride0_vec
= NULL
;
1498 LLVMValueRef row_stride1_vec
= NULL
;
1499 LLVMValueRef img_stride0_vec
= NULL
;
1500 LLVMValueRef img_stride1_vec
= NULL
;
1501 LLVMValueRef data_ptr0
= NULL
;
1502 LLVMValueRef data_ptr1
= NULL
;
1503 LLVMValueRef mipoff0
= NULL
;
1504 LLVMValueRef mipoff1
= NULL
;
1505 LLVMValueRef colors0
[4], colors1
[4];
1508 /* sample the first mipmap level */
1509 lp_build_mipmap_level_sizes(bld
, ilevel0
,
1511 &row_stride0_vec
, &img_stride0_vec
);
1512 if (bld
->num_mips
== 1) {
1513 data_ptr0
= lp_build_get_mipmap_level(bld
, ilevel0
);
1516 /* This path should work for num_lods 1 too but slightly less efficient */
1517 data_ptr0
= bld
->base_ptr
;
1518 mipoff0
= lp_build_get_mip_offsets(bld
, ilevel0
);
1520 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
1521 lp_build_sample_image_nearest(bld
, size0
,
1522 row_stride0_vec
, img_stride0_vec
,
1523 data_ptr0
, mipoff0
, coords
, offsets
,
1527 assert(img_filter
== PIPE_TEX_FILTER_LINEAR
);
1528 lp_build_sample_image_linear(bld
, is_gather
, size0
, NULL
,
1529 row_stride0_vec
, img_stride0_vec
,
1530 data_ptr0
, mipoff0
, coords
, offsets
,
1534 /* Store the first level's colors in the output variables */
1535 for (chan
= 0; chan
< 4; chan
++) {
1536 LLVMBuildStore(builder
, colors0
[chan
], colors_out
[chan
]);
1539 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1540 struct lp_build_if_state if_ctx
;
1541 LLVMValueRef need_lerp
;
1543 /* need_lerp = lod_fpart > 0 */
1544 if (bld
->num_lods
== 1) {
1545 need_lerp
= LLVMBuildFCmp(builder
, LLVMRealUGT
,
1546 lod_fpart
, bld
->lodf_bld
.zero
,
1551 * We'll do mip filtering if any of the quads (or individual
1552 * pixel in case of per-pixel lod) need it.
1553 * It might be better to split the vectors here and only fetch/filter
1554 * quads which need it (if there's one lod per quad).
1556 need_lerp
= lp_build_compare(bld
->gallivm
, bld
->lodf_bld
.type
,
1558 lod_fpart
, bld
->lodf_bld
.zero
);
1559 need_lerp
= lp_build_any_true_range(&bld
->lodi_bld
, bld
->num_lods
, need_lerp
);
1562 lp_build_if(&if_ctx
, bld
->gallivm
, need_lerp
);
1565 * We unfortunately need to clamp lod_fpart here since we can get
1566 * negative values which would screw up filtering if not all
1567 * lod_fpart values have same sign.
1569 lod_fpart
= lp_build_max(&bld
->lodf_bld
, lod_fpart
,
1570 bld
->lodf_bld
.zero
);
1571 /* sample the second mipmap level */
1572 lp_build_mipmap_level_sizes(bld
, ilevel1
,
1574 &row_stride1_vec
, &img_stride1_vec
);
1575 if (bld
->num_mips
== 1) {
1576 data_ptr1
= lp_build_get_mipmap_level(bld
, ilevel1
);
1579 data_ptr1
= bld
->base_ptr
;
1580 mipoff1
= lp_build_get_mip_offsets(bld
, ilevel1
);
1582 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
1583 lp_build_sample_image_nearest(bld
, size1
,
1584 row_stride1_vec
, img_stride1_vec
,
1585 data_ptr1
, mipoff1
, coords
, offsets
,
1589 lp_build_sample_image_linear(bld
, FALSE
, size1
, NULL
,
1590 row_stride1_vec
, img_stride1_vec
,
1591 data_ptr1
, mipoff1
, coords
, offsets
,
1595 /* interpolate samples from the two mipmap levels */
1597 if (bld
->num_lods
!= bld
->coord_type
.length
)
1598 lod_fpart
= lp_build_unpack_broadcast_aos_scalars(bld
->gallivm
,
1600 bld
->texel_bld
.type
,
1603 for (chan
= 0; chan
< 4; chan
++) {
1604 colors0
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
1605 colors0
[chan
], colors1
[chan
],
1607 LLVMBuildStore(builder
, colors0
[chan
], colors_out
[chan
]);
1610 lp_build_endif(&if_ctx
);
1616 * Sample the texture/mipmap using given mip filter, and using
1617 * both nearest and linear filtering at the same time depending
1619 * lod can be per quad but linear_mask is always per pixel.
1620 * ilevel0 and ilevel1 indicate the two mipmap levels to sample
1621 * from (vectors or scalars).
1622 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1625 lp_build_sample_mipmap_both(struct lp_build_sample_context
*bld
,
1626 LLVMValueRef linear_mask
,
1627 unsigned mip_filter
,
1628 LLVMValueRef
*coords
,
1629 const LLVMValueRef
*offsets
,
1630 LLVMValueRef ilevel0
,
1631 LLVMValueRef ilevel1
,
1632 LLVMValueRef lod_fpart
,
1633 LLVMValueRef lod_positive
,
1634 LLVMValueRef
*colors_out
)
1636 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1637 LLVMValueRef size0
= NULL
;
1638 LLVMValueRef size1
= NULL
;
1639 LLVMValueRef row_stride0_vec
= NULL
;
1640 LLVMValueRef row_stride1_vec
= NULL
;
1641 LLVMValueRef img_stride0_vec
= NULL
;
1642 LLVMValueRef img_stride1_vec
= NULL
;
1643 LLVMValueRef data_ptr0
= NULL
;
1644 LLVMValueRef data_ptr1
= NULL
;
1645 LLVMValueRef mipoff0
= NULL
;
1646 LLVMValueRef mipoff1
= NULL
;
1647 LLVMValueRef colors0
[4], colors1
[4];
1650 /* sample the first mipmap level */
1651 lp_build_mipmap_level_sizes(bld
, ilevel0
,
1653 &row_stride0_vec
, &img_stride0_vec
);
1654 if (bld
->num_mips
== 1) {
1655 data_ptr0
= lp_build_get_mipmap_level(bld
, ilevel0
);
1658 /* This path should work for num_lods 1 too but slightly less efficient */
1659 data_ptr0
= bld
->base_ptr
;
1660 mipoff0
= lp_build_get_mip_offsets(bld
, ilevel0
);
1663 lp_build_sample_image_linear(bld
, FALSE
, size0
, linear_mask
,
1664 row_stride0_vec
, img_stride0_vec
,
1665 data_ptr0
, mipoff0
, coords
, offsets
,
1668 /* Store the first level's colors in the output variables */
1669 for (chan
= 0; chan
< 4; chan
++) {
1670 LLVMBuildStore(builder
, colors0
[chan
], colors_out
[chan
]);
1673 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1674 struct lp_build_if_state if_ctx
;
1675 LLVMValueRef need_lerp
;
1678 * We'll do mip filtering if any of the quads (or individual
1679 * pixel in case of per-pixel lod) need it.
1680 * Note using lod_positive here not lod_fpart since it may be the same
1681 * condition as that used in the outer "if" in the caller hence llvm
1682 * should be able to merge the branches in this case.
1684 need_lerp
= lp_build_any_true_range(&bld
->lodi_bld
, bld
->num_lods
, lod_positive
);
1686 lp_build_if(&if_ctx
, bld
->gallivm
, need_lerp
);
1689 * We unfortunately need to clamp lod_fpart here since we can get
1690 * negative values which would screw up filtering if not all
1691 * lod_fpart values have same sign.
1693 lod_fpart
= lp_build_max(&bld
->lodf_bld
, lod_fpart
,
1694 bld
->lodf_bld
.zero
);
1695 /* sample the second mipmap level */
1696 lp_build_mipmap_level_sizes(bld
, ilevel1
,
1698 &row_stride1_vec
, &img_stride1_vec
);
1699 if (bld
->num_mips
== 1) {
1700 data_ptr1
= lp_build_get_mipmap_level(bld
, ilevel1
);
1703 data_ptr1
= bld
->base_ptr
;
1704 mipoff1
= lp_build_get_mip_offsets(bld
, ilevel1
);
1707 lp_build_sample_image_linear(bld
, FALSE
, size1
, linear_mask
,
1708 row_stride1_vec
, img_stride1_vec
,
1709 data_ptr1
, mipoff1
, coords
, offsets
,
1712 /* interpolate samples from the two mipmap levels */
1714 if (bld
->num_lods
!= bld
->coord_type
.length
)
1715 lod_fpart
= lp_build_unpack_broadcast_aos_scalars(bld
->gallivm
,
1717 bld
->texel_bld
.type
,
1720 for (chan
= 0; chan
< 4; chan
++) {
1721 colors0
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
1722 colors0
[chan
], colors1
[chan
],
1724 LLVMBuildStore(builder
, colors0
[chan
], colors_out
[chan
]);
1727 lp_build_endif(&if_ctx
);
1733 * Build (per-coord) layer value.
1734 * Either clamp layer to valid values or fill in optional out_of_bounds
1735 * value and just return value unclamped.
1738 lp_build_layer_coord(struct lp_build_sample_context
*bld
,
1739 unsigned texture_unit
,
1740 boolean is_cube_array
,
1742 LLVMValueRef
*out_of_bounds
)
1744 LLVMValueRef num_layers
;
1745 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
1747 num_layers
= bld
->dynamic_state
->depth(bld
->dynamic_state
, bld
->gallivm
,
1748 bld
->context_ptr
, texture_unit
);
1750 if (out_of_bounds
) {
1751 LLVMValueRef out1
, out
;
1752 assert(!is_cube_array
);
1753 num_layers
= lp_build_broadcast_scalar(int_coord_bld
, num_layers
);
1754 out
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, layer
, int_coord_bld
->zero
);
1755 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, layer
, num_layers
);
1756 *out_of_bounds
= lp_build_or(int_coord_bld
, out
, out1
);
1760 LLVMValueRef maxlayer
;
1761 LLVMValueRef s
= is_cube_array
? lp_build_const_int32(bld
->gallivm
, 6) :
1763 maxlayer
= lp_build_sub(&bld
->int_bld
, num_layers
, s
);
1764 maxlayer
= lp_build_broadcast_scalar(int_coord_bld
, maxlayer
);
1765 return lp_build_clamp(int_coord_bld
, layer
, int_coord_bld
->zero
, maxlayer
);
1771 * Calculate cube face, lod, mip levels.
1774 lp_build_sample_common(struct lp_build_sample_context
*bld
,
1775 unsigned texture_index
,
1776 unsigned sampler_index
,
1777 LLVMValueRef
*coords
,
1778 const struct lp_derivatives
*derivs
, /* optional */
1779 LLVMValueRef lod_bias
, /* optional */
1780 LLVMValueRef explicit_lod
, /* optional */
1781 LLVMValueRef
*lod_pos_or_zero
,
1782 LLVMValueRef
*lod_fpart
,
1783 LLVMValueRef
*ilevel0
,
1784 LLVMValueRef
*ilevel1
)
1786 const unsigned mip_filter
= bld
->static_sampler_state
->min_mip_filter
;
1787 const unsigned min_filter
= bld
->static_sampler_state
->min_img_filter
;
1788 const unsigned mag_filter
= bld
->static_sampler_state
->mag_img_filter
;
1789 const unsigned target
= bld
->static_texture_state
->target
;
1790 LLVMValueRef first_level
, cube_rho
= NULL
;
1791 LLVMValueRef lod_ipart
= NULL
;
1792 struct lp_derivatives cube_derivs
;
1795 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1796 mip_filter, min_filter, mag_filter);
1800 * Choose cube face, recompute texcoords for the chosen face and
1801 * compute rho here too (as it requires transform of derivatives).
1803 if (target
== PIPE_TEXTURE_CUBE
|| target
== PIPE_TEXTURE_CUBE_ARRAY
) {
1804 boolean need_derivs
;
1805 need_derivs
= ((min_filter
!= mag_filter
||
1806 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) &&
1807 !bld
->static_sampler_state
->min_max_lod_equal
&&
1809 lp_build_cube_lookup(bld
, coords
, derivs
, &cube_rho
, &cube_derivs
, need_derivs
);
1810 derivs
= &cube_derivs
;
1811 if (target
== PIPE_TEXTURE_CUBE_ARRAY
) {
1812 /* calculate cube layer coord now */
1813 LLVMValueRef layer
= lp_build_iround(&bld
->coord_bld
, coords
[3]);
1814 LLVMValueRef six
= lp_build_const_int_vec(bld
->gallivm
, bld
->int_coord_type
, 6);
1815 layer
= lp_build_mul(&bld
->int_coord_bld
, layer
, six
);
1816 coords
[3] = lp_build_layer_coord(bld
, texture_index
, TRUE
, layer
, NULL
);
1817 /* because of seamless filtering can't add it to face (coords[2]) here. */
1820 else if (target
== PIPE_TEXTURE_1D_ARRAY
||
1821 target
== PIPE_TEXTURE_2D_ARRAY
) {
1822 coords
[2] = lp_build_iround(&bld
->coord_bld
, coords
[2]);
1823 coords
[2] = lp_build_layer_coord(bld
, texture_index
, FALSE
, coords
[2], NULL
);
1826 if (bld
->static_sampler_state
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
1828 * Clamp p coords to [0,1] for fixed function depth texture format here.
1829 * Technically this is not entirely correct for unorm depth as the ref value
1830 * should be converted to the depth format (quantization!) and comparison
1831 * then done in texture format. This would actually help performance (since
1832 * only need to do it once and could save the per-sample conversion of texels
1833 * to floats instead), but it would need more messy code (would need to push
1834 * at least some bits down to actual fetch so conversion could be skipped,
1835 * and would have ugly interaction with border color, would need to convert
1836 * border color to that format too or do some other tricks to make it work).
1838 const struct util_format_description
*format_desc
= bld
->format_desc
;
1840 /* not entirely sure we couldn't end up with non-valid swizzle here */
1841 chan_type
= format_desc
->swizzle
[0] <= UTIL_FORMAT_SWIZZLE_W
?
1842 format_desc
->channel
[format_desc
->swizzle
[0]].type
:
1843 UTIL_FORMAT_TYPE_FLOAT
;
1844 if (chan_type
!= UTIL_FORMAT_TYPE_FLOAT
) {
1845 coords
[4] = lp_build_clamp(&bld
->coord_bld
, coords
[4],
1846 bld
->coord_bld
.zero
, bld
->coord_bld
.one
);
1851 * Compute the level of detail (float).
1853 if (min_filter
!= mag_filter
||
1854 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
1855 /* Need to compute lod either to choose mipmap levels or to
1856 * distinguish between minification/magnification with one mipmap level.
1858 lp_build_lod_selector(bld
, texture_index
, sampler_index
,
1859 coords
[0], coords
[1], coords
[2], cube_rho
,
1860 derivs
, lod_bias
, explicit_lod
,
1862 &lod_ipart
, lod_fpart
, lod_pos_or_zero
);
1864 lod_ipart
= bld
->lodi_bld
.zero
;
1865 *lod_pos_or_zero
= bld
->lodi_bld
.zero
;
1868 if (bld
->num_lods
!= bld
->num_mips
) {
1869 /* only makes sense if there's just a single mip level */
1870 assert(bld
->num_mips
== 1);
1871 lod_ipart
= lp_build_extract_range(bld
->gallivm
, lod_ipart
, 0, 1);
1875 * Compute integer mipmap level(s) to fetch texels from: ilevel0, ilevel1
1877 switch (mip_filter
) {
1879 assert(0 && "bad mip_filter value in lp_build_sample_soa()");
1881 case PIPE_TEX_MIPFILTER_NONE
:
1882 /* always use mip level 0 */
1883 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
1884 bld
->gallivm
, bld
->context_ptr
,
1886 first_level
= lp_build_broadcast_scalar(&bld
->leveli_bld
, first_level
);
1887 *ilevel0
= first_level
;
1889 case PIPE_TEX_MIPFILTER_NEAREST
:
1891 lp_build_nearest_mip_level(bld
, texture_index
, lod_ipart
, ilevel0
, NULL
);
1893 case PIPE_TEX_MIPFILTER_LINEAR
:
1896 lp_build_linear_mip_levels(bld
, texture_index
,
1897 lod_ipart
, lod_fpart
,
1904 lp_build_clamp_border_color(struct lp_build_sample_context
*bld
,
1905 unsigned sampler_unit
)
1907 struct gallivm_state
*gallivm
= bld
->gallivm
;
1908 LLVMBuilderRef builder
= gallivm
->builder
;
1909 LLVMValueRef border_color_ptr
=
1910 bld
->dynamic_state
->border_color(bld
->dynamic_state
, gallivm
,
1911 bld
->context_ptr
, sampler_unit
);
1912 LLVMValueRef border_color
;
1913 const struct util_format_description
*format_desc
= bld
->format_desc
;
1914 struct lp_type vec4_type
= bld
->texel_type
;
1915 struct lp_build_context vec4_bld
;
1916 LLVMValueRef min_clamp
= NULL
;
1917 LLVMValueRef max_clamp
= NULL
;
1920 * For normalized format need to clamp border color (technically
1921 * probably should also quantize the data). Really sucks doing this
1922 * here but can't avoid at least for now since this is part of
1923 * sampler state and texture format is part of sampler_view state.
1924 * GL expects also expects clamping for uint/sint formats too so
1925 * do that as well (d3d10 can't end up here with uint/sint since it
1926 * only supports them with ld).
1928 vec4_type
.length
= 4;
1929 lp_build_context_init(&vec4_bld
, gallivm
, vec4_type
);
1932 * Vectorized clamping of border color. Loading is a bit of a hack since
1933 * we just cast the pointer to float array to pointer to vec4
1936 border_color_ptr
= lp_build_array_get_ptr(gallivm
, border_color_ptr
,
1937 lp_build_const_int32(gallivm
, 0));
1938 border_color_ptr
= LLVMBuildBitCast(builder
, border_color_ptr
,
1939 LLVMPointerType(vec4_bld
.vec_type
, 0), "");
1940 border_color
= LLVMBuildLoad(builder
, border_color_ptr
, "");
1941 /* we don't have aligned type in the dynamic state unfortunately */
1942 lp_set_load_alignment(border_color
, 4);
1945 * Instead of having some incredibly complex logic which will try to figure out
1946 * clamping necessary for each channel, simply use the first channel, and treat
1947 * mixed signed/unsigned normalized formats specially.
1948 * (Mixed non-normalized, which wouldn't work at all here, do not exist for a
1951 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
) {
1953 /* d/s needs special handling because both present means just sampling depth */
1954 if (util_format_is_depth_and_stencil(format_desc
->format
)) {
1955 chan
= format_desc
->swizzle
[0];
1958 chan
= util_format_get_first_non_void_channel(format_desc
->format
);
1960 if (chan
>= 0 && chan
<= UTIL_FORMAT_SWIZZLE_W
) {
1961 unsigned chan_type
= format_desc
->channel
[chan
].type
;
1962 unsigned chan_norm
= format_desc
->channel
[chan
].normalized
;
1963 unsigned chan_pure
= format_desc
->channel
[chan
].pure_integer
;
1964 if (chan_type
== UTIL_FORMAT_TYPE_SIGNED
) {
1966 min_clamp
= lp_build_const_vec(gallivm
, vec4_type
, -1.0F
);
1967 max_clamp
= vec4_bld
.one
;
1969 else if (chan_pure
) {
1971 * Border color was stored as int, hence need min/max clamp
1972 * only if chan has less than 32 bits..
1974 unsigned chan_size
= format_desc
->channel
[chan
].size
;
1975 if (chan_size
< 32) {
1976 min_clamp
= lp_build_const_int_vec(gallivm
, vec4_type
,
1977 0 - (1 << (chan_size
- 1)));
1978 max_clamp
= lp_build_const_int_vec(gallivm
, vec4_type
,
1979 (1 << (chan_size
- 1)) - 1);
1982 /* TODO: no idea about non-pure, non-normalized! */
1984 else if (chan_type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
1986 min_clamp
= vec4_bld
.zero
;
1987 max_clamp
= vec4_bld
.one
;
1990 * Need a ugly hack here, because we don't have Z32_FLOAT_X8X24
1991 * we use Z32_FLOAT_S8X24 to imply sampling depth component
1992 * and ignoring stencil, which will blow up here if we try to
1993 * do a uint clamp in a float texel build...
1994 * And even if we had that format, mesa st also thinks using z24s8
1995 * means depth sampling ignoring stencil.
1997 else if (chan_pure
) {
1999 * Border color was stored as uint, hence never need min
2000 * clamp, and only need max clamp if chan has less than 32 bits.
2002 unsigned chan_size
= format_desc
->channel
[chan
].size
;
2003 if (chan_size
< 32) {
2004 max_clamp
= lp_build_const_int_vec(gallivm
, vec4_type
,
2005 (1 << chan_size
) - 1);
2007 /* TODO: no idea about non-pure, non-normalized! */
2010 else if (chan_type
== UTIL_FORMAT_TYPE_FIXED
) {
2011 /* TODO: I have no idea what clamp this would need if any! */
2014 /* mixed plain formats (or different pure size) */
2015 switch (format_desc
->format
) {
2016 case PIPE_FORMAT_B10G10R10A2_UINT
:
2017 case PIPE_FORMAT_R10G10B10A2_UINT
:
2019 unsigned max10
= (1 << 10) - 1;
2020 max_clamp
= lp_build_const_aos(gallivm
, vec4_type
, max10
, max10
,
2021 max10
, (1 << 2) - 1, NULL
);
2024 case PIPE_FORMAT_R10SG10SB10SA2U_NORM
:
2025 min_clamp
= lp_build_const_aos(gallivm
, vec4_type
, -1.0F
, -1.0F
,
2027 max_clamp
= vec4_bld
.one
;
2029 case PIPE_FORMAT_R8SG8SB8UX8U_NORM
:
2030 case PIPE_FORMAT_R5SG5SB6U_NORM
:
2031 min_clamp
= lp_build_const_aos(gallivm
, vec4_type
, -1.0F
, -1.0F
,
2033 max_clamp
= vec4_bld
.one
;
2040 /* cannot figure this out from format description */
2041 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
2042 /* s3tc formats are always unorm */
2043 min_clamp
= vec4_bld
.zero
;
2044 max_clamp
= vec4_bld
.one
;
2046 else if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
||
2047 format_desc
->layout
== UTIL_FORMAT_LAYOUT_ETC
) {
2048 switch (format_desc
->format
) {
2049 case PIPE_FORMAT_RGTC1_UNORM
:
2050 case PIPE_FORMAT_RGTC2_UNORM
:
2051 case PIPE_FORMAT_LATC1_UNORM
:
2052 case PIPE_FORMAT_LATC2_UNORM
:
2053 case PIPE_FORMAT_ETC1_RGB8
:
2054 min_clamp
= vec4_bld
.zero
;
2055 max_clamp
= vec4_bld
.one
;
2057 case PIPE_FORMAT_RGTC1_SNORM
:
2058 case PIPE_FORMAT_RGTC2_SNORM
:
2059 case PIPE_FORMAT_LATC1_SNORM
:
2060 case PIPE_FORMAT_LATC2_SNORM
:
2061 min_clamp
= lp_build_const_vec(gallivm
, vec4_type
, -1.0F
);
2062 max_clamp
= vec4_bld
.one
;
2070 * all others from subsampled/other group, though we don't care
2071 * about yuv (and should not have any from zs here)
2073 else if (format_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_YUV
){
2074 switch (format_desc
->format
) {
2075 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
2076 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
2077 case PIPE_FORMAT_G8R8_B8R8_UNORM
:
2078 case PIPE_FORMAT_R8G8_R8B8_UNORM
:
2079 case PIPE_FORMAT_R1_UNORM
: /* doesn't make sense but ah well */
2080 min_clamp
= vec4_bld
.zero
;
2081 max_clamp
= vec4_bld
.one
;
2083 case PIPE_FORMAT_R8G8Bx_SNORM
:
2084 min_clamp
= lp_build_const_vec(gallivm
, vec4_type
, -1.0F
);
2085 max_clamp
= vec4_bld
.one
;
2088 * Note smallfloat formats usually don't need clamping
2089 * (they still have infinite range) however this is not
2090 * true for r11g11b10 and r9g9b9e5, which can't represent
2091 * negative numbers (and additionally r9g9b9e5 can't represent
2092 * very large numbers). d3d10 seems happy without clamping in
2093 * this case, but gl spec is pretty clear: "for floating
2094 * point and integer formats, border values are clamped to
2095 * the representable range of the format" so do that here.
2097 case PIPE_FORMAT_R11G11B10_FLOAT
:
2098 min_clamp
= vec4_bld
.zero
;
2100 case PIPE_FORMAT_R9G9B9E5_FLOAT
:
2101 min_clamp
= vec4_bld
.zero
;
2102 max_clamp
= lp_build_const_vec(gallivm
, vec4_type
, MAX_RGB9E5
);
2112 border_color
= lp_build_max(&vec4_bld
, border_color
, min_clamp
);
2115 border_color
= lp_build_min(&vec4_bld
, border_color
, max_clamp
);
2118 bld
->border_color_clamped
= border_color
;
2123 * General texture sampling codegen.
2124 * This function handles texture sampling for all texture targets (1D,
2125 * 2D, 3D, cube) and all filtering modes.
2128 lp_build_sample_general(struct lp_build_sample_context
*bld
,
2129 unsigned sampler_unit
,
2131 LLVMValueRef
*coords
,
2132 const LLVMValueRef
*offsets
,
2133 LLVMValueRef lod_positive
,
2134 LLVMValueRef lod_fpart
,
2135 LLVMValueRef ilevel0
,
2136 LLVMValueRef ilevel1
,
2137 LLVMValueRef
*colors_out
)
2139 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2140 const struct lp_static_sampler_state
*sampler_state
= bld
->static_sampler_state
;
2141 const unsigned mip_filter
= sampler_state
->min_mip_filter
;
2142 const unsigned min_filter
= sampler_state
->min_img_filter
;
2143 const unsigned mag_filter
= sampler_state
->mag_img_filter
;
2144 LLVMValueRef texels
[4];
2147 /* if we need border color, (potentially) clamp it now */
2148 if (lp_sampler_wrap_mode_uses_border_color(sampler_state
->wrap_s
,
2152 lp_sampler_wrap_mode_uses_border_color(sampler_state
->wrap_t
,
2156 lp_sampler_wrap_mode_uses_border_color(sampler_state
->wrap_r
,
2159 lp_build_clamp_border_color(bld
, sampler_unit
);
2164 * Get/interpolate texture colors.
2167 for (chan
= 0; chan
< 4; ++chan
) {
2168 texels
[chan
] = lp_build_alloca(bld
->gallivm
, bld
->texel_bld
.vec_type
, "");
2169 lp_build_name(texels
[chan
], "sampler%u_texel_%c_var", sampler_unit
, "xyzw"[chan
]);
2172 if (min_filter
== mag_filter
) {
2173 /* no need to distinguish between minification and magnification */
2174 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
,
2177 ilevel0
, ilevel1
, lod_fpart
,
2182 * Could also get rid of the if-logic and always use mipmap_both, both
2183 * for the single lod and multi-lod case if nothing really uses this.
2185 if (bld
->num_lods
== 1) {
2186 /* Emit conditional to choose min image filter or mag image filter
2187 * depending on the lod being > 0 or <= 0, respectively.
2189 struct lp_build_if_state if_ctx
;
2191 lod_positive
= LLVMBuildTrunc(builder
, lod_positive
,
2192 LLVMInt1TypeInContext(bld
->gallivm
->context
), "");
2194 lp_build_if(&if_ctx
, bld
->gallivm
, lod_positive
);
2196 /* Use the minification filter */
2197 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
, FALSE
,
2199 ilevel0
, ilevel1
, lod_fpart
,
2202 lp_build_else(&if_ctx
);
2204 /* Use the magnification filter */
2205 lp_build_sample_mipmap(bld
, mag_filter
, PIPE_TEX_MIPFILTER_NONE
,
2208 ilevel0
, NULL
, NULL
,
2211 lp_build_endif(&if_ctx
);
2214 LLVMValueRef need_linear
, linear_mask
;
2215 unsigned mip_filter_for_nearest
;
2216 struct lp_build_if_state if_ctx
;
2218 if (min_filter
== PIPE_TEX_FILTER_LINEAR
) {
2219 linear_mask
= lod_positive
;
2220 mip_filter_for_nearest
= PIPE_TEX_MIPFILTER_NONE
;
2223 linear_mask
= lp_build_not(&bld
->lodi_bld
, lod_positive
);
2224 mip_filter_for_nearest
= mip_filter
;
2226 need_linear
= lp_build_any_true_range(&bld
->lodi_bld
, bld
->num_lods
,
2229 if (bld
->num_lods
!= bld
->coord_type
.length
) {
2230 linear_mask
= lp_build_unpack_broadcast_aos_scalars(bld
->gallivm
,
2232 bld
->int_coord_type
,
2236 lp_build_if(&if_ctx
, bld
->gallivm
, need_linear
);
2239 * Do sampling with both filters simultaneously. This means using
2240 * a linear filter and doing some tricks (with weights) for the pixels
2241 * which need nearest filter.
2242 * Note that it's probably rare some pixels need nearest and some
2243 * linear filter but the fixups required for the nearest pixels
2244 * aren't all that complicated so just always run a combined path
2245 * if at least some pixels require linear.
2247 lp_build_sample_mipmap_both(bld
, linear_mask
, mip_filter
,
2250 lod_fpart
, lod_positive
,
2253 lp_build_else(&if_ctx
);
2256 * All pixels require just nearest filtering, which is way
2257 * cheaper than linear, hence do a separate path for that.
2259 lp_build_sample_mipmap(bld
, PIPE_TEX_FILTER_NEAREST
, FALSE
,
2260 mip_filter_for_nearest
,
2262 ilevel0
, ilevel1
, lod_fpart
,
2265 lp_build_endif(&if_ctx
);
2269 for (chan
= 0; chan
< 4; ++chan
) {
2270 colors_out
[chan
] = LLVMBuildLoad(builder
, texels
[chan
], "");
2271 lp_build_name(colors_out
[chan
], "sampler%u_texel_%c", sampler_unit
, "xyzw"[chan
]);
2277 * Texel fetch function.
2278 * In contrast to general sampling there is no filtering, no coord minification,
2279 * lod (if any) is always explicit uint, coords are uints (in terms of texel units)
2280 * directly to be applied to the selected mip level (after adding texel offsets).
2281 * This function handles texel fetch for all targets where texel fetch is supported
2282 * (no cube maps, but 1d, 2d, 3d are supported, arrays and buffers should be too).
2285 lp_build_fetch_texel(struct lp_build_sample_context
*bld
,
2286 unsigned texture_unit
,
2287 const LLVMValueRef
*coords
,
2288 LLVMValueRef explicit_lod
,
2289 const LLVMValueRef
*offsets
,
2290 LLVMValueRef
*colors_out
)
2292 struct lp_build_context
*perquadi_bld
= &bld
->lodi_bld
;
2293 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
2294 unsigned dims
= bld
->dims
, chan
;
2295 unsigned target
= bld
->static_texture_state
->target
;
2296 boolean out_of_bound_ret_zero
= TRUE
;
2297 LLVMValueRef size
, ilevel
;
2298 LLVMValueRef row_stride_vec
= NULL
, img_stride_vec
= NULL
;
2299 LLVMValueRef x
= coords
[0], y
= coords
[1], z
= coords
[2];
2300 LLVMValueRef width
, height
, depth
, i
, j
;
2301 LLVMValueRef offset
, out_of_bounds
, out1
;
2303 out_of_bounds
= int_coord_bld
->zero
;
2305 if (explicit_lod
&& bld
->static_texture_state
->target
!= PIPE_BUFFER
) {
2306 if (bld
->num_mips
!= int_coord_bld
->type
.length
) {
2307 ilevel
= lp_build_pack_aos_scalars(bld
->gallivm
, int_coord_bld
->type
,
2308 perquadi_bld
->type
, explicit_lod
, 0);
2311 ilevel
= explicit_lod
;
2313 lp_build_nearest_mip_level(bld
, texture_unit
, ilevel
, &ilevel
,
2314 out_of_bound_ret_zero
? &out_of_bounds
: NULL
);
2317 assert(bld
->num_mips
== 1);
2318 if (bld
->static_texture_state
->target
!= PIPE_BUFFER
) {
2319 ilevel
= bld
->dynamic_state
->first_level(bld
->dynamic_state
, bld
->gallivm
,
2320 bld
->context_ptr
, texture_unit
);
2323 ilevel
= lp_build_const_int32(bld
->gallivm
, 0);
2326 lp_build_mipmap_level_sizes(bld
, ilevel
,
2328 &row_stride_vec
, &img_stride_vec
);
2329 lp_build_extract_image_sizes(bld
, &bld
->int_size_bld
, int_coord_bld
->type
,
2330 size
, &width
, &height
, &depth
);
2332 if (target
== PIPE_TEXTURE_1D_ARRAY
||
2333 target
== PIPE_TEXTURE_2D_ARRAY
) {
2334 if (out_of_bound_ret_zero
) {
2335 z
= lp_build_layer_coord(bld
, texture_unit
, FALSE
, z
, &out1
);
2336 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
2339 z
= lp_build_layer_coord(bld
, texture_unit
, FALSE
, z
, NULL
);
2343 /* This is a lot like border sampling */
2346 * coords are really unsigned, offsets are signed, but I don't think
2347 * exceeding 31 bits is possible
2349 x
= lp_build_add(int_coord_bld
, x
, offsets
[0]);
2351 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
2352 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
2353 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
2354 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
2358 y
= lp_build_add(int_coord_bld
, y
, offsets
[1]);
2360 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
2361 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
2362 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
2363 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
2367 z
= lp_build_add(int_coord_bld
, z
, offsets
[2]);
2369 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
2370 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
2371 out1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
2372 out_of_bounds
= lp_build_or(int_coord_bld
, out_of_bounds
, out1
);
2376 lp_build_sample_offset(int_coord_bld
,
2378 x
, y
, z
, row_stride_vec
, img_stride_vec
,
2381 if (bld
->static_texture_state
->target
!= PIPE_BUFFER
) {
2382 offset
= lp_build_add(int_coord_bld
, offset
,
2383 lp_build_get_mip_offsets(bld
, ilevel
));
2386 offset
= lp_build_andnot(int_coord_bld
, offset
, out_of_bounds
);
2388 lp_build_fetch_rgba_soa(bld
->gallivm
,
2391 bld
->base_ptr
, offset
,
2396 if (out_of_bound_ret_zero
) {
2398 * Only needed for ARB_robust_buffer_access_behavior and d3d10.
2399 * Could use min/max above instead of out-of-bounds comparisons
2400 * if we don't care about the result returned for out-of-bounds.
2402 for (chan
= 0; chan
< 4; chan
++) {
2403 colors_out
[chan
] = lp_build_select(&bld
->texel_bld
, out_of_bounds
,
2404 bld
->texel_bld
.zero
, colors_out
[chan
]);
2411 * Just set texels to white instead of actually sampling the texture.
2415 lp_build_sample_nop(struct gallivm_state
*gallivm
,
2416 struct lp_type type
,
2417 const LLVMValueRef
*coords
,
2418 LLVMValueRef texel_out
[4])
2420 LLVMValueRef one
= lp_build_one(gallivm
, type
);
2423 for (chan
= 0; chan
< 4; chan
++) {
2424 texel_out
[chan
] = one
;
2430 * Build the actual texture sampling code.
2431 * 'texel' will return a vector of four LLVMValueRefs corresponding to
2433 * \param type vector float type to use for coords, etc.
2435 * \param derivs partial derivatives of (s,t,r,q) with respect to x and y
2438 lp_build_sample_soa_code(struct gallivm_state
*gallivm
,
2439 const struct lp_static_texture_state
*static_texture_state
,
2440 const struct lp_static_sampler_state
*static_sampler_state
,
2441 struct lp_sampler_dynamic_state
*dynamic_state
,
2442 struct lp_type type
,
2443 unsigned sample_key
,
2444 unsigned texture_index
,
2445 unsigned sampler_index
,
2446 LLVMValueRef context_ptr
,
2447 LLVMValueRef thread_data_ptr
,
2448 const LLVMValueRef
*coords
,
2449 const LLVMValueRef
*offsets
,
2450 const struct lp_derivatives
*derivs
, /* optional */
2451 LLVMValueRef lod
, /* optional */
2452 LLVMValueRef texel_out
[4])
2454 unsigned target
= static_texture_state
->target
;
2455 unsigned dims
= texture_dims(target
);
2456 unsigned num_quads
= type
.length
/ 4;
2457 unsigned mip_filter
, min_img_filter
, mag_img_filter
, i
;
2458 struct lp_build_sample_context bld
;
2459 struct lp_static_sampler_state derived_sampler_state
= *static_sampler_state
;
2460 LLVMTypeRef i32t
= LLVMInt32TypeInContext(gallivm
->context
);
2461 LLVMBuilderRef builder
= gallivm
->builder
;
2462 LLVMValueRef tex_width
, newcoords
[5];
2463 enum lp_sampler_lod_property lod_property
;
2464 enum lp_sampler_lod_control lod_control
;
2465 enum lp_sampler_op_type op_type
;
2466 LLVMValueRef lod_bias
= NULL
;
2467 LLVMValueRef explicit_lod
= NULL
;
2471 enum pipe_format fmt
= static_texture_state
->format
;
2472 debug_printf("Sample from %s\n", util_format_name(fmt
));
2475 lod_property
= (sample_key
& LP_SAMPLER_LOD_PROPERTY_MASK
) >>
2476 LP_SAMPLER_LOD_PROPERTY_SHIFT
;
2477 lod_control
= (sample_key
& LP_SAMPLER_LOD_CONTROL_MASK
) >>
2478 LP_SAMPLER_LOD_CONTROL_SHIFT
;
2479 op_type
= (sample_key
& LP_SAMPLER_OP_TYPE_MASK
) >>
2480 LP_SAMPLER_OP_TYPE_SHIFT
;
2482 op_is_tex
= op_type
== LP_SAMPLER_OP_TEXTURE
;
2484 if (lod_control
== LP_SAMPLER_LOD_BIAS
) {
2487 assert(derivs
== NULL
);
2489 else if (lod_control
== LP_SAMPLER_LOD_EXPLICIT
) {
2492 assert(derivs
== NULL
);
2494 else if (lod_control
== LP_SAMPLER_LOD_DERIVATIVES
) {
2496 assert(lod
== NULL
);
2499 assert(derivs
== NULL
);
2500 assert(lod
== NULL
);
2503 if (static_texture_state
->format
== PIPE_FORMAT_NONE
) {
2505 * If there's nothing bound, format is NONE, and we must return
2506 * all zero as mandated by d3d10 in this case.
2509 LLVMValueRef zero
= lp_build_zero(gallivm
, type
);
2510 for (chan
= 0; chan
< 4; chan
++) {
2511 texel_out
[chan
] = zero
;
2516 assert(type
.floating
);
2518 /* Setup our build context */
2519 memset(&bld
, 0, sizeof bld
);
2520 bld
.gallivm
= gallivm
;
2521 bld
.context_ptr
= context_ptr
;
2522 bld
.static_sampler_state
= &derived_sampler_state
;
2523 bld
.static_texture_state
= static_texture_state
;
2524 bld
.dynamic_state
= dynamic_state
;
2525 bld
.format_desc
= util_format_description(static_texture_state
->format
);
2528 bld
.vector_width
= lp_type_width(type
);
2530 bld
.float_type
= lp_type_float(32);
2531 bld
.int_type
= lp_type_int(32);
2532 bld
.coord_type
= type
;
2533 bld
.int_coord_type
= lp_int_type(type
);
2534 bld
.float_size_in_type
= lp_type_float(32);
2535 bld
.float_size_in_type
.length
= dims
> 1 ? 4 : 1;
2536 bld
.int_size_in_type
= lp_int_type(bld
.float_size_in_type
);
2537 bld
.texel_type
= type
;
2539 /* always using the first channel hopefully should be safe,
2540 * if not things WILL break in other places anyway.
2542 if (bld
.format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
&&
2543 bld
.format_desc
->channel
[0].pure_integer
) {
2544 if (bld
.format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_SIGNED
) {
2545 bld
.texel_type
= lp_type_int_vec(type
.width
, type
.width
* type
.length
);
2547 else if (bld
.format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
2548 bld
.texel_type
= lp_type_uint_vec(type
.width
, type
.width
* type
.length
);
2551 else if (util_format_has_stencil(bld
.format_desc
) &&
2552 !util_format_has_depth(bld
.format_desc
)) {
2553 /* for stencil only formats, sample stencil (uint) */
2554 bld
.texel_type
= lp_type_int_vec(type
.width
, type
.width
* type
.length
);
2557 if (!static_texture_state
->level_zero_only
) {
2558 derived_sampler_state
.min_mip_filter
= static_sampler_state
->min_mip_filter
;
2560 derived_sampler_state
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
2562 if (op_type
== LP_SAMPLER_OP_GATHER
) {
2564 * gather4 is exactly like GL_LINEAR filtering but in the end skipping
2565 * the actual filtering. Using mostly the same paths, so cube face
2566 * selection, coord wrapping etc. all naturally uses the same code.
2568 derived_sampler_state
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
2569 derived_sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
2570 derived_sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
2572 mip_filter
= derived_sampler_state
.min_mip_filter
;
2575 debug_printf(" .min_mip_filter = %u\n", derived_sampler_state
.min_mip_filter
);
2578 if (static_texture_state
->target
== PIPE_TEXTURE_CUBE
||
2579 static_texture_state
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
2582 * Seamless filtering ignores wrap modes.
2583 * Setting to CLAMP_TO_EDGE is correct for nearest filtering, for
2584 * bilinear it's not correct but way better than using for instance repeat.
2585 * Note we even set this for non-seamless. Technically GL allows any wrap
2586 * mode, which made sense when supporting true borders (can get seamless
2587 * effect with border and CLAMP_TO_BORDER), but gallium doesn't support
2588 * borders and d3d9 requires wrap modes to be ignored and it's a pain to fix
2589 * up the sampler state (as it makes it texture dependent).
2591 derived_sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
2592 derived_sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
2595 * We could force CLAMP to CLAMP_TO_EDGE here if min/mag filter is nearest,
2596 * so AoS path could be used. Not sure it's worth the trouble...
2599 min_img_filter
= derived_sampler_state
.min_img_filter
;
2600 mag_img_filter
= derived_sampler_state
.mag_img_filter
;
2604 * This is all a bit complicated different paths are chosen for performance
2606 * Essentially, there can be 1 lod per element, 1 lod per quad or 1 lod for
2607 * everything (the last two options are equivalent for 4-wide case).
2608 * If there's per-quad lod but we split to 4-wide so we can use AoS, per-quad
2609 * lod is calculated then the lod value extracted afterwards so making this
2610 * case basically the same as far as lod handling is concerned for the
2611 * further sample/filter code as the 1 lod for everything case.
2612 * Different lod handling mostly shows up when building mipmap sizes
2613 * (lp_build_mipmap_level_sizes() and friends) and also in filtering
2614 * (getting the fractional part of the lod to the right texels).
2618 * There are other situations where at least the multiple int lods could be
2619 * avoided like min and max lod being equal.
2621 bld
.num_mips
= bld
.num_lods
= 1;
2623 if ((gallivm_debug
& GALLIVM_DEBUG_NO_QUAD_LOD
) &&
2624 (gallivm_debug
& GALLIVM_DEBUG_NO_RHO_APPROX
) &&
2625 (static_texture_state
->target
== PIPE_TEXTURE_CUBE
||
2626 static_texture_state
->target
== PIPE_TEXTURE_CUBE_ARRAY
) &&
2627 (op_is_tex
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)) {
2629 * special case for using per-pixel lod even for implicit lod,
2630 * which is generally never required (ok by APIs) except to please
2631 * some (somewhat broken imho) tests (because per-pixel face selection
2632 * can cause derivatives to be different for pixels outside the primitive
2633 * due to the major axis division even if pre-project derivatives are
2636 bld
.num_mips
= type
.length
;
2637 bld
.num_lods
= type
.length
;
2639 else if (lod_property
== LP_SAMPLER_LOD_PER_ELEMENT
||
2640 (explicit_lod
|| lod_bias
|| derivs
)) {
2641 if ((!op_is_tex
&& target
!= PIPE_BUFFER
) ||
2642 (op_is_tex
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)) {
2643 bld
.num_mips
= type
.length
;
2644 bld
.num_lods
= type
.length
;
2646 else if (op_is_tex
&& min_img_filter
!= mag_img_filter
) {
2648 bld
.num_lods
= type
.length
;
2651 /* TODO: for true scalar_lod should only use 1 lod value */
2652 else if ((!op_is_tex
&& explicit_lod
&& target
!= PIPE_BUFFER
) ||
2653 (op_is_tex
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)) {
2654 bld
.num_mips
= num_quads
;
2655 bld
.num_lods
= num_quads
;
2657 else if (op_is_tex
&& min_img_filter
!= mag_img_filter
) {
2659 bld
.num_lods
= num_quads
;
2663 bld
.lodf_type
= type
;
2664 /* we want native vector size to be able to use our intrinsics */
2665 if (bld
.num_lods
!= type
.length
) {
2666 /* TODO: this currently always has to be per-quad or per-element */
2667 bld
.lodf_type
.length
= type
.length
> 4 ? ((type
.length
+ 15) / 16) * 4 : 1;
2669 bld
.lodi_type
= lp_int_type(bld
.lodf_type
);
2670 bld
.levelf_type
= bld
.lodf_type
;
2671 if (bld
.num_mips
== 1) {
2672 bld
.levelf_type
.length
= 1;
2674 bld
.leveli_type
= lp_int_type(bld
.levelf_type
);
2675 bld
.float_size_type
= bld
.float_size_in_type
;
2676 /* Note: size vectors may not be native. They contain minified w/h/d/_ values,
2677 * with per-element lod that is w0/h0/d0/_/w1/h1/d1_/... so up to 8x4f32 */
2678 if (bld
.num_mips
> 1) {
2679 bld
.float_size_type
.length
= bld
.num_mips
== type
.length
?
2680 bld
.num_mips
* bld
.float_size_in_type
.length
:
2683 bld
.int_size_type
= lp_int_type(bld
.float_size_type
);
2685 lp_build_context_init(&bld
.float_bld
, gallivm
, bld
.float_type
);
2686 lp_build_context_init(&bld
.float_vec_bld
, gallivm
, type
);
2687 lp_build_context_init(&bld
.int_bld
, gallivm
, bld
.int_type
);
2688 lp_build_context_init(&bld
.coord_bld
, gallivm
, bld
.coord_type
);
2689 lp_build_context_init(&bld
.int_coord_bld
, gallivm
, bld
.int_coord_type
);
2690 lp_build_context_init(&bld
.int_size_in_bld
, gallivm
, bld
.int_size_in_type
);
2691 lp_build_context_init(&bld
.float_size_in_bld
, gallivm
, bld
.float_size_in_type
);
2692 lp_build_context_init(&bld
.int_size_bld
, gallivm
, bld
.int_size_type
);
2693 lp_build_context_init(&bld
.float_size_bld
, gallivm
, bld
.float_size_type
);
2694 lp_build_context_init(&bld
.texel_bld
, gallivm
, bld
.texel_type
);
2695 lp_build_context_init(&bld
.levelf_bld
, gallivm
, bld
.levelf_type
);
2696 lp_build_context_init(&bld
.leveli_bld
, gallivm
, bld
.leveli_type
);
2697 lp_build_context_init(&bld
.lodf_bld
, gallivm
, bld
.lodf_type
);
2698 lp_build_context_init(&bld
.lodi_bld
, gallivm
, bld
.lodi_type
);
2700 /* Get the dynamic state */
2701 tex_width
= dynamic_state
->width(dynamic_state
, gallivm
,
2702 context_ptr
, texture_index
);
2703 bld
.row_stride_array
= dynamic_state
->row_stride(dynamic_state
, gallivm
,
2704 context_ptr
, texture_index
);
2705 bld
.img_stride_array
= dynamic_state
->img_stride(dynamic_state
, gallivm
,
2706 context_ptr
, texture_index
);
2707 bld
.base_ptr
= dynamic_state
->base_ptr(dynamic_state
, gallivm
,
2708 context_ptr
, texture_index
);
2709 bld
.mip_offsets
= dynamic_state
->mip_offsets(dynamic_state
, gallivm
,
2710 context_ptr
, texture_index
);
2711 /* Note that mip_offsets is an array[level] of offsets to texture images */
2713 if (dynamic_state
->cache_ptr
&& thread_data_ptr
) {
2714 bld
.cache
= dynamic_state
->cache_ptr(dynamic_state
, gallivm
,
2715 thread_data_ptr
, texture_index
);
2718 /* width, height, depth as single int vector */
2720 bld
.int_size
= tex_width
;
2723 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size_in_bld
.undef
,
2725 LLVMConstInt(i32t
, 0, 0), "");
2727 LLVMValueRef tex_height
=
2728 dynamic_state
->height(dynamic_state
, gallivm
,
2729 context_ptr
, texture_index
);
2730 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size
,
2732 LLVMConstInt(i32t
, 1, 0), "");
2734 LLVMValueRef tex_depth
=
2735 dynamic_state
->depth(dynamic_state
, gallivm
, context_ptr
,
2737 bld
.int_size
= LLVMBuildInsertElement(builder
, bld
.int_size
,
2739 LLVMConstInt(i32t
, 2, 0), "");
2744 for (i
= 0; i
< 5; i
++) {
2745 newcoords
[i
] = coords
[i
];
2749 /* For debug: no-op texture sampling */
2750 lp_build_sample_nop(gallivm
,
2756 else if (op_type
== LP_SAMPLER_OP_FETCH
) {
2757 lp_build_fetch_texel(&bld
, texture_index
, newcoords
,
2763 LLVMValueRef lod_fpart
= NULL
, lod_positive
= NULL
;
2764 LLVMValueRef ilevel0
= NULL
, ilevel1
= NULL
;
2767 if (util_format_is_pure_integer(static_texture_state
->format
) &&
2768 !util_format_has_depth(bld
.format_desc
) &&
2769 (static_sampler_state
->min_mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
||
2770 static_sampler_state
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
||
2771 static_sampler_state
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
)) {
2773 * Bail if impossible filtering is specified (the awkard additional
2774 * depth check is because it is legal in gallium to have things like S8Z24
2775 * here which would say it's pure int despite such formats should sample
2776 * the depth component).
2777 * In GL such filters make the texture incomplete, this makes it robust
2778 * against state trackers which set this up regardless (we'd crash in the
2779 * lerp later (except for gather)).
2780 * Must do this after fetch_texel code since with GL state tracker we'll
2781 * get some junk sampler for buffer textures.
2784 LLVMValueRef zero
= lp_build_zero(gallivm
, type
);
2785 for (chan
= 0; chan
< 4; chan
++) {
2786 texel_out
[chan
] = zero
;
2791 use_aos
= util_format_fits_8unorm(bld
.format_desc
) &&
2793 /* not sure this is strictly needed or simply impossible */
2794 derived_sampler_state
.compare_mode
== PIPE_TEX_COMPARE_NONE
&&
2795 lp_is_simple_wrap_mode(derived_sampler_state
.wrap_s
);
2797 use_aos
&= bld
.num_lods
<= num_quads
||
2798 derived_sampler_state
.min_img_filter
==
2799 derived_sampler_state
.mag_img_filter
;
2801 use_aos
&= lp_is_simple_wrap_mode(derived_sampler_state
.wrap_t
);
2803 use_aos
&= lp_is_simple_wrap_mode(derived_sampler_state
.wrap_r
);
2806 if ((static_texture_state
->target
== PIPE_TEXTURE_CUBE
||
2807 static_texture_state
->target
== PIPE_TEXTURE_CUBE_ARRAY
) &&
2808 derived_sampler_state
.seamless_cube_map
&&
2809 (derived_sampler_state
.min_img_filter
== PIPE_TEX_FILTER_LINEAR
||
2810 derived_sampler_state
.mag_img_filter
== PIPE_TEX_FILTER_LINEAR
)) {
2811 /* theoretically possible with AoS filtering but not implemented (complex!) */
2815 if ((gallivm_debug
& GALLIVM_DEBUG_PERF
) &&
2816 !use_aos
&& util_format_fits_8unorm(bld
.format_desc
)) {
2817 debug_printf("%s: using floating point linear filtering for %s\n",
2818 __FUNCTION__
, bld
.format_desc
->short_name
);
2819 debug_printf(" min_img %d mag_img %d mip %d target %d seamless %d"
2820 " wraps %d wrapt %d wrapr %d\n",
2821 derived_sampler_state
.min_img_filter
,
2822 derived_sampler_state
.mag_img_filter
,
2823 derived_sampler_state
.min_mip_filter
,
2824 static_texture_state
->target
,
2825 derived_sampler_state
.seamless_cube_map
,
2826 derived_sampler_state
.wrap_s
,
2827 derived_sampler_state
.wrap_t
,
2828 derived_sampler_state
.wrap_r
);
2831 lp_build_sample_common(&bld
, texture_index
, sampler_index
,
2833 derivs
, lod_bias
, explicit_lod
,
2834 &lod_positive
, &lod_fpart
,
2835 &ilevel0
, &ilevel1
);
2837 if (use_aos
&& static_texture_state
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
2838 /* The aos path doesn't do seamless filtering so simply add cube layer
2841 newcoords
[2] = lp_build_add(&bld
.int_coord_bld
, newcoords
[2], newcoords
[3]);
2845 * we only try 8-wide sampling with soa as it appears to
2846 * be a loss with aos with AVX (but it should work, except
2847 * for conformance if min_filter != mag_filter if num_lods > 1).
2848 * (It should be faster if we'd support avx2)
2850 if (num_quads
== 1 || !use_aos
) {
2852 /* do sampling/filtering with fixed pt arithmetic */
2853 lp_build_sample_aos(&bld
, sampler_index
,
2854 newcoords
[0], newcoords
[1],
2856 offsets
, lod_positive
, lod_fpart
,
2862 lp_build_sample_general(&bld
, sampler_index
,
2863 op_type
== LP_SAMPLER_OP_GATHER
,
2865 lod_positive
, lod_fpart
,
2872 struct lp_build_sample_context bld4
;
2873 struct lp_type type4
= type
;
2875 LLVMValueRef texelout4
[4];
2876 LLVMValueRef texelouttmp
[4][LP_MAX_VECTOR_LENGTH
/16];
2880 /* Setup our build context */
2881 memset(&bld4
, 0, sizeof bld4
);
2882 bld4
.gallivm
= bld
.gallivm
;
2883 bld4
.context_ptr
= bld
.context_ptr
;
2884 bld4
.static_texture_state
= bld
.static_texture_state
;
2885 bld4
.static_sampler_state
= bld
.static_sampler_state
;
2886 bld4
.dynamic_state
= bld
.dynamic_state
;
2887 bld4
.format_desc
= bld
.format_desc
;
2888 bld4
.dims
= bld
.dims
;
2889 bld4
.row_stride_array
= bld
.row_stride_array
;
2890 bld4
.img_stride_array
= bld
.img_stride_array
;
2891 bld4
.base_ptr
= bld
.base_ptr
;
2892 bld4
.mip_offsets
= bld
.mip_offsets
;
2893 bld4
.int_size
= bld
.int_size
;
2894 bld4
.cache
= bld
.cache
;
2896 bld4
.vector_width
= lp_type_width(type4
);
2898 bld4
.float_type
= lp_type_float(32);
2899 bld4
.int_type
= lp_type_int(32);
2900 bld4
.coord_type
= type4
;
2901 bld4
.int_coord_type
= lp_int_type(type4
);
2902 bld4
.float_size_in_type
= lp_type_float(32);
2903 bld4
.float_size_in_type
.length
= dims
> 1 ? 4 : 1;
2904 bld4
.int_size_in_type
= lp_int_type(bld4
.float_size_in_type
);
2905 bld4
.texel_type
= bld
.texel_type
;
2906 bld4
.texel_type
.length
= 4;
2908 bld4
.num_mips
= bld4
.num_lods
= 1;
2909 if ((gallivm_debug
& GALLIVM_DEBUG_NO_QUAD_LOD
) &&
2910 (gallivm_debug
& GALLIVM_DEBUG_NO_RHO_APPROX
) &&
2911 (static_texture_state
->target
== PIPE_TEXTURE_CUBE
||
2912 static_texture_state
->target
== PIPE_TEXTURE_CUBE_ARRAY
) &&
2913 (op_is_tex
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)) {
2914 bld4
.num_mips
= type4
.length
;
2915 bld4
.num_lods
= type4
.length
;
2917 if (lod_property
== LP_SAMPLER_LOD_PER_ELEMENT
&&
2918 (explicit_lod
|| lod_bias
|| derivs
)) {
2919 if ((!op_is_tex
&& target
!= PIPE_BUFFER
) ||
2920 (op_is_tex
&& mip_filter
!= PIPE_TEX_MIPFILTER_NONE
)) {
2921 bld4
.num_mips
= type4
.length
;
2922 bld4
.num_lods
= type4
.length
;
2924 else if (op_is_tex
&& min_img_filter
!= mag_img_filter
) {
2926 bld4
.num_lods
= type4
.length
;
2930 /* we want native vector size to be able to use our intrinsics */
2931 bld4
.lodf_type
= type4
;
2932 if (bld4
.num_lods
!= type4
.length
) {
2933 bld4
.lodf_type
.length
= 1;
2935 bld4
.lodi_type
= lp_int_type(bld4
.lodf_type
);
2936 bld4
.levelf_type
= type4
;
2937 if (bld4
.num_mips
!= type4
.length
) {
2938 bld4
.levelf_type
.length
= 1;
2940 bld4
.leveli_type
= lp_int_type(bld4
.levelf_type
);
2941 bld4
.float_size_type
= bld4
.float_size_in_type
;
2942 if (bld4
.num_mips
> 1) {
2943 bld4
.float_size_type
.length
= bld4
.num_mips
== type4
.length
?
2944 bld4
.num_mips
* bld4
.float_size_in_type
.length
:
2947 bld4
.int_size_type
= lp_int_type(bld4
.float_size_type
);
2949 lp_build_context_init(&bld4
.float_bld
, gallivm
, bld4
.float_type
);
2950 lp_build_context_init(&bld4
.float_vec_bld
, gallivm
, type4
);
2951 lp_build_context_init(&bld4
.int_bld
, gallivm
, bld4
.int_type
);
2952 lp_build_context_init(&bld4
.coord_bld
, gallivm
, bld4
.coord_type
);
2953 lp_build_context_init(&bld4
.int_coord_bld
, gallivm
, bld4
.int_coord_type
);
2954 lp_build_context_init(&bld4
.int_size_in_bld
, gallivm
, bld4
.int_size_in_type
);
2955 lp_build_context_init(&bld4
.float_size_in_bld
, gallivm
, bld4
.float_size_in_type
);
2956 lp_build_context_init(&bld4
.int_size_bld
, gallivm
, bld4
.int_size_type
);
2957 lp_build_context_init(&bld4
.float_size_bld
, gallivm
, bld4
.float_size_type
);
2958 lp_build_context_init(&bld4
.texel_bld
, gallivm
, bld4
.texel_type
);
2959 lp_build_context_init(&bld4
.levelf_bld
, gallivm
, bld4
.levelf_type
);
2960 lp_build_context_init(&bld4
.leveli_bld
, gallivm
, bld4
.leveli_type
);
2961 lp_build_context_init(&bld4
.lodf_bld
, gallivm
, bld4
.lodf_type
);
2962 lp_build_context_init(&bld4
.lodi_bld
, gallivm
, bld4
.lodi_type
);
2964 for (i
= 0; i
< num_quads
; i
++) {
2965 LLVMValueRef s4
, t4
, r4
;
2966 LLVMValueRef lod_positive4
, lod_fpart4
= NULL
;
2967 LLVMValueRef ilevel04
, ilevel14
= NULL
;
2968 LLVMValueRef offsets4
[4] = { NULL
};
2969 unsigned num_lods
= bld4
.num_lods
;
2971 s4
= lp_build_extract_range(gallivm
, newcoords
[0], 4*i
, 4);
2972 t4
= lp_build_extract_range(gallivm
, newcoords
[1], 4*i
, 4);
2973 r4
= lp_build_extract_range(gallivm
, newcoords
[2], 4*i
, 4);
2976 offsets4
[0] = lp_build_extract_range(gallivm
, offsets
[0], 4*i
, 4);
2978 offsets4
[1] = lp_build_extract_range(gallivm
, offsets
[1], 4*i
, 4);
2980 offsets4
[2] = lp_build_extract_range(gallivm
, offsets
[2], 4*i
, 4);
2984 lod_positive4
= lp_build_extract_range(gallivm
, lod_positive
, num_lods
* i
, num_lods
);
2985 ilevel04
= bld
.num_mips
== 1 ? ilevel0
:
2986 lp_build_extract_range(gallivm
, ilevel0
, num_lods
* i
, num_lods
);
2987 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
2988 ilevel14
= lp_build_extract_range(gallivm
, ilevel1
, num_lods
* i
, num_lods
);
2989 lod_fpart4
= lp_build_extract_range(gallivm
, lod_fpart
, num_lods
* i
, num_lods
);
2993 /* do sampling/filtering with fixed pt arithmetic */
2994 lp_build_sample_aos(&bld4
, sampler_index
,
2995 s4
, t4
, r4
, offsets4
,
2996 lod_positive4
, lod_fpart4
,
3002 /* this path is currently unreachable and hence might break easily... */
3003 LLVMValueRef newcoords4
[5];
3007 newcoords4
[3] = lp_build_extract_range(gallivm
, newcoords
[3], 4*i
, 4);
3008 newcoords4
[4] = lp_build_extract_range(gallivm
, newcoords
[4], 4*i
, 4);
3010 lp_build_sample_general(&bld4
, sampler_index
,
3011 op_type
== LP_SAMPLER_OP_GATHER
,
3012 newcoords4
, offsets4
,
3013 lod_positive4
, lod_fpart4
,
3017 for (j
= 0; j
< 4; j
++) {
3018 texelouttmp
[j
][i
] = texelout4
[j
];
3022 for (j
= 0; j
< 4; j
++) {
3023 texel_out
[j
] = lp_build_concat(gallivm
, texelouttmp
[j
], type4
, num_quads
);
3028 if (target
!= PIPE_BUFFER
&& op_type
!= LP_SAMPLER_OP_GATHER
) {
3029 apply_sampler_swizzle(&bld
, texel_out
);
3033 * texel type can be a (32bit) int/uint (for pure int formats only),
3034 * however we are expected to always return floats (storage is untyped).
3036 if (!bld
.texel_type
.floating
) {
3038 for (chan
= 0; chan
< 4; chan
++) {
3039 texel_out
[chan
] = LLVMBuildBitCast(builder
, texel_out
[chan
],
3040 lp_build_vec_type(gallivm
, type
), "");
3046 #define USE_TEX_FUNC_CALL 1
3048 #define LP_MAX_TEX_FUNC_ARGS 32
3051 get_target_info(enum pipe_texture_target target
,
3052 unsigned *num_coords
, unsigned *num_derivs
,
3053 unsigned *num_offsets
, unsigned *layer
)
3055 unsigned dims
= texture_dims(target
);
3057 *num_offsets
= dims
;
3058 *num_derivs
= (target
== PIPE_TEXTURE_CUBE
||
3059 target
== PIPE_TEXTURE_CUBE_ARRAY
) ? 3 : dims
;
3060 *layer
= has_layer_coord(target
) ? 2: 0;
3061 if (target
== PIPE_TEXTURE_CUBE_ARRAY
) {
3063 * dims doesn't include r coord for cubes - this is handled
3064 * by layer instead, but need to fix up for cube arrays...
3073 * Generate the function body for a texture sampling function.
3076 lp_build_sample_gen_func(struct gallivm_state
*gallivm
,
3077 const struct lp_static_texture_state
*static_texture_state
,
3078 const struct lp_static_sampler_state
*static_sampler_state
,
3079 struct lp_sampler_dynamic_state
*dynamic_state
,
3080 struct lp_type type
,
3081 unsigned texture_index
,
3082 unsigned sampler_index
,
3083 LLVMValueRef function
,
3085 unsigned sample_key
)
3087 LLVMBuilderRef old_builder
;
3088 LLVMBasicBlockRef block
;
3089 LLVMValueRef coords
[5];
3090 LLVMValueRef offsets
[3] = { NULL
};
3091 LLVMValueRef lod
= NULL
;
3092 LLVMValueRef context_ptr
;
3093 LLVMValueRef thread_data_ptr
= NULL
;
3094 LLVMValueRef texel_out
[4];
3095 struct lp_derivatives derivs
;
3096 struct lp_derivatives
*deriv_ptr
= NULL
;
3097 unsigned num_param
= 0;
3098 unsigned i
, num_coords
, num_derivs
, num_offsets
, layer
;
3099 enum lp_sampler_lod_control lod_control
;
3100 boolean need_cache
= FALSE
;
3102 lod_control
= (sample_key
& LP_SAMPLER_LOD_CONTROL_MASK
) >>
3103 LP_SAMPLER_LOD_CONTROL_SHIFT
;
3105 get_target_info(static_texture_state
->target
,
3106 &num_coords
, &num_derivs
, &num_offsets
, &layer
);
3108 if (dynamic_state
->cache_ptr
) {
3109 const struct util_format_description
*format_desc
;
3110 format_desc
= util_format_description(static_texture_state
->format
);
3111 if (format_desc
&& format_desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
3116 /* "unpack" arguments */
3117 context_ptr
= LLVMGetParam(function
, num_param
++);
3119 thread_data_ptr
= LLVMGetParam(function
, num_param
++);
3121 for (i
= 0; i
< num_coords
; i
++) {
3122 coords
[i
] = LLVMGetParam(function
, num_param
++);
3124 for (i
= num_coords
; i
< 5; i
++) {
3125 /* This is rather unfortunate... */
3126 coords
[i
] = lp_build_undef(gallivm
, type
);
3129 coords
[layer
] = LLVMGetParam(function
, num_param
++);
3131 if (sample_key
& LP_SAMPLER_SHADOW
) {
3132 coords
[4] = LLVMGetParam(function
, num_param
++);
3134 if (sample_key
& LP_SAMPLER_OFFSETS
) {
3135 for (i
= 0; i
< num_offsets
; i
++) {
3136 offsets
[i
] = LLVMGetParam(function
, num_param
++);
3139 if (lod_control
== LP_SAMPLER_LOD_BIAS
||
3140 lod_control
== LP_SAMPLER_LOD_EXPLICIT
) {
3141 lod
= LLVMGetParam(function
, num_param
++);
3143 else if (lod_control
== LP_SAMPLER_LOD_DERIVATIVES
) {
3144 for (i
= 0; i
< num_derivs
; i
++) {
3145 derivs
.ddx
[i
] = LLVMGetParam(function
, num_param
++);
3146 derivs
.ddy
[i
] = LLVMGetParam(function
, num_param
++);
3148 deriv_ptr
= &derivs
;
3151 assert(num_args
== num_param
);
3157 old_builder
= gallivm
->builder
;
3158 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, function
, "entry");
3159 gallivm
->builder
= LLVMCreateBuilderInContext(gallivm
->context
);
3160 LLVMPositionBuilderAtEnd(gallivm
->builder
, block
);
3162 lp_build_sample_soa_code(gallivm
,
3163 static_texture_state
,
3164 static_sampler_state
,
3178 LLVMBuildAggregateRet(gallivm
->builder
, texel_out
, 4);
3180 LLVMDisposeBuilder(gallivm
->builder
);
3181 gallivm
->builder
= old_builder
;
3183 gallivm_verify_function(gallivm
, function
);
3188 * Call the matching function for texture sampling.
3189 * If there's no match, generate a new one.
3192 lp_build_sample_soa_func(struct gallivm_state
*gallivm
,
3193 const struct lp_static_texture_state
*static_texture_state
,
3194 const struct lp_static_sampler_state
*static_sampler_state
,
3195 struct lp_sampler_dynamic_state
*dynamic_state
,
3196 const struct lp_sampler_params
*params
)
3198 LLVMBuilderRef builder
= gallivm
->builder
;
3199 LLVMModuleRef module
= LLVMGetGlobalParent(LLVMGetBasicBlockParent(
3200 LLVMGetInsertBlock(builder
)));
3201 LLVMValueRef function
, inst
;
3202 LLVMValueRef args
[LP_MAX_TEX_FUNC_ARGS
];
3203 LLVMBasicBlockRef bb
;
3204 LLVMValueRef tex_ret
;
3205 unsigned num_args
= 0;
3207 unsigned i
, num_coords
, num_derivs
, num_offsets
, layer
;
3208 unsigned texture_index
= params
->texture_index
;
3209 unsigned sampler_index
= params
->sampler_index
;
3210 unsigned sample_key
= params
->sample_key
;
3211 const LLVMValueRef
*coords
= params
->coords
;
3212 const LLVMValueRef
*offsets
= params
->offsets
;
3213 const struct lp_derivatives
*derivs
= params
->derivs
;
3214 enum lp_sampler_lod_control lod_control
;
3215 boolean need_cache
= FALSE
;
3217 lod_control
= (sample_key
& LP_SAMPLER_LOD_CONTROL_MASK
) >>
3218 LP_SAMPLER_LOD_CONTROL_SHIFT
;
3220 get_target_info(static_texture_state
->target
,
3221 &num_coords
, &num_derivs
, &num_offsets
, &layer
);
3223 if (dynamic_state
->cache_ptr
) {
3224 const struct util_format_description
*format_desc
;
3225 format_desc
= util_format_description(static_texture_state
->format
);
3226 if (format_desc
&& format_desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
3228 * This is not 100% correct, if we have cache but the
3229 * util_format_s3tc_prefer is true the cache won't get used
3230 * regardless (could hook up the block decode there...) */
3235 * texture function matches are found by name.
3236 * Thus the name has to include both the texture and sampler unit
3237 * (which covers all static state) plus the actual texture function
3238 * (including things like offsets, shadow coord, lod control).
3239 * Additionally lod_property has to be included too.
3242 util_snprintf(func_name
, sizeof(func_name
), "texfunc_res_%d_sam_%d_%x",
3243 texture_index
, sampler_index
, sample_key
);
3245 function
= LLVMGetNamedFunction(module
, func_name
);
3248 LLVMTypeRef arg_types
[LP_MAX_TEX_FUNC_ARGS
];
3249 LLVMTypeRef ret_type
;
3250 LLVMTypeRef function_type
;
3251 LLVMTypeRef val_type
[4];
3252 unsigned num_param
= 0;
3255 * Generate the function prototype.
3258 arg_types
[num_param
++] = LLVMTypeOf(params
->context_ptr
);
3260 arg_types
[num_param
++] = LLVMTypeOf(params
->thread_data_ptr
);
3262 for (i
= 0; i
< num_coords
; i
++) {
3263 arg_types
[num_param
++] = LLVMTypeOf(coords
[0]);
3264 assert(LLVMTypeOf(coords
[0]) == LLVMTypeOf(coords
[i
]));
3267 arg_types
[num_param
++] = LLVMTypeOf(coords
[layer
]);
3268 assert(LLVMTypeOf(coords
[0]) == LLVMTypeOf(coords
[layer
]));
3270 if (sample_key
& LP_SAMPLER_SHADOW
) {
3271 arg_types
[num_param
++] = LLVMTypeOf(coords
[0]);
3273 if (sample_key
& LP_SAMPLER_OFFSETS
) {
3274 for (i
= 0; i
< num_offsets
; i
++) {
3275 arg_types
[num_param
++] = LLVMTypeOf(offsets
[0]);
3276 assert(LLVMTypeOf(offsets
[0]) == LLVMTypeOf(offsets
[i
]));
3279 if (lod_control
== LP_SAMPLER_LOD_BIAS
||
3280 lod_control
== LP_SAMPLER_LOD_EXPLICIT
) {
3281 arg_types
[num_param
++] = LLVMTypeOf(params
->lod
);
3283 else if (lod_control
== LP_SAMPLER_LOD_DERIVATIVES
) {
3284 for (i
= 0; i
< num_derivs
; i
++) {
3285 arg_types
[num_param
++] = LLVMTypeOf(derivs
->ddx
[i
]);
3286 arg_types
[num_param
++] = LLVMTypeOf(derivs
->ddy
[i
]);
3287 assert(LLVMTypeOf(derivs
->ddx
[0]) == LLVMTypeOf(derivs
->ddx
[i
]));
3288 assert(LLVMTypeOf(derivs
->ddy
[0]) == LLVMTypeOf(derivs
->ddy
[i
]));
3292 val_type
[0] = val_type
[1] = val_type
[2] = val_type
[3] =
3293 lp_build_vec_type(gallivm
, params
->type
);
3294 ret_type
= LLVMStructTypeInContext(gallivm
->context
, val_type
, 4, 0);
3295 function_type
= LLVMFunctionType(ret_type
, arg_types
, num_param
, 0);
3296 function
= LLVMAddFunction(module
, func_name
, function_type
);
3298 for (i
= 0; i
< num_param
; ++i
) {
3299 if(LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
) {
3300 LLVMAddAttribute(LLVMGetParam(function
, i
), LLVMNoAliasAttribute
);
3304 LLVMSetFunctionCallConv(function
, LLVMFastCallConv
);
3305 LLVMSetLinkage(function
, LLVMPrivateLinkage
);
3307 lp_build_sample_gen_func(gallivm
,
3308 static_texture_state
,
3309 static_sampler_state
,
3320 args
[num_args
++] = params
->context_ptr
;
3322 args
[num_args
++] = params
->thread_data_ptr
;
3324 for (i
= 0; i
< num_coords
; i
++) {
3325 args
[num_args
++] = coords
[i
];
3328 args
[num_args
++] = coords
[layer
];
3330 if (sample_key
& LP_SAMPLER_SHADOW
) {
3331 args
[num_args
++] = coords
[4];
3333 if (sample_key
& LP_SAMPLER_OFFSETS
) {
3334 for (i
= 0; i
< num_offsets
; i
++) {
3335 args
[num_args
++] = offsets
[i
];
3338 if (lod_control
== LP_SAMPLER_LOD_BIAS
||
3339 lod_control
== LP_SAMPLER_LOD_EXPLICIT
) {
3340 args
[num_args
++] = params
->lod
;
3342 else if (lod_control
== LP_SAMPLER_LOD_DERIVATIVES
) {
3343 for (i
= 0; i
< num_derivs
; i
++) {
3344 args
[num_args
++] = derivs
->ddx
[i
];
3345 args
[num_args
++] = derivs
->ddy
[i
];
3349 assert(num_args
<= LP_MAX_TEX_FUNC_ARGS
);
3351 tex_ret
= LLVMBuildCall(builder
, function
, args
, num_args
, "");
3352 bb
= LLVMGetInsertBlock(builder
);
3353 inst
= LLVMGetLastInstruction(bb
);
3354 LLVMSetInstructionCallConv(inst
, LLVMFastCallConv
);
3356 for (i
= 0; i
< 4; i
++) {
3357 params
->texel
[i
] = LLVMBuildExtractValue(gallivm
->builder
, tex_ret
, i
, "");
3363 * Build texture sampling code.
3364 * Either via a function call or inline it directly.
3367 lp_build_sample_soa(const struct lp_static_texture_state
*static_texture_state
,
3368 const struct lp_static_sampler_state
*static_sampler_state
,
3369 struct lp_sampler_dynamic_state
*dynamic_state
,
3370 struct gallivm_state
*gallivm
,
3371 const struct lp_sampler_params
*params
)
3373 boolean use_tex_func
= FALSE
;
3376 * Do not use a function call if the sampling is "simple enough".
3379 * b) no mips (either one level only or no mip filter)
3380 * No mips will definitely make the code smaller, though
3381 * the format requirement is a bit iffy - there's some (SoA) formats
3382 * which definitely generate less code. This does happen to catch
3383 * some important cases though which are hurt quite a bit by using
3384 * a call (though not really because of the call overhead but because
3385 * they are reusing the same texture unit with some of the same
3387 * Ideally we'd let llvm recognize this stuff by doing IPO passes.
3390 if (USE_TEX_FUNC_CALL
) {
3391 const struct util_format_description
*format_desc
;
3392 boolean simple_format
;
3394 enum lp_sampler_op_type op_type
;
3395 format_desc
= util_format_description(static_texture_state
->format
);
3396 simple_format
= !format_desc
||
3397 (util_format_is_rgba8_variant(format_desc
) &&
3398 format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
);
3400 op_type
= (params
->sample_key
& LP_SAMPLER_OP_TYPE_MASK
) >>
3401 LP_SAMPLER_OP_TYPE_SHIFT
;
3403 op_type
!= LP_SAMPLER_OP_TEXTURE
||
3404 ((static_sampler_state
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
||
3405 static_texture_state
->level_zero_only
== TRUE
) &&
3406 static_sampler_state
->min_img_filter
== static_sampler_state
->mag_img_filter
);
3408 use_tex_func
= format_desc
&& !(simple_format
&& simple_tex
);
3412 lp_build_sample_soa_func(gallivm
,
3413 static_texture_state
,
3414 static_sampler_state
,
3419 lp_build_sample_soa_code(gallivm
,
3420 static_texture_state
,
3421 static_sampler_state
,
3425 params
->texture_index
,
3426 params
->sampler_index
,
3427 params
->context_ptr
,
3428 params
->thread_data_ptr
,
3439 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
3440 const struct lp_static_texture_state
*static_state
,
3441 struct lp_sampler_dynamic_state
*dynamic_state
,
3442 struct lp_type int_type
,
3443 unsigned texture_unit
,
3445 LLVMValueRef context_ptr
,
3446 boolean is_sviewinfo
,
3447 enum lp_sampler_lod_property lod_property
,
3448 LLVMValueRef explicit_lod
,
3449 LLVMValueRef
*sizes_out
)
3451 LLVMValueRef lod
, level
, size
;
3452 LLVMValueRef first_level
= NULL
;
3455 unsigned num_lods
= 1;
3456 struct lp_build_context bld_int_vec4
;
3458 if (static_state
->format
== PIPE_FORMAT_NONE
) {
3460 * If there's nothing bound, format is NONE, and we must return
3461 * all zero as mandated by d3d10 in this case.
3464 LLVMValueRef zero
= lp_build_const_vec(gallivm
, int_type
, 0.0F
);
3465 for (chan
= 0; chan
< 4; chan
++) {
3466 sizes_out
[chan
] = zero
;
3472 * Do some sanity verification about bound texture and shader dcl target.
3473 * Not entirely sure what's possible but assume array/non-array
3474 * always compatible (probably not ok for OpenGL but d3d10 has no
3475 * distinction of arrays at the resource level).
3476 * Everything else looks bogus (though not entirely sure about rect/2d).
3477 * Currently disabled because it causes assertion failures if there's
3478 * nothing bound (or rather a dummy texture, not that this case would
3479 * return the right values).
3481 if (0 && static_state
->target
!= target
) {
3482 if (static_state
->target
== PIPE_TEXTURE_1D
)
3483 assert(target
== PIPE_TEXTURE_1D_ARRAY
);
3484 else if (static_state
->target
== PIPE_TEXTURE_1D_ARRAY
)
3485 assert(target
== PIPE_TEXTURE_1D
);
3486 else if (static_state
->target
== PIPE_TEXTURE_2D
)
3487 assert(target
== PIPE_TEXTURE_2D_ARRAY
);
3488 else if (static_state
->target
== PIPE_TEXTURE_2D_ARRAY
)
3489 assert(target
== PIPE_TEXTURE_2D
);
3490 else if (static_state
->target
== PIPE_TEXTURE_CUBE
)
3491 assert(target
== PIPE_TEXTURE_CUBE_ARRAY
);
3492 else if (static_state
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
3493 assert(target
== PIPE_TEXTURE_CUBE
);
3498 dims
= texture_dims(target
);
3501 case PIPE_TEXTURE_1D_ARRAY
:
3502 case PIPE_TEXTURE_2D_ARRAY
:
3503 case PIPE_TEXTURE_CUBE_ARRAY
:
3511 assert(!int_type
.floating
);
3513 lp_build_context_init(&bld_int_vec4
, gallivm
, lp_type_int_vec(32, 128));
3516 /* FIXME: this needs to honor per-element lod */
3517 lod
= LLVMBuildExtractElement(gallivm
->builder
, explicit_lod
,
3518 lp_build_const_int32(gallivm
, 0), "");
3519 first_level
= dynamic_state
->first_level(dynamic_state
, gallivm
,
3520 context_ptr
, texture_unit
);
3521 level
= LLVMBuildAdd(gallivm
->builder
, lod
, first_level
, "level");
3522 lod
= lp_build_broadcast_scalar(&bld_int_vec4
, level
);
3524 lod
= bld_int_vec4
.zero
;
3527 size
= bld_int_vec4
.undef
;
3529 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
3530 dynamic_state
->width(dynamic_state
, gallivm
,
3531 context_ptr
, texture_unit
),
3532 lp_build_const_int32(gallivm
, 0), "");
3535 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
3536 dynamic_state
->height(dynamic_state
, gallivm
,
3537 context_ptr
, texture_unit
),
3538 lp_build_const_int32(gallivm
, 1), "");
3542 size
= LLVMBuildInsertElement(gallivm
->builder
, size
,
3543 dynamic_state
->depth(dynamic_state
, gallivm
,
3544 context_ptr
, texture_unit
),
3545 lp_build_const_int32(gallivm
, 2), "");
3548 size
= lp_build_minify(&bld_int_vec4
, size
, lod
, TRUE
);
3551 LLVMValueRef layers
= dynamic_state
->depth(dynamic_state
, gallivm
,
3552 context_ptr
, texture_unit
);
3553 if (target
== PIPE_TEXTURE_CUBE_ARRAY
) {
3555 * It looks like GL wants number of cubes, d3d10.1 has it undefined?
3556 * Could avoid this by passing in number of cubes instead of total
3557 * number of layers (might make things easier elsewhere too).
3559 LLVMValueRef six
= lp_build_const_int32(gallivm
, 6);
3560 layers
= LLVMBuildSDiv(gallivm
->builder
, layers
, six
, "");
3562 size
= LLVMBuildInsertElement(gallivm
->builder
, size
, layers
,
3563 lp_build_const_int32(gallivm
, dims
), "");
3567 * d3d10 requires zero for x/y/z values (but not w, i.e. mip levels)
3568 * if level is out of bounds (note this can't cover unbound texture
3569 * here, which also requires returning zero).
3571 if (explicit_lod
&& is_sviewinfo
) {
3572 LLVMValueRef last_level
, out
, out1
;
3573 struct lp_build_context leveli_bld
;
3575 /* everything is scalar for now */
3576 lp_build_context_init(&leveli_bld
, gallivm
, lp_type_int_vec(32, 32));
3577 last_level
= dynamic_state
->last_level(dynamic_state
, gallivm
,
3578 context_ptr
, texture_unit
);
3580 out
= lp_build_cmp(&leveli_bld
, PIPE_FUNC_LESS
, level
, first_level
);
3581 out1
= lp_build_cmp(&leveli_bld
, PIPE_FUNC_GREATER
, level
, last_level
);
3582 out
= lp_build_or(&leveli_bld
, out
, out1
);
3583 if (num_lods
== 1) {
3584 out
= lp_build_broadcast_scalar(&bld_int_vec4
, out
);
3590 size
= lp_build_andnot(&bld_int_vec4
, size
, out
);
3592 for (i
= 0; i
< dims
+ (has_array
? 1 : 0); i
++) {
3593 sizes_out
[i
] = lp_build_extract_broadcast(gallivm
, bld_int_vec4
.type
, int_type
,
3595 lp_build_const_int32(gallivm
, i
));
3598 for (; i
< 4; i
++) {
3599 sizes_out
[i
] = lp_build_const_vec(gallivm
, int_type
, 0.0);
3604 * if there's no explicit_lod (buffers, rects) queries requiring nr of
3605 * mips would be illegal.
3607 if (is_sviewinfo
&& explicit_lod
) {
3608 struct lp_build_context bld_int_scalar
;
3609 LLVMValueRef num_levels
;
3610 lp_build_context_init(&bld_int_scalar
, gallivm
, lp_type_int(32));
3612 if (static_state
->level_zero_only
) {
3613 num_levels
= bld_int_scalar
.one
;
3616 LLVMValueRef last_level
;
3618 last_level
= dynamic_state
->last_level(dynamic_state
, gallivm
,
3619 context_ptr
, texture_unit
);
3620 num_levels
= lp_build_sub(&bld_int_scalar
, last_level
, first_level
);
3621 num_levels
= lp_build_add(&bld_int_scalar
, num_levels
, bld_int_scalar
.one
);
3623 sizes_out
[3] = lp_build_broadcast(gallivm
, lp_build_vec_type(gallivm
, int_type
),