gallivm: fix different handling of [non]normalized coords in linear soa path
[profile/ivi/mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_sample_soa.c
1 /**************************************************************************
2  *
3  * Copyright 2009 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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.
25  *
26  **************************************************************************/
27
28 /**
29  * @file
30  * Texture sampling -- SoA.
31  *
32  * @author Jose Fonseca <jfonseca@vmware.com>
33  * @author Brian Paul <brianp@vmware.com>
34  */
35
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"
59
60
61 /**
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
69  */
70 static void
71 lp_build_sample_texel_soa(struct lp_build_sample_context *bld,
72                           unsigned unit,
73                           LLVMValueRef width,
74                           LLVMValueRef height,
75                           LLVMValueRef depth,
76                           LLVMValueRef x,
77                           LLVMValueRef y,
78                           LLVMValueRef z,
79                           LLVMValueRef y_stride,
80                           LLVMValueRef z_stride,
81                           LLVMValueRef data_ptr,
82                           LLVMValueRef texel_out[4])
83 {
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    LLVMValueRef offset;
88    LLVMValueRef i, j;
89    LLVMValueRef use_border = NULL;
90
91    /* use_border = x < 0 || x >= width || y < 0 || y >= height */
92    if (lp_sampler_wrap_mode_uses_border_color(static_state->wrap_s,
93                                               static_state->min_img_filter,
94                                               static_state->mag_img_filter)) {
95       LLVMValueRef b1, b2;
96       b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, x, int_coord_bld->zero);
97       b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, x, width);
98       use_border = LLVMBuildOr(bld->builder, b1, b2, "b1_or_b2");
99    }
100
101    if (dims >= 2 &&
102        lp_sampler_wrap_mode_uses_border_color(static_state->wrap_t,
103                                               static_state->min_img_filter,
104                                               static_state->mag_img_filter)) {
105       LLVMValueRef b1, b2;
106       b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, y, int_coord_bld->zero);
107       b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, y, height);
108       if (use_border) {
109          use_border = LLVMBuildOr(bld->builder, use_border, b1, "ub_or_b1");
110          use_border = LLVMBuildOr(bld->builder, use_border, b2, "ub_or_b2");
111       }
112       else {
113          use_border = LLVMBuildOr(bld->builder, b1, b2, "b1_or_b2");
114       }
115    }
116
117    if (dims == 3 &&
118        lp_sampler_wrap_mode_uses_border_color(static_state->wrap_r,
119                                               static_state->min_img_filter,
120                                               static_state->mag_img_filter)) {
121       LLVMValueRef b1, b2;
122       b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, z, int_coord_bld->zero);
123       b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, z, depth);
124       if (use_border) {
125          use_border = LLVMBuildOr(bld->builder, use_border, b1, "ub_or_b1");
126          use_border = LLVMBuildOr(bld->builder, use_border, b2, "ub_or_b2");
127       }
128       else {
129          use_border = LLVMBuildOr(bld->builder, b1, b2, "b1_or_b2");
130       }
131    }
132
133    /* convert x,y,z coords to linear offset from start of texture, in bytes */
134    lp_build_sample_offset(&bld->int_coord_bld,
135                           bld->format_desc,
136                           x, y, z, y_stride, z_stride,
137                           &offset, &i, &j);
138
139    if (use_border) {
140       /* If we can sample the border color, it means that texcoords may
141        * lie outside the bounds of the texture image.  We need to do
142        * something to prevent reading out of bounds and causing a segfault.
143        *
144        * Simply AND the texture coords with !use_border.  This will cause
145        * coords which are out of bounds to become zero.  Zero's guaranteed
146        * to be inside the texture image.
147        */
148       offset = lp_build_andnot(&bld->int_coord_bld, offset, use_border);
149    }
150
151    lp_build_fetch_rgba_soa(bld->builder,
152                            bld->format_desc,
153                            bld->texel_type,
154                            data_ptr, offset,
155                            i, j,
156                            texel_out);
157
158    /*
159     * Note: if we find an app which frequently samples the texture border
160     * we might want to implement a true conditional here to avoid sampling
161     * the texture whenever possible (since that's quite a bit of code).
162     * Ex:
163     *   if (use_border) {
164     *      texel = border_color;
165     *   }
166     *   else {
167     *      texel = sample_texture(coord);
168     *   }
169     * As it is now, we always sample the texture, then selectively replace
170     * the texel color results with the border color.
171     */
172
173    if (use_border) {
174       /* select texel color or border color depending on use_border */
175       LLVMValueRef border_color_ptr = 
176          bld->dynamic_state->border_color(bld->dynamic_state,
177                                           bld->builder, unit);
178       int chan;
179       for (chan = 0; chan < 4; chan++) {
180          LLVMValueRef border_chan =
181             lp_build_array_get(bld->builder, border_color_ptr,
182                                lp_build_const_int32(chan));
183          LLVMValueRef border_chan_vec =
184             lp_build_broadcast_scalar(&bld->float_vec_bld, border_chan);
185          texel_out[chan] = lp_build_select(&bld->texel_bld, use_border,
186                                            border_chan_vec, texel_out[chan]);
187       }
188    }
189
190    apply_sampler_swizzle(bld, texel_out);
191 }
192
193
194 /**
195  * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
196  */
197 static LLVMValueRef
198 lp_build_coord_mirror(struct lp_build_sample_context *bld,
199                       LLVMValueRef coord)
200 {
201    struct lp_build_context *coord_bld = &bld->coord_bld;
202    struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
203    LLVMValueRef fract, flr, isOdd;
204
205    lp_build_ifloor_fract(coord_bld, coord, &flr, &fract);
206
207    /* isOdd = flr & 1 */
208    isOdd = LLVMBuildAnd(bld->builder, flr, int_coord_bld->one, "");
209
210    /* make coord positive or negative depending on isOdd */
211    coord = lp_build_set_sign(coord_bld, fract, isOdd);
212
213    /* convert isOdd to float */
214    isOdd = lp_build_int_to_float(coord_bld, isOdd);
215
216    /* add isOdd to coord */
217    coord = lp_build_add(coord_bld, coord, isOdd);
218
219    return coord;
220 }
221
222
223 /**
224  * Build LLVM code for texture wrap mode for linear filtering.
225  * \param x0_out  returns first integer texcoord
226  * \param x1_out  returns second integer texcoord
227  * \param weight_out  returns linear interpolation weight
228  */
229 static void
230 lp_build_sample_wrap_linear(struct lp_build_sample_context *bld,
231                             LLVMValueRef coord,
232                             LLVMValueRef length,
233                             LLVMValueRef length_f,
234                             boolean is_pot,
235                             unsigned wrap_mode,
236                             LLVMValueRef *x0_out,
237                             LLVMValueRef *x1_out,
238                             LLVMValueRef *weight_out)
239 {
240    struct lp_build_context *coord_bld = &bld->coord_bld;
241    struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
242    LLVMValueRef half = lp_build_const_vec(coord_bld->type, 0.5);
243    LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length, int_coord_bld->one);
244    LLVMValueRef coord0, coord1, weight;
245
246    switch(wrap_mode) {
247    case PIPE_TEX_WRAP_REPEAT:
248       /* mul by size and subtract 0.5 */
249       coord = lp_build_mul(coord_bld, coord, length_f);
250       coord = lp_build_sub(coord_bld, coord, half);
251       /* convert to int, compute lerp weight */
252       lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
253       /* repeat wrap */
254       if (is_pot) {
255          coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
256          coord0 = LLVMBuildAnd(bld->builder, coord0, length_minus_one, "");
257          coord1 = LLVMBuildAnd(bld->builder, coord1, length_minus_one, "");
258       }
259       else {
260          /* Add a bias to the texcoord to handle negative coords */
261          LLVMValueRef bias = lp_build_mul_imm(int_coord_bld, length, 1024);
262          LLVMValueRef mask;
263          coord0 = LLVMBuildAdd(bld->builder, coord0, bias, "");
264          coord0 = LLVMBuildURem(bld->builder, coord0, length, "");
265          mask = lp_build_compare(bld->builder, int_coord_bld->type,
266                                  PIPE_FUNC_NOTEQUAL, coord0, length_minus_one);
267          coord1 = LLVMBuildAnd(bld->builder,
268                               lp_build_add(int_coord_bld, coord0, int_coord_bld->one),
269                               mask, "");
270       }
271       break;
272
273    case PIPE_TEX_WRAP_CLAMP:
274       if (bld->static_state->normalized_coords) {
275          /* scale coord to length */
276          coord = lp_build_mul(coord_bld, coord, length_f);
277       }
278
279       /* clamp to [0, length] */
280       coord = lp_build_clamp(coord_bld, coord, coord_bld->zero, length_f);
281
282       coord = lp_build_sub(coord_bld, coord, half);
283
284       /* convert to int, compute lerp weight */
285       lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
286       coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
287       break;
288
289    case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
290       {
291          struct lp_build_context abs_coord_bld = bld->coord_bld;
292          abs_coord_bld.type.sign = FALSE;
293
294          if (bld->static_state->normalized_coords) {
295             /* mul by tex size */
296             coord = lp_build_mul(coord_bld, coord, length_f);
297          }
298          /* clamp to length max */
299          coord = lp_build_min(coord_bld, coord, length_f);
300          /* subtract 0.5 */
301          coord = lp_build_sub(coord_bld, coord, half);
302          /* clamp to [0, length - 0.5] */
303          coord = lp_build_max(coord_bld, coord, coord_bld->zero);
304          /* convert to int, compute lerp weight */
305          lp_build_ifloor_fract(&abs_coord_bld, coord, &coord0, &weight);
306          coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
307          /* coord1 = min(coord1, length-1) */
308          coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
309          break;
310       }
311
312    case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
313       {
314          LLVMValueRef min;
315          if (bld->static_state->normalized_coords) {
316             /* scale coord to length */
317             coord = lp_build_mul(coord_bld, coord, length_f);
318          }
319          /* was: clamp to [-0.5, length + 0.5], then sub 0.5 */
320          coord = lp_build_sub(coord_bld, coord, half);
321          min = lp_build_const_vec(coord_bld->type, -1.0F);
322          coord = lp_build_clamp(coord_bld, coord, min, length_f);
323          /* convert to int, compute lerp weight */
324          lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
325          coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
326       }
327       break;
328
329    case PIPE_TEX_WRAP_MIRROR_REPEAT:
330       /* compute mirror function */
331       coord = lp_build_coord_mirror(bld, coord);
332
333       /* scale coord to length */
334       coord = lp_build_mul(coord_bld, coord, length_f);
335       coord = lp_build_sub(coord_bld, coord, half);
336
337       /* convert to int, compute lerp weight */
338       lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
339       coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
340
341       /* coord0 = max(coord0, 0) */
342       coord0 = lp_build_max(int_coord_bld, coord0, int_coord_bld->zero);
343       /* coord1 = min(coord1, length-1) */
344       coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
345       break;
346
347    case PIPE_TEX_WRAP_MIRROR_CLAMP:
348       coord = lp_build_abs(coord_bld, coord);
349
350       if (bld->static_state->normalized_coords) {
351          /* scale coord to length */
352          coord = lp_build_mul(coord_bld, coord, length_f);
353       }
354
355       /* clamp to [0, length] */
356       coord = lp_build_min(coord_bld, coord, length_f);
357
358       coord = lp_build_sub(coord_bld, coord, half);
359
360       /* convert to int, compute lerp weight */
361       lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
362       coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
363       break;
364
365    case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
366       {
367          LLVMValueRef min, max;
368          struct lp_build_context abs_coord_bld = bld->coord_bld;
369          abs_coord_bld.type.sign = FALSE;
370          coord = lp_build_abs(coord_bld, coord);
371
372          if (bld->static_state->normalized_coords) {
373             /* scale coord to length */
374             coord = lp_build_mul(coord_bld, coord, length_f);
375          }
376
377          /* clamp to [0.5, length - 0.5] */
378          min = half;
379          max = lp_build_sub(coord_bld, length_f, min);
380          coord = lp_build_clamp(coord_bld, coord, min, max);
381
382          coord = lp_build_sub(coord_bld, coord, half);
383
384          /* convert to int, compute lerp weight */
385          lp_build_ifloor_fract(&abs_coord_bld, coord, &coord0, &weight);
386          coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
387       }
388       break;
389
390    case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
391       {
392          coord = lp_build_abs(coord_bld, coord);
393
394          if (bld->static_state->normalized_coords) {
395             /* scale coord to length */
396             coord = lp_build_mul(coord_bld, coord, length_f);
397          }
398
399          /* was: clamp to [-0.5, length + 0.5] then sub 0.5 */
400          /* skip -0.5 clamp (always positive), do sub first */
401          coord = lp_build_sub(coord_bld, coord, half);
402          coord = lp_build_min(coord_bld, coord, length_f);
403
404          /* convert to int, compute lerp weight */
405          lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
406          coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
407       }
408       break;
409
410    default:
411       assert(0);
412       coord0 = NULL;
413       coord1 = NULL;
414       weight = NULL;
415    }
416
417    *x0_out = coord0;
418    *x1_out = coord1;
419    *weight_out = weight;
420 }
421
422
423 /**
424  * Build LLVM code for texture wrap mode for nearest filtering.
425  * \param coord  the incoming texcoord (nominally in [0,1])
426  * \param length  the texture size along one dimension, as int vector
427  * \param is_pot  if TRUE, length is a power of two
428  * \param wrap_mode  one of PIPE_TEX_WRAP_x
429  */
430 static LLVMValueRef
431 lp_build_sample_wrap_nearest(struct lp_build_sample_context *bld,
432                              LLVMValueRef coord,
433                              LLVMValueRef length,
434                              LLVMValueRef length_f,
435                              boolean is_pot,
436                              unsigned wrap_mode)
437 {
438    struct lp_build_context *coord_bld = &bld->coord_bld;
439    struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
440    LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length, int_coord_bld->one);
441    LLVMValueRef icoord;
442    
443    switch(wrap_mode) {
444    case PIPE_TEX_WRAP_REPEAT:
445       coord = lp_build_mul(coord_bld, coord, length_f);
446       icoord = lp_build_ifloor(coord_bld, coord);
447       if (is_pot)
448          icoord = LLVMBuildAnd(bld->builder, icoord, length_minus_one, "");
449       else {
450          /* Add a bias to the texcoord to handle negative coords */
451          LLVMValueRef bias = lp_build_mul_imm(int_coord_bld, length, 1024);
452          icoord = LLVMBuildAdd(bld->builder, icoord, bias, "");
453          icoord = LLVMBuildURem(bld->builder, icoord, length, "");
454       }
455       break;
456
457    case PIPE_TEX_WRAP_CLAMP:
458    case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
459       if (bld->static_state->normalized_coords) {
460          /* scale coord to length */
461          coord = lp_build_mul(coord_bld, coord, length_f);
462       }
463
464       /* floor */
465       /* use itrunc instead since we clamp to 0 anyway */
466       icoord = lp_build_itrunc(coord_bld, coord);
467
468       /* clamp to [0, length - 1]. */
469       icoord = lp_build_clamp(int_coord_bld, icoord, int_coord_bld->zero,
470                               length_minus_one);
471       break;
472
473    case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
474       /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
475       {
476          LLVMValueRef min, max;
477
478          if (bld->static_state->normalized_coords) {
479             /* scale coord to length */
480             coord = lp_build_mul(coord_bld, coord, length_f);
481          }
482
483          icoord = lp_build_ifloor(coord_bld, coord);
484
485          /* clamp to [-1, length] */
486          min = lp_build_negate(int_coord_bld, int_coord_bld->one);
487          max = length;
488          icoord = lp_build_clamp(int_coord_bld, icoord, min, max);
489       }
490       break;
491
492    case PIPE_TEX_WRAP_MIRROR_REPEAT:
493       /* compute mirror function */
494       coord = lp_build_coord_mirror(bld, coord);
495
496       /* scale coord to length */
497       assert(bld->static_state->normalized_coords);
498       coord = lp_build_mul(coord_bld, coord, length_f);
499
500       /* itrunc == ifloor here */
501       icoord = lp_build_itrunc(coord_bld, coord);
502
503       /* clamp to [0, length - 1] */
504       icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
505       break;
506
507    case PIPE_TEX_WRAP_MIRROR_CLAMP:
508    case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
509       coord = lp_build_abs(coord_bld, coord);
510
511       if (bld->static_state->normalized_coords) {
512          /* scale coord to length */
513          coord = lp_build_mul(coord_bld, coord, length_f);
514       }
515
516       /* itrunc == ifloor here */
517       icoord = lp_build_itrunc(coord_bld, coord);
518
519       /* clamp to [0, length - 1] */
520       icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
521       break;
522
523    case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
524       coord = lp_build_abs(coord_bld, coord);
525
526       if (bld->static_state->normalized_coords) {
527          /* scale coord to length */
528          coord = lp_build_mul(coord_bld, coord, length_f);
529       }
530
531       /* itrunc == ifloor here */
532       icoord = lp_build_itrunc(coord_bld, coord);
533
534       /* clamp to [0, length] */
535       icoord = lp_build_min(int_coord_bld, icoord, length);
536       break;
537
538    default:
539       assert(0);
540       icoord = NULL;
541    }
542
543    return icoord;
544 }
545
546
547 /**
548  * Generate code to sample a mipmap level with nearest filtering.
549  * If sampling a cube texture, r = cube face in [0,5].
550  */
551 static void
552 lp_build_sample_image_nearest(struct lp_build_sample_context *bld,
553                               unsigned unit,
554                               LLVMValueRef size,
555                               LLVMValueRef row_stride_vec,
556                               LLVMValueRef img_stride_vec,
557                               LLVMValueRef data_ptr,
558                               LLVMValueRef s,
559                               LLVMValueRef t,
560                               LLVMValueRef r,
561                               LLVMValueRef colors_out[4])
562 {
563    const unsigned dims = bld->dims;
564    LLVMValueRef width_vec;
565    LLVMValueRef height_vec;
566    LLVMValueRef depth_vec;
567    LLVMValueRef flt_size;
568    LLVMValueRef flt_width_vec;
569    LLVMValueRef flt_height_vec;
570    LLVMValueRef flt_depth_vec;
571    LLVMValueRef x, y, z;
572
573    lp_build_extract_image_sizes(bld,
574                                 bld->int_size_type,
575                                 bld->int_coord_type,
576                                 size,
577                                 &width_vec, &height_vec, &depth_vec);
578
579    flt_size = lp_build_int_to_float(&bld->float_size_bld, size);
580
581    lp_build_extract_image_sizes(bld,
582                                 bld->float_size_type,
583                                 bld->coord_type,
584                                 flt_size,
585                                 &flt_width_vec, &flt_height_vec, &flt_depth_vec);
586
587    /*
588     * Compute integer texcoords.
589     */
590    x = lp_build_sample_wrap_nearest(bld, s, width_vec, flt_width_vec,
591                                     bld->static_state->pot_width,
592                                     bld->static_state->wrap_s);
593    lp_build_name(x, "tex.x.wrapped");
594
595    if (dims >= 2) {
596       y = lp_build_sample_wrap_nearest(bld, t, height_vec, flt_height_vec,
597                                        bld->static_state->pot_height,
598                                        bld->static_state->wrap_t);
599       lp_build_name(y, "tex.y.wrapped");
600
601       if (dims == 3) {
602          z = lp_build_sample_wrap_nearest(bld, r, depth_vec, flt_depth_vec,
603                                           bld->static_state->pot_depth,
604                                           bld->static_state->wrap_r);
605          lp_build_name(z, "tex.z.wrapped");
606       }
607       else if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
608          z = r;
609       }
610       else {
611          z = NULL;
612       }
613    }
614    else {
615       y = z = NULL;
616    }
617
618    /*
619     * Get texture colors.
620     */
621    lp_build_sample_texel_soa(bld, unit,
622                              width_vec, height_vec, depth_vec,
623                              x, y, z,
624                              row_stride_vec, img_stride_vec,
625                              data_ptr, colors_out);
626 }
627
628
629 /**
630  * Generate code to sample a mipmap level with linear filtering.
631  * If sampling a cube texture, r = cube face in [0,5].
632  */
633 static void
634 lp_build_sample_image_linear(struct lp_build_sample_context *bld,
635                              unsigned unit,
636                              LLVMValueRef size,
637                              LLVMValueRef row_stride_vec,
638                              LLVMValueRef img_stride_vec,
639                              LLVMValueRef data_ptr,
640                              LLVMValueRef s,
641                              LLVMValueRef t,
642                              LLVMValueRef r,
643                              LLVMValueRef colors_out[4])
644 {
645    const unsigned dims = bld->dims;
646    LLVMValueRef width_vec;
647    LLVMValueRef height_vec;
648    LLVMValueRef depth_vec;
649    LLVMValueRef flt_size;
650    LLVMValueRef flt_width_vec;
651    LLVMValueRef flt_height_vec;
652    LLVMValueRef flt_depth_vec;
653    LLVMValueRef x0, y0, z0, x1, y1, z1;
654    LLVMValueRef s_fpart, t_fpart, r_fpart;
655    LLVMValueRef neighbors[2][2][4];
656    int chan;
657
658    lp_build_extract_image_sizes(bld,
659                                 bld->int_size_type,
660                                 bld->int_coord_type,
661                                 size,
662                                 &width_vec, &height_vec, &depth_vec);
663
664    flt_size = lp_build_int_to_float(&bld->float_size_bld, size);
665
666    lp_build_extract_image_sizes(bld,
667                                 bld->float_size_type,
668                                 bld->coord_type,
669                                 flt_size,
670                                 &flt_width_vec, &flt_height_vec, &flt_depth_vec);
671
672    /*
673     * Compute integer texcoords.
674     */
675    lp_build_sample_wrap_linear(bld, s, width_vec, flt_width_vec,
676                                bld->static_state->pot_width,
677                                bld->static_state->wrap_s,
678                                &x0, &x1, &s_fpart);
679    lp_build_name(x0, "tex.x0.wrapped");
680    lp_build_name(x1, "tex.x1.wrapped");
681
682    if (dims >= 2) {
683       lp_build_sample_wrap_linear(bld, t, height_vec, flt_height_vec,
684                                   bld->static_state->pot_height,
685                                   bld->static_state->wrap_t,
686                                   &y0, &y1, &t_fpart);
687       lp_build_name(y0, "tex.y0.wrapped");
688       lp_build_name(y1, "tex.y1.wrapped");
689
690       if (dims == 3) {
691          lp_build_sample_wrap_linear(bld, r, depth_vec, flt_depth_vec,
692                                      bld->static_state->pot_depth,
693                                      bld->static_state->wrap_r,
694                                      &z0, &z1, &r_fpart);
695          lp_build_name(z0, "tex.z0.wrapped");
696          lp_build_name(z1, "tex.z1.wrapped");
697       }
698       else if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
699          z0 = z1 = r;  /* cube face */
700          r_fpart = NULL;
701       }
702       else {
703          z0 = z1 = NULL;
704          r_fpart = NULL;
705       }
706    }
707    else {
708       y0 = y1 = t_fpart = NULL;
709       z0 = z1 = r_fpart = NULL;
710    }
711
712    /*
713     * Get texture colors.
714     */
715    /* get x0/x1 texels */
716    lp_build_sample_texel_soa(bld, unit,
717                              width_vec, height_vec, depth_vec,
718                              x0, y0, z0,
719                              row_stride_vec, img_stride_vec,
720                              data_ptr, neighbors[0][0]);
721    lp_build_sample_texel_soa(bld, unit,
722                              width_vec, height_vec, depth_vec,
723                              x1, y0, z0,
724                              row_stride_vec, img_stride_vec,
725                              data_ptr, neighbors[0][1]);
726
727    if (dims == 1) {
728       /* Interpolate two samples from 1D image to produce one color */
729       for (chan = 0; chan < 4; chan++) {
730          colors_out[chan] = lp_build_lerp(&bld->texel_bld, s_fpart,
731                                           neighbors[0][0][chan],
732                                           neighbors[0][1][chan]);
733       }
734    }
735    else {
736       /* 2D/3D texture */
737       LLVMValueRef colors0[4];
738
739       /* get x0/x1 texels at y1 */
740       lp_build_sample_texel_soa(bld, unit,
741                                 width_vec, height_vec, depth_vec,
742                                 x0, y1, z0,
743                                 row_stride_vec, img_stride_vec,
744                                 data_ptr, neighbors[1][0]);
745       lp_build_sample_texel_soa(bld, unit,
746                                 width_vec, height_vec, depth_vec,
747                                 x1, y1, z0,
748                                 row_stride_vec, img_stride_vec,
749                                 data_ptr, neighbors[1][1]);
750
751       /* Bilinear interpolate the four samples from the 2D image / 3D slice */
752       for (chan = 0; chan < 4; chan++) {
753          colors0[chan] = lp_build_lerp_2d(&bld->texel_bld,
754                                           s_fpart, t_fpart,
755                                           neighbors[0][0][chan],
756                                           neighbors[0][1][chan],
757                                           neighbors[1][0][chan],
758                                           neighbors[1][1][chan]);
759       }
760
761       if (dims == 3) {
762          LLVMValueRef neighbors1[2][2][4];
763          LLVMValueRef colors1[4];
764
765          /* get x0/x1/y0/y1 texels at z1 */
766          lp_build_sample_texel_soa(bld, unit,
767                                    width_vec, height_vec, depth_vec,
768                                    x0, y0, z1,
769                                    row_stride_vec, img_stride_vec,
770                                    data_ptr, neighbors1[0][0]);
771          lp_build_sample_texel_soa(bld, unit,
772                                    width_vec, height_vec, depth_vec,
773                                    x1, y0, z1,
774                                    row_stride_vec, img_stride_vec,
775                                    data_ptr, neighbors1[0][1]);
776          lp_build_sample_texel_soa(bld, unit,
777                                    width_vec, height_vec, depth_vec,
778                                    x0, y1, z1,
779                                    row_stride_vec, img_stride_vec,
780                                    data_ptr, neighbors1[1][0]);
781          lp_build_sample_texel_soa(bld, unit,
782                                    width_vec, height_vec, depth_vec,
783                                    x1, y1, z1,
784                                    row_stride_vec, img_stride_vec,
785                                    data_ptr, neighbors1[1][1]);
786
787          /* Bilinear interpolate the four samples from the second Z slice */
788          for (chan = 0; chan < 4; chan++) {
789             colors1[chan] = lp_build_lerp_2d(&bld->texel_bld,
790                                              s_fpart, t_fpart,
791                                              neighbors1[0][0][chan],
792                                              neighbors1[0][1][chan],
793                                              neighbors1[1][0][chan],
794                                              neighbors1[1][1][chan]);
795          }
796
797          /* Linearly interpolate the two samples from the two 3D slices */
798          for (chan = 0; chan < 4; chan++) {
799             colors_out[chan] = lp_build_lerp(&bld->texel_bld,
800                                              r_fpart,
801                                              colors0[chan], colors1[chan]);
802          }
803       }
804       else {
805          /* 2D tex */
806          for (chan = 0; chan < 4; chan++) {
807             colors_out[chan] = colors0[chan];
808          }
809       }
810    }
811 }
812
813
814 /**
815  * Sample the texture/mipmap using given image filter and mip filter.
816  * data0_ptr and data1_ptr point to the two mipmap levels to sample
817  * from.  width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
818  * If we're using nearest miplevel sampling the '1' values will be null/unused.
819  */
820 static void
821 lp_build_sample_mipmap(struct lp_build_sample_context *bld,
822                        unsigned unit,
823                        unsigned img_filter,
824                        unsigned mip_filter,
825                        LLVMValueRef s,
826                        LLVMValueRef t,
827                        LLVMValueRef r,
828                        LLVMValueRef ilevel0,
829                        LLVMValueRef ilevel1,
830                        LLVMValueRef lod_fpart,
831                        LLVMValueRef *colors_out)
832 {
833    LLVMBuilderRef builder = bld->builder;
834    LLVMValueRef size0;
835    LLVMValueRef size1;
836    LLVMValueRef row_stride0_vec;
837    LLVMValueRef row_stride1_vec;
838    LLVMValueRef img_stride0_vec;
839    LLVMValueRef img_stride1_vec;
840    LLVMValueRef data_ptr0;
841    LLVMValueRef data_ptr1;
842    LLVMValueRef colors0[4], colors1[4];
843    unsigned chan;
844
845    /* sample the first mipmap level */
846    lp_build_mipmap_level_sizes(bld, ilevel0,
847                                &size0,
848                                &row_stride0_vec, &img_stride0_vec);
849    data_ptr0 = lp_build_get_mipmap_level(bld, ilevel0);
850    if (img_filter == PIPE_TEX_FILTER_NEAREST) {
851       lp_build_sample_image_nearest(bld, unit,
852                                     size0,
853                                     row_stride0_vec, img_stride0_vec,
854                                     data_ptr0, s, t, r,
855                                     colors0);
856    }
857    else {
858       assert(img_filter == PIPE_TEX_FILTER_LINEAR);
859       lp_build_sample_image_linear(bld, unit,
860                                    size0,
861                                    row_stride0_vec, img_stride0_vec,
862                                    data_ptr0, s, t, r,
863                                    colors0);
864    }
865
866    /* Store the first level's colors in the output variables */
867    for (chan = 0; chan < 4; chan++) {
868        LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
869    }
870
871    if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
872       struct lp_build_if_state if_ctx;
873       LLVMValueRef need_lerp;
874
875       /* need_lerp = lod_fpart > 0 */
876       need_lerp = LLVMBuildFCmp(builder, LLVMRealUGT,
877                                 lod_fpart,
878                                 bld->float_bld.zero,
879                                 "need_lerp");
880
881       lp_build_if(&if_ctx, builder, need_lerp);
882       {
883          /* sample the second mipmap level */
884          lp_build_mipmap_level_sizes(bld, ilevel1,
885                                      &size1,
886                                      &row_stride1_vec, &img_stride1_vec);
887          data_ptr1 = lp_build_get_mipmap_level(bld, ilevel1);
888          if (img_filter == PIPE_TEX_FILTER_NEAREST) {
889             lp_build_sample_image_nearest(bld, unit,
890                                           size1,
891                                           row_stride1_vec, img_stride1_vec,
892                                           data_ptr1, s, t, r,
893                                           colors1);
894          }
895          else {
896             lp_build_sample_image_linear(bld, unit,
897                                          size1,
898                                          row_stride1_vec, img_stride1_vec,
899                                          data_ptr1, s, t, r,
900                                          colors1);
901          }
902
903          /* interpolate samples from the two mipmap levels */
904
905          lod_fpart = lp_build_broadcast_scalar(&bld->texel_bld, lod_fpart);
906
907          for (chan = 0; chan < 4; chan++) {
908             colors0[chan] = lp_build_lerp(&bld->texel_bld, lod_fpart,
909                                           colors0[chan], colors1[chan]);
910             LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
911          }
912       }
913       lp_build_endif(&if_ctx);
914    }
915 }
916
917
918
919 /**
920  * General texture sampling codegen.
921  * This function handles texture sampling for all texture targets (1D,
922  * 2D, 3D, cube) and all filtering modes.
923  */
924 static void
925 lp_build_sample_general(struct lp_build_sample_context *bld,
926                         unsigned unit,
927                         LLVMValueRef s,
928                         LLVMValueRef t,
929                         LLVMValueRef r,
930                         const LLVMValueRef *ddx,
931                         const LLVMValueRef *ddy,
932                         LLVMValueRef lod_bias, /* optional */
933                         LLVMValueRef explicit_lod, /* optional */
934                         LLVMValueRef *colors_out)
935 {
936    struct lp_build_context *int_bld = &bld->int_bld;
937    LLVMBuilderRef builder = bld->builder;
938    const unsigned mip_filter = bld->static_state->min_mip_filter;
939    const unsigned min_filter = bld->static_state->min_img_filter;
940    const unsigned mag_filter = bld->static_state->mag_img_filter;
941    LLVMValueRef lod_ipart = NULL, lod_fpart = NULL;
942    LLVMValueRef ilevel0, ilevel1 = NULL;
943    LLVMValueRef face_ddx[4], face_ddy[4];
944    LLVMValueRef texels[4];
945    LLVMTypeRef i32t = LLVMInt32Type();
946    LLVMValueRef i32t_zero = LLVMConstInt(i32t, 0, 0);
947    unsigned chan;
948
949    /*
950    printf("%s mip %d  min %d  mag %d\n", __FUNCTION__,
951           mip_filter, min_filter, mag_filter);
952    */
953
954    /*
955     * Choose cube face, recompute texcoords and derivatives for the chosen face.
956     */
957    if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
958       LLVMValueRef face, face_s, face_t;
959       lp_build_cube_lookup(bld, s, t, r, &face, &face_s, &face_t);
960       s = face_s; /* vec */
961       t = face_t; /* vec */
962       /* use 'r' to indicate cube face */
963       r = lp_build_broadcast_scalar(&bld->int_coord_bld, face); /* vec */
964
965       /* recompute ddx, ddy using the new (s,t) face texcoords */
966       face_ddx[0] = lp_build_ddx(&bld->coord_bld, s);
967       face_ddx[1] = lp_build_ddx(&bld->coord_bld, t);
968       face_ddx[2] = NULL;
969       face_ddx[3] = NULL;
970       face_ddy[0] = lp_build_ddy(&bld->coord_bld, s);
971       face_ddy[1] = lp_build_ddy(&bld->coord_bld, t);
972       face_ddy[2] = NULL;
973       face_ddy[3] = NULL;
974       ddx = face_ddx;
975       ddy = face_ddy;
976    }
977
978    /*
979     * Compute the level of detail (float).
980     */
981    if (min_filter != mag_filter ||
982        mip_filter != PIPE_TEX_MIPFILTER_NONE) {
983       /* Need to compute lod either to choose mipmap levels or to
984        * distinguish between minification/magnification with one mipmap level.
985        */
986       lp_build_lod_selector(bld, unit, ddx, ddy,
987                             lod_bias, explicit_lod,
988                             mip_filter,
989                             &lod_ipart, &lod_fpart);
990    } else {
991       lod_ipart = i32t_zero;
992    }
993
994    /*
995     * Compute integer mipmap level(s) to fetch texels from: ilevel0, ilevel1
996     */
997    switch (mip_filter) {
998    default:
999       assert(0 && "bad mip_filter value in lp_build_sample_soa()");
1000       /* fall-through */
1001    case PIPE_TEX_MIPFILTER_NONE:
1002       /* always use mip level 0 */
1003       if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
1004          /* XXX this is a work-around for an apparent bug in LLVM 2.7.
1005           * We should be able to set ilevel0 = const(0) but that causes
1006           * bad x86 code to be emitted.
1007           */
1008          assert(lod_ipart);
1009          lp_build_nearest_mip_level(bld, unit, lod_ipart, &ilevel0);
1010       }
1011       else {
1012          ilevel0 = i32t_zero;
1013       }
1014       break;
1015    case PIPE_TEX_MIPFILTER_NEAREST:
1016       assert(lod_ipart);
1017       lp_build_nearest_mip_level(bld, unit, lod_ipart, &ilevel0);
1018       break;
1019    case PIPE_TEX_MIPFILTER_LINEAR:
1020       assert(lod_ipart);
1021       assert(lod_fpart);
1022       lp_build_linear_mip_levels(bld, unit,
1023                                  lod_ipart, &lod_fpart,
1024                                  &ilevel0, &ilevel1);
1025       break;
1026    }
1027
1028    /*
1029     * Get/interpolate texture colors.
1030     */
1031
1032    for (chan = 0; chan < 4; ++chan) {
1033      texels[chan] = lp_build_alloca(builder, bld->texel_bld.vec_type, "");
1034      lp_build_name(texels[chan], "sampler%u_texel_%c_var", unit, "xyzw"[chan]);
1035    }
1036
1037    if (min_filter == mag_filter) {
1038       /* no need to distinquish between minification and magnification */
1039       lp_build_sample_mipmap(bld, unit,
1040                              min_filter, mip_filter,
1041                              s, t, r,
1042                              ilevel0, ilevel1, lod_fpart,
1043                              texels);
1044    }
1045    else {
1046       /* Emit conditional to choose min image filter or mag image filter
1047        * depending on the lod being > 0 or <= 0, respectively.
1048        */
1049       struct lp_build_if_state if_ctx;
1050       LLVMValueRef minify;
1051
1052       /* minify = lod >= 0.0 */
1053       minify = LLVMBuildICmp(builder, LLVMIntSGE,
1054                              lod_ipart, int_bld->zero, "");
1055
1056       lp_build_if(&if_ctx, builder, minify);
1057       {
1058          /* Use the minification filter */
1059          lp_build_sample_mipmap(bld, unit,
1060                                 min_filter, mip_filter,
1061                                 s, t, r,
1062                                 ilevel0, ilevel1, lod_fpart,
1063                                 texels);
1064       }
1065       lp_build_else(&if_ctx);
1066       {
1067          /* Use the magnification filter */
1068          lp_build_sample_mipmap(bld, unit,
1069                                 mag_filter, PIPE_TEX_MIPFILTER_NONE,
1070                                 s, t, r,
1071                                 i32t_zero, NULL, NULL,
1072                                 texels);
1073       }
1074       lp_build_endif(&if_ctx);
1075    }
1076
1077    for (chan = 0; chan < 4; ++chan) {
1078      colors_out[chan] = LLVMBuildLoad(builder, texels[chan], "");
1079      lp_build_name(colors_out[chan], "sampler%u_texel_%c", unit, "xyzw"[chan]);
1080    }
1081 }
1082
1083
1084 /**
1085  * Do shadow test/comparison.
1086  * \param p  the texcoord Z (aka R, aka P) component
1087  * \param texel  the texel to compare against (use the X channel)
1088  */
1089 static void
1090 lp_build_sample_compare(struct lp_build_sample_context *bld,
1091                         LLVMValueRef p,
1092                         LLVMValueRef texel[4])
1093 {
1094    struct lp_build_context *texel_bld = &bld->texel_bld;
1095    LLVMValueRef res;
1096    const unsigned chan = 0;
1097
1098    if (bld->static_state->compare_mode == PIPE_TEX_COMPARE_NONE)
1099       return;
1100
1101    /* debug code */
1102    if (0) {
1103       LLVMValueRef indx = lp_build_const_int32(0);
1104       LLVMValueRef coord = LLVMBuildExtractElement(bld->builder, p, indx, "");
1105       LLVMValueRef tex = LLVMBuildExtractElement(bld->builder,
1106                                                  texel[chan], indx, "");
1107       lp_build_printf(bld->builder, "shadow compare coord %f to texture %f\n",
1108                       coord, tex);
1109    }
1110
1111    /* result = (p FUNC texel) ? 1 : 0 */
1112    res = lp_build_cmp(texel_bld, bld->static_state->compare_func,
1113                       p, texel[chan]);
1114    res = lp_build_select(texel_bld, res, texel_bld->one, texel_bld->zero);
1115
1116    /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1117    texel[0] =
1118    texel[1] =
1119    texel[2] = res;
1120    texel[3] = texel_bld->one;
1121 }
1122
1123
1124 /**
1125  * Just set texels to white instead of actually sampling the texture.
1126  * For debugging.
1127  */
1128 void
1129 lp_build_sample_nop(struct lp_type type,
1130                     LLVMValueRef texel_out[4])
1131 {
1132    LLVMValueRef one = lp_build_one(type);
1133    unsigned chan;
1134
1135    for (chan = 0; chan < 4; chan++) {
1136       texel_out[chan] = one;
1137    }  
1138 }
1139
1140
1141 /**
1142  * Build texture sampling code.
1143  * 'texel' will return a vector of four LLVMValueRefs corresponding to
1144  * R, G, B, A.
1145  * \param type  vector float type to use for coords, etc.
1146  * \param ddx  partial derivatives of (s,t,r,q) with respect to x
1147  * \param ddy  partial derivatives of (s,t,r,q) with respect to y
1148  */
1149 void
1150 lp_build_sample_soa(LLVMBuilderRef builder,
1151                     const struct lp_sampler_static_state *static_state,
1152                     struct lp_sampler_dynamic_state *dynamic_state,
1153                     struct lp_type type,
1154                     unsigned unit,
1155                     unsigned num_coords,
1156                     const LLVMValueRef *coords,
1157                     const LLVMValueRef ddx[4],
1158                     const LLVMValueRef ddy[4],
1159                     LLVMValueRef lod_bias, /* optional */
1160                     LLVMValueRef explicit_lod, /* optional */
1161                     LLVMValueRef texel_out[4])
1162 {
1163    unsigned dims = texture_dims(static_state->target);
1164    struct lp_build_sample_context bld;
1165    LLVMTypeRef i32t = LLVMInt32Type();
1166
1167    LLVMValueRef s;
1168    LLVMValueRef t;
1169    LLVMValueRef r;
1170    struct lp_type float_vec_type;
1171
1172    if (0) {
1173       enum pipe_format fmt = static_state->format;
1174       debug_printf("Sample from %s\n", util_format_name(fmt));
1175    }
1176
1177    assert(type.floating);
1178
1179    /* Setup our build context */
1180    memset(&bld, 0, sizeof bld);
1181    bld.builder = builder;
1182    bld.static_state = static_state;
1183    bld.dynamic_state = dynamic_state;
1184    bld.format_desc = util_format_description(static_state->format);
1185    bld.dims = dims;
1186
1187    bld.float_type = lp_type_float(32);
1188    bld.int_type = lp_type_int(32);
1189    bld.coord_type = type;
1190    bld.int_coord_type = lp_int_type(type);
1191    bld.float_size_type = lp_type_float(32);
1192    bld.float_size_type.length = dims > 1 ? 4 : 1;
1193    bld.int_size_type = lp_int_type(bld.float_size_type);
1194    bld.texel_type = type;
1195
1196    float_vec_type = lp_type_float_vec(32);
1197
1198    lp_build_context_init(&bld.float_bld, builder, bld.float_type);
1199    lp_build_context_init(&bld.float_vec_bld, builder, float_vec_type);
1200    lp_build_context_init(&bld.int_bld, builder, bld.int_type);
1201    lp_build_context_init(&bld.coord_bld, builder, bld.coord_type);
1202    lp_build_context_init(&bld.int_coord_bld, builder, bld.int_coord_type);
1203    lp_build_context_init(&bld.int_size_bld, builder, bld.int_size_type);
1204    lp_build_context_init(&bld.float_size_bld, builder, bld.float_size_type);
1205    lp_build_context_init(&bld.texel_bld, builder, bld.texel_type);
1206
1207    /* Get the dynamic state */
1208    bld.width = dynamic_state->width(dynamic_state, builder, unit);
1209    bld.height = dynamic_state->height(dynamic_state, builder, unit);
1210    bld.depth = dynamic_state->depth(dynamic_state, builder, unit);
1211    bld.row_stride_array = dynamic_state->row_stride(dynamic_state, builder, unit);
1212    bld.img_stride_array = dynamic_state->img_stride(dynamic_state, builder, unit);
1213    bld.data_array = dynamic_state->data_ptr(dynamic_state, builder, unit);
1214    /* Note that data_array is an array[level] of pointers to texture images */
1215
1216    s = coords[0];
1217    t = coords[1];
1218    r = coords[2];
1219
1220    /* width, height, depth as single int vector */
1221    if (dims <= 1) {
1222       bld.int_size = bld.width;
1223    }
1224    else {
1225       bld.int_size = LLVMBuildInsertElement(builder, bld.int_size_bld.undef,
1226                                             bld.width, LLVMConstInt(i32t, 0, 0), "");
1227       if (dims >= 2) {
1228          bld.int_size = LLVMBuildInsertElement(builder, bld.int_size,
1229                                                bld.height, LLVMConstInt(i32t, 1, 0), "");
1230          if (dims >= 3) {
1231             bld.int_size = LLVMBuildInsertElement(builder, bld.int_size,
1232                                                   bld.depth, LLVMConstInt(i32t, 2, 0), "");
1233          }
1234       }
1235    }
1236
1237    if (0) {
1238       /* For debug: no-op texture sampling */
1239       lp_build_sample_nop(bld.texel_type, texel_out);
1240    }
1241    else if (util_format_fits_8unorm(bld.format_desc) &&
1242             lp_is_simple_wrap_mode(static_state->wrap_s) &&
1243             lp_is_simple_wrap_mode(static_state->wrap_t)) {
1244       /* do sampling/filtering with fixed pt arithmetic */
1245       lp_build_sample_aos(&bld, unit, s, t, r, ddx, ddy,
1246                           lod_bias, explicit_lod,
1247                           texel_out);
1248    }
1249
1250    else {
1251       if ((gallivm_debug & GALLIVM_DEBUG_PERF) &&
1252           util_format_fits_8unorm(bld.format_desc)) {
1253          debug_printf("%s: using floating point linear filtering for %s\n",
1254                       __FUNCTION__, bld.format_desc->short_name);
1255          debug_printf("  min_img %d  mag_img %d  mip %d  wraps %d  wrapt %d\n",
1256                       static_state->min_img_filter,
1257                       static_state->mag_img_filter,
1258                       static_state->min_mip_filter,
1259                       static_state->wrap_s,
1260                       static_state->wrap_t);
1261       }
1262
1263       lp_build_sample_general(&bld, unit, s, t, r, ddx, ddy,
1264                               lod_bias, explicit_lod,
1265                               texel_out);
1266    }
1267
1268    lp_build_sample_compare(&bld, r, texel_out);
1269 }