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 "lp_bld_debug.h"
44 #include "lp_bld_type.h"
45 #include "lp_bld_const.h"
46 #include "lp_bld_conv.h"
47 #include "lp_bld_arit.h"
48 #include "lp_bld_logic.h"
49 #include "lp_bld_swizzle.h"
50 #include "lp_bld_pack.h"
51 #include "lp_bld_flow.h"
52 #include "lp_bld_gather.h"
53 #include "lp_bld_format.h"
54 #include "lp_bld_sample.h"
55 #include "lp_bld_quad.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");
268 /* convert x,y,z coords to linear offset from start of texture, in bytes */
269 lp_build_sample_offset(&bld
->uint_coord_bld
,
271 x
, y
, z
, y_stride
, z_stride
,
275 /* If we can sample the border color, it means that texcoords may
276 * lie outside the bounds of the texture image. We need to do
277 * something to prevent reading out of bounds and causing a segfault.
279 * Simply AND the texture coords with !use_border. This will cause
280 * coords which are out of bounds to become zero. Zero's guaranteed
281 * to be inside the texture image.
283 offset
= lp_build_andc(&bld
->uint_coord_bld
, offset
, use_border
);
286 lp_build_fetch_rgba_soa(bld
->builder
,
293 apply_sampler_swizzle(bld
, texel_out
);
296 * Note: if we find an app which frequently samples the texture border
297 * we might want to implement a true conditional here to avoid sampling
298 * the texture whenever possible (since that's quite a bit of code).
301 * texel = border_color;
304 * texel = sample_texture(coord);
306 * As it is now, we always sample the texture, then selectively replace
307 * the texel color results with the border color.
311 /* select texel color or border color depending on use_border */
313 for (chan
= 0; chan
< 4; chan
++) {
314 LLVMValueRef border_chan
=
315 lp_build_const_vec(bld
->texel_type
,
316 bld
->static_state
->border_color
[chan
]);
317 texel_out
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
318 border_chan
, texel_out
[chan
]);
325 * Fetch the texels as <4n x i8> in AoS form.
328 lp_build_sample_packed(struct lp_build_sample_context
*bld
,
331 LLVMValueRef y_stride
,
332 LLVMValueRef data_array
)
334 LLVMValueRef offset
, i
, j
;
335 LLVMValueRef data_ptr
;
338 /* convert x,y,z coords to linear offset from start of texture, in bytes */
339 lp_build_sample_offset(&bld
->uint_coord_bld
,
341 x
, y
, NULL
, y_stride
, NULL
,
344 /* get pointer to mipmap level 0 data */
345 data_ptr
= lp_build_get_const_mipmap_level(bld
, data_array
, 0);
347 if (util_format_is_rgba8_variant(bld
->format_desc
)) {
348 /* Just fetch the data directly without swizzling */
349 assert(bld
->format_desc
->block
.width
== 1);
350 assert(bld
->format_desc
->block
.height
== 1);
351 assert(bld
->format_desc
->block
.bits
<= bld
->texel_type
.width
);
353 res
= lp_build_gather(bld
->builder
,
354 bld
->texel_type
.length
,
355 bld
->format_desc
->block
.bits
,
356 bld
->texel_type
.width
,
362 assert(bld
->texel_type
.width
== 32);
364 memset(&type
, 0, sizeof type
);
366 type
.length
= bld
->texel_type
.length
*4;
369 res
= lp_build_fetch_rgba_aos(bld
->builder
, bld
->format_desc
, type
,
370 data_ptr
, offset
, i
, j
);
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
= LLVMBuildLShr(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 ddx partial derivatives of (s, t, r, q) with respect to X
822 * \param ddy partial derivatives of (s, t, r, q) with respect to Y
823 * \param lod_bias optional float vector with the shader lod bias
824 * \param explicit_lod optional float vector with the explicit lod
825 * \param width scalar int texture width
826 * \param height scalar int texture height
827 * \param depth scalar int texture depth
829 * XXX: The resulting lod is scalar, so ignore all but the first element of
830 * derivatives, lod_bias, etc that are passed by the shader.
833 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
834 const LLVMValueRef ddx
[4],
835 const LLVMValueRef ddy
[4],
836 LLVMValueRef lod_bias
, /* optional */
837 LLVMValueRef explicit_lod
, /* optional */
843 if (bld
->static_state
->min_lod
== bld
->static_state
->max_lod
) {
844 /* User is forcing sampling from a particular mipmap level.
845 * This is hit during mipmap generation.
847 return LLVMConstReal(LLVMFloatType(), bld
->static_state
->min_lod
);
850 struct lp_build_context
*float_bld
= &bld
->float_bld
;
851 LLVMValueRef sampler_lod_bias
= LLVMConstReal(LLVMFloatType(),
852 bld
->static_state
->lod_bias
);
853 LLVMValueRef min_lod
= LLVMConstReal(LLVMFloatType(),
854 bld
->static_state
->min_lod
);
855 LLVMValueRef max_lod
= LLVMConstReal(LLVMFloatType(),
856 bld
->static_state
->max_lod
);
857 LLVMValueRef index0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
861 lod
= LLVMBuildExtractElement(bld
->builder
, explicit_lod
,
865 const int dims
= texture_dims(bld
->static_state
->target
);
866 LLVMValueRef dsdx
, dsdy
;
867 LLVMValueRef dtdx
= NULL
, dtdy
= NULL
, drdx
= NULL
, drdy
= NULL
;
870 dsdx
= LLVMBuildExtractElement(bld
->builder
, ddx
[0], index0
, "dsdx");
871 dsdx
= lp_build_abs(float_bld
, dsdx
);
872 dsdy
= LLVMBuildExtractElement(bld
->builder
, ddy
[0], index0
, "dsdy");
873 dsdy
= lp_build_abs(float_bld
, dsdy
);
875 dtdx
= LLVMBuildExtractElement(bld
->builder
, ddx
[1], index0
, "dtdx");
876 dtdx
= lp_build_abs(float_bld
, dtdx
);
877 dtdy
= LLVMBuildExtractElement(bld
->builder
, ddy
[1], index0
, "dtdy");
878 dtdy
= lp_build_abs(float_bld
, dtdy
);
880 drdx
= LLVMBuildExtractElement(bld
->builder
, ddx
[2], index0
, "drdx");
881 drdx
= lp_build_abs(float_bld
, drdx
);
882 drdy
= LLVMBuildExtractElement(bld
->builder
, ddy
[2], index0
, "drdy");
883 drdy
= lp_build_abs(float_bld
, drdy
);
887 /* Compute rho = max of all partial derivatives scaled by texture size.
888 * XXX this could be vectorized somewhat
890 rho
= LLVMBuildFMul(bld
->builder
,
891 lp_build_max(float_bld
, dsdx
, dsdy
),
892 lp_build_int_to_float(float_bld
, width
), "");
895 max
= LLVMBuildFMul(bld
->builder
,
896 lp_build_max(float_bld
, dtdx
, dtdy
),
897 lp_build_int_to_float(float_bld
, height
), "");
898 rho
= lp_build_max(float_bld
, rho
, max
);
900 max
= LLVMBuildFMul(bld
->builder
,
901 lp_build_max(float_bld
, drdx
, drdy
),
902 lp_build_int_to_float(float_bld
, depth
), "");
903 rho
= lp_build_max(float_bld
, rho
, max
);
907 /* compute lod = log2(rho) */
908 lod
= lp_build_log2(float_bld
, rho
);
910 /* add shader lod bias */
912 lod_bias
= LLVMBuildExtractElement(bld
->builder
, lod_bias
,
914 lod
= LLVMBuildFAdd(bld
->builder
, lod
, lod_bias
, "shader_lod_bias");
918 /* add sampler lod bias */
919 lod
= LLVMBuildFAdd(bld
->builder
, lod
, sampler_lod_bias
, "sampler_lod_bias");
922 lod
= lp_build_clamp(float_bld
, lod
, min_lod
, max_lod
);
930 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
931 * mipmap level index.
932 * Note: this is all scalar code.
933 * \param lod scalar float texture level of detail
934 * \param level_out returns integer
937 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
940 LLVMValueRef
*level_out
)
942 struct lp_build_context
*float_bld
= &bld
->float_bld
;
943 struct lp_build_context
*int_bld
= &bld
->int_bld
;
944 LLVMValueRef last_level
, level
;
946 LLVMValueRef zero
= LLVMConstInt(LLVMInt32Type(), 0, 0);
948 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
951 /* convert float lod to integer */
952 level
= lp_build_iround(float_bld
, lod
);
954 /* clamp level to legal range of levels */
955 *level_out
= lp_build_clamp(int_bld
, level
, zero
, last_level
);
960 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
961 * two (adjacent) mipmap level indexes. Later, we'll sample from those
962 * two mipmap levels and interpolate between them.
965 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
968 LLVMValueRef
*level0_out
,
969 LLVMValueRef
*level1_out
,
970 LLVMValueRef
*weight_out
)
972 struct lp_build_context
*float_bld
= &bld
->float_bld
;
973 struct lp_build_context
*int_bld
= &bld
->int_bld
;
974 LLVMValueRef last_level
, level
;
976 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
979 /* convert float lod to integer */
980 level
= lp_build_ifloor(float_bld
, lod
);
982 /* compute level 0 and clamp to legal range of levels */
983 *level0_out
= lp_build_clamp(int_bld
, level
,
986 /* compute level 1 and clamp to legal range of levels */
987 level
= lp_build_add(int_bld
, level
, int_bld
->one
);
988 *level1_out
= lp_build_clamp(int_bld
, level
,
992 *weight_out
= lp_build_fract(float_bld
, lod
);
997 * Generate code to sample a mipmap level with nearest filtering.
998 * If sampling a cube texture, r = cube face in [0,5].
1001 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
1002 LLVMValueRef width_vec
,
1003 LLVMValueRef height_vec
,
1004 LLVMValueRef depth_vec
,
1005 LLVMValueRef row_stride_vec
,
1006 LLVMValueRef img_stride_vec
,
1007 LLVMValueRef data_ptr
,
1011 LLVMValueRef colors_out
[4])
1013 const int dims
= texture_dims(bld
->static_state
->target
);
1014 LLVMValueRef x
, y
, z
;
1017 * Compute integer texcoords.
1019 x
= lp_build_sample_wrap_nearest(bld
, s
, width_vec
,
1020 bld
->static_state
->pot_width
,
1021 bld
->static_state
->wrap_s
);
1022 lp_build_name(x
, "tex.x.wrapped");
1025 y
= lp_build_sample_wrap_nearest(bld
, t
, height_vec
,
1026 bld
->static_state
->pot_height
,
1027 bld
->static_state
->wrap_t
);
1028 lp_build_name(y
, "tex.y.wrapped");
1031 z
= lp_build_sample_wrap_nearest(bld
, r
, depth_vec
,
1032 bld
->static_state
->pot_height
,
1033 bld
->static_state
->wrap_r
);
1034 lp_build_name(z
, "tex.z.wrapped");
1036 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1048 * Get texture colors.
1050 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1052 row_stride_vec
, img_stride_vec
,
1053 data_ptr
, colors_out
);
1058 * Generate code to sample a mipmap level with linear filtering.
1059 * If sampling a cube texture, r = cube face in [0,5].
1062 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
1063 LLVMValueRef width_vec
,
1064 LLVMValueRef height_vec
,
1065 LLVMValueRef depth_vec
,
1066 LLVMValueRef row_stride_vec
,
1067 LLVMValueRef img_stride_vec
,
1068 LLVMValueRef data_ptr
,
1072 LLVMValueRef colors_out
[4])
1074 const int dims
= texture_dims(bld
->static_state
->target
);
1075 LLVMValueRef x0
, y0
, z0
, x1
, y1
, z1
;
1076 LLVMValueRef s_fpart
, t_fpart
, r_fpart
;
1077 LLVMValueRef neighbors
[2][2][4];
1081 * Compute integer texcoords.
1083 lp_build_sample_wrap_linear(bld
, s
, width_vec
,
1084 bld
->static_state
->pot_width
,
1085 bld
->static_state
->wrap_s
,
1086 &x0
, &x1
, &s_fpart
);
1087 lp_build_name(x0
, "tex.x0.wrapped");
1088 lp_build_name(x1
, "tex.x1.wrapped");
1091 lp_build_sample_wrap_linear(bld
, t
, height_vec
,
1092 bld
->static_state
->pot_height
,
1093 bld
->static_state
->wrap_t
,
1094 &y0
, &y1
, &t_fpart
);
1095 lp_build_name(y0
, "tex.y0.wrapped");
1096 lp_build_name(y1
, "tex.y1.wrapped");
1099 lp_build_sample_wrap_linear(bld
, r
, depth_vec
,
1100 bld
->static_state
->pot_depth
,
1101 bld
->static_state
->wrap_r
,
1102 &z0
, &z1
, &r_fpart
);
1103 lp_build_name(z0
, "tex.z0.wrapped");
1104 lp_build_name(z1
, "tex.z1.wrapped");
1106 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1107 z0
= z1
= r
; /* cube face */
1116 y0
= y1
= t_fpart
= NULL
;
1117 z0
= z1
= r_fpart
= NULL
;
1121 * Get texture colors.
1123 /* get x0/x1 texels */
1124 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1126 row_stride_vec
, img_stride_vec
,
1127 data_ptr
, neighbors
[0][0]);
1128 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1130 row_stride_vec
, img_stride_vec
,
1131 data_ptr
, neighbors
[0][1]);
1134 /* Interpolate two samples from 1D image to produce one color */
1135 for (chan
= 0; chan
< 4; chan
++) {
1136 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, s_fpart
,
1137 neighbors
[0][0][chan
],
1138 neighbors
[0][1][chan
]);
1143 LLVMValueRef colors0
[4];
1145 /* get x0/x1 texels at y1 */
1146 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1148 row_stride_vec
, img_stride_vec
,
1149 data_ptr
, neighbors
[1][0]);
1150 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1152 row_stride_vec
, img_stride_vec
,
1153 data_ptr
, neighbors
[1][1]);
1155 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
1156 for (chan
= 0; chan
< 4; chan
++) {
1157 colors0
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1159 neighbors
[0][0][chan
],
1160 neighbors
[0][1][chan
],
1161 neighbors
[1][0][chan
],
1162 neighbors
[1][1][chan
]);
1166 LLVMValueRef neighbors1
[2][2][4];
1167 LLVMValueRef colors1
[4];
1169 /* get x0/x1/y0/y1 texels at z1 */
1170 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1172 row_stride_vec
, img_stride_vec
,
1173 data_ptr
, neighbors1
[0][0]);
1174 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1176 row_stride_vec
, img_stride_vec
,
1177 data_ptr
, neighbors1
[0][1]);
1178 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1180 row_stride_vec
, img_stride_vec
,
1181 data_ptr
, neighbors1
[1][0]);
1182 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1184 row_stride_vec
, img_stride_vec
,
1185 data_ptr
, neighbors1
[1][1]);
1187 /* Bilinear interpolate the four samples from the second Z slice */
1188 for (chan
= 0; chan
< 4; chan
++) {
1189 colors1
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1191 neighbors1
[0][0][chan
],
1192 neighbors1
[0][1][chan
],
1193 neighbors1
[1][0][chan
],
1194 neighbors1
[1][1][chan
]);
1197 /* Linearly interpolate the two samples from the two 3D slices */
1198 for (chan
= 0; chan
< 4; chan
++) {
1199 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
,
1201 colors0
[chan
], colors1
[chan
]);
1206 for (chan
= 0; chan
< 4; chan
++) {
1207 colors_out
[chan
] = colors0
[chan
];
1214 /** Helper used by lp_build_cube_lookup() */
1216 lp_build_cube_ima(struct lp_build_context
*coord_bld
, LLVMValueRef coord
)
1218 /* ima = -0.5 / abs(coord); */
1219 LLVMValueRef negHalf
= lp_build_const_vec(coord_bld
->type
, -0.5);
1220 LLVMValueRef absCoord
= lp_build_abs(coord_bld
, coord
);
1221 LLVMValueRef ima
= lp_build_div(coord_bld
, negHalf
, absCoord
);
1227 * Helper used by lp_build_cube_lookup()
1228 * \param sign scalar +1 or -1
1229 * \param coord float vector
1230 * \param ima float vector
1233 lp_build_cube_coord(struct lp_build_context
*coord_bld
,
1234 LLVMValueRef sign
, int negate_coord
,
1235 LLVMValueRef coord
, LLVMValueRef ima
)
1237 /* return negate(coord) * ima * sign + 0.5; */
1238 LLVMValueRef half
= lp_build_const_vec(coord_bld
->type
, 0.5);
1241 assert(negate_coord
== +1 || negate_coord
== -1);
1243 if (negate_coord
== -1) {
1244 coord
= lp_build_negate(coord_bld
, coord
);
1247 res
= lp_build_mul(coord_bld
, coord
, ima
);
1249 sign
= lp_build_broadcast_scalar(coord_bld
, sign
);
1250 res
= lp_build_mul(coord_bld
, res
, sign
);
1252 res
= lp_build_add(coord_bld
, res
, half
);
1258 /** Helper used by lp_build_cube_lookup()
1259 * Return (major_coord >= 0) ? pos_face : neg_face;
1262 lp_build_cube_face(struct lp_build_sample_context
*bld
,
1263 LLVMValueRef major_coord
,
1264 unsigned pos_face
, unsigned neg_face
)
1266 LLVMValueRef cmp
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
1268 bld
->float_bld
.zero
, "");
1269 LLVMValueRef pos
= LLVMConstInt(LLVMInt32Type(), pos_face
, 0);
1270 LLVMValueRef neg
= LLVMConstInt(LLVMInt32Type(), neg_face
, 0);
1271 LLVMValueRef res
= LLVMBuildSelect(bld
->builder
, cmp
, pos
, neg
, "");
1278 * Generate code to do cube face selection and compute per-face texcoords.
1281 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
1286 LLVMValueRef
*face_s
,
1287 LLVMValueRef
*face_t
)
1289 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1290 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
1291 LLVMValueRef rx
, ry
, rz
;
1292 LLVMValueRef arx
, ary
, arz
;
1293 LLVMValueRef c25
= LLVMConstReal(LLVMFloatType(), 0.25);
1294 LLVMValueRef arx_ge_ary
, arx_ge_arz
;
1295 LLVMValueRef ary_ge_arx
, ary_ge_arz
;
1296 LLVMValueRef arx_ge_ary_arz
, ary_ge_arx_arz
;
1297 LLVMValueRef rx_pos
, ry_pos
, rz_pos
;
1299 assert(bld
->coord_bld
.type
.length
== 4);
1302 * Use the average of the four pixel's texcoords to choose the face.
1304 rx
= lp_build_mul(float_bld
, c25
,
1305 lp_build_sum_vector(&bld
->coord_bld
, s
));
1306 ry
= lp_build_mul(float_bld
, c25
,
1307 lp_build_sum_vector(&bld
->coord_bld
, t
));
1308 rz
= lp_build_mul(float_bld
, c25
,
1309 lp_build_sum_vector(&bld
->coord_bld
, r
));
1311 arx
= lp_build_abs(float_bld
, rx
);
1312 ary
= lp_build_abs(float_bld
, ry
);
1313 arz
= lp_build_abs(float_bld
, rz
);
1316 * Compare sign/magnitude of rx,ry,rz to determine face
1318 arx_ge_ary
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, arx
, ary
, "");
1319 arx_ge_arz
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, arx
, arz
, "");
1320 ary_ge_arx
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ary
, arx
, "");
1321 ary_ge_arz
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ary
, arz
, "");
1323 arx_ge_ary_arz
= LLVMBuildAnd(bld
->builder
, arx_ge_ary
, arx_ge_arz
, "");
1324 ary_ge_arx_arz
= LLVMBuildAnd(bld
->builder
, ary_ge_arx
, ary_ge_arz
, "");
1326 rx_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, rx
, float_bld
->zero
, "");
1327 ry_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ry
, float_bld
->zero
, "");
1328 rz_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, rz
, float_bld
->zero
, "");
1331 struct lp_build_flow_context
*flow_ctx
;
1332 struct lp_build_if_state if_ctx
;
1334 flow_ctx
= lp_build_flow_create(bld
->builder
);
1335 lp_build_flow_scope_begin(flow_ctx
);
1337 *face_s
= bld
->coord_bld
.undef
;
1338 *face_t
= bld
->coord_bld
.undef
;
1339 *face
= bld
->int_bld
.undef
;
1341 lp_build_name(*face_s
, "face_s");
1342 lp_build_name(*face_t
, "face_t");
1343 lp_build_name(*face
, "face");
1345 lp_build_flow_scope_declare(flow_ctx
, face_s
);
1346 lp_build_flow_scope_declare(flow_ctx
, face_t
);
1347 lp_build_flow_scope_declare(flow_ctx
, face
);
1349 lp_build_if(&if_ctx
, flow_ctx
, bld
->builder
, arx_ge_ary_arz
);
1352 LLVMValueRef sign
= lp_build_sgn(float_bld
, rx
);
1353 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, s
);
1354 *face_s
= lp_build_cube_coord(coord_bld
, sign
, +1, r
, ima
);
1355 *face_t
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1356 *face
= lp_build_cube_face(bld
, rx
,
1357 PIPE_TEX_FACE_POS_X
,
1358 PIPE_TEX_FACE_NEG_X
);
1360 lp_build_else(&if_ctx
);
1362 struct lp_build_flow_context
*flow_ctx2
;
1363 struct lp_build_if_state if_ctx2
;
1365 LLVMValueRef face_s2
= bld
->coord_bld
.undef
;
1366 LLVMValueRef face_t2
= bld
->coord_bld
.undef
;
1367 LLVMValueRef face2
= bld
->int_bld
.undef
;
1369 flow_ctx2
= lp_build_flow_create(bld
->builder
);
1370 lp_build_flow_scope_begin(flow_ctx2
);
1371 lp_build_flow_scope_declare(flow_ctx2
, &face_s2
);
1372 lp_build_flow_scope_declare(flow_ctx2
, &face_t2
);
1373 lp_build_flow_scope_declare(flow_ctx2
, &face2
);
1375 ary_ge_arx_arz
= LLVMBuildAnd(bld
->builder
, ary_ge_arx
, ary_ge_arz
, "");
1377 lp_build_if(&if_ctx2
, flow_ctx2
, bld
->builder
, ary_ge_arx_arz
);
1380 LLVMValueRef sign
= lp_build_sgn(float_bld
, ry
);
1381 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, t
);
1382 face_s2
= lp_build_cube_coord(coord_bld
, NULL
, -1, s
, ima
);
1383 face_t2
= lp_build_cube_coord(coord_bld
, sign
, -1, r
, ima
);
1384 face2
= lp_build_cube_face(bld
, ry
,
1385 PIPE_TEX_FACE_POS_Y
,
1386 PIPE_TEX_FACE_NEG_Y
);
1388 lp_build_else(&if_ctx2
);
1391 LLVMValueRef sign
= lp_build_sgn(float_bld
, rz
);
1392 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, r
);
1393 face_s2
= lp_build_cube_coord(coord_bld
, sign
, -1, s
, ima
);
1394 face_t2
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1395 face2
= lp_build_cube_face(bld
, rz
,
1396 PIPE_TEX_FACE_POS_Z
,
1397 PIPE_TEX_FACE_NEG_Z
);
1399 lp_build_endif(&if_ctx2
);
1400 lp_build_flow_scope_end(flow_ctx2
);
1401 lp_build_flow_destroy(flow_ctx2
);
1407 lp_build_endif(&if_ctx
);
1408 lp_build_flow_scope_end(flow_ctx
);
1409 lp_build_flow_destroy(flow_ctx
);
1416 * Sample the texture/mipmap using given image filter and mip filter.
1417 * data0_ptr and data1_ptr point to the two mipmap levels to sample
1418 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
1419 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1422 lp_build_sample_mipmap(struct lp_build_sample_context
*bld
,
1423 unsigned img_filter
,
1424 unsigned mip_filter
,
1428 LLVMValueRef lod_fpart
,
1429 LLVMValueRef width0_vec
,
1430 LLVMValueRef width1_vec
,
1431 LLVMValueRef height0_vec
,
1432 LLVMValueRef height1_vec
,
1433 LLVMValueRef depth0_vec
,
1434 LLVMValueRef depth1_vec
,
1435 LLVMValueRef row_stride0_vec
,
1436 LLVMValueRef row_stride1_vec
,
1437 LLVMValueRef img_stride0_vec
,
1438 LLVMValueRef img_stride1_vec
,
1439 LLVMValueRef data_ptr0
,
1440 LLVMValueRef data_ptr1
,
1441 LLVMValueRef
*colors_out
)
1443 LLVMValueRef colors0
[4], colors1
[4];
1446 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
1447 /* sample the first mipmap level */
1448 lp_build_sample_image_nearest(bld
,
1449 width0_vec
, height0_vec
, depth0_vec
,
1450 row_stride0_vec
, img_stride0_vec
,
1451 data_ptr0
, s
, t
, r
, colors0
);
1453 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1454 /* sample the second mipmap level */
1455 lp_build_sample_image_nearest(bld
,
1456 width1_vec
, height1_vec
, depth1_vec
,
1457 row_stride1_vec
, img_stride1_vec
,
1458 data_ptr1
, s
, t
, r
, colors1
);
1462 assert(img_filter
== PIPE_TEX_FILTER_LINEAR
);
1464 /* sample the first mipmap level */
1465 lp_build_sample_image_linear(bld
,
1466 width0_vec
, height0_vec
, depth0_vec
,
1467 row_stride0_vec
, img_stride0_vec
,
1468 data_ptr0
, s
, t
, r
, colors0
);
1470 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1471 /* sample the second mipmap level */
1472 lp_build_sample_image_linear(bld
,
1473 width1_vec
, height1_vec
, depth1_vec
,
1474 row_stride1_vec
, img_stride1_vec
,
1475 data_ptr1
, s
, t
, r
, colors1
);
1479 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1480 /* interpolate samples from the two mipmap levels */
1481 for (chan
= 0; chan
< 4; chan
++) {
1482 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
1483 colors0
[chan
], colors1
[chan
]);
1487 /* use first/only level's colors */
1488 for (chan
= 0; chan
< 4; chan
++) {
1489 colors_out
[chan
] = colors0
[chan
];
1497 * General texture sampling codegen.
1498 * This function handles texture sampling for all texture targets (1D,
1499 * 2D, 3D, cube) and all filtering modes.
1502 lp_build_sample_general(struct lp_build_sample_context
*bld
,
1507 const LLVMValueRef
*ddx
,
1508 const LLVMValueRef
*ddy
,
1509 LLVMValueRef lod_bias
, /* optional */
1510 LLVMValueRef explicit_lod
, /* optional */
1512 LLVMValueRef height
,
1514 LLVMValueRef width_vec
,
1515 LLVMValueRef height_vec
,
1516 LLVMValueRef depth_vec
,
1517 LLVMValueRef row_stride_array
,
1518 LLVMValueRef img_stride_array
,
1519 LLVMValueRef data_array
,
1520 LLVMValueRef
*colors_out
)
1522 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1523 const unsigned mip_filter
= bld
->static_state
->min_mip_filter
;
1524 const unsigned min_filter
= bld
->static_state
->min_img_filter
;
1525 const unsigned mag_filter
= bld
->static_state
->mag_img_filter
;
1526 const int dims
= texture_dims(bld
->static_state
->target
);
1527 LLVMValueRef lod
= NULL
, lod_fpart
= NULL
;
1528 LLVMValueRef ilevel0
, ilevel1
= NULL
, ilevel0_vec
, ilevel1_vec
= NULL
;
1529 LLVMValueRef width0_vec
= NULL
, height0_vec
= NULL
, depth0_vec
= NULL
;
1530 LLVMValueRef width1_vec
= NULL
, height1_vec
= NULL
, depth1_vec
= NULL
;
1531 LLVMValueRef row_stride0_vec
= NULL
, row_stride1_vec
= NULL
;
1532 LLVMValueRef img_stride0_vec
= NULL
, img_stride1_vec
= NULL
;
1533 LLVMValueRef data_ptr0
, data_ptr1
= NULL
;
1534 LLVMValueRef face_ddx
[4], face_ddy
[4];
1537 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1538 mip_filter, min_filter, mag_filter);
1542 * Choose cube face, recompute texcoords and derivatives for the chosen face.
1544 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1545 LLVMValueRef face
, face_s
, face_t
;
1546 lp_build_cube_lookup(bld
, s
, t
, r
, &face
, &face_s
, &face_t
);
1547 s
= face_s
; /* vec */
1548 t
= face_t
; /* vec */
1549 /* use 'r' to indicate cube face */
1550 r
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, face
); /* vec */
1552 /* recompute ddx, ddy using the new (s,t) face texcoords */
1553 face_ddx
[0] = lp_build_ddx(&bld
->coord_bld
, s
);
1554 face_ddx
[1] = lp_build_ddx(&bld
->coord_bld
, t
);
1557 face_ddy
[0] = lp_build_ddy(&bld
->coord_bld
, s
);
1558 face_ddy
[1] = lp_build_ddy(&bld
->coord_bld
, t
);
1566 * Compute the level of detail (float).
1568 if (min_filter
!= mag_filter
||
1569 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
1570 /* Need to compute lod either to choose mipmap levels or to
1571 * distinguish between minification/magnification with one mipmap level.
1573 lod
= lp_build_lod_selector(bld
, ddx
, ddy
,
1574 lod_bias
, explicit_lod
,
1575 width
, height
, depth
);
1579 * Compute integer mipmap level(s) to fetch texels from.
1581 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
1582 /* always use mip level 0 */
1583 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1584 /* XXX this is a work-around for an apparent bug in LLVM 2.7.
1585 * We should be able to set ilevel0 = const(0) but that causes
1586 * bad x86 code to be emitted.
1588 lod
= lp_build_const_elem(bld
->coord_bld
.type
, 0.0);
1589 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
1592 ilevel0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
1597 if (mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
1598 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
1601 assert(mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
);
1602 lp_build_linear_mip_levels(bld
, unit
, lod
, &ilevel0
, &ilevel1
,
1604 lod_fpart
= lp_build_broadcast_scalar(&bld
->coord_bld
, lod_fpart
);
1609 * Convert scalar integer mipmap levels into vectors.
1611 ilevel0_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel0
);
1612 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
)
1613 ilevel1_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel1
);
1616 * Compute width, height at mipmap level 'ilevel0'
1618 width0_vec
= lp_build_minify(bld
, width_vec
, ilevel0_vec
);
1620 height0_vec
= lp_build_minify(bld
, height_vec
, ilevel0_vec
);
1621 row_stride0_vec
= lp_build_get_level_stride_vec(bld
, row_stride_array
,
1623 if (dims
== 3 || bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1624 img_stride0_vec
= lp_build_get_level_stride_vec(bld
,
1628 depth0_vec
= lp_build_minify(bld
, depth_vec
, ilevel0_vec
);
1632 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1633 /* compute width, height, depth for second mipmap level at 'ilevel1' */
1634 width1_vec
= lp_build_minify(bld
, width_vec
, ilevel1_vec
);
1636 height1_vec
= lp_build_minify(bld
, height_vec
, ilevel1_vec
);
1637 row_stride1_vec
= lp_build_get_level_stride_vec(bld
, row_stride_array
,
1639 if (dims
== 3 || bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1640 img_stride1_vec
= lp_build_get_level_stride_vec(bld
,
1644 depth1_vec
= lp_build_minify(bld
, depth_vec
, ilevel1_vec
);
1651 * Get pointer(s) to image data for mipmap level(s).
1653 data_ptr0
= lp_build_get_mipmap_level(bld
, data_array
, ilevel0
);
1654 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1655 data_ptr1
= lp_build_get_mipmap_level(bld
, data_array
, ilevel1
);
1659 * Get/interpolate texture colors.
1661 if (min_filter
== mag_filter
) {
1662 /* no need to distinquish between minification and magnification */
1663 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
, s
, t
, r
, lod_fpart
,
1664 width0_vec
, width1_vec
,
1665 height0_vec
, height1_vec
,
1666 depth0_vec
, depth1_vec
,
1667 row_stride0_vec
, row_stride1_vec
,
1668 img_stride0_vec
, img_stride1_vec
,
1669 data_ptr0
, data_ptr1
,
1673 /* Emit conditional to choose min image filter or mag image filter
1674 * depending on the lod being >0 or <= 0, respectively.
1676 struct lp_build_flow_context
*flow_ctx
;
1677 struct lp_build_if_state if_ctx
;
1678 LLVMValueRef minify
;
1680 flow_ctx
= lp_build_flow_create(bld
->builder
);
1681 lp_build_flow_scope_begin(flow_ctx
);
1683 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[0]);
1684 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[1]);
1685 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[2]);
1686 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[3]);
1688 /* minify = lod > 0.0 */
1689 minify
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
1690 lod
, float_bld
->zero
, "");
1692 lp_build_if(&if_ctx
, flow_ctx
, bld
->builder
, minify
);
1694 /* Use the minification filter */
1695 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
,
1697 width0_vec
, width1_vec
,
1698 height0_vec
, height1_vec
,
1699 depth0_vec
, depth1_vec
,
1700 row_stride0_vec
, row_stride1_vec
,
1701 img_stride0_vec
, img_stride1_vec
,
1702 data_ptr0
, data_ptr1
,
1705 lp_build_else(&if_ctx
);
1707 /* Use the magnification filter */
1708 lp_build_sample_mipmap(bld
, mag_filter
, mip_filter
,
1710 width0_vec
, width1_vec
,
1711 height0_vec
, height1_vec
,
1712 depth0_vec
, depth1_vec
,
1713 row_stride0_vec
, row_stride1_vec
,
1714 img_stride0_vec
, img_stride1_vec
,
1715 data_ptr0
, data_ptr1
,
1718 lp_build_endif(&if_ctx
);
1720 lp_build_flow_scope_end(flow_ctx
);
1721 lp_build_flow_destroy(flow_ctx
);
1728 lp_build_sample_2d_linear_aos(struct lp_build_sample_context
*bld
,
1732 LLVMValueRef height
,
1733 LLVMValueRef stride_array
,
1734 LLVMValueRef data_array
,
1735 LLVMValueRef texel_out
[4])
1737 LLVMBuilderRef builder
= bld
->builder
;
1738 struct lp_build_context i32
, h16
, u8n
;
1739 LLVMTypeRef i32_vec_type
, h16_vec_type
, u8n_vec_type
;
1740 LLVMValueRef i32_c8
, i32_c128
, i32_c255
;
1741 LLVMValueRef s_ipart
, s_fpart
, s_fpart_lo
, s_fpart_hi
;
1742 LLVMValueRef t_ipart
, t_fpart
, t_fpart_lo
, t_fpart_hi
;
1743 LLVMValueRef x0
, x1
;
1744 LLVMValueRef y0
, y1
;
1745 LLVMValueRef neighbors
[2][2];
1746 LLVMValueRef neighbors_lo
[2][2];
1747 LLVMValueRef neighbors_hi
[2][2];
1748 LLVMValueRef packed
, packed_lo
, packed_hi
;
1749 LLVMValueRef unswizzled
[4];
1750 LLVMValueRef stride
;
1752 assert(bld
->static_state
->target
== PIPE_TEXTURE_2D
);
1753 assert(bld
->static_state
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
);
1754 assert(bld
->static_state
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
);
1755 assert(bld
->static_state
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
);
1757 lp_build_context_init(&i32
, builder
, lp_type_int_vec(32));
1758 lp_build_context_init(&h16
, builder
, lp_type_ufixed(16));
1759 lp_build_context_init(&u8n
, builder
, lp_type_unorm(8));
1761 i32_vec_type
= lp_build_vec_type(i32
.type
);
1762 h16_vec_type
= lp_build_vec_type(h16
.type
);
1763 u8n_vec_type
= lp_build_vec_type(u8n
.type
);
1765 if (bld
->static_state
->normalized_coords
) {
1766 LLVMTypeRef coord_vec_type
= lp_build_vec_type(bld
->coord_type
);
1767 LLVMValueRef fp_width
= LLVMBuildSIToFP(bld
->builder
, width
, coord_vec_type
, "");
1768 LLVMValueRef fp_height
= LLVMBuildSIToFP(bld
->builder
, height
, coord_vec_type
, "");
1769 s
= lp_build_mul(&bld
->coord_bld
, s
, fp_width
);
1770 t
= lp_build_mul(&bld
->coord_bld
, t
, fp_height
);
1773 /* scale coords by 256 (8 fractional bits) */
1774 s
= lp_build_mul_imm(&bld
->coord_bld
, s
, 256);
1775 t
= lp_build_mul_imm(&bld
->coord_bld
, t
, 256);
1777 /* convert float to int */
1778 s
= LLVMBuildFPToSI(builder
, s
, i32_vec_type
, "");
1779 t
= LLVMBuildFPToSI(builder
, t
, i32_vec_type
, "");
1781 /* subtract 0.5 (add -128) */
1782 i32_c128
= lp_build_const_int_vec(i32
.type
, -128);
1783 s
= LLVMBuildAdd(builder
, s
, i32_c128
, "");
1784 t
= LLVMBuildAdd(builder
, t
, i32_c128
, "");
1786 /* compute floor (shift right 8) */
1787 i32_c8
= lp_build_const_int_vec(i32
.type
, 8);
1788 s_ipart
= LLVMBuildAShr(builder
, s
, i32_c8
, "");
1789 t_ipart
= LLVMBuildAShr(builder
, t
, i32_c8
, "");
1791 /* compute fractional part (AND with 0xff) */
1792 i32_c255
= lp_build_const_int_vec(i32
.type
, 255);
1793 s_fpart
= LLVMBuildAnd(builder
, s
, i32_c255
, "");
1794 t_fpart
= LLVMBuildAnd(builder
, t
, i32_c255
, "");
1799 x1
= lp_build_add(&bld
->int_coord_bld
, x0
, bld
->int_coord_bld
.one
);
1800 y1
= lp_build_add(&bld
->int_coord_bld
, y0
, bld
->int_coord_bld
.one
);
1802 x0
= lp_build_sample_wrap_int(bld
, x0
, width
, bld
->static_state
->pot_width
,
1803 bld
->static_state
->wrap_s
);
1804 y0
= lp_build_sample_wrap_int(bld
, y0
, height
, bld
->static_state
->pot_height
,
1805 bld
->static_state
->wrap_t
);
1807 x1
= lp_build_sample_wrap_int(bld
, x1
, width
, bld
->static_state
->pot_width
,
1808 bld
->static_state
->wrap_s
);
1809 y1
= lp_build_sample_wrap_int(bld
, y1
, height
, bld
->static_state
->pot_height
,
1810 bld
->static_state
->wrap_t
);
1813 * Transform 4 x i32 in
1815 * s_fpart = {s0, s1, s2, s3}
1819 * s_fpart = {00, s0, 00, s1, 00, s2, 00, s3}
1823 * s_fpart_lo = {s0, s0, s0, s0, s1, s1, s1, s1}
1824 * s_fpart_hi = {s2, s2, s2, s2, s3, s3, s3, s3}
1826 * and likewise for t_fpart. There is no risk of loosing precision here
1827 * since the fractional parts only use the lower 8bits.
1830 s_fpart
= LLVMBuildBitCast(builder
, s_fpart
, h16_vec_type
, "");
1831 t_fpart
= LLVMBuildBitCast(builder
, t_fpart
, h16_vec_type
, "");
1834 LLVMTypeRef elem_type
= LLVMInt32Type();
1835 LLVMValueRef shuffles_lo
[LP_MAX_VECTOR_LENGTH
];
1836 LLVMValueRef shuffles_hi
[LP_MAX_VECTOR_LENGTH
];
1837 LLVMValueRef shuffle_lo
;
1838 LLVMValueRef shuffle_hi
;
1841 for(j
= 0; j
< h16
.type
.length
; j
+= 4) {
1842 #ifdef PIPE_ARCH_LITTLE_ENDIAN
1843 unsigned subindex
= 0;
1845 unsigned subindex
= 1;
1849 index
= LLVMConstInt(elem_type
, j
/2 + subindex
, 0);
1850 for(i
= 0; i
< 4; ++i
)
1851 shuffles_lo
[j
+ i
] = index
;
1853 index
= LLVMConstInt(elem_type
, h16
.type
.length
/2 + j
/2 + subindex
, 0);
1854 for(i
= 0; i
< 4; ++i
)
1855 shuffles_hi
[j
+ i
] = index
;
1858 shuffle_lo
= LLVMConstVector(shuffles_lo
, h16
.type
.length
);
1859 shuffle_hi
= LLVMConstVector(shuffles_hi
, h16
.type
.length
);
1861 s_fpart_lo
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_lo
, "");
1862 t_fpart_lo
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_lo
, "");
1863 s_fpart_hi
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_hi
, "");
1864 t_fpart_hi
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_hi
, "");
1867 stride
= lp_build_get_const_level_stride_vec(bld
, stride_array
, 0);
1870 * Fetch the pixels as 4 x 32bit (rgba order might differ):
1872 * rgba0 rgba1 rgba2 rgba3
1874 * bit cast them into 16 x u8
1876 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
1878 * unpack them into two 8 x i16:
1880 * r0 g0 b0 a0 r1 g1 b1 a1
1881 * r2 g2 b2 a2 r3 g3 b3 a3
1883 * The higher 8 bits of the resulting elements will be zero.
1886 neighbors
[0][0] = lp_build_sample_packed(bld
, x0
, y0
, stride
, data_array
);
1887 neighbors
[0][1] = lp_build_sample_packed(bld
, x1
, y0
, stride
, data_array
);
1888 neighbors
[1][0] = lp_build_sample_packed(bld
, x0
, y1
, stride
, data_array
);
1889 neighbors
[1][1] = lp_build_sample_packed(bld
, x1
, y1
, stride
, data_array
);
1891 neighbors
[0][0] = LLVMBuildBitCast(builder
, neighbors
[0][0], u8n_vec_type
, "");
1892 neighbors
[0][1] = LLVMBuildBitCast(builder
, neighbors
[0][1], u8n_vec_type
, "");
1893 neighbors
[1][0] = LLVMBuildBitCast(builder
, neighbors
[1][0], u8n_vec_type
, "");
1894 neighbors
[1][1] = LLVMBuildBitCast(builder
, neighbors
[1][1], u8n_vec_type
, "");
1896 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][0], &neighbors_lo
[0][0], &neighbors_hi
[0][0]);
1897 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][1], &neighbors_lo
[0][1], &neighbors_hi
[0][1]);
1898 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][0], &neighbors_lo
[1][0], &neighbors_hi
[1][0]);
1899 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][1], &neighbors_lo
[1][1], &neighbors_hi
[1][1]);
1902 * Linear interpolate with 8.8 fixed point.
1905 packed_lo
= lp_build_lerp_2d(&h16
,
1906 s_fpart_lo
, t_fpart_lo
,
1910 neighbors_lo
[1][1]);
1912 packed_hi
= lp_build_lerp_2d(&h16
,
1913 s_fpart_hi
, t_fpart_hi
,
1917 neighbors_hi
[1][1]);
1919 packed
= lp_build_pack2(builder
, h16
.type
, u8n
.type
, packed_lo
, packed_hi
);
1922 * Convert to SoA and swizzle.
1925 lp_build_rgba8_to_f32_soa(bld
->builder
,
1927 packed
, unswizzled
);
1929 if (util_format_is_rgba8_variant(bld
->format_desc
)) {
1930 lp_build_format_swizzle_soa(bld
->format_desc
,
1932 unswizzled
, texel_out
);
1934 texel_out
[0] = unswizzled
[0];
1935 texel_out
[1] = unswizzled
[1];
1936 texel_out
[2] = unswizzled
[2];
1937 texel_out
[3] = unswizzled
[3];
1940 apply_sampler_swizzle(bld
, texel_out
);
1945 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
1947 LLVMValueRef texel
[4])
1949 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1953 if(bld
->static_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
1956 /* TODO: Compare before swizzling, to avoid redundant computations */
1958 for(chan
= 0; chan
< 4; ++chan
) {
1960 cmp
= lp_build_cmp(texel_bld
, bld
->static_state
->compare_func
, p
, texel
[chan
]);
1961 cmp
= lp_build_select(texel_bld
, cmp
, texel_bld
->one
, texel_bld
->zero
);
1964 res
= lp_build_add(texel_bld
, res
, cmp
);
1970 res
= lp_build_mul(texel_bld
, res
, lp_build_const_vec(texel_bld
->type
, 0.25));
1972 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1973 for(chan
= 0; chan
< 3; ++chan
)
1975 texel
[3] = texel_bld
->one
;
1980 * Just set texels to white instead of actually sampling the texture.
1984 lp_build_sample_nop(struct lp_build_sample_context
*bld
,
1985 LLVMValueRef texel_out
[4])
1987 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1990 for (chan
= 0; chan
< 4; chan
++) {
1991 /*lp_bld_mov(texel_bld, texel, texel_bld->one);*/
1992 texel_out
[chan
] = texel_bld
->one
;
1998 * Build texture sampling code.
1999 * 'texel' will return a vector of four LLVMValueRefs corresponding to
2001 * \param type vector float type to use for coords, etc.
2002 * \param ddx partial derivatives of (s,t,r,q) with respect to x
2003 * \param ddy partial derivatives of (s,t,r,q) with respect to y
2006 lp_build_sample_soa(LLVMBuilderRef builder
,
2007 const struct lp_sampler_static_state
*static_state
,
2008 struct lp_sampler_dynamic_state
*dynamic_state
,
2009 struct lp_type type
,
2011 unsigned num_coords
,
2012 const LLVMValueRef
*coords
,
2013 const LLVMValueRef ddx
[4],
2014 const LLVMValueRef ddy
[4],
2015 LLVMValueRef lod_bias
, /* optional */
2016 LLVMValueRef explicit_lod
, /* optional */
2017 LLVMValueRef texel_out
[4])
2019 struct lp_build_sample_context bld
;
2020 LLVMValueRef width
, width_vec
;
2021 LLVMValueRef height
, height_vec
;
2022 LLVMValueRef depth
, depth_vec
;
2023 LLVMValueRef row_stride_array
, img_stride_array
;
2024 LLVMValueRef data_array
;
2030 enum pipe_format fmt
= static_state
->format
;
2031 debug_printf("Sample from %s\n", util_format_name(fmt
));
2034 assert(type
.floating
);
2036 /* Setup our build context */
2037 memset(&bld
, 0, sizeof bld
);
2038 bld
.builder
= builder
;
2039 bld
.static_state
= static_state
;
2040 bld
.dynamic_state
= dynamic_state
;
2041 bld
.format_desc
= util_format_description(static_state
->format
);
2043 bld
.float_type
= lp_type_float(32);
2044 bld
.int_type
= lp_type_int(32);
2045 bld
.coord_type
= type
;
2046 bld
.uint_coord_type
= lp_uint_type(type
);
2047 bld
.int_coord_type
= lp_int_type(type
);
2048 bld
.texel_type
= type
;
2050 lp_build_context_init(&bld
.float_bld
, builder
, bld
.float_type
);
2051 lp_build_context_init(&bld
.int_bld
, builder
, bld
.int_type
);
2052 lp_build_context_init(&bld
.coord_bld
, builder
, bld
.coord_type
);
2053 lp_build_context_init(&bld
.uint_coord_bld
, builder
, bld
.uint_coord_type
);
2054 lp_build_context_init(&bld
.int_coord_bld
, builder
, bld
.int_coord_type
);
2055 lp_build_context_init(&bld
.texel_bld
, builder
, bld
.texel_type
);
2057 /* Get the dynamic state */
2058 width
= dynamic_state
->width(dynamic_state
, builder
, unit
);
2059 height
= dynamic_state
->height(dynamic_state
, builder
, unit
);
2060 depth
= dynamic_state
->depth(dynamic_state
, builder
, unit
);
2061 row_stride_array
= dynamic_state
->row_stride(dynamic_state
, builder
, unit
);
2062 img_stride_array
= dynamic_state
->img_stride(dynamic_state
, builder
, unit
);
2063 data_array
= dynamic_state
->data_ptr(dynamic_state
, builder
, unit
);
2064 /* Note that data_array is an array[level] of pointers to texture images */
2070 width_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, width
);
2071 height_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, height
);
2072 depth_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, depth
);
2075 /* For debug: no-op texture sampling */
2076 lp_build_sample_nop(&bld
, texel_out
);
2078 else if (util_format_fits_8unorm(bld
.format_desc
) &&
2079 bld
.format_desc
->nr_channels
> 1 &&
2080 static_state
->target
== PIPE_TEXTURE_2D
&&
2081 static_state
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
2082 static_state
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
2083 static_state
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
&&
2084 is_simple_wrap_mode(static_state
->wrap_s
) &&
2085 is_simple_wrap_mode(static_state
->wrap_t
)) {
2087 lp_build_sample_2d_linear_aos(&bld
, s
, t
, width_vec
, height_vec
,
2088 row_stride_array
, data_array
, texel_out
);
2091 lp_build_sample_general(&bld
, unit
, s
, t
, r
, ddx
, ddy
,
2092 lod_bias
, explicit_lod
,
2093 width
, height
, depth
,
2094 width_vec
, height_vec
, depth_vec
,
2095 row_stride_array
, img_stride_array
,
2100 lp_build_sample_compare(&bld
, r
, texel_out
);