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 /** regular scalar float type */
69 struct lp_type float_type
;
70 struct lp_build_context float_bld
;
72 /** regular scalar float type */
73 struct lp_type int_type
;
74 struct lp_build_context int_bld
;
76 /** Incoming coordinates type and build context */
77 struct lp_type coord_type
;
78 struct lp_build_context coord_bld
;
80 /** Unsigned integer coordinates */
81 struct lp_type uint_coord_type
;
82 struct lp_build_context uint_coord_bld
;
84 /** Signed integer coordinates */
85 struct lp_type int_coord_type
;
86 struct lp_build_context int_coord_bld
;
88 /** Output texels type and build context */
89 struct lp_type texel_type
;
90 struct lp_build_context texel_bld
;
95 * Does the given texture wrap mode allow sampling the texture border color?
96 * XXX maybe move this into gallium util code.
99 wrap_mode_uses_border_color(unsigned mode
)
102 case PIPE_TEX_WRAP_REPEAT
:
103 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
104 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
105 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
107 case PIPE_TEX_WRAP_CLAMP
:
108 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
109 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
110 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
113 assert(0 && "unexpected wrap mode");
120 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
121 LLVMValueRef data_array
, LLVMValueRef level
)
123 LLVMValueRef indexes
[2], data_ptr
;
124 indexes
[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
126 data_ptr
= LLVMBuildGEP(bld
->builder
, data_array
, indexes
, 2, "");
127 data_ptr
= LLVMBuildLoad(bld
->builder
, data_ptr
, "");
133 lp_build_get_const_mipmap_level(struct lp_build_sample_context
*bld
,
134 LLVMValueRef data_array
, int level
)
136 LLVMValueRef lvl
= LLVMConstInt(LLVMInt32Type(), level
, 0);
137 return lp_build_get_mipmap_level(bld
, data_array
, lvl
);
142 texture_dims(enum pipe_texture_target tex
)
145 case PIPE_TEXTURE_1D
:
147 case PIPE_TEXTURE_2D
:
148 case PIPE_TEXTURE_CUBE
:
150 case PIPE_TEXTURE_3D
:
153 assert(0 && "bad texture target in texture_dims()");
161 * Generate code to fetch a texel from a texture at int coords (x, y, z).
162 * The computation depends on whether the texture is 1D, 2D or 3D.
163 * The result, texel, will be:
164 * texel[0] = red values
165 * texel[1] = green values
166 * texel[2] = blue values
167 * texel[3] = alpha values
170 lp_build_sample_texel_soa(struct lp_build_sample_context
*bld
,
177 LLVMValueRef y_stride
,
178 LLVMValueRef z_stride
,
179 LLVMValueRef data_ptr
,
182 const int dims
= texture_dims(bld
->static_state
->target
);
183 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
186 LLVMValueRef use_border
= NULL
;
188 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
189 if (wrap_mode_uses_border_color(bld
->static_state
->wrap_s
)) {
191 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
192 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
193 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
196 if (dims
>= 2 && wrap_mode_uses_border_color(bld
->static_state
->wrap_t
)) {
198 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
199 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
201 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
202 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
205 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
209 if (dims
== 3 && wrap_mode_uses_border_color(bld
->static_state
->wrap_r
)) {
211 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
212 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
214 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
215 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
218 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
223 * Note: if we find an app which frequently samples the texture border
224 * we might want to implement a true conditional here to avoid sampling
225 * the texture whenever possible (since that's quite a bit of code).
228 * texel = border_color;
231 * texel = sample_texture(coord);
233 * As it is now, we always sample the texture, then selectively replace
234 * the texel color results with the border color.
237 /* convert x,y,z coords to linear offset from start of texture, in bytes */
238 offset
= lp_build_sample_offset(&bld
->uint_coord_bld
,
240 x
, y
, z
, y_stride
, z_stride
);
242 assert(bld
->format_desc
->block
.width
== 1);
243 assert(bld
->format_desc
->block
.height
== 1);
244 assert(bld
->format_desc
->block
.bits
<= bld
->texel_type
.width
);
246 /* gather the texels from the texture */
247 packed
= lp_build_gather(bld
->builder
,
248 bld
->texel_type
.length
,
249 bld
->format_desc
->block
.bits
,
250 bld
->texel_type
.width
,
253 texel
[0] = texel
[1] = texel
[2] = texel
[3] = NULL
;
255 /* convert texels to float rgba */
256 lp_build_unpack_rgba_soa(bld
->builder
,
262 /* select texel color or border color depending on use_border */
264 for (chan
= 0; chan
< 4; chan
++) {
265 LLVMValueRef border_chan
=
266 lp_build_const_scalar(bld
->texel_type
,
267 bld
->static_state
->border_color
[chan
]);
268 texel
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
269 border_chan
, texel
[chan
]);
276 lp_build_sample_packed(struct lp_build_sample_context
*bld
,
279 LLVMValueRef y_stride
,
280 LLVMValueRef data_array
)
283 LLVMValueRef data_ptr
;
285 offset
= lp_build_sample_offset(&bld
->uint_coord_bld
,
287 x
, y
, NULL
, y_stride
, NULL
);
289 assert(bld
->format_desc
->block
.width
== 1);
290 assert(bld
->format_desc
->block
.height
== 1);
291 assert(bld
->format_desc
->block
.bits
<= bld
->texel_type
.width
);
293 /* get pointer to mipmap level 0 data */
294 data_ptr
= lp_build_get_const_mipmap_level(bld
, data_array
, 0);
296 return lp_build_gather(bld
->builder
,
297 bld
->texel_type
.length
,
298 bld
->format_desc
->block
.bits
,
299 bld
->texel_type
.width
,
305 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
308 lp_build_coord_mirror(struct lp_build_sample_context
*bld
,
311 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
312 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
313 LLVMValueRef fract
, flr
, isOdd
;
315 /* fract = coord - floor(coord) */
316 fract
= lp_build_sub(coord_bld
, coord
, lp_build_floor(coord_bld
, coord
));
318 /* flr = ifloor(coord); */
319 flr
= lp_build_ifloor(coord_bld
, coord
);
321 /* isOdd = flr & 1 */
322 isOdd
= LLVMBuildAnd(bld
->builder
, flr
, int_coord_bld
->one
, "");
324 /* make coord positive or negative depending on isOdd */
325 coord
= lp_build_set_sign(coord_bld
, fract
, isOdd
);
327 /* convert isOdd to float */
328 isOdd
= lp_build_int_to_float(coord_bld
, isOdd
);
330 /* add isOdd to coord */
331 coord
= lp_build_add(coord_bld
, coord
, isOdd
);
338 * We only support a few wrap modes in lp_build_sample_wrap_int() at this time.
339 * Return whether the given mode is supported by that function.
342 is_simple_wrap_mode(unsigned mode
)
345 case PIPE_TEX_WRAP_REPEAT
:
346 case PIPE_TEX_WRAP_CLAMP
:
347 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
349 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
357 * Build LLVM code for texture wrap mode, for scaled integer texcoords.
358 * \param coord the incoming texcoord (s,t,r or q) scaled to the texture size
359 * \param length the texture size along one dimension
360 * \param is_pot if TRUE, length is a power of two
361 * \param wrap_mode one of PIPE_TEX_WRAP_x
364 lp_build_sample_wrap_int(struct lp_build_sample_context
*bld
,
370 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
371 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
372 LLVMValueRef length_minus_one
;
374 length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
377 case PIPE_TEX_WRAP_REPEAT
:
379 coord
= LLVMBuildAnd(bld
->builder
, coord
, length_minus_one
, "");
381 /* Signed remainder won't give the right results for negative
382 * dividends but unsigned remainder does.*/
383 coord
= LLVMBuildURem(bld
->builder
, coord
, length
, "");
386 case PIPE_TEX_WRAP_CLAMP
:
387 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
388 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
389 coord
= lp_build_max(int_coord_bld
, coord
, int_coord_bld
->zero
);
390 coord
= lp_build_min(int_coord_bld
, coord
, length_minus_one
);
393 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
394 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
395 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
396 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
398 _debug_printf("llvmpipe: failed to translate texture wrap mode %s\n",
399 util_dump_tex_wrap(wrap_mode
, TRUE
));
400 coord
= lp_build_max(uint_coord_bld
, coord
, uint_coord_bld
->zero
);
401 coord
= lp_build_min(uint_coord_bld
, coord
, length_minus_one
);
413 * Build LLVM code for texture wrap mode for linear filtering.
414 * \param x0_out returns first integer texcoord
415 * \param x1_out returns second integer texcoord
416 * \param weight_out returns linear interpolation weight
419 lp_build_sample_wrap_linear(struct lp_build_sample_context
*bld
,
424 LLVMValueRef
*x0_out
,
425 LLVMValueRef
*x1_out
,
426 LLVMValueRef
*weight_out
)
428 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
429 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
430 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
431 LLVMValueRef two
= lp_build_const_scalar(coord_bld
->type
, 2.0);
432 LLVMValueRef half
= lp_build_const_scalar(coord_bld
->type
, 0.5);
433 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
434 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
435 LLVMValueRef length_f_minus_one
= lp_build_sub(coord_bld
, length_f
, coord_bld
->one
);
436 LLVMValueRef coord0
, coord1
, weight
;
439 case PIPE_TEX_WRAP_REPEAT
:
440 /* mul by size and subtract 0.5 */
441 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
442 coord
= lp_build_sub(coord_bld
, coord
, half
);
444 coord0
= lp_build_ifloor(coord_bld
, coord
);
445 coord1
= lp_build_add(uint_coord_bld
, coord0
, uint_coord_bld
->one
);
446 /* compute lerp weight */
447 weight
= lp_build_fract(coord_bld
, coord
);
450 coord0
= LLVMBuildAnd(bld
->builder
, coord0
, length_minus_one
, "");
451 coord1
= LLVMBuildAnd(bld
->builder
, coord1
, length_minus_one
, "");
454 /* Signed remainder won't give the right results for negative
455 * dividends but unsigned remainder does.*/
456 coord0
= LLVMBuildURem(bld
->builder
, coord0
, length
, "");
457 coord1
= LLVMBuildURem(bld
->builder
, coord1
, length
, "");
461 case PIPE_TEX_WRAP_CLAMP
:
462 if (bld
->static_state
->normalized_coords
) {
463 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
465 weight
= lp_build_fract(coord_bld
, coord
);
466 coord0
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
,
468 coord1
= lp_build_add(coord_bld
, coord
, coord_bld
->one
);
469 coord1
= lp_build_clamp(coord_bld
, coord1
, coord_bld
->zero
,
471 coord0
= lp_build_ifloor(coord_bld
, coord0
);
472 coord1
= lp_build_ifloor(coord_bld
, coord1
);
475 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
476 if (bld
->static_state
->normalized_coords
) {
478 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, coord_bld
->one
);
479 /* mul by tex size and subtract 0.5 */
480 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
481 coord
= lp_build_sub(coord_bld
, coord
, half
);
484 LLVMValueRef min
, max
;
485 /* clamp to [0.5, length - 0.5] */
486 min
= lp_build_const_scalar(coord_bld
->type
, 0.5F
);
487 max
= lp_build_sub(coord_bld
, length_f
, min
);
488 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
490 /* compute lerp weight */
491 weight
= lp_build_fract(coord_bld
, coord
);
492 /* coord0 = floor(coord); */
493 coord0
= lp_build_ifloor(coord_bld
, coord
);
494 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
495 /* coord0 = max(coord0, 0) */
496 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
497 /* coord1 = min(coord1, length-1) */
498 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
501 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
503 LLVMValueRef min
, max
;
504 if (bld
->static_state
->normalized_coords
) {
505 /* min = -1.0 / (2 * length) = -0.5 / length */
506 min
= lp_build_mul(coord_bld
,
507 lp_build_const_scalar(coord_bld
->type
, -0.5F
),
508 lp_build_rcp(coord_bld
, length_f
));
509 /* max = 1.0 - min */
510 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
511 /* coord = clamp(coord, min, max) */
512 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
513 /* scale coord to length (and sub 0.5?) */
514 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
515 coord
= lp_build_sub(coord_bld
, coord
, half
);
518 /* clamp to [-0.5, length + 0.5] */
519 min
= lp_build_const_scalar(coord_bld
->type
, -0.5F
);
520 max
= lp_build_sub(coord_bld
, length_f
, min
);
521 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
522 coord
= lp_build_sub(coord_bld
, coord
, half
);
524 /* compute lerp weight */
525 weight
= lp_build_fract(coord_bld
, coord
);
527 coord0
= lp_build_ifloor(coord_bld
, coord
);
528 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
532 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
533 /* compute mirror function */
534 coord
= lp_build_coord_mirror(bld
, coord
);
536 /* scale coord to length */
537 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
538 coord
= lp_build_sub(coord_bld
, coord
, half
);
540 /* compute lerp weight */
541 weight
= lp_build_fract(coord_bld
, coord
);
543 /* convert to int coords */
544 coord0
= lp_build_ifloor(coord_bld
, coord
);
545 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
547 /* coord0 = max(coord0, 0) */
548 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
549 /* coord1 = min(coord1, length-1) */
550 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
553 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
555 LLVMValueRef min
, max
;
556 /* min = 1.0 / (2 * length) */
557 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
558 /* max = 1.0 - min */
559 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
561 coord
= lp_build_abs(coord_bld
, coord
);
562 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
563 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
564 if(0)coord
= lp_build_sub(coord_bld
, coord
, half
);
565 weight
= lp_build_fract(coord_bld
, coord
);
566 coord0
= lp_build_ifloor(coord_bld
, coord
);
567 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
571 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
573 LLVMValueRef min
, max
;
574 /* min = 1.0 / (2 * length) */
575 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
576 /* max = 1.0 - min */
577 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
579 coord
= lp_build_abs(coord_bld
, coord
);
580 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
581 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
582 coord
= lp_build_sub(coord_bld
, coord
, half
);
583 weight
= lp_build_fract(coord_bld
, coord
);
584 coord0
= lp_build_ifloor(coord_bld
, coord
);
585 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
589 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
591 LLVMValueRef min
, max
;
592 /* min = -1.0 / (2 * length) = -0.5 / length */
593 min
= lp_build_mul(coord_bld
,
594 lp_build_const_scalar(coord_bld
->type
, -0.5F
),
595 lp_build_rcp(coord_bld
, length_f
));
596 /* max = 1.0 - min */
597 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
599 coord
= lp_build_abs(coord_bld
, coord
);
600 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
601 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
602 coord
= lp_build_sub(coord_bld
, coord
, half
);
603 weight
= lp_build_fract(coord_bld
, coord
);
604 coord0
= lp_build_ifloor(coord_bld
, coord
);
605 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
618 *weight_out
= weight
;
623 * Build LLVM code for texture wrap mode for nearest filtering.
624 * \param coord the incoming texcoord (nominally in [0,1])
625 * \param length the texture size along one dimension, as int
626 * \param is_pot if TRUE, length is a power of two
627 * \param wrap_mode one of PIPE_TEX_WRAP_x
630 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
636 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
637 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
638 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
639 LLVMValueRef two
= lp_build_const_scalar(coord_bld
->type
, 2.0);
640 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
641 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
642 LLVMValueRef length_f_minus_one
= lp_build_sub(coord_bld
, length_f
, coord_bld
->one
);
646 case PIPE_TEX_WRAP_REPEAT
:
647 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
648 icoord
= lp_build_ifloor(coord_bld
, coord
);
650 icoord
= LLVMBuildAnd(bld
->builder
, icoord
, length_minus_one
, "");
652 /* Signed remainder won't give the right results for negative
653 * dividends but unsigned remainder does.*/
654 icoord
= LLVMBuildURem(bld
->builder
, icoord
, length
, "");
657 case PIPE_TEX_WRAP_CLAMP
:
659 if (bld
->static_state
->normalized_coords
) {
660 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
663 icoord
= lp_build_ifloor(coord_bld
, coord
);
664 /* clamp to [0, size-1]. Note: int coord builder type */
665 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
669 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
671 LLVMValueRef min
, max
;
672 if (bld
->static_state
->normalized_coords
) {
673 /* min = 1.0 / (2 * length) */
674 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
675 /* max = length - min */
676 max
= lp_build_sub(coord_bld
, length_f
, min
);
677 /* scale coord to length */
678 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
681 /* clamp to [0.5, length - 0.5] */
682 min
= lp_build_const_scalar(coord_bld
->type
, 0.5F
);
683 max
= lp_build_sub(coord_bld
, length_f
, min
);
685 /* coord = clamp(coord, min, max) */
686 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
687 icoord
= lp_build_ifloor(coord_bld
, coord
);
691 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
692 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
694 LLVMValueRef min
, max
;
695 if (bld
->static_state
->normalized_coords
) {
696 /* min = -1.0 / (2 * length) = -0.5 / length */
697 min
= lp_build_mul(coord_bld
,
698 lp_build_const_scalar(coord_bld
->type
, -0.5F
),
699 lp_build_rcp(coord_bld
, length_f
));
700 /* max = length - min */
701 max
= lp_build_sub(coord_bld
, length_f
, min
);
702 /* scale coord to length */
703 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
706 /* clamp to [-0.5, length + 0.5] */
707 min
= lp_build_const_scalar(coord_bld
->type
, -0.5F
);
708 max
= lp_build_sub(coord_bld
, length_f
, min
);
710 /* coord = clamp(coord, min, max) */
711 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
712 icoord
= lp_build_ifloor(coord_bld
, coord
);
716 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
718 LLVMValueRef min
, max
;
719 /* min = 1.0 / (2 * length) */
720 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
721 /* max = length - min */
722 max
= lp_build_sub(coord_bld
, length_f
, min
);
724 /* compute mirror function */
725 coord
= lp_build_coord_mirror(bld
, coord
);
727 /* scale coord to length */
728 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
730 /* coord = clamp(coord, min, max) */
731 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
732 icoord
= lp_build_ifloor(coord_bld
, coord
);
736 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
737 coord
= lp_build_abs(coord_bld
, coord
);
738 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
739 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, length_f_minus_one
);
740 icoord
= lp_build_ifloor(coord_bld
, coord
);
743 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
745 LLVMValueRef min
, max
;
746 /* min = 1.0 / (2 * length) */
747 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
748 /* max = length - min */
749 max
= lp_build_sub(coord_bld
, length_f
, min
);
751 coord
= lp_build_abs(coord_bld
, coord
);
752 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
753 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
754 icoord
= lp_build_ifloor(coord_bld
, coord
);
758 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
760 LLVMValueRef min
, max
;
761 /* min = 1.0 / (2 * length) */
762 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
763 min
= lp_build_negate(coord_bld
, min
);
764 /* max = length - min */
765 max
= lp_build_sub(coord_bld
, length_f
, min
);
767 coord
= lp_build_abs(coord_bld
, coord
);
768 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
769 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
770 icoord
= lp_build_ifloor(coord_bld
, coord
);
784 * Codegen equivalent for u_minify().
785 * Return max(1, base_size >> level);
788 lp_build_minify(struct lp_build_sample_context
*bld
,
789 LLVMValueRef base_size
,
792 LLVMValueRef size
= LLVMBuildAShr(bld
->builder
, base_size
, level
, "minify");
793 size
= lp_build_max(&bld
->int_coord_bld
, size
, bld
->int_coord_bld
.one
);
799 * Generate code to compute texture level of detail (lambda).
800 * \param s vector of texcoord s values
801 * \param t vector of texcoord t values
802 * \param r vector of texcoord r values
803 * \param width scalar int texture width
804 * \param height scalar int texture height
805 * \param depth scalar int texture depth
808 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
817 const int dims
= texture_dims(bld
->static_state
->target
);
818 struct lp_build_context
*float_bld
= &bld
->float_bld
;
819 LLVMValueRef lod_bias
= LLVMConstReal(LLVMFloatType(), bld
->static_state
->lod_bias
);
820 LLVMValueRef min_lod
= LLVMConstReal(LLVMFloatType(), bld
->static_state
->min_lod
);
821 LLVMValueRef max_lod
= LLVMConstReal(LLVMFloatType(), bld
->static_state
->max_lod
);
823 LLVMValueRef index0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
824 LLVMValueRef index1
= LLVMConstInt(LLVMInt32Type(), 1, 0);
825 LLVMValueRef index2
= LLVMConstInt(LLVMInt32Type(), 2, 0);
827 LLVMValueRef s0
, s1
, s2
;
828 LLVMValueRef t0
, t1
, t2
;
829 LLVMValueRef r0
, r1
, r2
;
830 LLVMValueRef dsdx
, dsdy
, dtdx
, dtdy
, drdx
, drdy
;
831 LLVMValueRef rho
, lod
;
834 * dsdx = abs(s[1] - s[0]);
835 * dsdy = abs(s[2] - s[0]);
836 * dtdx = abs(t[1] - t[0]);
837 * dtdy = abs(t[2] - t[0]);
838 * drdx = abs(r[1] - r[0]);
839 * drdy = abs(r[2] - r[0]);
840 * XXX we're assuming a four-element quad in 2x2 layout here.
842 s0
= LLVMBuildExtractElement(bld
->builder
, s
, index0
, "s0");
843 s1
= LLVMBuildExtractElement(bld
->builder
, s
, index1
, "s1");
844 s2
= LLVMBuildExtractElement(bld
->builder
, s
, index2
, "s2");
845 dsdx
= LLVMBuildSub(bld
->builder
, s1
, s0
, "");
846 dsdx
= lp_build_abs(float_bld
, dsdx
);
847 dsdy
= LLVMBuildSub(bld
->builder
, s2
, s0
, "");
848 dsdy
= lp_build_abs(float_bld
, dsdy
);
850 t0
= LLVMBuildExtractElement(bld
->builder
, t
, index0
, "t0");
851 t1
= LLVMBuildExtractElement(bld
->builder
, t
, index1
, "t1");
852 t2
= LLVMBuildExtractElement(bld
->builder
, t
, index2
, "t2");
853 dtdx
= LLVMBuildSub(bld
->builder
, t1
, t0
, "");
854 dtdx
= lp_build_abs(float_bld
, dtdx
);
855 dtdy
= LLVMBuildSub(bld
->builder
, t2
, t0
, "");
856 dtdy
= lp_build_abs(float_bld
, dtdy
);
858 r0
= LLVMBuildExtractElement(bld
->builder
, r
, index0
, "r0");
859 r1
= LLVMBuildExtractElement(bld
->builder
, r
, index1
, "r1");
860 r2
= LLVMBuildExtractElement(bld
->builder
, r
, index2
, "r2");
861 drdx
= LLVMBuildSub(bld
->builder
, r1
, r0
, "");
862 drdx
= lp_build_abs(float_bld
, drdx
);
863 drdy
= LLVMBuildSub(bld
->builder
, r2
, r0
, "");
864 drdy
= lp_build_abs(float_bld
, drdy
);
868 /* Compute rho = max of all partial derivatives scaled by texture size.
869 * XXX this could be vectorized somewhat
871 rho
= LLVMBuildMul(bld
->builder
,
872 lp_build_max(float_bld
, dsdx
, dsdy
),
873 lp_build_int_to_float(float_bld
, width
), "");
876 max
= LLVMBuildMul(bld
->builder
,
877 lp_build_max(float_bld
, dtdx
, dtdy
),
878 lp_build_int_to_float(float_bld
, height
), "");
879 rho
= lp_build_max(float_bld
, rho
, max
);
881 max
= LLVMBuildMul(bld
->builder
,
882 lp_build_max(float_bld
, drdx
, drdy
),
883 lp_build_int_to_float(float_bld
, depth
), "");
884 rho
= lp_build_max(float_bld
, rho
, max
);
888 /* compute lod = log2(rho) */
889 lod
= lp_build_log2(float_bld
, rho
);
892 lod
= LLVMBuildAdd(bld
->builder
, lod
, lod_bias
, "LOD bias");
895 lod
= lp_build_clamp(float_bld
, lod
, min_lod
, max_lod
);
902 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
903 * mipmap level index.
904 * Note: this is all scalar code.
905 * \param lod scalar float texture level of detail
906 * \param level_out returns integer
909 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
912 LLVMValueRef
*level_out
)
914 struct lp_build_context
*float_bld
= &bld
->float_bld
;
915 struct lp_build_context
*int_bld
= &bld
->int_bld
;
916 LLVMValueRef last_level
, level
;
918 LLVMValueRef zero
= LLVMConstInt(LLVMInt32Type(), 0, 0);
920 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
923 /* convert float lod to integer */
924 level
= lp_build_iround(float_bld
, lod
);
926 /* clamp level to legal range of levels */
927 *level_out
= lp_build_clamp(int_bld
, level
, zero
, last_level
);
932 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
933 * two (adjacent) mipmap level indexes. Later, we'll sample from those
934 * two mipmap levels and interpolate between them.
937 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
940 LLVMValueRef
*level0_out
,
941 LLVMValueRef
*level1_out
,
942 LLVMValueRef
*weight_out
)
944 struct lp_build_context
*float_bld
= &bld
->float_bld
;
945 struct lp_build_context
*int_bld
= &bld
->int_bld
;
946 LLVMValueRef last_level
, level
;
948 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
951 /* convert float lod to integer */
952 level
= lp_build_ifloor(float_bld
, lod
);
954 /* compute level 0 and clamp to legal range of levels */
955 *level0_out
= lp_build_clamp(int_bld
, level
,
958 /* compute level 1 and clamp to legal range of levels */
959 *level1_out
= lp_build_add(int_bld
, *level0_out
, int_bld
->one
);
960 *level1_out
= lp_build_min(int_bld
, *level1_out
, int_bld
->zero
);
962 *weight_out
= lp_build_fract(float_bld
, lod
);
967 * Generate code to sample a mipmap level with nearest filtering.
970 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
971 LLVMValueRef width_vec
,
972 LLVMValueRef height_vec
,
973 LLVMValueRef depth_vec
,
974 LLVMValueRef row_stride_vec
,
975 LLVMValueRef img_stride_vec
,
976 LLVMValueRef data_ptr
,
980 LLVMValueRef colors_out
[4])
982 const int dims
= texture_dims(bld
->static_state
->target
);
983 LLVMValueRef x
, y
, z
;
986 * Compute integer texcoords.
988 x
= lp_build_sample_wrap_nearest(bld
, s
, width_vec
,
989 bld
->static_state
->pot_width
,
990 bld
->static_state
->wrap_s
);
991 lp_build_name(x
, "tex.x.wrapped");
994 y
= lp_build_sample_wrap_nearest(bld
, t
, height_vec
,
995 bld
->static_state
->pot_height
,
996 bld
->static_state
->wrap_t
);
997 lp_build_name(y
, "tex.y.wrapped");
1000 z
= lp_build_sample_wrap_nearest(bld
, r
, depth_vec
,
1001 bld
->static_state
->pot_height
,
1002 bld
->static_state
->wrap_r
);
1003 lp_build_name(z
, "tex.z.wrapped");
1014 * Get texture colors.
1016 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1018 row_stride_vec
, img_stride_vec
,
1019 data_ptr
, colors_out
);
1024 * Generate code to sample a mipmap level with linear filtering.
1025 * 1D, 2D and 3D images are suppored.
1028 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
1029 LLVMValueRef width_vec
,
1030 LLVMValueRef height_vec
,
1031 LLVMValueRef depth_vec
,
1032 LLVMValueRef row_stride_vec
,
1033 LLVMValueRef img_stride_vec
,
1034 LLVMValueRef data_ptr
,
1038 LLVMValueRef colors_out
[4])
1040 const int dims
= texture_dims(bld
->static_state
->target
);
1041 LLVMValueRef x0
, y0
, z0
, x1
, y1
, z1
;
1042 LLVMValueRef s_fpart
, t_fpart
, r_fpart
;
1043 LLVMValueRef neighbors
[2][2][4];
1047 * Compute integer texcoords.
1049 lp_build_sample_wrap_linear(bld
, s
, width_vec
,
1050 bld
->static_state
->pot_width
,
1051 bld
->static_state
->wrap_s
,
1052 &x0
, &x1
, &s_fpart
);
1053 lp_build_name(x0
, "tex.x0.wrapped");
1054 lp_build_name(x1
, "tex.x1.wrapped");
1057 lp_build_sample_wrap_linear(bld
, t
, height_vec
,
1058 bld
->static_state
->pot_height
,
1059 bld
->static_state
->wrap_t
,
1060 &y0
, &y1
, &t_fpart
);
1061 lp_build_name(y0
, "tex.y0.wrapped");
1062 lp_build_name(y1
, "tex.y1.wrapped");
1065 lp_build_sample_wrap_linear(bld
, r
, depth_vec
,
1066 bld
->static_state
->pot_depth
,
1067 bld
->static_state
->wrap_r
,
1068 &z0
, &z1
, &r_fpart
);
1069 lp_build_name(z0
, "tex.z0.wrapped");
1070 lp_build_name(z1
, "tex.z1.wrapped");
1073 z0
= z1
= r_fpart
= NULL
;
1077 y0
= y1
= t_fpart
= NULL
;
1078 z0
= z1
= r_fpart
= NULL
;
1082 * Get texture colors.
1084 /* get x0/x1 texels */
1085 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1087 row_stride_vec
, img_stride_vec
,
1088 data_ptr
, neighbors
[0][0]);
1089 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1091 row_stride_vec
, img_stride_vec
,
1092 data_ptr
, neighbors
[0][1]);
1095 /* Interpolate two samples from 1D image to produce one color */
1096 for (chan
= 0; chan
< 4; chan
++) {
1097 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, s_fpart
,
1098 neighbors
[0][0][chan
],
1099 neighbors
[0][1][chan
]);
1104 LLVMValueRef colors0
[4];
1106 /* get x0/x1 texels at y1 */
1107 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1109 row_stride_vec
, img_stride_vec
,
1110 data_ptr
, neighbors
[1][0]);
1111 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1113 row_stride_vec
, img_stride_vec
,
1114 data_ptr
, neighbors
[1][1]);
1116 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
1117 for (chan
= 0; chan
< 4; chan
++) {
1118 colors0
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1120 neighbors
[0][0][chan
],
1121 neighbors
[0][1][chan
],
1122 neighbors
[1][0][chan
],
1123 neighbors
[1][1][chan
]);
1127 LLVMValueRef neighbors1
[2][2][4];
1128 LLVMValueRef colors1
[4];
1130 /* get x0/x1/y0/y1 texels at z1 */
1131 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1133 row_stride_vec
, img_stride_vec
,
1134 data_ptr
, neighbors1
[0][0]);
1135 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1137 row_stride_vec
, img_stride_vec
,
1138 data_ptr
, neighbors1
[0][1]);
1139 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1141 row_stride_vec
, img_stride_vec
,
1142 data_ptr
, neighbors1
[1][0]);
1143 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1145 row_stride_vec
, img_stride_vec
,
1146 data_ptr
, neighbors1
[1][1]);
1148 /* Bilinear interpolate the four samples from the second Z slice */
1149 for (chan
= 0; chan
< 4; chan
++) {
1150 colors1
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1152 neighbors1
[0][0][chan
],
1153 neighbors1
[0][1][chan
],
1154 neighbors1
[1][0][chan
],
1155 neighbors1
[1][1][chan
]);
1158 /* Linearly interpolate the two samples from the two 3D slices */
1159 for (chan
= 0; chan
< 4; chan
++) {
1160 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
,
1162 colors0
[chan
], colors1
[chan
]);
1167 for (chan
= 0; chan
< 4; chan
++) {
1168 colors_out
[chan
] = colors0
[chan
];
1177 * General texture sampling codegen.
1178 * This function handles texture sampling for all texture targets (1D,
1179 * 2D, 3D, cube) and all filtering modes.
1182 lp_build_sample_general(struct lp_build_sample_context
*bld
,
1188 LLVMValueRef height
,
1190 LLVMValueRef width_vec
,
1191 LLVMValueRef height_vec
,
1192 LLVMValueRef depth_vec
,
1193 LLVMValueRef row_stride_vec
,
1194 LLVMValueRef img_stride_vec
,
1195 LLVMValueRef data_array
,
1196 LLVMValueRef
*colors_out
)
1198 const unsigned mip_filter
= bld
->static_state
->min_mip_filter
;
1199 const unsigned min_filter
= bld
->static_state
->min_img_filter
;
1200 const unsigned mag_filter
= bld
->static_state
->mag_img_filter
;
1201 const int dims
= texture_dims(bld
->static_state
->target
);
1202 LLVMValueRef lod
, lod_fpart
;
1203 LLVMValueRef ilevel0
, ilevel1
, ilevel0_vec
, ilevel1_vec
;
1204 LLVMValueRef width0_vec
= NULL
, height0_vec
= NULL
, depth0_vec
= NULL
;
1205 LLVMValueRef width1_vec
= NULL
, height1_vec
= NULL
, depth1_vec
= NULL
;
1206 LLVMValueRef row_stride0_vec
= NULL
, row_stride1_vec
= NULL
;
1207 LLVMValueRef img_stride0_vec
= NULL
, img_stride1_vec
= NULL
;
1208 LLVMValueRef data_ptr0
, data_ptr1
;
1212 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1213 mip_filter, min_filter, mag_filter);
1217 * Compute the level of detail (mipmap level index(es)).
1219 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
1220 /* always use mip level 0 */
1221 ilevel0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
1224 /* compute float LOD */
1225 lod
= lp_build_lod_selector(bld
, s
, t
, r
, width
, height
, depth
);
1227 if (mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
1228 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
1231 assert(mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
);
1232 lp_build_linear_mip_levels(bld
, unit
, lod
, &ilevel0
, &ilevel1
,
1234 lod_fpart
= lp_build_broadcast_scalar(&bld
->coord_bld
, lod_fpart
);
1239 * Convert scalar integer mipmap levels into vectors.
1241 ilevel0_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel0
);
1242 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
)
1243 ilevel1_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel1
);
1246 * Compute width, height at mipmap level 'ilevel0'
1248 width0_vec
= lp_build_minify(bld
, width_vec
, ilevel0_vec
);
1250 height0_vec
= lp_build_minify(bld
, height_vec
, ilevel0_vec
);
1251 row_stride0_vec
= lp_build_minify(bld
, row_stride_vec
, ilevel0_vec
);
1253 depth0_vec
= lp_build_minify(bld
, depth_vec
, ilevel0_vec
);
1256 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1257 /* compute width, height, depth for second mipmap level at ilevel1 */
1258 width1_vec
= lp_build_minify(bld
, width_vec
, ilevel1_vec
);
1260 height1_vec
= lp_build_minify(bld
, height_vec
, ilevel1_vec
);
1261 row_stride1_vec
= lp_build_minify(bld
, row_stride_vec
, ilevel1_vec
);
1263 depth1_vec
= lp_build_minify(bld
, depth_vec
, ilevel1_vec
);
1269 * Choose cube face, recompute texcoords.
1271 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1276 * Get pointer(s) to image data for mipmap level(s).
1278 data_ptr0
= lp_build_get_mipmap_level(bld
, data_array
, ilevel0
);
1279 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1280 data_ptr1
= lp_build_get_mipmap_level(bld
, data_array
, ilevel1
);
1284 * Get/interpolate texture colors.
1286 /* XXX temporarily force this path: */
1287 if (1 /*min_filter == mag_filter*/) {
1288 /* same filter for minification or magnification */
1289 LLVMValueRef colors0
[4], colors1
[4];
1291 if (min_filter
== PIPE_TEX_FILTER_NEAREST
) {
1292 lp_build_sample_image_nearest(bld
,
1293 width0_vec
, height0_vec
, depth0_vec
,
1294 row_stride0_vec
, img_stride0_vec
,
1295 data_ptr0
, s
, t
, r
, colors0
);
1297 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1298 /* sample the second mipmap level, and interp */
1299 lp_build_sample_image_nearest(bld
,
1300 width1_vec
, height1_vec
, depth1_vec
,
1301 row_stride1_vec
, img_stride1_vec
,
1302 data_ptr1
, s
, t
, r
, colors1
);
1306 assert(min_filter
== PIPE_TEX_FILTER_LINEAR
);
1308 lp_build_sample_image_linear(bld
,
1309 width0_vec
, height0_vec
, depth0_vec
,
1310 row_stride0_vec
, img_stride0_vec
,
1311 data_ptr0
, s
, t
, r
, colors0
);
1314 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1315 /* sample the second mipmap level, and interp */
1316 lp_build_sample_image_linear(bld
,
1317 width1_vec
, height1_vec
, depth1_vec
,
1318 row_stride1_vec
, img_stride1_vec
,
1319 data_ptr1
, s
, t
, r
, colors1
);
1323 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1324 /* interpolate samples from the two mipmap levels */
1325 for (chan
= 0; chan
< 4; chan
++) {
1326 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
1327 colors0
[chan
], colors1
[chan
]);
1331 /* use first/only level's colors */
1332 for (chan
= 0; chan
< 4; chan
++) {
1333 colors_out
[chan
] = colors0
[chan
];
1338 /* emit conditional to choose min image filter or mag image filter
1339 * depending on the lod being >0 or <= 0, respectively.
1348 lp_build_rgba8_to_f32_soa(LLVMBuilderRef builder
,
1349 struct lp_type dst_type
,
1350 LLVMValueRef packed
,
1353 LLVMValueRef mask
= lp_build_int_const_scalar(dst_type
, 0xff);
1356 /* Decode the input vector components */
1357 for (chan
= 0; chan
< 4; ++chan
) {
1358 unsigned start
= chan
*8;
1359 unsigned stop
= start
+ 8;
1365 input
= LLVMBuildLShr(builder
, input
, lp_build_int_const_scalar(dst_type
, start
), "");
1368 input
= LLVMBuildAnd(builder
, input
, mask
, "");
1370 input
= lp_build_unsigned_norm_to_float(builder
, 8, dst_type
, input
);
1378 lp_build_sample_2d_linear_aos(struct lp_build_sample_context
*bld
,
1382 LLVMValueRef height
,
1383 LLVMValueRef stride
,
1384 LLVMValueRef data_array
,
1385 LLVMValueRef
*texel
)
1387 LLVMBuilderRef builder
= bld
->builder
;
1388 struct lp_build_context i32
, h16
, u8n
;
1389 LLVMTypeRef i32_vec_type
, h16_vec_type
, u8n_vec_type
;
1390 LLVMValueRef i32_c8
, i32_c128
, i32_c255
;
1391 LLVMValueRef s_ipart
, s_fpart
, s_fpart_lo
, s_fpart_hi
;
1392 LLVMValueRef t_ipart
, t_fpart
, t_fpart_lo
, t_fpart_hi
;
1393 LLVMValueRef x0
, x1
;
1394 LLVMValueRef y0
, y1
;
1395 LLVMValueRef neighbors
[2][2];
1396 LLVMValueRef neighbors_lo
[2][2];
1397 LLVMValueRef neighbors_hi
[2][2];
1398 LLVMValueRef packed
, packed_lo
, packed_hi
;
1399 LLVMValueRef unswizzled
[4];
1401 lp_build_context_init(&i32
, builder
, lp_type_int_vec(32));
1402 lp_build_context_init(&h16
, builder
, lp_type_ufixed(16));
1403 lp_build_context_init(&u8n
, builder
, lp_type_unorm(8));
1405 i32_vec_type
= lp_build_vec_type(i32
.type
);
1406 h16_vec_type
= lp_build_vec_type(h16
.type
);
1407 u8n_vec_type
= lp_build_vec_type(u8n
.type
);
1409 if (bld
->static_state
->normalized_coords
) {
1410 LLVMTypeRef coord_vec_type
= lp_build_vec_type(bld
->coord_type
);
1411 LLVMValueRef fp_width
= LLVMBuildSIToFP(bld
->builder
, width
, coord_vec_type
, "");
1412 LLVMValueRef fp_height
= LLVMBuildSIToFP(bld
->builder
, height
, coord_vec_type
, "");
1413 s
= lp_build_mul(&bld
->coord_bld
, s
, fp_width
);
1414 t
= lp_build_mul(&bld
->coord_bld
, t
, fp_height
);
1417 /* scale coords by 256 (8 fractional bits) */
1418 s
= lp_build_mul_imm(&bld
->coord_bld
, s
, 256);
1419 t
= lp_build_mul_imm(&bld
->coord_bld
, t
, 256);
1421 /* convert float to int */
1422 s
= LLVMBuildFPToSI(builder
, s
, i32_vec_type
, "");
1423 t
= LLVMBuildFPToSI(builder
, t
, i32_vec_type
, "");
1425 /* subtract 0.5 (add -128) */
1426 i32_c128
= lp_build_int_const_scalar(i32
.type
, -128);
1427 s
= LLVMBuildAdd(builder
, s
, i32_c128
, "");
1428 t
= LLVMBuildAdd(builder
, t
, i32_c128
, "");
1430 /* compute floor (shift right 8) */
1431 i32_c8
= lp_build_int_const_scalar(i32
.type
, 8);
1432 s_ipart
= LLVMBuildAShr(builder
, s
, i32_c8
, "");
1433 t_ipart
= LLVMBuildAShr(builder
, t
, i32_c8
, "");
1435 /* compute fractional part (AND with 0xff) */
1436 i32_c255
= lp_build_int_const_scalar(i32
.type
, 255);
1437 s_fpart
= LLVMBuildAnd(builder
, s
, i32_c255
, "");
1438 t_fpart
= LLVMBuildAnd(builder
, t
, i32_c255
, "");
1443 x1
= lp_build_add(&bld
->int_coord_bld
, x0
, bld
->int_coord_bld
.one
);
1444 y1
= lp_build_add(&bld
->int_coord_bld
, y0
, bld
->int_coord_bld
.one
);
1446 x0
= lp_build_sample_wrap_int(bld
, x0
, width
, bld
->static_state
->pot_width
,
1447 bld
->static_state
->wrap_s
);
1448 y0
= lp_build_sample_wrap_int(bld
, y0
, height
, bld
->static_state
->pot_height
,
1449 bld
->static_state
->wrap_t
);
1451 x1
= lp_build_sample_wrap_int(bld
, x1
, width
, bld
->static_state
->pot_width
,
1452 bld
->static_state
->wrap_s
);
1453 y1
= lp_build_sample_wrap_int(bld
, y1
, height
, bld
->static_state
->pot_height
,
1454 bld
->static_state
->wrap_t
);
1457 * Transform 4 x i32 in
1459 * s_fpart = {s0, s1, s2, s3}
1463 * s_fpart = {00, s0, 00, s1, 00, s2, 00, s3}
1467 * s_fpart_lo = {s0, s0, s0, s0, s1, s1, s1, s1}
1468 * s_fpart_hi = {s2, s2, s2, s2, s3, s3, s3, s3}
1470 * and likewise for t_fpart. There is no risk of loosing precision here
1471 * since the fractional parts only use the lower 8bits.
1474 s_fpart
= LLVMBuildBitCast(builder
, s_fpart
, h16_vec_type
, "");
1475 t_fpart
= LLVMBuildBitCast(builder
, t_fpart
, h16_vec_type
, "");
1478 LLVMTypeRef elem_type
= LLVMInt32Type();
1479 LLVMValueRef shuffles_lo
[LP_MAX_VECTOR_LENGTH
];
1480 LLVMValueRef shuffles_hi
[LP_MAX_VECTOR_LENGTH
];
1481 LLVMValueRef shuffle_lo
;
1482 LLVMValueRef shuffle_hi
;
1485 for(j
= 0; j
< h16
.type
.length
; j
+= 4) {
1486 unsigned subindex
= util_cpu_caps
.little_endian
? 0 : 1;
1489 index
= LLVMConstInt(elem_type
, j
/2 + subindex
, 0);
1490 for(i
= 0; i
< 4; ++i
)
1491 shuffles_lo
[j
+ i
] = index
;
1493 index
= LLVMConstInt(elem_type
, h16
.type
.length
/2 + j
/2 + subindex
, 0);
1494 for(i
= 0; i
< 4; ++i
)
1495 shuffles_hi
[j
+ i
] = index
;
1498 shuffle_lo
= LLVMConstVector(shuffles_lo
, h16
.type
.length
);
1499 shuffle_hi
= LLVMConstVector(shuffles_hi
, h16
.type
.length
);
1501 s_fpart_lo
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_lo
, "");
1502 t_fpart_lo
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_lo
, "");
1503 s_fpart_hi
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_hi
, "");
1504 t_fpart_hi
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_hi
, "");
1508 * Fetch the pixels as 4 x 32bit (rgba order might differ):
1510 * rgba0 rgba1 rgba2 rgba3
1512 * bit cast them into 16 x u8
1514 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
1516 * unpack them into two 8 x i16:
1518 * r0 g0 b0 a0 r1 g1 b1 a1
1519 * r2 g2 b2 a2 r3 g3 b3 a3
1521 * The higher 8 bits of the resulting elements will be zero.
1524 neighbors
[0][0] = lp_build_sample_packed(bld
, x0
, y0
, stride
, data_array
);
1525 neighbors
[0][1] = lp_build_sample_packed(bld
, x1
, y0
, stride
, data_array
);
1526 neighbors
[1][0] = lp_build_sample_packed(bld
, x0
, y1
, stride
, data_array
);
1527 neighbors
[1][1] = lp_build_sample_packed(bld
, x1
, y1
, stride
, data_array
);
1529 neighbors
[0][0] = LLVMBuildBitCast(builder
, neighbors
[0][0], u8n_vec_type
, "");
1530 neighbors
[0][1] = LLVMBuildBitCast(builder
, neighbors
[0][1], u8n_vec_type
, "");
1531 neighbors
[1][0] = LLVMBuildBitCast(builder
, neighbors
[1][0], u8n_vec_type
, "");
1532 neighbors
[1][1] = LLVMBuildBitCast(builder
, neighbors
[1][1], u8n_vec_type
, "");
1534 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][0], &neighbors_lo
[0][0], &neighbors_hi
[0][0]);
1535 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][1], &neighbors_lo
[0][1], &neighbors_hi
[0][1]);
1536 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][0], &neighbors_lo
[1][0], &neighbors_hi
[1][0]);
1537 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][1], &neighbors_lo
[1][1], &neighbors_hi
[1][1]);
1540 * Linear interpolate with 8.8 fixed point.
1543 packed_lo
= lp_build_lerp_2d(&h16
,
1544 s_fpart_lo
, t_fpart_lo
,
1548 neighbors_lo
[1][1]);
1550 packed_hi
= lp_build_lerp_2d(&h16
,
1551 s_fpart_hi
, t_fpart_hi
,
1555 neighbors_hi
[1][1]);
1557 packed
= lp_build_pack2(builder
, h16
.type
, u8n
.type
, packed_lo
, packed_hi
);
1560 * Convert to SoA and swizzle.
1563 packed
= LLVMBuildBitCast(builder
, packed
, i32_vec_type
, "");
1565 lp_build_rgba8_to_f32_soa(bld
->builder
,
1567 packed
, unswizzled
);
1569 lp_build_format_swizzle_soa(bld
->format_desc
,
1570 bld
->texel_type
, unswizzled
,
1576 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
1578 LLVMValueRef
*texel
)
1580 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1584 if(bld
->static_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
1587 /* TODO: Compare before swizzling, to avoid redundant computations */
1589 for(chan
= 0; chan
< 4; ++chan
) {
1591 cmp
= lp_build_cmp(texel_bld
, bld
->static_state
->compare_func
, p
, texel
[chan
]);
1592 cmp
= lp_build_select(texel_bld
, cmp
, texel_bld
->one
, texel_bld
->zero
);
1595 res
= lp_build_add(texel_bld
, res
, cmp
);
1601 res
= lp_build_mul(texel_bld
, res
, lp_build_const_scalar(texel_bld
->type
, 0.25));
1603 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1604 for(chan
= 0; chan
< 3; ++chan
)
1606 texel
[3] = texel_bld
->one
;
1611 * Build texture sampling code.
1612 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1614 * \param type vector float type to use for coords, etc.
1617 lp_build_sample_soa(LLVMBuilderRef builder
,
1618 const struct lp_sampler_static_state
*static_state
,
1619 struct lp_sampler_dynamic_state
*dynamic_state
,
1620 struct lp_type type
,
1622 unsigned num_coords
,
1623 const LLVMValueRef
*coords
,
1624 LLVMValueRef lodbias
,
1625 LLVMValueRef
*texel
)
1627 struct lp_build_sample_context bld
;
1628 LLVMValueRef width
, width_vec
;
1629 LLVMValueRef height
, height_vec
;
1630 LLVMValueRef depth
, depth_vec
;
1631 LLVMValueRef stride
, stride_vec
;
1632 LLVMValueRef data_array
;
1637 (void) lp_build_lod_selector
; /* temporary to silence warning */
1638 (void) lp_build_nearest_mip_level
;
1639 (void) lp_build_linear_mip_levels
;
1640 (void) lp_build_minify
;
1642 /* Setup our build context */
1643 memset(&bld
, 0, sizeof bld
);
1644 bld
.builder
= builder
;
1645 bld
.static_state
= static_state
;
1646 bld
.dynamic_state
= dynamic_state
;
1647 bld
.format_desc
= util_format_description(static_state
->format
);
1649 bld
.float_type
= lp_type_float(32);
1650 bld
.int_type
= lp_type_int(32);
1651 bld
.coord_type
= type
;
1652 bld
.uint_coord_type
= lp_uint_type(type
);
1653 bld
.int_coord_type
= lp_int_type(type
);
1654 bld
.texel_type
= type
;
1656 lp_build_context_init(&bld
.float_bld
, builder
, bld
.float_type
);
1657 lp_build_context_init(&bld
.int_bld
, builder
, bld
.int_type
);
1658 lp_build_context_init(&bld
.coord_bld
, builder
, bld
.coord_type
);
1659 lp_build_context_init(&bld
.uint_coord_bld
, builder
, bld
.uint_coord_type
);
1660 lp_build_context_init(&bld
.int_coord_bld
, builder
, bld
.int_coord_type
);
1661 lp_build_context_init(&bld
.texel_bld
, builder
, bld
.texel_type
);
1663 /* Get the dynamic state */
1664 width
= dynamic_state
->width(dynamic_state
, builder
, unit
);
1665 height
= dynamic_state
->height(dynamic_state
, builder
, unit
);
1666 depth
= dynamic_state
->depth(dynamic_state
, builder
, unit
);
1667 stride
= dynamic_state
->stride(dynamic_state
, builder
, unit
);
1668 data_array
= dynamic_state
->data_ptr(dynamic_state
, builder
, unit
);
1669 /* Note that data_array is an array[level] of pointers to texture images */
1675 width_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, width
);
1676 height_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, height
);
1677 depth_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, depth
);
1678 stride_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, stride
);
1680 if (lp_format_is_rgba8(bld
.format_desc
) &&
1681 static_state
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
1682 static_state
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
1683 static_state
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
&&
1684 is_simple_wrap_mode(static_state
->wrap_s
) &&
1685 is_simple_wrap_mode(static_state
->wrap_t
)) {
1687 lp_build_sample_2d_linear_aos(&bld
, s
, t
, width_vec
, height_vec
,
1688 stride_vec
, data_array
, texel
);
1691 lp_build_sample_general(&bld
, unit
, s
, t
, r
,
1692 width
, height
, depth
,
1693 width_vec
, height_vec
, depth_vec
,
1694 stride_vec
, NULL
, data_array
,
1698 lp_build_sample_compare(&bld
, r
, texel
);