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 "util/u_debug.h"
39 #include "util/u_dump.h"
40 #include "util/u_memory.h"
41 #include "util/u_math.h"
42 #include "util/u_format.h"
43 #include "util/u_cpu_detect.h"
44 #include "lp_bld_debug.h"
45 #include "lp_bld_type.h"
46 #include "lp_bld_const.h"
47 #include "lp_bld_conv.h"
48 #include "lp_bld_arit.h"
49 #include "lp_bld_logic.h"
50 #include "lp_bld_swizzle.h"
51 #include "lp_bld_pack.h"
52 #include "lp_bld_flow.h"
53 #include "lp_bld_gather.h"
54 #include "lp_bld_format.h"
55 #include "lp_bld_sample.h"
59 * Keep all information for sampling code generation in a single place.
61 struct lp_build_sample_context
63 LLVMBuilderRef builder
;
65 const struct lp_sampler_static_state
*static_state
;
67 struct lp_sampler_dynamic_state
*dynamic_state
;
69 const struct util_format_description
*format_desc
;
71 /** regular scalar float type */
72 struct lp_type float_type
;
73 struct lp_build_context float_bld
;
75 /** regular scalar float type */
76 struct lp_type int_type
;
77 struct lp_build_context int_bld
;
79 /** Incoming coordinates type and build context */
80 struct lp_type coord_type
;
81 struct lp_build_context coord_bld
;
83 /** Unsigned integer coordinates */
84 struct lp_type uint_coord_type
;
85 struct lp_build_context uint_coord_bld
;
87 /** Signed integer coordinates */
88 struct lp_type int_coord_type
;
89 struct lp_build_context int_coord_bld
;
91 /** Output texels type and build context */
92 struct lp_type texel_type
;
93 struct lp_build_context texel_bld
;
98 * Does the given texture wrap mode allow sampling the texture border color?
99 * XXX maybe move this into gallium util code.
102 wrap_mode_uses_border_color(unsigned mode
)
105 case PIPE_TEX_WRAP_REPEAT
:
106 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
107 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
108 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
110 case PIPE_TEX_WRAP_CLAMP
:
111 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
112 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
113 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
116 assert(0 && "unexpected wrap mode");
123 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
124 LLVMValueRef data_array
, LLVMValueRef level
)
126 LLVMValueRef indexes
[2], data_ptr
;
127 indexes
[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
129 data_ptr
= LLVMBuildGEP(bld
->builder
, data_array
, indexes
, 2, "");
130 data_ptr
= LLVMBuildLoad(bld
->builder
, data_ptr
, "");
136 lp_build_get_const_mipmap_level(struct lp_build_sample_context
*bld
,
137 LLVMValueRef data_array
, int level
)
139 LLVMValueRef lvl
= LLVMConstInt(LLVMInt32Type(), level
, 0);
140 return lp_build_get_mipmap_level(bld
, data_array
, lvl
);
145 * Dereference stride_array[mipmap_level] array to get a stride.
146 * Return stride as a vector.
149 lp_build_get_level_stride_vec(struct lp_build_sample_context
*bld
,
150 LLVMValueRef stride_array
, LLVMValueRef level
)
152 LLVMValueRef indexes
[2], stride
;
153 indexes
[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
155 stride
= LLVMBuildGEP(bld
->builder
, stride_array
, indexes
, 2, "");
156 stride
= LLVMBuildLoad(bld
->builder
, stride
, "");
157 stride
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, stride
);
162 /** Dereference stride_array[0] array to get a stride (as vector). */
164 lp_build_get_const_level_stride_vec(struct lp_build_sample_context
*bld
,
165 LLVMValueRef stride_array
, int level
)
167 LLVMValueRef lvl
= LLVMConstInt(LLVMInt32Type(), level
, 0);
168 return lp_build_get_level_stride_vec(bld
, stride_array
, lvl
);
173 texture_dims(enum pipe_texture_target tex
)
176 case PIPE_TEXTURE_1D
:
178 case PIPE_TEXTURE_2D
:
179 case PIPE_TEXTURE_CUBE
:
181 case PIPE_TEXTURE_3D
:
184 assert(0 && "bad texture target in texture_dims()");
191 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
194 unsigned char swizzles
[4];
196 swizzles
[0] = bld
->static_state
->swizzle_r
;
197 swizzles
[1] = bld
->static_state
->swizzle_g
;
198 swizzles
[2] = bld
->static_state
->swizzle_b
;
199 swizzles
[3] = bld
->static_state
->swizzle_a
;
201 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
207 * Generate code to fetch a texel from a texture at int coords (x, y, z).
208 * The computation depends on whether the texture is 1D, 2D or 3D.
209 * The result, texel, will be:
210 * texel[0] = red values
211 * texel[1] = green values
212 * texel[2] = blue values
213 * texel[3] = alpha values
216 lp_build_sample_texel_soa(struct lp_build_sample_context
*bld
,
223 LLVMValueRef y_stride
,
224 LLVMValueRef z_stride
,
225 LLVMValueRef data_ptr
,
226 LLVMValueRef texel_out
[4])
228 const int dims
= texture_dims(bld
->static_state
->target
);
229 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
232 LLVMValueRef use_border
= NULL
;
234 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
235 if (wrap_mode_uses_border_color(bld
->static_state
->wrap_s
)) {
237 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
238 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
239 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
242 if (dims
>= 2 && wrap_mode_uses_border_color(bld
->static_state
->wrap_t
)) {
244 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
245 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
247 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
248 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
251 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
255 if (dims
== 3 && wrap_mode_uses_border_color(bld
->static_state
->wrap_r
)) {
257 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
258 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
260 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
261 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
264 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
269 * Describe the coordinates in terms of pixel blocks.
271 * TODO: pixel blocks are power of two. LLVM should convert rem/div to
272 * bit arithmetic. Verify this.
275 if (bld
->format_desc
->block
.width
== 1) {
276 i
= bld
->uint_coord_bld
.zero
;
279 LLVMValueRef block_width
= lp_build_const_int_vec(bld
->uint_coord_bld
.type
, bld
->format_desc
->block
.width
);
280 i
= LLVMBuildURem(bld
->builder
, x
, block_width
, "");
281 x
= LLVMBuildUDiv(bld
->builder
, x
, block_width
, "");
284 if (bld
->format_desc
->block
.height
== 1) {
285 j
= bld
->uint_coord_bld
.zero
;
288 LLVMValueRef block_height
= lp_build_const_int_vec(bld
->uint_coord_bld
.type
, bld
->format_desc
->block
.height
);
289 j
= LLVMBuildURem(bld
->builder
, y
, block_height
, "");
290 y
= LLVMBuildUDiv(bld
->builder
, y
, block_height
, "");
293 /* convert x,y,z coords to linear offset from start of texture, in bytes */
294 offset
= lp_build_sample_offset(&bld
->uint_coord_bld
,
296 x
, y
, z
, y_stride
, z_stride
);
299 /* If we can sample the border color, it means that texcoords may
300 * lie outside the bounds of the texture image. We need to do
301 * something to prevent reading out of bounds and causing a segfault.
303 * Simply AND the texture coords with !use_border. This will cause
304 * coords which are out of bounds to become zero. Zero's guaranteed
305 * to be inside the texture image.
307 offset
= lp_build_andc(&bld
->uint_coord_bld
, offset
, use_border
);
310 lp_build_fetch_rgba_soa(bld
->builder
,
317 apply_sampler_swizzle(bld
, texel_out
);
320 * Note: if we find an app which frequently samples the texture border
321 * we might want to implement a true conditional here to avoid sampling
322 * the texture whenever possible (since that's quite a bit of code).
325 * texel = border_color;
328 * texel = sample_texture(coord);
330 * As it is now, we always sample the texture, then selectively replace
331 * the texel color results with the border color.
335 /* select texel color or border color depending on use_border */
337 for (chan
= 0; chan
< 4; chan
++) {
338 LLVMValueRef border_chan
=
339 lp_build_const_vec(bld
->texel_type
,
340 bld
->static_state
->border_color
[chan
]);
341 texel_out
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
342 border_chan
, texel_out
[chan
]);
349 lp_build_sample_packed(struct lp_build_sample_context
*bld
,
352 LLVMValueRef y_stride
,
353 LLVMValueRef data_array
)
356 LLVMValueRef data_ptr
;
358 offset
= lp_build_sample_offset(&bld
->uint_coord_bld
,
360 x
, y
, NULL
, y_stride
, NULL
);
362 assert(bld
->format_desc
->block
.width
== 1);
363 assert(bld
->format_desc
->block
.height
== 1);
364 assert(bld
->format_desc
->block
.bits
<= bld
->texel_type
.width
);
366 /* get pointer to mipmap level 0 data */
367 data_ptr
= lp_build_get_const_mipmap_level(bld
, data_array
, 0);
369 return lp_build_gather(bld
->builder
,
370 bld
->texel_type
.length
,
371 bld
->format_desc
->block
.bits
,
372 bld
->texel_type
.width
,
378 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
381 lp_build_coord_mirror(struct lp_build_sample_context
*bld
,
384 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
385 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
386 LLVMValueRef fract
, flr
, isOdd
;
388 /* fract = coord - floor(coord) */
389 fract
= lp_build_sub(coord_bld
, coord
, lp_build_floor(coord_bld
, coord
));
391 /* flr = ifloor(coord); */
392 flr
= lp_build_ifloor(coord_bld
, coord
);
394 /* isOdd = flr & 1 */
395 isOdd
= LLVMBuildAnd(bld
->builder
, flr
, int_coord_bld
->one
, "");
397 /* make coord positive or negative depending on isOdd */
398 coord
= lp_build_set_sign(coord_bld
, fract
, isOdd
);
400 /* convert isOdd to float */
401 isOdd
= lp_build_int_to_float(coord_bld
, isOdd
);
403 /* add isOdd to coord */
404 coord
= lp_build_add(coord_bld
, coord
, isOdd
);
411 * We only support a few wrap modes in lp_build_sample_wrap_int() at this time.
412 * Return whether the given mode is supported by that function.
415 is_simple_wrap_mode(unsigned mode
)
418 case PIPE_TEX_WRAP_REPEAT
:
419 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
428 * Build LLVM code for texture wrap mode, for scaled integer texcoords.
429 * \param coord the incoming texcoord (s,t,r or q) scaled to the texture size
430 * \param length the texture size along one dimension
431 * \param is_pot if TRUE, length is a power of two
432 * \param wrap_mode one of PIPE_TEX_WRAP_x
435 lp_build_sample_wrap_int(struct lp_build_sample_context
*bld
,
441 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
442 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
443 LLVMValueRef length_minus_one
;
445 length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
448 case PIPE_TEX_WRAP_REPEAT
:
450 coord
= LLVMBuildAnd(bld
->builder
, coord
, length_minus_one
, "");
452 /* Signed remainder won't give the right results for negative
453 * dividends but unsigned remainder does.*/
454 coord
= LLVMBuildURem(bld
->builder
, coord
, length
, "");
457 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
458 coord
= lp_build_max(int_coord_bld
, coord
, int_coord_bld
->zero
);
459 coord
= lp_build_min(int_coord_bld
, coord
, length_minus_one
);
462 case PIPE_TEX_WRAP_CLAMP
:
463 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
464 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
465 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
466 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
467 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
477 * Build LLVM code for texture wrap mode for linear filtering.
478 * \param x0_out returns first integer texcoord
479 * \param x1_out returns second integer texcoord
480 * \param weight_out returns linear interpolation weight
483 lp_build_sample_wrap_linear(struct lp_build_sample_context
*bld
,
488 LLVMValueRef
*x0_out
,
489 LLVMValueRef
*x1_out
,
490 LLVMValueRef
*weight_out
)
492 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
493 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
494 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
495 LLVMValueRef half
= lp_build_const_vec(coord_bld
->type
, 0.5);
496 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
497 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
498 LLVMValueRef coord0
, coord1
, weight
;
501 case PIPE_TEX_WRAP_REPEAT
:
502 /* mul by size and subtract 0.5 */
503 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
504 coord
= lp_build_sub(coord_bld
, coord
, half
);
506 coord0
= lp_build_ifloor(coord_bld
, coord
);
507 coord1
= lp_build_add(uint_coord_bld
, coord0
, uint_coord_bld
->one
);
508 /* compute lerp weight */
509 weight
= lp_build_fract(coord_bld
, coord
);
512 coord0
= LLVMBuildAnd(bld
->builder
, coord0
, length_minus_one
, "");
513 coord1
= LLVMBuildAnd(bld
->builder
, coord1
, length_minus_one
, "");
516 /* Signed remainder won't give the right results for negative
517 * dividends but unsigned remainder does.*/
518 coord0
= LLVMBuildURem(bld
->builder
, coord0
, length
, "");
519 coord1
= LLVMBuildURem(bld
->builder
, coord1
, length
, "");
523 case PIPE_TEX_WRAP_CLAMP
:
524 if (bld
->static_state
->normalized_coords
) {
525 /* scale coord to length */
526 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
529 /* clamp to [0, length] */
530 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, length_f
);
532 coord
= lp_build_sub(coord_bld
, coord
, half
);
534 weight
= lp_build_fract(coord_bld
, coord
);
535 coord0
= lp_build_ifloor(coord_bld
, coord
);
536 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
539 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
540 if (bld
->static_state
->normalized_coords
) {
542 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, coord_bld
->one
);
543 /* mul by tex size and subtract 0.5 */
544 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
545 coord
= lp_build_sub(coord_bld
, coord
, half
);
548 LLVMValueRef min
, max
;
549 /* clamp to [0.5, length - 0.5] */
551 max
= lp_build_sub(coord_bld
, length_f
, min
);
552 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
554 /* compute lerp weight */
555 weight
= lp_build_fract(coord_bld
, coord
);
556 /* coord0 = floor(coord); */
557 coord0
= lp_build_ifloor(coord_bld
, coord
);
558 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
559 /* coord0 = max(coord0, 0) */
560 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
561 /* coord1 = min(coord1, length-1) */
562 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
565 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
567 LLVMValueRef min
, max
;
568 if (bld
->static_state
->normalized_coords
) {
569 /* scale coord to length */
570 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
572 /* clamp to [-0.5, length + 0.5] */
573 min
= lp_build_const_vec(coord_bld
->type
, -0.5F
);
574 max
= lp_build_sub(coord_bld
, length_f
, min
);
575 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
576 coord
= lp_build_sub(coord_bld
, coord
, half
);
577 /* compute lerp weight */
578 weight
= lp_build_fract(coord_bld
, coord
);
580 coord0
= lp_build_ifloor(coord_bld
, coord
);
581 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
585 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
586 /* compute mirror function */
587 coord
= lp_build_coord_mirror(bld
, coord
);
589 /* scale coord to length */
590 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
591 coord
= lp_build_sub(coord_bld
, coord
, half
);
593 /* compute lerp weight */
594 weight
= lp_build_fract(coord_bld
, coord
);
596 /* convert to int coords */
597 coord0
= lp_build_ifloor(coord_bld
, coord
);
598 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
600 /* coord0 = max(coord0, 0) */
601 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
602 /* coord1 = min(coord1, length-1) */
603 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
606 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
607 coord
= lp_build_abs(coord_bld
, coord
);
609 if (bld
->static_state
->normalized_coords
) {
610 /* scale coord to length */
611 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
614 /* clamp to [0, length] */
615 coord
= lp_build_min(coord_bld
, coord
, length_f
);
617 coord
= lp_build_sub(coord_bld
, coord
, half
);
619 weight
= lp_build_fract(coord_bld
, coord
);
620 coord0
= lp_build_ifloor(coord_bld
, coord
);
621 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
624 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
626 LLVMValueRef min
, max
;
628 coord
= lp_build_abs(coord_bld
, coord
);
630 if (bld
->static_state
->normalized_coords
) {
631 /* scale coord to length */
632 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
635 /* clamp to [0.5, length - 0.5] */
637 max
= lp_build_sub(coord_bld
, length_f
, min
);
638 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
640 coord
= lp_build_sub(coord_bld
, coord
, half
);
642 weight
= lp_build_fract(coord_bld
, coord
);
643 coord0
= lp_build_ifloor(coord_bld
, coord
);
644 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
648 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
650 LLVMValueRef min
, max
;
652 coord
= lp_build_abs(coord_bld
, coord
);
654 if (bld
->static_state
->normalized_coords
) {
655 /* scale coord to length */
656 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
659 /* clamp to [-0.5, length + 0.5] */
660 min
= lp_build_negate(coord_bld
, half
);
661 max
= lp_build_sub(coord_bld
, length_f
, min
);
662 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
664 coord
= lp_build_sub(coord_bld
, coord
, half
);
666 weight
= lp_build_fract(coord_bld
, coord
);
667 coord0
= lp_build_ifloor(coord_bld
, coord
);
668 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
681 *weight_out
= weight
;
686 * Build LLVM code for texture wrap mode for nearest filtering.
687 * \param coord the incoming texcoord (nominally in [0,1])
688 * \param length the texture size along one dimension, as int
689 * \param is_pot if TRUE, length is a power of two
690 * \param wrap_mode one of PIPE_TEX_WRAP_x
693 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
699 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
700 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
701 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
702 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
703 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
707 case PIPE_TEX_WRAP_REPEAT
:
708 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
709 icoord
= lp_build_ifloor(coord_bld
, coord
);
711 icoord
= LLVMBuildAnd(bld
->builder
, icoord
, length_minus_one
, "");
713 /* Signed remainder won't give the right results for negative
714 * dividends but unsigned remainder does.*/
715 icoord
= LLVMBuildURem(bld
->builder
, icoord
, length
, "");
718 case PIPE_TEX_WRAP_CLAMP
:
719 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
720 if (bld
->static_state
->normalized_coords
) {
721 /* scale coord to length */
722 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
726 icoord
= lp_build_ifloor(coord_bld
, coord
);
728 /* clamp to [0, length - 1]. */
729 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
733 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
734 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
736 LLVMValueRef min
, max
;
738 if (bld
->static_state
->normalized_coords
) {
739 /* scale coord to length */
740 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
743 icoord
= lp_build_ifloor(coord_bld
, coord
);
745 /* clamp to [-1, length] */
746 min
= lp_build_negate(int_coord_bld
, int_coord_bld
->one
);
748 icoord
= lp_build_clamp(int_coord_bld
, icoord
, min
, max
);
752 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
753 /* compute mirror function */
754 coord
= lp_build_coord_mirror(bld
, coord
);
756 /* scale coord to length */
757 assert(bld
->static_state
->normalized_coords
);
758 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
760 icoord
= lp_build_ifloor(coord_bld
, coord
);
762 /* clamp to [0, length - 1] */
763 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
766 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
767 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
768 coord
= lp_build_abs(coord_bld
, coord
);
770 if (bld
->static_state
->normalized_coords
) {
771 /* scale coord to length */
772 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
775 icoord
= lp_build_ifloor(coord_bld
, coord
);
777 /* clamp to [0, length - 1] */
778 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
781 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
782 coord
= lp_build_abs(coord_bld
, coord
);
784 if (bld
->static_state
->normalized_coords
) {
785 /* scale coord to length */
786 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
789 icoord
= lp_build_ifloor(coord_bld
, coord
);
791 /* clamp to [0, length] */
792 icoord
= lp_build_min(int_coord_bld
, icoord
, length
);
805 * Codegen equivalent for u_minify().
806 * Return max(1, base_size >> level);
809 lp_build_minify(struct lp_build_sample_context
*bld
,
810 LLVMValueRef base_size
,
813 LLVMValueRef size
= LLVMBuildAShr(bld
->builder
, base_size
, level
, "minify");
814 size
= lp_build_max(&bld
->int_coord_bld
, size
, bld
->int_coord_bld
.one
);
820 * Generate code to compute texture level of detail (lambda).
821 * \param s vector of texcoord s values
822 * \param t vector of texcoord t values
823 * \param r vector of texcoord r values
824 * \param lod_bias optional float vector with the shader lod bias
825 * \param explicit_lod optional float vector with the explicit lod
826 * \param width scalar int texture width
827 * \param height scalar int texture height
828 * \param depth scalar int texture depth
830 * XXX: The resulting lod is scalar, so ignore all but the first element of
831 * derivatives, lod_bias, etc that are passed by the shader.
834 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
838 const LLVMValueRef
*ddx
,
839 const LLVMValueRef
*ddy
,
840 LLVMValueRef lod_bias
, /* optional */
841 LLVMValueRef explicit_lod
, /* optional */
847 if (bld
->static_state
->min_lod
== bld
->static_state
->max_lod
) {
848 /* User is forcing sampling from a particular mipmap level.
849 * This is hit during mipmap generation.
851 return LLVMConstReal(LLVMFloatType(), bld
->static_state
->min_lod
);
854 struct lp_build_context
*float_bld
= &bld
->float_bld
;
855 LLVMValueRef sampler_lod_bias
= LLVMConstReal(LLVMFloatType(),
856 bld
->static_state
->lod_bias
);
857 LLVMValueRef min_lod
= LLVMConstReal(LLVMFloatType(),
858 bld
->static_state
->min_lod
);
859 LLVMValueRef max_lod
= LLVMConstReal(LLVMFloatType(),
860 bld
->static_state
->max_lod
);
861 LLVMValueRef index0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
865 lod
= LLVMBuildExtractElement(bld
->builder
, explicit_lod
,
869 const int dims
= texture_dims(bld
->static_state
->target
);
870 LLVMValueRef dsdx
, dsdy
;
871 LLVMValueRef dtdx
= NULL
, dtdy
= NULL
, drdx
= NULL
, drdy
= NULL
;
875 * dsdx = abs(s[1] - s[0]);
876 * dsdy = abs(s[2] - s[0]);
877 * dtdx = abs(t[1] - t[0]);
878 * dtdy = abs(t[2] - t[0]);
879 * drdx = abs(r[1] - r[0]);
880 * drdy = abs(r[2] - r[0]);
882 dsdx
= LLVMBuildExtractElement(bld
->builder
, ddx
[0], index0
, "dsdx");
883 dsdx
= lp_build_abs(float_bld
, dsdx
);
884 dsdy
= LLVMBuildExtractElement(bld
->builder
, ddy
[0], index0
, "dsdy");
885 dsdy
= lp_build_abs(float_bld
, dsdy
);
887 dtdx
= LLVMBuildExtractElement(bld
->builder
, ddx
[1], index0
, "dtdx");
888 dtdx
= lp_build_abs(float_bld
, dtdx
);
889 dtdy
= LLVMBuildExtractElement(bld
->builder
, ddy
[1], index0
, "dtdy");
890 dtdy
= lp_build_abs(float_bld
, dtdy
);
892 drdx
= LLVMBuildExtractElement(bld
->builder
, ddx
[2], index0
, "drdx");
893 drdx
= lp_build_abs(float_bld
, drdx
);
894 drdy
= LLVMBuildExtractElement(bld
->builder
, ddy
[2], index0
, "drdy");
895 drdy
= lp_build_abs(float_bld
, drdy
);
899 /* Compute rho = max of all partial derivatives scaled by texture size.
900 * XXX this could be vectorized somewhat
902 rho
= LLVMBuildMul(bld
->builder
,
903 lp_build_max(float_bld
, dsdx
, dsdy
),
904 lp_build_int_to_float(float_bld
, width
), "");
907 max
= LLVMBuildMul(bld
->builder
,
908 lp_build_max(float_bld
, dtdx
, dtdy
),
909 lp_build_int_to_float(float_bld
, height
), "");
910 rho
= lp_build_max(float_bld
, rho
, max
);
912 max
= LLVMBuildMul(bld
->builder
,
913 lp_build_max(float_bld
, drdx
, drdy
),
914 lp_build_int_to_float(float_bld
, depth
), "");
915 rho
= lp_build_max(float_bld
, rho
, max
);
919 /* compute lod = log2(rho) */
920 lod
= lp_build_log2(float_bld
, rho
);
922 /* add shader lod bias */
924 lod_bias
= LLVMBuildExtractElement(bld
->builder
, lod_bias
,
926 lod
= LLVMBuildAdd(bld
->builder
, lod
, lod_bias
, "shader_lod_bias");
930 /* add sampler lod bias */
931 lod
= LLVMBuildAdd(bld
->builder
, lod
, sampler_lod_bias
, "sampler_lod_bias");
934 lod
= lp_build_clamp(float_bld
, lod
, min_lod
, max_lod
);
942 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
943 * mipmap level index.
944 * Note: this is all scalar code.
945 * \param lod scalar float texture level of detail
946 * \param level_out returns integer
949 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
952 LLVMValueRef
*level_out
)
954 struct lp_build_context
*float_bld
= &bld
->float_bld
;
955 struct lp_build_context
*int_bld
= &bld
->int_bld
;
956 LLVMValueRef last_level
, level
;
958 LLVMValueRef zero
= LLVMConstInt(LLVMInt32Type(), 0, 0);
960 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
963 /* convert float lod to integer */
964 level
= lp_build_iround(float_bld
, lod
);
966 /* clamp level to legal range of levels */
967 *level_out
= lp_build_clamp(int_bld
, level
, zero
, last_level
);
972 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
973 * two (adjacent) mipmap level indexes. Later, we'll sample from those
974 * two mipmap levels and interpolate between them.
977 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
980 LLVMValueRef
*level0_out
,
981 LLVMValueRef
*level1_out
,
982 LLVMValueRef
*weight_out
)
984 struct lp_build_context
*float_bld
= &bld
->float_bld
;
985 struct lp_build_context
*int_bld
= &bld
->int_bld
;
986 LLVMValueRef last_level
, level
;
988 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
991 /* convert float lod to integer */
992 level
= lp_build_ifloor(float_bld
, lod
);
994 /* compute level 0 and clamp to legal range of levels */
995 *level0_out
= lp_build_clamp(int_bld
, level
,
998 /* compute level 1 and clamp to legal range of levels */
999 level
= lp_build_add(int_bld
, level
, int_bld
->one
);
1000 *level1_out
= lp_build_clamp(int_bld
, level
,
1004 *weight_out
= lp_build_fract(float_bld
, lod
);
1009 * Generate code to sample a mipmap level with nearest filtering.
1010 * If sampling a cube texture, r = cube face in [0,5].
1013 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
1014 LLVMValueRef width_vec
,
1015 LLVMValueRef height_vec
,
1016 LLVMValueRef depth_vec
,
1017 LLVMValueRef row_stride_vec
,
1018 LLVMValueRef img_stride_vec
,
1019 LLVMValueRef data_ptr
,
1023 LLVMValueRef colors_out
[4])
1025 const int dims
= texture_dims(bld
->static_state
->target
);
1026 LLVMValueRef x
, y
, z
;
1029 * Compute integer texcoords.
1031 x
= lp_build_sample_wrap_nearest(bld
, s
, width_vec
,
1032 bld
->static_state
->pot_width
,
1033 bld
->static_state
->wrap_s
);
1034 lp_build_name(x
, "tex.x.wrapped");
1037 y
= lp_build_sample_wrap_nearest(bld
, t
, height_vec
,
1038 bld
->static_state
->pot_height
,
1039 bld
->static_state
->wrap_t
);
1040 lp_build_name(y
, "tex.y.wrapped");
1043 z
= lp_build_sample_wrap_nearest(bld
, r
, depth_vec
,
1044 bld
->static_state
->pot_height
,
1045 bld
->static_state
->wrap_r
);
1046 lp_build_name(z
, "tex.z.wrapped");
1048 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1060 * Get texture colors.
1062 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1064 row_stride_vec
, img_stride_vec
,
1065 data_ptr
, colors_out
);
1070 * Generate code to sample a mipmap level with linear filtering.
1071 * If sampling a cube texture, r = cube face in [0,5].
1074 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
1075 LLVMValueRef width_vec
,
1076 LLVMValueRef height_vec
,
1077 LLVMValueRef depth_vec
,
1078 LLVMValueRef row_stride_vec
,
1079 LLVMValueRef img_stride_vec
,
1080 LLVMValueRef data_ptr
,
1084 LLVMValueRef colors_out
[4])
1086 const int dims
= texture_dims(bld
->static_state
->target
);
1087 LLVMValueRef x0
, y0
, z0
, x1
, y1
, z1
;
1088 LLVMValueRef s_fpart
, t_fpart
, r_fpart
;
1089 LLVMValueRef neighbors
[2][2][4];
1093 * Compute integer texcoords.
1095 lp_build_sample_wrap_linear(bld
, s
, width_vec
,
1096 bld
->static_state
->pot_width
,
1097 bld
->static_state
->wrap_s
,
1098 &x0
, &x1
, &s_fpart
);
1099 lp_build_name(x0
, "tex.x0.wrapped");
1100 lp_build_name(x1
, "tex.x1.wrapped");
1103 lp_build_sample_wrap_linear(bld
, t
, height_vec
,
1104 bld
->static_state
->pot_height
,
1105 bld
->static_state
->wrap_t
,
1106 &y0
, &y1
, &t_fpart
);
1107 lp_build_name(y0
, "tex.y0.wrapped");
1108 lp_build_name(y1
, "tex.y1.wrapped");
1111 lp_build_sample_wrap_linear(bld
, r
, depth_vec
,
1112 bld
->static_state
->pot_depth
,
1113 bld
->static_state
->wrap_r
,
1114 &z0
, &z1
, &r_fpart
);
1115 lp_build_name(z0
, "tex.z0.wrapped");
1116 lp_build_name(z1
, "tex.z1.wrapped");
1118 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1119 z0
= z1
= r
; /* cube face */
1128 y0
= y1
= t_fpart
= NULL
;
1129 z0
= z1
= r_fpart
= NULL
;
1133 * Get texture colors.
1135 /* get x0/x1 texels */
1136 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1138 row_stride_vec
, img_stride_vec
,
1139 data_ptr
, neighbors
[0][0]);
1140 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1142 row_stride_vec
, img_stride_vec
,
1143 data_ptr
, neighbors
[0][1]);
1146 /* Interpolate two samples from 1D image to produce one color */
1147 for (chan
= 0; chan
< 4; chan
++) {
1148 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, s_fpart
,
1149 neighbors
[0][0][chan
],
1150 neighbors
[0][1][chan
]);
1155 LLVMValueRef colors0
[4];
1157 /* get x0/x1 texels at y1 */
1158 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1160 row_stride_vec
, img_stride_vec
,
1161 data_ptr
, neighbors
[1][0]);
1162 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1164 row_stride_vec
, img_stride_vec
,
1165 data_ptr
, neighbors
[1][1]);
1167 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
1168 for (chan
= 0; chan
< 4; chan
++) {
1169 colors0
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1171 neighbors
[0][0][chan
],
1172 neighbors
[0][1][chan
],
1173 neighbors
[1][0][chan
],
1174 neighbors
[1][1][chan
]);
1178 LLVMValueRef neighbors1
[2][2][4];
1179 LLVMValueRef colors1
[4];
1181 /* get x0/x1/y0/y1 texels at z1 */
1182 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1184 row_stride_vec
, img_stride_vec
,
1185 data_ptr
, neighbors1
[0][0]);
1186 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1188 row_stride_vec
, img_stride_vec
,
1189 data_ptr
, neighbors1
[0][1]);
1190 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1192 row_stride_vec
, img_stride_vec
,
1193 data_ptr
, neighbors1
[1][0]);
1194 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1196 row_stride_vec
, img_stride_vec
,
1197 data_ptr
, neighbors1
[1][1]);
1199 /* Bilinear interpolate the four samples from the second Z slice */
1200 for (chan
= 0; chan
< 4; chan
++) {
1201 colors1
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1203 neighbors1
[0][0][chan
],
1204 neighbors1
[0][1][chan
],
1205 neighbors1
[1][0][chan
],
1206 neighbors1
[1][1][chan
]);
1209 /* Linearly interpolate the two samples from the two 3D slices */
1210 for (chan
= 0; chan
< 4; chan
++) {
1211 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
,
1213 colors0
[chan
], colors1
[chan
]);
1218 for (chan
= 0; chan
< 4; chan
++) {
1219 colors_out
[chan
] = colors0
[chan
];
1226 /** Helper used by lp_build_cube_lookup() */
1228 lp_build_cube_ima(struct lp_build_context
*coord_bld
, LLVMValueRef coord
)
1230 /* ima = -0.5 / abs(coord); */
1231 LLVMValueRef negHalf
= lp_build_const_vec(coord_bld
->type
, -0.5);
1232 LLVMValueRef absCoord
= lp_build_abs(coord_bld
, coord
);
1233 LLVMValueRef ima
= lp_build_mul(coord_bld
, negHalf
,
1234 lp_build_rcp(coord_bld
, absCoord
));
1240 * Helper used by lp_build_cube_lookup()
1241 * \param sign scalar +1 or -1
1242 * \param coord float vector
1243 * \param ima float vector
1246 lp_build_cube_coord(struct lp_build_context
*coord_bld
,
1247 LLVMValueRef sign
, int negate_coord
,
1248 LLVMValueRef coord
, LLVMValueRef ima
)
1250 /* return negate(coord) * ima * sign + 0.5; */
1251 LLVMValueRef half
= lp_build_const_vec(coord_bld
->type
, 0.5);
1254 assert(negate_coord
== +1 || negate_coord
== -1);
1256 if (negate_coord
== -1) {
1257 coord
= lp_build_negate(coord_bld
, coord
);
1260 res
= lp_build_mul(coord_bld
, coord
, ima
);
1262 sign
= lp_build_broadcast_scalar(coord_bld
, sign
);
1263 res
= lp_build_mul(coord_bld
, res
, sign
);
1265 res
= lp_build_add(coord_bld
, res
, half
);
1271 /** Helper used by lp_build_cube_lookup()
1272 * Return (major_coord >= 0) ? pos_face : neg_face;
1275 lp_build_cube_face(struct lp_build_sample_context
*bld
,
1276 LLVMValueRef major_coord
,
1277 unsigned pos_face
, unsigned neg_face
)
1279 LLVMValueRef cmp
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
1281 bld
->float_bld
.zero
, "");
1282 LLVMValueRef pos
= LLVMConstInt(LLVMInt32Type(), pos_face
, 0);
1283 LLVMValueRef neg
= LLVMConstInt(LLVMInt32Type(), neg_face
, 0);
1284 LLVMValueRef res
= LLVMBuildSelect(bld
->builder
, cmp
, pos
, neg
, "");
1291 * Generate code to do cube face selection and per-face texcoords.
1294 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
1299 LLVMValueRef
*face_s
,
1300 LLVMValueRef
*face_t
)
1302 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1303 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
1304 LLVMValueRef rx
, ry
, rz
;
1305 LLVMValueRef arx
, ary
, arz
;
1306 LLVMValueRef c25
= LLVMConstReal(LLVMFloatType(), 0.25);
1307 LLVMValueRef arx_ge_ary
, arx_ge_arz
;
1308 LLVMValueRef ary_ge_arx
, ary_ge_arz
;
1309 LLVMValueRef arx_ge_ary_arz
, ary_ge_arx_arz
;
1310 LLVMValueRef rx_pos
, ry_pos
, rz_pos
;
1312 assert(bld
->coord_bld
.type
.length
== 4);
1315 * Use the average of the four pixel's texcoords to choose the face.
1317 rx
= lp_build_mul(float_bld
, c25
,
1318 lp_build_sum_vector(&bld
->coord_bld
, s
));
1319 ry
= lp_build_mul(float_bld
, c25
,
1320 lp_build_sum_vector(&bld
->coord_bld
, t
));
1321 rz
= lp_build_mul(float_bld
, c25
,
1322 lp_build_sum_vector(&bld
->coord_bld
, r
));
1324 arx
= lp_build_abs(float_bld
, rx
);
1325 ary
= lp_build_abs(float_bld
, ry
);
1326 arz
= lp_build_abs(float_bld
, rz
);
1329 * Compare sign/magnitude of rx,ry,rz to determine face
1331 arx_ge_ary
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, arx
, ary
, "");
1332 arx_ge_arz
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, arx
, arz
, "");
1333 ary_ge_arx
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ary
, arx
, "");
1334 ary_ge_arz
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ary
, arz
, "");
1336 arx_ge_ary_arz
= LLVMBuildAnd(bld
->builder
, arx_ge_ary
, arx_ge_arz
, "");
1337 ary_ge_arx_arz
= LLVMBuildAnd(bld
->builder
, ary_ge_arx
, ary_ge_arz
, "");
1339 rx_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, rx
, float_bld
->zero
, "");
1340 ry_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ry
, float_bld
->zero
, "");
1341 rz_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, rz
, float_bld
->zero
, "");
1344 struct lp_build_flow_context
*flow_ctx
;
1345 struct lp_build_if_state if_ctx
;
1347 flow_ctx
= lp_build_flow_create(bld
->builder
);
1348 lp_build_flow_scope_begin(flow_ctx
);
1350 *face_s
= bld
->coord_bld
.undef
;
1351 *face_t
= bld
->coord_bld
.undef
;
1352 *face
= bld
->int_bld
.undef
;
1354 lp_build_name(*face_s
, "face_s");
1355 lp_build_name(*face_t
, "face_t");
1356 lp_build_name(*face
, "face");
1358 lp_build_flow_scope_declare(flow_ctx
, face_s
);
1359 lp_build_flow_scope_declare(flow_ctx
, face_t
);
1360 lp_build_flow_scope_declare(flow_ctx
, face
);
1362 lp_build_if(&if_ctx
, flow_ctx
, bld
->builder
, arx_ge_ary_arz
);
1365 LLVMValueRef sign
= lp_build_sgn(float_bld
, rx
);
1366 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, s
);
1367 *face_s
= lp_build_cube_coord(coord_bld
, sign
, +1, r
, ima
);
1368 *face_t
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1369 *face
= lp_build_cube_face(bld
, rx
,
1370 PIPE_TEX_FACE_POS_X
,
1371 PIPE_TEX_FACE_NEG_X
);
1373 lp_build_else(&if_ctx
);
1375 struct lp_build_flow_context
*flow_ctx2
;
1376 struct lp_build_if_state if_ctx2
;
1378 LLVMValueRef face_s2
= bld
->coord_bld
.undef
;
1379 LLVMValueRef face_t2
= bld
->coord_bld
.undef
;
1380 LLVMValueRef face2
= bld
->int_bld
.undef
;
1382 flow_ctx2
= lp_build_flow_create(bld
->builder
);
1383 lp_build_flow_scope_begin(flow_ctx2
);
1384 lp_build_flow_scope_declare(flow_ctx2
, &face_s2
);
1385 lp_build_flow_scope_declare(flow_ctx2
, &face_t2
);
1386 lp_build_flow_scope_declare(flow_ctx2
, &face2
);
1388 ary_ge_arx_arz
= LLVMBuildAnd(bld
->builder
, ary_ge_arx
, ary_ge_arz
, "");
1390 lp_build_if(&if_ctx2
, flow_ctx2
, bld
->builder
, ary_ge_arx_arz
);
1393 LLVMValueRef sign
= lp_build_sgn(float_bld
, ry
);
1394 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, t
);
1395 face_s2
= lp_build_cube_coord(coord_bld
, NULL
, -1, s
, ima
);
1396 face_t2
= lp_build_cube_coord(coord_bld
, sign
, -1, r
, ima
);
1397 face2
= lp_build_cube_face(bld
, ry
,
1398 PIPE_TEX_FACE_POS_Y
,
1399 PIPE_TEX_FACE_NEG_Y
);
1401 lp_build_else(&if_ctx2
);
1404 LLVMValueRef sign
= lp_build_sgn(float_bld
, rz
);
1405 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, r
);
1406 face_s2
= lp_build_cube_coord(coord_bld
, sign
, -1, s
, ima
);
1407 face_t2
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1408 face2
= lp_build_cube_face(bld
, rz
,
1409 PIPE_TEX_FACE_POS_Z
,
1410 PIPE_TEX_FACE_NEG_Z
);
1412 lp_build_endif(&if_ctx2
);
1413 lp_build_flow_scope_end(flow_ctx2
);
1414 lp_build_flow_destroy(flow_ctx2
);
1421 lp_build_endif(&if_ctx
);
1422 lp_build_flow_scope_end(flow_ctx
);
1423 lp_build_flow_destroy(flow_ctx
);
1430 * Sample the texture/mipmap using given image filter and mip filter.
1431 * data0_ptr and data1_ptr point to the two mipmap levels to sample
1432 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
1433 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1436 lp_build_sample_mipmap(struct lp_build_sample_context
*bld
,
1437 unsigned img_filter
,
1438 unsigned mip_filter
,
1442 LLVMValueRef lod_fpart
,
1443 LLVMValueRef width0_vec
,
1444 LLVMValueRef width1_vec
,
1445 LLVMValueRef height0_vec
,
1446 LLVMValueRef height1_vec
,
1447 LLVMValueRef depth0_vec
,
1448 LLVMValueRef depth1_vec
,
1449 LLVMValueRef row_stride0_vec
,
1450 LLVMValueRef row_stride1_vec
,
1451 LLVMValueRef img_stride0_vec
,
1452 LLVMValueRef img_stride1_vec
,
1453 LLVMValueRef data_ptr0
,
1454 LLVMValueRef data_ptr1
,
1455 LLVMValueRef
*colors_out
)
1457 LLVMValueRef colors0
[4], colors1
[4];
1460 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
1461 lp_build_sample_image_nearest(bld
,
1462 width0_vec
, height0_vec
, depth0_vec
,
1463 row_stride0_vec
, img_stride0_vec
,
1464 data_ptr0
, s
, t
, r
, colors0
);
1466 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1467 /* sample the second mipmap level, and interp */
1468 lp_build_sample_image_nearest(bld
,
1469 width1_vec
, height1_vec
, depth1_vec
,
1470 row_stride1_vec
, img_stride1_vec
,
1471 data_ptr1
, s
, t
, r
, colors1
);
1475 assert(img_filter
== PIPE_TEX_FILTER_LINEAR
);
1477 lp_build_sample_image_linear(bld
,
1478 width0_vec
, height0_vec
, depth0_vec
,
1479 row_stride0_vec
, img_stride0_vec
,
1480 data_ptr0
, s
, t
, r
, colors0
);
1482 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1483 /* sample the second mipmap level, and interp */
1484 lp_build_sample_image_linear(bld
,
1485 width1_vec
, height1_vec
, depth1_vec
,
1486 row_stride1_vec
, img_stride1_vec
,
1487 data_ptr1
, s
, t
, r
, colors1
);
1491 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1492 /* interpolate samples from the two mipmap levels */
1493 for (chan
= 0; chan
< 4; chan
++) {
1494 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
1495 colors0
[chan
], colors1
[chan
]);
1499 /* use first/only level's colors */
1500 for (chan
= 0; chan
< 4; chan
++) {
1501 colors_out
[chan
] = colors0
[chan
];
1509 * General texture sampling codegen.
1510 * This function handles texture sampling for all texture targets (1D,
1511 * 2D, 3D, cube) and all filtering modes.
1514 lp_build_sample_general(struct lp_build_sample_context
*bld
,
1519 const LLVMValueRef
*ddx
,
1520 const LLVMValueRef
*ddy
,
1521 LLVMValueRef lod_bias
, /* optional */
1522 LLVMValueRef explicit_lod
, /* optional */
1524 LLVMValueRef height
,
1526 LLVMValueRef width_vec
,
1527 LLVMValueRef height_vec
,
1528 LLVMValueRef depth_vec
,
1529 LLVMValueRef row_stride_array
,
1530 LLVMValueRef img_stride_array
,
1531 LLVMValueRef data_array
,
1532 LLVMValueRef
*colors_out
)
1534 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1535 const unsigned mip_filter
= bld
->static_state
->min_mip_filter
;
1536 const unsigned min_filter
= bld
->static_state
->min_img_filter
;
1537 const unsigned mag_filter
= bld
->static_state
->mag_img_filter
;
1538 const int dims
= texture_dims(bld
->static_state
->target
);
1539 LLVMValueRef lod
= NULL
, lod_fpart
= NULL
;
1540 LLVMValueRef ilevel0
, ilevel1
= NULL
, ilevel0_vec
, ilevel1_vec
= NULL
;
1541 LLVMValueRef width0_vec
= NULL
, height0_vec
= NULL
, depth0_vec
= NULL
;
1542 LLVMValueRef width1_vec
= NULL
, height1_vec
= NULL
, depth1_vec
= NULL
;
1543 LLVMValueRef row_stride0_vec
= NULL
, row_stride1_vec
= NULL
;
1544 LLVMValueRef img_stride0_vec
= NULL
, img_stride1_vec
= NULL
;
1545 LLVMValueRef data_ptr0
, data_ptr1
= NULL
;
1548 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1549 mip_filter, min_filter, mag_filter);
1553 * Compute the level of detail (float).
1555 if (min_filter
!= mag_filter
||
1556 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
1557 /* Need to compute lod either to choose mipmap levels or to
1558 * distinguish between minification/magnification with one mipmap level.
1560 lod
= lp_build_lod_selector(bld
, s
, t
, r
, ddx
, ddy
,
1561 lod_bias
, explicit_lod
,
1562 width
, height
, depth
);
1566 * Compute integer mipmap level(s) to fetch texels from.
1568 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
1569 /* always use mip level 0 */
1570 ilevel0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
1573 if (mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
1574 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
1577 assert(mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
);
1578 lp_build_linear_mip_levels(bld
, unit
, lod
, &ilevel0
, &ilevel1
,
1580 lod_fpart
= lp_build_broadcast_scalar(&bld
->coord_bld
, lod_fpart
);
1585 * Convert scalar integer mipmap levels into vectors.
1587 ilevel0_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel0
);
1588 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
)
1589 ilevel1_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel1
);
1592 * Compute width, height at mipmap level 'ilevel0'
1594 width0_vec
= lp_build_minify(bld
, width_vec
, ilevel0_vec
);
1596 height0_vec
= lp_build_minify(bld
, height_vec
, ilevel0_vec
);
1597 row_stride0_vec
= lp_build_get_level_stride_vec(bld
, row_stride_array
,
1599 if (dims
== 3 || bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1600 img_stride0_vec
= lp_build_get_level_stride_vec(bld
,
1604 depth0_vec
= lp_build_minify(bld
, depth_vec
, ilevel0_vec
);
1608 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1609 /* compute width, height, depth for second mipmap level at 'ilevel1' */
1610 width1_vec
= lp_build_minify(bld
, width_vec
, ilevel1_vec
);
1612 height1_vec
= lp_build_minify(bld
, height_vec
, ilevel1_vec
);
1613 row_stride1_vec
= lp_build_get_level_stride_vec(bld
, row_stride_array
,
1615 if (dims
== 3 || bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1616 img_stride1_vec
= lp_build_get_level_stride_vec(bld
,
1620 depth1_vec
= lp_build_minify(bld
, depth_vec
, ilevel1_vec
);
1627 * Choose cube face, recompute per-face texcoords.
1629 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1630 LLVMValueRef face
, face_s
, face_t
;
1631 lp_build_cube_lookup(bld
, s
, t
, r
, &face
, &face_s
, &face_t
);
1632 s
= face_s
; /* vec */
1633 t
= face_t
; /* vec */
1634 /* use 'r' to indicate cube face */
1635 r
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, face
); /* vec */
1639 * Get pointer(s) to image data for mipmap level(s).
1641 data_ptr0
= lp_build_get_mipmap_level(bld
, data_array
, ilevel0
);
1642 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1643 data_ptr1
= lp_build_get_mipmap_level(bld
, data_array
, ilevel1
);
1647 * Get/interpolate texture colors.
1649 if (min_filter
== mag_filter
) {
1650 /* no need to distinquish between minification and magnification */
1651 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
, s
, t
, r
, lod_fpart
,
1652 width0_vec
, width1_vec
,
1653 height0_vec
, height1_vec
,
1654 depth0_vec
, depth1_vec
,
1655 row_stride0_vec
, row_stride1_vec
,
1656 img_stride0_vec
, img_stride1_vec
,
1657 data_ptr0
, data_ptr1
,
1661 /* Emit conditional to choose min image filter or mag image filter
1662 * depending on the lod being >0 or <= 0, respectively.
1664 struct lp_build_flow_context
*flow_ctx
;
1665 struct lp_build_if_state if_ctx
;
1666 LLVMValueRef minify
;
1668 flow_ctx
= lp_build_flow_create(bld
->builder
);
1669 lp_build_flow_scope_begin(flow_ctx
);
1671 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[0]);
1672 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[1]);
1673 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[2]);
1674 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[3]);
1676 /* minify = lod > 0.0 */
1677 minify
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
1678 lod
, float_bld
->zero
, "");
1680 lp_build_if(&if_ctx
, flow_ctx
, bld
->builder
, minify
);
1682 /* Use the minification filter */
1683 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
,
1685 width0_vec
, width1_vec
,
1686 height0_vec
, height1_vec
,
1687 depth0_vec
, depth1_vec
,
1688 row_stride0_vec
, row_stride1_vec
,
1689 img_stride0_vec
, img_stride1_vec
,
1690 data_ptr0
, data_ptr1
,
1693 lp_build_else(&if_ctx
);
1695 /* Use the magnification filter */
1696 lp_build_sample_mipmap(bld
, mag_filter
, mip_filter
,
1698 width0_vec
, width1_vec
,
1699 height0_vec
, height1_vec
,
1700 depth0_vec
, depth1_vec
,
1701 row_stride0_vec
, row_stride1_vec
,
1702 img_stride0_vec
, img_stride1_vec
,
1703 data_ptr0
, data_ptr1
,
1706 lp_build_endif(&if_ctx
);
1708 lp_build_flow_scope_end(flow_ctx
);
1709 lp_build_flow_destroy(flow_ctx
);
1716 lp_build_sample_2d_linear_aos(struct lp_build_sample_context
*bld
,
1720 LLVMValueRef height
,
1721 LLVMValueRef stride_array
,
1722 LLVMValueRef data_array
,
1723 LLVMValueRef texel_out
[4])
1725 LLVMBuilderRef builder
= bld
->builder
;
1726 struct lp_build_context i32
, h16
, u8n
;
1727 LLVMTypeRef i32_vec_type
, h16_vec_type
, u8n_vec_type
;
1728 LLVMValueRef i32_c8
, i32_c128
, i32_c255
;
1729 LLVMValueRef s_ipart
, s_fpart
, s_fpart_lo
, s_fpart_hi
;
1730 LLVMValueRef t_ipart
, t_fpart
, t_fpart_lo
, t_fpart_hi
;
1731 LLVMValueRef x0
, x1
;
1732 LLVMValueRef y0
, y1
;
1733 LLVMValueRef neighbors
[2][2];
1734 LLVMValueRef neighbors_lo
[2][2];
1735 LLVMValueRef neighbors_hi
[2][2];
1736 LLVMValueRef packed
, packed_lo
, packed_hi
;
1737 LLVMValueRef unswizzled
[4];
1738 LLVMValueRef stride
;
1740 assert(bld
->static_state
->target
== PIPE_TEXTURE_2D
);
1741 assert(bld
->static_state
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
);
1742 assert(bld
->static_state
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
);
1743 assert(bld
->static_state
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
);
1745 lp_build_context_init(&i32
, builder
, lp_type_int_vec(32));
1746 lp_build_context_init(&h16
, builder
, lp_type_ufixed(16));
1747 lp_build_context_init(&u8n
, builder
, lp_type_unorm(8));
1749 i32_vec_type
= lp_build_vec_type(i32
.type
);
1750 h16_vec_type
= lp_build_vec_type(h16
.type
);
1751 u8n_vec_type
= lp_build_vec_type(u8n
.type
);
1753 if (bld
->static_state
->normalized_coords
) {
1754 LLVMTypeRef coord_vec_type
= lp_build_vec_type(bld
->coord_type
);
1755 LLVMValueRef fp_width
= LLVMBuildSIToFP(bld
->builder
, width
, coord_vec_type
, "");
1756 LLVMValueRef fp_height
= LLVMBuildSIToFP(bld
->builder
, height
, coord_vec_type
, "");
1757 s
= lp_build_mul(&bld
->coord_bld
, s
, fp_width
);
1758 t
= lp_build_mul(&bld
->coord_bld
, t
, fp_height
);
1761 /* scale coords by 256 (8 fractional bits) */
1762 s
= lp_build_mul_imm(&bld
->coord_bld
, s
, 256);
1763 t
= lp_build_mul_imm(&bld
->coord_bld
, t
, 256);
1765 /* convert float to int */
1766 s
= LLVMBuildFPToSI(builder
, s
, i32_vec_type
, "");
1767 t
= LLVMBuildFPToSI(builder
, t
, i32_vec_type
, "");
1769 /* subtract 0.5 (add -128) */
1770 i32_c128
= lp_build_const_int_vec(i32
.type
, -128);
1771 s
= LLVMBuildAdd(builder
, s
, i32_c128
, "");
1772 t
= LLVMBuildAdd(builder
, t
, i32_c128
, "");
1774 /* compute floor (shift right 8) */
1775 i32_c8
= lp_build_const_int_vec(i32
.type
, 8);
1776 s_ipart
= LLVMBuildAShr(builder
, s
, i32_c8
, "");
1777 t_ipart
= LLVMBuildAShr(builder
, t
, i32_c8
, "");
1779 /* compute fractional part (AND with 0xff) */
1780 i32_c255
= lp_build_const_int_vec(i32
.type
, 255);
1781 s_fpart
= LLVMBuildAnd(builder
, s
, i32_c255
, "");
1782 t_fpart
= LLVMBuildAnd(builder
, t
, i32_c255
, "");
1787 x1
= lp_build_add(&bld
->int_coord_bld
, x0
, bld
->int_coord_bld
.one
);
1788 y1
= lp_build_add(&bld
->int_coord_bld
, y0
, bld
->int_coord_bld
.one
);
1790 x0
= lp_build_sample_wrap_int(bld
, x0
, width
, bld
->static_state
->pot_width
,
1791 bld
->static_state
->wrap_s
);
1792 y0
= lp_build_sample_wrap_int(bld
, y0
, height
, bld
->static_state
->pot_height
,
1793 bld
->static_state
->wrap_t
);
1795 x1
= lp_build_sample_wrap_int(bld
, x1
, width
, bld
->static_state
->pot_width
,
1796 bld
->static_state
->wrap_s
);
1797 y1
= lp_build_sample_wrap_int(bld
, y1
, height
, bld
->static_state
->pot_height
,
1798 bld
->static_state
->wrap_t
);
1801 * Transform 4 x i32 in
1803 * s_fpart = {s0, s1, s2, s3}
1807 * s_fpart = {00, s0, 00, s1, 00, s2, 00, s3}
1811 * s_fpart_lo = {s0, s0, s0, s0, s1, s1, s1, s1}
1812 * s_fpart_hi = {s2, s2, s2, s2, s3, s3, s3, s3}
1814 * and likewise for t_fpart. There is no risk of loosing precision here
1815 * since the fractional parts only use the lower 8bits.
1818 s_fpart
= LLVMBuildBitCast(builder
, s_fpart
, h16_vec_type
, "");
1819 t_fpart
= LLVMBuildBitCast(builder
, t_fpart
, h16_vec_type
, "");
1822 LLVMTypeRef elem_type
= LLVMInt32Type();
1823 LLVMValueRef shuffles_lo
[LP_MAX_VECTOR_LENGTH
];
1824 LLVMValueRef shuffles_hi
[LP_MAX_VECTOR_LENGTH
];
1825 LLVMValueRef shuffle_lo
;
1826 LLVMValueRef shuffle_hi
;
1829 for(j
= 0; j
< h16
.type
.length
; j
+= 4) {
1830 unsigned subindex
= util_cpu_caps
.little_endian
? 0 : 1;
1833 index
= LLVMConstInt(elem_type
, j
/2 + subindex
, 0);
1834 for(i
= 0; i
< 4; ++i
)
1835 shuffles_lo
[j
+ i
] = index
;
1837 index
= LLVMConstInt(elem_type
, h16
.type
.length
/2 + j
/2 + subindex
, 0);
1838 for(i
= 0; i
< 4; ++i
)
1839 shuffles_hi
[j
+ i
] = index
;
1842 shuffle_lo
= LLVMConstVector(shuffles_lo
, h16
.type
.length
);
1843 shuffle_hi
= LLVMConstVector(shuffles_hi
, h16
.type
.length
);
1845 s_fpart_lo
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_lo
, "");
1846 t_fpart_lo
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_lo
, "");
1847 s_fpart_hi
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_hi
, "");
1848 t_fpart_hi
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_hi
, "");
1851 stride
= lp_build_get_const_level_stride_vec(bld
, stride_array
, 0);
1854 * Fetch the pixels as 4 x 32bit (rgba order might differ):
1856 * rgba0 rgba1 rgba2 rgba3
1858 * bit cast them into 16 x u8
1860 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
1862 * unpack them into two 8 x i16:
1864 * r0 g0 b0 a0 r1 g1 b1 a1
1865 * r2 g2 b2 a2 r3 g3 b3 a3
1867 * The higher 8 bits of the resulting elements will be zero.
1870 neighbors
[0][0] = lp_build_sample_packed(bld
, x0
, y0
, stride
, data_array
);
1871 neighbors
[0][1] = lp_build_sample_packed(bld
, x1
, y0
, stride
, data_array
);
1872 neighbors
[1][0] = lp_build_sample_packed(bld
, x0
, y1
, stride
, data_array
);
1873 neighbors
[1][1] = lp_build_sample_packed(bld
, x1
, y1
, stride
, data_array
);
1875 neighbors
[0][0] = LLVMBuildBitCast(builder
, neighbors
[0][0], u8n_vec_type
, "");
1876 neighbors
[0][1] = LLVMBuildBitCast(builder
, neighbors
[0][1], u8n_vec_type
, "");
1877 neighbors
[1][0] = LLVMBuildBitCast(builder
, neighbors
[1][0], u8n_vec_type
, "");
1878 neighbors
[1][1] = LLVMBuildBitCast(builder
, neighbors
[1][1], u8n_vec_type
, "");
1880 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][0], &neighbors_lo
[0][0], &neighbors_hi
[0][0]);
1881 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][1], &neighbors_lo
[0][1], &neighbors_hi
[0][1]);
1882 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][0], &neighbors_lo
[1][0], &neighbors_hi
[1][0]);
1883 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][1], &neighbors_lo
[1][1], &neighbors_hi
[1][1]);
1886 * Linear interpolate with 8.8 fixed point.
1889 packed_lo
= lp_build_lerp_2d(&h16
,
1890 s_fpart_lo
, t_fpart_lo
,
1894 neighbors_lo
[1][1]);
1896 packed_hi
= lp_build_lerp_2d(&h16
,
1897 s_fpart_hi
, t_fpart_hi
,
1901 neighbors_hi
[1][1]);
1903 packed
= lp_build_pack2(builder
, h16
.type
, u8n
.type
, packed_lo
, packed_hi
);
1906 * Convert to SoA and swizzle.
1909 lp_build_rgba8_to_f32_soa(bld
->builder
,
1911 packed
, unswizzled
);
1913 lp_build_format_swizzle_soa(bld
->format_desc
,
1915 unswizzled
, texel_out
);
1917 apply_sampler_swizzle(bld
, texel_out
);
1922 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
1924 LLVMValueRef texel
[4])
1926 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1930 if(bld
->static_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
1933 /* TODO: Compare before swizzling, to avoid redundant computations */
1935 for(chan
= 0; chan
< 4; ++chan
) {
1937 cmp
= lp_build_cmp(texel_bld
, bld
->static_state
->compare_func
, p
, texel
[chan
]);
1938 cmp
= lp_build_select(texel_bld
, cmp
, texel_bld
->one
, texel_bld
->zero
);
1941 res
= lp_build_add(texel_bld
, res
, cmp
);
1947 res
= lp_build_mul(texel_bld
, res
, lp_build_const_vec(texel_bld
->type
, 0.25));
1949 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1950 for(chan
= 0; chan
< 3; ++chan
)
1952 texel
[3] = texel_bld
->one
;
1957 * Just set texels to white instead of actually sampling the texture.
1961 lp_build_sample_nop(struct lp_build_sample_context
*bld
,
1962 LLVMValueRef texel_out
[4])
1964 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1967 for (chan
= 0; chan
< 4; chan
++) {
1968 /*lp_bld_mov(texel_bld, texel, texel_bld->one);*/
1969 texel_out
[chan
] = texel_bld
->one
;
1975 * Build texture sampling code.
1976 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1978 * \param type vector float type to use for coords, etc.
1981 lp_build_sample_soa(LLVMBuilderRef builder
,
1982 const struct lp_sampler_static_state
*static_state
,
1983 struct lp_sampler_dynamic_state
*dynamic_state
,
1984 struct lp_type type
,
1986 unsigned num_coords
,
1987 const LLVMValueRef
*coords
,
1988 const LLVMValueRef
*ddx
,
1989 const LLVMValueRef
*ddy
,
1990 LLVMValueRef lod_bias
, /* optional */
1991 LLVMValueRef explicit_lod
, /* optional */
1992 LLVMValueRef texel_out
[4])
1994 struct lp_build_sample_context bld
;
1995 LLVMValueRef width
, width_vec
;
1996 LLVMValueRef height
, height_vec
;
1997 LLVMValueRef depth
, depth_vec
;
1998 LLVMValueRef row_stride_array
, img_stride_array
;
1999 LLVMValueRef data_array
;
2005 enum pipe_format fmt
= static_state
->format
;
2006 debug_printf("Sample from %s\n", util_format_name(fmt
));
2009 /* Setup our build context */
2010 memset(&bld
, 0, sizeof bld
);
2011 bld
.builder
= builder
;
2012 bld
.static_state
= static_state
;
2013 bld
.dynamic_state
= dynamic_state
;
2014 bld
.format_desc
= util_format_description(static_state
->format
);
2016 bld
.float_type
= lp_type_float(32);
2017 bld
.int_type
= lp_type_int(32);
2018 bld
.coord_type
= type
;
2019 bld
.uint_coord_type
= lp_uint_type(type
);
2020 bld
.int_coord_type
= lp_int_type(type
);
2021 bld
.texel_type
= type
;
2023 lp_build_context_init(&bld
.float_bld
, builder
, bld
.float_type
);
2024 lp_build_context_init(&bld
.int_bld
, builder
, bld
.int_type
);
2025 lp_build_context_init(&bld
.coord_bld
, builder
, bld
.coord_type
);
2026 lp_build_context_init(&bld
.uint_coord_bld
, builder
, bld
.uint_coord_type
);
2027 lp_build_context_init(&bld
.int_coord_bld
, builder
, bld
.int_coord_type
);
2028 lp_build_context_init(&bld
.texel_bld
, builder
, bld
.texel_type
);
2030 /* Get the dynamic state */
2031 width
= dynamic_state
->width(dynamic_state
, builder
, unit
);
2032 height
= dynamic_state
->height(dynamic_state
, builder
, unit
);
2033 depth
= dynamic_state
->depth(dynamic_state
, builder
, unit
);
2034 row_stride_array
= dynamic_state
->row_stride(dynamic_state
, builder
, unit
);
2035 img_stride_array
= dynamic_state
->img_stride(dynamic_state
, builder
, unit
);
2036 data_array
= dynamic_state
->data_ptr(dynamic_state
, builder
, unit
);
2037 /* Note that data_array is an array[level] of pointers to texture images */
2043 width_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, width
);
2044 height_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, height
);
2045 depth_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, depth
);
2048 /* For debug: no-op texture sampling */
2049 lp_build_sample_nop(&bld
, texel_out
);
2051 else if (util_format_is_rgba8_variant(bld
.format_desc
) &&
2052 static_state
->target
== PIPE_TEXTURE_2D
&&
2053 static_state
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
2054 static_state
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
2055 static_state
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
&&
2056 is_simple_wrap_mode(static_state
->wrap_s
) &&
2057 is_simple_wrap_mode(static_state
->wrap_t
)) {
2059 lp_build_sample_2d_linear_aos(&bld
, s
, t
, width_vec
, height_vec
,
2060 row_stride_array
, data_array
, texel_out
);
2063 lp_build_sample_general(&bld
, unit
, s
, t
, r
, ddx
, ddy
,
2064 lod_bias
, explicit_lod
,
2065 width
, height
, depth
,
2066 width_vec
, height_vec
, depth_vec
,
2067 row_stride_array
, img_stride_array
,
2072 lp_build_sample_compare(&bld
, r
, texel_out
);