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 * Gen code to fetch a texel from a texture at int coords (x, y).
143 * The result, texel, will be:
144 * texel[0] = red values
145 * texel[1] = green values
146 * texel[2] = blue values
147 * texel[3] = alpha values
150 lp_build_sample_texel_soa(struct lp_build_sample_context
*bld
,
155 LLVMValueRef y_stride
,
156 LLVMValueRef data_ptr
,
159 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
162 LLVMValueRef use_border
= NULL
;
164 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
165 if (wrap_mode_uses_border_color(bld
->static_state
->wrap_s
)) {
167 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
168 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
169 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
172 if (wrap_mode_uses_border_color(bld
->static_state
->wrap_t
)) {
174 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
175 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
177 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
178 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
181 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
186 * Note: if we find an app which frequently samples the texture border
187 * we might want to implement a true conditional here to avoid sampling
188 * the texture whenever possible (since that's quite a bit of code).
191 * texel = border_color;
194 * texel = sample_texture(coord);
196 * As it is now, we always sample the texture, then selectively replace
197 * the texel color results with the border color.
200 /* convert x,y coords to linear offset from start of texture, in bytes */
201 offset
= lp_build_sample_offset(&bld
->uint_coord_bld
,
205 assert(bld
->format_desc
->block
.width
== 1);
206 assert(bld
->format_desc
->block
.height
== 1);
207 assert(bld
->format_desc
->block
.bits
<= bld
->texel_type
.width
);
209 /* gather the texels from the texture */
210 packed
= lp_build_gather(bld
->builder
,
211 bld
->texel_type
.length
,
212 bld
->format_desc
->block
.bits
,
213 bld
->texel_type
.width
,
216 /* convert texels to float rgba */
217 lp_build_unpack_rgba_soa(bld
->builder
,
223 /* select texel color or border color depending on use_border */
225 for (chan
= 0; chan
< 4; chan
++) {
226 LLVMValueRef border_chan
=
227 lp_build_const_scalar(bld
->texel_type
,
228 bld
->static_state
->border_color
[chan
]);
229 texel
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
230 border_chan
, texel
[chan
]);
237 lp_build_sample_packed(struct lp_build_sample_context
*bld
,
240 LLVMValueRef y_stride
,
241 LLVMValueRef data_array
)
244 LLVMValueRef data_ptr
;
246 offset
= lp_build_sample_offset(&bld
->uint_coord_bld
,
250 assert(bld
->format_desc
->block
.width
== 1);
251 assert(bld
->format_desc
->block
.height
== 1);
252 assert(bld
->format_desc
->block
.bits
<= bld
->texel_type
.width
);
254 /* get pointer to mipmap level 0 data */
255 data_ptr
= lp_build_get_const_mipmap_level(bld
, data_array
, 0);
257 return lp_build_gather(bld
->builder
,
258 bld
->texel_type
.length
,
259 bld
->format_desc
->block
.bits
,
260 bld
->texel_type
.width
,
266 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
269 lp_build_coord_mirror(struct lp_build_sample_context
*bld
,
272 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
273 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
274 LLVMValueRef fract
, flr
, isOdd
;
276 /* fract = coord - floor(coord) */
277 fract
= lp_build_sub(coord_bld
, coord
, lp_build_floor(coord_bld
, coord
));
279 /* flr = ifloor(coord); */
280 flr
= lp_build_ifloor(coord_bld
, coord
);
282 /* isOdd = flr & 1 */
283 isOdd
= LLVMBuildAnd(bld
->builder
, flr
, int_coord_bld
->one
, "");
285 /* make coord positive or negative depending on isOdd */
286 coord
= lp_build_set_sign(coord_bld
, fract
, isOdd
);
288 /* convert isOdd to float */
289 isOdd
= lp_build_int_to_float(coord_bld
, isOdd
);
291 /* add isOdd to coord */
292 coord
= lp_build_add(coord_bld
, coord
, isOdd
);
299 * We only support a few wrap modes in lp_build_sample_wrap_int() at this time.
300 * Return whether the given mode is supported by that function.
303 is_simple_wrap_mode(unsigned mode
)
306 case PIPE_TEX_WRAP_REPEAT
:
307 case PIPE_TEX_WRAP_CLAMP
:
308 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
310 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
318 * Build LLVM code for texture wrap mode, for scaled integer texcoords.
319 * \param coord the incoming texcoord (s,t,r or q) scaled to the texture size
320 * \param length the texture size along one dimension
321 * \param is_pot if TRUE, length is a power of two
322 * \param wrap_mode one of PIPE_TEX_WRAP_x
325 lp_build_sample_wrap_int(struct lp_build_sample_context
*bld
,
331 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
332 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
333 LLVMValueRef length_minus_one
;
335 length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
338 case PIPE_TEX_WRAP_REPEAT
:
340 coord
= LLVMBuildAnd(bld
->builder
, coord
, length_minus_one
, "");
342 /* Signed remainder won't give the right results for negative
343 * dividends but unsigned remainder does.*/
344 coord
= LLVMBuildURem(bld
->builder
, coord
, length
, "");
347 case PIPE_TEX_WRAP_CLAMP
:
348 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
349 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
350 coord
= lp_build_max(int_coord_bld
, coord
, int_coord_bld
->zero
);
351 coord
= lp_build_min(int_coord_bld
, coord
, length_minus_one
);
354 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
355 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
356 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
357 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
359 _debug_printf("llvmpipe: failed to translate texture wrap mode %s\n",
360 util_dump_tex_wrap(wrap_mode
, TRUE
));
361 coord
= lp_build_max(uint_coord_bld
, coord
, uint_coord_bld
->zero
);
362 coord
= lp_build_min(uint_coord_bld
, coord
, length_minus_one
);
374 * Build LLVM code for texture wrap mode for linear filtering.
375 * \param x0_out returns first integer texcoord
376 * \param x1_out returns second integer texcoord
377 * \param weight_out returns linear interpolation weight
380 lp_build_sample_wrap_linear(struct lp_build_sample_context
*bld
,
385 LLVMValueRef
*x0_out
,
386 LLVMValueRef
*x1_out
,
387 LLVMValueRef
*weight_out
)
389 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
390 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
391 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
392 LLVMValueRef two
= lp_build_const_scalar(coord_bld
->type
, 2.0);
393 LLVMValueRef half
= lp_build_const_scalar(coord_bld
->type
, 0.5);
394 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
395 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
396 LLVMValueRef length_f_minus_one
= lp_build_sub(coord_bld
, length_f
, coord_bld
->one
);
397 LLVMValueRef coord0
, coord1
, weight
;
400 case PIPE_TEX_WRAP_REPEAT
:
401 /* mul by size and subtract 0.5 */
402 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
403 coord
= lp_build_sub(coord_bld
, coord
, half
);
405 coord0
= lp_build_ifloor(coord_bld
, coord
);
406 coord1
= lp_build_add(uint_coord_bld
, coord0
, uint_coord_bld
->one
);
407 /* compute lerp weight */
408 weight
= lp_build_fract(coord_bld
, coord
);
411 coord0
= LLVMBuildAnd(bld
->builder
, coord0
, length_minus_one
, "");
412 coord1
= LLVMBuildAnd(bld
->builder
, coord1
, length_minus_one
, "");
415 /* Signed remainder won't give the right results for negative
416 * dividends but unsigned remainder does.*/
417 coord0
= LLVMBuildURem(bld
->builder
, coord0
, length
, "");
418 coord1
= LLVMBuildURem(bld
->builder
, coord1
, length
, "");
422 case PIPE_TEX_WRAP_CLAMP
:
423 if (bld
->static_state
->normalized_coords
) {
424 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
426 weight
= lp_build_fract(coord_bld
, coord
);
427 coord0
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
,
429 coord1
= lp_build_add(coord_bld
, coord
, coord_bld
->one
);
430 coord1
= lp_build_clamp(coord_bld
, coord1
, coord_bld
->zero
,
432 coord0
= lp_build_ifloor(coord_bld
, coord0
);
433 coord1
= lp_build_ifloor(coord_bld
, coord1
);
436 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
437 if (bld
->static_state
->normalized_coords
) {
439 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, coord_bld
->one
);
440 /* mul by tex size and subtract 0.5 */
441 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
442 coord
= lp_build_sub(coord_bld
, coord
, half
);
445 LLVMValueRef min
, max
;
446 /* clamp to [0.5, length - 0.5] */
447 min
= lp_build_const_scalar(coord_bld
->type
, 0.5F
);
448 max
= lp_build_sub(coord_bld
, length_f
, min
);
449 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
451 /* compute lerp weight */
452 weight
= lp_build_fract(coord_bld
, coord
);
453 /* coord0 = floor(coord); */
454 coord0
= lp_build_ifloor(coord_bld
, coord
);
455 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
456 /* coord0 = max(coord0, 0) */
457 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
458 /* coord1 = min(coord1, length-1) */
459 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
462 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
464 LLVMValueRef min
, max
;
465 if (bld
->static_state
->normalized_coords
) {
466 /* min = -1.0 / (2 * length) = -0.5 / length */
467 min
= lp_build_mul(coord_bld
,
468 lp_build_const_scalar(coord_bld
->type
, -0.5F
),
469 lp_build_rcp(coord_bld
, length_f
));
470 /* max = 1.0 - min */
471 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
472 /* coord = clamp(coord, min, max) */
473 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
474 /* scale coord to length (and sub 0.5?) */
475 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
476 coord
= lp_build_sub(coord_bld
, coord
, half
);
479 /* clamp to [-0.5, length + 0.5] */
480 min
= lp_build_const_scalar(coord_bld
->type
, -0.5F
);
481 max
= lp_build_sub(coord_bld
, length_f
, min
);
482 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
483 coord
= lp_build_sub(coord_bld
, coord
, half
);
485 /* compute lerp weight */
486 weight
= lp_build_fract(coord_bld
, coord
);
488 coord0
= lp_build_ifloor(coord_bld
, coord
);
489 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
493 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
494 /* compute mirror function */
495 coord
= lp_build_coord_mirror(bld
, coord
);
497 /* scale coord to length */
498 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
499 coord
= lp_build_sub(coord_bld
, coord
, half
);
501 /* compute lerp weight */
502 weight
= lp_build_fract(coord_bld
, coord
);
504 /* convert to int coords */
505 coord0
= lp_build_ifloor(coord_bld
, coord
);
506 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
508 /* coord0 = max(coord0, 0) */
509 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
510 /* coord1 = min(coord1, length-1) */
511 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
514 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
516 LLVMValueRef min
, max
;
517 /* min = 1.0 / (2 * length) */
518 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
519 /* max = 1.0 - min */
520 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
522 coord
= lp_build_abs(coord_bld
, coord
);
523 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
524 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
525 if(0)coord
= lp_build_sub(coord_bld
, coord
, half
);
526 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_CLAMP_TO_EDGE
:
534 LLVMValueRef min
, max
;
535 /* min = 1.0 / (2 * length) */
536 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
537 /* max = 1.0 - min */
538 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
540 coord
= lp_build_abs(coord_bld
, coord
);
541 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
542 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
543 coord
= lp_build_sub(coord_bld
, coord
, half
);
544 weight
= lp_build_fract(coord_bld
, coord
);
545 coord0
= lp_build_ifloor(coord_bld
, coord
);
546 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
550 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
552 LLVMValueRef min
, max
;
553 /* min = -1.0 / (2 * length) = -0.5 / length */
554 min
= lp_build_mul(coord_bld
,
555 lp_build_const_scalar(coord_bld
->type
, -0.5F
),
556 lp_build_rcp(coord_bld
, length_f
));
557 /* max = 1.0 - min */
558 max
= lp_build_sub(coord_bld
, coord_bld
->one
, min
);
560 coord
= lp_build_abs(coord_bld
, coord
);
561 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
562 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
563 coord
= lp_build_sub(coord_bld
, coord
, half
);
564 weight
= lp_build_fract(coord_bld
, coord
);
565 coord0
= lp_build_ifloor(coord_bld
, coord
);
566 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
579 *weight_out
= weight
;
584 * Build LLVM code for texture wrap mode for nearest filtering.
585 * \param coord the incoming texcoord (nominally in [0,1])
586 * \param length the texture size along one dimension, as int
587 * \param is_pot if TRUE, length is a power of two
588 * \param wrap_mode one of PIPE_TEX_WRAP_x
591 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
597 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
598 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
599 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
600 LLVMValueRef two
= lp_build_const_scalar(coord_bld
->type
, 2.0);
601 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
602 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
603 LLVMValueRef length_f_minus_one
= lp_build_sub(coord_bld
, length_f
, coord_bld
->one
);
607 case PIPE_TEX_WRAP_REPEAT
:
608 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
609 icoord
= lp_build_ifloor(coord_bld
, coord
);
611 icoord
= LLVMBuildAnd(bld
->builder
, icoord
, length_minus_one
, "");
613 /* Signed remainder won't give the right results for negative
614 * dividends but unsigned remainder does.*/
615 icoord
= LLVMBuildURem(bld
->builder
, icoord
, length
, "");
618 case PIPE_TEX_WRAP_CLAMP
:
620 if (bld
->static_state
->normalized_coords
) {
621 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
624 icoord
= lp_build_ifloor(coord_bld
, coord
);
625 /* clamp to [0, size-1]. Note: int coord builder type */
626 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
630 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
632 LLVMValueRef min
, max
;
633 if (bld
->static_state
->normalized_coords
) {
634 /* min = 1.0 / (2 * length) */
635 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
636 /* max = length - min */
637 max
= lp_build_sub(coord_bld
, length_f
, min
);
638 /* scale coord to length */
639 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
642 /* clamp to [0.5, length - 0.5] */
643 min
= lp_build_const_scalar(coord_bld
->type
, 0.5F
);
644 max
= lp_build_sub(coord_bld
, length_f
, min
);
646 /* coord = clamp(coord, min, max) */
647 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
648 icoord
= lp_build_ifloor(coord_bld
, coord
);
652 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
653 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
655 LLVMValueRef min
, max
;
656 if (bld
->static_state
->normalized_coords
) {
657 /* min = -1.0 / (2 * length) = -0.5 / length */
658 min
= lp_build_mul(coord_bld
,
659 lp_build_const_scalar(coord_bld
->type
, -0.5F
),
660 lp_build_rcp(coord_bld
, length_f
));
661 /* max = length - min */
662 max
= lp_build_sub(coord_bld
, length_f
, min
);
663 /* scale coord to length */
664 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
667 /* clamp to [-0.5, length + 0.5] */
668 min
= lp_build_const_scalar(coord_bld
->type
, -0.5F
);
669 max
= lp_build_sub(coord_bld
, length_f
, min
);
671 /* coord = clamp(coord, min, max) */
672 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
673 icoord
= lp_build_ifloor(coord_bld
, coord
);
677 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
679 LLVMValueRef min
, max
;
680 /* min = 1.0 / (2 * length) */
681 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
682 /* max = length - min */
683 max
= lp_build_sub(coord_bld
, length_f
, min
);
685 /* compute mirror function */
686 coord
= lp_build_coord_mirror(bld
, coord
);
688 /* scale coord to length */
689 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
691 /* coord = clamp(coord, min, max) */
692 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
693 icoord
= lp_build_ifloor(coord_bld
, coord
);
697 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
698 coord
= lp_build_abs(coord_bld
, coord
);
699 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
700 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, length_f_minus_one
);
701 icoord
= lp_build_ifloor(coord_bld
, coord
);
704 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
706 LLVMValueRef min
, max
;
707 /* min = 1.0 / (2 * length) */
708 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
709 /* max = length - min */
710 max
= lp_build_sub(coord_bld
, length_f
, min
);
712 coord
= lp_build_abs(coord_bld
, coord
);
713 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
714 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
715 icoord
= lp_build_ifloor(coord_bld
, coord
);
719 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
721 LLVMValueRef min
, max
;
722 /* min = 1.0 / (2 * length) */
723 min
= lp_build_rcp(coord_bld
, lp_build_mul(coord_bld
, two
, length_f
));
724 min
= lp_build_negate(coord_bld
, min
);
725 /* max = length - min */
726 max
= lp_build_sub(coord_bld
, length_f
, min
);
728 coord
= lp_build_abs(coord_bld
, coord
);
729 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
730 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
731 icoord
= lp_build_ifloor(coord_bld
, coord
);
745 * Codegen equivalent for u_minify().
746 * Return max(1, base_size >> level);
749 lp_build_minify(struct lp_build_sample_context
*bld
,
750 LLVMValueRef base_size
,
753 LLVMValueRef size
= LLVMBuildAShr(bld
->builder
, base_size
, level
, "minify");
754 size
= lp_build_max(&bld
->int_coord_bld
, size
, bld
->int_coord_bld
.one
);
760 texture_dims(enum pipe_texture_target tex
)
763 case PIPE_TEXTURE_1D
:
765 case PIPE_TEXTURE_2D
:
766 case PIPE_TEXTURE_CUBE
:
768 case PIPE_TEXTURE_3D
:
771 assert(0 && "bad texture target in texture_dims()");
778 * Generate code to compute texture level of detail (lambda).
779 * \param s vector of texcoord s values
780 * \param t vector of texcoord t values
781 * \param r vector of texcoord r values
782 * \param width scalar int texture width
783 * \param height scalar int texture height
784 * \param depth scalar int texture depth
787 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
796 const int dims
= texture_dims(bld
->static_state
->target
);
797 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
798 struct lp_build_context
*float_bld
= &bld
->float_bld
;
799 LLVMValueRef lod_bias
= LLVMConstReal(LLVMFloatType(), bld
->static_state
->lod_bias
);
800 LLVMValueRef min_lod
= LLVMConstReal(LLVMFloatType(), bld
->static_state
->min_lod
);
801 LLVMValueRef max_lod
= LLVMConstReal(LLVMFloatType(), bld
->static_state
->max_lod
);
803 LLVMValueRef index0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
804 LLVMValueRef index1
= LLVMConstInt(LLVMInt32Type(), 1, 0);
805 LLVMValueRef index2
= LLVMConstInt(LLVMInt32Type(), 2, 0);
807 LLVMValueRef s0
, s1
, s2
;
808 LLVMValueRef t0
, t1
, t2
;
809 LLVMValueRef r0
, r1
, r2
;
810 LLVMValueRef dsdx
, dsdy
, dtdx
, dtdy
, drdx
, drdy
;
811 LLVMValueRef rho
, lod
;
814 * dsdx = abs(s[1] - s[0]);
815 * dsdy = abs(s[2] - s[0]);
816 * dtdx = abs(t[1] - t[0]);
817 * dtdy = abs(t[2] - t[0]);
818 * drdx = abs(r[1] - r[0]);
819 * drdy = abs(r[2] - r[0]);
820 * XXX we're assuming a four-element quad in 2x2 layout here.
822 s0
= LLVMBuildExtractElement(bld
->builder
, s
, index0
, "s0");
823 s1
= LLVMBuildExtractElement(bld
->builder
, s
, index1
, "s1");
824 s2
= LLVMBuildExtractElement(bld
->builder
, s
, index2
, "s2");
825 dsdx
= LLVMBuildSub(bld
->builder
, s1
, s0
, "");
826 dsdx
= lp_build_abs(float_bld
, dsdx
);
827 dsdy
= LLVMBuildSub(bld
->builder
, s2
, s0
, "");
828 dsdy
= lp_build_abs(float_bld
, dsdy
);
830 t0
= LLVMBuildExtractElement(bld
->builder
, t
, index0
, "t0");
831 t1
= LLVMBuildExtractElement(bld
->builder
, t
, index1
, "t1");
832 t2
= LLVMBuildExtractElement(bld
->builder
, t
, index2
, "t2");
833 dtdx
= LLVMBuildSub(bld
->builder
, t1
, t0
, "");
834 dtdx
= lp_build_abs(float_bld
, dtdx
);
835 dtdy
= LLVMBuildSub(bld
->builder
, t2
, t0
, "");
836 dtdy
= lp_build_abs(float_bld
, dtdy
);
838 r0
= LLVMBuildExtractElement(bld
->builder
, r
, index0
, "r0");
839 r1
= LLVMBuildExtractElement(bld
->builder
, r
, index1
, "r1");
840 r2
= LLVMBuildExtractElement(bld
->builder
, r
, index2
, "r2");
841 drdx
= LLVMBuildSub(bld
->builder
, r1
, r0
, "");
842 drdx
= lp_build_abs(float_bld
, drdx
);
843 drdy
= LLVMBuildSub(bld
->builder
, r2
, r0
, "");
844 drdy
= lp_build_abs(float_bld
, drdy
);
848 /* Compute rho = max of all partial derivatives scaled by texture size.
849 * XXX this could be vectorized somewhat
851 rho
= LLVMBuildMul(bld
->builder
,
852 lp_build_max(float_bld
, dsdx
, dsdy
),
853 lp_build_int_to_float(float_bld
, width
), "");
856 max
= LLVMBuildMul(bld
->builder
,
857 lp_build_max(float_bld
, dtdx
, dtdy
),
858 lp_build_int_to_float(float_bld
, height
), "");
859 rho
= lp_build_max(float_bld
, rho
, max
);
861 max
= LLVMBuildMul(bld
->builder
,
862 lp_build_max(float_bld
, drdx
, drdy
),
863 lp_build_int_to_float(float_bld
, depth
), "");
864 rho
= lp_build_max(float_bld
, rho
, max
);
868 /* compute lod = log2(rho) */
869 lod
= lp_build_log2(float_bld
, rho
);
872 lod
= LLVMBuildAdd(bld
->builder
, lod
, lod_bias
, "LOD bias");
875 lod
= lp_build_clamp(float_bld
, lod
, min_lod
, max_lod
);
882 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
883 * mipmap level index.
884 * Note: this is all scalar code.
885 * \param lod scalar float texture level of detail
886 * \param level_out returns integer
889 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
892 LLVMValueRef
*level_out
)
894 struct lp_build_context
*float_bld
= &bld
->float_bld
;
895 struct lp_build_context
*int_bld
= &bld
->int_bld
;
896 LLVMValueRef last_level
, level
;
898 LLVMValueRef zero
= LLVMConstInt(LLVMInt32Type(), 0, 0);
900 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
903 /* convert float lod to integer */
904 level
= lp_build_iround(float_bld
, lod
);
906 /* clamp level to legal range of levels */
907 *level_out
= lp_build_clamp(int_bld
, level
, zero
, last_level
);
912 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
913 * two (adjacent) mipmap level indexes. Later, we'll sample from those
914 * two mipmap levels and interpolate between them.
917 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
920 LLVMValueRef
*level0_out
,
921 LLVMValueRef
*level1_out
,
922 LLVMValueRef
*weight_out
)
924 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
925 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
926 LLVMValueRef last_level
, level
;
928 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
931 /* convert float lod to integer */
932 level
= lp_build_ifloor(coord_bld
, lod
);
934 /* compute level 0 and clamp to legal range of levels */
935 *level0_out
= lp_build_clamp(int_coord_bld
, level
,
938 /* compute level 1 and clamp to legal range of levels */
939 *level1_out
= lp_build_add(int_coord_bld
, *level0_out
, int_coord_bld
->one
);
940 *level1_out
= lp_build_min(int_coord_bld
, *level1_out
, int_coord_bld
->zero
);
942 *weight_out
= lp_build_fract(coord_bld
, lod
);
948 * Sample 2D texture with nearest filtering, no mipmapping.
951 lp_build_sample_2d_nearest_soa(struct lp_build_sample_context
*bld
,
957 LLVMValueRef data_array
,
961 LLVMValueRef data_ptr
;
963 x
= lp_build_sample_wrap_nearest(bld
, s
, width
,
964 bld
->static_state
->pot_width
,
965 bld
->static_state
->wrap_s
);
966 y
= lp_build_sample_wrap_nearest(bld
, t
, height
,
967 bld
->static_state
->pot_height
,
968 bld
->static_state
->wrap_t
);
970 lp_build_name(x
, "tex.x.wrapped");
971 lp_build_name(y
, "tex.y.wrapped");
973 /* get pointer to mipmap level 0 data */
974 data_ptr
= lp_build_get_const_mipmap_level(bld
, data_array
, 0);
976 lp_build_sample_texel_soa(bld
, width
, height
, x
, y
, stride
, data_ptr
, texel
);
981 * Sample 2D texture with nearest filtering, nearest mipmap.
984 lp_build_sample_2d_nearest_mip_nearest_soa(struct lp_build_sample_context
*bld
,
990 LLVMValueRef width_vec
,
991 LLVMValueRef height_vec
,
993 LLVMValueRef data_array
,
997 LLVMValueRef lod
, ilevel
, ilevel_vec
;
998 LLVMValueRef data_ptr
;
1000 /* compute float LOD */
1001 lod
= lp_build_lod_selector(bld
, s
, t
, NULL
, width
, height
, NULL
);
1003 /* convert LOD to int */
1004 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel
);
1006 ilevel_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel
);
1008 /* compute width_vec, height at mipmap level 'ilevel' */
1009 width_vec
= lp_build_minify(bld
, width_vec
, ilevel_vec
);
1010 height_vec
= lp_build_minify(bld
, height_vec
, ilevel_vec
);
1011 stride
= lp_build_minify(bld
, stride
, ilevel_vec
);
1013 x
= lp_build_sample_wrap_nearest(bld
, s
, width_vec
,
1014 bld
->static_state
->pot_width
,
1015 bld
->static_state
->wrap_s
);
1016 y
= lp_build_sample_wrap_nearest(bld
, t
, height_vec
,
1017 bld
->static_state
->pot_height
,
1018 bld
->static_state
->wrap_t
);
1020 lp_build_name(x
, "tex.x.wrapped");
1021 lp_build_name(y
, "tex.y.wrapped");
1023 /* get pointer to mipmap level [ilevel] data */
1025 data_ptr
= lp_build_get_mipmap_level(bld
, data_array
, ilevel
);
1027 data_ptr
= lp_build_get_const_mipmap_level(bld
, data_array
, 0);
1029 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, x
, y
, stride
, data_ptr
, texel
);
1034 * Sample 2D texture with bilinear filtering.
1037 lp_build_sample_2d_linear_soa(struct lp_build_sample_context
*bld
,
1041 LLVMValueRef height
,
1042 LLVMValueRef stride
,
1043 LLVMValueRef data_array
,
1044 LLVMValueRef
*texel
)
1046 LLVMValueRef s_fpart
;
1047 LLVMValueRef t_fpart
;
1048 LLVMValueRef x0
, x1
;
1049 LLVMValueRef y0
, y1
;
1050 LLVMValueRef neighbors
[2][2][4];
1051 LLVMValueRef data_ptr
;
1054 lp_build_sample_wrap_linear(bld
, s
, width
, bld
->static_state
->pot_width
,
1055 bld
->static_state
->wrap_s
, &x0
, &x1
, &s_fpart
);
1056 lp_build_sample_wrap_linear(bld
, t
, height
, bld
->static_state
->pot_height
,
1057 bld
->static_state
->wrap_t
, &y0
, &y1
, &t_fpart
);
1059 /* get pointer to mipmap level 0 data */
1060 data_ptr
= lp_build_get_const_mipmap_level(bld
, data_array
, 0);
1062 lp_build_sample_texel_soa(bld
, width
, height
, x0
, y0
, stride
, data_ptr
, neighbors
[0][0]);
1063 lp_build_sample_texel_soa(bld
, width
, height
, x1
, y0
, stride
, data_ptr
, neighbors
[0][1]);
1064 lp_build_sample_texel_soa(bld
, width
, height
, x0
, y1
, stride
, data_ptr
, neighbors
[1][0]);
1065 lp_build_sample_texel_soa(bld
, width
, height
, x1
, y1
, stride
, data_ptr
, neighbors
[1][1]);
1067 /* TODO: Don't interpolate missing channels */
1068 for(chan
= 0; chan
< 4; ++chan
) {
1069 texel
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1071 neighbors
[0][0][chan
],
1072 neighbors
[0][1][chan
],
1073 neighbors
[1][0][chan
],
1074 neighbors
[1][1][chan
]);
1080 lp_build_rgba8_to_f32_soa(LLVMBuilderRef builder
,
1081 struct lp_type dst_type
,
1082 LLVMValueRef packed
,
1085 LLVMValueRef mask
= lp_build_int_const_scalar(dst_type
, 0xff);
1088 /* Decode the input vector components */
1089 for (chan
= 0; chan
< 4; ++chan
) {
1090 unsigned start
= chan
*8;
1091 unsigned stop
= start
+ 8;
1097 input
= LLVMBuildLShr(builder
, input
, lp_build_int_const_scalar(dst_type
, start
), "");
1100 input
= LLVMBuildAnd(builder
, input
, mask
, "");
1102 input
= lp_build_unsigned_norm_to_float(builder
, 8, dst_type
, input
);
1110 lp_build_sample_2d_linear_aos(struct lp_build_sample_context
*bld
,
1114 LLVMValueRef height
,
1115 LLVMValueRef stride
,
1116 LLVMValueRef data_array
,
1117 LLVMValueRef
*texel
)
1119 LLVMBuilderRef builder
= bld
->builder
;
1120 struct lp_build_context i32
, h16
, u8n
;
1121 LLVMTypeRef i32_vec_type
, h16_vec_type
, u8n_vec_type
;
1122 LLVMValueRef i32_c8
, i32_c128
, i32_c255
;
1123 LLVMValueRef s_ipart
, s_fpart
, s_fpart_lo
, s_fpart_hi
;
1124 LLVMValueRef t_ipart
, t_fpart
, t_fpart_lo
, t_fpart_hi
;
1125 LLVMValueRef x0
, x1
;
1126 LLVMValueRef y0
, y1
;
1127 LLVMValueRef neighbors
[2][2];
1128 LLVMValueRef neighbors_lo
[2][2];
1129 LLVMValueRef neighbors_hi
[2][2];
1130 LLVMValueRef packed
, packed_lo
, packed_hi
;
1131 LLVMValueRef unswizzled
[4];
1133 lp_build_context_init(&i32
, builder
, lp_type_int_vec(32));
1134 lp_build_context_init(&h16
, builder
, lp_type_ufixed(16));
1135 lp_build_context_init(&u8n
, builder
, lp_type_unorm(8));
1137 i32_vec_type
= lp_build_vec_type(i32
.type
);
1138 h16_vec_type
= lp_build_vec_type(h16
.type
);
1139 u8n_vec_type
= lp_build_vec_type(u8n
.type
);
1141 if (bld
->static_state
->normalized_coords
) {
1142 LLVMTypeRef coord_vec_type
= lp_build_vec_type(bld
->coord_type
);
1143 LLVMValueRef fp_width
= LLVMBuildSIToFP(bld
->builder
, width
, coord_vec_type
, "");
1144 LLVMValueRef fp_height
= LLVMBuildSIToFP(bld
->builder
, height
, coord_vec_type
, "");
1145 s
= lp_build_mul(&bld
->coord_bld
, s
, fp_width
);
1146 t
= lp_build_mul(&bld
->coord_bld
, t
, fp_height
);
1149 /* scale coords by 256 (8 fractional bits) */
1150 s
= lp_build_mul_imm(&bld
->coord_bld
, s
, 256);
1151 t
= lp_build_mul_imm(&bld
->coord_bld
, t
, 256);
1153 /* convert float to int */
1154 s
= LLVMBuildFPToSI(builder
, s
, i32_vec_type
, "");
1155 t
= LLVMBuildFPToSI(builder
, t
, i32_vec_type
, "");
1157 /* subtract 0.5 (add -128) */
1158 i32_c128
= lp_build_int_const_scalar(i32
.type
, -128);
1159 s
= LLVMBuildAdd(builder
, s
, i32_c128
, "");
1160 t
= LLVMBuildAdd(builder
, t
, i32_c128
, "");
1162 /* compute floor (shift right 8) */
1163 i32_c8
= lp_build_int_const_scalar(i32
.type
, 8);
1164 s_ipart
= LLVMBuildAShr(builder
, s
, i32_c8
, "");
1165 t_ipart
= LLVMBuildAShr(builder
, t
, i32_c8
, "");
1167 /* compute fractional part (AND with 0xff) */
1168 i32_c255
= lp_build_int_const_scalar(i32
.type
, 255);
1169 s_fpart
= LLVMBuildAnd(builder
, s
, i32_c255
, "");
1170 t_fpart
= LLVMBuildAnd(builder
, t
, i32_c255
, "");
1175 x1
= lp_build_add(&bld
->int_coord_bld
, x0
, bld
->int_coord_bld
.one
);
1176 y1
= lp_build_add(&bld
->int_coord_bld
, y0
, bld
->int_coord_bld
.one
);
1178 x0
= lp_build_sample_wrap_int(bld
, x0
, width
, bld
->static_state
->pot_width
,
1179 bld
->static_state
->wrap_s
);
1180 y0
= lp_build_sample_wrap_int(bld
, y0
, height
, bld
->static_state
->pot_height
,
1181 bld
->static_state
->wrap_t
);
1183 x1
= lp_build_sample_wrap_int(bld
, x1
, width
, bld
->static_state
->pot_width
,
1184 bld
->static_state
->wrap_s
);
1185 y1
= lp_build_sample_wrap_int(bld
, y1
, height
, bld
->static_state
->pot_height
,
1186 bld
->static_state
->wrap_t
);
1189 * Transform 4 x i32 in
1191 * s_fpart = {s0, s1, s2, s3}
1195 * s_fpart = {00, s0, 00, s1, 00, s2, 00, s3}
1199 * s_fpart_lo = {s0, s0, s0, s0, s1, s1, s1, s1}
1200 * s_fpart_hi = {s2, s2, s2, s2, s3, s3, s3, s3}
1202 * and likewise for t_fpart. There is no risk of loosing precision here
1203 * since the fractional parts only use the lower 8bits.
1206 s_fpart
= LLVMBuildBitCast(builder
, s_fpart
, h16_vec_type
, "");
1207 t_fpart
= LLVMBuildBitCast(builder
, t_fpart
, h16_vec_type
, "");
1210 LLVMTypeRef elem_type
= LLVMInt32Type();
1211 LLVMValueRef shuffles_lo
[LP_MAX_VECTOR_LENGTH
];
1212 LLVMValueRef shuffles_hi
[LP_MAX_VECTOR_LENGTH
];
1213 LLVMValueRef shuffle_lo
;
1214 LLVMValueRef shuffle_hi
;
1217 for(j
= 0; j
< h16
.type
.length
; j
+= 4) {
1218 unsigned subindex
= util_cpu_caps
.little_endian
? 0 : 1;
1221 index
= LLVMConstInt(elem_type
, j
/2 + subindex
, 0);
1222 for(i
= 0; i
< 4; ++i
)
1223 shuffles_lo
[j
+ i
] = index
;
1225 index
= LLVMConstInt(elem_type
, h16
.type
.length
/2 + j
/2 + subindex
, 0);
1226 for(i
= 0; i
< 4; ++i
)
1227 shuffles_hi
[j
+ i
] = index
;
1230 shuffle_lo
= LLVMConstVector(shuffles_lo
, h16
.type
.length
);
1231 shuffle_hi
= LLVMConstVector(shuffles_hi
, h16
.type
.length
);
1233 s_fpart_lo
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_lo
, "");
1234 t_fpart_lo
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_lo
, "");
1235 s_fpart_hi
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_hi
, "");
1236 t_fpart_hi
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_hi
, "");
1240 * Fetch the pixels as 4 x 32bit (rgba order might differ):
1242 * rgba0 rgba1 rgba2 rgba3
1244 * bit cast them into 16 x u8
1246 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
1248 * unpack them into two 8 x i16:
1250 * r0 g0 b0 a0 r1 g1 b1 a1
1251 * r2 g2 b2 a2 r3 g3 b3 a3
1253 * The higher 8 bits of the resulting elements will be zero.
1256 neighbors
[0][0] = lp_build_sample_packed(bld
, x0
, y0
, stride
, data_array
);
1257 neighbors
[0][1] = lp_build_sample_packed(bld
, x1
, y0
, stride
, data_array
);
1258 neighbors
[1][0] = lp_build_sample_packed(bld
, x0
, y1
, stride
, data_array
);
1259 neighbors
[1][1] = lp_build_sample_packed(bld
, x1
, y1
, stride
, data_array
);
1261 neighbors
[0][0] = LLVMBuildBitCast(builder
, neighbors
[0][0], u8n_vec_type
, "");
1262 neighbors
[0][1] = LLVMBuildBitCast(builder
, neighbors
[0][1], u8n_vec_type
, "");
1263 neighbors
[1][0] = LLVMBuildBitCast(builder
, neighbors
[1][0], u8n_vec_type
, "");
1264 neighbors
[1][1] = LLVMBuildBitCast(builder
, neighbors
[1][1], u8n_vec_type
, "");
1266 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][0], &neighbors_lo
[0][0], &neighbors_hi
[0][0]);
1267 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][1], &neighbors_lo
[0][1], &neighbors_hi
[0][1]);
1268 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][0], &neighbors_lo
[1][0], &neighbors_hi
[1][0]);
1269 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][1], &neighbors_lo
[1][1], &neighbors_hi
[1][1]);
1272 * Linear interpolate with 8.8 fixed point.
1275 packed_lo
= lp_build_lerp_2d(&h16
,
1276 s_fpart_lo
, t_fpart_lo
,
1280 neighbors_lo
[1][1]);
1282 packed_hi
= lp_build_lerp_2d(&h16
,
1283 s_fpart_hi
, t_fpart_hi
,
1287 neighbors_hi
[1][1]);
1289 packed
= lp_build_pack2(builder
, h16
.type
, u8n
.type
, packed_lo
, packed_hi
);
1292 * Convert to SoA and swizzle.
1295 packed
= LLVMBuildBitCast(builder
, packed
, i32_vec_type
, "");
1297 lp_build_rgba8_to_f32_soa(bld
->builder
,
1299 packed
, unswizzled
);
1301 lp_build_format_swizzle_soa(bld
->format_desc
,
1302 bld
->texel_type
, unswizzled
,
1308 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
1310 LLVMValueRef
*texel
)
1312 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1316 if(bld
->static_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
1319 /* TODO: Compare before swizzling, to avoid redundant computations */
1321 for(chan
= 0; chan
< 4; ++chan
) {
1323 cmp
= lp_build_cmp(texel_bld
, bld
->static_state
->compare_func
, p
, texel
[chan
]);
1324 cmp
= lp_build_select(texel_bld
, cmp
, texel_bld
->one
, texel_bld
->zero
);
1327 res
= lp_build_add(texel_bld
, res
, cmp
);
1333 res
= lp_build_mul(texel_bld
, res
, lp_build_const_scalar(texel_bld
->type
, 0.25));
1335 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1336 for(chan
= 0; chan
< 3; ++chan
)
1338 texel
[3] = texel_bld
->one
;
1343 * Build texture sampling code.
1344 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1346 * \param type vector float type to use for coords, etc.
1349 lp_build_sample_soa(LLVMBuilderRef builder
,
1350 const struct lp_sampler_static_state
*static_state
,
1351 struct lp_sampler_dynamic_state
*dynamic_state
,
1352 struct lp_type type
,
1354 unsigned num_coords
,
1355 const LLVMValueRef
*coords
,
1356 LLVMValueRef lodbias
,
1357 LLVMValueRef
*texel
)
1359 struct lp_build_sample_context bld
;
1360 LLVMValueRef width
, width_vec
;
1361 LLVMValueRef height
, height_vec
;
1362 LLVMValueRef stride
, stride_vec
;
1363 LLVMValueRef data_array
;
1367 boolean done
= FALSE
;
1369 (void) lp_build_lod_selector
; /* temporary to silence warning */
1370 (void) lp_build_nearest_mip_level
;
1371 (void) lp_build_linear_mip_levels
;
1372 (void) lp_build_minify
;
1374 /* Setup our build context */
1375 memset(&bld
, 0, sizeof bld
);
1376 bld
.builder
= builder
;
1377 bld
.static_state
= static_state
;
1378 bld
.dynamic_state
= dynamic_state
;
1379 bld
.format_desc
= util_format_description(static_state
->format
);
1381 bld
.float_type
= lp_type_float(32);
1382 bld
.int_type
= lp_type_int(32);
1383 bld
.coord_type
= type
;
1384 bld
.uint_coord_type
= lp_uint_type(type
);
1385 bld
.int_coord_type
= lp_int_type(type
);
1386 bld
.texel_type
= type
;
1388 lp_build_context_init(&bld
.float_bld
, builder
, bld
.float_type
);
1389 lp_build_context_init(&bld
.int_bld
, builder
, bld
.int_type
);
1390 lp_build_context_init(&bld
.coord_bld
, builder
, bld
.coord_type
);
1391 lp_build_context_init(&bld
.uint_coord_bld
, builder
, bld
.uint_coord_type
);
1392 lp_build_context_init(&bld
.int_coord_bld
, builder
, bld
.int_coord_type
);
1393 lp_build_context_init(&bld
.texel_bld
, builder
, bld
.texel_type
);
1395 /* Get the dynamic state */
1396 width
= dynamic_state
->width(dynamic_state
, builder
, unit
);
1397 height
= dynamic_state
->height(dynamic_state
, builder
, unit
);
1398 stride
= dynamic_state
->stride(dynamic_state
, builder
, unit
);
1399 data_array
= dynamic_state
->data_ptr(dynamic_state
, builder
, unit
);
1400 /* Note that data_array is an array[level] of pointers to texture images */
1406 width_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, width
);
1407 height_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, height
);
1408 stride_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, stride
);
1410 if(static_state
->target
== PIPE_TEXTURE_1D
)
1411 t
= bld
.coord_bld
.zero
;
1413 switch (static_state
->min_mip_filter
) {
1414 case PIPE_TEX_MIPFILTER_NONE
:
1416 case PIPE_TEX_MIPFILTER_NEAREST
:
1418 switch (static_state
->min_img_filter
) {
1419 case PIPE_TEX_FILTER_NEAREST
:
1420 lp_build_sample_2d_nearest_mip_nearest_soa(&bld
, unit
,
1423 width_vec
, height_vec
,
1431 case PIPE_TEX_MIPFILTER_LINEAR
:
1434 assert(0 && "invalid mip filter");
1438 switch (static_state
->min_img_filter
) {
1439 case PIPE_TEX_FILTER_NEAREST
:
1440 lp_build_sample_2d_nearest_soa(&bld
, s
, t
, width_vec
, height_vec
,
1441 stride_vec
, data_array
, texel
);
1443 case PIPE_TEX_FILTER_LINEAR
:
1444 if(lp_format_is_rgba8(bld
.format_desc
) &&
1445 is_simple_wrap_mode(static_state
->wrap_s
) &&
1446 is_simple_wrap_mode(static_state
->wrap_t
))
1447 lp_build_sample_2d_linear_aos(&bld
, s
, t
, width_vec
, height_vec
,
1448 stride_vec
, data_array
, texel
);
1450 lp_build_sample_2d_linear_soa(&bld
, s
, t
, width_vec
, height_vec
,
1451 stride_vec
, data_array
, texel
);
1458 /* FIXME: respect static_state->min_mip_filter */;
1459 /* FIXME: respect static_state->mag_img_filter */;
1461 lp_build_sample_compare(&bld
, r
, texel
);