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>
35 #include "pipe/p_defines.h"
36 #include "pipe/p_state.h"
37 #include "util/u_debug.h"
38 #include "util/u_dump.h"
39 #include "util/u_memory.h"
40 #include "util/u_math.h"
41 #include "util/u_format.h"
42 #include "util/u_cpu_detect.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_format.h"
53 #include "lp_bld_sample.h"
57 * Keep all information for sampling code generation in a single place.
59 struct lp_build_sample_context
61 LLVMBuilderRef builder
;
63 const struct lp_sampler_static_state
*static_state
;
65 struct lp_sampler_dynamic_state
*dynamic_state
;
67 const struct util_format_description
*format_desc
;
69 /** regular scalar float type */
70 struct lp_type float_type
;
71 struct lp_build_context float_bld
;
73 /** regular scalar float type */
74 struct lp_type int_type
;
75 struct lp_build_context int_bld
;
77 /** Incoming coordinates type and build context */
78 struct lp_type coord_type
;
79 struct lp_build_context coord_bld
;
81 /** Unsigned integer coordinates */
82 struct lp_type uint_coord_type
;
83 struct lp_build_context uint_coord_bld
;
85 /** Signed integer coordinates */
86 struct lp_type int_coord_type
;
87 struct lp_build_context int_coord_bld
;
89 /** Output texels type and build context */
90 struct lp_type texel_type
;
91 struct lp_build_context texel_bld
;
96 * Does the given texture wrap mode allow sampling the texture border color?
97 * XXX maybe move this into gallium util code.
100 wrap_mode_uses_border_color(unsigned mode
)
103 case PIPE_TEX_WRAP_REPEAT
:
104 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
105 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
106 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
108 case PIPE_TEX_WRAP_CLAMP
:
109 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
110 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
111 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
114 assert(0 && "unexpected wrap mode");
121 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
122 LLVMValueRef data_array
, LLVMValueRef level
)
124 LLVMValueRef indexes
[2], data_ptr
;
125 indexes
[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
127 data_ptr
= LLVMBuildGEP(bld
->builder
, data_array
, indexes
, 2, "");
128 data_ptr
= LLVMBuildLoad(bld
->builder
, data_ptr
, "");
134 lp_build_get_const_mipmap_level(struct lp_build_sample_context
*bld
,
135 LLVMValueRef data_array
, int level
)
137 LLVMValueRef lvl
= LLVMConstInt(LLVMInt32Type(), level
, 0);
138 return lp_build_get_mipmap_level(bld
, data_array
, lvl
);
143 * Dereference stride_array[mipmap_level] array to get a stride.
144 * Return stride as a vector.
147 lp_build_get_level_stride_vec(struct lp_build_sample_context
*bld
,
148 LLVMValueRef stride_array
, LLVMValueRef level
)
150 LLVMValueRef indexes
[2], stride
;
151 indexes
[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
153 stride
= LLVMBuildGEP(bld
->builder
, stride_array
, indexes
, 2, "");
154 stride
= LLVMBuildLoad(bld
->builder
, stride
, "");
155 stride
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, stride
);
160 /** Dereference stride_array[0] array to get a stride (as vector). */
162 lp_build_get_const_level_stride_vec(struct lp_build_sample_context
*bld
,
163 LLVMValueRef stride_array
, int level
)
165 LLVMValueRef lvl
= LLVMConstInt(LLVMInt32Type(), level
, 0);
166 return lp_build_get_level_stride_vec(bld
, stride_array
, lvl
);
171 texture_dims(enum pipe_texture_target tex
)
174 case PIPE_TEXTURE_1D
:
176 case PIPE_TEXTURE_2D
:
177 case PIPE_TEXTURE_CUBE
:
179 case PIPE_TEXTURE_3D
:
182 assert(0 && "bad texture target in texture_dims()");
190 * Generate code to fetch a texel from a texture at int coords (x, y, z).
191 * The computation depends on whether the texture is 1D, 2D or 3D.
192 * The result, texel, will be:
193 * texel[0] = red values
194 * texel[1] = green values
195 * texel[2] = blue values
196 * texel[3] = alpha values
199 lp_build_sample_texel_soa(struct lp_build_sample_context
*bld
,
206 LLVMValueRef y_stride
,
207 LLVMValueRef z_stride
,
208 LLVMValueRef data_ptr
,
211 const int dims
= texture_dims(bld
->static_state
->target
);
212 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
215 LLVMValueRef use_border
= NULL
;
217 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
218 if (wrap_mode_uses_border_color(bld
->static_state
->wrap_s
)) {
220 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
221 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
222 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
225 if (dims
>= 2 && wrap_mode_uses_border_color(bld
->static_state
->wrap_t
)) {
227 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
228 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
230 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
231 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
234 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
238 if (dims
== 3 && wrap_mode_uses_border_color(bld
->static_state
->wrap_r
)) {
240 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
241 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
243 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
244 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
247 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
252 * Note: if we find an app which frequently samples the texture border
253 * we might want to implement a true conditional here to avoid sampling
254 * the texture whenever possible (since that's quite a bit of code).
257 * texel = border_color;
260 * texel = sample_texture(coord);
262 * As it is now, we always sample the texture, then selectively replace
263 * the texel color results with the border color.
266 /* convert x,y,z coords to linear offset from start of texture, in bytes */
267 offset
= lp_build_sample_offset(&bld
->uint_coord_bld
,
269 x
, y
, z
, y_stride
, z_stride
);
271 assert(bld
->format_desc
->block
.width
== 1);
272 assert(bld
->format_desc
->block
.height
== 1);
273 assert(bld
->format_desc
->block
.bits
<= bld
->texel_type
.width
);
275 /* gather the texels from the texture */
276 packed
= lp_build_gather(bld
->builder
,
277 bld
->texel_type
.length
,
278 bld
->format_desc
->block
.bits
,
279 bld
->texel_type
.width
,
282 texel
[0] = texel
[1] = texel
[2] = texel
[3] = NULL
;
284 /* convert texels to float rgba */
285 lp_build_unpack_rgba_soa(bld
->builder
,
291 /* select texel color or border color depending on use_border */
293 for (chan
= 0; chan
< 4; chan
++) {
294 LLVMValueRef border_chan
=
295 lp_build_const_scalar(bld
->texel_type
,
296 bld
->static_state
->border_color
[chan
]);
297 texel
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
298 border_chan
, texel
[chan
]);
305 lp_build_sample_packed(struct lp_build_sample_context
*bld
,
308 LLVMValueRef y_stride
,
309 LLVMValueRef data_array
)
312 LLVMValueRef data_ptr
;
314 offset
= lp_build_sample_offset(&bld
->uint_coord_bld
,
316 x
, y
, NULL
, y_stride
, NULL
);
318 assert(bld
->format_desc
->block
.width
== 1);
319 assert(bld
->format_desc
->block
.height
== 1);
320 assert(bld
->format_desc
->block
.bits
<= bld
->texel_type
.width
);
322 /* get pointer to mipmap level 0 data */
323 data_ptr
= lp_build_get_const_mipmap_level(bld
, data_array
, 0);
325 return lp_build_gather(bld
->builder
,
326 bld
->texel_type
.length
,
327 bld
->format_desc
->block
.bits
,
328 bld
->texel_type
.width
,
334 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
337 lp_build_coord_mirror(struct lp_build_sample_context
*bld
,
340 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
341 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
342 LLVMValueRef fract
, flr
, isOdd
;
344 /* fract = coord - floor(coord) */
345 fract
= lp_build_sub(coord_bld
, coord
, lp_build_floor(coord_bld
, coord
));
347 /* flr = ifloor(coord); */
348 flr
= lp_build_ifloor(coord_bld
, coord
);
350 /* isOdd = flr & 1 */
351 isOdd
= LLVMBuildAnd(bld
->builder
, flr
, int_coord_bld
->one
, "");
353 /* make coord positive or negative depending on isOdd */
354 coord
= lp_build_set_sign(coord_bld
, fract
, isOdd
);
356 /* convert isOdd to float */
357 isOdd
= lp_build_int_to_float(coord_bld
, isOdd
);
359 /* add isOdd to coord */
360 coord
= lp_build_add(coord_bld
, coord
, isOdd
);
367 * We only support a few wrap modes in lp_build_sample_wrap_int() at this time.
368 * Return whether the given mode is supported by that function.
371 is_simple_wrap_mode(unsigned mode
)
374 case PIPE_TEX_WRAP_REPEAT
:
375 case PIPE_TEX_WRAP_CLAMP
:
376 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
378 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
386 * Build LLVM code for texture wrap mode, for scaled integer texcoords.
387 * \param coord the incoming texcoord (s,t,r or q) scaled to the texture size
388 * \param length the texture size along one dimension
389 * \param is_pot if TRUE, length is a power of two
390 * \param wrap_mode one of PIPE_TEX_WRAP_x
393 lp_build_sample_wrap_int(struct lp_build_sample_context
*bld
,
399 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
400 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
401 LLVMValueRef length_minus_one
;
403 length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
406 case PIPE_TEX_WRAP_REPEAT
:
408 coord
= LLVMBuildAnd(bld
->builder
, coord
, length_minus_one
, "");
410 /* Signed remainder won't give the right results for negative
411 * dividends but unsigned remainder does.*/
412 coord
= LLVMBuildURem(bld
->builder
, coord
, length
, "");
415 case PIPE_TEX_WRAP_CLAMP
:
416 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
417 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
418 coord
= lp_build_max(int_coord_bld
, coord
, int_coord_bld
->zero
);
419 coord
= lp_build_min(int_coord_bld
, coord
, length_minus_one
);
422 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
423 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
424 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
425 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
427 _debug_printf("llvmpipe: failed to translate texture wrap mode %s\n",
428 util_dump_tex_wrap(wrap_mode
, TRUE
));
429 coord
= lp_build_max(uint_coord_bld
, coord
, uint_coord_bld
->zero
);
430 coord
= lp_build_min(uint_coord_bld
, coord
, length_minus_one
);
442 * Build LLVM code for texture wrap mode for linear filtering.
443 * \param x0_out returns first integer texcoord
444 * \param x1_out returns second integer texcoord
445 * \param weight_out returns linear interpolation weight
448 lp_build_sample_wrap_linear(struct lp_build_sample_context
*bld
,
453 LLVMValueRef
*x0_out
,
454 LLVMValueRef
*x1_out
,
455 LLVMValueRef
*weight_out
)
457 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
458 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
459 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
460 LLVMValueRef two
= lp_build_const_scalar(coord_bld
->type
, 2.0);
461 LLVMValueRef half
= lp_build_const_scalar(coord_bld
->type
, 0.5);
462 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
463 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
464 LLVMValueRef length_f_minus_one
= lp_build_sub(coord_bld
, length_f
, coord_bld
->one
);
465 LLVMValueRef coord0
, coord1
, weight
;
468 case PIPE_TEX_WRAP_REPEAT
:
469 /* mul by size and subtract 0.5 */
470 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
471 coord
= lp_build_sub(coord_bld
, coord
, half
);
473 coord0
= lp_build_ifloor(coord_bld
, coord
);
474 coord1
= lp_build_add(uint_coord_bld
, coord0
, uint_coord_bld
->one
);
475 /* compute lerp weight */
476 weight
= lp_build_fract(coord_bld
, coord
);
479 coord0
= LLVMBuildAnd(bld
->builder
, coord0
, length_minus_one
, "");
480 coord1
= LLVMBuildAnd(bld
->builder
, coord1
, length_minus_one
, "");
483 /* Signed remainder won't give the right results for negative
484 * dividends but unsigned remainder does.*/
485 coord0
= LLVMBuildURem(bld
->builder
, coord0
, length
, "");
486 coord1
= LLVMBuildURem(bld
->builder
, coord1
, length
, "");
490 case PIPE_TEX_WRAP_CLAMP
:
491 if (bld
->static_state
->normalized_coords
) {
492 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
494 weight
= lp_build_fract(coord_bld
, coord
);
495 coord0
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
,
497 coord1
= lp_build_add(coord_bld
, coord
, coord_bld
->one
);
498 coord1
= lp_build_clamp(coord_bld
, coord1
, coord_bld
->zero
,
500 coord0
= lp_build_ifloor(coord_bld
, coord0
);
501 coord1
= lp_build_ifloor(coord_bld
, coord1
);
504 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
505 if (bld
->static_state
->normalized_coords
) {
507 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, coord_bld
->one
);
508 /* mul by tex size and subtract 0.5 */
509 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
510 coord
= lp_build_sub(coord_bld
, coord
, half
);
513 LLVMValueRef min
, max
;
514 /* clamp to [0.5, length - 0.5] */
515 min
= lp_build_const_scalar(coord_bld
->type
, 0.5F
);
516 max
= lp_build_sub(coord_bld
, length_f
, min
);
517 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
519 /* compute lerp weight */
520 weight
= lp_build_fract(coord_bld
, coord
);
521 /* coord0 = floor(coord); */
522 coord0
= lp_build_ifloor(coord_bld
, coord
);
523 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
524 /* coord0 = max(coord0, 0) */
525 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
526 /* coord1 = min(coord1, length-1) */
527 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
530 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
532 LLVMValueRef min
, max
;
533 if (bld
->static_state
->normalized_coords
) {
534 /* min = -1.0 / (2 * length) = -0.5 / length */
535 min
= lp_build_mul(coord_bld
,
536 lp_build_const_scalar(coord_bld
->type
, -0.5F
),
537 lp_build_rcp(coord_bld
, length_f
));
538 /* max = 1.0 - min */
539 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
540 /* coord = clamp(coord, min, max) */
541 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
542 /* scale coord to length (and sub 0.5?) */
543 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
544 coord
= lp_build_sub(coord_bld
, coord
, half
);
547 /* clamp to [-0.5, length + 0.5] */
548 min
= lp_build_const_scalar(coord_bld
->type
, -0.5F
);
549 max
= lp_build_sub(coord_bld
, length_f
, min
);
550 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
551 coord
= lp_build_sub(coord_bld
, coord
, half
);
553 /* compute lerp weight */
554 weight
= lp_build_fract(coord_bld
, coord
);
556 coord0
= lp_build_ifloor(coord_bld
, coord
);
557 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
561 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
562 /* compute mirror function */
563 coord
= lp_build_coord_mirror(bld
, coord
);
565 /* scale coord to length */
566 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
567 coord
= lp_build_sub(coord_bld
, coord
, half
);
569 /* compute lerp weight */
570 weight
= lp_build_fract(coord_bld
, coord
);
572 /* convert to int coords */
573 coord0
= lp_build_ifloor(coord_bld
, coord
);
574 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
576 /* coord0 = max(coord0, 0) */
577 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
578 /* coord1 = min(coord1, length-1) */
579 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
582 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
584 LLVMValueRef min
, max
;
585 /* min = 1.0 / (2 * length) */
586 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
587 /* max = 1.0 - min */
588 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
590 coord
= lp_build_abs(coord_bld
, coord
);
591 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
592 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
593 if(0)coord
= lp_build_sub(coord_bld
, coord
, half
);
594 weight
= lp_build_fract(coord_bld
, coord
);
595 coord0
= lp_build_ifloor(coord_bld
, coord
);
596 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
600 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
602 LLVMValueRef min
, max
;
603 /* min = 1.0 / (2 * length) */
604 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
605 /* max = 1.0 - min */
606 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
608 coord
= lp_build_abs(coord_bld
, coord
);
609 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
610 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
611 coord
= lp_build_sub(coord_bld
, coord
, half
);
612 weight
= lp_build_fract(coord_bld
, coord
);
613 coord0
= lp_build_ifloor(coord_bld
, coord
);
614 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
618 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
620 LLVMValueRef min
, max
;
621 /* min = -1.0 / (2 * length) = -0.5 / length */
622 min
= lp_build_mul(coord_bld
,
623 lp_build_const_scalar(coord_bld
->type
, -0.5F
),
624 lp_build_rcp(coord_bld
, length_f
));
625 /* max = 1.0 - min */
626 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
628 coord
= lp_build_abs(coord_bld
, coord
);
629 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
630 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
631 coord
= lp_build_sub(coord_bld
, coord
, half
);
632 weight
= lp_build_fract(coord_bld
, coord
);
633 coord0
= lp_build_ifloor(coord_bld
, coord
);
634 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
647 *weight_out
= weight
;
652 * Build LLVM code for texture wrap mode for nearest filtering.
653 * \param coord the incoming texcoord (nominally in [0,1])
654 * \param length the texture size along one dimension, as int
655 * \param is_pot if TRUE, length is a power of two
656 * \param wrap_mode one of PIPE_TEX_WRAP_x
659 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
665 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
666 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
667 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
668 LLVMValueRef two
= lp_build_const_scalar(coord_bld
->type
, 2.0);
669 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
670 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
671 LLVMValueRef length_f_minus_one
= lp_build_sub(coord_bld
, length_f
, coord_bld
->one
);
675 case PIPE_TEX_WRAP_REPEAT
:
676 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
677 icoord
= lp_build_ifloor(coord_bld
, coord
);
679 icoord
= LLVMBuildAnd(bld
->builder
, icoord
, length_minus_one
, "");
681 /* Signed remainder won't give the right results for negative
682 * dividends but unsigned remainder does.*/
683 icoord
= LLVMBuildURem(bld
->builder
, icoord
, length
, "");
686 case PIPE_TEX_WRAP_CLAMP
:
688 if (bld
->static_state
->normalized_coords
) {
689 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
692 icoord
= lp_build_ifloor(coord_bld
, coord
);
693 /* clamp to [0, size-1]. Note: int coord builder type */
694 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
698 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
700 LLVMValueRef min
, max
;
701 if (bld
->static_state
->normalized_coords
) {
702 /* min = 1.0 / (2 * length) */
703 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
704 /* max = length - min */
705 max
= lp_build_sub(coord_bld
, length_f
, min
);
706 /* scale coord to length */
707 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
710 /* clamp to [0.5, length - 0.5] */
711 min
= lp_build_const_scalar(coord_bld
->type
, 0.5F
);
712 max
= lp_build_sub(coord_bld
, length_f
, min
);
714 /* coord = clamp(coord, min, max) */
715 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
716 icoord
= lp_build_ifloor(coord_bld
, coord
);
720 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
721 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
723 LLVMValueRef min
, max
;
724 if (bld
->static_state
->normalized_coords
) {
725 /* min = -1.0 / (2 * length) = -0.5 / length */
726 min
= lp_build_mul(coord_bld
,
727 lp_build_const_scalar(coord_bld
->type
, -0.5F
),
728 lp_build_rcp(coord_bld
, length_f
));
729 /* max = length - min */
730 max
= lp_build_sub(coord_bld
, length_f
, min
);
731 /* scale coord to length */
732 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
735 /* clamp to [-0.5, length + 0.5] */
736 min
= lp_build_const_scalar(coord_bld
->type
, -0.5F
);
737 max
= lp_build_sub(coord_bld
, length_f
, min
);
739 /* coord = clamp(coord, min, max) */
740 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
741 icoord
= lp_build_ifloor(coord_bld
, coord
);
745 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
747 LLVMValueRef min
, max
;
748 /* min = 1.0 / (2 * length) */
749 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
750 /* max = length - min */
751 max
= lp_build_sub(coord_bld
, length_f
, min
);
753 /* compute mirror function */
754 coord
= lp_build_coord_mirror(bld
, coord
);
756 /* scale coord to length */
757 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
759 /* coord = clamp(coord, min, max) */
760 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
761 icoord
= lp_build_ifloor(coord_bld
, coord
);
765 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
766 coord
= lp_build_abs(coord_bld
, coord
);
767 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
768 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, length_f_minus_one
);
769 icoord
= lp_build_ifloor(coord_bld
, coord
);
772 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
774 LLVMValueRef min
, max
;
775 /* min = 1.0 / (2 * length) */
776 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
777 /* max = length - min */
778 max
= lp_build_sub(coord_bld
, length_f
, min
);
780 coord
= lp_build_abs(coord_bld
, coord
);
781 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
782 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
783 icoord
= lp_build_ifloor(coord_bld
, coord
);
787 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
789 LLVMValueRef min
, max
;
790 /* min = 1.0 / (2 * length) */
791 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
792 min
= lp_build_negate(coord_bld
, min
);
793 /* max = length - min */
794 max
= lp_build_sub(coord_bld
, length_f
, min
);
796 coord
= lp_build_abs(coord_bld
, coord
);
797 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
798 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
799 icoord
= lp_build_ifloor(coord_bld
, coord
);
813 * Codegen equivalent for u_minify().
814 * Return max(1, base_size >> level);
817 lp_build_minify(struct lp_build_sample_context
*bld
,
818 LLVMValueRef base_size
,
821 LLVMValueRef size
= LLVMBuildAShr(bld
->builder
, base_size
, level
, "minify");
822 size
= lp_build_max(&bld
->int_coord_bld
, size
, bld
->int_coord_bld
.one
);
828 * Generate code to compute texture level of detail (lambda).
829 * \param s vector of texcoord s values
830 * \param t vector of texcoord t values
831 * \param r vector of texcoord r values
832 * \param width scalar int texture width
833 * \param height scalar int texture height
834 * \param depth scalar int texture depth
837 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
846 const int dims
= texture_dims(bld
->static_state
->target
);
847 struct lp_build_context
*float_bld
= &bld
->float_bld
;
848 LLVMValueRef lod_bias
= LLVMConstReal(LLVMFloatType(), bld
->static_state
->lod_bias
);
849 LLVMValueRef min_lod
= LLVMConstReal(LLVMFloatType(), bld
->static_state
->min_lod
);
850 LLVMValueRef max_lod
= LLVMConstReal(LLVMFloatType(), bld
->static_state
->max_lod
);
852 LLVMValueRef index0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
853 LLVMValueRef index1
= LLVMConstInt(LLVMInt32Type(), 1, 0);
854 LLVMValueRef index2
= LLVMConstInt(LLVMInt32Type(), 2, 0);
856 LLVMValueRef s0
, s1
, s2
;
857 LLVMValueRef t0
, t1
, t2
;
858 LLVMValueRef r0
, r1
, r2
;
859 LLVMValueRef dsdx
, dsdy
, dtdx
, dtdy
, drdx
, drdy
;
860 LLVMValueRef rho
, lod
;
863 * dsdx = abs(s[1] - s[0]);
864 * dsdy = abs(s[2] - s[0]);
865 * dtdx = abs(t[1] - t[0]);
866 * dtdy = abs(t[2] - t[0]);
867 * drdx = abs(r[1] - r[0]);
868 * drdy = abs(r[2] - r[0]);
869 * XXX we're assuming a four-element quad in 2x2 layout here.
871 s0
= LLVMBuildExtractElement(bld
->builder
, s
, index0
, "s0");
872 s1
= LLVMBuildExtractElement(bld
->builder
, s
, index1
, "s1");
873 s2
= LLVMBuildExtractElement(bld
->builder
, s
, index2
, "s2");
874 dsdx
= LLVMBuildSub(bld
->builder
, s1
, s0
, "");
875 dsdx
= lp_build_abs(float_bld
, dsdx
);
876 dsdy
= LLVMBuildSub(bld
->builder
, s2
, s0
, "");
877 dsdy
= lp_build_abs(float_bld
, dsdy
);
879 t0
= LLVMBuildExtractElement(bld
->builder
, t
, index0
, "t0");
880 t1
= LLVMBuildExtractElement(bld
->builder
, t
, index1
, "t1");
881 t2
= LLVMBuildExtractElement(bld
->builder
, t
, index2
, "t2");
882 dtdx
= LLVMBuildSub(bld
->builder
, t1
, t0
, "");
883 dtdx
= lp_build_abs(float_bld
, dtdx
);
884 dtdy
= LLVMBuildSub(bld
->builder
, t2
, t0
, "");
885 dtdy
= lp_build_abs(float_bld
, dtdy
);
887 r0
= LLVMBuildExtractElement(bld
->builder
, r
, index0
, "r0");
888 r1
= LLVMBuildExtractElement(bld
->builder
, r
, index1
, "r1");
889 r2
= LLVMBuildExtractElement(bld
->builder
, r
, index2
, "r2");
890 drdx
= LLVMBuildSub(bld
->builder
, r1
, r0
, "");
891 drdx
= lp_build_abs(float_bld
, drdx
);
892 drdy
= LLVMBuildSub(bld
->builder
, r2
, r0
, "");
893 drdy
= lp_build_abs(float_bld
, drdy
);
897 /* Compute rho = max of all partial derivatives scaled by texture size.
898 * XXX this could be vectorized somewhat
900 rho
= LLVMBuildMul(bld
->builder
,
901 lp_build_max(float_bld
, dsdx
, dsdy
),
902 lp_build_int_to_float(float_bld
, width
), "");
905 max
= LLVMBuildMul(bld
->builder
,
906 lp_build_max(float_bld
, dtdx
, dtdy
),
907 lp_build_int_to_float(float_bld
, height
), "");
908 rho
= lp_build_max(float_bld
, rho
, max
);
910 max
= LLVMBuildMul(bld
->builder
,
911 lp_build_max(float_bld
, drdx
, drdy
),
912 lp_build_int_to_float(float_bld
, depth
), "");
913 rho
= lp_build_max(float_bld
, rho
, max
);
917 /* compute lod = log2(rho) */
918 lod
= lp_build_log2(float_bld
, rho
);
921 lod
= LLVMBuildAdd(bld
->builder
, lod
, lod_bias
, "LOD bias");
924 lod
= lp_build_clamp(float_bld
, lod
, min_lod
, max_lod
);
931 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
932 * mipmap level index.
933 * Note: this is all scalar code.
934 * \param lod scalar float texture level of detail
935 * \param level_out returns integer
938 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
941 LLVMValueRef
*level_out
)
943 struct lp_build_context
*float_bld
= &bld
->float_bld
;
944 struct lp_build_context
*int_bld
= &bld
->int_bld
;
945 LLVMValueRef last_level
, level
;
947 LLVMValueRef zero
= LLVMConstInt(LLVMInt32Type(), 0, 0);
949 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
952 /* convert float lod to integer */
953 level
= lp_build_iround(float_bld
, lod
);
955 /* clamp level to legal range of levels */
956 *level_out
= lp_build_clamp(int_bld
, level
, zero
, last_level
);
961 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
962 * two (adjacent) mipmap level indexes. Later, we'll sample from those
963 * two mipmap levels and interpolate between them.
966 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
969 LLVMValueRef
*level0_out
,
970 LLVMValueRef
*level1_out
,
971 LLVMValueRef
*weight_out
)
973 struct lp_build_context
*float_bld
= &bld
->float_bld
;
974 struct lp_build_context
*int_bld
= &bld
->int_bld
;
975 LLVMValueRef last_level
, level
;
977 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
980 /* convert float lod to integer */
981 level
= lp_build_ifloor(float_bld
, lod
);
983 /* compute level 0 and clamp to legal range of levels */
984 *level0_out
= lp_build_clamp(int_bld
, level
,
987 /* compute level 1 and clamp to legal range of levels */
988 *level1_out
= lp_build_add(int_bld
, *level0_out
, int_bld
->one
);
989 *level1_out
= lp_build_min(int_bld
, *level1_out
, last_level
);
991 *weight_out
= lp_build_fract(float_bld
, lod
);
996 * Generate code to sample a mipmap level with nearest filtering.
997 * If sampling a cube texture, r = cube face in [0,5].
1000 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
1001 LLVMValueRef width_vec
,
1002 LLVMValueRef height_vec
,
1003 LLVMValueRef depth_vec
,
1004 LLVMValueRef row_stride_vec
,
1005 LLVMValueRef img_stride_vec
,
1006 LLVMValueRef data_ptr
,
1010 LLVMValueRef colors_out
[4])
1012 const int dims
= texture_dims(bld
->static_state
->target
);
1013 LLVMValueRef x
, y
, z
;
1016 * Compute integer texcoords.
1018 x
= lp_build_sample_wrap_nearest(bld
, s
, width_vec
,
1019 bld
->static_state
->pot_width
,
1020 bld
->static_state
->wrap_s
);
1021 lp_build_name(x
, "tex.x.wrapped");
1024 y
= lp_build_sample_wrap_nearest(bld
, t
, height_vec
,
1025 bld
->static_state
->pot_height
,
1026 bld
->static_state
->wrap_t
);
1027 lp_build_name(y
, "tex.y.wrapped");
1030 z
= lp_build_sample_wrap_nearest(bld
, r
, depth_vec
,
1031 bld
->static_state
->pot_height
,
1032 bld
->static_state
->wrap_r
);
1033 lp_build_name(z
, "tex.z.wrapped");
1035 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1047 * Get texture colors.
1049 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1051 row_stride_vec
, img_stride_vec
,
1052 data_ptr
, colors_out
);
1057 * Generate code to sample a mipmap level with linear filtering.
1058 * If sampling a cube texture, r = cube face in [0,5].
1061 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
1062 LLVMValueRef width_vec
,
1063 LLVMValueRef height_vec
,
1064 LLVMValueRef depth_vec
,
1065 LLVMValueRef row_stride_vec
,
1066 LLVMValueRef img_stride_vec
,
1067 LLVMValueRef data_ptr
,
1071 LLVMValueRef colors_out
[4])
1073 const int dims
= texture_dims(bld
->static_state
->target
);
1074 LLVMValueRef x0
, y0
, z0
, x1
, y1
, z1
;
1075 LLVMValueRef s_fpart
, t_fpart
, r_fpart
;
1076 LLVMValueRef neighbors
[2][2][4];
1080 * Compute integer texcoords.
1082 lp_build_sample_wrap_linear(bld
, s
, width_vec
,
1083 bld
->static_state
->pot_width
,
1084 bld
->static_state
->wrap_s
,
1085 &x0
, &x1
, &s_fpart
);
1086 lp_build_name(x0
, "tex.x0.wrapped");
1087 lp_build_name(x1
, "tex.x1.wrapped");
1090 lp_build_sample_wrap_linear(bld
, t
, height_vec
,
1091 bld
->static_state
->pot_height
,
1092 bld
->static_state
->wrap_t
,
1093 &y0
, &y1
, &t_fpart
);
1094 lp_build_name(y0
, "tex.y0.wrapped");
1095 lp_build_name(y1
, "tex.y1.wrapped");
1098 lp_build_sample_wrap_linear(bld
, r
, depth_vec
,
1099 bld
->static_state
->pot_depth
,
1100 bld
->static_state
->wrap_r
,
1101 &z0
, &z1
, &r_fpart
);
1102 lp_build_name(z0
, "tex.z0.wrapped");
1103 lp_build_name(z1
, "tex.z1.wrapped");
1105 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1106 z0
= z1
= r
; /* cube face */
1115 y0
= y1
= t_fpart
= NULL
;
1116 z0
= z1
= r_fpart
= NULL
;
1120 * Get texture colors.
1122 /* get x0/x1 texels */
1123 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1125 row_stride_vec
, img_stride_vec
,
1126 data_ptr
, neighbors
[0][0]);
1127 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1129 row_stride_vec
, img_stride_vec
,
1130 data_ptr
, neighbors
[0][1]);
1133 /* Interpolate two samples from 1D image to produce one color */
1134 for (chan
= 0; chan
< 4; chan
++) {
1135 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, s_fpart
,
1136 neighbors
[0][0][chan
],
1137 neighbors
[0][1][chan
]);
1142 LLVMValueRef colors0
[4];
1144 /* get x0/x1 texels at y1 */
1145 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1147 row_stride_vec
, img_stride_vec
,
1148 data_ptr
, neighbors
[1][0]);
1149 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1151 row_stride_vec
, img_stride_vec
,
1152 data_ptr
, neighbors
[1][1]);
1154 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
1155 for (chan
= 0; chan
< 4; chan
++) {
1156 colors0
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1158 neighbors
[0][0][chan
],
1159 neighbors
[0][1][chan
],
1160 neighbors
[1][0][chan
],
1161 neighbors
[1][1][chan
]);
1165 LLVMValueRef neighbors1
[2][2][4];
1166 LLVMValueRef colors1
[4];
1168 /* get x0/x1/y0/y1 texels at z1 */
1169 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1171 row_stride_vec
, img_stride_vec
,
1172 data_ptr
, neighbors1
[0][0]);
1173 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1175 row_stride_vec
, img_stride_vec
,
1176 data_ptr
, neighbors1
[0][1]);
1177 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1179 row_stride_vec
, img_stride_vec
,
1180 data_ptr
, neighbors1
[1][0]);
1181 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1183 row_stride_vec
, img_stride_vec
,
1184 data_ptr
, neighbors1
[1][1]);
1186 /* Bilinear interpolate the four samples from the second Z slice */
1187 for (chan
= 0; chan
< 4; chan
++) {
1188 colors1
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1190 neighbors1
[0][0][chan
],
1191 neighbors1
[0][1][chan
],
1192 neighbors1
[1][0][chan
],
1193 neighbors1
[1][1][chan
]);
1196 /* Linearly interpolate the two samples from the two 3D slices */
1197 for (chan
= 0; chan
< 4; chan
++) {
1198 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
,
1200 colors0
[chan
], colors1
[chan
]);
1205 for (chan
= 0; chan
< 4; chan
++) {
1206 colors_out
[chan
] = colors0
[chan
];
1213 /** Helper used by lp_build_cube_lookup() */
1215 lp_build_cube_ima(struct lp_build_context
*coord_bld
, LLVMValueRef coord
)
1217 /* ima = -0.5 / abs(coord); */
1218 LLVMValueRef negHalf
= lp_build_const_scalar(coord_bld
->type
, -0.5);
1219 LLVMValueRef absCoord
= lp_build_abs(coord_bld
, coord
);
1220 LLVMValueRef ima
= lp_build_mul(coord_bld
, negHalf
,
1221 lp_build_rcp(coord_bld
, 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_scalar(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 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
);
1408 lp_build_endif(&if_ctx
);
1409 lp_build_flow_scope_end(flow_ctx
);
1410 lp_build_flow_destroy(flow_ctx
);
1417 * Sample the texture/mipmap using given image filter and mip filter.
1418 * data0_ptr and data1_ptr point to the two mipmap levels to sample
1419 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
1420 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1423 lp_sample_mipmap(struct lp_build_sample_context
*bld
,
1424 unsigned img_filter
,
1425 unsigned mip_filter
,
1429 LLVMValueRef lod_fpart
,
1430 LLVMValueRef width0_vec
,
1431 LLVMValueRef width1_vec
,
1432 LLVMValueRef height0_vec
,
1433 LLVMValueRef height1_vec
,
1434 LLVMValueRef depth0_vec
,
1435 LLVMValueRef depth1_vec
,
1436 LLVMValueRef row_stride0_vec
,
1437 LLVMValueRef row_stride1_vec
,
1438 LLVMValueRef img_stride0_vec
,
1439 LLVMValueRef img_stride1_vec
,
1440 LLVMValueRef data_ptr0
,
1441 LLVMValueRef data_ptr1
,
1442 LLVMValueRef
*colors_out
)
1444 LLVMValueRef colors0
[4], colors1
[4];
1447 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
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, and interp */
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 lp_build_sample_image_linear(bld
,
1465 width0_vec
, height0_vec
, depth0_vec
,
1466 row_stride0_vec
, img_stride0_vec
,
1467 data_ptr0
, s
, t
, r
, colors0
);
1469 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1470 /* sample the second mipmap level, and interp */
1471 lp_build_sample_image_linear(bld
,
1472 width1_vec
, height1_vec
, depth1_vec
,
1473 row_stride1_vec
, img_stride1_vec
,
1474 data_ptr1
, s
, t
, r
, colors1
);
1478 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1479 /* interpolate samples from the two mipmap levels */
1480 for (chan
= 0; chan
< 4; chan
++) {
1481 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
1482 colors0
[chan
], colors1
[chan
]);
1486 /* use first/only level's colors */
1487 for (chan
= 0; chan
< 4; chan
++) {
1488 colors_out
[chan
] = colors0
[chan
];
1496 * General texture sampling codegen.
1497 * This function handles texture sampling for all texture targets (1D,
1498 * 2D, 3D, cube) and all filtering modes.
1501 lp_build_sample_general(struct lp_build_sample_context
*bld
,
1507 LLVMValueRef height
,
1509 LLVMValueRef width_vec
,
1510 LLVMValueRef height_vec
,
1511 LLVMValueRef depth_vec
,
1512 LLVMValueRef row_stride_array
,
1513 LLVMValueRef img_stride_vec
,
1514 LLVMValueRef data_array
,
1515 LLVMValueRef
*colors_out
)
1517 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1518 const unsigned mip_filter
= bld
->static_state
->min_mip_filter
;
1519 const unsigned min_filter
= bld
->static_state
->min_img_filter
;
1520 const unsigned mag_filter
= bld
->static_state
->mag_img_filter
;
1521 const int dims
= texture_dims(bld
->static_state
->target
);
1522 LLVMValueRef lod
, lod_fpart
;
1523 LLVMValueRef ilevel0
, ilevel1
, ilevel0_vec
, ilevel1_vec
;
1524 LLVMValueRef width0_vec
= NULL
, height0_vec
= NULL
, depth0_vec
= NULL
;
1525 LLVMValueRef width1_vec
= NULL
, height1_vec
= NULL
, depth1_vec
= NULL
;
1526 LLVMValueRef row_stride0_vec
= NULL
, row_stride1_vec
= NULL
;
1527 LLVMValueRef img_stride0_vec
= NULL
, img_stride1_vec
= NULL
;
1528 LLVMValueRef data_ptr0
, data_ptr1
;
1531 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1532 mip_filter, min_filter, mag_filter);
1536 * Compute the level of detail (mipmap level index(es)).
1538 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
1539 /* always use mip level 0 */
1540 ilevel0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
1542 /* XXX temporary here */
1543 lod
= lp_build_lod_selector(bld
, s
, t
, r
, width
, height
, depth
);
1547 /* compute float LOD */
1548 lod
= lp_build_lod_selector(bld
, s
, t
, r
, width
, height
, depth
);
1550 if (mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
1551 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
1554 assert(mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
);
1555 lp_build_linear_mip_levels(bld
, unit
, lod
, &ilevel0
, &ilevel1
,
1557 lod_fpart
= lp_build_broadcast_scalar(&bld
->coord_bld
, lod_fpart
);
1562 * Convert scalar integer mipmap levels into vectors.
1564 ilevel0_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel0
);
1565 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
)
1566 ilevel1_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel1
);
1569 * Compute width, height at mipmap level 'ilevel0'
1571 width0_vec
= lp_build_minify(bld
, width_vec
, ilevel0_vec
);
1573 height0_vec
= lp_build_minify(bld
, height_vec
, ilevel0_vec
);
1574 row_stride0_vec
= lp_build_get_level_stride_vec(bld
, row_stride_array
,
1576 if (dims
== 3 || bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1577 img_stride0_vec
= lp_build_mul(&bld
->int_coord_bld
,
1578 row_stride0_vec
, height0_vec
);
1580 depth0_vec
= lp_build_minify(bld
, depth_vec
, ilevel0_vec
);
1584 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1585 /* compute width, height, depth for second mipmap level at ilevel1 */
1586 width1_vec
= lp_build_minify(bld
, width_vec
, ilevel1_vec
);
1588 height1_vec
= lp_build_minify(bld
, height_vec
, ilevel1_vec
);
1589 row_stride1_vec
= lp_build_get_level_stride_vec(bld
, row_stride_array
,
1591 if (dims
== 3 || bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1592 img_stride1_vec
= lp_build_mul(&bld
->int_coord_bld
,
1593 row_stride1_vec
, height1_vec
);
1595 depth1_vec
= lp_build_minify(bld
, depth_vec
, ilevel1_vec
);
1602 * Choose cube face, recompute texcoords.
1604 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1605 LLVMValueRef face
, face_s
, face_t
;
1606 lp_build_cube_lookup(bld
, s
, t
, r
, &face
, &face_s
, &face_t
);
1607 s
= face_s
; /* vec */
1608 t
= face_t
; /* vec */
1609 /* use 'r' to indicate cube face */
1610 r
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, face
); /* vec */
1614 * Get pointer(s) to image data for mipmap level(s).
1616 data_ptr0
= lp_build_get_mipmap_level(bld
, data_array
, ilevel0
);
1617 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1618 data_ptr1
= lp_build_get_mipmap_level(bld
, data_array
, ilevel1
);
1622 * Get/interpolate texture colors.
1624 if (min_filter
== mag_filter
) {
1625 /* no need to distinquish between minification and magnification */
1626 lp_sample_mipmap(bld
, min_filter
, mip_filter
, s
, t
, r
, lod_fpart
,
1627 width0_vec
, width1_vec
,
1628 height0_vec
, height1_vec
,
1629 depth0_vec
, depth1_vec
,
1630 row_stride0_vec
, row_stride1_vec
,
1631 img_stride0_vec
, img_stride1_vec
,
1632 data_ptr0
, data_ptr1
,
1636 /* Emit conditional to choose min image filter or mag image filter
1637 * depending on the lod being >0 or <= 0, respectively.
1639 struct lp_build_flow_context
*flow_ctx
;
1640 struct lp_build_if_state if_ctx
;
1641 LLVMValueRef minify
;
1643 flow_ctx
= lp_build_flow_create(bld
->builder
);
1644 lp_build_flow_scope_begin(flow_ctx
);
1646 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[0]);
1647 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[1]);
1648 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[2]);
1649 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[3]);
1651 /* minify = lod > 0.0 */
1652 minify
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
1653 lod
, float_bld
->zero
, "");
1655 lp_build_if(&if_ctx
, flow_ctx
, bld
->builder
, minify
);
1657 /* Use the minification filter */
1658 lp_sample_mipmap(bld
, min_filter
, mip_filter
, s
, t
, r
, lod_fpart
,
1659 width0_vec
, width1_vec
,
1660 height0_vec
, height1_vec
,
1661 depth0_vec
, depth1_vec
,
1662 row_stride0_vec
, row_stride1_vec
,
1663 img_stride0_vec
, img_stride1_vec
,
1664 data_ptr0
, data_ptr1
,
1667 lp_build_else(&if_ctx
);
1669 /* Use the magnification filter */
1670 lp_sample_mipmap(bld
, mag_filter
, mip_filter
, s
, t
, r
, lod_fpart
,
1671 width0_vec
, width1_vec
,
1672 height0_vec
, height1_vec
,
1673 depth0_vec
, depth1_vec
,
1674 row_stride0_vec
, row_stride1_vec
,
1675 img_stride0_vec
, img_stride1_vec
,
1676 data_ptr0
, data_ptr1
,
1679 lp_build_endif(&if_ctx
);
1681 lp_build_flow_scope_end(flow_ctx
);
1682 lp_build_flow_destroy(flow_ctx
);
1689 lp_build_rgba8_to_f32_soa(LLVMBuilderRef builder
,
1690 struct lp_type dst_type
,
1691 LLVMValueRef packed
,
1694 LLVMValueRef mask
= lp_build_int_const_scalar(dst_type
, 0xff);
1697 /* Decode the input vector components */
1698 for (chan
= 0; chan
< 4; ++chan
) {
1699 unsigned start
= chan
*8;
1700 unsigned stop
= start
+ 8;
1706 input
= LLVMBuildLShr(builder
, input
, lp_build_int_const_scalar(dst_type
, start
), "");
1709 input
= LLVMBuildAnd(builder
, input
, mask
, "");
1711 input
= lp_build_unsigned_norm_to_float(builder
, 8, dst_type
, input
);
1719 lp_build_sample_2d_linear_aos(struct lp_build_sample_context
*bld
,
1723 LLVMValueRef height
,
1724 LLVMValueRef stride_array
,
1725 LLVMValueRef data_array
,
1726 LLVMValueRef
*texel
)
1728 LLVMBuilderRef builder
= bld
->builder
;
1729 struct lp_build_context i32
, h16
, u8n
;
1730 LLVMTypeRef i32_vec_type
, h16_vec_type
, u8n_vec_type
;
1731 LLVMValueRef i32_c8
, i32_c128
, i32_c255
;
1732 LLVMValueRef s_ipart
, s_fpart
, s_fpart_lo
, s_fpart_hi
;
1733 LLVMValueRef t_ipart
, t_fpart
, t_fpart_lo
, t_fpart_hi
;
1734 LLVMValueRef x0
, x1
;
1735 LLVMValueRef y0
, y1
;
1736 LLVMValueRef neighbors
[2][2];
1737 LLVMValueRef neighbors_lo
[2][2];
1738 LLVMValueRef neighbors_hi
[2][2];
1739 LLVMValueRef packed
, packed_lo
, packed_hi
;
1740 LLVMValueRef unswizzled
[4];
1741 LLVMValueRef stride
;
1743 lp_build_context_init(&i32
, builder
, lp_type_int_vec(32));
1744 lp_build_context_init(&h16
, builder
, lp_type_ufixed(16));
1745 lp_build_context_init(&u8n
, builder
, lp_type_unorm(8));
1747 i32_vec_type
= lp_build_vec_type(i32
.type
);
1748 h16_vec_type
= lp_build_vec_type(h16
.type
);
1749 u8n_vec_type
= lp_build_vec_type(u8n
.type
);
1751 if (bld
->static_state
->normalized_coords
) {
1752 LLVMTypeRef coord_vec_type
= lp_build_vec_type(bld
->coord_type
);
1753 LLVMValueRef fp_width
= LLVMBuildSIToFP(bld
->builder
, width
, coord_vec_type
, "");
1754 LLVMValueRef fp_height
= LLVMBuildSIToFP(bld
->builder
, height
, coord_vec_type
, "");
1755 s
= lp_build_mul(&bld
->coord_bld
, s
, fp_width
);
1756 t
= lp_build_mul(&bld
->coord_bld
, t
, fp_height
);
1759 /* scale coords by 256 (8 fractional bits) */
1760 s
= lp_build_mul_imm(&bld
->coord_bld
, s
, 256);
1761 t
= lp_build_mul_imm(&bld
->coord_bld
, t
, 256);
1763 /* convert float to int */
1764 s
= LLVMBuildFPToSI(builder
, s
, i32_vec_type
, "");
1765 t
= LLVMBuildFPToSI(builder
, t
, i32_vec_type
, "");
1767 /* subtract 0.5 (add -128) */
1768 i32_c128
= lp_build_int_const_scalar(i32
.type
, -128);
1769 s
= LLVMBuildAdd(builder
, s
, i32_c128
, "");
1770 t
= LLVMBuildAdd(builder
, t
, i32_c128
, "");
1772 /* compute floor (shift right 8) */
1773 i32_c8
= lp_build_int_const_scalar(i32
.type
, 8);
1774 s_ipart
= LLVMBuildAShr(builder
, s
, i32_c8
, "");
1775 t_ipart
= LLVMBuildAShr(builder
, t
, i32_c8
, "");
1777 /* compute fractional part (AND with 0xff) */
1778 i32_c255
= lp_build_int_const_scalar(i32
.type
, 255);
1779 s_fpart
= LLVMBuildAnd(builder
, s
, i32_c255
, "");
1780 t_fpart
= LLVMBuildAnd(builder
, t
, i32_c255
, "");
1785 x1
= lp_build_add(&bld
->int_coord_bld
, x0
, bld
->int_coord_bld
.one
);
1786 y1
= lp_build_add(&bld
->int_coord_bld
, y0
, bld
->int_coord_bld
.one
);
1788 x0
= lp_build_sample_wrap_int(bld
, x0
, width
, bld
->static_state
->pot_width
,
1789 bld
->static_state
->wrap_s
);
1790 y0
= lp_build_sample_wrap_int(bld
, y0
, height
, bld
->static_state
->pot_height
,
1791 bld
->static_state
->wrap_t
);
1793 x1
= lp_build_sample_wrap_int(bld
, x1
, width
, bld
->static_state
->pot_width
,
1794 bld
->static_state
->wrap_s
);
1795 y1
= lp_build_sample_wrap_int(bld
, y1
, height
, bld
->static_state
->pot_height
,
1796 bld
->static_state
->wrap_t
);
1799 * Transform 4 x i32 in
1801 * s_fpart = {s0, s1, s2, s3}
1805 * s_fpart = {00, s0, 00, s1, 00, s2, 00, s3}
1809 * s_fpart_lo = {s0, s0, s0, s0, s1, s1, s1, s1}
1810 * s_fpart_hi = {s2, s2, s2, s2, s3, s3, s3, s3}
1812 * and likewise for t_fpart. There is no risk of loosing precision here
1813 * since the fractional parts only use the lower 8bits.
1816 s_fpart
= LLVMBuildBitCast(builder
, s_fpart
, h16_vec_type
, "");
1817 t_fpart
= LLVMBuildBitCast(builder
, t_fpart
, h16_vec_type
, "");
1820 LLVMTypeRef elem_type
= LLVMInt32Type();
1821 LLVMValueRef shuffles_lo
[LP_MAX_VECTOR_LENGTH
];
1822 LLVMValueRef shuffles_hi
[LP_MAX_VECTOR_LENGTH
];
1823 LLVMValueRef shuffle_lo
;
1824 LLVMValueRef shuffle_hi
;
1827 for(j
= 0; j
< h16
.type
.length
; j
+= 4) {
1828 unsigned subindex
= util_cpu_caps
.little_endian
? 0 : 1;
1831 index
= LLVMConstInt(elem_type
, j
/2 + subindex
, 0);
1832 for(i
= 0; i
< 4; ++i
)
1833 shuffles_lo
[j
+ i
] = index
;
1835 index
= LLVMConstInt(elem_type
, h16
.type
.length
/2 + j
/2 + subindex
, 0);
1836 for(i
= 0; i
< 4; ++i
)
1837 shuffles_hi
[j
+ i
] = index
;
1840 shuffle_lo
= LLVMConstVector(shuffles_lo
, h16
.type
.length
);
1841 shuffle_hi
= LLVMConstVector(shuffles_hi
, h16
.type
.length
);
1843 s_fpart_lo
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_lo
, "");
1844 t_fpart_lo
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_lo
, "");
1845 s_fpart_hi
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_hi
, "");
1846 t_fpart_hi
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_hi
, "");
1849 stride
= lp_build_get_const_level_stride_vec(bld
, stride_array
, 0);
1852 * Fetch the pixels as 4 x 32bit (rgba order might differ):
1854 * rgba0 rgba1 rgba2 rgba3
1856 * bit cast them into 16 x u8
1858 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
1860 * unpack them into two 8 x i16:
1862 * r0 g0 b0 a0 r1 g1 b1 a1
1863 * r2 g2 b2 a2 r3 g3 b3 a3
1865 * The higher 8 bits of the resulting elements will be zero.
1868 neighbors
[0][0] = lp_build_sample_packed(bld
, x0
, y0
, stride
, data_array
);
1869 neighbors
[0][1] = lp_build_sample_packed(bld
, x1
, y0
, stride
, data_array
);
1870 neighbors
[1][0] = lp_build_sample_packed(bld
, x0
, y1
, stride
, data_array
);
1871 neighbors
[1][1] = lp_build_sample_packed(bld
, x1
, y1
, stride
, data_array
);
1873 neighbors
[0][0] = LLVMBuildBitCast(builder
, neighbors
[0][0], u8n_vec_type
, "");
1874 neighbors
[0][1] = LLVMBuildBitCast(builder
, neighbors
[0][1], u8n_vec_type
, "");
1875 neighbors
[1][0] = LLVMBuildBitCast(builder
, neighbors
[1][0], u8n_vec_type
, "");
1876 neighbors
[1][1] = LLVMBuildBitCast(builder
, neighbors
[1][1], u8n_vec_type
, "");
1878 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][0], &neighbors_lo
[0][0], &neighbors_hi
[0][0]);
1879 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][1], &neighbors_lo
[0][1], &neighbors_hi
[0][1]);
1880 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][0], &neighbors_lo
[1][0], &neighbors_hi
[1][0]);
1881 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][1], &neighbors_lo
[1][1], &neighbors_hi
[1][1]);
1884 * Linear interpolate with 8.8 fixed point.
1887 packed_lo
= lp_build_lerp_2d(&h16
,
1888 s_fpart_lo
, t_fpart_lo
,
1892 neighbors_lo
[1][1]);
1894 packed_hi
= lp_build_lerp_2d(&h16
,
1895 s_fpart_hi
, t_fpart_hi
,
1899 neighbors_hi
[1][1]);
1901 packed
= lp_build_pack2(builder
, h16
.type
, u8n
.type
, packed_lo
, packed_hi
);
1904 * Convert to SoA and swizzle.
1907 packed
= LLVMBuildBitCast(builder
, packed
, i32_vec_type
, "");
1909 lp_build_rgba8_to_f32_soa(bld
->builder
,
1911 packed
, unswizzled
);
1913 lp_build_format_swizzle_soa(bld
->format_desc
,
1914 bld
->texel_type
, unswizzled
,
1920 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
1922 LLVMValueRef
*texel
)
1924 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1928 if(bld
->static_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
1931 /* TODO: Compare before swizzling, to avoid redundant computations */
1933 for(chan
= 0; chan
< 4; ++chan
) {
1935 cmp
= lp_build_cmp(texel_bld
, bld
->static_state
->compare_func
, p
, texel
[chan
]);
1936 cmp
= lp_build_select(texel_bld
, cmp
, texel_bld
->one
, texel_bld
->zero
);
1939 res
= lp_build_add(texel_bld
, res
, cmp
);
1945 res
= lp_build_mul(texel_bld
, res
, lp_build_const_scalar(texel_bld
->type
, 0.25));
1947 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1948 for(chan
= 0; chan
< 3; ++chan
)
1950 texel
[3] = texel_bld
->one
;
1955 * Build texture sampling code.
1956 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1958 * \param type vector float type to use for coords, etc.
1961 lp_build_sample_soa(LLVMBuilderRef builder
,
1962 const struct lp_sampler_static_state
*static_state
,
1963 struct lp_sampler_dynamic_state
*dynamic_state
,
1964 struct lp_type type
,
1966 unsigned num_coords
,
1967 const LLVMValueRef
*coords
,
1968 LLVMValueRef lodbias
,
1969 LLVMValueRef
*texel
)
1971 struct lp_build_sample_context bld
;
1972 LLVMValueRef width
, width_vec
;
1973 LLVMValueRef height
, height_vec
;
1974 LLVMValueRef depth
, depth_vec
;
1975 LLVMValueRef stride_array
;
1976 LLVMValueRef data_array
;
1981 (void) lp_build_lod_selector
; /* temporary to silence warning */
1982 (void) lp_build_nearest_mip_level
;
1983 (void) lp_build_linear_mip_levels
;
1984 (void) lp_build_minify
;
1986 /* Setup our build context */
1987 memset(&bld
, 0, sizeof bld
);
1988 bld
.builder
= builder
;
1989 bld
.static_state
= static_state
;
1990 bld
.dynamic_state
= dynamic_state
;
1991 bld
.format_desc
= util_format_description(static_state
->format
);
1993 bld
.float_type
= lp_type_float(32);
1994 bld
.int_type
= lp_type_int(32);
1995 bld
.coord_type
= type
;
1996 bld
.uint_coord_type
= lp_uint_type(type
);
1997 bld
.int_coord_type
= lp_int_type(type
);
1998 bld
.texel_type
= type
;
2000 lp_build_context_init(&bld
.float_bld
, builder
, bld
.float_type
);
2001 lp_build_context_init(&bld
.int_bld
, builder
, bld
.int_type
);
2002 lp_build_context_init(&bld
.coord_bld
, builder
, bld
.coord_type
);
2003 lp_build_context_init(&bld
.uint_coord_bld
, builder
, bld
.uint_coord_type
);
2004 lp_build_context_init(&bld
.int_coord_bld
, builder
, bld
.int_coord_type
);
2005 lp_build_context_init(&bld
.texel_bld
, builder
, bld
.texel_type
);
2007 /* Get the dynamic state */
2008 width
= dynamic_state
->width(dynamic_state
, builder
, unit
);
2009 height
= dynamic_state
->height(dynamic_state
, builder
, unit
);
2010 depth
= dynamic_state
->depth(dynamic_state
, builder
, unit
);
2011 stride_array
= dynamic_state
->row_stride(dynamic_state
, builder
, unit
);
2012 data_array
= dynamic_state
->data_ptr(dynamic_state
, builder
, unit
);
2013 /* Note that data_array is an array[level] of pointers to texture images */
2019 width_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, width
);
2020 height_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, height
);
2021 depth_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, depth
);
2023 if (lp_format_is_rgba8(bld
.format_desc
) &&
2024 static_state
->target
== PIPE_TEXTURE_2D
&&
2025 static_state
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
2026 static_state
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
2027 static_state
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
&&
2028 is_simple_wrap_mode(static_state
->wrap_s
) &&
2029 is_simple_wrap_mode(static_state
->wrap_t
)) {
2031 lp_build_sample_2d_linear_aos(&bld
, s
, t
, width_vec
, height_vec
,
2032 stride_array
, data_array
, texel
);
2035 lp_build_sample_general(&bld
, unit
, s
, t
, r
,
2036 width
, height
, depth
,
2037 width_vec
, height_vec
, depth_vec
,
2038 stride_array
, NULL
, data_array
,
2042 lp_build_sample_compare(&bld
, r
, texel
);