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 * Generate code to fetch a texel from a texture at int coords (x, y, z).
63 * The computation depends on whether the texture is 1D, 2D or 3D.
64 * The result, texel, will be float vectors:
65 * texel[0] = red values
66 * texel[1] = green values
67 * texel[2] = blue values
68 * texel[3] = alpha values
71 lp_build_sample_texel_soa(struct lp_build_sample_context *bld,
79 LLVMValueRef y_stride,
80 LLVMValueRef z_stride,
81 LLVMValueRef data_ptr,
82 LLVMValueRef texel_out[4])
84 const struct lp_sampler_static_state *static_state = bld->static_state;
85 const unsigned dims = bld->dims;
86 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
87 LLVMBuilderRef builder = bld->gallivm->builder;
90 LLVMValueRef use_border = NULL;
92 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
93 if (lp_sampler_wrap_mode_uses_border_color(static_state->wrap_s,
94 static_state->min_img_filter,
95 static_state->mag_img_filter)) {
97 b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, x, int_coord_bld->zero);
98 b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, x, width);
99 use_border = LLVMBuildOr(builder, b1, b2, "b1_or_b2");
103 lp_sampler_wrap_mode_uses_border_color(static_state->wrap_t,
104 static_state->min_img_filter,
105 static_state->mag_img_filter)) {
107 b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, y, int_coord_bld->zero);
108 b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, y, height);
110 use_border = LLVMBuildOr(builder, use_border, b1, "ub_or_b1");
111 use_border = LLVMBuildOr(builder, use_border, b2, "ub_or_b2");
114 use_border = LLVMBuildOr(builder, b1, b2, "b1_or_b2");
119 lp_sampler_wrap_mode_uses_border_color(static_state->wrap_r,
120 static_state->min_img_filter,
121 static_state->mag_img_filter)) {
123 b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, z, int_coord_bld->zero);
124 b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, z, depth);
126 use_border = LLVMBuildOr(builder, use_border, b1, "ub_or_b1");
127 use_border = LLVMBuildOr(builder, use_border, b2, "ub_or_b2");
130 use_border = LLVMBuildOr(builder, b1, b2, "b1_or_b2");
134 /* convert x,y,z coords to linear offset from start of texture, in bytes */
135 lp_build_sample_offset(&bld->int_coord_bld,
137 x, y, z, y_stride, z_stride,
141 /* If we can sample the border color, it means that texcoords may
142 * lie outside the bounds of the texture image. We need to do
143 * something to prevent reading out of bounds and causing a segfault.
145 * Simply AND the texture coords with !use_border. This will cause
146 * coords which are out of bounds to become zero. Zero's guaranteed
147 * to be inside the texture image.
149 offset = lp_build_andnot(&bld->int_coord_bld, offset, use_border);
152 lp_build_fetch_rgba_soa(bld->gallivm,
160 * Note: if we find an app which frequently samples the texture border
161 * we might want to implement a true conditional here to avoid sampling
162 * the texture whenever possible (since that's quite a bit of code).
165 * texel = border_color;
168 * texel = sample_texture(coord);
170 * As it is now, we always sample the texture, then selectively replace
171 * the texel color results with the border color.
175 /* select texel color or border color depending on use_border */
176 LLVMValueRef border_color_ptr =
177 bld->dynamic_state->border_color(bld->dynamic_state,
180 for (chan = 0; chan < 4; chan++) {
181 LLVMValueRef border_chan =
182 lp_build_array_get(bld->gallivm, border_color_ptr,
183 lp_build_const_int32(bld->gallivm, chan));
184 LLVMValueRef border_chan_vec =
185 lp_build_broadcast_scalar(&bld->float_vec_bld, border_chan);
186 texel_out[chan] = lp_build_select(&bld->texel_bld, use_border,
187 border_chan_vec, texel_out[chan]);
191 apply_sampler_swizzle(bld, texel_out);
196 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
199 lp_build_coord_mirror(struct lp_build_sample_context *bld,
202 struct lp_build_context *coord_bld = &bld->coord_bld;
203 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
204 LLVMValueRef fract, flr, isOdd;
206 lp_build_ifloor_fract(coord_bld, coord, &flr, &fract);
208 /* isOdd = flr & 1 */
209 isOdd = LLVMBuildAnd(bld->gallivm->builder, flr, int_coord_bld->one, "");
211 /* make coord positive or negative depending on isOdd */
212 coord = lp_build_set_sign(coord_bld, fract, isOdd);
214 /* convert isOdd to float */
215 isOdd = lp_build_int_to_float(coord_bld, isOdd);
217 /* add isOdd to coord */
218 coord = lp_build_add(coord_bld, coord, isOdd);
225 * Build LLVM code for texture wrap mode for linear filtering.
226 * \param x0_out returns first integer texcoord
227 * \param x1_out returns second integer texcoord
228 * \param weight_out returns linear interpolation weight
231 lp_build_sample_wrap_linear(struct lp_build_sample_context *bld,
234 LLVMValueRef length_f,
237 LLVMValueRef *x0_out,
238 LLVMValueRef *x1_out,
239 LLVMValueRef *weight_out)
241 struct lp_build_context *coord_bld = &bld->coord_bld;
242 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
243 LLVMBuilderRef builder = bld->gallivm->builder;
244 LLVMValueRef half = lp_build_const_vec(bld->gallivm, coord_bld->type, 0.5);
245 LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length, int_coord_bld->one);
246 LLVMValueRef coord0, coord1, weight;
249 case PIPE_TEX_WRAP_REPEAT:
250 /* mul by size and subtract 0.5 */
251 coord = lp_build_mul(coord_bld, coord, length_f);
252 coord = lp_build_sub(coord_bld, coord, half);
253 /* convert to int, compute lerp weight */
254 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
257 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
258 coord0 = LLVMBuildAnd(builder, coord0, length_minus_one, "");
259 coord1 = LLVMBuildAnd(builder, coord1, length_minus_one, "");
262 /* Add a bias to the texcoord to handle negative coords */
263 LLVMValueRef bias = lp_build_mul_imm(int_coord_bld, length, 1024);
265 coord0 = LLVMBuildAdd(builder, coord0, bias, "");
266 coord0 = LLVMBuildURem(builder, coord0, length, "");
267 mask = lp_build_compare(bld->gallivm, int_coord_bld->type,
268 PIPE_FUNC_NOTEQUAL, coord0, length_minus_one);
269 coord1 = LLVMBuildAnd(builder,
270 lp_build_add(int_coord_bld, coord0, int_coord_bld->one),
275 case PIPE_TEX_WRAP_CLAMP:
276 if (bld->static_state->normalized_coords) {
277 /* scale coord to length */
278 coord = lp_build_mul(coord_bld, coord, length_f);
281 /* clamp to [0, length] */
282 coord = lp_build_clamp(coord_bld, coord, coord_bld->zero, length_f);
284 coord = lp_build_sub(coord_bld, coord, half);
286 /* convert to int, compute lerp weight */
287 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
288 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
291 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
293 struct lp_build_context abs_coord_bld = bld->coord_bld;
294 abs_coord_bld.type.sign = FALSE;
296 if (bld->static_state->normalized_coords) {
297 /* mul by tex size */
298 coord = lp_build_mul(coord_bld, coord, length_f);
300 /* clamp to length max */
301 coord = lp_build_min(coord_bld, coord, length_f);
303 coord = lp_build_sub(coord_bld, coord, half);
304 /* clamp to [0, length - 0.5] */
305 coord = lp_build_max(coord_bld, coord, coord_bld->zero);
306 /* convert to int, compute lerp weight */
307 lp_build_ifloor_fract(&abs_coord_bld, coord, &coord0, &weight);
308 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
309 /* coord1 = min(coord1, length-1) */
310 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
314 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
317 if (bld->static_state->normalized_coords) {
318 /* scale coord to length */
319 coord = lp_build_mul(coord_bld, coord, length_f);
321 /* was: clamp to [-0.5, length + 0.5], then sub 0.5 */
322 coord = lp_build_sub(coord_bld, coord, half);
323 min = lp_build_const_vec(bld->gallivm, coord_bld->type, -1.0F);
324 coord = lp_build_clamp(coord_bld, coord, min, length_f);
325 /* convert to int, compute lerp weight */
326 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
327 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
331 case PIPE_TEX_WRAP_MIRROR_REPEAT:
332 /* compute mirror function */
333 coord = lp_build_coord_mirror(bld, coord);
335 /* scale coord to length */
336 coord = lp_build_mul(coord_bld, coord, length_f);
337 coord = lp_build_sub(coord_bld, coord, half);
339 /* convert to int, compute lerp weight */
340 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
341 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
343 /* coord0 = max(coord0, 0) */
344 coord0 = lp_build_max(int_coord_bld, coord0, int_coord_bld->zero);
345 /* coord1 = min(coord1, length-1) */
346 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
349 case PIPE_TEX_WRAP_MIRROR_CLAMP:
350 coord = lp_build_abs(coord_bld, coord);
352 if (bld->static_state->normalized_coords) {
353 /* scale coord to length */
354 coord = lp_build_mul(coord_bld, coord, length_f);
357 /* clamp to [0, length] */
358 coord = lp_build_min(coord_bld, coord, length_f);
360 coord = lp_build_sub(coord_bld, coord, half);
362 /* convert to int, compute lerp weight */
363 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
364 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
367 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
369 LLVMValueRef min, max;
370 struct lp_build_context abs_coord_bld = bld->coord_bld;
371 abs_coord_bld.type.sign = FALSE;
372 coord = lp_build_abs(coord_bld, coord);
374 if (bld->static_state->normalized_coords) {
375 /* scale coord to length */
376 coord = lp_build_mul(coord_bld, coord, length_f);
379 /* clamp to [0.5, length - 0.5] */
381 max = lp_build_sub(coord_bld, length_f, min);
382 coord = lp_build_clamp(coord_bld, coord, min, max);
384 coord = lp_build_sub(coord_bld, coord, half);
386 /* convert to int, compute lerp weight */
387 lp_build_ifloor_fract(&abs_coord_bld, coord, &coord0, &weight);
388 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
392 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
394 coord = lp_build_abs(coord_bld, coord);
396 if (bld->static_state->normalized_coords) {
397 /* scale coord to length */
398 coord = lp_build_mul(coord_bld, coord, length_f);
401 /* was: clamp to [-0.5, length + 0.5] then sub 0.5 */
402 /* skip -0.5 clamp (always positive), do sub first */
403 coord = lp_build_sub(coord_bld, coord, half);
404 coord = lp_build_min(coord_bld, coord, length_f);
406 /* convert to int, compute lerp weight */
407 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
408 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
421 *weight_out = weight;
426 * Build LLVM code for texture wrap mode for nearest filtering.
427 * \param coord the incoming texcoord (nominally in [0,1])
428 * \param length the texture size along one dimension, as int vector
429 * \param is_pot if TRUE, length is a power of two
430 * \param wrap_mode one of PIPE_TEX_WRAP_x
433 lp_build_sample_wrap_nearest(struct lp_build_sample_context *bld,
436 LLVMValueRef length_f,
440 struct lp_build_context *coord_bld = &bld->coord_bld;
441 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
442 LLVMBuilderRef builder = bld->gallivm->builder;
443 LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length, int_coord_bld->one);
447 case PIPE_TEX_WRAP_REPEAT:
448 coord = lp_build_mul(coord_bld, coord, length_f);
449 icoord = lp_build_ifloor(coord_bld, coord);
451 icoord = LLVMBuildAnd(builder, icoord, length_minus_one, "");
453 /* Add a bias to the texcoord to handle negative coords */
454 LLVMValueRef bias = lp_build_mul_imm(int_coord_bld, length, 1024);
455 icoord = LLVMBuildAdd(builder, icoord, bias, "");
456 icoord = LLVMBuildURem(builder, icoord, length, "");
460 case PIPE_TEX_WRAP_CLAMP:
461 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
462 if (bld->static_state->normalized_coords) {
463 /* scale coord to length */
464 coord = lp_build_mul(coord_bld, coord, length_f);
468 /* use itrunc instead since we clamp to 0 anyway */
469 icoord = lp_build_itrunc(coord_bld, coord);
471 /* clamp to [0, length - 1]. */
472 icoord = lp_build_clamp(int_coord_bld, icoord, int_coord_bld->zero,
476 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
477 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
479 LLVMValueRef min, max;
481 if (bld->static_state->normalized_coords) {
482 /* scale coord to length */
483 coord = lp_build_mul(coord_bld, coord, length_f);
486 icoord = lp_build_ifloor(coord_bld, coord);
488 /* clamp to [-1, length] */
489 min = lp_build_negate(int_coord_bld, int_coord_bld->one);
491 icoord = lp_build_clamp(int_coord_bld, icoord, min, max);
495 case PIPE_TEX_WRAP_MIRROR_REPEAT:
496 /* compute mirror function */
497 coord = lp_build_coord_mirror(bld, coord);
499 /* scale coord to length */
500 assert(bld->static_state->normalized_coords);
501 coord = lp_build_mul(coord_bld, coord, length_f);
503 /* itrunc == ifloor here */
504 icoord = lp_build_itrunc(coord_bld, coord);
506 /* clamp to [0, length - 1] */
507 icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
510 case PIPE_TEX_WRAP_MIRROR_CLAMP:
511 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
512 coord = lp_build_abs(coord_bld, coord);
514 if (bld->static_state->normalized_coords) {
515 /* scale coord to length */
516 coord = lp_build_mul(coord_bld, coord, length_f);
519 /* itrunc == ifloor here */
520 icoord = lp_build_itrunc(coord_bld, coord);
522 /* clamp to [0, length - 1] */
523 icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
526 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
527 coord = lp_build_abs(coord_bld, coord);
529 if (bld->static_state->normalized_coords) {
530 /* scale coord to length */
531 coord = lp_build_mul(coord_bld, coord, length_f);
534 /* itrunc == ifloor here */
535 icoord = lp_build_itrunc(coord_bld, coord);
537 /* clamp to [0, length] */
538 icoord = lp_build_min(int_coord_bld, icoord, length);
551 * Generate code to sample a mipmap level with nearest filtering.
552 * If sampling a cube texture, r = cube face in [0,5].
555 lp_build_sample_image_nearest(struct lp_build_sample_context *bld,
558 LLVMValueRef row_stride_vec,
559 LLVMValueRef img_stride_vec,
560 LLVMValueRef data_ptr,
564 LLVMValueRef colors_out[4])
566 const unsigned dims = bld->dims;
567 LLVMValueRef width_vec;
568 LLVMValueRef height_vec;
569 LLVMValueRef depth_vec;
570 LLVMValueRef flt_size;
571 LLVMValueRef flt_width_vec;
572 LLVMValueRef flt_height_vec;
573 LLVMValueRef flt_depth_vec;
574 LLVMValueRef x, y, z;
576 lp_build_extract_image_sizes(bld,
580 &width_vec, &height_vec, &depth_vec);
582 flt_size = lp_build_int_to_float(&bld->float_size_bld, size);
584 lp_build_extract_image_sizes(bld,
585 bld->float_size_type,
588 &flt_width_vec, &flt_height_vec, &flt_depth_vec);
591 * Compute integer texcoords.
593 x = lp_build_sample_wrap_nearest(bld, s, width_vec, flt_width_vec,
594 bld->static_state->pot_width,
595 bld->static_state->wrap_s);
596 lp_build_name(x, "tex.x.wrapped");
599 y = lp_build_sample_wrap_nearest(bld, t, height_vec, flt_height_vec,
600 bld->static_state->pot_height,
601 bld->static_state->wrap_t);
602 lp_build_name(y, "tex.y.wrapped");
605 z = lp_build_sample_wrap_nearest(bld, r, depth_vec, flt_depth_vec,
606 bld->static_state->pot_depth,
607 bld->static_state->wrap_r);
608 lp_build_name(z, "tex.z.wrapped");
610 else if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
622 * Get texture colors.
624 lp_build_sample_texel_soa(bld, unit,
625 width_vec, height_vec, depth_vec,
627 row_stride_vec, img_stride_vec,
628 data_ptr, colors_out);
633 * Generate code to sample a mipmap level with linear filtering.
634 * If sampling a cube texture, r = cube face in [0,5].
637 lp_build_sample_image_linear(struct lp_build_sample_context *bld,
640 LLVMValueRef row_stride_vec,
641 LLVMValueRef img_stride_vec,
642 LLVMValueRef data_ptr,
646 LLVMValueRef colors_out[4])
648 const unsigned dims = bld->dims;
649 LLVMValueRef width_vec;
650 LLVMValueRef height_vec;
651 LLVMValueRef depth_vec;
652 LLVMValueRef flt_size;
653 LLVMValueRef flt_width_vec;
654 LLVMValueRef flt_height_vec;
655 LLVMValueRef flt_depth_vec;
656 LLVMValueRef x0, y0, z0, x1, y1, z1;
657 LLVMValueRef s_fpart, t_fpart, r_fpart;
658 LLVMValueRef neighbors[2][2][4];
661 lp_build_extract_image_sizes(bld,
665 &width_vec, &height_vec, &depth_vec);
667 flt_size = lp_build_int_to_float(&bld->float_size_bld, size);
669 lp_build_extract_image_sizes(bld,
670 bld->float_size_type,
673 &flt_width_vec, &flt_height_vec, &flt_depth_vec);
676 * Compute integer texcoords.
678 lp_build_sample_wrap_linear(bld, s, width_vec, flt_width_vec,
679 bld->static_state->pot_width,
680 bld->static_state->wrap_s,
682 lp_build_name(x0, "tex.x0.wrapped");
683 lp_build_name(x1, "tex.x1.wrapped");
686 lp_build_sample_wrap_linear(bld, t, height_vec, flt_height_vec,
687 bld->static_state->pot_height,
688 bld->static_state->wrap_t,
690 lp_build_name(y0, "tex.y0.wrapped");
691 lp_build_name(y1, "tex.y1.wrapped");
694 lp_build_sample_wrap_linear(bld, r, depth_vec, flt_depth_vec,
695 bld->static_state->pot_depth,
696 bld->static_state->wrap_r,
698 lp_build_name(z0, "tex.z0.wrapped");
699 lp_build_name(z1, "tex.z1.wrapped");
701 else if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
702 z0 = z1 = r; /* cube face */
711 y0 = y1 = t_fpart = NULL;
712 z0 = z1 = r_fpart = NULL;
716 * Get texture colors.
718 /* get x0/x1 texels */
719 lp_build_sample_texel_soa(bld, unit,
720 width_vec, height_vec, depth_vec,
722 row_stride_vec, img_stride_vec,
723 data_ptr, neighbors[0][0]);
724 lp_build_sample_texel_soa(bld, unit,
725 width_vec, height_vec, depth_vec,
727 row_stride_vec, img_stride_vec,
728 data_ptr, neighbors[0][1]);
731 /* Interpolate two samples from 1D image to produce one color */
732 for (chan = 0; chan < 4; chan++) {
733 colors_out[chan] = lp_build_lerp(&bld->texel_bld, s_fpart,
734 neighbors[0][0][chan],
735 neighbors[0][1][chan]);
740 LLVMValueRef colors0[4];
742 /* get x0/x1 texels at y1 */
743 lp_build_sample_texel_soa(bld, unit,
744 width_vec, height_vec, depth_vec,
746 row_stride_vec, img_stride_vec,
747 data_ptr, neighbors[1][0]);
748 lp_build_sample_texel_soa(bld, unit,
749 width_vec, height_vec, depth_vec,
751 row_stride_vec, img_stride_vec,
752 data_ptr, neighbors[1][1]);
754 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
755 for (chan = 0; chan < 4; chan++) {
756 colors0[chan] = lp_build_lerp_2d(&bld->texel_bld,
758 neighbors[0][0][chan],
759 neighbors[0][1][chan],
760 neighbors[1][0][chan],
761 neighbors[1][1][chan]);
765 LLVMValueRef neighbors1[2][2][4];
766 LLVMValueRef colors1[4];
768 /* get x0/x1/y0/y1 texels at z1 */
769 lp_build_sample_texel_soa(bld, unit,
770 width_vec, height_vec, depth_vec,
772 row_stride_vec, img_stride_vec,
773 data_ptr, neighbors1[0][0]);
774 lp_build_sample_texel_soa(bld, unit,
775 width_vec, height_vec, depth_vec,
777 row_stride_vec, img_stride_vec,
778 data_ptr, neighbors1[0][1]);
779 lp_build_sample_texel_soa(bld, unit,
780 width_vec, height_vec, depth_vec,
782 row_stride_vec, img_stride_vec,
783 data_ptr, neighbors1[1][0]);
784 lp_build_sample_texel_soa(bld, unit,
785 width_vec, height_vec, depth_vec,
787 row_stride_vec, img_stride_vec,
788 data_ptr, neighbors1[1][1]);
790 /* Bilinear interpolate the four samples from the second Z slice */
791 for (chan = 0; chan < 4; chan++) {
792 colors1[chan] = lp_build_lerp_2d(&bld->texel_bld,
794 neighbors1[0][0][chan],
795 neighbors1[0][1][chan],
796 neighbors1[1][0][chan],
797 neighbors1[1][1][chan]);
800 /* Linearly interpolate the two samples from the two 3D slices */
801 for (chan = 0; chan < 4; chan++) {
802 colors_out[chan] = lp_build_lerp(&bld->texel_bld,
804 colors0[chan], colors1[chan]);
809 for (chan = 0; chan < 4; chan++) {
810 colors_out[chan] = colors0[chan];
818 * Sample the texture/mipmap using given image filter and mip filter.
819 * data0_ptr and data1_ptr point to the two mipmap levels to sample
820 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
821 * If we're using nearest miplevel sampling the '1' values will be null/unused.
824 lp_build_sample_mipmap(struct lp_build_sample_context *bld,
831 LLVMValueRef ilevel0,
832 LLVMValueRef ilevel1,
833 LLVMValueRef lod_fpart,
834 LLVMValueRef *colors_out)
836 LLVMBuilderRef builder = bld->gallivm->builder;
839 LLVMValueRef row_stride0_vec;
840 LLVMValueRef row_stride1_vec;
841 LLVMValueRef img_stride0_vec;
842 LLVMValueRef img_stride1_vec;
843 LLVMValueRef data_ptr0;
844 LLVMValueRef data_ptr1;
845 LLVMValueRef colors0[4], colors1[4];
848 /* sample the first mipmap level */
849 lp_build_mipmap_level_sizes(bld, ilevel0,
851 &row_stride0_vec, &img_stride0_vec);
852 data_ptr0 = lp_build_get_mipmap_level(bld, ilevel0);
853 if (img_filter == PIPE_TEX_FILTER_NEAREST) {
854 lp_build_sample_image_nearest(bld, unit,
856 row_stride0_vec, img_stride0_vec,
861 assert(img_filter == PIPE_TEX_FILTER_LINEAR);
862 lp_build_sample_image_linear(bld, unit,
864 row_stride0_vec, img_stride0_vec,
869 /* Store the first level's colors in the output variables */
870 for (chan = 0; chan < 4; chan++) {
871 LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
874 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
875 struct lp_build_if_state if_ctx;
876 LLVMValueRef need_lerp;
878 /* need_lerp = lod_fpart > 0 */
879 need_lerp = LLVMBuildFCmp(builder, LLVMRealUGT,
884 lp_build_if(&if_ctx, bld->gallivm, need_lerp);
886 /* sample the second mipmap level */
887 lp_build_mipmap_level_sizes(bld, ilevel1,
889 &row_stride1_vec, &img_stride1_vec);
890 data_ptr1 = lp_build_get_mipmap_level(bld, ilevel1);
891 if (img_filter == PIPE_TEX_FILTER_NEAREST) {
892 lp_build_sample_image_nearest(bld, unit,
894 row_stride1_vec, img_stride1_vec,
899 lp_build_sample_image_linear(bld, unit,
901 row_stride1_vec, img_stride1_vec,
906 /* interpolate samples from the two mipmap levels */
908 lod_fpart = lp_build_broadcast_scalar(&bld->texel_bld, lod_fpart);
910 for (chan = 0; chan < 4; chan++) {
911 colors0[chan] = lp_build_lerp(&bld->texel_bld, lod_fpart,
912 colors0[chan], colors1[chan]);
913 LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
916 lp_build_endif(&if_ctx);
923 * General texture sampling codegen.
924 * This function handles texture sampling for all texture targets (1D,
925 * 2D, 3D, cube) and all filtering modes.
928 lp_build_sample_general(struct lp_build_sample_context *bld,
933 const LLVMValueRef *ddx,
934 const LLVMValueRef *ddy,
935 LLVMValueRef lod_bias, /* optional */
936 LLVMValueRef explicit_lod, /* optional */
937 LLVMValueRef *colors_out)
939 struct lp_build_context *int_bld = &bld->int_bld;
940 LLVMBuilderRef builder = bld->gallivm->builder;
941 const unsigned mip_filter = bld->static_state->min_mip_filter;
942 const unsigned min_filter = bld->static_state->min_img_filter;
943 const unsigned mag_filter = bld->static_state->mag_img_filter;
944 LLVMValueRef lod_ipart = NULL, lod_fpart = NULL;
945 LLVMValueRef ilevel0, ilevel1 = NULL;
946 LLVMValueRef face_ddx[4], face_ddy[4];
947 LLVMValueRef texels[4];
948 LLVMValueRef i32t_zero = lp_build_const_int32(bld->gallivm, 0);
952 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
953 mip_filter, min_filter, mag_filter);
957 * Choose cube face, recompute texcoords and derivatives for the chosen face.
959 if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
960 LLVMValueRef face, face_s, face_t;
961 lp_build_cube_lookup(bld, s, t, r, &face, &face_s, &face_t);
962 s = face_s; /* vec */
963 t = face_t; /* vec */
964 /* use 'r' to indicate cube face */
965 r = lp_build_broadcast_scalar(&bld->int_coord_bld, face); /* vec */
967 /* recompute ddx, ddy using the new (s,t) face texcoords */
968 face_ddx[0] = lp_build_scalar_ddx(&bld->coord_bld, s);
969 face_ddx[1] = lp_build_scalar_ddx(&bld->coord_bld, t);
972 face_ddy[0] = lp_build_scalar_ddy(&bld->coord_bld, s);
973 face_ddy[1] = lp_build_scalar_ddy(&bld->coord_bld, t);
981 * Compute the level of detail (float).
983 if (min_filter != mag_filter ||
984 mip_filter != PIPE_TEX_MIPFILTER_NONE) {
985 /* Need to compute lod either to choose mipmap levels or to
986 * distinguish between minification/magnification with one mipmap level.
988 lp_build_lod_selector(bld, unit, ddx, ddy,
989 lod_bias, explicit_lod,
991 &lod_ipart, &lod_fpart);
993 lod_ipart = i32t_zero;
997 * Compute integer mipmap level(s) to fetch texels from: ilevel0, ilevel1
999 switch (mip_filter) {
1001 assert(0 && "bad mip_filter value in lp_build_sample_soa()");
1003 case PIPE_TEX_MIPFILTER_NONE:
1004 /* always use mip level 0 */
1005 if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
1006 /* XXX this is a work-around for an apparent bug in LLVM 2.7.
1007 * We should be able to set ilevel0 = const(0) but that causes
1008 * bad x86 code to be emitted.
1011 lp_build_nearest_mip_level(bld, unit, lod_ipart, &ilevel0);
1014 ilevel0 = i32t_zero;
1017 case PIPE_TEX_MIPFILTER_NEAREST:
1019 lp_build_nearest_mip_level(bld, unit, lod_ipart, &ilevel0);
1021 case PIPE_TEX_MIPFILTER_LINEAR:
1024 lp_build_linear_mip_levels(bld, unit,
1025 lod_ipart, &lod_fpart,
1026 &ilevel0, &ilevel1);
1031 * Get/interpolate texture colors.
1034 for (chan = 0; chan < 4; ++chan) {
1035 texels[chan] = lp_build_alloca(bld->gallivm, bld->texel_bld.vec_type, "");
1036 lp_build_name(texels[chan], "sampler%u_texel_%c_var", unit, "xyzw"[chan]);
1039 if (min_filter == mag_filter) {
1040 /* no need to distinquish between minification and magnification */
1041 lp_build_sample_mipmap(bld, unit,
1042 min_filter, mip_filter,
1044 ilevel0, ilevel1, lod_fpart,
1048 /* Emit conditional to choose min image filter or mag image filter
1049 * depending on the lod being > 0 or <= 0, respectively.
1051 struct lp_build_if_state if_ctx;
1052 LLVMValueRef minify;
1054 /* minify = lod >= 0.0 */
1055 minify = LLVMBuildICmp(builder, LLVMIntSGE,
1056 lod_ipart, int_bld->zero, "");
1058 lp_build_if(&if_ctx, bld->gallivm, minify);
1060 /* Use the minification filter */
1061 lp_build_sample_mipmap(bld, unit,
1062 min_filter, mip_filter,
1064 ilevel0, ilevel1, lod_fpart,
1067 lp_build_else(&if_ctx);
1069 /* Use the magnification filter */
1070 lp_build_sample_mipmap(bld, unit,
1071 mag_filter, PIPE_TEX_MIPFILTER_NONE,
1073 i32t_zero, NULL, NULL,
1076 lp_build_endif(&if_ctx);
1079 for (chan = 0; chan < 4; ++chan) {
1080 colors_out[chan] = LLVMBuildLoad(builder, texels[chan], "");
1081 lp_build_name(colors_out[chan], "sampler%u_texel_%c", unit, "xyzw"[chan]);
1087 * Do shadow test/comparison.
1088 * \param p the texcoord Z (aka R, aka P) component
1089 * \param texel the texel to compare against (use the X channel)
1092 lp_build_sample_compare(struct lp_build_sample_context *bld,
1094 LLVMValueRef texel[4])
1096 struct lp_build_context *texel_bld = &bld->texel_bld;
1097 LLVMBuilderRef builder = bld->gallivm->builder;
1099 const unsigned chan = 0;
1101 if (bld->static_state->compare_mode == PIPE_TEX_COMPARE_NONE)
1106 LLVMValueRef indx = lp_build_const_int32(bld->gallivm, 0);
1107 LLVMValueRef coord = LLVMBuildExtractElement(builder, p, indx, "");
1108 LLVMValueRef tex = LLVMBuildExtractElement(builder, texel[chan], indx, "");
1109 lp_build_printf(bld->gallivm, "shadow compare coord %f to texture %f\n",
1113 /* result = (p FUNC texel) ? 1 : 0 */
1114 res = lp_build_cmp(texel_bld, bld->static_state->compare_func,
1116 res = lp_build_select(texel_bld, res, texel_bld->one, texel_bld->zero);
1118 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1122 texel[3] = texel_bld->one;
1127 * Just set texels to white instead of actually sampling the texture.
1131 lp_build_sample_nop(struct gallivm_state *gallivm, struct lp_type type,
1132 LLVMValueRef texel_out[4])
1134 LLVMValueRef one = lp_build_one(gallivm, type);
1137 for (chan = 0; chan < 4; chan++) {
1138 texel_out[chan] = one;
1144 * Build texture sampling code.
1145 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1147 * \param type vector float type to use for coords, etc.
1148 * \param ddx partial derivatives of (s,t,r,q) with respect to x
1149 * \param ddy partial derivatives of (s,t,r,q) with respect to y
1152 lp_build_sample_soa(struct gallivm_state *gallivm,
1153 const struct lp_sampler_static_state *static_state,
1154 struct lp_sampler_dynamic_state *dynamic_state,
1155 struct lp_type type,
1157 unsigned num_coords,
1158 const LLVMValueRef *coords,
1159 const LLVMValueRef ddx[4],
1160 const LLVMValueRef ddy[4],
1161 LLVMValueRef lod_bias, /* optional */
1162 LLVMValueRef explicit_lod, /* optional */
1163 LLVMValueRef texel_out[4])
1165 unsigned dims = texture_dims(static_state->target);
1166 struct lp_build_sample_context bld;
1167 LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context);
1168 LLVMBuilderRef builder = gallivm->builder;
1172 struct lp_type float_vec_type;
1175 enum pipe_format fmt = static_state->format;
1176 debug_printf("Sample from %s\n", util_format_name(fmt));
1179 assert(type.floating);
1181 /* Setup our build context */
1182 memset(&bld, 0, sizeof bld);
1183 bld.gallivm = gallivm;
1184 bld.static_state = static_state;
1185 bld.dynamic_state = dynamic_state;
1186 bld.format_desc = util_format_description(static_state->format);
1189 bld.float_type = lp_type_float(32);
1190 bld.int_type = lp_type_int(32);
1191 bld.coord_type = type;
1192 bld.int_coord_type = lp_int_type(type);
1193 bld.float_size_type = lp_type_float(32);
1194 bld.float_size_type.length = dims > 1 ? 4 : 1;
1195 bld.int_size_type = lp_int_type(bld.float_size_type);
1196 bld.texel_type = type;
1198 float_vec_type = lp_type_float_vec(32);
1200 lp_build_context_init(&bld.float_bld, gallivm, bld.float_type);
1201 lp_build_context_init(&bld.float_vec_bld, gallivm, float_vec_type);
1202 lp_build_context_init(&bld.int_bld, gallivm, bld.int_type);
1203 lp_build_context_init(&bld.coord_bld, gallivm, bld.coord_type);
1204 lp_build_context_init(&bld.int_coord_bld, gallivm, bld.int_coord_type);
1205 lp_build_context_init(&bld.int_size_bld, gallivm, bld.int_size_type);
1206 lp_build_context_init(&bld.float_size_bld, gallivm, bld.float_size_type);
1207 lp_build_context_init(&bld.texel_bld, gallivm, bld.texel_type);
1209 /* Get the dynamic state */
1210 bld.width = dynamic_state->width(dynamic_state, gallivm, unit);
1211 bld.height = dynamic_state->height(dynamic_state, gallivm, unit);
1212 bld.depth = dynamic_state->depth(dynamic_state, gallivm, unit);
1213 bld.row_stride_array = dynamic_state->row_stride(dynamic_state, gallivm, unit);
1214 bld.img_stride_array = dynamic_state->img_stride(dynamic_state, gallivm, unit);
1215 bld.data_array = dynamic_state->data_ptr(dynamic_state, gallivm, unit);
1216 /* Note that data_array is an array[level] of pointers to texture images */
1222 /* width, height, depth as single int vector */
1224 bld.int_size = bld.width;
1227 bld.int_size = LLVMBuildInsertElement(builder, bld.int_size_bld.undef,
1228 bld.width, LLVMConstInt(i32t, 0, 0), "");
1230 bld.int_size = LLVMBuildInsertElement(builder, bld.int_size,
1231 bld.height, LLVMConstInt(i32t, 1, 0), "");
1233 bld.int_size = LLVMBuildInsertElement(builder, bld.int_size,
1234 bld.depth, LLVMConstInt(i32t, 2, 0), "");
1240 /* For debug: no-op texture sampling */
1241 lp_build_sample_nop(gallivm, bld.texel_type, texel_out);
1243 else if (util_format_fits_8unorm(bld.format_desc) &&
1244 lp_is_simple_wrap_mode(static_state->wrap_s) &&
1245 lp_is_simple_wrap_mode(static_state->wrap_t)) {
1246 /* do sampling/filtering with fixed pt arithmetic */
1247 lp_build_sample_aos(&bld, unit, s, t, r, ddx, ddy,
1248 lod_bias, explicit_lod,
1253 if ((gallivm_debug & GALLIVM_DEBUG_PERF) &&
1254 util_format_fits_8unorm(bld.format_desc)) {
1255 debug_printf("%s: using floating point linear filtering for %s\n",
1256 __FUNCTION__, bld.format_desc->short_name);
1257 debug_printf(" min_img %d mag_img %d mip %d wraps %d wrapt %d\n",
1258 static_state->min_img_filter,
1259 static_state->mag_img_filter,
1260 static_state->min_mip_filter,
1261 static_state->wrap_s,
1262 static_state->wrap_t);
1265 lp_build_sample_general(&bld, unit, s, t, r, ddx, ddy,
1266 lod_bias, explicit_lod,
1270 lp_build_sample_compare(&bld, r, texel_out);