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
= LLVMBuildLShr(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_div(coord_bld
, negHalf
, absCoord
);
1228 * Helper used by lp_build_cube_lookup()
1229 * \param sign scalar +1 or -1
1230 * \param coord float vector
1231 * \param ima float vector
1234 lp_build_cube_coord(struct lp_build_context
*coord_bld
,
1235 LLVMValueRef sign
, int negate_coord
,
1236 LLVMValueRef coord
, LLVMValueRef ima
)
1238 /* return negate(coord) * ima * sign + 0.5; */
1239 LLVMValueRef half
= lp_build_const_vec(coord_bld
->type
, 0.5);
1242 assert(negate_coord
== +1 || negate_coord
== -1);
1244 if (negate_coord
== -1) {
1245 coord
= lp_build_negate(coord_bld
, coord
);
1248 res
= lp_build_mul(coord_bld
, coord
, ima
);
1250 sign
= lp_build_broadcast_scalar(coord_bld
, sign
);
1251 res
= lp_build_mul(coord_bld
, res
, sign
);
1253 res
= lp_build_add(coord_bld
, res
, half
);
1259 /** Helper used by lp_build_cube_lookup()
1260 * Return (major_coord >= 0) ? pos_face : neg_face;
1263 lp_build_cube_face(struct lp_build_sample_context
*bld
,
1264 LLVMValueRef major_coord
,
1265 unsigned pos_face
, unsigned neg_face
)
1267 LLVMValueRef cmp
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
1269 bld
->float_bld
.zero
, "");
1270 LLVMValueRef pos
= LLVMConstInt(LLVMInt32Type(), pos_face
, 0);
1271 LLVMValueRef neg
= LLVMConstInt(LLVMInt32Type(), neg_face
, 0);
1272 LLVMValueRef res
= LLVMBuildSelect(bld
->builder
, cmp
, pos
, neg
, "");
1279 * Generate code to do cube face selection and compute per-face texcoords.
1282 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
1287 LLVMValueRef
*face_s
,
1288 LLVMValueRef
*face_t
)
1290 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1291 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
1292 LLVMValueRef rx
, ry
, rz
;
1293 LLVMValueRef arx
, ary
, arz
;
1294 LLVMValueRef c25
= LLVMConstReal(LLVMFloatType(), 0.25);
1295 LLVMValueRef arx_ge_ary
, arx_ge_arz
;
1296 LLVMValueRef ary_ge_arx
, ary_ge_arz
;
1297 LLVMValueRef arx_ge_ary_arz
, ary_ge_arx_arz
;
1298 LLVMValueRef rx_pos
, ry_pos
, rz_pos
;
1300 assert(bld
->coord_bld
.type
.length
== 4);
1303 * Use the average of the four pixel's texcoords to choose the face.
1305 rx
= lp_build_mul(float_bld
, c25
,
1306 lp_build_sum_vector(&bld
->coord_bld
, s
));
1307 ry
= lp_build_mul(float_bld
, c25
,
1308 lp_build_sum_vector(&bld
->coord_bld
, t
));
1309 rz
= lp_build_mul(float_bld
, c25
,
1310 lp_build_sum_vector(&bld
->coord_bld
, r
));
1312 arx
= lp_build_abs(float_bld
, rx
);
1313 ary
= lp_build_abs(float_bld
, ry
);
1314 arz
= lp_build_abs(float_bld
, rz
);
1317 * Compare sign/magnitude of rx,ry,rz to determine face
1319 arx_ge_ary
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, arx
, ary
, "");
1320 arx_ge_arz
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, arx
, arz
, "");
1321 ary_ge_arx
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ary
, arx
, "");
1322 ary_ge_arz
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ary
, arz
, "");
1324 arx_ge_ary_arz
= LLVMBuildAnd(bld
->builder
, arx_ge_ary
, arx_ge_arz
, "");
1325 ary_ge_arx_arz
= LLVMBuildAnd(bld
->builder
, ary_ge_arx
, ary_ge_arz
, "");
1327 rx_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, rx
, float_bld
->zero
, "");
1328 ry_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ry
, float_bld
->zero
, "");
1329 rz_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, rz
, float_bld
->zero
, "");
1332 struct lp_build_flow_context
*flow_ctx
;
1333 struct lp_build_if_state if_ctx
;
1335 flow_ctx
= lp_build_flow_create(bld
->builder
);
1336 lp_build_flow_scope_begin(flow_ctx
);
1338 *face_s
= bld
->coord_bld
.undef
;
1339 *face_t
= bld
->coord_bld
.undef
;
1340 *face
= bld
->int_bld
.undef
;
1342 lp_build_name(*face_s
, "face_s");
1343 lp_build_name(*face_t
, "face_t");
1344 lp_build_name(*face
, "face");
1346 lp_build_flow_scope_declare(flow_ctx
, face_s
);
1347 lp_build_flow_scope_declare(flow_ctx
, face_t
);
1348 lp_build_flow_scope_declare(flow_ctx
, face
);
1350 lp_build_if(&if_ctx
, flow_ctx
, bld
->builder
, arx_ge_ary_arz
);
1353 LLVMValueRef sign
= lp_build_sgn(float_bld
, rx
);
1354 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, s
);
1355 *face_s
= lp_build_cube_coord(coord_bld
, sign
, +1, r
, ima
);
1356 *face_t
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1357 *face
= lp_build_cube_face(bld
, rx
,
1358 PIPE_TEX_FACE_POS_X
,
1359 PIPE_TEX_FACE_NEG_X
);
1361 lp_build_else(&if_ctx
);
1363 struct lp_build_flow_context
*flow_ctx2
;
1364 struct lp_build_if_state if_ctx2
;
1366 LLVMValueRef face_s2
= bld
->coord_bld
.undef
;
1367 LLVMValueRef face_t2
= bld
->coord_bld
.undef
;
1368 LLVMValueRef face2
= bld
->int_bld
.undef
;
1370 flow_ctx2
= lp_build_flow_create(bld
->builder
);
1371 lp_build_flow_scope_begin(flow_ctx2
);
1372 lp_build_flow_scope_declare(flow_ctx2
, &face_s2
);
1373 lp_build_flow_scope_declare(flow_ctx2
, &face_t2
);
1374 lp_build_flow_scope_declare(flow_ctx2
, &face2
);
1376 ary_ge_arx_arz
= LLVMBuildAnd(bld
->builder
, ary_ge_arx
, ary_ge_arz
, "");
1378 lp_build_if(&if_ctx2
, flow_ctx2
, bld
->builder
, ary_ge_arx_arz
);
1381 LLVMValueRef sign
= lp_build_sgn(float_bld
, ry
);
1382 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, t
);
1383 face_s2
= lp_build_cube_coord(coord_bld
, NULL
, -1, s
, ima
);
1384 face_t2
= lp_build_cube_coord(coord_bld
, sign
, -1, r
, ima
);
1385 face2
= lp_build_cube_face(bld
, ry
,
1386 PIPE_TEX_FACE_POS_Y
,
1387 PIPE_TEX_FACE_NEG_Y
);
1389 lp_build_else(&if_ctx2
);
1392 LLVMValueRef sign
= lp_build_sgn(float_bld
, rz
);
1393 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, r
);
1394 face_s2
= lp_build_cube_coord(coord_bld
, sign
, -1, s
, ima
);
1395 face_t2
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1396 face2
= lp_build_cube_face(bld
, rz
,
1397 PIPE_TEX_FACE_POS_Z
,
1398 PIPE_TEX_FACE_NEG_Z
);
1400 lp_build_endif(&if_ctx2
);
1401 lp_build_flow_scope_end(flow_ctx2
);
1402 lp_build_flow_destroy(flow_ctx2
);
1408 lp_build_endif(&if_ctx
);
1409 lp_build_flow_scope_end(flow_ctx
);
1410 lp_build_flow_destroy(flow_ctx
);
1417 * Sample the texture/mipmap using given image filter and mip filter.
1418 * data0_ptr and data1_ptr point to the two mipmap levels to sample
1419 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
1420 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1423 lp_build_sample_mipmap(struct lp_build_sample_context
*bld
,
1424 unsigned img_filter
,
1425 unsigned mip_filter
,
1429 LLVMValueRef lod_fpart
,
1430 LLVMValueRef width0_vec
,
1431 LLVMValueRef width1_vec
,
1432 LLVMValueRef height0_vec
,
1433 LLVMValueRef height1_vec
,
1434 LLVMValueRef depth0_vec
,
1435 LLVMValueRef depth1_vec
,
1436 LLVMValueRef row_stride0_vec
,
1437 LLVMValueRef row_stride1_vec
,
1438 LLVMValueRef img_stride0_vec
,
1439 LLVMValueRef img_stride1_vec
,
1440 LLVMValueRef data_ptr0
,
1441 LLVMValueRef data_ptr1
,
1442 LLVMValueRef
*colors_out
)
1444 LLVMValueRef colors0
[4], colors1
[4];
1447 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
1448 /* sample the first mipmap level */
1449 lp_build_sample_image_nearest(bld
,
1450 width0_vec
, height0_vec
, depth0_vec
,
1451 row_stride0_vec
, img_stride0_vec
,
1452 data_ptr0
, s
, t
, r
, colors0
);
1454 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1455 /* sample the second mipmap level */
1456 lp_build_sample_image_nearest(bld
,
1457 width1_vec
, height1_vec
, depth1_vec
,
1458 row_stride1_vec
, img_stride1_vec
,
1459 data_ptr1
, s
, t
, r
, colors1
);
1463 assert(img_filter
== PIPE_TEX_FILTER_LINEAR
);
1465 /* sample the first mipmap level */
1466 lp_build_sample_image_linear(bld
,
1467 width0_vec
, height0_vec
, depth0_vec
,
1468 row_stride0_vec
, img_stride0_vec
,
1469 data_ptr0
, s
, t
, r
, colors0
);
1471 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1472 /* sample the second mipmap level */
1473 lp_build_sample_image_linear(bld
,
1474 width1_vec
, height1_vec
, depth1_vec
,
1475 row_stride1_vec
, img_stride1_vec
,
1476 data_ptr1
, s
, t
, r
, colors1
);
1480 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1481 /* interpolate samples from the two mipmap levels */
1482 for (chan
= 0; chan
< 4; chan
++) {
1483 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
1484 colors0
[chan
], colors1
[chan
]);
1488 /* use first/only level's colors */
1489 for (chan
= 0; chan
< 4; chan
++) {
1490 colors_out
[chan
] = colors0
[chan
];
1498 * General texture sampling codegen.
1499 * This function handles texture sampling for all texture targets (1D,
1500 * 2D, 3D, cube) and all filtering modes.
1503 lp_build_sample_general(struct lp_build_sample_context
*bld
,
1508 const LLVMValueRef
*ddx
,
1509 const LLVMValueRef
*ddy
,
1510 LLVMValueRef lod_bias
, /* optional */
1511 LLVMValueRef explicit_lod
, /* optional */
1513 LLVMValueRef height
,
1515 LLVMValueRef width_vec
,
1516 LLVMValueRef height_vec
,
1517 LLVMValueRef depth_vec
,
1518 LLVMValueRef row_stride_array
,
1519 LLVMValueRef img_stride_array
,
1520 LLVMValueRef data_array
,
1521 LLVMValueRef
*colors_out
)
1523 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1524 const unsigned mip_filter
= bld
->static_state
->min_mip_filter
;
1525 const unsigned min_filter
= bld
->static_state
->min_img_filter
;
1526 const unsigned mag_filter
= bld
->static_state
->mag_img_filter
;
1527 const int dims
= texture_dims(bld
->static_state
->target
);
1528 LLVMValueRef lod
= NULL
, lod_fpart
= NULL
;
1529 LLVMValueRef ilevel0
, ilevel1
= NULL
, ilevel0_vec
, ilevel1_vec
= NULL
;
1530 LLVMValueRef width0_vec
= NULL
, height0_vec
= NULL
, depth0_vec
= NULL
;
1531 LLVMValueRef width1_vec
= NULL
, height1_vec
= NULL
, depth1_vec
= NULL
;
1532 LLVMValueRef row_stride0_vec
= NULL
, row_stride1_vec
= NULL
;
1533 LLVMValueRef img_stride0_vec
= NULL
, img_stride1_vec
= NULL
;
1534 LLVMValueRef data_ptr0
, data_ptr1
= NULL
;
1535 LLVMValueRef face_ddx
[4], face_ddy
[4];
1538 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1539 mip_filter, min_filter, mag_filter);
1543 * Choose cube face, recompute texcoords and derivatives for the chosen face.
1545 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1546 LLVMValueRef face
, face_s
, face_t
;
1547 lp_build_cube_lookup(bld
, s
, t
, r
, &face
, &face_s
, &face_t
);
1548 s
= face_s
; /* vec */
1549 t
= face_t
; /* vec */
1550 /* use 'r' to indicate cube face */
1551 r
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, face
); /* vec */
1553 /* recompute ddx, ddy using the new (s,t) face texcoords */
1554 face_ddx
[0] = lp_build_ddx(&bld
->coord_bld
, s
);
1555 face_ddx
[1] = lp_build_ddx(&bld
->coord_bld
, t
);
1558 face_ddy
[0] = lp_build_ddy(&bld
->coord_bld
, s
);
1559 face_ddy
[1] = lp_build_ddy(&bld
->coord_bld
, t
);
1567 * Compute the level of detail (float).
1569 if (min_filter
!= mag_filter
||
1570 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
1571 /* Need to compute lod either to choose mipmap levels or to
1572 * distinguish between minification/magnification with one mipmap level.
1574 lod
= lp_build_lod_selector(bld
, ddx
, ddy
,
1575 lod_bias
, explicit_lod
,
1576 width
, height
, depth
);
1580 * Compute integer mipmap level(s) to fetch texels from.
1582 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
1583 /* always use mip level 0 */
1584 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1585 /* XXX this is a work-around for an apparent bug in LLVM 2.7.
1586 * We should be able to set ilevel0 = const(0) but that causes
1587 * bad x86 code to be emitted.
1589 lod
= lp_build_const_elem(bld
->coord_bld
.type
, 0.0);
1590 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
1593 ilevel0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
1598 if (mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
1599 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
1602 assert(mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
);
1603 lp_build_linear_mip_levels(bld
, unit
, lod
, &ilevel0
, &ilevel1
,
1605 lod_fpart
= lp_build_broadcast_scalar(&bld
->coord_bld
, lod_fpart
);
1610 * Convert scalar integer mipmap levels into vectors.
1612 ilevel0_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel0
);
1613 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
)
1614 ilevel1_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, ilevel1
);
1617 * Compute width, height at mipmap level 'ilevel0'
1619 width0_vec
= lp_build_minify(bld
, width_vec
, ilevel0_vec
);
1621 height0_vec
= lp_build_minify(bld
, height_vec
, ilevel0_vec
);
1622 row_stride0_vec
= lp_build_get_level_stride_vec(bld
, row_stride_array
,
1624 if (dims
== 3 || bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1625 img_stride0_vec
= lp_build_get_level_stride_vec(bld
,
1629 depth0_vec
= lp_build_minify(bld
, depth_vec
, ilevel0_vec
);
1633 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1634 /* compute width, height, depth for second mipmap level at 'ilevel1' */
1635 width1_vec
= lp_build_minify(bld
, width_vec
, ilevel1_vec
);
1637 height1_vec
= lp_build_minify(bld
, height_vec
, ilevel1_vec
);
1638 row_stride1_vec
= lp_build_get_level_stride_vec(bld
, row_stride_array
,
1640 if (dims
== 3 || bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
1641 img_stride1_vec
= lp_build_get_level_stride_vec(bld
,
1645 depth1_vec
= lp_build_minify(bld
, depth_vec
, ilevel1_vec
);
1652 * Get pointer(s) to image data for mipmap level(s).
1654 data_ptr0
= lp_build_get_mipmap_level(bld
, data_array
, ilevel0
);
1655 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1656 data_ptr1
= lp_build_get_mipmap_level(bld
, data_array
, ilevel1
);
1660 * Get/interpolate texture colors.
1662 if (min_filter
== mag_filter
) {
1663 /* no need to distinquish between minification and magnification */
1664 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
, s
, t
, r
, lod_fpart
,
1665 width0_vec
, width1_vec
,
1666 height0_vec
, height1_vec
,
1667 depth0_vec
, depth1_vec
,
1668 row_stride0_vec
, row_stride1_vec
,
1669 img_stride0_vec
, img_stride1_vec
,
1670 data_ptr0
, data_ptr1
,
1674 /* Emit conditional to choose min image filter or mag image filter
1675 * depending on the lod being >0 or <= 0, respectively.
1677 struct lp_build_flow_context
*flow_ctx
;
1678 struct lp_build_if_state if_ctx
;
1679 LLVMValueRef minify
;
1681 flow_ctx
= lp_build_flow_create(bld
->builder
);
1682 lp_build_flow_scope_begin(flow_ctx
);
1684 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[0]);
1685 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[1]);
1686 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[2]);
1687 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[3]);
1689 /* minify = lod > 0.0 */
1690 minify
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
1691 lod
, float_bld
->zero
, "");
1693 lp_build_if(&if_ctx
, flow_ctx
, bld
->builder
, minify
);
1695 /* Use the minification filter */
1696 lp_build_sample_mipmap(bld
, min_filter
, mip_filter
,
1698 width0_vec
, width1_vec
,
1699 height0_vec
, height1_vec
,
1700 depth0_vec
, depth1_vec
,
1701 row_stride0_vec
, row_stride1_vec
,
1702 img_stride0_vec
, img_stride1_vec
,
1703 data_ptr0
, data_ptr1
,
1706 lp_build_else(&if_ctx
);
1708 /* Use the magnification filter */
1709 lp_build_sample_mipmap(bld
, mag_filter
, mip_filter
,
1711 width0_vec
, width1_vec
,
1712 height0_vec
, height1_vec
,
1713 depth0_vec
, depth1_vec
,
1714 row_stride0_vec
, row_stride1_vec
,
1715 img_stride0_vec
, img_stride1_vec
,
1716 data_ptr0
, data_ptr1
,
1719 lp_build_endif(&if_ctx
);
1721 lp_build_flow_scope_end(flow_ctx
);
1722 lp_build_flow_destroy(flow_ctx
);
1729 lp_build_sample_2d_linear_aos(struct lp_build_sample_context
*bld
,
1733 LLVMValueRef height
,
1734 LLVMValueRef stride_array
,
1735 LLVMValueRef data_array
,
1736 LLVMValueRef texel_out
[4])
1738 LLVMBuilderRef builder
= bld
->builder
;
1739 struct lp_build_context i32
, h16
, u8n
;
1740 LLVMTypeRef i32_vec_type
, h16_vec_type
, u8n_vec_type
;
1741 LLVMValueRef i32_c8
, i32_c128
, i32_c255
;
1742 LLVMValueRef s_ipart
, s_fpart
, s_fpart_lo
, s_fpart_hi
;
1743 LLVMValueRef t_ipart
, t_fpart
, t_fpart_lo
, t_fpart_hi
;
1744 LLVMValueRef x0
, x1
;
1745 LLVMValueRef y0
, y1
;
1746 LLVMValueRef neighbors
[2][2];
1747 LLVMValueRef neighbors_lo
[2][2];
1748 LLVMValueRef neighbors_hi
[2][2];
1749 LLVMValueRef packed
, packed_lo
, packed_hi
;
1750 LLVMValueRef unswizzled
[4];
1751 LLVMValueRef stride
;
1753 assert(bld
->static_state
->target
== PIPE_TEXTURE_2D
);
1754 assert(bld
->static_state
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
);
1755 assert(bld
->static_state
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
);
1756 assert(bld
->static_state
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
);
1758 lp_build_context_init(&i32
, builder
, lp_type_int_vec(32));
1759 lp_build_context_init(&h16
, builder
, lp_type_ufixed(16));
1760 lp_build_context_init(&u8n
, builder
, lp_type_unorm(8));
1762 i32_vec_type
= lp_build_vec_type(i32
.type
);
1763 h16_vec_type
= lp_build_vec_type(h16
.type
);
1764 u8n_vec_type
= lp_build_vec_type(u8n
.type
);
1766 if (bld
->static_state
->normalized_coords
) {
1767 LLVMTypeRef coord_vec_type
= lp_build_vec_type(bld
->coord_type
);
1768 LLVMValueRef fp_width
= LLVMBuildSIToFP(bld
->builder
, width
, coord_vec_type
, "");
1769 LLVMValueRef fp_height
= LLVMBuildSIToFP(bld
->builder
, height
, coord_vec_type
, "");
1770 s
= lp_build_mul(&bld
->coord_bld
, s
, fp_width
);
1771 t
= lp_build_mul(&bld
->coord_bld
, t
, fp_height
);
1774 /* scale coords by 256 (8 fractional bits) */
1775 s
= lp_build_mul_imm(&bld
->coord_bld
, s
, 256);
1776 t
= lp_build_mul_imm(&bld
->coord_bld
, t
, 256);
1778 /* convert float to int */
1779 s
= LLVMBuildFPToSI(builder
, s
, i32_vec_type
, "");
1780 t
= LLVMBuildFPToSI(builder
, t
, i32_vec_type
, "");
1782 /* subtract 0.5 (add -128) */
1783 i32_c128
= lp_build_const_int_vec(i32
.type
, -128);
1784 s
= LLVMBuildAdd(builder
, s
, i32_c128
, "");
1785 t
= LLVMBuildAdd(builder
, t
, i32_c128
, "");
1787 /* compute floor (shift right 8) */
1788 i32_c8
= lp_build_const_int_vec(i32
.type
, 8);
1789 s_ipart
= LLVMBuildAShr(builder
, s
, i32_c8
, "");
1790 t_ipart
= LLVMBuildAShr(builder
, t
, i32_c8
, "");
1792 /* compute fractional part (AND with 0xff) */
1793 i32_c255
= lp_build_const_int_vec(i32
.type
, 255);
1794 s_fpart
= LLVMBuildAnd(builder
, s
, i32_c255
, "");
1795 t_fpart
= LLVMBuildAnd(builder
, t
, i32_c255
, "");
1800 x1
= lp_build_add(&bld
->int_coord_bld
, x0
, bld
->int_coord_bld
.one
);
1801 y1
= lp_build_add(&bld
->int_coord_bld
, y0
, bld
->int_coord_bld
.one
);
1803 x0
= lp_build_sample_wrap_int(bld
, x0
, width
, bld
->static_state
->pot_width
,
1804 bld
->static_state
->wrap_s
);
1805 y0
= lp_build_sample_wrap_int(bld
, y0
, height
, bld
->static_state
->pot_height
,
1806 bld
->static_state
->wrap_t
);
1808 x1
= lp_build_sample_wrap_int(bld
, x1
, width
, bld
->static_state
->pot_width
,
1809 bld
->static_state
->wrap_s
);
1810 y1
= lp_build_sample_wrap_int(bld
, y1
, height
, bld
->static_state
->pot_height
,
1811 bld
->static_state
->wrap_t
);
1814 * Transform 4 x i32 in
1816 * s_fpart = {s0, s1, s2, s3}
1820 * s_fpart = {00, s0, 00, s1, 00, s2, 00, s3}
1824 * s_fpart_lo = {s0, s0, s0, s0, s1, s1, s1, s1}
1825 * s_fpart_hi = {s2, s2, s2, s2, s3, s3, s3, s3}
1827 * and likewise for t_fpart. There is no risk of loosing precision here
1828 * since the fractional parts only use the lower 8bits.
1831 s_fpart
= LLVMBuildBitCast(builder
, s_fpart
, h16_vec_type
, "");
1832 t_fpart
= LLVMBuildBitCast(builder
, t_fpart
, h16_vec_type
, "");
1835 LLVMTypeRef elem_type
= LLVMInt32Type();
1836 LLVMValueRef shuffles_lo
[LP_MAX_VECTOR_LENGTH
];
1837 LLVMValueRef shuffles_hi
[LP_MAX_VECTOR_LENGTH
];
1838 LLVMValueRef shuffle_lo
;
1839 LLVMValueRef shuffle_hi
;
1842 for(j
= 0; j
< h16
.type
.length
; j
+= 4) {
1843 unsigned subindex
= util_cpu_caps
.little_endian
? 0 : 1;
1846 index
= LLVMConstInt(elem_type
, j
/2 + subindex
, 0);
1847 for(i
= 0; i
< 4; ++i
)
1848 shuffles_lo
[j
+ i
] = index
;
1850 index
= LLVMConstInt(elem_type
, h16
.type
.length
/2 + j
/2 + subindex
, 0);
1851 for(i
= 0; i
< 4; ++i
)
1852 shuffles_hi
[j
+ i
] = index
;
1855 shuffle_lo
= LLVMConstVector(shuffles_lo
, h16
.type
.length
);
1856 shuffle_hi
= LLVMConstVector(shuffles_hi
, h16
.type
.length
);
1858 s_fpart_lo
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_lo
, "");
1859 t_fpart_lo
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_lo
, "");
1860 s_fpart_hi
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
, shuffle_hi
, "");
1861 t_fpart_hi
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
, shuffle_hi
, "");
1864 stride
= lp_build_get_const_level_stride_vec(bld
, stride_array
, 0);
1867 * Fetch the pixels as 4 x 32bit (rgba order might differ):
1869 * rgba0 rgba1 rgba2 rgba3
1871 * bit cast them into 16 x u8
1873 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
1875 * unpack them into two 8 x i16:
1877 * r0 g0 b0 a0 r1 g1 b1 a1
1878 * r2 g2 b2 a2 r3 g3 b3 a3
1880 * The higher 8 bits of the resulting elements will be zero.
1883 neighbors
[0][0] = lp_build_sample_packed(bld
, x0
, y0
, stride
, data_array
);
1884 neighbors
[0][1] = lp_build_sample_packed(bld
, x1
, y0
, stride
, data_array
);
1885 neighbors
[1][0] = lp_build_sample_packed(bld
, x0
, y1
, stride
, data_array
);
1886 neighbors
[1][1] = lp_build_sample_packed(bld
, x1
, y1
, stride
, data_array
);
1888 neighbors
[0][0] = LLVMBuildBitCast(builder
, neighbors
[0][0], u8n_vec_type
, "");
1889 neighbors
[0][1] = LLVMBuildBitCast(builder
, neighbors
[0][1], u8n_vec_type
, "");
1890 neighbors
[1][0] = LLVMBuildBitCast(builder
, neighbors
[1][0], u8n_vec_type
, "");
1891 neighbors
[1][1] = LLVMBuildBitCast(builder
, neighbors
[1][1], u8n_vec_type
, "");
1893 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][0], &neighbors_lo
[0][0], &neighbors_hi
[0][0]);
1894 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[0][1], &neighbors_lo
[0][1], &neighbors_hi
[0][1]);
1895 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][0], &neighbors_lo
[1][0], &neighbors_hi
[1][0]);
1896 lp_build_unpack2(builder
, u8n
.type
, h16
.type
, neighbors
[1][1], &neighbors_lo
[1][1], &neighbors_hi
[1][1]);
1899 * Linear interpolate with 8.8 fixed point.
1902 packed_lo
= lp_build_lerp_2d(&h16
,
1903 s_fpart_lo
, t_fpart_lo
,
1907 neighbors_lo
[1][1]);
1909 packed_hi
= lp_build_lerp_2d(&h16
,
1910 s_fpart_hi
, t_fpart_hi
,
1914 neighbors_hi
[1][1]);
1916 packed
= lp_build_pack2(builder
, h16
.type
, u8n
.type
, packed_lo
, packed_hi
);
1919 * Convert to SoA and swizzle.
1922 lp_build_rgba8_to_f32_soa(bld
->builder
,
1924 packed
, unswizzled
);
1926 if (util_format_is_rgba8_variant(bld
->format_desc
)) {
1927 lp_build_format_swizzle_soa(bld
->format_desc
,
1929 unswizzled
, texel_out
);
1931 texel_out
[0] = unswizzled
[0];
1932 texel_out
[1] = unswizzled
[1];
1933 texel_out
[2] = unswizzled
[2];
1934 texel_out
[3] = unswizzled
[3];
1937 apply_sampler_swizzle(bld
, texel_out
);
1942 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
1944 LLVMValueRef texel
[4])
1946 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1950 if(bld
->static_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
1953 /* TODO: Compare before swizzling, to avoid redundant computations */
1955 for(chan
= 0; chan
< 4; ++chan
) {
1957 cmp
= lp_build_cmp(texel_bld
, bld
->static_state
->compare_func
, p
, texel
[chan
]);
1958 cmp
= lp_build_select(texel_bld
, cmp
, texel_bld
->one
, texel_bld
->zero
);
1961 res
= lp_build_add(texel_bld
, res
, cmp
);
1967 res
= lp_build_mul(texel_bld
, res
, lp_build_const_vec(texel_bld
->type
, 0.25));
1969 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1970 for(chan
= 0; chan
< 3; ++chan
)
1972 texel
[3] = texel_bld
->one
;
1977 * Just set texels to white instead of actually sampling the texture.
1981 lp_build_sample_nop(struct lp_build_sample_context
*bld
,
1982 LLVMValueRef texel_out
[4])
1984 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1987 for (chan
= 0; chan
< 4; chan
++) {
1988 /*lp_bld_mov(texel_bld, texel, texel_bld->one);*/
1989 texel_out
[chan
] = texel_bld
->one
;
1995 * Build texture sampling code.
1996 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1998 * \param type vector float type to use for coords, etc.
1999 * \param ddx partial derivatives of (s,t,r,q) with respect to x
2000 * \param ddy partial derivatives of (s,t,r,q) with respect to y
2003 lp_build_sample_soa(LLVMBuilderRef builder
,
2004 const struct lp_sampler_static_state
*static_state
,
2005 struct lp_sampler_dynamic_state
*dynamic_state
,
2006 struct lp_type type
,
2008 unsigned num_coords
,
2009 const LLVMValueRef
*coords
,
2010 const LLVMValueRef ddx
[4],
2011 const LLVMValueRef ddy
[4],
2012 LLVMValueRef lod_bias
, /* optional */
2013 LLVMValueRef explicit_lod
, /* optional */
2014 LLVMValueRef texel_out
[4])
2016 struct lp_build_sample_context bld
;
2017 LLVMValueRef width
, width_vec
;
2018 LLVMValueRef height
, height_vec
;
2019 LLVMValueRef depth
, depth_vec
;
2020 LLVMValueRef row_stride_array
, img_stride_array
;
2021 LLVMValueRef data_array
;
2027 enum pipe_format fmt
= static_state
->format
;
2028 debug_printf("Sample from %s\n", util_format_name(fmt
));
2031 assert(type
.floating
);
2033 /* Setup our build context */
2034 memset(&bld
, 0, sizeof bld
);
2035 bld
.builder
= builder
;
2036 bld
.static_state
= static_state
;
2037 bld
.dynamic_state
= dynamic_state
;
2038 bld
.format_desc
= util_format_description(static_state
->format
);
2040 bld
.float_type
= lp_type_float(32);
2041 bld
.int_type
= lp_type_int(32);
2042 bld
.coord_type
= type
;
2043 bld
.uint_coord_type
= lp_uint_type(type
);
2044 bld
.int_coord_type
= lp_int_type(type
);
2045 bld
.texel_type
= type
;
2047 lp_build_context_init(&bld
.float_bld
, builder
, bld
.float_type
);
2048 lp_build_context_init(&bld
.int_bld
, builder
, bld
.int_type
);
2049 lp_build_context_init(&bld
.coord_bld
, builder
, bld
.coord_type
);
2050 lp_build_context_init(&bld
.uint_coord_bld
, builder
, bld
.uint_coord_type
);
2051 lp_build_context_init(&bld
.int_coord_bld
, builder
, bld
.int_coord_type
);
2052 lp_build_context_init(&bld
.texel_bld
, builder
, bld
.texel_type
);
2054 /* Get the dynamic state */
2055 width
= dynamic_state
->width(dynamic_state
, builder
, unit
);
2056 height
= dynamic_state
->height(dynamic_state
, builder
, unit
);
2057 depth
= dynamic_state
->depth(dynamic_state
, builder
, unit
);
2058 row_stride_array
= dynamic_state
->row_stride(dynamic_state
, builder
, unit
);
2059 img_stride_array
= dynamic_state
->img_stride(dynamic_state
, builder
, unit
);
2060 data_array
= dynamic_state
->data_ptr(dynamic_state
, builder
, unit
);
2061 /* Note that data_array is an array[level] of pointers to texture images */
2067 width_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, width
);
2068 height_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, height
);
2069 depth_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, depth
);
2072 /* For debug: no-op texture sampling */
2073 lp_build_sample_nop(&bld
, texel_out
);
2075 else if (util_format_fits_8unorm(bld
.format_desc
) &&
2076 bld
.format_desc
->nr_channels
> 1 &&
2077 static_state
->target
== PIPE_TEXTURE_2D
&&
2078 static_state
->min_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
2079 static_state
->mag_img_filter
== PIPE_TEX_FILTER_LINEAR
&&
2080 static_state
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
&&
2081 is_simple_wrap_mode(static_state
->wrap_s
) &&
2082 is_simple_wrap_mode(static_state
->wrap_t
)) {
2084 lp_build_sample_2d_linear_aos(&bld
, s
, t
, width_vec
, height_vec
,
2085 row_stride_array
, data_array
, texel_out
);
2088 lp_build_sample_general(&bld
, unit
, s
, t
, r
, ddx
, ddy
,
2089 lod_bias
, explicit_lod
,
2090 width
, height
, depth
,
2091 width_vec
, height_vec
, depth_vec
,
2092 row_stride_array
, img_stride_array
,
2097 lp_build_sample_compare(&bld
, r
, texel_out
);