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()");
189 lp_build_swizzle_chan_soa(struct lp_type type
,
190 const LLVMValueRef
*unswizzled
,
191 enum util_format_swizzle swizzle
)
194 case PIPE_SWIZZLE_RED
:
195 case PIPE_SWIZZLE_GREEN
:
196 case PIPE_SWIZZLE_BLUE
:
197 case PIPE_SWIZZLE_ALPHA
:
198 return unswizzled
[swizzle
];
199 case PIPE_SWIZZLE_ZERO
:
200 return lp_build_zero(type
);
201 case PIPE_SWIZZLE_ONE
:
202 return lp_build_one(type
);
205 return lp_build_undef(type
);
211 lp_build_swizzle_soa(struct lp_build_sample_context
*bld
,
214 LLVMValueRef unswizzled
[4];
215 unsigned char swizzles
[4];
218 for (chan
= 0; chan
< 4; ++chan
) {
219 unswizzled
[chan
] = texel
[chan
];
222 swizzles
[0] = bld
->static_state
->swizzle_r
;
223 swizzles
[1] = bld
->static_state
->swizzle_g
;
224 swizzles
[2] = bld
->static_state
->swizzle_b
;
225 swizzles
[3] = bld
->static_state
->swizzle_a
;
227 for (chan
= 0; chan
< 4; ++chan
) {
228 unsigned swizzle
= swizzles
[chan
];
229 texel
[chan
] = lp_build_swizzle_chan_soa(bld
->texel_type
,
230 unswizzled
, swizzle
);
237 * Generate code to fetch a texel from a texture at int coords (x, y, z).
238 * The computation depends on whether the texture is 1D, 2D or 3D.
239 * The result, texel, will be:
240 * texel[0] = red values
241 * texel[1] = green values
242 * texel[2] = blue values
243 * texel[3] = alpha values
246 lp_build_sample_texel_soa(struct lp_build_sample_context
*bld
,
253 LLVMValueRef y_stride
,
254 LLVMValueRef z_stride
,
255 LLVMValueRef data_ptr
,
258 const int dims
= texture_dims(bld
->static_state
->target
);
259 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
262 LLVMValueRef use_border
= NULL
;
264 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
265 if (wrap_mode_uses_border_color(bld
->static_state
->wrap_s
)) {
267 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
268 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
269 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
272 if (dims
>= 2 && wrap_mode_uses_border_color(bld
->static_state
->wrap_t
)) {
274 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
275 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
277 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
278 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
281 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
285 if (dims
== 3 && wrap_mode_uses_border_color(bld
->static_state
->wrap_r
)) {
287 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
288 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
290 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
291 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
294 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
299 * Describe the coordinates in terms of pixel blocks.
301 * TODO: pixel blocks are power of two. LLVM should convert rem/div to
302 * bit arithmetic. Verify this.
305 if (bld
->format_desc
->block
.width
== 1) {
306 i
= bld
->uint_coord_bld
.zero
;
309 LLVMValueRef block_width
= lp_build_const_int_vec(bld
->uint_coord_bld
.type
, bld
->format_desc
->block
.width
);
310 i
= LLVMBuildURem(bld
->builder
, x
, block_width
, "");
311 x
= LLVMBuildUDiv(bld
->builder
, x
, block_width
, "");
314 if (bld
->format_desc
->block
.height
== 1) {
315 j
= bld
->uint_coord_bld
.zero
;
318 LLVMValueRef block_height
= lp_build_const_int_vec(bld
->uint_coord_bld
.type
, bld
->format_desc
->block
.height
);
319 j
= LLVMBuildURem(bld
->builder
, y
, block_height
, "");
320 y
= LLVMBuildUDiv(bld
->builder
, y
, block_height
, "");
323 /* convert x,y,z coords to linear offset from start of texture, in bytes */
324 offset
= lp_build_sample_offset(&bld
->uint_coord_bld
,
326 x
, y
, z
, y_stride
, z_stride
);
328 lp_build_fetch_rgba_soa(bld
->builder
,
335 lp_build_swizzle_soa(bld
, texel
);
338 * Note: if we find an app which frequently samples the texture border
339 * we might want to implement a true conditional here to avoid sampling
340 * the texture whenever possible (since that's quite a bit of code).
343 * texel = border_color;
346 * texel = sample_texture(coord);
348 * As it is now, we always sample the texture, then selectively replace
349 * the texel color results with the border color.
353 /* select texel color or border color depending on use_border */
355 for (chan
= 0; chan
< 4; chan
++) {
356 LLVMValueRef border_chan
=
357 lp_build_const_vec(bld
->texel_type
,
358 bld
->static_state
->border_color
[chan
]);
359 texel
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
360 border_chan
, texel
[chan
]);
367 lp_build_sample_packed(struct lp_build_sample_context
*bld
,
370 LLVMValueRef y_stride
,
371 LLVMValueRef data_array
)
374 LLVMValueRef data_ptr
;
376 offset
= lp_build_sample_offset(&bld
->uint_coord_bld
,
378 x
, y
, NULL
, y_stride
, NULL
);
380 assert(bld
->format_desc
->block
.width
== 1);
381 assert(bld
->format_desc
->block
.height
== 1);
382 assert(bld
->format_desc
->block
.bits
<= bld
->texel_type
.width
);
384 /* get pointer to mipmap level 0 data */
385 data_ptr
= lp_build_get_const_mipmap_level(bld
, data_array
, 0);
387 return lp_build_gather(bld
->builder
,
388 bld
->texel_type
.length
,
389 bld
->format_desc
->block
.bits
,
390 bld
->texel_type
.width
,
396 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
399 lp_build_coord_mirror(struct lp_build_sample_context
*bld
,
402 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
403 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
404 LLVMValueRef fract
, flr
, isOdd
;
406 /* fract = coord - floor(coord) */
407 fract
= lp_build_sub(coord_bld
, coord
, lp_build_floor(coord_bld
, coord
));
409 /* flr = ifloor(coord); */
410 flr
= lp_build_ifloor(coord_bld
, coord
);
412 /* isOdd = flr & 1 */
413 isOdd
= LLVMBuildAnd(bld
->builder
, flr
, int_coord_bld
->one
, "");
415 /* make coord positive or negative depending on isOdd */
416 coord
= lp_build_set_sign(coord_bld
, fract
, isOdd
);
418 /* convert isOdd to float */
419 isOdd
= lp_build_int_to_float(coord_bld
, isOdd
);
421 /* add isOdd to coord */
422 coord
= lp_build_add(coord_bld
, coord
, isOdd
);
429 * We only support a few wrap modes in lp_build_sample_wrap_int() at this time.
430 * Return whether the given mode is supported by that function.
433 is_simple_wrap_mode(unsigned mode
)
436 case PIPE_TEX_WRAP_REPEAT
:
437 case PIPE_TEX_WRAP_CLAMP
:
438 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
440 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
448 * Build LLVM code for texture wrap mode, for scaled integer texcoords.
449 * \param coord the incoming texcoord (s,t,r or q) scaled to the texture size
450 * \param length the texture size along one dimension
451 * \param is_pot if TRUE, length is a power of two
452 * \param wrap_mode one of PIPE_TEX_WRAP_x
455 lp_build_sample_wrap_int(struct lp_build_sample_context
*bld
,
461 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
462 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
463 LLVMValueRef length_minus_one
;
465 length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
468 case PIPE_TEX_WRAP_REPEAT
:
470 coord
= LLVMBuildAnd(bld
->builder
, coord
, length_minus_one
, "");
472 /* Signed remainder won't give the right results for negative
473 * dividends but unsigned remainder does.*/
474 coord
= LLVMBuildURem(bld
->builder
, coord
, length
, "");
477 case PIPE_TEX_WRAP_CLAMP
:
478 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
479 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
480 coord
= lp_build_max(int_coord_bld
, coord
, int_coord_bld
->zero
);
481 coord
= lp_build_min(int_coord_bld
, coord
, length_minus_one
);
484 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
485 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
486 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
487 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
489 _debug_printf("llvmpipe: failed to translate texture wrap mode %s\n",
490 util_dump_tex_wrap(wrap_mode
, TRUE
));
491 coord
= lp_build_max(uint_coord_bld
, coord
, uint_coord_bld
->zero
);
492 coord
= lp_build_min(uint_coord_bld
, coord
, length_minus_one
);
504 * Build LLVM code for texture wrap mode for linear filtering.
505 * \param x0_out returns first integer texcoord
506 * \param x1_out returns second integer texcoord
507 * \param weight_out returns linear interpolation weight
510 lp_build_sample_wrap_linear(struct lp_build_sample_context
*bld
,
515 LLVMValueRef
*x0_out
,
516 LLVMValueRef
*x1_out
,
517 LLVMValueRef
*weight_out
)
519 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
520 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
521 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
522 LLVMValueRef two
= lp_build_const_vec(coord_bld
->type
, 2.0);
523 LLVMValueRef half
= lp_build_const_vec(coord_bld
->type
, 0.5);
524 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
525 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
526 LLVMValueRef length_f_minus_one
= lp_build_sub(coord_bld
, length_f
, coord_bld
->one
);
527 LLVMValueRef coord0
, coord1
, weight
;
530 case PIPE_TEX_WRAP_REPEAT
:
531 /* mul by size and subtract 0.5 */
532 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
533 coord
= lp_build_sub(coord_bld
, coord
, half
);
535 coord0
= lp_build_ifloor(coord_bld
, coord
);
536 coord1
= lp_build_add(uint_coord_bld
, coord0
, uint_coord_bld
->one
);
537 /* compute lerp weight */
538 weight
= lp_build_fract(coord_bld
, coord
);
541 coord0
= LLVMBuildAnd(bld
->builder
, coord0
, length_minus_one
, "");
542 coord1
= LLVMBuildAnd(bld
->builder
, coord1
, length_minus_one
, "");
545 /* Signed remainder won't give the right results for negative
546 * dividends but unsigned remainder does.*/
547 coord0
= LLVMBuildURem(bld
->builder
, coord0
, length
, "");
548 coord1
= LLVMBuildURem(bld
->builder
, coord1
, length
, "");
552 case PIPE_TEX_WRAP_CLAMP
:
553 if (bld
->static_state
->normalized_coords
) {
554 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
556 weight
= lp_build_fract(coord_bld
, coord
);
557 coord0
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
,
559 coord1
= lp_build_add(coord_bld
, coord
, coord_bld
->one
);
560 coord1
= lp_build_clamp(coord_bld
, coord1
, coord_bld
->zero
,
562 coord0
= lp_build_ifloor(coord_bld
, coord0
);
563 coord1
= lp_build_ifloor(coord_bld
, coord1
);
566 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
567 if (bld
->static_state
->normalized_coords
) {
569 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, coord_bld
->one
);
570 /* mul by tex size and subtract 0.5 */
571 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
572 coord
= lp_build_sub(coord_bld
, coord
, half
);
575 LLVMValueRef min
, max
;
576 /* clamp to [0.5, length - 0.5] */
577 min
= lp_build_const_vec(coord_bld
->type
, 0.5F
);
578 max
= lp_build_sub(coord_bld
, length_f
, min
);
579 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
581 /* compute lerp weight */
582 weight
= lp_build_fract(coord_bld
, coord
);
583 /* coord0 = floor(coord); */
584 coord0
= lp_build_ifloor(coord_bld
, coord
);
585 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
586 /* coord0 = max(coord0, 0) */
587 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
588 /* coord1 = min(coord1, length-1) */
589 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
592 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
594 LLVMValueRef min
, max
;
595 if (bld
->static_state
->normalized_coords
) {
596 /* min = -1.0 / (2 * length) = -0.5 / length */
597 min
= lp_build_mul(coord_bld
,
598 lp_build_const_vec(coord_bld
->type
, -0.5F
),
599 lp_build_rcp(coord_bld
, length_f
));
600 /* max = 1.0 - min */
601 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
602 /* coord = clamp(coord, min, max) */
603 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
604 /* scale coord to length (and sub 0.5?) */
605 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
606 coord
= lp_build_sub(coord_bld
, coord
, half
);
609 /* clamp to [-0.5, length + 0.5] */
610 min
= lp_build_const_vec(coord_bld
->type
, -0.5F
);
611 max
= lp_build_sub(coord_bld
, length_f
, min
);
612 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
613 coord
= lp_build_sub(coord_bld
, coord
, half
);
615 /* compute lerp weight */
616 weight
= lp_build_fract(coord_bld
, coord
);
618 coord0
= lp_build_ifloor(coord_bld
, coord
);
619 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
623 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
624 /* compute mirror function */
625 coord
= lp_build_coord_mirror(bld
, coord
);
627 /* scale coord to length */
628 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
629 coord
= lp_build_sub(coord_bld
, coord
, half
);
631 /* compute lerp weight */
632 weight
= lp_build_fract(coord_bld
, coord
);
634 /* convert to int coords */
635 coord0
= lp_build_ifloor(coord_bld
, coord
);
636 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
638 /* coord0 = max(coord0, 0) */
639 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
640 /* coord1 = min(coord1, length-1) */
641 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
644 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
646 LLVMValueRef min
, max
;
647 /* min = 1.0 / (2 * length) */
648 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
649 /* max = 1.0 - min */
650 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
652 coord
= lp_build_abs(coord_bld
, coord
);
653 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
654 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
655 if(0)coord
= lp_build_sub(coord_bld
, coord
, half
);
656 weight
= lp_build_fract(coord_bld
, coord
);
657 coord0
= lp_build_ifloor(coord_bld
, coord
);
658 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
662 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
664 LLVMValueRef min
, max
;
665 /* min = 1.0 / (2 * length) */
666 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
667 /* max = 1.0 - min */
668 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
670 coord
= lp_build_abs(coord_bld
, coord
);
671 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
672 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
673 coord
= lp_build_sub(coord_bld
, coord
, half
);
674 weight
= lp_build_fract(coord_bld
, coord
);
675 coord0
= lp_build_ifloor(coord_bld
, coord
);
676 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
680 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
682 LLVMValueRef min
, max
;
683 /* min = -1.0 / (2 * length) = -0.5 / length */
684 min
= lp_build_mul(coord_bld
,
685 lp_build_const_vec(coord_bld
->type
, -0.5F
),
686 lp_build_rcp(coord_bld
, length_f
));
687 /* max = 1.0 - min */
688 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
690 coord
= lp_build_abs(coord_bld
, coord
);
691 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
692 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
693 coord
= lp_build_sub(coord_bld
, coord
, half
);
694 weight
= lp_build_fract(coord_bld
, coord
);
695 coord0
= lp_build_ifloor(coord_bld
, coord
);
696 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
709 *weight_out
= weight
;
714 * Build LLVM code for texture wrap mode for nearest filtering.
715 * \param coord the incoming texcoord (nominally in [0,1])
716 * \param length the texture size along one dimension, as int
717 * \param is_pot if TRUE, length is a power of two
718 * \param wrap_mode one of PIPE_TEX_WRAP_x
721 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
727 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
728 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
729 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
730 LLVMValueRef two
= lp_build_const_vec(coord_bld
->type
, 2.0);
731 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
732 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
733 LLVMValueRef length_f_minus_one
= lp_build_sub(coord_bld
, length_f
, coord_bld
->one
);
737 case PIPE_TEX_WRAP_REPEAT
:
738 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
739 icoord
= lp_build_ifloor(coord_bld
, coord
);
741 icoord
= LLVMBuildAnd(bld
->builder
, icoord
, length_minus_one
, "");
743 /* Signed remainder won't give the right results for negative
744 * dividends but unsigned remainder does.*/
745 icoord
= LLVMBuildURem(bld
->builder
, icoord
, length
, "");
748 case PIPE_TEX_WRAP_CLAMP
:
750 if (bld
->static_state
->normalized_coords
) {
751 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
754 icoord
= lp_build_ifloor(coord_bld
, coord
);
755 /* clamp to [0, size-1]. Note: int coord builder type */
756 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
760 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
762 LLVMValueRef min
, max
;
763 if (bld
->static_state
->normalized_coords
) {
764 /* min = 1.0 / (2 * length) */
765 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
766 /* max = length - min */
767 max
= lp_build_sub(coord_bld
, length_f
, min
);
768 /* scale coord to length */
769 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
772 /* clamp to [0.5, length - 0.5] */
773 min
= lp_build_const_vec(coord_bld
->type
, 0.5F
);
774 max
= lp_build_sub(coord_bld
, length_f
, min
);
776 /* coord = clamp(coord, min, max) */
777 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
778 icoord
= lp_build_ifloor(coord_bld
, coord
);
782 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
783 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
785 LLVMValueRef min
, max
;
786 if (bld
->static_state
->normalized_coords
) {
787 /* min = -1.0 / (2 * length) = -0.5 / length */
788 min
= lp_build_mul(coord_bld
,
789 lp_build_const_vec(coord_bld
->type
, -0.5F
),
790 lp_build_rcp(coord_bld
, length_f
));
791 /* max = length - min */
792 max
= lp_build_sub(coord_bld
, length_f
, min
);
793 /* scale coord to length */
794 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
797 /* clamp to [-0.5, length + 0.5] */
798 min
= lp_build_const_vec(coord_bld
->type
, -0.5F
);
799 max
= lp_build_sub(coord_bld
, length_f
, min
);
801 /* coord = clamp(coord, min, max) */
802 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
803 icoord
= lp_build_ifloor(coord_bld
, coord
);
807 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
809 LLVMValueRef min
, max
;
810 /* min = 1.0 / (2 * length) */
811 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
812 /* max = length - min */
813 max
= lp_build_sub(coord_bld
, length_f
, min
);
815 /* compute mirror function */
816 coord
= lp_build_coord_mirror(bld
, coord
);
818 /* scale coord to length */
819 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
821 /* coord = clamp(coord, min, max) */
822 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
823 icoord
= lp_build_ifloor(coord_bld
, coord
);
827 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
828 coord
= lp_build_abs(coord_bld
, coord
);
829 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
830 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, length_f_minus_one
);
831 icoord
= lp_build_ifloor(coord_bld
, coord
);
834 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
836 LLVMValueRef min
, max
;
837 /* min = 1.0 / (2 * length) */
838 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
839 /* max = length - min */
840 max
= lp_build_sub(coord_bld
, length_f
, min
);
842 coord
= lp_build_abs(coord_bld
, coord
);
843 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
844 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
845 icoord
= lp_build_ifloor(coord_bld
, coord
);
849 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
851 LLVMValueRef min
, max
;
852 /* min = 1.0 / (2 * length) */
853 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
854 min
= lp_build_negate(coord_bld
, min
);
855 /* max = length - min */
856 max
= lp_build_sub(coord_bld
, length_f
, min
);
858 coord
= lp_build_abs(coord_bld
, coord
);
859 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
860 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
861 icoord
= lp_build_ifloor(coord_bld
, coord
);
875 * Codegen equivalent for u_minify().
876 * Return max(1, base_size >> level);
879 lp_build_minify(struct lp_build_sample_context
*bld
,
880 LLVMValueRef base_size
,
883 LLVMValueRef size
= LLVMBuildAShr(bld
->builder
, base_size
, level
, "minify");
884 size
= lp_build_max(&bld
->int_coord_bld
, size
, bld
->int_coord_bld
.one
);
890 * Generate code to compute texture level of detail (lambda).
891 * \param s vector of texcoord s values
892 * \param t vector of texcoord t values
893 * \param r vector of texcoord r values
894 * \param shader_lod_bias vector float with the shader lod bias,
895 * \param width scalar int texture width
896 * \param height scalar int texture height
897 * \param depth scalar int texture depth
900 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
904 LLVMValueRef shader_lod_bias
,
910 if (bld
->static_state
->min_lod
== bld
->static_state
->max_lod
) {
911 /* User is forcing sampling from a particular mipmap level.
912 * This is hit during mipmap generation.
914 return LLVMConstReal(LLVMFloatType(), bld
->static_state
->min_lod
);
917 const int dims
= texture_dims(bld
->static_state
->target
);
918 struct lp_build_context
*float_bld
= &bld
->float_bld
;
919 LLVMValueRef sampler_lod_bias
= LLVMConstReal(LLVMFloatType(),
920 bld
->static_state
->lod_bias
);
921 LLVMValueRef min_lod
= LLVMConstReal(LLVMFloatType(),
922 bld
->static_state
->min_lod
);
923 LLVMValueRef max_lod
= LLVMConstReal(LLVMFloatType(),
924 bld
->static_state
->max_lod
);
926 LLVMValueRef index0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
927 LLVMValueRef index1
= LLVMConstInt(LLVMInt32Type(), 1, 0);
928 LLVMValueRef index2
= LLVMConstInt(LLVMInt32Type(), 2, 0);
930 LLVMValueRef s0
, s1
, s2
;
931 LLVMValueRef t0
, t1
, t2
;
932 LLVMValueRef r0
, r1
, r2
;
933 LLVMValueRef dsdx
, dsdy
, dtdx
, dtdy
, drdx
, drdy
;
934 LLVMValueRef rho
, lod
;
937 * dsdx = abs(s[1] - s[0]);
938 * dsdy = abs(s[2] - s[0]);
939 * dtdx = abs(t[1] - t[0]);
940 * dtdy = abs(t[2] - t[0]);
941 * drdx = abs(r[1] - r[0]);
942 * drdy = abs(r[2] - r[0]);
943 * XXX we're assuming a four-element quad in 2x2 layout here.
945 s0
= LLVMBuildExtractElement(bld
->builder
, s
, index0
, "s0");
946 s1
= LLVMBuildExtractElement(bld
->builder
, s
, index1
, "s1");
947 s2
= LLVMBuildExtractElement(bld
->builder
, s
, index2
, "s2");
948 dsdx
= LLVMBuildSub(bld
->builder
, s1
, s0
, "");
949 dsdx
= lp_build_abs(float_bld
, dsdx
);
950 dsdy
= LLVMBuildSub(bld
->builder
, s2
, s0
, "");
951 dsdy
= lp_build_abs(float_bld
, dsdy
);
953 t0
= LLVMBuildExtractElement(bld
->builder
, t
, index0
, "t0");
954 t1
= LLVMBuildExtractElement(bld
->builder
, t
, index1
, "t1");
955 t2
= LLVMBuildExtractElement(bld
->builder
, t
, index2
, "t2");
956 dtdx
= LLVMBuildSub(bld
->builder
, t1
, t0
, "");
957 dtdx
= lp_build_abs(float_bld
, dtdx
);
958 dtdy
= LLVMBuildSub(bld
->builder
, t2
, t0
, "");
959 dtdy
= lp_build_abs(float_bld
, dtdy
);
961 r0
= LLVMBuildExtractElement(bld
->builder
, r
, index0
, "r0");
962 r1
= LLVMBuildExtractElement(bld
->builder
, r
, index1
, "r1");
963 r2
= LLVMBuildExtractElement(bld
->builder
, r
, index2
, "r2");
964 drdx
= LLVMBuildSub(bld
->builder
, r1
, r0
, "");
965 drdx
= lp_build_abs(float_bld
, drdx
);
966 drdy
= LLVMBuildSub(bld
->builder
, r2
, r0
, "");
967 drdy
= lp_build_abs(float_bld
, drdy
);
971 /* Compute rho = max of all partial derivatives scaled by texture size.
972 * XXX this could be vectorized somewhat
974 rho
= LLVMBuildMul(bld
->builder
,
975 lp_build_max(float_bld
, dsdx
, dsdy
),
976 lp_build_int_to_float(float_bld
, width
), "");
979 max
= LLVMBuildMul(bld
->builder
,
980 lp_build_max(float_bld
, dtdx
, dtdy
),
981 lp_build_int_to_float(float_bld
, height
), "");
982 rho
= lp_build_max(float_bld
, rho
, max
);
984 max
= LLVMBuildMul(bld
->builder
,
985 lp_build_max(float_bld
, drdx
, drdy
),
986 lp_build_int_to_float(float_bld
, depth
), "");
987 rho
= lp_build_max(float_bld
, rho
, max
);
991 /* compute lod = log2(rho) */
992 lod
= lp_build_log2(float_bld
, rho
);
994 /* add sampler lod bias */
995 lod
= LLVMBuildAdd(bld
->builder
, lod
, sampler_lod_bias
, "sampler LOD bias");
997 /* add shader lod bias */
998 /* XXX for now we take only the first element since our lod is scalar */
999 shader_lod_bias
= LLVMBuildExtractElement(bld
->builder
, shader_lod_bias
,
1000 LLVMConstInt(LLVMInt32Type(), 0, 0), "");
1001 lod
= LLVMBuildAdd(bld
->builder
, lod
, shader_lod_bias
, "shader LOD bias");
1004 lod
= lp_build_clamp(float_bld
, lod
, min_lod
, max_lod
);
1012 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
1013 * mipmap level index.
1014 * Note: this is all scalar code.
1015 * \param lod scalar float texture level of detail
1016 * \param level_out returns integer
1019 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
1022 LLVMValueRef
*level_out
)
1024 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1025 struct lp_build_context
*int_bld
= &bld
->int_bld
;
1026 LLVMValueRef last_level
, level
;
1028 LLVMValueRef zero
= LLVMConstInt(LLVMInt32Type(), 0, 0);
1030 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
1031 bld
->builder
, unit
);
1033 /* convert float lod to integer */
1034 level
= lp_build_iround(float_bld
, lod
);
1036 /* clamp level to legal range of levels */
1037 *level_out
= lp_build_clamp(int_bld
, level
, zero
, last_level
);
1042 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
1043 * two (adjacent) mipmap level indexes. Later, we'll sample from those
1044 * two mipmap levels and interpolate between them.
1047 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
1050 LLVMValueRef
*level0_out
,
1051 LLVMValueRef
*level1_out
,
1052 LLVMValueRef
*weight_out
)
1054 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1055 struct lp_build_context
*int_bld
= &bld
->int_bld
;
1056 LLVMValueRef last_level
, level
;
1058 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
1059 bld
->builder
, unit
);
1061 /* convert float lod to integer */
1062 level
= lp_build_ifloor(float_bld
, lod
);
1064 /* compute level 0 and clamp to legal range of levels */
1065 *level0_out
= lp_build_clamp(int_bld
, level
,
1068 /* compute level 1 and clamp to legal range of levels */
1069 *level1_out
= lp_build_add(int_bld
, *level0_out
, int_bld
->one
);
1070 *level1_out
= lp_build_min(int_bld
, *level1_out
, last_level
);
1072 *weight_out
= lp_build_fract(float_bld
, lod
);
1077 * Generate code to sample a mipmap level with nearest filtering.
1078 * If sampling a cube texture, r = cube face in [0,5].
1081 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
1082 LLVMValueRef width_vec
,
1083 LLVMValueRef height_vec
,
1084 LLVMValueRef depth_vec
,
1085 LLVMValueRef row_stride_vec
,
1086 LLVMValueRef img_stride_vec
,
1087 LLVMValueRef data_ptr
,
1091 LLVMValueRef colors_out
[4])
1093 const int dims
= texture_dims(bld
->static_state
->target
);
1094 LLVMValueRef x
, y
, z
;
1097 * Compute integer texcoords.
1099 x
= lp_build_sample_wrap_nearest(bld
, s
, width_vec
,
1100 bld
->static_state
->pot_width
,
1101 bld
->static_state
->wrap_s
);
1102 lp_build_name(x
, "tex.x.wrapped");
1105 y
= lp_build_sample_wrap_nearest(bld
, t
, height_vec
,
1106 bld
->static_state
->pot_height
,
1107 bld
->static_state
->wrap_t
);
1108 lp_build_name(y
, "tex.y.wrapped");
1111 z
= lp_build_sample_wrap_nearest(bld
, r
, depth_vec
,
1112 bld
->static_state
->pot_height
,
1113 bld
->static_state
->wrap_r
);
1114 lp_build_name(z
, "tex.z.wrapped");
1116 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1128 * Get texture colors.
1130 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1132 row_stride_vec
, img_stride_vec
,
1133 data_ptr
, colors_out
);
1138 * Generate code to sample a mipmap level with linear filtering.
1139 * If sampling a cube texture, r = cube face in [0,5].
1142 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
1143 LLVMValueRef width_vec
,
1144 LLVMValueRef height_vec
,
1145 LLVMValueRef depth_vec
,
1146 LLVMValueRef row_stride_vec
,
1147 LLVMValueRef img_stride_vec
,
1148 LLVMValueRef data_ptr
,
1152 LLVMValueRef colors_out
[4])
1154 const int dims
= texture_dims(bld
->static_state
->target
);
1155 LLVMValueRef x0
, y0
, z0
, x1
, y1
, z1
;
1156 LLVMValueRef s_fpart
, t_fpart
, r_fpart
;
1157 LLVMValueRef neighbors
[2][2][4];
1161 * Compute integer texcoords.
1163 lp_build_sample_wrap_linear(bld
, s
, width_vec
,
1164 bld
->static_state
->pot_width
,
1165 bld
->static_state
->wrap_s
,
1166 &x0
, &x1
, &s_fpart
);
1167 lp_build_name(x0
, "tex.x0.wrapped");
1168 lp_build_name(x1
, "tex.x1.wrapped");
1171 lp_build_sample_wrap_linear(bld
, t
, height_vec
,
1172 bld
->static_state
->pot_height
,
1173 bld
->static_state
->wrap_t
,
1174 &y0
, &y1
, &t_fpart
);
1175 lp_build_name(y0
, "tex.y0.wrapped");
1176 lp_build_name(y1
, "tex.y1.wrapped");
1179 lp_build_sample_wrap_linear(bld
, r
, depth_vec
,
1180 bld
->static_state
->pot_depth
,
1181 bld
->static_state
->wrap_r
,
1182 &z0
, &z1
, &r_fpart
);
1183 lp_build_name(z0
, "tex.z0.wrapped");
1184 lp_build_name(z1
, "tex.z1.wrapped");
1186 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1187 z0
= z1
= r
; /* cube face */
1196 y0
= y1
= t_fpart
= NULL
;
1197 z0
= z1
= r_fpart
= NULL
;
1201 * Get texture colors.
1203 /* get x0/x1 texels */
1204 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1206 row_stride_vec
, img_stride_vec
,
1207 data_ptr
, neighbors
[0][0]);
1208 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1210 row_stride_vec
, img_stride_vec
,
1211 data_ptr
, neighbors
[0][1]);
1214 /* Interpolate two samples from 1D image to produce one color */
1215 for (chan
= 0; chan
< 4; chan
++) {
1216 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, s_fpart
,
1217 neighbors
[0][0][chan
],
1218 neighbors
[0][1][chan
]);
1223 LLVMValueRef colors0
[4];
1225 /* get x0/x1 texels at y1 */
1226 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1228 row_stride_vec
, img_stride_vec
,
1229 data_ptr
, neighbors
[1][0]);
1230 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1232 row_stride_vec
, img_stride_vec
,
1233 data_ptr
, neighbors
[1][1]);
1235 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
1236 for (chan
= 0; chan
< 4; chan
++) {
1237 colors0
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1239 neighbors
[0][0][chan
],
1240 neighbors
[0][1][chan
],
1241 neighbors
[1][0][chan
],
1242 neighbors
[1][1][chan
]);
1246 LLVMValueRef neighbors1
[2][2][4];
1247 LLVMValueRef colors1
[4];
1249 /* get x0/x1/y0/y1 texels at z1 */
1250 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1252 row_stride_vec
, img_stride_vec
,
1253 data_ptr
, neighbors1
[0][0]);
1254 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1256 row_stride_vec
, img_stride_vec
,
1257 data_ptr
, neighbors1
[0][1]);
1258 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1260 row_stride_vec
, img_stride_vec
,
1261 data_ptr
, neighbors1
[1][0]);
1262 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1264 row_stride_vec
, img_stride_vec
,
1265 data_ptr
, neighbors1
[1][1]);
1267 /* Bilinear interpolate the four samples from the second Z slice */
1268 for (chan
= 0; chan
< 4; chan
++) {
1269 colors1
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1271 neighbors1
[0][0][chan
],
1272 neighbors1
[0][1][chan
],
1273 neighbors1
[1][0][chan
],
1274 neighbors1
[1][1][chan
]);
1277 /* Linearly interpolate the two samples from the two 3D slices */
1278 for (chan
= 0; chan
< 4; chan
++) {
1279 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
,
1281 colors0
[chan
], colors1
[chan
]);
1286 for (chan
= 0; chan
< 4; chan
++) {
1287 colors_out
[chan
] = colors0
[chan
];
1294 /** Helper used by lp_build_cube_lookup() */
1296 lp_build_cube_ima(struct lp_build_context
*coord_bld
, LLVMValueRef coord
)
1298 /* ima = -0.5 / abs(coord); */
1299 LLVMValueRef negHalf
= lp_build_const_vec(coord_bld
->type
, -0.5);
1300 LLVMValueRef absCoord
= lp_build_abs(coord_bld
, coord
);
1301 LLVMValueRef ima
= lp_build_mul(coord_bld
, negHalf
,
1302 lp_build_rcp(coord_bld
, absCoord
));
1308 * Helper used by lp_build_cube_lookup()
1309 * \param sign scalar +1 or -1
1310 * \param coord float vector
1311 * \param ima float vector
1314 lp_build_cube_coord(struct lp_build_context
*coord_bld
,
1315 LLVMValueRef sign
, int negate_coord
,
1316 LLVMValueRef coord
, LLVMValueRef ima
)
1318 /* return negate(coord) * ima * sign + 0.5; */
1319 LLVMValueRef half
= lp_build_const_vec(coord_bld
->type
, 0.5);
1322 assert(negate_coord
== +1 || negate_coord
== -1);
1324 if (negate_coord
== -1) {
1325 coord
= lp_build_negate(coord_bld
, coord
);
1328 res
= lp_build_mul(coord_bld
, coord
, ima
);
1330 sign
= lp_build_broadcast_scalar(coord_bld
, sign
);
1331 res
= lp_build_mul(coord_bld
, res
, sign
);
1333 res
= lp_build_add(coord_bld
, res
, half
);
1339 /** Helper used by lp_build_cube_lookup()
1340 * Return (major_coord >= 0) ? pos_face : neg_face;
1343 lp_build_cube_face(struct lp_build_sample_context
*bld
,
1344 LLVMValueRef major_coord
,
1345 unsigned pos_face
, unsigned neg_face
)
1347 LLVMValueRef cmp
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
1349 bld
->float_bld
.zero
, "");
1350 LLVMValueRef pos
= LLVMConstInt(LLVMInt32Type(), pos_face
, 0);
1351 LLVMValueRef neg
= LLVMConstInt(LLVMInt32Type(), neg_face
, 0);
1352 LLVMValueRef res
= LLVMBuildSelect(bld
->builder
, cmp
, pos
, neg
, "");
1359 * Generate code to do cube face selection and per-face texcoords.
1362 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
1367 LLVMValueRef
*face_s
,
1368 LLVMValueRef
*face_t
)
1370 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1371 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
1372 LLVMValueRef rx
, ry
, rz
;
1373 LLVMValueRef arx
, ary
, arz
;
1374 LLVMValueRef c25
= LLVMConstReal(LLVMFloatType(), 0.25);
1375 LLVMValueRef arx_ge_ary
, arx_ge_arz
;
1376 LLVMValueRef ary_ge_arx
, ary_ge_arz
;
1377 LLVMValueRef arx_ge_ary_arz
, ary_ge_arx_arz
;
1378 LLVMValueRef rx_pos
, ry_pos
, rz_pos
;
1380 assert(bld
->coord_bld
.type
.length
== 4);
1383 * Use the average of the four pixel's texcoords to choose the face.
1385 rx
= lp_build_mul(float_bld
, c25
,
1386 lp_build_sum_vector(&bld
->coord_bld
, s
));
1387 ry
= lp_build_mul(float_bld
, c25
,
1388 lp_build_sum_vector(&bld
->coord_bld
, t
));
1389 rz
= lp_build_mul(float_bld
, c25
,
1390 lp_build_sum_vector(&bld
->coord_bld
, r
));
1392 arx
= lp_build_abs(float_bld
, rx
);
1393 ary
= lp_build_abs(float_bld
, ry
);
1394 arz
= lp_build_abs(float_bld
, rz
);
1397 * Compare sign/magnitude of rx,ry,rz to determine face
1399 arx_ge_ary
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, arx
, ary
, "");
1400 arx_ge_arz
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, arx
, arz
, "");
1401 ary_ge_arx
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ary
, arx
, "");
1402 ary_ge_arz
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ary
, arz
, "");
1404 arx_ge_ary_arz
= LLVMBuildAnd(bld
->builder
, arx_ge_ary
, arx_ge_arz
, "");
1405 ary_ge_arx_arz
= LLVMBuildAnd(bld
->builder
, ary_ge_arx
, ary_ge_arz
, "");
1407 rx_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, rx
, float_bld
->zero
, "");
1408 ry_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ry
, float_bld
->zero
, "");
1409 rz_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, rz
, float_bld
->zero
, "");
1412 struct lp_build_flow_context
*flow_ctx
;
1413 struct lp_build_if_state if_ctx
;
1415 flow_ctx
= lp_build_flow_create(bld
->builder
);
1416 lp_build_flow_scope_begin(flow_ctx
);
1418 *face_s
= bld
->coord_bld
.undef
;
1419 *face_t
= bld
->coord_bld
.undef
;
1420 *face
= bld
->int_bld
.undef
;
1422 lp_build_name(*face_s
, "face_s");
1423 lp_build_name(*face_t
, "face_t");
1424 lp_build_name(*face
, "face");
1426 lp_build_flow_scope_declare(flow_ctx
, face_s
);
1427 lp_build_flow_scope_declare(flow_ctx
, face_t
);
1428 lp_build_flow_scope_declare(flow_ctx
, face
);
1430 lp_build_if(&if_ctx
, flow_ctx
, bld
->builder
, arx_ge_ary_arz
);
1433 LLVMValueRef sign
= lp_build_sgn(float_bld
, rx
);
1434 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, s
);
1435 *face_s
= lp_build_cube_coord(coord_bld
, sign
, +1, r
, ima
);
1436 *face_t
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1437 *face
= lp_build_cube_face(bld
, rx
,
1438 PIPE_TEX_FACE_POS_X
,
1439 PIPE_TEX_FACE_NEG_X
);
1441 lp_build_else(&if_ctx
);
1443 struct lp_build_flow_context
*flow_ctx2
;
1444 struct lp_build_if_state if_ctx2
;
1446 LLVMValueRef face_s2
= bld
->coord_bld
.undef
;
1447 LLVMValueRef face_t2
= bld
->coord_bld
.undef
;
1448 LLVMValueRef face2
= bld
->int_bld
.undef
;
1450 flow_ctx2
= lp_build_flow_create(bld
->builder
);
1451 lp_build_flow_scope_begin(flow_ctx2
);
1452 lp_build_flow_scope_declare(flow_ctx2
, &face_s2
);
1453 lp_build_flow_scope_declare(flow_ctx2
, &face_t2
);
1454 lp_build_flow_scope_declare(flow_ctx2
, &face2
);
1456 ary_ge_arx_arz
= LLVMBuildAnd(bld
->builder
, ary_ge_arx
, ary_ge_arz
, "");
1458 lp_build_if(&if_ctx2
, flow_ctx2
, bld
->builder
, ary_ge_arx_arz
);
1461 LLVMValueRef sign
= lp_build_sgn(float_bld
, ry
);
1462 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, t
);
1463 face_s2
= lp_build_cube_coord(coord_bld
, NULL
, -1, s
, ima
);
1464 face_t2
= lp_build_cube_coord(coord_bld
, sign
, -1, r
, ima
);
1465 face2
= lp_build_cube_face(bld
, ry
,
1466 PIPE_TEX_FACE_POS_Y
,
1467 PIPE_TEX_FACE_NEG_Y
);
1469 lp_build_else(&if_ctx2
);
1472 LLVMValueRef sign
= lp_build_sgn(float_bld
, rz
);
1473 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, r
);
1474 face_s2
= lp_build_cube_coord(coord_bld
, sign
, -1, s
, ima
);
1475 face_t2
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1476 face2
= lp_build_cube_face(bld
, rz
,
1477 PIPE_TEX_FACE_POS_Z
,
1478 PIPE_TEX_FACE_NEG_Z
);
1480 lp_build_endif(&if_ctx2
);
1481 lp_build_flow_scope_end(flow_ctx2
);
1482 lp_build_flow_destroy(flow_ctx2
);
1489 lp_build_endif(&if_ctx
);
1490 lp_build_flow_scope_end(flow_ctx
);
1491 lp_build_flow_destroy(flow_ctx
);
1498 * Sample the texture/mipmap using given image filter and mip filter.
1499 * data0_ptr and data1_ptr point to the two mipmap levels to sample
1500 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
1501 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1504 lp_build_sample_mipmap(struct lp_build_sample_context
*bld
,
1505 unsigned img_filter
,
1506 unsigned mip_filter
,
1510 LLVMValueRef lod_fpart
,
1511 LLVMValueRef width0_vec
,
1512 LLVMValueRef width1_vec
,
1513 LLVMValueRef height0_vec
,
1514 LLVMValueRef height1_vec
,
1515 LLVMValueRef depth0_vec
,
1516 LLVMValueRef depth1_vec
,
1517 LLVMValueRef row_stride0_vec
,
1518 LLVMValueRef row_stride1_vec
,
1519 LLVMValueRef img_stride0_vec
,
1520 LLVMValueRef img_stride1_vec
,
1521 LLVMValueRef data_ptr0
,
1522 LLVMValueRef data_ptr1
,
1523 LLVMValueRef
*colors_out
)
1525 LLVMValueRef colors0
[4], colors1
[4];
1528 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
1529 lp_build_sample_image_nearest(bld
,
1530 width0_vec
, height0_vec
, depth0_vec
,
1531 row_stride0_vec
, img_stride0_vec
,
1532 data_ptr0
, s
, t
, r
, colors0
);
1534 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1535 /* sample the second mipmap level, and interp */
1536 lp_build_sample_image_nearest(bld
,
1537 width1_vec
, height1_vec
, depth1_vec
,
1538 row_stride1_vec
, img_stride1_vec
,
1539 data_ptr1
, s
, t
, r
, colors1
);
1543 assert(img_filter
== PIPE_TEX_FILTER_LINEAR
);
1545 lp_build_sample_image_linear(bld
,
1546 width0_vec
, height0_vec
, depth0_vec
,
1547 row_stride0_vec
, img_stride0_vec
,
1548 data_ptr0
, s
, t
, r
, colors0
);
1550 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1551 /* sample the second mipmap level, and interp */
1552 lp_build_sample_image_linear(bld
,
1553 width1_vec
, height1_vec
, depth1_vec
,
1554 row_stride1_vec
, img_stride1_vec
,
1555 data_ptr1
, s
, t
, r
, colors1
);
1559 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1560 /* interpolate samples from the two mipmap levels */
1561 for (chan
= 0; chan
< 4; chan
++) {
1562 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
1563 colors0
[chan
], colors1
[chan
]);
1567 /* use first/only level's colors */
1568 for (chan
= 0; chan
< 4; chan
++) {
1569 colors_out
[chan
] = colors0
[chan
];
1577 * General texture sampling codegen.
1578 * This function handles texture sampling for all texture targets (1D,
1579 * 2D, 3D, cube) and all filtering modes.
1582 lp_build_sample_general(struct lp_build_sample_context
*bld
,
1587 LLVMValueRef lodbias
,
1589 LLVMValueRef height
,
1591 LLVMValueRef width_vec
,
1592 LLVMValueRef height_vec
,
1593 LLVMValueRef depth_vec
,
1594 LLVMValueRef row_stride_array
,
1595 LLVMValueRef img_stride_array
,
1596 LLVMValueRef data_array
,
1597 LLVMValueRef
*colors_out
)
1599 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1600 const unsigned mip_filter
= bld
->static_state
->min_mip_filter
;
1601 const unsigned min_filter
= bld
->static_state
->min_img_filter
;
1602 const unsigned mag_filter
= bld
->static_state
->mag_img_filter
;
1603 const int dims
= texture_dims(bld
->static_state
->target
);
1604 LLVMValueRef lod
= NULL
, lod_fpart
= NULL
;
1605 LLVMValueRef ilevel0
, ilevel1
= NULL
, ilevel0_vec
, ilevel1_vec
= NULL
;
1606 LLVMValueRef width0_vec
= NULL
, height0_vec
= NULL
, depth0_vec
= NULL
;
1607 LLVMValueRef width1_vec
= NULL
, height1_vec
= NULL
, depth1_vec
= NULL
;
1608 LLVMValueRef row_stride0_vec
= NULL
, row_stride1_vec
= NULL
;
1609 LLVMValueRef img_stride0_vec
= NULL
, img_stride1_vec
= NULL
;
1610 LLVMValueRef data_ptr0
, data_ptr1
= NULL
;
1613 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1614 mip_filter, min_filter, mag_filter);
1618 * Compute the level of detail (float).
1620 if (min_filter
!= mag_filter
||
1621 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
1622 /* Need to compute lod either to choose mipmap levels or to
1623 * distinguish between minification/magnification with one mipmap level.
1625 lod
= lp_build_lod_selector(bld
, s
, t
, r
, lodbias
, width
, height
, depth
);
1629 * Compute integer mipmap level(s) to fetch texels from.
1631 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
1632 /* always use mip level 0 */
1633 ilevel0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
1636 if (mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
1637 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
1640 assert(mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
);
1641 lp_build_linear_mip_levels(bld
, unit
, lod
, &ilevel0
, &ilevel1
,
1643 lod_fpart
= lp_build_broadcast_scalar(&bld
->coord_bld
, lod_fpart
);
1648 * Convert scalar integer mipmap levels into vectors.
1650 ilevel0_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel0
);
1651 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
)
1652 ilevel1_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel1
);
1655 * Compute width, height at mipmap level 'ilevel0'
1657 width0_vec
= lp_build_minify(bld
, width_vec
, ilevel0_vec
);
1659 height0_vec
= lp_build_minify(bld
, height_vec
, ilevel0_vec
);
1660 row_stride0_vec
= lp_build_get_level_stride_vec(bld
, row_stride_array
,
1662 if (dims
== 3 || bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1663 img_stride0_vec
= lp_build_get_level_stride_vec(bld
,
1667 depth0_vec
= lp_build_minify(bld
, depth_vec
, ilevel0_vec
);
1671 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1672 /* compute width, height, depth for second mipmap level at 'ilevel1' */
1673 width1_vec
= lp_build_minify(bld
, width_vec
, ilevel1_vec
);
1675 height1_vec
= lp_build_minify(bld
, height_vec
, ilevel1_vec
);
1676 row_stride1_vec
= lp_build_get_level_stride_vec(bld
, row_stride_array
,
1678 if (dims
== 3 || bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1679 img_stride1_vec
= lp_build_get_level_stride_vec(bld
,
1683 depth1_vec
= lp_build_minify(bld
, depth_vec
, ilevel1_vec
);
1690 * Choose cube face, recompute per-face texcoords.
1692 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1693 LLVMValueRef face
, face_s
, face_t
;
1694 lp_build_cube_lookup(bld
, s
, t
, r
, &face
, &face_s
, &face_t
);
1695 s
= face_s
; /* vec */
1696 t
= face_t
; /* vec */
1697 /* use 'r' to indicate cube face */
1698 r
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, face
); /* vec */
1702 * Get pointer(s) to image data for mipmap level(s).
1704 data_ptr0
= lp_build_get_mipmap_level(bld
, data_array
, ilevel0
);
1705 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1706 data_ptr1
= lp_build_get_mipmap_level(bld
, data_array
, ilevel1
);
1710 * Get/interpolate texture colors.
1712 if (min_filter
== mag_filter
) {
1713 /* no need to distinquish between minification and magnification */
1714 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
, s
, t
, r
, lod_fpart
,
1715 width0_vec
, width1_vec
,
1716 height0_vec
, height1_vec
,
1717 depth0_vec
, depth1_vec
,
1718 row_stride0_vec
, row_stride1_vec
,
1719 img_stride0_vec
, img_stride1_vec
,
1720 data_ptr0
, data_ptr1
,
1724 /* Emit conditional to choose min image filter or mag image filter
1725 * depending on the lod being >0 or <= 0, respectively.
1727 struct lp_build_flow_context
*flow_ctx
;
1728 struct lp_build_if_state if_ctx
;
1729 LLVMValueRef minify
;
1731 flow_ctx
= lp_build_flow_create(bld
->builder
);
1732 lp_build_flow_scope_begin(flow_ctx
);
1734 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[0]);
1735 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[1]);
1736 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[2]);
1737 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[3]);
1739 /* minify = lod > 0.0 */
1740 minify
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
1741 lod
, float_bld
->zero
, "");
1743 lp_build_if(&if_ctx
, flow_ctx
, bld
->builder
, minify
);
1745 /* Use the minification filter */
1746 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
,
1748 width0_vec
, width1_vec
,
1749 height0_vec
, height1_vec
,
1750 depth0_vec
, depth1_vec
,
1751 row_stride0_vec
, row_stride1_vec
,
1752 img_stride0_vec
, img_stride1_vec
,
1753 data_ptr0
, data_ptr1
,
1756 lp_build_else(&if_ctx
);
1758 /* Use the magnification filter */
1759 lp_build_sample_mipmap(bld
, mag_filter
, mip_filter
,
1761 width0_vec
, width1_vec
,
1762 height0_vec
, height1_vec
,
1763 depth0_vec
, depth1_vec
,
1764 row_stride0_vec
, row_stride1_vec
,
1765 img_stride0_vec
, img_stride1_vec
,
1766 data_ptr0
, data_ptr1
,
1769 lp_build_endif(&if_ctx
);
1771 lp_build_flow_scope_end(flow_ctx
);
1772 lp_build_flow_destroy(flow_ctx
);
1779 lp_build_rgba8_to_f32_soa(LLVMBuilderRef builder
,
1780 struct lp_type dst_type
,
1781 LLVMValueRef packed
,
1784 LLVMValueRef mask
= lp_build_const_int_vec(dst_type
, 0xff);
1787 /* Decode the input vector components */
1788 for (chan
= 0; chan
< 4; ++chan
) {
1789 unsigned start
= chan
*8;
1790 unsigned stop
= start
+ 8;
1796 input
= LLVMBuildLShr(builder
, input
, lp_build_const_int_vec(dst_type
, start
), "");
1799 input
= LLVMBuildAnd(builder
, input
, mask
, "");
1801 input
= lp_build_unsigned_norm_to_float(builder
, 8, dst_type
, input
);
1809 lp_build_sample_2d_linear_aos(struct lp_build_sample_context
*bld
,
1813 LLVMValueRef height
,
1814 LLVMValueRef stride_array
,
1815 LLVMValueRef data_array
,
1816 LLVMValueRef
*texel
)
1818 LLVMBuilderRef builder
= bld
->builder
;
1819 struct lp_build_context i32
, h16
, u8n
;
1820 LLVMTypeRef i32_vec_type
, h16_vec_type
, u8n_vec_type
;
1821 LLVMValueRef i32_c8
, i32_c128
, i32_c255
;
1822 LLVMValueRef s_ipart
, s_fpart
, s_fpart_lo
, s_fpart_hi
;
1823 LLVMValueRef t_ipart
, t_fpart
, t_fpart_lo
, t_fpart_hi
;
1824 LLVMValueRef x0
, x1
;
1825 LLVMValueRef y0
, y1
;
1826 LLVMValueRef neighbors
[2][2];
1827 LLVMValueRef neighbors_lo
[2][2];
1828 LLVMValueRef neighbors_hi
[2][2];
1829 LLVMValueRef packed
, packed_lo
, packed_hi
;
1830 LLVMValueRef unswizzled
[4];
1831 LLVMValueRef stride
;
1833 lp_build_context_init(&i32
, builder
, lp_type_int_vec(32));
1834 lp_build_context_init(&h16
, builder
, lp_type_ufixed(16));
1835 lp_build_context_init(&u8n
, builder
, lp_type_unorm(8));
1837 i32_vec_type
= lp_build_vec_type(i32
.type
);
1838 h16_vec_type
= lp_build_vec_type(h16
.type
);
1839 u8n_vec_type
= lp_build_vec_type(u8n
.type
);
1841 if (bld
->static_state
->normalized_coords
) {
1842 LLVMTypeRef coord_vec_type
= lp_build_vec_type(bld
->coord_type
);
1843 LLVMValueRef fp_width
= LLVMBuildSIToFP(bld
->builder
, width
, coord_vec_type
, "");
1844 LLVMValueRef fp_height
= LLVMBuildSIToFP(bld
->builder
, height
, coord_vec_type
, "");
1845 s
= lp_build_mul(&bld
->coord_bld
, s
, fp_width
);
1846 t
= lp_build_mul(&bld
->coord_bld
, t
, fp_height
);
1849 /* scale coords by 256 (8 fractional bits) */
1850 s
= lp_build_mul_imm(&bld
->coord_bld
, s
, 256);
1851 t
= lp_build_mul_imm(&bld
->coord_bld
, t
, 256);
1853 /* convert float to int */
1854 s
= LLVMBuildFPToSI(builder
, s
, i32_vec_type
, "");
1855 t
= LLVMBuildFPToSI(builder
, t
, i32_vec_type
, "");
1857 /* subtract 0.5 (add -128) */
1858 i32_c128
= lp_build_const_int_vec(i32
.type
, -128);
1859 s
= LLVMBuildAdd(builder
, s
, i32_c128
, "");
1860 t
= LLVMBuildAdd(builder
, t
, i32_c128
, "");
1862 /* compute floor (shift right 8) */
1863 i32_c8
= lp_build_const_int_vec(i32
.type
, 8);
1864 s_ipart
= LLVMBuildAShr(builder
, s
, i32_c8
, "");
1865 t_ipart
= LLVMBuildAShr(builder
, t
, i32_c8
, "");
1867 /* compute fractional part (AND with 0xff) */
1868 i32_c255
= lp_build_const_int_vec(i32
.type
, 255);
1869 s_fpart
= LLVMBuildAnd(builder
, s
, i32_c255
, "");
1870 t_fpart
= LLVMBuildAnd(builder
, t
, i32_c255
, "");
1875 x1
= lp_build_add(&bld
->int_coord_bld
, x0
, bld
->int_coord_bld
.one
);
1876 y1
= lp_build_add(&bld
->int_coord_bld
, y0
, bld
->int_coord_bld
.one
);
1878 x0
= lp_build_sample_wrap_int(bld
, x0
, width
, bld
->static_state
->pot_width
,
1879 bld
->static_state
->wrap_s
);
1880 y0
= lp_build_sample_wrap_int(bld
, y0
, height
, bld
->static_state
->pot_height
,
1881 bld
->static_state
->wrap_t
);
1883 x1
= lp_build_sample_wrap_int(bld
, x1
, width
, bld
->static_state
->pot_width
,
1884 bld
->static_state
->wrap_s
);
1885 y1
= lp_build_sample_wrap_int(bld
, y1
, height
, bld
->static_state
->pot_height
,
1886 bld
->static_state
->wrap_t
);
1889 * Transform 4 x i32 in
1891 * s_fpart = {s0, s1, s2, s3}
1895 * s_fpart = {00, s0, 00, s1, 00, s2, 00, s3}
1899 * s_fpart_lo = {s0, s0, s0, s0, s1, s1, s1, s1}
1900 * s_fpart_hi = {s2, s2, s2, s2, s3, s3, s3, s3}
1902 * and likewise for t_fpart. There is no risk of loosing precision here
1903 * since the fractional parts only use the lower 8bits.
1906 s_fpart
= LLVMBuildBitCast(builder
, s_fpart
, h16_vec_type
, "");
1907 t_fpart
= LLVMBuildBitCast(builder
, t_fpart
, h16_vec_type
, "");
1910 LLVMTypeRef elem_type
= LLVMInt32Type();
1911 LLVMValueRef shuffles_lo
[LP_MAX_VECTOR_LENGTH
];
1912 LLVMValueRef shuffles_hi
[LP_MAX_VECTOR_LENGTH
];
1913 LLVMValueRef shuffle_lo
;
1914 LLVMValueRef shuffle_hi
;
1917 for(j
= 0; j
< h16
.type
.length
; j
+= 4) {
1918 unsigned subindex
= util_cpu_caps
.little_endian
? 0 : 1;
1921 index
= LLVMConstInt(elem_type
, j
/2 + subindex
, 0);
1922 for(i
= 0; i
< 4; ++i
)
1923 shuffles_lo
[j
+ i
] = index
;
1925 index
= LLVMConstInt(elem_type
, h16
.type
.length
/2 + j
/2 + subindex
, 0);
1926 for(i
= 0; i
< 4; ++i
)
1927 shuffles_hi
[j
+ i
] = index
;
1930 shuffle_lo
= LLVMConstVector(shuffles_lo
, h16
.type
.length
);
1931 shuffle_hi
= LLVMConstVector(shuffles_hi
, h16
.type
.length
);
1933 s_fpart_lo
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_lo
, "");
1934 t_fpart_lo
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_lo
, "");
1935 s_fpart_hi
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_hi
, "");
1936 t_fpart_hi
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_hi
, "");
1939 stride
= lp_build_get_const_level_stride_vec(bld
, stride_array
, 0);
1942 * Fetch the pixels as 4 x 32bit (rgba order might differ):
1944 * rgba0 rgba1 rgba2 rgba3
1946 * bit cast them into 16 x u8
1948 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
1950 * unpack them into two 8 x i16:
1952 * r0 g0 b0 a0 r1 g1 b1 a1
1953 * r2 g2 b2 a2 r3 g3 b3 a3
1955 * The higher 8 bits of the resulting elements will be zero.
1958 neighbors
[0][0] = lp_build_sample_packed(bld
, x0
, y0
, stride
, data_array
);
1959 neighbors
[0][1] = lp_build_sample_packed(bld
, x1
, y0
, stride
, data_array
);
1960 neighbors
[1][0] = lp_build_sample_packed(bld
, x0
, y1
, stride
, data_array
);
1961 neighbors
[1][1] = lp_build_sample_packed(bld
, x1
, y1
, stride
, data_array
);
1963 neighbors
[0][0] = LLVMBuildBitCast(builder
, neighbors
[0][0], u8n_vec_type
, "");
1964 neighbors
[0][1] = LLVMBuildBitCast(builder
, neighbors
[0][1], u8n_vec_type
, "");
1965 neighbors
[1][0] = LLVMBuildBitCast(builder
, neighbors
[1][0], u8n_vec_type
, "");
1966 neighbors
[1][1] = LLVMBuildBitCast(builder
, neighbors
[1][1], u8n_vec_type
, "");
1968 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][0], &neighbors_lo
[0][0], &neighbors_hi
[0][0]);
1969 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][1], &neighbors_lo
[0][1], &neighbors_hi
[0][1]);
1970 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][0], &neighbors_lo
[1][0], &neighbors_hi
[1][0]);
1971 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][1], &neighbors_lo
[1][1], &neighbors_hi
[1][1]);
1974 * Linear interpolate with 8.8 fixed point.
1977 packed_lo
= lp_build_lerp_2d(&h16
,
1978 s_fpart_lo
, t_fpart_lo
,
1982 neighbors_lo
[1][1]);
1984 packed_hi
= lp_build_lerp_2d(&h16
,
1985 s_fpart_hi
, t_fpart_hi
,
1989 neighbors_hi
[1][1]);
1991 packed
= lp_build_pack2(builder
, h16
.type
, u8n
.type
, packed_lo
, packed_hi
);
1994 * Convert to SoA and swizzle.
1997 packed
= LLVMBuildBitCast(builder
, packed
, i32_vec_type
, "");
1999 lp_build_rgba8_to_f32_soa(bld
->builder
,
2001 packed
, unswizzled
);
2003 lp_build_format_swizzle_soa(bld
->format_desc
,
2004 bld
->texel_type
, unswizzled
,
2007 lp_build_swizzle_soa(bld
, texel
);
2012 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
2014 LLVMValueRef
*texel
)
2016 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
2020 if(bld
->static_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
2023 /* TODO: Compare before swizzling, to avoid redundant computations */
2025 for(chan
= 0; chan
< 4; ++chan
) {
2027 cmp
= lp_build_cmp(texel_bld
, bld
->static_state
->compare_func
, p
, texel
[chan
]);
2028 cmp
= lp_build_select(texel_bld
, cmp
, texel_bld
->one
, texel_bld
->zero
);
2031 res
= lp_build_add(texel_bld
, res
, cmp
);
2037 res
= lp_build_mul(texel_bld
, res
, lp_build_const_vec(texel_bld
->type
, 0.25));
2039 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
2040 for(chan
= 0; chan
< 3; ++chan
)
2042 texel
[3] = texel_bld
->one
;
2047 * Build texture sampling code.
2048 * 'texel' will return a vector of four LLVMValueRefs corresponding to
2050 * \param type vector float type to use for coords, etc.
2053 lp_build_sample_soa(LLVMBuilderRef builder
,
2054 const struct lp_sampler_static_state
*static_state
,
2055 struct lp_sampler_dynamic_state
*dynamic_state
,
2056 struct lp_type type
,
2058 unsigned num_coords
,
2059 const LLVMValueRef
*coords
,
2060 LLVMValueRef lodbias
,
2061 LLVMValueRef
*texel
)
2063 struct lp_build_sample_context bld
;
2064 LLVMValueRef width
, width_vec
;
2065 LLVMValueRef height
, height_vec
;
2066 LLVMValueRef depth
, depth_vec
;
2067 LLVMValueRef row_stride_array
, img_stride_array
;
2068 LLVMValueRef data_array
;
2073 /* Setup our build context */
2074 memset(&bld
, 0, sizeof bld
);
2075 bld
.builder
= builder
;
2076 bld
.static_state
= static_state
;
2077 bld
.dynamic_state
= dynamic_state
;
2078 bld
.format_desc
= util_format_description(static_state
->format
);
2080 bld
.float_type
= lp_type_float(32);
2081 bld
.int_type
= lp_type_int(32);
2082 bld
.coord_type
= type
;
2083 bld
.uint_coord_type
= lp_uint_type(type
);
2084 bld
.int_coord_type
= lp_int_type(type
);
2085 bld
.texel_type
= type
;
2087 lp_build_context_init(&bld
.float_bld
, builder
, bld
.float_type
);
2088 lp_build_context_init(&bld
.int_bld
, builder
, bld
.int_type
);
2089 lp_build_context_init(&bld
.coord_bld
, builder
, bld
.coord_type
);
2090 lp_build_context_init(&bld
.uint_coord_bld
, builder
, bld
.uint_coord_type
);
2091 lp_build_context_init(&bld
.int_coord_bld
, builder
, bld
.int_coord_type
);
2092 lp_build_context_init(&bld
.texel_bld
, builder
, bld
.texel_type
);
2094 /* Get the dynamic state */
2095 width
= dynamic_state
->width(dynamic_state
, builder
, unit
);
2096 height
= dynamic_state
->height(dynamic_state
, builder
, unit
);
2097 depth
= dynamic_state
->depth(dynamic_state
, builder
, unit
);
2098 row_stride_array
= dynamic_state
->row_stride(dynamic_state
, builder
, unit
);
2099 img_stride_array
= dynamic_state
->img_stride(dynamic_state
, builder
, unit
);
2100 data_array
= dynamic_state
->data_ptr(dynamic_state
, builder
, unit
);
2101 /* Note that data_array is an array[level] of pointers to texture images */
2107 width_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, width
);
2108 height_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, height
);
2109 depth_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, depth
);
2111 if (util_format_is_rgba8_variant(bld
.format_desc
) &&
2112 static_state
->target
== PIPE_TEXTURE_2D
&&
2113 static_state
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
2114 static_state
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
2115 static_state
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
&&
2116 is_simple_wrap_mode(static_state
->wrap_s
) &&
2117 is_simple_wrap_mode(static_state
->wrap_t
)) {
2119 lp_build_sample_2d_linear_aos(&bld
, s
, t
, width_vec
, height_vec
,
2120 row_stride_array
, data_array
, texel
);
2123 lp_build_sample_general(&bld
, unit
, s
, t
, r
, lodbias
,
2124 width
, height
, depth
,
2125 width_vec
, height_vec
, depth_vec
,
2126 row_stride_array
, img_stride_array
,
2131 lp_build_sample_compare(&bld
, r
, texel
);