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>
33 * @author Brian Paul <brianp@vmware.com>
36 #include "pipe/p_defines.h"
37 #include "pipe/p_state.h"
38 #include "util/u_debug.h"
39 #include "util/u_dump.h"
40 #include "util/u_memory.h"
41 #include "util/u_math.h"
42 #include "util/u_format.h"
43 #include "util/u_cpu_detect.h"
44 #include "lp_bld_debug.h"
45 #include "lp_bld_type.h"
46 #include "lp_bld_const.h"
47 #include "lp_bld_conv.h"
48 #include "lp_bld_arit.h"
49 #include "lp_bld_logic.h"
50 #include "lp_bld_swizzle.h"
51 #include "lp_bld_pack.h"
52 #include "lp_bld_flow.h"
53 #include "lp_bld_gather.h"
54 #include "lp_bld_format.h"
55 #include "lp_bld_sample.h"
56 #include "lp_bld_quad.h"
60 * Keep all information for sampling code generation in a single place.
62 struct lp_build_sample_context
64 LLVMBuilderRef builder
;
66 const struct lp_sampler_static_state
*static_state
;
68 struct lp_sampler_dynamic_state
*dynamic_state
;
70 const struct util_format_description
*format_desc
;
72 /** regular scalar float type */
73 struct lp_type float_type
;
74 struct lp_build_context float_bld
;
76 /** regular scalar float type */
77 struct lp_type int_type
;
78 struct lp_build_context int_bld
;
80 /** Incoming coordinates type and build context */
81 struct lp_type coord_type
;
82 struct lp_build_context coord_bld
;
84 /** Unsigned integer coordinates */
85 struct lp_type uint_coord_type
;
86 struct lp_build_context uint_coord_bld
;
88 /** Signed integer coordinates */
89 struct lp_type int_coord_type
;
90 struct lp_build_context int_coord_bld
;
92 /** Output texels type and build context */
93 struct lp_type texel_type
;
94 struct lp_build_context texel_bld
;
99 * Does the given texture wrap mode allow sampling the texture border color?
100 * XXX maybe move this into gallium util code.
103 wrap_mode_uses_border_color(unsigned mode
)
106 case PIPE_TEX_WRAP_REPEAT
:
107 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
108 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
109 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
111 case PIPE_TEX_WRAP_CLAMP
:
112 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
113 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
114 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
117 assert(0 && "unexpected wrap mode");
124 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
125 LLVMValueRef data_array
, LLVMValueRef level
)
127 LLVMValueRef indexes
[2], data_ptr
;
128 indexes
[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
130 data_ptr
= LLVMBuildGEP(bld
->builder
, data_array
, indexes
, 2, "");
131 data_ptr
= LLVMBuildLoad(bld
->builder
, data_ptr
, "");
137 lp_build_get_const_mipmap_level(struct lp_build_sample_context
*bld
,
138 LLVMValueRef data_array
, int level
)
140 LLVMValueRef lvl
= LLVMConstInt(LLVMInt32Type(), level
, 0);
141 return lp_build_get_mipmap_level(bld
, data_array
, lvl
);
146 * Dereference stride_array[mipmap_level] array to get a stride.
147 * Return stride as a vector.
150 lp_build_get_level_stride_vec(struct lp_build_sample_context
*bld
,
151 LLVMValueRef stride_array
, LLVMValueRef level
)
153 LLVMValueRef indexes
[2], stride
;
154 indexes
[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
156 stride
= LLVMBuildGEP(bld
->builder
, stride_array
, indexes
, 2, "");
157 stride
= LLVMBuildLoad(bld
->builder
, stride
, "");
158 stride
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, stride
);
163 /** Dereference stride_array[0] array to get a stride (as vector). */
165 lp_build_get_const_level_stride_vec(struct lp_build_sample_context
*bld
,
166 LLVMValueRef stride_array
, int level
)
168 LLVMValueRef lvl
= LLVMConstInt(LLVMInt32Type(), level
, 0);
169 return lp_build_get_level_stride_vec(bld
, stride_array
, lvl
);
174 texture_dims(enum pipe_texture_target tex
)
177 case PIPE_TEXTURE_1D
:
179 case PIPE_TEXTURE_2D
:
180 case PIPE_TEXTURE_CUBE
:
182 case PIPE_TEXTURE_3D
:
185 assert(0 && "bad texture target in texture_dims()");
192 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
195 unsigned char swizzles
[4];
197 swizzles
[0] = bld
->static_state
->swizzle_r
;
198 swizzles
[1] = bld
->static_state
->swizzle_g
;
199 swizzles
[2] = bld
->static_state
->swizzle_b
;
200 swizzles
[3] = bld
->static_state
->swizzle_a
;
202 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
208 * Generate code to fetch a texel from a texture at int coords (x, y, z).
209 * The computation depends on whether the texture is 1D, 2D or 3D.
210 * The result, texel, will be:
211 * texel[0] = red values
212 * texel[1] = green values
213 * texel[2] = blue values
214 * texel[3] = alpha values
217 lp_build_sample_texel_soa(struct lp_build_sample_context
*bld
,
224 LLVMValueRef y_stride
,
225 LLVMValueRef z_stride
,
226 LLVMValueRef data_ptr
,
227 LLVMValueRef texel_out
[4])
229 const int dims
= texture_dims(bld
->static_state
->target
);
230 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
233 LLVMValueRef use_border
= NULL
;
235 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
236 if (wrap_mode_uses_border_color(bld
->static_state
->wrap_s
)) {
238 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
239 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
240 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
243 if (dims
>= 2 && wrap_mode_uses_border_color(bld
->static_state
->wrap_t
)) {
245 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
246 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
248 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
249 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
252 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
256 if (dims
== 3 && wrap_mode_uses_border_color(bld
->static_state
->wrap_r
)) {
258 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
259 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
261 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
262 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
265 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
269 /* convert x,y,z coords to linear offset from start of texture, in bytes */
270 lp_build_sample_offset(&bld
->uint_coord_bld
,
272 x
, y
, z
, y_stride
, z_stride
,
276 /* If we can sample the border color, it means that texcoords may
277 * lie outside the bounds of the texture image. We need to do
278 * something to prevent reading out of bounds and causing a segfault.
280 * Simply AND the texture coords with !use_border. This will cause
281 * coords which are out of bounds to become zero. Zero's guaranteed
282 * to be inside the texture image.
284 offset
= lp_build_andc(&bld
->uint_coord_bld
, offset
, use_border
);
287 lp_build_fetch_rgba_soa(bld
->builder
,
294 apply_sampler_swizzle(bld
, texel_out
);
297 * Note: if we find an app which frequently samples the texture border
298 * we might want to implement a true conditional here to avoid sampling
299 * the texture whenever possible (since that's quite a bit of code).
302 * texel = border_color;
305 * texel = sample_texture(coord);
307 * As it is now, we always sample the texture, then selectively replace
308 * the texel color results with the border color.
312 /* select texel color or border color depending on use_border */
314 for (chan
= 0; chan
< 4; chan
++) {
315 LLVMValueRef border_chan
=
316 lp_build_const_vec(bld
->texel_type
,
317 bld
->static_state
->border_color
[chan
]);
318 texel_out
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
319 border_chan
, texel_out
[chan
]);
326 * Fetch the texels as <4n x i8> in AoS form.
329 lp_build_sample_packed(struct lp_build_sample_context
*bld
,
332 LLVMValueRef y_stride
,
333 LLVMValueRef data_array
)
335 LLVMValueRef offset
, i
, j
;
336 LLVMValueRef data_ptr
;
339 /* convert x,y,z coords to linear offset from start of texture, in bytes */
340 lp_build_sample_offset(&bld
->uint_coord_bld
,
342 x
, y
, NULL
, y_stride
, NULL
,
345 /* get pointer to mipmap level 0 data */
346 data_ptr
= lp_build_get_const_mipmap_level(bld
, data_array
, 0);
348 if (util_format_is_rgba8_variant(bld
->format_desc
)) {
349 /* Just fetch the data directly without swizzling */
350 assert(bld
->format_desc
->block
.width
== 1);
351 assert(bld
->format_desc
->block
.height
== 1);
352 assert(bld
->format_desc
->block
.bits
<= bld
->texel_type
.width
);
354 res
= lp_build_gather(bld
->builder
,
355 bld
->texel_type
.length
,
356 bld
->format_desc
->block
.bits
,
357 bld
->texel_type
.width
,
363 assert(bld
->texel_type
.width
== 32);
365 memset(&type
, 0, sizeof type
);
367 type
.length
= bld
->texel_type
.length
*4;
370 res
= lp_build_fetch_rgba_aos(bld
->builder
, bld
->format_desc
, type
,
371 data_ptr
, offset
, i
, j
);
379 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
382 lp_build_coord_mirror(struct lp_build_sample_context
*bld
,
385 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
386 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
387 LLVMValueRef fract
, flr
, isOdd
;
389 /* fract = coord - floor(coord) */
390 fract
= lp_build_sub(coord_bld
, coord
, lp_build_floor(coord_bld
, coord
));
392 /* flr = ifloor(coord); */
393 flr
= lp_build_ifloor(coord_bld
, coord
);
395 /* isOdd = flr & 1 */
396 isOdd
= LLVMBuildAnd(bld
->builder
, flr
, int_coord_bld
->one
, "");
398 /* make coord positive or negative depending on isOdd */
399 coord
= lp_build_set_sign(coord_bld
, fract
, isOdd
);
401 /* convert isOdd to float */
402 isOdd
= lp_build_int_to_float(coord_bld
, isOdd
);
404 /* add isOdd to coord */
405 coord
= lp_build_add(coord_bld
, coord
, isOdd
);
412 * We only support a few wrap modes in lp_build_sample_wrap_int() at this time.
413 * Return whether the given mode is supported by that function.
416 is_simple_wrap_mode(unsigned mode
)
419 case PIPE_TEX_WRAP_REPEAT
:
420 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
429 * Build LLVM code for texture wrap mode, for scaled integer texcoords.
430 * \param coord the incoming texcoord (s,t,r or q) scaled to the texture size
431 * \param length the texture size along one dimension
432 * \param is_pot if TRUE, length is a power of two
433 * \param wrap_mode one of PIPE_TEX_WRAP_x
436 lp_build_sample_wrap_int(struct lp_build_sample_context
*bld
,
442 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
443 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
444 LLVMValueRef length_minus_one
;
446 length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
449 case PIPE_TEX_WRAP_REPEAT
:
451 coord
= LLVMBuildAnd(bld
->builder
, coord
, length_minus_one
, "");
453 /* Signed remainder won't give the right results for negative
454 * dividends but unsigned remainder does.*/
455 coord
= LLVMBuildURem(bld
->builder
, coord
, length
, "");
458 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
459 coord
= lp_build_max(int_coord_bld
, coord
, int_coord_bld
->zero
);
460 coord
= lp_build_min(int_coord_bld
, coord
, length_minus_one
);
463 case PIPE_TEX_WRAP_CLAMP
:
464 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
465 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
466 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
467 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
468 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
478 * Build LLVM code for texture wrap mode for linear filtering.
479 * \param x0_out returns first integer texcoord
480 * \param x1_out returns second integer texcoord
481 * \param weight_out returns linear interpolation weight
484 lp_build_sample_wrap_linear(struct lp_build_sample_context
*bld
,
489 LLVMValueRef
*x0_out
,
490 LLVMValueRef
*x1_out
,
491 LLVMValueRef
*weight_out
)
493 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
494 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
495 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
496 LLVMValueRef half
= lp_build_const_vec(coord_bld
->type
, 0.5);
497 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
498 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
499 LLVMValueRef coord0
, coord1
, weight
;
502 case PIPE_TEX_WRAP_REPEAT
:
503 /* mul by size and subtract 0.5 */
504 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
505 coord
= lp_build_sub(coord_bld
, coord
, half
);
507 coord0
= lp_build_ifloor(coord_bld
, coord
);
508 coord1
= lp_build_add(uint_coord_bld
, coord0
, uint_coord_bld
->one
);
509 /* compute lerp weight */
510 weight
= lp_build_fract(coord_bld
, coord
);
513 coord0
= LLVMBuildAnd(bld
->builder
, coord0
, length_minus_one
, "");
514 coord1
= LLVMBuildAnd(bld
->builder
, coord1
, length_minus_one
, "");
517 /* Signed remainder won't give the right results for negative
518 * dividends but unsigned remainder does.*/
519 coord0
= LLVMBuildURem(bld
->builder
, coord0
, length
, "");
520 coord1
= LLVMBuildURem(bld
->builder
, coord1
, length
, "");
524 case PIPE_TEX_WRAP_CLAMP
:
525 if (bld
->static_state
->normalized_coords
) {
526 /* scale coord to length */
527 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
530 /* clamp to [0, length] */
531 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, length_f
);
533 coord
= lp_build_sub(coord_bld
, coord
, half
);
535 weight
= lp_build_fract(coord_bld
, coord
);
536 coord0
= lp_build_ifloor(coord_bld
, coord
);
537 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
540 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
541 if (bld
->static_state
->normalized_coords
) {
543 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, coord_bld
->one
);
544 /* mul by tex size and subtract 0.5 */
545 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
546 coord
= lp_build_sub(coord_bld
, coord
, half
);
549 LLVMValueRef min
, max
;
550 /* clamp to [0.5, length - 0.5] */
552 max
= lp_build_sub(coord_bld
, length_f
, min
);
553 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
555 /* compute lerp weight */
556 weight
= lp_build_fract(coord_bld
, coord
);
557 /* coord0 = floor(coord); */
558 coord0
= lp_build_ifloor(coord_bld
, coord
);
559 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
560 /* coord0 = max(coord0, 0) */
561 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
562 /* coord1 = min(coord1, length-1) */
563 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
566 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
568 LLVMValueRef min
, max
;
569 if (bld
->static_state
->normalized_coords
) {
570 /* scale coord to length */
571 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
573 /* clamp to [-0.5, length + 0.5] */
574 min
= lp_build_const_vec(coord_bld
->type
, -0.5F
);
575 max
= lp_build_sub(coord_bld
, length_f
, min
);
576 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
577 coord
= lp_build_sub(coord_bld
, coord
, half
);
578 /* compute lerp weight */
579 weight
= lp_build_fract(coord_bld
, coord
);
581 coord0
= lp_build_ifloor(coord_bld
, coord
);
582 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
586 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
587 /* compute mirror function */
588 coord
= lp_build_coord_mirror(bld
, coord
);
590 /* scale coord to length */
591 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
592 coord
= lp_build_sub(coord_bld
, coord
, half
);
594 /* compute lerp weight */
595 weight
= lp_build_fract(coord_bld
, coord
);
597 /* convert to int coords */
598 coord0
= lp_build_ifloor(coord_bld
, coord
);
599 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
601 /* coord0 = max(coord0, 0) */
602 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
603 /* coord1 = min(coord1, length-1) */
604 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
607 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
608 coord
= lp_build_abs(coord_bld
, coord
);
610 if (bld
->static_state
->normalized_coords
) {
611 /* scale coord to length */
612 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
615 /* clamp to [0, length] */
616 coord
= lp_build_min(coord_bld
, coord
, length_f
);
618 coord
= lp_build_sub(coord_bld
, coord
, half
);
620 weight
= lp_build_fract(coord_bld
, coord
);
621 coord0
= lp_build_ifloor(coord_bld
, coord
);
622 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
625 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
627 LLVMValueRef min
, max
;
629 coord
= lp_build_abs(coord_bld
, coord
);
631 if (bld
->static_state
->normalized_coords
) {
632 /* scale coord to length */
633 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
636 /* clamp to [0.5, length - 0.5] */
638 max
= lp_build_sub(coord_bld
, length_f
, min
);
639 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
641 coord
= lp_build_sub(coord_bld
, coord
, half
);
643 weight
= lp_build_fract(coord_bld
, coord
);
644 coord0
= lp_build_ifloor(coord_bld
, coord
);
645 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
649 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
651 LLVMValueRef min
, max
;
653 coord
= lp_build_abs(coord_bld
, coord
);
655 if (bld
->static_state
->normalized_coords
) {
656 /* scale coord to length */
657 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
660 /* clamp to [-0.5, length + 0.5] */
661 min
= lp_build_negate(coord_bld
, half
);
662 max
= lp_build_sub(coord_bld
, length_f
, min
);
663 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
665 coord
= lp_build_sub(coord_bld
, coord
, half
);
667 weight
= lp_build_fract(coord_bld
, coord
);
668 coord0
= lp_build_ifloor(coord_bld
, coord
);
669 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
682 *weight_out
= weight
;
687 * Build LLVM code for texture wrap mode for nearest filtering.
688 * \param coord the incoming texcoord (nominally in [0,1])
689 * \param length the texture size along one dimension, as int
690 * \param is_pot if TRUE, length is a power of two
691 * \param wrap_mode one of PIPE_TEX_WRAP_x
694 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
700 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
701 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
702 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
703 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
704 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
708 case PIPE_TEX_WRAP_REPEAT
:
709 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
710 icoord
= lp_build_ifloor(coord_bld
, coord
);
712 icoord
= LLVMBuildAnd(bld
->builder
, icoord
, length_minus_one
, "");
714 /* Signed remainder won't give the right results for negative
715 * dividends but unsigned remainder does.*/
716 icoord
= LLVMBuildURem(bld
->builder
, icoord
, length
, "");
719 case PIPE_TEX_WRAP_CLAMP
:
720 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
721 if (bld
->static_state
->normalized_coords
) {
722 /* scale coord to length */
723 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
727 icoord
= lp_build_ifloor(coord_bld
, coord
);
729 /* clamp to [0, length - 1]. */
730 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
734 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
735 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
737 LLVMValueRef min
, max
;
739 if (bld
->static_state
->normalized_coords
) {
740 /* scale coord to length */
741 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
744 icoord
= lp_build_ifloor(coord_bld
, coord
);
746 /* clamp to [-1, length] */
747 min
= lp_build_negate(int_coord_bld
, int_coord_bld
->one
);
749 icoord
= lp_build_clamp(int_coord_bld
, icoord
, min
, max
);
753 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
754 /* compute mirror function */
755 coord
= lp_build_coord_mirror(bld
, coord
);
757 /* scale coord to length */
758 assert(bld
->static_state
->normalized_coords
);
759 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
761 icoord
= lp_build_ifloor(coord_bld
, coord
);
763 /* clamp to [0, length - 1] */
764 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
767 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
768 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
769 coord
= lp_build_abs(coord_bld
, coord
);
771 if (bld
->static_state
->normalized_coords
) {
772 /* scale coord to length */
773 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
776 icoord
= lp_build_ifloor(coord_bld
, coord
);
778 /* clamp to [0, length - 1] */
779 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
782 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
783 coord
= lp_build_abs(coord_bld
, coord
);
785 if (bld
->static_state
->normalized_coords
) {
786 /* scale coord to length */
787 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
790 icoord
= lp_build_ifloor(coord_bld
, coord
);
792 /* clamp to [0, length] */
793 icoord
= lp_build_min(int_coord_bld
, icoord
, length
);
806 * Codegen equivalent for u_minify().
807 * Return max(1, base_size >> level);
810 lp_build_minify(struct lp_build_sample_context
*bld
,
811 LLVMValueRef base_size
,
814 LLVMValueRef size
= LLVMBuildAShr(bld
->builder
, base_size
, level
, "minify");
815 size
= lp_build_max(&bld
->int_coord_bld
, size
, bld
->int_coord_bld
.one
);
821 * Generate code to compute texture level of detail (lambda).
822 * \param ddx partial derivatives of (s, t, r, q) with respect to X
823 * \param ddy partial derivatives of (s, t, r, q) with respect to Y
824 * \param lod_bias optional float vector with the shader lod bias
825 * \param explicit_lod optional float vector with the explicit lod
826 * \param width scalar int texture width
827 * \param height scalar int texture height
828 * \param depth scalar int texture depth
830 * XXX: The resulting lod is scalar, so ignore all but the first element of
831 * derivatives, lod_bias, etc that are passed by the shader.
834 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
835 const LLVMValueRef ddx
[4],
836 const LLVMValueRef ddy
[4],
837 LLVMValueRef lod_bias
, /* optional */
838 LLVMValueRef explicit_lod
, /* optional */
844 if (bld
->static_state
->min_lod
== bld
->static_state
->max_lod
) {
845 /* User is forcing sampling from a particular mipmap level.
846 * This is hit during mipmap generation.
848 return LLVMConstReal(LLVMFloatType(), bld
->static_state
->min_lod
);
851 struct lp_build_context
*float_bld
= &bld
->float_bld
;
852 LLVMValueRef sampler_lod_bias
= LLVMConstReal(LLVMFloatType(),
853 bld
->static_state
->lod_bias
);
854 LLVMValueRef min_lod
= LLVMConstReal(LLVMFloatType(),
855 bld
->static_state
->min_lod
);
856 LLVMValueRef max_lod
= LLVMConstReal(LLVMFloatType(),
857 bld
->static_state
->max_lod
);
858 LLVMValueRef index0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
862 lod
= LLVMBuildExtractElement(bld
->builder
, explicit_lod
,
866 const int dims
= texture_dims(bld
->static_state
->target
);
867 LLVMValueRef dsdx
, dsdy
;
868 LLVMValueRef dtdx
= NULL
, dtdy
= NULL
, drdx
= NULL
, drdy
= NULL
;
871 dsdx
= LLVMBuildExtractElement(bld
->builder
, ddx
[0], index0
, "dsdx");
872 dsdx
= lp_build_abs(float_bld
, dsdx
);
873 dsdy
= LLVMBuildExtractElement(bld
->builder
, ddy
[0], index0
, "dsdy");
874 dsdy
= lp_build_abs(float_bld
, dsdy
);
876 dtdx
= LLVMBuildExtractElement(bld
->builder
, ddx
[1], index0
, "dtdx");
877 dtdx
= lp_build_abs(float_bld
, dtdx
);
878 dtdy
= LLVMBuildExtractElement(bld
->builder
, ddy
[1], index0
, "dtdy");
879 dtdy
= lp_build_abs(float_bld
, dtdy
);
881 drdx
= LLVMBuildExtractElement(bld
->builder
, ddx
[2], index0
, "drdx");
882 drdx
= lp_build_abs(float_bld
, drdx
);
883 drdy
= LLVMBuildExtractElement(bld
->builder
, ddy
[2], index0
, "drdy");
884 drdy
= lp_build_abs(float_bld
, drdy
);
888 /* Compute rho = max of all partial derivatives scaled by texture size.
889 * XXX this could be vectorized somewhat
891 rho
= LLVMBuildFMul(bld
->builder
,
892 lp_build_max(float_bld
, dsdx
, dsdy
),
893 lp_build_int_to_float(float_bld
, width
), "");
896 max
= LLVMBuildFMul(bld
->builder
,
897 lp_build_max(float_bld
, dtdx
, dtdy
),
898 lp_build_int_to_float(float_bld
, height
), "");
899 rho
= lp_build_max(float_bld
, rho
, max
);
901 max
= LLVMBuildFMul(bld
->builder
,
902 lp_build_max(float_bld
, drdx
, drdy
),
903 lp_build_int_to_float(float_bld
, depth
), "");
904 rho
= lp_build_max(float_bld
, rho
, max
);
908 /* compute lod = log2(rho) */
909 lod
= lp_build_log2(float_bld
, rho
);
911 /* add shader lod bias */
913 lod_bias
= LLVMBuildExtractElement(bld
->builder
, lod_bias
,
915 lod
= LLVMBuildFAdd(bld
->builder
, lod
, lod_bias
, "shader_lod_bias");
919 /* add sampler lod bias */
920 lod
= LLVMBuildFAdd(bld
->builder
, lod
, sampler_lod_bias
, "sampler_lod_bias");
923 lod
= lp_build_clamp(float_bld
, lod
, min_lod
, max_lod
);
931 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
932 * mipmap level index.
933 * Note: this is all scalar code.
934 * \param lod scalar float texture level of detail
935 * \param level_out returns integer
938 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
941 LLVMValueRef
*level_out
)
943 struct lp_build_context
*float_bld
= &bld
->float_bld
;
944 struct lp_build_context
*int_bld
= &bld
->int_bld
;
945 LLVMValueRef last_level
, level
;
947 LLVMValueRef zero
= LLVMConstInt(LLVMInt32Type(), 0, 0);
949 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
952 /* convert float lod to integer */
953 level
= lp_build_iround(float_bld
, lod
);
955 /* clamp level to legal range of levels */
956 *level_out
= lp_build_clamp(int_bld
, level
, zero
, last_level
);
961 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
962 * two (adjacent) mipmap level indexes. Later, we'll sample from those
963 * two mipmap levels and interpolate between them.
966 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
969 LLVMValueRef
*level0_out
,
970 LLVMValueRef
*level1_out
,
971 LLVMValueRef
*weight_out
)
973 struct lp_build_context
*float_bld
= &bld
->float_bld
;
974 struct lp_build_context
*int_bld
= &bld
->int_bld
;
975 LLVMValueRef last_level
, level
;
977 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
980 /* convert float lod to integer */
981 level
= lp_build_ifloor(float_bld
, lod
);
983 /* compute level 0 and clamp to legal range of levels */
984 *level0_out
= lp_build_clamp(int_bld
, level
,
987 /* compute level 1 and clamp to legal range of levels */
988 level
= lp_build_add(int_bld
, level
, int_bld
->one
);
989 *level1_out
= lp_build_clamp(int_bld
, level
,
993 *weight_out
= lp_build_fract(float_bld
, lod
);
998 * Generate code to sample a mipmap level with nearest filtering.
999 * If sampling a cube texture, r = cube face in [0,5].
1002 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
1003 LLVMValueRef width_vec
,
1004 LLVMValueRef height_vec
,
1005 LLVMValueRef depth_vec
,
1006 LLVMValueRef row_stride_vec
,
1007 LLVMValueRef img_stride_vec
,
1008 LLVMValueRef data_ptr
,
1012 LLVMValueRef colors_out
[4])
1014 const int dims
= texture_dims(bld
->static_state
->target
);
1015 LLVMValueRef x
, y
, z
;
1018 * Compute integer texcoords.
1020 x
= lp_build_sample_wrap_nearest(bld
, s
, width_vec
,
1021 bld
->static_state
->pot_width
,
1022 bld
->static_state
->wrap_s
);
1023 lp_build_name(x
, "tex.x.wrapped");
1026 y
= lp_build_sample_wrap_nearest(bld
, t
, height_vec
,
1027 bld
->static_state
->pot_height
,
1028 bld
->static_state
->wrap_t
);
1029 lp_build_name(y
, "tex.y.wrapped");
1032 z
= lp_build_sample_wrap_nearest(bld
, r
, depth_vec
,
1033 bld
->static_state
->pot_height
,
1034 bld
->static_state
->wrap_r
);
1035 lp_build_name(z
, "tex.z.wrapped");
1037 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1049 * Get texture colors.
1051 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1053 row_stride_vec
, img_stride_vec
,
1054 data_ptr
, colors_out
);
1059 * Generate code to sample a mipmap level with linear filtering.
1060 * If sampling a cube texture, r = cube face in [0,5].
1063 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
1064 LLVMValueRef width_vec
,
1065 LLVMValueRef height_vec
,
1066 LLVMValueRef depth_vec
,
1067 LLVMValueRef row_stride_vec
,
1068 LLVMValueRef img_stride_vec
,
1069 LLVMValueRef data_ptr
,
1073 LLVMValueRef colors_out
[4])
1075 const int dims
= texture_dims(bld
->static_state
->target
);
1076 LLVMValueRef x0
, y0
, z0
, x1
, y1
, z1
;
1077 LLVMValueRef s_fpart
, t_fpart
, r_fpart
;
1078 LLVMValueRef neighbors
[2][2][4];
1082 * Compute integer texcoords.
1084 lp_build_sample_wrap_linear(bld
, s
, width_vec
,
1085 bld
->static_state
->pot_width
,
1086 bld
->static_state
->wrap_s
,
1087 &x0
, &x1
, &s_fpart
);
1088 lp_build_name(x0
, "tex.x0.wrapped");
1089 lp_build_name(x1
, "tex.x1.wrapped");
1092 lp_build_sample_wrap_linear(bld
, t
, height_vec
,
1093 bld
->static_state
->pot_height
,
1094 bld
->static_state
->wrap_t
,
1095 &y0
, &y1
, &t_fpart
);
1096 lp_build_name(y0
, "tex.y0.wrapped");
1097 lp_build_name(y1
, "tex.y1.wrapped");
1100 lp_build_sample_wrap_linear(bld
, r
, depth_vec
,
1101 bld
->static_state
->pot_depth
,
1102 bld
->static_state
->wrap_r
,
1103 &z0
, &z1
, &r_fpart
);
1104 lp_build_name(z0
, "tex.z0.wrapped");
1105 lp_build_name(z1
, "tex.z1.wrapped");
1107 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1108 z0
= z1
= r
; /* cube face */
1117 y0
= y1
= t_fpart
= NULL
;
1118 z0
= z1
= r_fpart
= NULL
;
1122 * Get texture colors.
1124 /* get x0/x1 texels */
1125 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1127 row_stride_vec
, img_stride_vec
,
1128 data_ptr
, neighbors
[0][0]);
1129 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1131 row_stride_vec
, img_stride_vec
,
1132 data_ptr
, neighbors
[0][1]);
1135 /* Interpolate two samples from 1D image to produce one color */
1136 for (chan
= 0; chan
< 4; chan
++) {
1137 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, s_fpart
,
1138 neighbors
[0][0][chan
],
1139 neighbors
[0][1][chan
]);
1144 LLVMValueRef colors0
[4];
1146 /* get x0/x1 texels at y1 */
1147 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1149 row_stride_vec
, img_stride_vec
,
1150 data_ptr
, neighbors
[1][0]);
1151 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1153 row_stride_vec
, img_stride_vec
,
1154 data_ptr
, neighbors
[1][1]);
1156 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
1157 for (chan
= 0; chan
< 4; chan
++) {
1158 colors0
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1160 neighbors
[0][0][chan
],
1161 neighbors
[0][1][chan
],
1162 neighbors
[1][0][chan
],
1163 neighbors
[1][1][chan
]);
1167 LLVMValueRef neighbors1
[2][2][4];
1168 LLVMValueRef colors1
[4];
1170 /* get x0/x1/y0/y1 texels at z1 */
1171 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1173 row_stride_vec
, img_stride_vec
,
1174 data_ptr
, neighbors1
[0][0]);
1175 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1177 row_stride_vec
, img_stride_vec
,
1178 data_ptr
, neighbors1
[0][1]);
1179 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1181 row_stride_vec
, img_stride_vec
,
1182 data_ptr
, neighbors1
[1][0]);
1183 lp_build_sample_texel_soa(bld
, width_vec
, height_vec
, depth_vec
,
1185 row_stride_vec
, img_stride_vec
,
1186 data_ptr
, neighbors1
[1][1]);
1188 /* Bilinear interpolate the four samples from the second Z slice */
1189 for (chan
= 0; chan
< 4; chan
++) {
1190 colors1
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
1192 neighbors1
[0][0][chan
],
1193 neighbors1
[0][1][chan
],
1194 neighbors1
[1][0][chan
],
1195 neighbors1
[1][1][chan
]);
1198 /* Linearly interpolate the two samples from the two 3D slices */
1199 for (chan
= 0; chan
< 4; chan
++) {
1200 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
,
1202 colors0
[chan
], colors1
[chan
]);
1207 for (chan
= 0; chan
< 4; chan
++) {
1208 colors_out
[chan
] = colors0
[chan
];
1215 /** Helper used by lp_build_cube_lookup() */
1217 lp_build_cube_ima(struct lp_build_context
*coord_bld
, LLVMValueRef coord
)
1219 /* ima = -0.5 / abs(coord); */
1220 LLVMValueRef negHalf
= lp_build_const_vec(coord_bld
->type
, -0.5);
1221 LLVMValueRef absCoord
= lp_build_abs(coord_bld
, coord
);
1222 LLVMValueRef ima
= lp_build_mul(coord_bld
, negHalf
,
1223 lp_build_rcp(coord_bld
, absCoord
));
1229 * Helper used by lp_build_cube_lookup()
1230 * \param sign scalar +1 or -1
1231 * \param coord float vector
1232 * \param ima float vector
1235 lp_build_cube_coord(struct lp_build_context
*coord_bld
,
1236 LLVMValueRef sign
, int negate_coord
,
1237 LLVMValueRef coord
, LLVMValueRef ima
)
1239 /* return negate(coord) * ima * sign + 0.5; */
1240 LLVMValueRef half
= lp_build_const_vec(coord_bld
->type
, 0.5);
1243 assert(negate_coord
== +1 || negate_coord
== -1);
1245 if (negate_coord
== -1) {
1246 coord
= lp_build_negate(coord_bld
, coord
);
1249 res
= lp_build_mul(coord_bld
, coord
, ima
);
1251 sign
= lp_build_broadcast_scalar(coord_bld
, sign
);
1252 res
= lp_build_mul(coord_bld
, res
, sign
);
1254 res
= lp_build_add(coord_bld
, res
, half
);
1260 /** Helper used by lp_build_cube_lookup()
1261 * Return (major_coord >= 0) ? pos_face : neg_face;
1264 lp_build_cube_face(struct lp_build_sample_context
*bld
,
1265 LLVMValueRef major_coord
,
1266 unsigned pos_face
, unsigned neg_face
)
1268 LLVMValueRef cmp
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
1270 bld
->float_bld
.zero
, "");
1271 LLVMValueRef pos
= LLVMConstInt(LLVMInt32Type(), pos_face
, 0);
1272 LLVMValueRef neg
= LLVMConstInt(LLVMInt32Type(), neg_face
, 0);
1273 LLVMValueRef res
= LLVMBuildSelect(bld
->builder
, cmp
, pos
, neg
, "");
1280 * Generate code to do cube face selection and compute per-face texcoords.
1283 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
1288 LLVMValueRef
*face_s
,
1289 LLVMValueRef
*face_t
)
1291 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1292 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
1293 LLVMValueRef rx
, ry
, rz
;
1294 LLVMValueRef arx
, ary
, arz
;
1295 LLVMValueRef c25
= LLVMConstReal(LLVMFloatType(), 0.25);
1296 LLVMValueRef arx_ge_ary
, arx_ge_arz
;
1297 LLVMValueRef ary_ge_arx
, ary_ge_arz
;
1298 LLVMValueRef arx_ge_ary_arz
, ary_ge_arx_arz
;
1299 LLVMValueRef rx_pos
, ry_pos
, rz_pos
;
1301 assert(bld
->coord_bld
.type
.length
== 4);
1304 * Use the average of the four pixel's texcoords to choose the face.
1306 rx
= lp_build_mul(float_bld
, c25
,
1307 lp_build_sum_vector(&bld
->coord_bld
, s
));
1308 ry
= lp_build_mul(float_bld
, c25
,
1309 lp_build_sum_vector(&bld
->coord_bld
, t
));
1310 rz
= lp_build_mul(float_bld
, c25
,
1311 lp_build_sum_vector(&bld
->coord_bld
, r
));
1313 arx
= lp_build_abs(float_bld
, rx
);
1314 ary
= lp_build_abs(float_bld
, ry
);
1315 arz
= lp_build_abs(float_bld
, rz
);
1318 * Compare sign/magnitude of rx,ry,rz to determine face
1320 arx_ge_ary
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, arx
, ary
, "");
1321 arx_ge_arz
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, arx
, arz
, "");
1322 ary_ge_arx
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ary
, arx
, "");
1323 ary_ge_arz
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ary
, arz
, "");
1325 arx_ge_ary_arz
= LLVMBuildAnd(bld
->builder
, arx_ge_ary
, arx_ge_arz
, "");
1326 ary_ge_arx_arz
= LLVMBuildAnd(bld
->builder
, ary_ge_arx
, ary_ge_arz
, "");
1328 rx_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, rx
, float_bld
->zero
, "");
1329 ry_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ry
, float_bld
->zero
, "");
1330 rz_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, rz
, float_bld
->zero
, "");
1333 struct lp_build_flow_context
*flow_ctx
;
1334 struct lp_build_if_state if_ctx
;
1336 flow_ctx
= lp_build_flow_create(bld
->builder
);
1337 lp_build_flow_scope_begin(flow_ctx
);
1339 *face_s
= bld
->coord_bld
.undef
;
1340 *face_t
= bld
->coord_bld
.undef
;
1341 *face
= bld
->int_bld
.undef
;
1343 lp_build_name(*face_s
, "face_s");
1344 lp_build_name(*face_t
, "face_t");
1345 lp_build_name(*face
, "face");
1347 lp_build_flow_scope_declare(flow_ctx
, face_s
);
1348 lp_build_flow_scope_declare(flow_ctx
, face_t
);
1349 lp_build_flow_scope_declare(flow_ctx
, face
);
1351 lp_build_if(&if_ctx
, flow_ctx
, bld
->builder
, arx_ge_ary_arz
);
1354 LLVMValueRef sign
= lp_build_sgn(float_bld
, rx
);
1355 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, s
);
1356 *face_s
= lp_build_cube_coord(coord_bld
, sign
, +1, r
, ima
);
1357 *face_t
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1358 *face
= lp_build_cube_face(bld
, rx
,
1359 PIPE_TEX_FACE_POS_X
,
1360 PIPE_TEX_FACE_NEG_X
);
1362 lp_build_else(&if_ctx
);
1364 struct lp_build_flow_context
*flow_ctx2
;
1365 struct lp_build_if_state if_ctx2
;
1367 LLVMValueRef face_s2
= bld
->coord_bld
.undef
;
1368 LLVMValueRef face_t2
= bld
->coord_bld
.undef
;
1369 LLVMValueRef face2
= bld
->int_bld
.undef
;
1371 flow_ctx2
= lp_build_flow_create(bld
->builder
);
1372 lp_build_flow_scope_begin(flow_ctx2
);
1373 lp_build_flow_scope_declare(flow_ctx2
, &face_s2
);
1374 lp_build_flow_scope_declare(flow_ctx2
, &face_t2
);
1375 lp_build_flow_scope_declare(flow_ctx2
, &face2
);
1377 ary_ge_arx_arz
= LLVMBuildAnd(bld
->builder
, ary_ge_arx
, ary_ge_arz
, "");
1379 lp_build_if(&if_ctx2
, flow_ctx2
, bld
->builder
, ary_ge_arx_arz
);
1382 LLVMValueRef sign
= lp_build_sgn(float_bld
, ry
);
1383 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, t
);
1384 face_s2
= lp_build_cube_coord(coord_bld
, NULL
, -1, s
, ima
);
1385 face_t2
= lp_build_cube_coord(coord_bld
, sign
, -1, r
, ima
);
1386 face2
= lp_build_cube_face(bld
, ry
,
1387 PIPE_TEX_FACE_POS_Y
,
1388 PIPE_TEX_FACE_NEG_Y
);
1390 lp_build_else(&if_ctx2
);
1393 LLVMValueRef sign
= lp_build_sgn(float_bld
, rz
);
1394 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, r
);
1395 face_s2
= lp_build_cube_coord(coord_bld
, sign
, -1, s
, ima
);
1396 face_t2
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1397 face2
= lp_build_cube_face(bld
, rz
,
1398 PIPE_TEX_FACE_POS_Z
,
1399 PIPE_TEX_FACE_NEG_Z
);
1401 lp_build_endif(&if_ctx2
);
1402 lp_build_flow_scope_end(flow_ctx2
);
1403 lp_build_flow_destroy(flow_ctx2
);
1409 lp_build_endif(&if_ctx
);
1410 lp_build_flow_scope_end(flow_ctx
);
1411 lp_build_flow_destroy(flow_ctx
);
1418 * Sample the texture/mipmap using given image filter and mip filter.
1419 * data0_ptr and data1_ptr point to the two mipmap levels to sample
1420 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
1421 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1424 lp_build_sample_mipmap(struct lp_build_sample_context
*bld
,
1425 unsigned img_filter
,
1426 unsigned mip_filter
,
1430 LLVMValueRef lod_fpart
,
1431 LLVMValueRef width0_vec
,
1432 LLVMValueRef width1_vec
,
1433 LLVMValueRef height0_vec
,
1434 LLVMValueRef height1_vec
,
1435 LLVMValueRef depth0_vec
,
1436 LLVMValueRef depth1_vec
,
1437 LLVMValueRef row_stride0_vec
,
1438 LLVMValueRef row_stride1_vec
,
1439 LLVMValueRef img_stride0_vec
,
1440 LLVMValueRef img_stride1_vec
,
1441 LLVMValueRef data_ptr0
,
1442 LLVMValueRef data_ptr1
,
1443 LLVMValueRef
*colors_out
)
1445 LLVMValueRef colors0
[4], colors1
[4];
1448 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
1449 /* sample the first mipmap level */
1450 lp_build_sample_image_nearest(bld
,
1451 width0_vec
, height0_vec
, depth0_vec
,
1452 row_stride0_vec
, img_stride0_vec
,
1453 data_ptr0
, s
, t
, r
, colors0
);
1455 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1456 /* sample the second mipmap level */
1457 lp_build_sample_image_nearest(bld
,
1458 width1_vec
, height1_vec
, depth1_vec
,
1459 row_stride1_vec
, img_stride1_vec
,
1460 data_ptr1
, s
, t
, r
, colors1
);
1464 assert(img_filter
== PIPE_TEX_FILTER_LINEAR
);
1466 /* sample the first mipmap level */
1467 lp_build_sample_image_linear(bld
,
1468 width0_vec
, height0_vec
, depth0_vec
,
1469 row_stride0_vec
, img_stride0_vec
,
1470 data_ptr0
, s
, t
, r
, colors0
);
1472 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1473 /* sample the second mipmap level */
1474 lp_build_sample_image_linear(bld
,
1475 width1_vec
, height1_vec
, depth1_vec
,
1476 row_stride1_vec
, img_stride1_vec
,
1477 data_ptr1
, s
, t
, r
, colors1
);
1481 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1482 /* interpolate samples from the two mipmap levels */
1483 for (chan
= 0; chan
< 4; chan
++) {
1484 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
1485 colors0
[chan
], colors1
[chan
]);
1489 /* use first/only level's colors */
1490 for (chan
= 0; chan
< 4; chan
++) {
1491 colors_out
[chan
] = colors0
[chan
];
1499 * General texture sampling codegen.
1500 * This function handles texture sampling for all texture targets (1D,
1501 * 2D, 3D, cube) and all filtering modes.
1504 lp_build_sample_general(struct lp_build_sample_context
*bld
,
1509 const LLVMValueRef
*ddx
,
1510 const LLVMValueRef
*ddy
,
1511 LLVMValueRef lod_bias
, /* optional */
1512 LLVMValueRef explicit_lod
, /* optional */
1514 LLVMValueRef height
,
1516 LLVMValueRef width_vec
,
1517 LLVMValueRef height_vec
,
1518 LLVMValueRef depth_vec
,
1519 LLVMValueRef row_stride_array
,
1520 LLVMValueRef img_stride_array
,
1521 LLVMValueRef data_array
,
1522 LLVMValueRef
*colors_out
)
1524 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1525 const unsigned mip_filter
= bld
->static_state
->min_mip_filter
;
1526 const unsigned min_filter
= bld
->static_state
->min_img_filter
;
1527 const unsigned mag_filter
= bld
->static_state
->mag_img_filter
;
1528 const int dims
= texture_dims(bld
->static_state
->target
);
1529 LLVMValueRef lod
= NULL
, lod_fpart
= NULL
;
1530 LLVMValueRef ilevel0
, ilevel1
= NULL
, ilevel0_vec
, ilevel1_vec
= NULL
;
1531 LLVMValueRef width0_vec
= NULL
, height0_vec
= NULL
, depth0_vec
= NULL
;
1532 LLVMValueRef width1_vec
= NULL
, height1_vec
= NULL
, depth1_vec
= NULL
;
1533 LLVMValueRef row_stride0_vec
= NULL
, row_stride1_vec
= NULL
;
1534 LLVMValueRef img_stride0_vec
= NULL
, img_stride1_vec
= NULL
;
1535 LLVMValueRef data_ptr0
, data_ptr1
= NULL
;
1536 LLVMValueRef face_ddx
[4], face_ddy
[4];
1539 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1540 mip_filter, min_filter, mag_filter);
1544 * Choose cube face, recompute texcoords and derivatives for the chosen face.
1546 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1547 LLVMValueRef face
, face_s
, face_t
;
1548 lp_build_cube_lookup(bld
, s
, t
, r
, &face
, &face_s
, &face_t
);
1549 s
= face_s
; /* vec */
1550 t
= face_t
; /* vec */
1551 /* use 'r' to indicate cube face */
1552 r
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, face
); /* vec */
1554 /* recompute ddx, ddy using the new (s,t) face texcoords */
1555 face_ddx
[0] = lp_build_ddx(&bld
->coord_bld
, s
);
1556 face_ddx
[1] = lp_build_ddx(&bld
->coord_bld
, t
);
1559 face_ddy
[0] = lp_build_ddy(&bld
->coord_bld
, s
);
1560 face_ddy
[1] = lp_build_ddy(&bld
->coord_bld
, t
);
1568 * Compute the level of detail (float).
1570 if (min_filter
!= mag_filter
||
1571 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
1572 /* Need to compute lod either to choose mipmap levels or to
1573 * distinguish between minification/magnification with one mipmap level.
1575 lod
= lp_build_lod_selector(bld
, ddx
, ddy
,
1576 lod_bias
, explicit_lod
,
1577 width
, height
, depth
);
1581 * Compute integer mipmap level(s) to fetch texels from.
1583 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
1584 /* always use mip level 0 */
1585 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1586 /* XXX this is a work-around for an apparent bug in LLVM 2.7.
1587 * We should be able to set ilevel0 = const(0) but that causes
1588 * bad x86 code to be emitted.
1590 lod
= lp_build_const_elem(bld
->coord_bld
.type
, 0.0);
1591 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
1594 ilevel0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
1599 if (mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
1600 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
1603 assert(mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
);
1604 lp_build_linear_mip_levels(bld
, unit
, lod
, &ilevel0
, &ilevel1
,
1606 lod_fpart
= lp_build_broadcast_scalar(&bld
->coord_bld
, lod_fpart
);
1611 * Convert scalar integer mipmap levels into vectors.
1613 ilevel0_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel0
);
1614 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
)
1615 ilevel1_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel1
);
1618 * Compute width, height at mipmap level 'ilevel0'
1620 width0_vec
= lp_build_minify(bld
, width_vec
, ilevel0_vec
);
1622 height0_vec
= lp_build_minify(bld
, height_vec
, ilevel0_vec
);
1623 row_stride0_vec
= lp_build_get_level_stride_vec(bld
, row_stride_array
,
1625 if (dims
== 3 || bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1626 img_stride0_vec
= lp_build_get_level_stride_vec(bld
,
1630 depth0_vec
= lp_build_minify(bld
, depth_vec
, ilevel0_vec
);
1634 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1635 /* compute width, height, depth for second mipmap level at 'ilevel1' */
1636 width1_vec
= lp_build_minify(bld
, width_vec
, ilevel1_vec
);
1638 height1_vec
= lp_build_minify(bld
, height_vec
, ilevel1_vec
);
1639 row_stride1_vec
= lp_build_get_level_stride_vec(bld
, row_stride_array
,
1641 if (dims
== 3 || bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1642 img_stride1_vec
= lp_build_get_level_stride_vec(bld
,
1646 depth1_vec
= lp_build_minify(bld
, depth_vec
, ilevel1_vec
);
1653 * Get pointer(s) to image data for mipmap level(s).
1655 data_ptr0
= lp_build_get_mipmap_level(bld
, data_array
, ilevel0
);
1656 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1657 data_ptr1
= lp_build_get_mipmap_level(bld
, data_array
, ilevel1
);
1661 * Get/interpolate texture colors.
1663 if (min_filter
== mag_filter
) {
1664 /* no need to distinquish between minification and magnification */
1665 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
, s
, t
, r
, lod_fpart
,
1666 width0_vec
, width1_vec
,
1667 height0_vec
, height1_vec
,
1668 depth0_vec
, depth1_vec
,
1669 row_stride0_vec
, row_stride1_vec
,
1670 img_stride0_vec
, img_stride1_vec
,
1671 data_ptr0
, data_ptr1
,
1675 /* Emit conditional to choose min image filter or mag image filter
1676 * depending on the lod being >0 or <= 0, respectively.
1678 struct lp_build_flow_context
*flow_ctx
;
1679 struct lp_build_if_state if_ctx
;
1680 LLVMValueRef minify
;
1682 flow_ctx
= lp_build_flow_create(bld
->builder
);
1683 lp_build_flow_scope_begin(flow_ctx
);
1685 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[0]);
1686 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[1]);
1687 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[2]);
1688 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[3]);
1690 /* minify = lod > 0.0 */
1691 minify
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
1692 lod
, float_bld
->zero
, "");
1694 lp_build_if(&if_ctx
, flow_ctx
, bld
->builder
, minify
);
1696 /* Use the minification filter */
1697 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
,
1699 width0_vec
, width1_vec
,
1700 height0_vec
, height1_vec
,
1701 depth0_vec
, depth1_vec
,
1702 row_stride0_vec
, row_stride1_vec
,
1703 img_stride0_vec
, img_stride1_vec
,
1704 data_ptr0
, data_ptr1
,
1707 lp_build_else(&if_ctx
);
1709 /* Use the magnification filter */
1710 lp_build_sample_mipmap(bld
, mag_filter
, mip_filter
,
1712 width0_vec
, width1_vec
,
1713 height0_vec
, height1_vec
,
1714 depth0_vec
, depth1_vec
,
1715 row_stride0_vec
, row_stride1_vec
,
1716 img_stride0_vec
, img_stride1_vec
,
1717 data_ptr0
, data_ptr1
,
1720 lp_build_endif(&if_ctx
);
1722 lp_build_flow_scope_end(flow_ctx
);
1723 lp_build_flow_destroy(flow_ctx
);
1730 lp_build_sample_2d_linear_aos(struct lp_build_sample_context
*bld
,
1734 LLVMValueRef height
,
1735 LLVMValueRef stride_array
,
1736 LLVMValueRef data_array
,
1737 LLVMValueRef texel_out
[4])
1739 LLVMBuilderRef builder
= bld
->builder
;
1740 struct lp_build_context i32
, h16
, u8n
;
1741 LLVMTypeRef i32_vec_type
, h16_vec_type
, u8n_vec_type
;
1742 LLVMValueRef i32_c8
, i32_c128
, i32_c255
;
1743 LLVMValueRef s_ipart
, s_fpart
, s_fpart_lo
, s_fpart_hi
;
1744 LLVMValueRef t_ipart
, t_fpart
, t_fpart_lo
, t_fpart_hi
;
1745 LLVMValueRef x0
, x1
;
1746 LLVMValueRef y0
, y1
;
1747 LLVMValueRef neighbors
[2][2];
1748 LLVMValueRef neighbors_lo
[2][2];
1749 LLVMValueRef neighbors_hi
[2][2];
1750 LLVMValueRef packed
, packed_lo
, packed_hi
;
1751 LLVMValueRef unswizzled
[4];
1752 LLVMValueRef stride
;
1754 assert(bld
->static_state
->target
== PIPE_TEXTURE_2D
);
1755 assert(bld
->static_state
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
);
1756 assert(bld
->static_state
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
);
1757 assert(bld
->static_state
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
);
1759 lp_build_context_init(&i32
, builder
, lp_type_int_vec(32));
1760 lp_build_context_init(&h16
, builder
, lp_type_ufixed(16));
1761 lp_build_context_init(&u8n
, builder
, lp_type_unorm(8));
1763 i32_vec_type
= lp_build_vec_type(i32
.type
);
1764 h16_vec_type
= lp_build_vec_type(h16
.type
);
1765 u8n_vec_type
= lp_build_vec_type(u8n
.type
);
1767 if (bld
->static_state
->normalized_coords
) {
1768 LLVMTypeRef coord_vec_type
= lp_build_vec_type(bld
->coord_type
);
1769 LLVMValueRef fp_width
= LLVMBuildSIToFP(bld
->builder
, width
, coord_vec_type
, "");
1770 LLVMValueRef fp_height
= LLVMBuildSIToFP(bld
->builder
, height
, coord_vec_type
, "");
1771 s
= lp_build_mul(&bld
->coord_bld
, s
, fp_width
);
1772 t
= lp_build_mul(&bld
->coord_bld
, t
, fp_height
);
1775 /* scale coords by 256 (8 fractional bits) */
1776 s
= lp_build_mul_imm(&bld
->coord_bld
, s
, 256);
1777 t
= lp_build_mul_imm(&bld
->coord_bld
, t
, 256);
1779 /* convert float to int */
1780 s
= LLVMBuildFPToSI(builder
, s
, i32_vec_type
, "");
1781 t
= LLVMBuildFPToSI(builder
, t
, i32_vec_type
, "");
1783 /* subtract 0.5 (add -128) */
1784 i32_c128
= lp_build_const_int_vec(i32
.type
, -128);
1785 s
= LLVMBuildAdd(builder
, s
, i32_c128
, "");
1786 t
= LLVMBuildAdd(builder
, t
, i32_c128
, "");
1788 /* compute floor (shift right 8) */
1789 i32_c8
= lp_build_const_int_vec(i32
.type
, 8);
1790 s_ipart
= LLVMBuildAShr(builder
, s
, i32_c8
, "");
1791 t_ipart
= LLVMBuildAShr(builder
, t
, i32_c8
, "");
1793 /* compute fractional part (AND with 0xff) */
1794 i32_c255
= lp_build_const_int_vec(i32
.type
, 255);
1795 s_fpart
= LLVMBuildAnd(builder
, s
, i32_c255
, "");
1796 t_fpart
= LLVMBuildAnd(builder
, t
, i32_c255
, "");
1801 x1
= lp_build_add(&bld
->int_coord_bld
, x0
, bld
->int_coord_bld
.one
);
1802 y1
= lp_build_add(&bld
->int_coord_bld
, y0
, bld
->int_coord_bld
.one
);
1804 x0
= lp_build_sample_wrap_int(bld
, x0
, width
, bld
->static_state
->pot_width
,
1805 bld
->static_state
->wrap_s
);
1806 y0
= lp_build_sample_wrap_int(bld
, y0
, height
, bld
->static_state
->pot_height
,
1807 bld
->static_state
->wrap_t
);
1809 x1
= lp_build_sample_wrap_int(bld
, x1
, width
, bld
->static_state
->pot_width
,
1810 bld
->static_state
->wrap_s
);
1811 y1
= lp_build_sample_wrap_int(bld
, y1
, height
, bld
->static_state
->pot_height
,
1812 bld
->static_state
->wrap_t
);
1815 * Transform 4 x i32 in
1817 * s_fpart = {s0, s1, s2, s3}
1821 * s_fpart = {00, s0, 00, s1, 00, s2, 00, s3}
1825 * s_fpart_lo = {s0, s0, s0, s0, s1, s1, s1, s1}
1826 * s_fpart_hi = {s2, s2, s2, s2, s3, s3, s3, s3}
1828 * and likewise for t_fpart. There is no risk of loosing precision here
1829 * since the fractional parts only use the lower 8bits.
1832 s_fpart
= LLVMBuildBitCast(builder
, s_fpart
, h16_vec_type
, "");
1833 t_fpart
= LLVMBuildBitCast(builder
, t_fpart
, h16_vec_type
, "");
1836 LLVMTypeRef elem_type
= LLVMInt32Type();
1837 LLVMValueRef shuffles_lo
[LP_MAX_VECTOR_LENGTH
];
1838 LLVMValueRef shuffles_hi
[LP_MAX_VECTOR_LENGTH
];
1839 LLVMValueRef shuffle_lo
;
1840 LLVMValueRef shuffle_hi
;
1843 for(j
= 0; j
< h16
.type
.length
; j
+= 4) {
1844 unsigned subindex
= util_cpu_caps
.little_endian
? 0 : 1;
1847 index
= LLVMConstInt(elem_type
, j
/2 + subindex
, 0);
1848 for(i
= 0; i
< 4; ++i
)
1849 shuffles_lo
[j
+ i
] = index
;
1851 index
= LLVMConstInt(elem_type
, h16
.type
.length
/2 + j
/2 + subindex
, 0);
1852 for(i
= 0; i
< 4; ++i
)
1853 shuffles_hi
[j
+ i
] = index
;
1856 shuffle_lo
= LLVMConstVector(shuffles_lo
, h16
.type
.length
);
1857 shuffle_hi
= LLVMConstVector(shuffles_hi
, h16
.type
.length
);
1859 s_fpart_lo
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_lo
, "");
1860 t_fpart_lo
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_lo
, "");
1861 s_fpart_hi
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_hi
, "");
1862 t_fpart_hi
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_hi
, "");
1865 stride
= lp_build_get_const_level_stride_vec(bld
, stride_array
, 0);
1868 * Fetch the pixels as 4 x 32bit (rgba order might differ):
1870 * rgba0 rgba1 rgba2 rgba3
1872 * bit cast them into 16 x u8
1874 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
1876 * unpack them into two 8 x i16:
1878 * r0 g0 b0 a0 r1 g1 b1 a1
1879 * r2 g2 b2 a2 r3 g3 b3 a3
1881 * The higher 8 bits of the resulting elements will be zero.
1884 neighbors
[0][0] = lp_build_sample_packed(bld
, x0
, y0
, stride
, data_array
);
1885 neighbors
[0][1] = lp_build_sample_packed(bld
, x1
, y0
, stride
, data_array
);
1886 neighbors
[1][0] = lp_build_sample_packed(bld
, x0
, y1
, stride
, data_array
);
1887 neighbors
[1][1] = lp_build_sample_packed(bld
, x1
, y1
, stride
, data_array
);
1889 neighbors
[0][0] = LLVMBuildBitCast(builder
, neighbors
[0][0], u8n_vec_type
, "");
1890 neighbors
[0][1] = LLVMBuildBitCast(builder
, neighbors
[0][1], u8n_vec_type
, "");
1891 neighbors
[1][0] = LLVMBuildBitCast(builder
, neighbors
[1][0], u8n_vec_type
, "");
1892 neighbors
[1][1] = LLVMBuildBitCast(builder
, neighbors
[1][1], u8n_vec_type
, "");
1894 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][0], &neighbors_lo
[0][0], &neighbors_hi
[0][0]);
1895 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][1], &neighbors_lo
[0][1], &neighbors_hi
[0][1]);
1896 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][0], &neighbors_lo
[1][0], &neighbors_hi
[1][0]);
1897 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][1], &neighbors_lo
[1][1], &neighbors_hi
[1][1]);
1900 * Linear interpolate with 8.8 fixed point.
1903 packed_lo
= lp_build_lerp_2d(&h16
,
1904 s_fpart_lo
, t_fpart_lo
,
1908 neighbors_lo
[1][1]);
1910 packed_hi
= lp_build_lerp_2d(&h16
,
1911 s_fpart_hi
, t_fpart_hi
,
1915 neighbors_hi
[1][1]);
1917 packed
= lp_build_pack2(builder
, h16
.type
, u8n
.type
, packed_lo
, packed_hi
);
1920 * Convert to SoA and swizzle.
1923 lp_build_rgba8_to_f32_soa(bld
->builder
,
1925 packed
, unswizzled
);
1927 if (util_format_is_rgba8_variant(bld
->format_desc
)) {
1928 lp_build_format_swizzle_soa(bld
->format_desc
,
1930 unswizzled
, texel_out
);
1932 texel_out
[0] = unswizzled
[0];
1933 texel_out
[1] = unswizzled
[1];
1934 texel_out
[2] = unswizzled
[2];
1935 texel_out
[3] = unswizzled
[3];
1938 apply_sampler_swizzle(bld
, texel_out
);
1943 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
1945 LLVMValueRef texel
[4])
1947 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1951 if(bld
->static_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
1954 /* TODO: Compare before swizzling, to avoid redundant computations */
1956 for(chan
= 0; chan
< 4; ++chan
) {
1958 cmp
= lp_build_cmp(texel_bld
, bld
->static_state
->compare_func
, p
, texel
[chan
]);
1959 cmp
= lp_build_select(texel_bld
, cmp
, texel_bld
->one
, texel_bld
->zero
);
1962 res
= lp_build_add(texel_bld
, res
, cmp
);
1968 res
= lp_build_mul(texel_bld
, res
, lp_build_const_vec(texel_bld
->type
, 0.25));
1970 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1971 for(chan
= 0; chan
< 3; ++chan
)
1973 texel
[3] = texel_bld
->one
;
1978 * Just set texels to white instead of actually sampling the texture.
1982 lp_build_sample_nop(struct lp_build_sample_context
*bld
,
1983 LLVMValueRef texel_out
[4])
1985 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1988 for (chan
= 0; chan
< 4; chan
++) {
1989 /*lp_bld_mov(texel_bld, texel, texel_bld->one);*/
1990 texel_out
[chan
] = texel_bld
->one
;
1996 * Build texture sampling code.
1997 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1999 * \param type vector float type to use for coords, etc.
2000 * \param ddx partial derivatives of (s,t,r,q) with respect to x
2001 * \param ddy partial derivatives of (s,t,r,q) with respect to y
2004 lp_build_sample_soa(LLVMBuilderRef builder
,
2005 const struct lp_sampler_static_state
*static_state
,
2006 struct lp_sampler_dynamic_state
*dynamic_state
,
2007 struct lp_type type
,
2009 unsigned num_coords
,
2010 const LLVMValueRef
*coords
,
2011 const LLVMValueRef ddx
[4],
2012 const LLVMValueRef ddy
[4],
2013 LLVMValueRef lod_bias
, /* optional */
2014 LLVMValueRef explicit_lod
, /* optional */
2015 LLVMValueRef texel_out
[4])
2017 struct lp_build_sample_context bld
;
2018 LLVMValueRef width
, width_vec
;
2019 LLVMValueRef height
, height_vec
;
2020 LLVMValueRef depth
, depth_vec
;
2021 LLVMValueRef row_stride_array
, img_stride_array
;
2022 LLVMValueRef data_array
;
2028 enum pipe_format fmt
= static_state
->format
;
2029 debug_printf("Sample from %s\n", util_format_name(fmt
));
2032 assert(type
.floating
);
2034 /* Setup our build context */
2035 memset(&bld
, 0, sizeof bld
);
2036 bld
.builder
= builder
;
2037 bld
.static_state
= static_state
;
2038 bld
.dynamic_state
= dynamic_state
;
2039 bld
.format_desc
= util_format_description(static_state
->format
);
2041 bld
.float_type
= lp_type_float(32);
2042 bld
.int_type
= lp_type_int(32);
2043 bld
.coord_type
= type
;
2044 bld
.uint_coord_type
= lp_uint_type(type
);
2045 bld
.int_coord_type
= lp_int_type(type
);
2046 bld
.texel_type
= type
;
2048 lp_build_context_init(&bld
.float_bld
, builder
, bld
.float_type
);
2049 lp_build_context_init(&bld
.int_bld
, builder
, bld
.int_type
);
2050 lp_build_context_init(&bld
.coord_bld
, builder
, bld
.coord_type
);
2051 lp_build_context_init(&bld
.uint_coord_bld
, builder
, bld
.uint_coord_type
);
2052 lp_build_context_init(&bld
.int_coord_bld
, builder
, bld
.int_coord_type
);
2053 lp_build_context_init(&bld
.texel_bld
, builder
, bld
.texel_type
);
2055 /* Get the dynamic state */
2056 width
= dynamic_state
->width(dynamic_state
, builder
, unit
);
2057 height
= dynamic_state
->height(dynamic_state
, builder
, unit
);
2058 depth
= dynamic_state
->depth(dynamic_state
, builder
, unit
);
2059 row_stride_array
= dynamic_state
->row_stride(dynamic_state
, builder
, unit
);
2060 img_stride_array
= dynamic_state
->img_stride(dynamic_state
, builder
, unit
);
2061 data_array
= dynamic_state
->data_ptr(dynamic_state
, builder
, unit
);
2062 /* Note that data_array is an array[level] of pointers to texture images */
2068 width_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, width
);
2069 height_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, height
);
2070 depth_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, depth
);
2073 /* For debug: no-op texture sampling */
2074 lp_build_sample_nop(&bld
, texel_out
);
2076 else if (util_format_fits_8unorm(bld
.format_desc
) &&
2077 bld
.format_desc
->nr_channels
> 1 &&
2078 static_state
->target
== PIPE_TEXTURE_2D
&&
2079 static_state
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
2080 static_state
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
2081 static_state
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
&&
2082 is_simple_wrap_mode(static_state
->wrap_s
) &&
2083 is_simple_wrap_mode(static_state
->wrap_t
)) {
2085 lp_build_sample_2d_linear_aos(&bld
, s
, t
, width_vec
, height_vec
,
2086 row_stride_array
, data_array
, texel_out
);
2089 lp_build_sample_general(&bld
, unit
, s
, t
, r
, ddx
, ddy
,
2090 lod_bias
, explicit_lod
,
2091 width
, height
, depth
,
2092 width_vec
, height_vec
, depth_vec
,
2093 row_stride_array
, img_stride_array
,
2098 lp_build_sample_compare(&bld
, r
, texel_out
);