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 "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_bitarit.h"
49 #include "lp_bld_logic.h"
50 #include "lp_bld_printf.h"
51 #include "lp_bld_swizzle.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_sample_aos.h"
57 #include "lp_bld_struct.h"
58 #include "lp_bld_quad.h"
62 * Does the given texture wrap mode allow sampling the texture border color?
63 * XXX maybe move this into gallium util code.
66 wrap_mode_uses_border_color(unsigned mode
)
69 case PIPE_TEX_WRAP_REPEAT
:
70 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
71 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
72 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
74 case PIPE_TEX_WRAP_CLAMP
:
75 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
76 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
77 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
80 assert(0 && "unexpected wrap mode");
87 * Generate code to fetch a texel from a texture at int coords (x, y, z).
88 * The computation depends on whether the texture is 1D, 2D or 3D.
89 * The result, texel, will be float vectors:
90 * texel[0] = red values
91 * texel[1] = green values
92 * texel[2] = blue values
93 * texel[3] = alpha values
96 lp_build_sample_texel_soa(struct lp_build_sample_context
*bld
,
104 LLVMValueRef y_stride
,
105 LLVMValueRef z_stride
,
106 LLVMValueRef data_ptr
,
107 LLVMValueRef texel_out
[4])
109 const int dims
= texture_dims(bld
->static_state
->target
);
110 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
113 LLVMValueRef use_border
= NULL
;
115 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
116 if (wrap_mode_uses_border_color(bld
->static_state
->wrap_s
)) {
118 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, x
, int_coord_bld
->zero
);
119 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, x
, width
);
120 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
123 if (dims
>= 2 && wrap_mode_uses_border_color(bld
->static_state
->wrap_t
)) {
125 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, y
, int_coord_bld
->zero
);
126 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, y
, height
);
128 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
129 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
132 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
136 if (dims
== 3 && wrap_mode_uses_border_color(bld
->static_state
->wrap_r
)) {
138 b1
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_LESS
, z
, int_coord_bld
->zero
);
139 b2
= lp_build_cmp(int_coord_bld
, PIPE_FUNC_GEQUAL
, z
, depth
);
141 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b1
, "ub_or_b1");
142 use_border
= LLVMBuildOr(bld
->builder
, use_border
, b2
, "ub_or_b2");
145 use_border
= LLVMBuildOr(bld
->builder
, b1
, b2
, "b1_or_b2");
149 /* convert x,y,z coords to linear offset from start of texture, in bytes */
150 lp_build_sample_offset(&bld
->uint_coord_bld
,
152 x
, y
, z
, y_stride
, z_stride
,
156 /* If we can sample the border color, it means that texcoords may
157 * lie outside the bounds of the texture image. We need to do
158 * something to prevent reading out of bounds and causing a segfault.
160 * Simply AND the texture coords with !use_border. This will cause
161 * coords which are out of bounds to become zero. Zero's guaranteed
162 * to be inside the texture image.
164 offset
= lp_build_andnot(&bld
->uint_coord_bld
, offset
, use_border
);
167 lp_build_fetch_rgba_soa(bld
->builder
,
174 apply_sampler_swizzle(bld
, texel_out
);
177 * Note: if we find an app which frequently samples the texture border
178 * we might want to implement a true conditional here to avoid sampling
179 * the texture whenever possible (since that's quite a bit of code).
182 * texel = border_color;
185 * texel = sample_texture(coord);
187 * As it is now, we always sample the texture, then selectively replace
188 * the texel color results with the border color.
192 /* select texel color or border color depending on use_border */
193 LLVMValueRef border_color_ptr
=
194 bld
->dynamic_state
->border_color(bld
->dynamic_state
,
197 for (chan
= 0; chan
< 4; chan
++) {
198 LLVMValueRef border_chan
=
199 lp_build_array_get(bld
->builder
, border_color_ptr
,
200 lp_build_const_int32(chan
));
201 LLVMValueRef border_chan_vec
=
202 lp_build_broadcast_scalar(&bld
->float_vec_bld
, border_chan
);
203 texel_out
[chan
] = lp_build_select(&bld
->texel_bld
, use_border
,
204 border_chan_vec
, texel_out
[chan
]);
211 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
214 lp_build_coord_mirror(struct lp_build_sample_context
*bld
,
217 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
218 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
219 LLVMValueRef fract
, flr
, isOdd
;
221 /* fract = coord - floor(coord) */
222 fract
= lp_build_sub(coord_bld
, coord
, lp_build_floor(coord_bld
, coord
));
224 /* flr = ifloor(coord); */
225 flr
= lp_build_ifloor(coord_bld
, coord
);
227 /* isOdd = flr & 1 */
228 isOdd
= LLVMBuildAnd(bld
->builder
, flr
, int_coord_bld
->one
, "");
230 /* make coord positive or negative depending on isOdd */
231 coord
= lp_build_set_sign(coord_bld
, fract
, isOdd
);
233 /* convert isOdd to float */
234 isOdd
= lp_build_int_to_float(coord_bld
, isOdd
);
236 /* add isOdd to coord */
237 coord
= lp_build_add(coord_bld
, coord
, isOdd
);
244 * Build LLVM code for texture wrap mode for linear filtering.
245 * \param x0_out returns first integer texcoord
246 * \param x1_out returns second integer texcoord
247 * \param weight_out returns linear interpolation weight
250 lp_build_sample_wrap_linear(struct lp_build_sample_context
*bld
,
255 LLVMValueRef
*x0_out
,
256 LLVMValueRef
*x1_out
,
257 LLVMValueRef
*weight_out
)
259 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
260 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
261 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
262 LLVMValueRef half
= lp_build_const_vec(coord_bld
->type
, 0.5);
263 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
264 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
265 LLVMValueRef coord0
, coord1
, weight
;
268 case PIPE_TEX_WRAP_REPEAT
:
269 /* mul by size and subtract 0.5 */
270 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
271 coord
= lp_build_sub(coord_bld
, coord
, half
);
273 coord0
= lp_build_ifloor(coord_bld
, coord
);
274 coord1
= lp_build_add(uint_coord_bld
, coord0
, uint_coord_bld
->one
);
275 /* compute lerp weight */
276 weight
= lp_build_fract(coord_bld
, coord
);
279 coord0
= LLVMBuildAnd(bld
->builder
, coord0
, length_minus_one
, "");
280 coord1
= LLVMBuildAnd(bld
->builder
, coord1
, length_minus_one
, "");
283 /* Signed remainder won't give the right results for negative
284 * dividends but unsigned remainder does.*/
285 coord0
= LLVMBuildURem(bld
->builder
, coord0
, length
, "");
286 coord1
= LLVMBuildURem(bld
->builder
, coord1
, length
, "");
290 case PIPE_TEX_WRAP_CLAMP
:
291 if (bld
->static_state
->normalized_coords
) {
292 /* scale coord to length */
293 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
296 /* clamp to [0, length] */
297 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, length_f
);
299 coord
= lp_build_sub(coord_bld
, coord
, half
);
301 weight
= lp_build_fract(coord_bld
, coord
);
302 coord0
= lp_build_ifloor(coord_bld
, coord
);
303 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
306 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
307 if (bld
->static_state
->normalized_coords
) {
309 coord
= lp_build_clamp(coord_bld
, coord
, coord_bld
->zero
, coord_bld
->one
);
310 /* mul by tex size and subtract 0.5 */
311 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
312 coord
= lp_build_sub(coord_bld
, coord
, half
);
315 LLVMValueRef min
, max
;
316 /* clamp to [0.5, length - 0.5] */
318 max
= lp_build_sub(coord_bld
, length_f
, min
);
319 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
321 /* compute lerp weight */
322 weight
= lp_build_fract(coord_bld
, coord
);
323 /* coord0 = floor(coord); */
324 coord0
= lp_build_ifloor(coord_bld
, coord
);
325 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
326 /* coord0 = max(coord0, 0) */
327 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
328 /* coord1 = min(coord1, length-1) */
329 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
332 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
334 LLVMValueRef min
, max
;
335 if (bld
->static_state
->normalized_coords
) {
336 /* scale coord to length */
337 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
339 /* clamp to [-0.5, length + 0.5] */
340 min
= lp_build_const_vec(coord_bld
->type
, -0.5F
);
341 max
= lp_build_sub(coord_bld
, length_f
, min
);
342 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
343 coord
= lp_build_sub(coord_bld
, coord
, half
);
344 /* compute lerp weight */
345 weight
= lp_build_fract(coord_bld
, coord
);
347 coord0
= lp_build_ifloor(coord_bld
, coord
);
348 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
352 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
353 /* compute mirror function */
354 coord
= lp_build_coord_mirror(bld
, coord
);
356 /* scale coord to length */
357 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
358 coord
= lp_build_sub(coord_bld
, coord
, half
);
360 /* compute lerp weight */
361 weight
= lp_build_fract(coord_bld
, coord
);
363 /* convert to int coords */
364 coord0
= lp_build_ifloor(coord_bld
, coord
);
365 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
367 /* coord0 = max(coord0, 0) */
368 coord0
= lp_build_max(int_coord_bld
, coord0
, int_coord_bld
->zero
);
369 /* coord1 = min(coord1, length-1) */
370 coord1
= lp_build_min(int_coord_bld
, coord1
, length_minus_one
);
373 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
374 coord
= lp_build_abs(coord_bld
, coord
);
376 if (bld
->static_state
->normalized_coords
) {
377 /* scale coord to length */
378 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
381 /* clamp to [0, length] */
382 coord
= lp_build_min(coord_bld
, coord
, length_f
);
384 coord
= lp_build_sub(coord_bld
, coord
, half
);
386 weight
= lp_build_fract(coord_bld
, coord
);
387 coord0
= lp_build_ifloor(coord_bld
, coord
);
388 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
391 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
393 LLVMValueRef min
, max
;
395 coord
= lp_build_abs(coord_bld
, coord
);
397 if (bld
->static_state
->normalized_coords
) {
398 /* scale coord to length */
399 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
402 /* clamp to [0.5, length - 0.5] */
404 max
= lp_build_sub(coord_bld
, length_f
, min
);
405 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
407 coord
= lp_build_sub(coord_bld
, coord
, half
);
409 weight
= lp_build_fract(coord_bld
, coord
);
410 coord0
= lp_build_ifloor(coord_bld
, coord
);
411 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
415 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
417 LLVMValueRef min
, max
;
419 coord
= lp_build_abs(coord_bld
, coord
);
421 if (bld
->static_state
->normalized_coords
) {
422 /* scale coord to length */
423 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
426 /* clamp to [-0.5, length + 0.5] */
427 min
= lp_build_negate(coord_bld
, half
);
428 max
= lp_build_sub(coord_bld
, length_f
, min
);
429 coord
= lp_build_clamp(coord_bld
, coord
, min
, max
);
431 coord
= lp_build_sub(coord_bld
, coord
, half
);
433 weight
= lp_build_fract(coord_bld
, coord
);
434 coord0
= lp_build_ifloor(coord_bld
, coord
);
435 coord1
= lp_build_add(int_coord_bld
, coord0
, int_coord_bld
->one
);
448 *weight_out
= weight
;
453 * Build LLVM code for texture wrap mode for nearest filtering.
454 * \param coord the incoming texcoord (nominally in [0,1])
455 * \param length the texture size along one dimension, as int vector
456 * \param is_pot if TRUE, length is a power of two
457 * \param wrap_mode one of PIPE_TEX_WRAP_x
460 lp_build_sample_wrap_nearest(struct lp_build_sample_context
*bld
,
466 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
467 struct lp_build_context
*int_coord_bld
= &bld
->int_coord_bld
;
468 struct lp_build_context
*uint_coord_bld
= &bld
->uint_coord_bld
;
469 LLVMValueRef length_f
= lp_build_int_to_float(coord_bld
, length
);
470 LLVMValueRef length_minus_one
= lp_build_sub(uint_coord_bld
, length
, uint_coord_bld
->one
);
474 case PIPE_TEX_WRAP_REPEAT
:
475 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
476 icoord
= lp_build_ifloor(coord_bld
, coord
);
478 icoord
= LLVMBuildAnd(bld
->builder
, icoord
, length_minus_one
, "");
480 /* Signed remainder won't give the right results for negative
481 * dividends but unsigned remainder does.*/
482 icoord
= LLVMBuildURem(bld
->builder
, icoord
, length
, "");
485 case PIPE_TEX_WRAP_CLAMP
:
486 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
487 if (bld
->static_state
->normalized_coords
) {
488 /* scale coord to length */
489 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
493 icoord
= lp_build_ifloor(coord_bld
, coord
);
495 /* clamp to [0, length - 1]. */
496 icoord
= lp_build_clamp(int_coord_bld
, icoord
, int_coord_bld
->zero
,
500 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
501 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
503 LLVMValueRef min
, max
;
505 if (bld
->static_state
->normalized_coords
) {
506 /* scale coord to length */
507 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
510 icoord
= lp_build_ifloor(coord_bld
, coord
);
512 /* clamp to [-1, length] */
513 min
= lp_build_negate(int_coord_bld
, int_coord_bld
->one
);
515 icoord
= lp_build_clamp(int_coord_bld
, icoord
, min
, max
);
519 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
520 /* compute mirror function */
521 coord
= lp_build_coord_mirror(bld
, coord
);
523 /* scale coord to length */
524 assert(bld
->static_state
->normalized_coords
);
525 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
527 icoord
= lp_build_ifloor(coord_bld
, coord
);
529 /* clamp to [0, length - 1] */
530 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
533 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
534 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
535 coord
= lp_build_abs(coord_bld
, coord
);
537 if (bld
->static_state
->normalized_coords
) {
538 /* scale coord to length */
539 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
542 icoord
= lp_build_ifloor(coord_bld
, coord
);
544 /* clamp to [0, length - 1] */
545 icoord
= lp_build_min(int_coord_bld
, icoord
, length_minus_one
);
548 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
549 coord
= lp_build_abs(coord_bld
, coord
);
551 if (bld
->static_state
->normalized_coords
) {
552 /* scale coord to length */
553 coord
= lp_build_mul(coord_bld
, coord
, length_f
);
556 icoord
= lp_build_ifloor(coord_bld
, coord
);
558 /* clamp to [0, length] */
559 icoord
= lp_build_min(int_coord_bld
, icoord
, length
);
572 * Generate code to sample a mipmap level with nearest filtering.
573 * If sampling a cube texture, r = cube face in [0,5].
576 lp_build_sample_image_nearest(struct lp_build_sample_context
*bld
,
578 LLVMValueRef width_vec
,
579 LLVMValueRef height_vec
,
580 LLVMValueRef depth_vec
,
581 LLVMValueRef row_stride_vec
,
582 LLVMValueRef img_stride_vec
,
583 LLVMValueRef data_ptr
,
587 LLVMValueRef colors_out
[4])
589 const int dims
= texture_dims(bld
->static_state
->target
);
590 LLVMValueRef x
, y
, z
;
593 * Compute integer texcoords.
595 x
= lp_build_sample_wrap_nearest(bld
, s
, width_vec
,
596 bld
->static_state
->pot_width
,
597 bld
->static_state
->wrap_s
);
598 lp_build_name(x
, "tex.x.wrapped");
601 y
= lp_build_sample_wrap_nearest(bld
, t
, height_vec
,
602 bld
->static_state
->pot_height
,
603 bld
->static_state
->wrap_t
);
604 lp_build_name(y
, "tex.y.wrapped");
607 z
= lp_build_sample_wrap_nearest(bld
, r
, depth_vec
,
608 bld
->static_state
->pot_depth
,
609 bld
->static_state
->wrap_r
);
610 lp_build_name(z
, "tex.z.wrapped");
612 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
624 * Get texture colors.
626 lp_build_sample_texel_soa(bld
, unit
,
627 width_vec
, height_vec
, depth_vec
,
629 row_stride_vec
, img_stride_vec
,
630 data_ptr
, colors_out
);
635 * Generate code to sample a mipmap level with linear filtering.
636 * If sampling a cube texture, r = cube face in [0,5].
639 lp_build_sample_image_linear(struct lp_build_sample_context
*bld
,
641 LLVMValueRef width_vec
,
642 LLVMValueRef height_vec
,
643 LLVMValueRef depth_vec
,
644 LLVMValueRef row_stride_vec
,
645 LLVMValueRef img_stride_vec
,
646 LLVMValueRef data_ptr
,
650 LLVMValueRef colors_out
[4])
652 const int dims
= texture_dims(bld
->static_state
->target
);
653 LLVMValueRef x0
, y0
, z0
, x1
, y1
, z1
;
654 LLVMValueRef s_fpart
, t_fpart
, r_fpart
;
655 LLVMValueRef neighbors
[2][2][4];
659 * Compute integer texcoords.
661 lp_build_sample_wrap_linear(bld
, s
, width_vec
,
662 bld
->static_state
->pot_width
,
663 bld
->static_state
->wrap_s
,
665 lp_build_name(x0
, "tex.x0.wrapped");
666 lp_build_name(x1
, "tex.x1.wrapped");
669 lp_build_sample_wrap_linear(bld
, t
, height_vec
,
670 bld
->static_state
->pot_height
,
671 bld
->static_state
->wrap_t
,
673 lp_build_name(y0
, "tex.y0.wrapped");
674 lp_build_name(y1
, "tex.y1.wrapped");
677 lp_build_sample_wrap_linear(bld
, r
, depth_vec
,
678 bld
->static_state
->pot_depth
,
679 bld
->static_state
->wrap_r
,
681 lp_build_name(z0
, "tex.z0.wrapped");
682 lp_build_name(z1
, "tex.z1.wrapped");
684 else if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
685 z0
= z1
= r
; /* cube face */
694 y0
= y1
= t_fpart
= NULL
;
695 z0
= z1
= r_fpart
= NULL
;
699 * Get texture colors.
701 /* get x0/x1 texels */
702 lp_build_sample_texel_soa(bld
, unit
,
703 width_vec
, height_vec
, depth_vec
,
705 row_stride_vec
, img_stride_vec
,
706 data_ptr
, neighbors
[0][0]);
707 lp_build_sample_texel_soa(bld
, unit
,
708 width_vec
, height_vec
, depth_vec
,
710 row_stride_vec
, img_stride_vec
,
711 data_ptr
, neighbors
[0][1]);
714 /* Interpolate two samples from 1D image to produce one color */
715 for (chan
= 0; chan
< 4; chan
++) {
716 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, s_fpart
,
717 neighbors
[0][0][chan
],
718 neighbors
[0][1][chan
]);
723 LLVMValueRef colors0
[4];
725 /* get x0/x1 texels at y1 */
726 lp_build_sample_texel_soa(bld
, unit
,
727 width_vec
, height_vec
, depth_vec
,
729 row_stride_vec
, img_stride_vec
,
730 data_ptr
, neighbors
[1][0]);
731 lp_build_sample_texel_soa(bld
, unit
,
732 width_vec
, height_vec
, depth_vec
,
734 row_stride_vec
, img_stride_vec
,
735 data_ptr
, neighbors
[1][1]);
737 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
738 for (chan
= 0; chan
< 4; chan
++) {
739 colors0
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
741 neighbors
[0][0][chan
],
742 neighbors
[0][1][chan
],
743 neighbors
[1][0][chan
],
744 neighbors
[1][1][chan
]);
748 LLVMValueRef neighbors1
[2][2][4];
749 LLVMValueRef colors1
[4];
751 /* get x0/x1/y0/y1 texels at z1 */
752 lp_build_sample_texel_soa(bld
, unit
,
753 width_vec
, height_vec
, depth_vec
,
755 row_stride_vec
, img_stride_vec
,
756 data_ptr
, neighbors1
[0][0]);
757 lp_build_sample_texel_soa(bld
, unit
,
758 width_vec
, height_vec
, depth_vec
,
760 row_stride_vec
, img_stride_vec
,
761 data_ptr
, neighbors1
[0][1]);
762 lp_build_sample_texel_soa(bld
, unit
,
763 width_vec
, height_vec
, depth_vec
,
765 row_stride_vec
, img_stride_vec
,
766 data_ptr
, neighbors1
[1][0]);
767 lp_build_sample_texel_soa(bld
, unit
,
768 width_vec
, height_vec
, depth_vec
,
770 row_stride_vec
, img_stride_vec
,
771 data_ptr
, neighbors1
[1][1]);
773 /* Bilinear interpolate the four samples from the second Z slice */
774 for (chan
= 0; chan
< 4; chan
++) {
775 colors1
[chan
] = lp_build_lerp_2d(&bld
->texel_bld
,
777 neighbors1
[0][0][chan
],
778 neighbors1
[0][1][chan
],
779 neighbors1
[1][0][chan
],
780 neighbors1
[1][1][chan
]);
783 /* Linearly interpolate the two samples from the two 3D slices */
784 for (chan
= 0; chan
< 4; chan
++) {
785 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
,
787 colors0
[chan
], colors1
[chan
]);
792 for (chan
= 0; chan
< 4; chan
++) {
793 colors_out
[chan
] = colors0
[chan
];
801 * Sample the texture/mipmap using given image filter and mip filter.
802 * data0_ptr and data1_ptr point to the two mipmap levels to sample
803 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
804 * If we're using nearest miplevel sampling the '1' values will be null/unused.
807 lp_build_sample_mipmap(struct lp_build_sample_context
*bld
,
814 LLVMValueRef lod_fpart
,
815 LLVMValueRef width0_vec
,
816 LLVMValueRef width1_vec
,
817 LLVMValueRef height0_vec
,
818 LLVMValueRef height1_vec
,
819 LLVMValueRef depth0_vec
,
820 LLVMValueRef depth1_vec
,
821 LLVMValueRef row_stride0_vec
,
822 LLVMValueRef row_stride1_vec
,
823 LLVMValueRef img_stride0_vec
,
824 LLVMValueRef img_stride1_vec
,
825 LLVMValueRef data_ptr0
,
826 LLVMValueRef data_ptr1
,
827 LLVMValueRef
*colors_out
)
829 LLVMValueRef colors0
[4], colors1
[4];
832 if (img_filter
== PIPE_TEX_FILTER_NEAREST
) {
833 /* sample the first mipmap level */
834 lp_build_sample_image_nearest(bld
, unit
,
835 width0_vec
, height0_vec
, depth0_vec
,
836 row_stride0_vec
, img_stride0_vec
,
837 data_ptr0
, s
, t
, r
, colors0
);
839 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
840 /* sample the second mipmap level */
841 lp_build_sample_image_nearest(bld
, unit
,
842 width1_vec
, height1_vec
, depth1_vec
,
843 row_stride1_vec
, img_stride1_vec
,
844 data_ptr1
, s
, t
, r
, colors1
);
848 assert(img_filter
== PIPE_TEX_FILTER_LINEAR
);
850 /* sample the first mipmap level */
851 lp_build_sample_image_linear(bld
, unit
,
852 width0_vec
, height0_vec
, depth0_vec
,
853 row_stride0_vec
, img_stride0_vec
,
854 data_ptr0
, s
, t
, r
, colors0
);
856 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
857 /* sample the second mipmap level */
858 lp_build_sample_image_linear(bld
, unit
,
859 width1_vec
, height1_vec
, depth1_vec
,
860 row_stride1_vec
, img_stride1_vec
,
861 data_ptr1
, s
, t
, r
, colors1
);
865 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
866 /* interpolate samples from the two mipmap levels */
867 for (chan
= 0; chan
< 4; chan
++) {
868 colors_out
[chan
] = lp_build_lerp(&bld
->texel_bld
, lod_fpart
,
869 colors0
[chan
], colors1
[chan
]);
873 /* use first/only level's colors */
874 for (chan
= 0; chan
< 4; chan
++) {
875 colors_out
[chan
] = colors0
[chan
];
883 * General texture sampling codegen.
884 * This function handles texture sampling for all texture targets (1D,
885 * 2D, 3D, cube) and all filtering modes.
888 lp_build_sample_general(struct lp_build_sample_context
*bld
,
893 const LLVMValueRef
*ddx
,
894 const LLVMValueRef
*ddy
,
895 LLVMValueRef lod_bias
, /* optional */
896 LLVMValueRef explicit_lod
, /* optional */
900 LLVMValueRef width_vec
,
901 LLVMValueRef height_vec
,
902 LLVMValueRef depth_vec
,
903 LLVMValueRef row_stride_array
,
904 LLVMValueRef img_stride_array
,
905 LLVMValueRef data_array
,
906 LLVMValueRef
*colors_out
)
908 struct lp_build_context
*float_bld
= &bld
->float_bld
;
909 const unsigned mip_filter
= bld
->static_state
->min_mip_filter
;
910 const unsigned min_filter
= bld
->static_state
->min_img_filter
;
911 const unsigned mag_filter
= bld
->static_state
->mag_img_filter
;
912 const int dims
= texture_dims(bld
->static_state
->target
);
913 LLVMValueRef lod
= NULL
, lod_fpart
= NULL
;
914 LLVMValueRef ilevel0
, ilevel1
= NULL
;
915 LLVMValueRef width0_vec
= NULL
, height0_vec
= NULL
, depth0_vec
= NULL
;
916 LLVMValueRef width1_vec
= NULL
, height1_vec
= NULL
, depth1_vec
= NULL
;
917 LLVMValueRef row_stride0_vec
= NULL
, row_stride1_vec
= NULL
;
918 LLVMValueRef img_stride0_vec
= NULL
, img_stride1_vec
= NULL
;
919 LLVMValueRef data_ptr0
, data_ptr1
= NULL
;
920 LLVMValueRef face_ddx
[4], face_ddy
[4];
923 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
924 mip_filter, min_filter, mag_filter);
928 * Choose cube face, recompute texcoords and derivatives for the chosen face.
930 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
931 LLVMValueRef face
, face_s
, face_t
;
932 lp_build_cube_lookup(bld
, s
, t
, r
, &face
, &face_s
, &face_t
);
933 s
= face_s
; /* vec */
934 t
= face_t
; /* vec */
935 /* use 'r' to indicate cube face */
936 r
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, face
); /* vec */
938 /* recompute ddx, ddy using the new (s,t) face texcoords */
939 face_ddx
[0] = lp_build_ddx(&bld
->coord_bld
, s
);
940 face_ddx
[1] = lp_build_ddx(&bld
->coord_bld
, t
);
943 face_ddy
[0] = lp_build_ddy(&bld
->coord_bld
, s
);
944 face_ddy
[1] = lp_build_ddy(&bld
->coord_bld
, t
);
952 * Compute the level of detail (float).
954 if (min_filter
!= mag_filter
||
955 mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
956 /* Need to compute lod either to choose mipmap levels or to
957 * distinguish between minification/magnification with one mipmap level.
959 lod
= lp_build_lod_selector(bld
, unit
, ddx
, ddy
,
960 lod_bias
, explicit_lod
,
961 width
, height
, depth
);
965 * Compute integer mipmap level(s) to fetch texels from.
967 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
) {
968 /* always use mip level 0 */
969 if (bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
970 /* XXX this is a work-around for an apparent bug in LLVM 2.7.
971 * We should be able to set ilevel0 = const(0) but that causes
972 * bad x86 code to be emitted.
974 lod
= lp_build_const_elem(bld
->coord_bld
.type
, 0.0);
975 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
978 ilevel0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
983 if (mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
984 lp_build_nearest_mip_level(bld
, unit
, lod
, &ilevel0
);
987 assert(mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
);
988 lp_build_linear_mip_levels(bld
, unit
, lod
, &ilevel0
, &ilevel1
,
990 lod_fpart
= lp_build_broadcast_scalar(&bld
->coord_bld
, lod_fpart
);
994 /* compute image size(s) of source mipmap level(s) */
995 lp_build_mipmap_level_sizes(bld
, dims
, width_vec
, height_vec
, depth_vec
,
997 row_stride_array
, img_stride_array
,
998 &width0_vec
, &width1_vec
,
999 &height0_vec
, &height1_vec
,
1000 &depth0_vec
, &depth1_vec
,
1001 &row_stride0_vec
, &row_stride1_vec
,
1002 &img_stride0_vec
, &img_stride1_vec
);
1005 * Get pointer(s) to image data for mipmap level(s).
1007 data_ptr0
= lp_build_get_mipmap_level(bld
, data_array
, ilevel0
);
1008 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
1009 data_ptr1
= lp_build_get_mipmap_level(bld
, data_array
, ilevel1
);
1013 * Get/interpolate texture colors.
1015 if (min_filter
== mag_filter
) {
1016 /* no need to distinquish between minification and magnification */
1017 lp_build_sample_mipmap(bld
, unit
,
1018 min_filter
, mip_filter
, s
, t
, r
, lod_fpart
,
1019 width0_vec
, width1_vec
,
1020 height0_vec
, height1_vec
,
1021 depth0_vec
, depth1_vec
,
1022 row_stride0_vec
, row_stride1_vec
,
1023 img_stride0_vec
, img_stride1_vec
,
1024 data_ptr0
, data_ptr1
,
1028 /* Emit conditional to choose min image filter or mag image filter
1029 * depending on the lod being >0 or <= 0, respectively.
1031 struct lp_build_flow_context
*flow_ctx
;
1032 struct lp_build_if_state if_ctx
;
1033 LLVMValueRef minify
;
1035 flow_ctx
= lp_build_flow_create(bld
->builder
);
1036 lp_build_flow_scope_begin(flow_ctx
);
1038 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[0]);
1039 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[1]);
1040 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[2]);
1041 lp_build_flow_scope_declare(flow_ctx
, &colors_out
[3]);
1043 /* minify = lod > 0.0 */
1044 minify
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
1045 lod
, float_bld
->zero
, "");
1047 lp_build_if(&if_ctx
, flow_ctx
, bld
->builder
, minify
);
1049 /* Use the minification filter */
1050 lp_build_sample_mipmap(bld
, unit
,
1051 min_filter
, mip_filter
,
1053 width0_vec
, width1_vec
,
1054 height0_vec
, height1_vec
,
1055 depth0_vec
, depth1_vec
,
1056 row_stride0_vec
, row_stride1_vec
,
1057 img_stride0_vec
, img_stride1_vec
,
1058 data_ptr0
, data_ptr1
,
1061 lp_build_else(&if_ctx
);
1063 /* Use the magnification filter */
1064 lp_build_sample_mipmap(bld
, unit
,
1065 mag_filter
, mip_filter
,
1067 width0_vec
, width1_vec
,
1068 height0_vec
, height1_vec
,
1069 depth0_vec
, depth1_vec
,
1070 row_stride0_vec
, row_stride1_vec
,
1071 img_stride0_vec
, img_stride1_vec
,
1072 data_ptr0
, data_ptr1
,
1075 lp_build_endif(&if_ctx
);
1077 lp_build_flow_scope_end(flow_ctx
);
1078 lp_build_flow_destroy(flow_ctx
);
1084 * Do shadow test/comparison.
1085 * \param p the texcoord Z (aka R, aka P) component
1086 * \param texel the texel to compare against (use the X channel)
1089 lp_build_sample_compare(struct lp_build_sample_context
*bld
,
1091 LLVMValueRef texel
[4])
1093 struct lp_build_context
*texel_bld
= &bld
->texel_bld
;
1095 const unsigned chan
= 0;
1097 if (bld
->static_state
->compare_mode
== PIPE_TEX_COMPARE_NONE
)
1102 LLVMValueRef indx
= lp_build_const_int32(0);
1103 LLVMValueRef coord
= LLVMBuildExtractElement(bld
->builder
, p
, indx
, "");
1104 LLVMValueRef tex
= LLVMBuildExtractElement(bld
->builder
,
1105 texel
[chan
], indx
, "");
1106 lp_build_printf(bld
->builder
, "shadow compare coord %f to texture %f\n",
1110 /* result = (p FUNC texel) ? 1 : 0 */
1111 res
= lp_build_cmp(texel_bld
, bld
->static_state
->compare_func
,
1113 res
= lp_build_select(texel_bld
, res
, texel_bld
->one
, texel_bld
->zero
);
1115 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1119 texel
[3] = texel_bld
->one
;
1124 * Just set texels to white instead of actually sampling the texture.
1128 lp_build_sample_nop(struct lp_type type
,
1129 LLVMValueRef texel_out
[4])
1131 LLVMValueRef one
= lp_build_one(type
);
1134 for (chan
= 0; chan
< 4; chan
++) {
1135 texel_out
[chan
] = one
;
1141 * Build texture sampling code.
1142 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1144 * \param type vector float type to use for coords, etc.
1145 * \param ddx partial derivatives of (s,t,r,q) with respect to x
1146 * \param ddy partial derivatives of (s,t,r,q) with respect to y
1149 lp_build_sample_soa(LLVMBuilderRef builder
,
1150 const struct lp_sampler_static_state
*static_state
,
1151 struct lp_sampler_dynamic_state
*dynamic_state
,
1152 struct lp_type type
,
1154 unsigned num_coords
,
1155 const LLVMValueRef
*coords
,
1156 const LLVMValueRef ddx
[4],
1157 const LLVMValueRef ddy
[4],
1158 LLVMValueRef lod_bias
, /* optional */
1159 LLVMValueRef explicit_lod
, /* optional */
1160 LLVMValueRef texel_out
[4])
1162 struct lp_build_sample_context bld
;
1163 LLVMValueRef width
, width_vec
;
1164 LLVMValueRef height
, height_vec
;
1165 LLVMValueRef depth
, depth_vec
;
1166 LLVMValueRef row_stride_array
, img_stride_array
;
1167 LLVMValueRef data_array
;
1171 struct lp_type float_vec_type
;
1174 enum pipe_format fmt
= static_state
->format
;
1175 debug_printf("Sample from %s\n", util_format_name(fmt
));
1178 assert(type
.floating
);
1180 /* Setup our build context */
1181 memset(&bld
, 0, sizeof bld
);
1182 bld
.builder
= builder
;
1183 bld
.static_state
= static_state
;
1184 bld
.dynamic_state
= dynamic_state
;
1185 bld
.format_desc
= util_format_description(static_state
->format
);
1187 bld
.float_type
= lp_type_float(32);
1188 bld
.int_type
= lp_type_int(32);
1189 bld
.coord_type
= type
;
1190 bld
.uint_coord_type
= lp_uint_type(type
);
1191 bld
.int_coord_type
= lp_int_type(type
);
1192 bld
.texel_type
= type
;
1194 float_vec_type
= lp_type_float_vec(32);
1196 lp_build_context_init(&bld
.float_bld
, builder
, bld
.float_type
);
1197 lp_build_context_init(&bld
.float_vec_bld
, builder
, float_vec_type
);
1198 lp_build_context_init(&bld
.int_bld
, builder
, bld
.int_type
);
1199 lp_build_context_init(&bld
.coord_bld
, builder
, bld
.coord_type
);
1200 lp_build_context_init(&bld
.uint_coord_bld
, builder
, bld
.uint_coord_type
);
1201 lp_build_context_init(&bld
.int_coord_bld
, builder
, bld
.int_coord_type
);
1202 lp_build_context_init(&bld
.texel_bld
, builder
, bld
.texel_type
);
1204 /* Get the dynamic state */
1205 width
= dynamic_state
->width(dynamic_state
, builder
, unit
);
1206 height
= dynamic_state
->height(dynamic_state
, builder
, unit
);
1207 depth
= dynamic_state
->depth(dynamic_state
, builder
, unit
);
1208 row_stride_array
= dynamic_state
->row_stride(dynamic_state
, builder
, unit
);
1209 img_stride_array
= dynamic_state
->img_stride(dynamic_state
, builder
, unit
);
1210 data_array
= dynamic_state
->data_ptr(dynamic_state
, builder
, unit
);
1211 /* Note that data_array is an array[level] of pointers to texture images */
1217 /* width, height, depth as uint vectors */
1218 width_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, width
);
1219 height_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, height
);
1220 depth_vec
= lp_build_broadcast_scalar(&bld
.uint_coord_bld
, depth
);
1223 /* For debug: no-op texture sampling */
1224 lp_build_sample_nop(bld
.texel_type
, texel_out
);
1226 else if (util_format_fits_8unorm(bld
.format_desc
) &&
1227 lp_is_simple_wrap_mode(static_state
->wrap_s
) &&
1228 lp_is_simple_wrap_mode(static_state
->wrap_t
)) {
1229 /* do sampling/filtering with fixed pt arithmetic */
1230 lp_build_sample_aos(&bld
, unit
, s
, t
, r
, ddx
, ddy
,
1231 lod_bias
, explicit_lod
,
1232 width
, height
, depth
,
1233 width_vec
, height_vec
, depth_vec
,
1234 row_stride_array
, img_stride_array
,
1235 data_array
, texel_out
);
1239 if ((gallivm_debug
& GALLIVM_DEBUG_PERF
) &&
1240 util_format_fits_8unorm(bld
.format_desc
)) {
1241 debug_printf("%s: using floating point linear filtering for %s\n",
1242 __FUNCTION__
, bld
.format_desc
->short_name
);
1243 debug_printf(" min_img %d mag_img %d mip %d wraps %d wrapt %d\n",
1244 static_state
->min_img_filter
,
1245 static_state
->mag_img_filter
,
1246 static_state
->min_mip_filter
,
1247 static_state
->wrap_s
,
1248 static_state
->wrap_t
);
1251 lp_build_sample_general(&bld
, unit
, s
, t
, r
, ddx
, ddy
,
1252 lod_bias
, explicit_lod
,
1253 width
, height
, depth
,
1254 width_vec
, height_vec
, depth_vec
,
1255 row_stride_array
, img_stride_array
,
1260 lp_build_sample_compare(&bld
, r
, texel_out
);