1 /**************************************************************************
3 * Copyright 2010 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 "lp_bld_debug.h"
44 #include "lp_bld_type.h"
45 #include "lp_bld_const.h"
46 #include "lp_bld_conv.h"
47 #include "lp_bld_arit.h"
48 #include "lp_bld_logic.h"
49 #include "lp_bld_swizzle.h"
50 #include "lp_bld_pack.h"
51 #include "lp_bld_flow.h"
52 #include "lp_bld_gather.h"
53 #include "lp_bld_format.h"
54 #include "lp_bld_sample.h"
55 #include "lp_bld_sample_aos.h"
56 #include "lp_bld_quad.h"
60 * Build LLVM code for texture coord wrapping, for nearest filtering,
61 * for scaled integer texcoords.
62 * \param block_length is the length of the pixel block along the
64 * \param coord the incoming texcoord (s,t,r or q) scaled to the texture size
65 * \param length the texture size along one dimension
66 * \param stride pixel stride along the coordinate axis (in bytes)
67 * \param is_pot if TRUE, length is a power of two
68 * \param wrap_mode one of PIPE_TEX_WRAP_x
69 * \param out_offset byte offset for the wrapped coordinate
70 * \param out_i resulting sub-block pixel coordinate for coord0
73 lp_build_sample_wrap_nearest_int(struct lp_build_sample_context
*bld
,
74 unsigned block_length
,
80 LLVMValueRef
*out_offset
,
83 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
84 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
85 LLVMValueRef length_minus_one
;
87 length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
90 case PIPE_TEX_WRAP_REPEAT
:
92 coord
= LLVMBuildAnd(bld
->builder
, coord
, length_minus_one
, "");
94 /* Add a bias to the texcoord to handle negative coords */
95 LLVMValueRef bias
= lp_build_mul_imm(uint_coord_bld
, length
, 1024);
96 coord
= LLVMBuildAdd(bld
->builder
, coord
, bias
, "");
97 coord
= LLVMBuildURem(bld
->builder
, coord
, length
, "");
101 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
102 coord
= lp_build_max(int_coord_bld
, coord
, int_coord_bld
->zero
);
103 coord
= lp_build_min(int_coord_bld
, coord
, length_minus_one
);
106 case PIPE_TEX_WRAP_CLAMP
:
107 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
108 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
109 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
110 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
111 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
116 lp_build_sample_partial_offset(uint_coord_bld
, block_length
, coord
, stride
,
122 * Build LLVM code for texture coord wrapping, for linear filtering,
123 * for scaled integer texcoords.
124 * \param block_length is the length of the pixel block along the
126 * \param coord0 the incoming texcoord (s,t,r or q) scaled to the texture size
127 * \param length the texture size along one dimension
128 * \param stride pixel stride along the coordinate axis (in bytes)
129 * \param is_pot if TRUE, length is a power of two
130 * \param wrap_mode one of PIPE_TEX_WRAP_x
131 * \param offset0 resulting relative offset for coord0
132 * \param offset1 resulting relative offset for coord0 + 1
133 * \param i0 resulting sub-block pixel coordinate for coord0
134 * \param i1 resulting sub-block pixel coordinate for coord0 + 1
137 lp_build_sample_wrap_linear_int(struct lp_build_sample_context
*bld
,
138 unsigned block_length
,
144 LLVMValueRef
*offset0
,
145 LLVMValueRef
*offset1
,
149 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
150 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
151 LLVMValueRef length_minus_one
;
152 LLVMValueRef lmask
, umask
, mask
;
154 if (block_length
!= 1) {
156 * If the pixel block covers more than one pixel then there is no easy
157 * way to calculate offset1 relative to offset0. Instead, compute them
163 lp_build_sample_wrap_nearest_int(bld
,
172 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
174 lp_build_sample_wrap_nearest_int(bld
,
187 * Scalar pixels -- try to compute offset0 and offset1 with a single stride
191 *i0
= uint_coord_bld
->zero
;
192 *i1
= uint_coord_bld
->zero
;
194 length_minus_one
= lp_build_sub(int_coord_bld
, length
, int_coord_bld
->one
);
197 case PIPE_TEX_WRAP_REPEAT
:
199 coord0
= LLVMBuildAnd(bld
->builder
, coord0
, length_minus_one
, "");
202 /* Add a bias to the texcoord to handle negative coords */
203 LLVMValueRef bias
= lp_build_mul_imm(uint_coord_bld
, length
, 1024);
204 coord0
= LLVMBuildAdd(bld
->builder
, coord0
, bias
, "");
205 coord0
= LLVMBuildURem(bld
->builder
, coord0
, length
, "");
208 mask
= lp_build_compare(bld
->builder
, int_coord_bld
->type
,
209 PIPE_FUNC_NOTEQUAL
, coord0
, length_minus_one
);
211 *offset0
= lp_build_mul(uint_coord_bld
, coord0
, stride
);
212 *offset1
= LLVMBuildAnd(bld
->builder
,
213 lp_build_add(uint_coord_bld
, *offset0
, stride
),
217 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
218 lmask
= lp_build_compare(int_coord_bld
->builder
, int_coord_bld
->type
,
219 PIPE_FUNC_GEQUAL
, coord0
, int_coord_bld
->zero
);
220 umask
= lp_build_compare(int_coord_bld
->builder
, int_coord_bld
->type
,
221 PIPE_FUNC_LESS
, coord0
, length_minus_one
);
223 coord0
= lp_build_select(int_coord_bld
, lmask
, coord0
, int_coord_bld
->zero
);
224 coord0
= lp_build_select(int_coord_bld
, umask
, coord0
, length_minus_one
);
226 mask
= LLVMBuildAnd(bld
->builder
, lmask
, umask
, "");
228 *offset0
= lp_build_mul(uint_coord_bld
, coord0
, stride
);
229 *offset1
= lp_build_add(uint_coord_bld
,
231 LLVMBuildAnd(bld
->builder
, stride
, mask
, ""));
234 case PIPE_TEX_WRAP_CLAMP
:
235 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
236 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
237 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
238 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
239 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
242 *offset0
= uint_coord_bld
->zero
;
243 *offset1
= uint_coord_bld
->zero
;
250 * Sample a single texture image with nearest sampling.
251 * If sampling a cube texture, r = cube face in [0,5].
252 * Return filtered color as two vectors of 16-bit fixed point values.
255 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
256 LLVMValueRef width_vec
,
257 LLVMValueRef height_vec
,
258 LLVMValueRef depth_vec
,
259 LLVMValueRef row_stride_vec
,
260 LLVMValueRef img_stride_vec
,
261 LLVMValueRef data_ptr
,
265 LLVMValueRef
*colors_lo
,
266 LLVMValueRef
*colors_hi
)
268 const unsigned dims
= bld
->dims
;
269 LLVMBuilderRef builder
= bld
->builder
;
270 struct lp_build_context i32
, h16
, u8n
;
271 LLVMTypeRef i32_vec_type
, h16_vec_type
, u8n_vec_type
;
273 LLVMValueRef s_ipart
, t_ipart
, r_ipart
;
274 LLVMValueRef x_stride
;
275 LLVMValueRef x_offset
, offset
;
276 LLVMValueRef x_subcoord
, y_subcoord
, z_subcoord
;
278 lp_build_context_init(&i32
, builder
, lp_type_int_vec(32));
279 lp_build_context_init(&h16
, builder
, lp_type_ufixed(16));
280 lp_build_context_init(&u8n
, builder
, lp_type_unorm(8));
282 i32_vec_type
= lp_build_vec_type(i32
.type
);
283 h16_vec_type
= lp_build_vec_type(h16
.type
);
284 u8n_vec_type
= lp_build_vec_type(u8n
.type
);
286 if (bld
->static_state
->normalized_coords
) {
287 /* s = s * width, t = t * height */
288 LLVMTypeRef coord_vec_type
= lp_build_vec_type(bld
->coord_type
);
289 LLVMValueRef fp_width
= LLVMBuildSIToFP(bld
->builder
, width_vec
,
291 s
= lp_build_mul(&bld
->coord_bld
, s
, fp_width
);
293 LLVMValueRef fp_height
= LLVMBuildSIToFP(bld
->builder
, height_vec
,
295 t
= lp_build_mul(&bld
->coord_bld
, t
, fp_height
);
297 LLVMValueRef fp_depth
= LLVMBuildSIToFP(bld
->builder
, depth_vec
,
299 r
= lp_build_mul(&bld
->coord_bld
, r
, fp_depth
);
304 /* scale coords by 256 (8 fractional bits) */
305 s
= lp_build_mul_imm(&bld
->coord_bld
, s
, 256);
307 t
= lp_build_mul_imm(&bld
->coord_bld
, t
, 256);
309 r
= lp_build_mul_imm(&bld
->coord_bld
, r
, 256);
311 /* convert float to int */
312 s
= LLVMBuildFPToSI(builder
, s
, i32_vec_type
, "");
314 t
= LLVMBuildFPToSI(builder
, t
, i32_vec_type
, "");
316 r
= LLVMBuildFPToSI(builder
, r
, i32_vec_type
, "");
318 /* compute floor (shift right 8) */
319 i32_c8
= lp_build_const_int_vec(i32
.type
, 8);
320 s_ipart
= LLVMBuildAShr(builder
, s
, i32_c8
, "");
322 t_ipart
= LLVMBuildAShr(builder
, t
, i32_c8
, "");
324 r_ipart
= LLVMBuildAShr(builder
, r
, i32_c8
, "");
326 /* get pixel, row, image strides */
327 x_stride
= lp_build_const_vec(bld
->uint_coord_bld
.type
,
328 bld
->format_desc
->block
.bits
/8);
330 /* Do texcoord wrapping, compute texel offset */
331 lp_build_sample_wrap_nearest_int(bld
,
332 bld
->format_desc
->block
.width
,
333 s_ipart
, width_vec
, x_stride
,
334 bld
->static_state
->pot_width
,
335 bld
->static_state
->wrap_s
,
336 &x_offset
, &x_subcoord
);
339 LLVMValueRef y_offset
;
340 lp_build_sample_wrap_nearest_int(bld
,
341 bld
->format_desc
->block
.height
,
342 t_ipart
, height_vec
, row_stride_vec
,
343 bld
->static_state
->pot_height
,
344 bld
->static_state
->wrap_t
,
345 &y_offset
, &y_subcoord
);
346 offset
= lp_build_add(&bld
->uint_coord_bld
, offset
, y_offset
);
348 LLVMValueRef z_offset
;
349 lp_build_sample_wrap_nearest_int(bld
,
350 1, /* block length (depth) */
351 r_ipart
, depth_vec
, img_stride_vec
,
352 bld
->static_state
->pot_height
,
353 bld
->static_state
->wrap_r
,
354 &z_offset
, &z_subcoord
);
355 offset
= lp_build_add(&bld
->uint_coord_bld
, offset
, z_offset
);
357 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
358 LLVMValueRef z_offset
;
359 /* The r coord is the cube face in [0,5] */
360 z_offset
= lp_build_mul(&bld
->uint_coord_bld
, r
, img_stride_vec
);
361 offset
= lp_build_add(&bld
->uint_coord_bld
, offset
, z_offset
);
366 * Fetch the pixels as 4 x 32bit (rgba order might differ):
368 * rgba0 rgba1 rgba2 rgba3
370 * bit cast them into 16 x u8
372 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
374 * unpack them into two 8 x i16:
376 * r0 g0 b0 a0 r1 g1 b1 a1
377 * r2 g2 b2 a2 r3 g3 b3 a3
379 * The higher 8 bits of the resulting elements will be zero.
384 if (util_format_is_rgba8_variant(bld
->format_desc
)) {
386 * Given the format is a rgba8, just read the pixels as is,
387 * without any swizzling. Swizzling will be done later.
389 rgba8
= lp_build_gather(bld
->builder
,
390 bld
->texel_type
.length
,
391 bld
->format_desc
->block
.bits
,
392 bld
->texel_type
.width
,
395 rgba8
= LLVMBuildBitCast(builder
, rgba8
, u8n_vec_type
, "");
398 rgba8
= lp_build_fetch_rgba_aos(bld
->builder
,
406 /* Expand one 4*rgba8 to two 2*rgba16 */
407 lp_build_unpack2(builder
, u8n
.type
, h16
.type
,
409 colors_lo
, colors_hi
);
415 * Sample a single texture image with (bi-)(tri-)linear sampling.
416 * Return filtered color as two vectors of 16-bit fixed point values.
419 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
420 LLVMValueRef width_vec
,
421 LLVMValueRef height_vec
,
422 LLVMValueRef depth_vec
,
423 LLVMValueRef row_stride_vec
,
424 LLVMValueRef img_stride_vec
,
425 LLVMValueRef data_ptr
,
429 LLVMValueRef
*colors_lo
,
430 LLVMValueRef
*colors_hi
)
432 const unsigned dims
= bld
->dims
;
433 LLVMBuilderRef builder
= bld
->builder
;
434 struct lp_build_context i32
, h16
, u8n
;
435 LLVMTypeRef i32_vec_type
, h16_vec_type
, u8n_vec_type
;
436 LLVMValueRef i32_c8
, i32_c128
, i32_c255
;
437 LLVMValueRef s_ipart
, s_fpart
, s_fpart_lo
, s_fpart_hi
;
438 LLVMValueRef t_ipart
, t_fpart
, t_fpart_lo
, t_fpart_hi
;
439 LLVMValueRef r_ipart
, r_fpart
, r_fpart_lo
, r_fpart_hi
;
440 LLVMValueRef x_stride
, y_stride
, z_stride
;
441 LLVMValueRef x_offset0
, x_offset1
;
442 LLVMValueRef y_offset0
, y_offset1
;
443 LLVMValueRef z_offset0
, z_offset1
;
444 LLVMValueRef offset
[2][2][2]; /* [z][y][x] */
445 LLVMValueRef x_subcoord
[2], y_subcoord
[2], z_subcoord
[2];
446 LLVMValueRef neighbors_lo
[2][2][2]; /* [z][y][x] */
447 LLVMValueRef neighbors_hi
[2][2][2]; /* [z][y][x] */
448 LLVMValueRef packed_lo
, packed_hi
;
453 lp_build_context_init(&i32
, builder
, lp_type_int_vec(32));
454 lp_build_context_init(&h16
, builder
, lp_type_ufixed(16));
455 lp_build_context_init(&u8n
, builder
, lp_type_unorm(8));
457 i32_vec_type
= lp_build_vec_type(i32
.type
);
458 h16_vec_type
= lp_build_vec_type(h16
.type
);
459 u8n_vec_type
= lp_build_vec_type(u8n
.type
);
461 if (bld
->static_state
->normalized_coords
) {
462 /* s = s * width, t = t * height */
463 LLVMTypeRef coord_vec_type
= lp_build_vec_type(bld
->coord_type
);
464 LLVMValueRef fp_width
= LLVMBuildSIToFP(bld
->builder
, width_vec
,
466 s
= lp_build_mul(&bld
->coord_bld
, s
, fp_width
);
468 LLVMValueRef fp_height
= LLVMBuildSIToFP(bld
->builder
, height_vec
,
470 t
= lp_build_mul(&bld
->coord_bld
, t
, fp_height
);
473 LLVMValueRef fp_depth
= LLVMBuildSIToFP(bld
->builder
, depth_vec
,
475 r
= lp_build_mul(&bld
->coord_bld
, r
, fp_depth
);
479 /* scale coords by 256 (8 fractional bits) */
480 s
= lp_build_mul_imm(&bld
->coord_bld
, s
, 256);
482 t
= lp_build_mul_imm(&bld
->coord_bld
, t
, 256);
484 r
= lp_build_mul_imm(&bld
->coord_bld
, r
, 256);
486 /* convert float to int */
487 s
= LLVMBuildFPToSI(builder
, s
, i32_vec_type
, "");
489 t
= LLVMBuildFPToSI(builder
, t
, i32_vec_type
, "");
491 r
= LLVMBuildFPToSI(builder
, r
, i32_vec_type
, "");
493 /* subtract 0.5 (add -128) */
494 i32_c128
= lp_build_const_int_vec(i32
.type
, -128);
495 s
= LLVMBuildAdd(builder
, s
, i32_c128
, "");
497 t
= LLVMBuildAdd(builder
, t
, i32_c128
, "");
500 r
= LLVMBuildAdd(builder
, r
, i32_c128
, "");
503 /* compute floor (shift right 8) */
504 i32_c8
= lp_build_const_int_vec(i32
.type
, 8);
505 s_ipart
= LLVMBuildAShr(builder
, s
, i32_c8
, "");
507 t_ipart
= LLVMBuildAShr(builder
, t
, i32_c8
, "");
509 r_ipart
= LLVMBuildAShr(builder
, r
, i32_c8
, "");
511 /* compute fractional part (AND with 0xff) */
512 i32_c255
= lp_build_const_int_vec(i32
.type
, 255);
513 s_fpart
= LLVMBuildAnd(builder
, s
, i32_c255
, "");
515 t_fpart
= LLVMBuildAnd(builder
, t
, i32_c255
, "");
517 r_fpart
= LLVMBuildAnd(builder
, r
, i32_c255
, "");
519 /* get pixel, row and image strides */
520 x_stride
= lp_build_const_vec(bld
->uint_coord_bld
.type
,
521 bld
->format_desc
->block
.bits
/8);
522 y_stride
= row_stride_vec
;
523 z_stride
= img_stride_vec
;
525 /* do texcoord wrapping and compute texel offsets */
526 lp_build_sample_wrap_linear_int(bld
,
527 bld
->format_desc
->block
.width
,
528 s_ipart
, width_vec
, x_stride
,
529 bld
->static_state
->pot_width
,
530 bld
->static_state
->wrap_s
,
531 &x_offset0
, &x_offset1
,
532 &x_subcoord
[0], &x_subcoord
[1]);
533 for (z
= 0; z
< 2; z
++) {
534 for (y
= 0; y
< 2; y
++) {
535 offset
[z
][y
][0] = x_offset0
;
536 offset
[z
][y
][1] = x_offset1
;
541 lp_build_sample_wrap_linear_int(bld
,
542 bld
->format_desc
->block
.height
,
543 t_ipart
, height_vec
, y_stride
,
544 bld
->static_state
->pot_height
,
545 bld
->static_state
->wrap_t
,
546 &y_offset0
, &y_offset1
,
547 &y_subcoord
[0], &y_subcoord
[1]);
549 for (z
= 0; z
< 2; z
++) {
550 for (x
= 0; x
< 2; x
++) {
551 offset
[z
][0][x
] = lp_build_add(&bld
->uint_coord_bld
,
552 offset
[z
][0][x
], y_offset0
);
553 offset
[z
][1][x
] = lp_build_add(&bld
->uint_coord_bld
,
554 offset
[z
][1][x
], y_offset1
);
560 lp_build_sample_wrap_linear_int(bld
,
561 bld
->format_desc
->block
.height
,
562 r_ipart
, depth_vec
, z_stride
,
563 bld
->static_state
->pot_depth
,
564 bld
->static_state
->wrap_r
,
565 &z_offset0
, &z_offset1
,
566 &z_subcoord
[0], &z_subcoord
[1]);
567 for (y
= 0; y
< 2; y
++) {
568 for (x
= 0; x
< 2; x
++) {
569 offset
[0][y
][x
] = lp_build_add(&bld
->uint_coord_bld
,
570 offset
[0][y
][x
], z_offset0
);
571 offset
[1][y
][x
] = lp_build_add(&bld
->uint_coord_bld
,
572 offset
[1][y
][x
], z_offset1
);
576 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
577 LLVMValueRef z_offset
;
578 z_offset
= lp_build_mul(&bld
->uint_coord_bld
, r
, img_stride_vec
);
579 for (y
= 0; y
< 2; y
++) {
580 for (x
= 0; x
< 2; x
++) {
581 /* The r coord is the cube face in [0,5] */
582 offset
[0][y
][x
] = lp_build_add(&bld
->uint_coord_bld
,
583 offset
[0][y
][x
], z_offset
);
589 * Transform 4 x i32 in
591 * s_fpart = {s0, s1, s2, s3}
595 * s_fpart = {00, s0, 00, s1, 00, s2, 00, s3}
599 * s_fpart_lo = {s0, s0, s0, s0, s1, s1, s1, s1}
600 * s_fpart_hi = {s2, s2, s2, s2, s3, s3, s3, s3}
602 * and likewise for t_fpart. There is no risk of loosing precision here
603 * since the fractional parts only use the lower 8bits.
605 s_fpart
= LLVMBuildBitCast(builder
, s_fpart
, h16_vec_type
, "");
607 t_fpart
= LLVMBuildBitCast(builder
, t_fpart
, h16_vec_type
, "");
609 r_fpart
= LLVMBuildBitCast(builder
, r_fpart
, h16_vec_type
, "");
612 LLVMTypeRef elem_type
= LLVMInt32Type();
613 LLVMValueRef shuffles_lo
[LP_MAX_VECTOR_LENGTH
];
614 LLVMValueRef shuffles_hi
[LP_MAX_VECTOR_LENGTH
];
615 LLVMValueRef shuffle_lo
;
616 LLVMValueRef shuffle_hi
;
618 for (j
= 0; j
< h16
.type
.length
; j
+= 4) {
619 #ifdef PIPE_ARCH_LITTLE_ENDIAN
620 unsigned subindex
= 0;
622 unsigned subindex
= 1;
626 index
= LLVMConstInt(elem_type
, j
/2 + subindex
, 0);
627 for (i
= 0; i
< 4; ++i
)
628 shuffles_lo
[j
+ i
] = index
;
630 index
= LLVMConstInt(elem_type
, h16
.type
.length
/2 + j
/2 + subindex
, 0);
631 for (i
= 0; i
< 4; ++i
)
632 shuffles_hi
[j
+ i
] = index
;
635 shuffle_lo
= LLVMConstVector(shuffles_lo
, h16
.type
.length
);
636 shuffle_hi
= LLVMConstVector(shuffles_hi
, h16
.type
.length
);
638 s_fpart_lo
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
,
640 s_fpart_hi
= LLVMBuildShuffleVector(builder
, s_fpart
, h16
.undef
,
643 t_fpart_lo
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
,
645 t_fpart_hi
= LLVMBuildShuffleVector(builder
, t_fpart
, h16
.undef
,
649 r_fpart_lo
= LLVMBuildShuffleVector(builder
, r_fpart
, h16
.undef
,
651 r_fpart_hi
= LLVMBuildShuffleVector(builder
, r_fpart
, h16
.undef
,
657 * Fetch the pixels as 4 x 32bit (rgba order might differ):
659 * rgba0 rgba1 rgba2 rgba3
661 * bit cast them into 16 x u8
663 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
665 * unpack them into two 8 x i16:
667 * r0 g0 b0 a0 r1 g1 b1 a1
668 * r2 g2 b2 a2 r3 g3 b3 a3
670 * The higher 8 bits of the resulting elements will be zero.
672 numj
= 1 + (dims
>= 2);
673 numk
= 1 + (dims
>= 3);
675 for (k
= 0; k
< numk
; k
++) {
676 for (j
= 0; j
< numj
; j
++) {
677 for (i
= 0; i
< 2; i
++) {
680 if (util_format_is_rgba8_variant(bld
->format_desc
)) {
682 * Given the format is a rgba8, just read the pixels as is,
683 * without any swizzling. Swizzling will be done later.
685 rgba8
= lp_build_gather(bld
->builder
,
686 bld
->texel_type
.length
,
687 bld
->format_desc
->block
.bits
,
688 bld
->texel_type
.width
,
689 data_ptr
, offset
[k
][j
][i
]);
691 rgba8
= LLVMBuildBitCast(builder
, rgba8
, u8n_vec_type
, "");
694 rgba8
= lp_build_fetch_rgba_aos(bld
->builder
,
697 data_ptr
, offset
[k
][j
][i
],
702 /* Expand one 4*rgba8 to two 2*rgba16 */
703 lp_build_unpack2(builder
, u8n
.type
, h16
.type
,
705 &neighbors_lo
[k
][j
][i
], &neighbors_hi
[k
][j
][i
]);
711 * Linear interpolation with 8.8 fixed point.
715 packed_lo
= lp_build_lerp(&h16
,
717 neighbors_lo
[0][0][0],
718 neighbors_lo
[0][0][1]);
720 packed_hi
= lp_build_lerp(&h16
,
722 neighbors_hi
[0][0][0],
723 neighbors_hi
[0][0][1]);
727 packed_lo
= lp_build_lerp_2d(&h16
,
728 s_fpart_lo
, t_fpart_lo
,
729 neighbors_lo
[0][0][0],
730 neighbors_lo
[0][0][1],
731 neighbors_lo
[0][1][0],
732 neighbors_lo
[0][1][1]);
734 packed_hi
= lp_build_lerp_2d(&h16
,
735 s_fpart_hi
, t_fpart_hi
,
736 neighbors_hi
[0][0][0],
737 neighbors_hi
[0][0][1],
738 neighbors_hi
[0][1][0],
739 neighbors_hi
[0][1][1]);
742 LLVMValueRef packed_lo2
, packed_hi2
;
744 /* lerp in the second z slice */
745 packed_lo2
= lp_build_lerp_2d(&h16
,
746 s_fpart_lo
, t_fpart_lo
,
747 neighbors_lo
[1][0][0],
748 neighbors_lo
[1][0][1],
749 neighbors_lo
[1][1][0],
750 neighbors_lo
[1][1][1]);
752 packed_hi2
= lp_build_lerp_2d(&h16
,
753 s_fpart_hi
, t_fpart_hi
,
754 neighbors_hi
[1][0][0],
755 neighbors_hi
[1][0][1],
756 neighbors_hi
[1][1][0],
757 neighbors_hi
[1][1][1]);
758 /* interp between two z slices */
759 packed_lo
= lp_build_lerp(&h16
, r_fpart_lo
,
760 packed_lo
, packed_lo2
);
761 packed_hi
= lp_build_lerp(&h16
, r_fpart_hi
,
762 packed_hi
, packed_hi2
);
766 *colors_lo
= packed_lo
;
767 *colors_hi
= packed_hi
;
772 * Sample the texture/mipmap using given image filter and mip filter.
773 * data0_ptr and data1_ptr point to the two mipmap levels to sample
774 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
775 * If we're using nearest miplevel sampling the '1' values will be null/unused.
778 lp_build_sample_mipmap(struct lp_build_sample_context
*bld
,
784 LLVMValueRef ilevel0
,
785 LLVMValueRef ilevel1
,
786 LLVMValueRef lod_fpart
,
787 LLVMValueRef colors_lo_var
,
788 LLVMValueRef colors_hi_var
)
790 LLVMBuilderRef builder
= bld
->builder
;
791 LLVMValueRef width0_vec
;
792 LLVMValueRef width1_vec
;
793 LLVMValueRef height0_vec
;
794 LLVMValueRef height1_vec
;
795 LLVMValueRef depth0_vec
;
796 LLVMValueRef depth1_vec
;
797 LLVMValueRef row_stride0_vec
;
798 LLVMValueRef row_stride1_vec
;
799 LLVMValueRef img_stride0_vec
;
800 LLVMValueRef img_stride1_vec
;
801 LLVMValueRef data_ptr0
;
802 LLVMValueRef data_ptr1
;
803 LLVMValueRef colors0_lo
, colors0_hi
;
804 LLVMValueRef colors1_lo
, colors1_hi
;
807 /* sample the first mipmap level */
808 lp_build_mipmap_level_sizes(bld
, ilevel0
,
809 &width0_vec
, &height0_vec
, &depth0_vec
,
810 &row_stride0_vec
, &img_stride0_vec
);
811 data_ptr0
= lp_build_get_mipmap_level(bld
, ilevel0
);
812 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
813 lp_build_sample_image_nearest(bld
,
814 width0_vec
, height0_vec
, depth0_vec
,
815 row_stride0_vec
, img_stride0_vec
,
817 &colors0_lo
, &colors0_hi
);
820 assert(img_filter
== PIPE_TEX_FILTER_LINEAR
);
821 lp_build_sample_image_linear(bld
,
822 width0_vec
, height0_vec
, depth0_vec
,
823 row_stride0_vec
, img_stride0_vec
,
825 &colors0_lo
, &colors0_hi
);
828 /* Store the first level's colors in the output variables */
829 LLVMBuildStore(builder
, colors0_lo
, colors_lo_var
);
830 LLVMBuildStore(builder
, colors0_hi
, colors_hi_var
);
832 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
833 LLVMValueRef h16_scale
= LLVMConstReal(LLVMFloatType(), 256.0);
834 LLVMTypeRef i32_type
= LLVMIntType(32);
835 struct lp_build_flow_context
*flow_ctx
;
836 struct lp_build_if_state if_ctx
;
837 LLVMValueRef need_lerp
;
839 flow_ctx
= lp_build_flow_create(builder
);
841 lod_fpart
= LLVMBuildFMul(builder
, lod_fpart
, h16_scale
, "");
842 lod_fpart
= LLVMBuildFPToSI(builder
, lod_fpart
, i32_type
, "lod_fpart.fixed16");
844 /* need_lerp = lod_fpart > 0 */
845 need_lerp
= LLVMBuildICmp(builder
, LLVMIntSGT
,
846 lod_fpart
, LLVMConstNull(i32_type
),
849 lp_build_if(&if_ctx
, flow_ctx
, builder
, need_lerp
);
851 struct lp_build_context h16_bld
;
853 lp_build_context_init(&h16_bld
, builder
, lp_type_ufixed(16));
855 /* sample the second mipmap level */
856 lp_build_mipmap_level_sizes(bld
, ilevel1
,
857 &width1_vec
, &height1_vec
, &depth1_vec
,
858 &row_stride1_vec
, &img_stride1_vec
);
859 data_ptr1
= lp_build_get_mipmap_level(bld
, ilevel1
);
860 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
861 lp_build_sample_image_nearest(bld
,
862 width1_vec
, height1_vec
, depth1_vec
,
863 row_stride1_vec
, img_stride1_vec
,
865 &colors1_lo
, &colors1_hi
);
868 lp_build_sample_image_linear(bld
,
869 width1_vec
, height1_vec
, depth1_vec
,
870 row_stride1_vec
, img_stride1_vec
,
872 &colors1_lo
, &colors1_hi
);
875 /* interpolate samples from the two mipmap levels */
877 lod_fpart
= LLVMBuildTrunc(builder
, lod_fpart
, h16_bld
.elem_type
, "");
878 lod_fpart
= lp_build_broadcast_scalar(&h16_bld
, lod_fpart
);
880 colors0_lo
= lp_build_lerp(&h16_bld
, lod_fpart
,
881 colors0_lo
, colors1_lo
);
882 colors0_hi
= lp_build_lerp(&h16_bld
, lod_fpart
,
883 colors0_hi
, colors1_hi
);
885 LLVMBuildStore(builder
, colors0_lo
, colors_lo_var
);
886 LLVMBuildStore(builder
, colors0_hi
, colors_hi_var
);
888 lp_build_endif(&if_ctx
);
890 lp_build_flow_destroy(flow_ctx
);
897 * Texture sampling in AoS format. Used when sampling common 32-bit/texel
898 * formats. 1D/2D/3D/cube texture supported. All mipmap sampling modes
899 * but only limited texture coord wrap modes.
902 lp_build_sample_aos(struct lp_build_sample_context
*bld
,
907 const LLVMValueRef
*ddx
,
908 const LLVMValueRef
*ddy
,
909 LLVMValueRef lod_bias
, /* optional */
910 LLVMValueRef explicit_lod
, /* optional */
911 LLVMValueRef texel_out
[4])
913 struct lp_build_context
*int_bld
= &bld
->int_bld
;
914 LLVMBuilderRef builder
= bld
->builder
;
915 const unsigned mip_filter
= bld
->static_state
->min_mip_filter
;
916 const unsigned min_filter
= bld
->static_state
->min_img_filter
;
917 const unsigned mag_filter
= bld
->static_state
->mag_img_filter
;
918 const unsigned dims
= bld
->dims
;
919 LLVMValueRef lod_ipart
= NULL
, lod_fpart
= NULL
;
920 LLVMValueRef ilevel0
, ilevel1
= NULL
;
921 LLVMValueRef packed
, packed_lo
, packed_hi
;
922 LLVMValueRef unswizzled
[4];
923 LLVMValueRef face_ddx
[4], face_ddy
[4];
924 struct lp_build_context h16_bld
;
925 LLVMTypeRef i32t
= LLVMInt32Type();
926 LLVMValueRef i32t_zero
= LLVMConstInt(i32t
, 0, 0);
928 /* we only support the common/simple wrap modes at this time */
929 assert(lp_is_simple_wrap_mode(bld
->static_state
->wrap_s
));
931 assert(lp_is_simple_wrap_mode(bld
->static_state
->wrap_t
));
933 assert(lp_is_simple_wrap_mode(bld
->static_state
->wrap_r
));
936 /* make 16-bit fixed-pt builder context */
937 lp_build_context_init(&h16_bld
, builder
, lp_type_ufixed(16));
939 /* cube face selection, compute pre-face coords, etc. */
940 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
941 LLVMValueRef face
, face_s
, face_t
;
942 lp_build_cube_lookup(bld
, s
, t
, r
, &face
, &face_s
, &face_t
);
943 s
= face_s
; /* vec */
944 t
= face_t
; /* vec */
945 /* use 'r' to indicate cube face */
946 r
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, face
); /* vec */
948 /* recompute ddx, ddy using the new (s,t) face texcoords */
949 face_ddx
[0] = lp_build_ddx(&bld
->coord_bld
, s
);
950 face_ddx
[1] = lp_build_ddx(&bld
->coord_bld
, t
);
953 face_ddy
[0] = lp_build_ddy(&bld
->coord_bld
, s
);
954 face_ddy
[1] = lp_build_ddy(&bld
->coord_bld
, t
);
962 * Compute the level of detail (float).
964 if (min_filter
!= mag_filter
||
965 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
966 /* Need to compute lod either to choose mipmap levels or to
967 * distinguish between minification/magnification with one mipmap level.
969 lp_build_lod_selector(bld
, unit
, ddx
, ddy
,
970 lod_bias
, explicit_lod
,
972 &lod_ipart
, &lod_fpart
);
974 lod_ipart
= i32t_zero
;
978 * Compute integer mipmap level(s) to fetch texels from: ilevel0, ilevel1
980 switch (mip_filter
) {
982 assert(0 && "bad mip_filter value in lp_build_sample_aos()");
984 case PIPE_TEX_MIPFILTER_NONE
:
985 /* always use mip level 0 */
986 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
987 /* XXX this is a work-around for an apparent bug in LLVM 2.7.
988 * We should be able to set ilevel0 = const(0) but that causes
989 * bad x86 code to be emitted.
992 lp_build_nearest_mip_level(bld
, unit
, lod_ipart
, &ilevel0
);
998 case PIPE_TEX_MIPFILTER_NEAREST
:
1000 lp_build_nearest_mip_level(bld
, unit
, lod_ipart
, &ilevel0
);
1002 case PIPE_TEX_MIPFILTER_LINEAR
:
1005 lp_build_linear_mip_levels(bld
, unit
,
1006 lod_ipart
, &lod_fpart
,
1007 &ilevel0
, &ilevel1
);
1012 * Get/interpolate texture colors.
1015 packed_lo
= lp_build_alloca(builder
, h16_bld
.vec_type
, "packed_lo");
1016 packed_hi
= lp_build_alloca(builder
, h16_bld
.vec_type
, "packed_hi");
1018 if (min_filter
== mag_filter
) {
1019 /* no need to distinquish between minification and magnification */
1020 lp_build_sample_mipmap(bld
,
1021 min_filter
, mip_filter
,
1023 ilevel0
, ilevel1
, lod_fpart
,
1024 packed_lo
, packed_hi
);
1027 /* Emit conditional to choose min image filter or mag image filter
1028 * depending on the lod being > 0 or <= 0, respectively.
1030 struct lp_build_flow_context
*flow_ctx
;
1031 struct lp_build_if_state if_ctx
;
1032 LLVMValueRef minify
;
1034 flow_ctx
= lp_build_flow_create(builder
);
1036 /* minify = lod >= 0.0 */
1037 minify
= LLVMBuildICmp(builder
, LLVMIntSGE
,
1038 lod_ipart
, int_bld
->zero
, "");
1040 lp_build_if(&if_ctx
, flow_ctx
, builder
, minify
);
1042 /* Use the minification filter */
1043 lp_build_sample_mipmap(bld
,
1044 min_filter
, mip_filter
,
1046 ilevel0
, ilevel1
, lod_fpart
,
1047 packed_lo
, packed_hi
);
1049 lp_build_else(&if_ctx
);
1051 /* Use the magnification filter */
1052 lp_build_sample_mipmap(bld
,
1053 mag_filter
, PIPE_TEX_MIPFILTER_NONE
,
1055 i32t_zero
, NULL
, NULL
,
1056 packed_lo
, packed_hi
);
1058 lp_build_endif(&if_ctx
);
1060 lp_build_flow_destroy(flow_ctx
);
1064 * combine the values stored in 'packed_lo' and 'packed_hi' variables
1067 packed
= lp_build_pack2(builder
,
1068 h16_bld
.type
, lp_type_unorm(8),
1069 LLVMBuildLoad(builder
, packed_lo
, ""),
1070 LLVMBuildLoad(builder
, packed_hi
, ""));
1073 * Convert to SoA and swizzle.
1075 lp_build_rgba8_to_f32_soa(builder
,
1077 packed
, unswizzled
);
1079 if (util_format_is_rgba8_variant(bld
->format_desc
)) {
1080 lp_build_format_swizzle_soa(bld
->format_desc
,
1082 unswizzled
, texel_out
);
1085 texel_out
[0] = unswizzled
[0];
1086 texel_out
[1] = unswizzled
[1];
1087 texel_out
[2] = unswizzled
[2];
1088 texel_out
[3] = unswizzled
[3];
1091 apply_sampler_swizzle(bld
, texel_out
);