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_format.h"
52 #include "lp_bld_sample.h"
56 * Keep all information for sampling code generation in a single place.
58 struct lp_build_sample_context
60 LLVMBuilderRef builder
;
62 const struct lp_sampler_static_state
*static_state
;
64 struct lp_sampler_dynamic_state
*dynamic_state
;
66 const struct util_format_description
*format_desc
;
68 /** Incoming coordinates type and build context */
69 struct lp_type coord_type
;
70 struct lp_build_context coord_bld
;
72 /** Unsigned integer coordinates */
73 struct lp_type uint_coord_type
;
74 struct lp_build_context uint_coord_bld
;
76 /** Signed integer coordinates */
77 struct lp_type int_coord_type
;
78 struct lp_build_context int_coord_bld
;
80 /** Output texels type and build context */
81 struct lp_type texel_type
;
82 struct lp_build_context texel_bld
;
87 * Does the given texture wrap mode allow sampling the texture border color?
88 * XXX maybe move this into gallium util code.
91 wrap_mode_uses_border_color(unsigned mode
)
94 case PIPE_TEX_WRAP_REPEAT
:
95 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
96 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
97 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
99 case PIPE_TEX_WRAP_CLAMP
:
100 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
101 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
102 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
105 assert(0 && "unexpected wrap mode");
113 * Gen code to fetch a texel from a texture at int coords (x, y).
114 * The result, texel, will be:
115 * texel[0] = red values
116 * texel[1] = green values
117 * texel[2] = blue values
118 * texel[3] = alpha values
121 lp_build_sample_texel_soa(struct lp_build_sample_context
*bld
,
126 LLVMValueRef y_stride
,
127 LLVMValueRef data_array
,
130 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
133 LLVMValueRef use_border
= NULL
;
134 LLVMValueRef data_ptr
;
136 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
137 if (wrap_mode_uses_border_color(bld
->static_state
->wrap_s
)) {
139 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
140 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
141 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
144 if (wrap_mode_uses_border_color(bld
->static_state
->wrap_t
)) {
146 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
147 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
149 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
150 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
153 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
157 /* XXX always use mipmap level 0 for now */
160 LLVMValueRef indexes
[2];
161 indexes
[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
162 indexes
[1] = LLVMConstInt(LLVMInt32Type(), level
, 0);
163 data_ptr
= LLVMBuildGEP(bld
->builder
, data_array
, indexes
, 2, "");
164 data_ptr
= LLVMBuildLoad(bld
->builder
, data_ptr
, "");
168 * Note: if we find an app which frequently samples the texture border
169 * we might want to implement a true conditional here to avoid sampling
170 * the texture whenever possible (since that's quite a bit of code).
173 * texel = border_color;
176 * texel = sample_texture(coord);
178 * As it is now, we always sample the texture, then selectively replace
179 * the texel color results with the border color.
182 /* convert x,y coords to linear offset from start of texture, in bytes */
183 offset
= lp_build_sample_offset(&bld
->uint_coord_bld
,
187 assert(bld
->format_desc
->block
.width
== 1);
188 assert(bld
->format_desc
->block
.height
== 1);
189 assert(bld
->format_desc
->block
.bits
<= bld
->texel_type
.width
);
191 /* gather the texels from the texture */
192 packed
= lp_build_gather(bld
->builder
,
193 bld
->texel_type
.length
,
194 bld
->format_desc
->block
.bits
,
195 bld
->texel_type
.width
,
198 /* convert texels to float rgba */
199 lp_build_unpack_rgba_soa(bld
->builder
,
205 /* select texel color or border color depending on use_border */
207 for (chan
= 0; chan
< 4; chan
++) {
208 LLVMValueRef border_chan
=
209 lp_build_const_scalar(bld
->texel_type
,
210 bld
->static_state
->border_color
[chan
]);
211 texel
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
212 border_chan
, texel
[chan
]);
219 lp_build_sample_packed(struct lp_build_sample_context
*bld
,
222 LLVMValueRef y_stride
,
223 LLVMValueRef data_array
)
226 LLVMValueRef data_ptr
;
228 offset
= lp_build_sample_offset(&bld
->uint_coord_bld
,
232 assert(bld
->format_desc
->block
.width
== 1);
233 assert(bld
->format_desc
->block
.height
== 1);
234 assert(bld
->format_desc
->block
.bits
<= bld
->texel_type
.width
);
236 /* XXX always use mipmap level 0 for now */
239 LLVMValueRef indexes
[2];
240 /* get data_ptr[level] */
241 indexes
[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
242 indexes
[1] = LLVMConstInt(LLVMInt32Type(), level
, 0);
243 data_ptr
= LLVMBuildGEP(bld
->builder
, data_array
, indexes
, 2, "");
244 /* load texture base address */
245 data_ptr
= LLVMBuildLoad(bld
->builder
, data_ptr
, "");
248 return lp_build_gather(bld
->builder
,
249 bld
->texel_type
.length
,
250 bld
->format_desc
->block
.bits
,
251 bld
->texel_type
.width
,
257 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
260 lp_build_coord_mirror(struct lp_build_sample_context
*bld
,
263 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
264 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
265 LLVMValueRef fract
, flr
, isOdd
;
267 /* fract = coord - floor(coord) */
268 fract
= lp_build_sub(coord_bld
, coord
, lp_build_floor(coord_bld
, coord
));
270 /* flr = ifloor(coord); */
271 flr
= lp_build_ifloor(coord_bld
, coord
);
273 /* isOdd = flr & 1 */
274 isOdd
= LLVMBuildAnd(bld
->builder
, flr
, int_coord_bld
->one
, "");
276 /* make coord positive or negative depending on isOdd */
277 coord
= lp_build_set_sign(coord_bld
, fract
, isOdd
);
279 /* convert isOdd to float */
280 isOdd
= lp_build_int_to_float(coord_bld
, isOdd
);
282 /* add isOdd to coord */
283 coord
= lp_build_add(coord_bld
, coord
, isOdd
);
290 * We only support a few wrap modes in lp_build_sample_wrap_int() at this time.
291 * Return whether the given mode is supported by that function.
294 is_simple_wrap_mode(unsigned mode
)
297 case PIPE_TEX_WRAP_REPEAT
:
298 case PIPE_TEX_WRAP_CLAMP
:
299 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
301 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
309 * Build LLVM code for texture wrap mode, for scaled integer texcoords.
310 * \param coord the incoming texcoord (s,t,r or q) scaled to the texture size
311 * \param length the texture size along one dimension
312 * \param is_pot if TRUE, length is a power of two
313 * \param wrap_mode one of PIPE_TEX_WRAP_x
316 lp_build_sample_wrap_int(struct lp_build_sample_context
*bld
,
322 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
323 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
324 LLVMValueRef length_minus_one
;
326 length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
329 case PIPE_TEX_WRAP_REPEAT
:
331 coord
= LLVMBuildAnd(bld
->builder
, coord
, length_minus_one
, "");
333 /* Signed remainder won't give the right results for negative
334 * dividends but unsigned remainder does.*/
335 coord
= LLVMBuildURem(bld
->builder
, coord
, length
, "");
338 case PIPE_TEX_WRAP_CLAMP
:
339 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
340 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
341 coord
= lp_build_max(int_coord_bld
, coord
, int_coord_bld
->zero
);
342 coord
= lp_build_min(int_coord_bld
, coord
, length_minus_one
);
345 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
346 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
347 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
348 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
350 _debug_printf("llvmpipe: failed to translate texture wrap mode %s\n",
351 util_dump_tex_wrap(wrap_mode
, TRUE
));
352 coord
= lp_build_max(uint_coord_bld
, coord
, uint_coord_bld
->zero
);
353 coord
= lp_build_min(uint_coord_bld
, coord
, length_minus_one
);
365 * Build LLVM code for texture wrap mode for linear filtering.
366 * \param x0_out returns first integer texcoord
367 * \param x1_out returns second integer texcoord
368 * \param weight_out returns linear interpolation weight
371 lp_build_sample_wrap_linear(struct lp_build_sample_context
*bld
,
376 LLVMValueRef
*x0_out
,
377 LLVMValueRef
*x1_out
,
378 LLVMValueRef
*weight_out
)
380 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
381 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
382 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
383 LLVMValueRef two
= lp_build_const_scalar(coord_bld
->type
, 2.0);
384 LLVMValueRef half
= lp_build_const_scalar(coord_bld
->type
, 0.5);
385 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
386 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
387 LLVMValueRef length_f_minus_one
= lp_build_sub(coord_bld
, length_f
, coord_bld
->one
);
388 LLVMValueRef coord0
, coord1
, weight
;
391 case PIPE_TEX_WRAP_REPEAT
:
392 /* mul by size and subtract 0.5 */
393 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
394 coord
= lp_build_sub(coord_bld
, coord
, half
);
396 coord0
= lp_build_ifloor(coord_bld
, coord
);
397 coord1
= lp_build_add(uint_coord_bld
, coord0
, uint_coord_bld
->one
);
398 /* compute lerp weight */
399 weight
= lp_build_fract(coord_bld
, coord
);
402 coord0
= LLVMBuildAnd(bld
->builder
, coord0
, length_minus_one
, "");
403 coord1
= LLVMBuildAnd(bld
->builder
, coord1
, length_minus_one
, "");
406 /* Signed remainder won't give the right results for negative
407 * dividends but unsigned remainder does.*/
408 coord0
= LLVMBuildURem(bld
->builder
, coord0
, length
, "");
409 coord1
= LLVMBuildURem(bld
->builder
, coord1
, length
, "");
413 case PIPE_TEX_WRAP_CLAMP
:
414 if (bld
->static_state
->normalized_coords
) {
415 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
417 weight
= lp_build_fract(coord_bld
, coord
);
418 coord0
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
,
420 coord1
= lp_build_add(coord_bld
, coord
, coord_bld
->one
);
421 coord1
= lp_build_clamp(coord_bld
, coord1
, coord_bld
->zero
,
423 coord0
= lp_build_ifloor(coord_bld
, coord0
);
424 coord1
= lp_build_ifloor(coord_bld
, coord1
);
427 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
428 if (bld
->static_state
->normalized_coords
) {
430 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, coord_bld
->one
);
431 /* mul by tex size and subtract 0.5 */
432 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
433 coord
= lp_build_sub(coord_bld
, coord
, half
);
436 LLVMValueRef min
, max
;
437 /* clamp to [0.5, length - 0.5] */
438 min
= lp_build_const_scalar(coord_bld
->type
, 0.5F
);
439 max
= lp_build_sub(coord_bld
, length_f
, min
);
440 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
442 /* compute lerp weight */
443 weight
= lp_build_fract(coord_bld
, coord
);
444 /* coord0 = floor(coord); */
445 coord0
= lp_build_ifloor(coord_bld
, coord
);
446 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
447 /* coord0 = max(coord0, 0) */
448 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
449 /* coord1 = min(coord1, length-1) */
450 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
453 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
455 LLVMValueRef min
, max
;
456 if (bld
->static_state
->normalized_coords
) {
457 /* min = -1.0 / (2 * length) = -0.5 / length */
458 min
= lp_build_mul(coord_bld
,
459 lp_build_const_scalar(coord_bld
->type
, -0.5F
),
460 lp_build_rcp(coord_bld
, length_f
));
461 /* max = 1.0 - min */
462 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
463 /* coord = clamp(coord, min, max) */
464 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
465 /* scale coord to length (and sub 0.5?) */
466 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
467 coord
= lp_build_sub(coord_bld
, coord
, half
);
470 /* clamp to [-0.5, length + 0.5] */
471 min
= lp_build_const_scalar(coord_bld
->type
, -0.5F
);
472 max
= lp_build_sub(coord_bld
, length_f
, min
);
473 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
474 coord
= lp_build_sub(coord_bld
, coord
, half
);
476 /* compute lerp weight */
477 weight
= lp_build_fract(coord_bld
, coord
);
479 coord0
= lp_build_ifloor(coord_bld
, coord
);
480 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
484 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
485 /* compute mirror function */
486 coord
= lp_build_coord_mirror(bld
, coord
);
488 /* scale coord to length */
489 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
490 coord
= lp_build_sub(coord_bld
, coord
, half
);
492 /* compute lerp weight */
493 weight
= lp_build_fract(coord_bld
, coord
);
495 /* convert to int coords */
496 coord0
= lp_build_ifloor(coord_bld
, coord
);
497 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
499 /* coord0 = max(coord0, 0) */
500 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
501 /* coord1 = min(coord1, length-1) */
502 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
505 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
507 LLVMValueRef min
, max
;
508 /* min = 1.0 / (2 * length) */
509 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
510 /* max = 1.0 - min */
511 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
513 coord
= lp_build_abs(coord_bld
, coord
);
514 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
515 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
516 if(0)coord
= lp_build_sub(coord_bld
, coord
, half
);
517 weight
= lp_build_fract(coord_bld
, coord
);
518 coord0
= lp_build_ifloor(coord_bld
, coord
);
519 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
523 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
525 LLVMValueRef min
, max
;
526 /* min = 1.0 / (2 * length) */
527 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
528 /* max = 1.0 - min */
529 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
531 coord
= lp_build_abs(coord_bld
, coord
);
532 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
533 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
534 coord
= lp_build_sub(coord_bld
, coord
, half
);
535 weight
= lp_build_fract(coord_bld
, coord
);
536 coord0
= lp_build_ifloor(coord_bld
, coord
);
537 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
541 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
543 LLVMValueRef min
, max
;
544 /* min = -1.0 / (2 * length) = -0.5 / length */
545 min
= lp_build_mul(coord_bld
,
546 lp_build_const_scalar(coord_bld
->type
, -0.5F
),
547 lp_build_rcp(coord_bld
, length_f
));
548 /* max = 1.0 - min */
549 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
551 coord
= lp_build_abs(coord_bld
, coord
);
552 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
553 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
554 coord
= lp_build_sub(coord_bld
, coord
, half
);
555 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
);
570 *weight_out
= weight
;
575 * Build LLVM code for texture wrap mode for nearest filtering.
576 * \param coord the incoming texcoord (nominally in [0,1])
577 * \param length the texture size along one dimension, as int
578 * \param is_pot if TRUE, length is a power of two
579 * \param wrap_mode one of PIPE_TEX_WRAP_x
582 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
588 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
589 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
590 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
591 LLVMValueRef two
= lp_build_const_scalar(coord_bld
->type
, 2.0);
592 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
593 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
594 LLVMValueRef length_f_minus_one
= lp_build_sub(coord_bld
, length_f
, coord_bld
->one
);
598 case PIPE_TEX_WRAP_REPEAT
:
599 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
600 icoord
= lp_build_ifloor(coord_bld
, coord
);
602 icoord
= LLVMBuildAnd(bld
->builder
, icoord
, length_minus_one
, "");
604 /* Signed remainder won't give the right results for negative
605 * dividends but unsigned remainder does.*/
606 icoord
= LLVMBuildURem(bld
->builder
, icoord
, length
, "");
609 case PIPE_TEX_WRAP_CLAMP
:
611 if (bld
->static_state
->normalized_coords
) {
612 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
615 icoord
= lp_build_ifloor(coord_bld
, coord
);
616 /* clamp to [0, size-1]. Note: int coord builder type */
617 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
621 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
623 LLVMValueRef min
, max
;
624 if (bld
->static_state
->normalized_coords
) {
625 /* min = 1.0 / (2 * length) */
626 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
627 /* max = length - min */
628 max
= lp_build_sub(coord_bld
, length_f
, min
);
629 /* scale coord to length */
630 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
633 /* clamp to [0.5, length - 0.5] */
634 min
= lp_build_const_scalar(coord_bld
->type
, 0.5F
);
635 max
= lp_build_sub(coord_bld
, length_f
, min
);
637 /* coord = clamp(coord, min, max) */
638 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
639 icoord
= lp_build_ifloor(coord_bld
, coord
);
643 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
644 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
646 LLVMValueRef min
, max
;
647 if (bld
->static_state
->normalized_coords
) {
648 /* min = -1.0 / (2 * length) = -0.5 / length */
649 min
= lp_build_mul(coord_bld
,
650 lp_build_const_scalar(coord_bld
->type
, -0.5F
),
651 lp_build_rcp(coord_bld
, length_f
));
652 /* max = length - min */
653 max
= lp_build_sub(coord_bld
, length_f
, min
);
654 /* scale coord to length */
655 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
658 /* clamp to [-0.5, length + 0.5] */
659 min
= lp_build_const_scalar(coord_bld
->type
, -0.5F
);
660 max
= lp_build_sub(coord_bld
, length_f
, min
);
662 /* coord = clamp(coord, min, max) */
663 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
664 icoord
= lp_build_ifloor(coord_bld
, coord
);
668 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
670 LLVMValueRef min
, max
;
671 /* min = 1.0 / (2 * length) */
672 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
673 /* max = length - min */
674 max
= lp_build_sub(coord_bld
, length_f
, min
);
676 /* compute mirror function */
677 coord
= lp_build_coord_mirror(bld
, coord
);
679 /* scale coord to length */
680 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
682 /* coord = clamp(coord, min, max) */
683 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
684 icoord
= lp_build_ifloor(coord_bld
, coord
);
688 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
689 coord
= lp_build_abs(coord_bld
, coord
);
690 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
691 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, length_f_minus_one
);
692 icoord
= lp_build_ifloor(coord_bld
, coord
);
695 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
697 LLVMValueRef min
, max
;
698 /* min = 1.0 / (2 * length) */
699 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
700 /* max = length - min */
701 max
= lp_build_sub(coord_bld
, length_f
, min
);
703 coord
= lp_build_abs(coord_bld
, coord
);
704 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
705 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
706 icoord
= lp_build_ifloor(coord_bld
, coord
);
710 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
712 LLVMValueRef min
, max
;
713 /* min = 1.0 / (2 * length) */
714 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
715 min
= lp_build_negate(coord_bld
, min
);
716 /* max = length - min */
717 max
= lp_build_sub(coord_bld
, length_f
, min
);
719 coord
= lp_build_abs(coord_bld
, coord
);
720 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
721 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
722 icoord
= lp_build_ifloor(coord_bld
, coord
);
736 * Sample 2D texture with nearest filtering.
739 lp_build_sample_2d_nearest_soa(struct lp_build_sample_context
*bld
,
745 LLVMValueRef data_array
,
750 x
= lp_build_sample_wrap_nearest(bld
, s
, width
,
751 bld
->static_state
->pot_width
,
752 bld
->static_state
->wrap_s
);
753 y
= lp_build_sample_wrap_nearest(bld
, t
, height
,
754 bld
->static_state
->pot_height
,
755 bld
->static_state
->wrap_t
);
757 lp_build_name(x
, "tex.x.wrapped");
758 lp_build_name(y
, "tex.y.wrapped");
760 lp_build_sample_texel_soa(bld
, width
, height
, x
, y
, stride
, data_array
, texel
);
765 * Sample 2D texture with bilinear filtering.
768 lp_build_sample_2d_linear_soa(struct lp_build_sample_context
*bld
,
774 LLVMValueRef data_array
,
777 LLVMValueRef s_fpart
;
778 LLVMValueRef t_fpart
;
781 LLVMValueRef neighbors
[2][2][4];
784 lp_build_sample_wrap_linear(bld
, s
, width
, bld
->static_state
->pot_width
,
785 bld
->static_state
->wrap_s
, &x0
, &x1
, &s_fpart
);
786 lp_build_sample_wrap_linear(bld
, t
, height
, bld
->static_state
->pot_height
,
787 bld
->static_state
->wrap_t
, &y0
, &y1
, &t_fpart
);
789 lp_build_sample_texel_soa(bld
, width
, height
, x0
, y0
, stride
, data_array
, neighbors
[0][0]);
790 lp_build_sample_texel_soa(bld
, width
, height
, x1
, y0
, stride
, data_array
, neighbors
[0][1]);
791 lp_build_sample_texel_soa(bld
, width
, height
, x0
, y1
, stride
, data_array
, neighbors
[1][0]);
792 lp_build_sample_texel_soa(bld
, width
, height
, x1
, y1
, stride
, data_array
, neighbors
[1][1]);
794 /* TODO: Don't interpolate missing channels */
795 for(chan
= 0; chan
< 4; ++chan
) {
796 texel
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
798 neighbors
[0][0][chan
],
799 neighbors
[0][1][chan
],
800 neighbors
[1][0][chan
],
801 neighbors
[1][1][chan
]);
807 lp_build_rgba8_to_f32_soa(LLVMBuilderRef builder
,
808 struct lp_type dst_type
,
812 LLVMValueRef mask
= lp_build_int_const_scalar(dst_type
, 0xff);
815 /* Decode the input vector components */
816 for (chan
= 0; chan
< 4; ++chan
) {
817 unsigned start
= chan
*8;
818 unsigned stop
= start
+ 8;
824 input
= LLVMBuildLShr(builder
, input
, lp_build_int_const_scalar(dst_type
, start
), "");
827 input
= LLVMBuildAnd(builder
, input
, mask
, "");
829 input
= lp_build_unsigned_norm_to_float(builder
, 8, dst_type
, input
);
837 lp_build_sample_2d_linear_aos(struct lp_build_sample_context
*bld
,
843 LLVMValueRef data_array
,
846 LLVMBuilderRef builder
= bld
->builder
;
847 struct lp_build_context i32
, h16
, u8n
;
848 LLVMTypeRef i32_vec_type
, h16_vec_type
, u8n_vec_type
;
849 LLVMValueRef i32_c8
, i32_c128
, i32_c255
;
850 LLVMValueRef s_ipart
, s_fpart
, s_fpart_lo
, s_fpart_hi
;
851 LLVMValueRef t_ipart
, t_fpart
, t_fpart_lo
, t_fpart_hi
;
854 LLVMValueRef neighbors
[2][2];
855 LLVMValueRef neighbors_lo
[2][2];
856 LLVMValueRef neighbors_hi
[2][2];
857 LLVMValueRef packed
, packed_lo
, packed_hi
;
858 LLVMValueRef unswizzled
[4];
860 lp_build_context_init(&i32
, builder
, lp_type_int(32));
861 lp_build_context_init(&h16
, builder
, lp_type_ufixed(16));
862 lp_build_context_init(&u8n
, builder
, lp_type_unorm(8));
864 i32_vec_type
= lp_build_vec_type(i32
.type
);
865 h16_vec_type
= lp_build_vec_type(h16
.type
);
866 u8n_vec_type
= lp_build_vec_type(u8n
.type
);
868 if (bld
->static_state
->normalized_coords
) {
869 LLVMTypeRef coord_vec_type
= lp_build_vec_type(bld
->coord_type
);
870 LLVMValueRef fp_width
= LLVMBuildSIToFP(bld
->builder
, width
, coord_vec_type
, "");
871 LLVMValueRef fp_height
= LLVMBuildSIToFP(bld
->builder
, height
, coord_vec_type
, "");
872 s
= lp_build_mul(&bld
->coord_bld
, s
, fp_width
);
873 t
= lp_build_mul(&bld
->coord_bld
, t
, fp_height
);
876 /* scale coords by 256 (8 fractional bits) */
877 s
= lp_build_mul_imm(&bld
->coord_bld
, s
, 256);
878 t
= lp_build_mul_imm(&bld
->coord_bld
, t
, 256);
880 /* convert float to int */
881 s
= LLVMBuildFPToSI(builder
, s
, i32_vec_type
, "");
882 t
= LLVMBuildFPToSI(builder
, t
, i32_vec_type
, "");
884 /* subtract 0.5 (add -128) */
885 i32_c128
= lp_build_int_const_scalar(i32
.type
, -128);
886 s
= LLVMBuildAdd(builder
, s
, i32_c128
, "");
887 t
= LLVMBuildAdd(builder
, t
, i32_c128
, "");
889 /* compute floor (shift right 8) */
890 i32_c8
= lp_build_int_const_scalar(i32
.type
, 8);
891 s_ipart
= LLVMBuildAShr(builder
, s
, i32_c8
, "");
892 t_ipart
= LLVMBuildAShr(builder
, t
, i32_c8
, "");
894 /* compute fractional part (AND with 0xff) */
895 i32_c255
= lp_build_int_const_scalar(i32
.type
, 255);
896 s_fpart
= LLVMBuildAnd(builder
, s
, i32_c255
, "");
897 t_fpart
= LLVMBuildAnd(builder
, t
, i32_c255
, "");
902 x1
= lp_build_add(&bld
->int_coord_bld
, x0
, bld
->int_coord_bld
.one
);
903 y1
= lp_build_add(&bld
->int_coord_bld
, y0
, bld
->int_coord_bld
.one
);
905 x0
= lp_build_sample_wrap_int(bld
, x0
, width
, bld
->static_state
->pot_width
,
906 bld
->static_state
->wrap_s
);
907 y0
= lp_build_sample_wrap_int(bld
, y0
, height
, bld
->static_state
->pot_height
,
908 bld
->static_state
->wrap_t
);
910 x1
= lp_build_sample_wrap_int(bld
, x1
, width
, bld
->static_state
->pot_width
,
911 bld
->static_state
->wrap_s
);
912 y1
= lp_build_sample_wrap_int(bld
, y1
, height
, bld
->static_state
->pot_height
,
913 bld
->static_state
->wrap_t
);
916 * Transform 4 x i32 in
918 * s_fpart = {s0, s1, s2, s3}
922 * s_fpart = {00, s0, 00, s1, 00, s2, 00, s3}
926 * s_fpart_lo = {s0, s0, s0, s0, s1, s1, s1, s1}
927 * s_fpart_hi = {s2, s2, s2, s2, s3, s3, s3, s3}
929 * and likewise for t_fpart. There is no risk of loosing precision here
930 * since the fractional parts only use the lower 8bits.
933 s_fpart
= LLVMBuildBitCast(builder
, s_fpart
, h16_vec_type
, "");
934 t_fpart
= LLVMBuildBitCast(builder
, t_fpart
, h16_vec_type
, "");
937 LLVMTypeRef elem_type
= LLVMInt32Type();
938 LLVMValueRef shuffles_lo
[LP_MAX_VECTOR_LENGTH
];
939 LLVMValueRef shuffles_hi
[LP_MAX_VECTOR_LENGTH
];
940 LLVMValueRef shuffle_lo
;
941 LLVMValueRef shuffle_hi
;
944 for(j
= 0; j
< h16
.type
.length
; j
+= 4) {
945 unsigned subindex
= util_cpu_caps
.little_endian
? 0 : 1;
948 index
= LLVMConstInt(elem_type
, j
/2 + subindex
, 0);
949 for(i
= 0; i
< 4; ++i
)
950 shuffles_lo
[j
+ i
] = index
;
952 index
= LLVMConstInt(elem_type
, h16
.type
.length
/2 + j
/2 + subindex
, 0);
953 for(i
= 0; i
< 4; ++i
)
954 shuffles_hi
[j
+ i
] = index
;
957 shuffle_lo
= LLVMConstVector(shuffles_lo
, h16
.type
.length
);
958 shuffle_hi
= LLVMConstVector(shuffles_hi
, h16
.type
.length
);
960 s_fpart_lo
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_lo
, "");
961 t_fpart_lo
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_lo
, "");
962 s_fpart_hi
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_hi
, "");
963 t_fpart_hi
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_hi
, "");
967 * Fetch the pixels as 4 x 32bit (rgba order might differ):
969 * rgba0 rgba1 rgba2 rgba3
971 * bit cast them into 16 x u8
973 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
975 * unpack them into two 8 x i16:
977 * r0 g0 b0 a0 r1 g1 b1 a1
978 * r2 g2 b2 a2 r3 g3 b3 a3
980 * The higher 8 bits of the resulting elements will be zero.
983 neighbors
[0][0] = lp_build_sample_packed(bld
, x0
, y0
, stride
, data_array
);
984 neighbors
[0][1] = lp_build_sample_packed(bld
, x1
, y0
, stride
, data_array
);
985 neighbors
[1][0] = lp_build_sample_packed(bld
, x0
, y1
, stride
, data_array
);
986 neighbors
[1][1] = lp_build_sample_packed(bld
, x1
, y1
, stride
, data_array
);
988 neighbors
[0][0] = LLVMBuildBitCast(builder
, neighbors
[0][0], u8n_vec_type
, "");
989 neighbors
[0][1] = LLVMBuildBitCast(builder
, neighbors
[0][1], u8n_vec_type
, "");
990 neighbors
[1][0] = LLVMBuildBitCast(builder
, neighbors
[1][0], u8n_vec_type
, "");
991 neighbors
[1][1] = LLVMBuildBitCast(builder
, neighbors
[1][1], u8n_vec_type
, "");
993 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][0], &neighbors_lo
[0][0], &neighbors_hi
[0][0]);
994 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][1], &neighbors_lo
[0][1], &neighbors_hi
[0][1]);
995 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][0], &neighbors_lo
[1][0], &neighbors_hi
[1][0]);
996 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][1], &neighbors_lo
[1][1], &neighbors_hi
[1][1]);
999 * Linear interpolate with 8.8 fixed point.
1002 packed_lo
= lp_build_lerp_2d(&h16
,
1003 s_fpart_lo
, t_fpart_lo
,
1007 neighbors_lo
[1][1]);
1009 packed_hi
= lp_build_lerp_2d(&h16
,
1010 s_fpart_hi
, t_fpart_hi
,
1014 neighbors_hi
[1][1]);
1016 packed
= lp_build_pack2(builder
, h16
.type
, u8n
.type
, packed_lo
, packed_hi
);
1019 * Convert to SoA and swizzle.
1022 packed
= LLVMBuildBitCast(builder
, packed
, i32_vec_type
, "");
1024 lp_build_rgba8_to_f32_soa(bld
->builder
,
1026 packed
, unswizzled
);
1028 lp_build_format_swizzle_soa(bld
->format_desc
,
1029 bld
->texel_type
, unswizzled
,
1035 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
1037 LLVMValueRef
*texel
)
1039 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1043 if(bld
->static_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
1046 /* TODO: Compare before swizzling, to avoid redundant computations */
1048 for(chan
= 0; chan
< 4; ++chan
) {
1050 cmp
= lp_build_cmp(texel_bld
, bld
->static_state
->compare_func
, p
, texel
[chan
]);
1051 cmp
= lp_build_select(texel_bld
, cmp
, texel_bld
->one
, texel_bld
->zero
);
1054 res
= lp_build_add(texel_bld
, res
, cmp
);
1060 res
= lp_build_mul(texel_bld
, res
, lp_build_const_scalar(texel_bld
->type
, 0.25));
1062 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1063 for(chan
= 0; chan
< 3; ++chan
)
1065 texel
[3] = texel_bld
->one
;
1070 texture_dims(enum pipe_texture_target tex
)
1073 case PIPE_TEXTURE_1D
:
1075 case PIPE_TEXTURE_2D
:
1076 case PIPE_TEXTURE_CUBE
:
1078 case PIPE_TEXTURE_3D
:
1081 assert(0 && "bad texture target in texture_dims()");
1088 * Generate code to compute texture level of detail (lambda).
1089 * \param s vector of texcoord s values
1090 * \param t vector of texcoord t values
1091 * \param r vector of texcoord r values
1092 * \param width scalar int texture width
1093 * \param height scalar int texture height
1094 * \param depth scalar int texture depth
1097 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
1102 LLVMValueRef height
,
1106 const int dims
= texture_dims(bld
->static_state
->target
);
1107 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
1109 LLVMValueRef lod_bias
= lp_build_const_scalar(bld
->coord_bld
.type
,
1110 bld
->static_state
->lod_bias
);
1111 LLVMValueRef min_lod
= lp_build_const_scalar(bld
->coord_bld
.type
,
1112 bld
->static_state
->min_lod
);
1113 LLVMValueRef max_lod
= lp_build_const_scalar(bld
->coord_bld
.type
,
1114 bld
->static_state
->max_lod
);
1116 LLVMValueRef index0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
1117 LLVMValueRef index1
= LLVMConstInt(LLVMInt32Type(), 1, 0);
1118 LLVMValueRef index2
= LLVMConstInt(LLVMInt32Type(), 2, 0);
1120 LLVMValueRef s0
, s1
, s2
;
1121 LLVMValueRef t0
, t1
, t2
;
1122 LLVMValueRef r0
, r1
, r2
;
1123 LLVMValueRef dsdx
, dsdy
, dtdx
, dtdy
, drdx
, drdy
;
1124 LLVMValueRef rho
, lod
;
1127 * dsdx = abs(s[1] - s[0]);
1128 * dsdy = abs(s[2] - s[0]);
1129 * dtdx = abs(t[1] - t[0]);
1130 * dtdy = abs(t[2] - t[0]);
1131 * drdx = abs(r[1] - r[0]);
1132 * drdy = abs(r[2] - r[0]);
1133 * XXX we're assuming a four-element quad in 2x2 layout here.
1135 s0
= LLVMBuildExtractElement(bld
->builder
, s
, index0
, "s0");
1136 s1
= LLVMBuildExtractElement(bld
->builder
, s
, index1
, "s1");
1137 s2
= LLVMBuildExtractElement(bld
->builder
, s
, index2
, "s2");
1138 dsdx
= lp_build_abs(coord_bld
, lp_build_sub(coord_bld
, s1
, s0
));
1139 dsdy
= lp_build_abs(coord_bld
, lp_build_sub(coord_bld
, s2
, s0
));
1141 t0
= LLVMBuildExtractElement(bld
->builder
, t
, index0
, "t0");
1142 t1
= LLVMBuildExtractElement(bld
->builder
, t
, index1
, "t1");
1143 t2
= LLVMBuildExtractElement(bld
->builder
, t
, index2
, "t2");
1144 dtdx
= lp_build_abs(coord_bld
, lp_build_sub(coord_bld
, t1
, t0
));
1145 dtdy
= lp_build_abs(coord_bld
, lp_build_sub(coord_bld
, t2
, t0
));
1147 r0
= LLVMBuildExtractElement(bld
->builder
, r
, index0
, "r0");
1148 r1
= LLVMBuildExtractElement(bld
->builder
, r
, index1
, "r1");
1149 r2
= LLVMBuildExtractElement(bld
->builder
, r
, index2
, "r2");
1150 drdx
= lp_build_abs(coord_bld
, lp_build_sub(coord_bld
, r1
, r0
));
1151 drdy
= lp_build_abs(coord_bld
, lp_build_sub(coord_bld
, r2
, r0
));
1155 /* Compute rho = max of all partial derivatives scaled by texture size.
1156 * XXX this can be vectorized somewhat
1158 rho
= lp_build_mul(coord_bld
,
1159 lp_build_max(coord_bld
, dsdx
, dsdy
),
1160 lp_build_int_to_float(coord_bld
, width
));
1163 max
= lp_build_mul(coord_bld
,
1164 lp_build_max(coord_bld
, dtdx
, dtdy
),
1165 lp_build_int_to_float(coord_bld
, height
));
1166 rho
= lp_build_max(coord_bld
, rho
, max
);
1168 max
= lp_build_mul(coord_bld
,
1169 lp_build_max(coord_bld
, drdx
, drdy
),
1170 lp_build_int_to_float(coord_bld
, depth
));
1171 rho
= lp_build_max(coord_bld
, rho
, max
);
1175 /* compute lod = log2(rho) */
1176 lod
= lp_build_log2(coord_bld
, rho
);
1179 lod
= lp_build_add(coord_bld
, lod
, lod_bias
);
1182 lod
= lp_build_clamp(coord_bld
, lod
, min_lod
, max_lod
);
1189 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
1190 * mipmap level index.
1191 * \param lod scalar float texture level of detail
1192 * \param level_out returns integer
1195 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
1198 LLVMValueRef
*level_out
)
1200 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
1201 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
1202 LLVMValueRef last_level
, level
;
1204 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
1205 bld
->builder
, unit
);
1207 /* convert float lod to integer */
1208 level
= lp_build_iround(coord_bld
, lod
);
1210 /* clamp level to legal range of levels */
1211 *level_out
= lp_build_clamp(int_coord_bld
, level
,
1212 int_coord_bld
->zero
,
1218 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
1219 * two (adjacent) mipmap level indexes. Later, we'll sample from those
1220 * two mipmap levels and interpolate between them.
1223 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
1226 LLVMValueRef
*level0_out
,
1227 LLVMValueRef
*level1_out
,
1228 LLVMValueRef
*weight_out
)
1230 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
1231 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
1232 LLVMValueRef last_level
, level
;
1234 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
1235 bld
->builder
, unit
);
1237 /* convert float lod to integer */
1238 level
= lp_build_ifloor(coord_bld
, lod
);
1240 /* compute level 0 and clamp to legal range of levels */
1241 *level0_out
= lp_build_clamp(int_coord_bld
, level
,
1242 int_coord_bld
->zero
,
1244 /* compute level 1 and clamp to legal range of levels */
1245 *level1_out
= lp_build_add(int_coord_bld
, *level0_out
, int_coord_bld
->one
);
1246 *level1_out
= lp_build_min(int_coord_bld
, *level1_out
, int_coord_bld
->zero
);
1248 *weight_out
= lp_build_fract(coord_bld
, lod
);
1254 * Build texture sampling code.
1255 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1259 lp_build_sample_soa(LLVMBuilderRef builder
,
1260 const struct lp_sampler_static_state
*static_state
,
1261 struct lp_sampler_dynamic_state
*dynamic_state
,
1262 struct lp_type type
,
1264 unsigned num_coords
,
1265 const LLVMValueRef
*coords
,
1266 LLVMValueRef lodbias
,
1267 LLVMValueRef
*texel
)
1269 struct lp_build_sample_context bld
;
1271 LLVMValueRef height
;
1272 LLVMValueRef stride
;
1273 LLVMValueRef data_array
;
1278 (void) lp_build_lod_selector
; /* temporary to silence warning */
1279 (void) lp_build_nearest_mip_level
;
1280 (void) lp_build_linear_mip_levels
;
1282 /* Setup our build context */
1283 memset(&bld
, 0, sizeof bld
);
1284 bld
.builder
= builder
;
1285 bld
.static_state
= static_state
;
1286 bld
.dynamic_state
= dynamic_state
;
1287 bld
.format_desc
= util_format_description(static_state
->format
);
1288 bld
.coord_type
= type
;
1289 bld
.uint_coord_type
= lp_uint_type(type
);
1290 bld
.int_coord_type
= lp_int_type(type
);
1291 bld
.texel_type
= type
;
1292 lp_build_context_init(&bld
.coord_bld
, builder
, bld
.coord_type
);
1293 lp_build_context_init(&bld
.uint_coord_bld
, builder
, bld
.uint_coord_type
);
1294 lp_build_context_init(&bld
.int_coord_bld
, builder
, bld
.int_coord_type
);
1295 lp_build_context_init(&bld
.texel_bld
, builder
, bld
.texel_type
);
1297 /* Get the dynamic state */
1298 width
= dynamic_state
->width(dynamic_state
, builder
, unit
);
1299 height
= dynamic_state
->height(dynamic_state
, builder
, unit
);
1300 stride
= dynamic_state
->stride(dynamic_state
, builder
, unit
);
1301 data_array
= dynamic_state
->data_ptr(dynamic_state
, builder
, unit
);
1302 /* Note that data_array is an array[level] of pointers to texture images */
1308 width
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, width
);
1309 height
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, height
);
1310 stride
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, stride
);
1312 if(static_state
->target
== PIPE_TEXTURE_1D
)
1313 t
= bld
.coord_bld
.zero
;
1315 switch (static_state
->min_img_filter
) {
1316 case PIPE_TEX_FILTER_NEAREST
:
1317 lp_build_sample_2d_nearest_soa(&bld
, s
, t
, width
, height
,
1318 stride
, data_array
, texel
);
1320 case PIPE_TEX_FILTER_LINEAR
:
1321 if(lp_format_is_rgba8(bld
.format_desc
) &&
1322 is_simple_wrap_mode(static_state
->wrap_s
) &&
1323 is_simple_wrap_mode(static_state
->wrap_t
))
1324 lp_build_sample_2d_linear_aos(&bld
, s
, t
, width
, height
,
1325 stride
, data_array
, texel
);
1327 lp_build_sample_2d_linear_soa(&bld
, s
, t
, width
, height
,
1328 stride
, data_array
, texel
);
1334 /* FIXME: respect static_state->min_mip_filter */;
1335 /* FIXME: respect static_state->mag_img_filter */;
1337 lp_build_sample_compare(&bld
, r
, texel
);