OSDN Git Service

gallivm: more linear tex wrap mode calculation simplification
[android-x86/external-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->uint_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->uint_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                             boolean is_pot,
234                             unsigned wrap_mode,
235                             LLVMValueRef *x0_out,
236                             LLVMValueRef *x1_out,
237                             LLVMValueRef *weight_out)
238 {
239    struct lp_build_context *coord_bld = &bld->coord_bld;
240    struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
241    struct lp_build_context *uint_coord_bld = &bld->uint_coord_bld;
242    LLVMValueRef half = lp_build_const_vec(coord_bld->type, 0.5);
243    LLVMValueRef length_f = lp_build_int_to_float(coord_bld, length);
244    LLVMValueRef length_minus_one = lp_build_sub(uint_coord_bld, length, uint_coord_bld->one);
245    LLVMValueRef coord0, coord1, weight;
246
247    switch(wrap_mode) {
248    case PIPE_TEX_WRAP_REPEAT:
249       /* mul by size and subtract 0.5 */
250       coord = lp_build_mul(coord_bld, coord, length_f);
251       coord = lp_build_sub(coord_bld, coord, half);
252       /* convert to int, compute lerp weight */
253       lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
254       coord1 = lp_build_add(uint_coord_bld, coord0, uint_coord_bld->one);
255       /* repeat wrap */
256       if (is_pot) {
257          coord0 = LLVMBuildAnd(bld->builder, coord0, length_minus_one, "");
258          coord1 = LLVMBuildAnd(bld->builder, coord1, length_minus_one, "");
259       }
260       else {
261          /* Add a bias to the texcoord to handle negative coords */
262          LLVMValueRef bias = lp_build_mul_imm(uint_coord_bld, length, 1024);
263          coord0 = LLVMBuildAdd(bld->builder, coord0, bias, "");
264          coord1 = LLVMBuildAdd(bld->builder, coord1, bias, "");
265          coord0 = LLVMBuildURem(bld->builder, coord0, length, "");
266          coord1 = LLVMBuildURem(bld->builder, coord1, length, "");
267       }
268       break;
269
270    case PIPE_TEX_WRAP_CLAMP:
271       if (bld->static_state->normalized_coords) {
272          /* scale coord to length */
273          coord = lp_build_mul(coord_bld, coord, length_f);
274       }
275
276       /* clamp to [0, length] */
277       coord = lp_build_clamp(coord_bld, coord, coord_bld->zero, length_f);
278
279       coord = lp_build_sub(coord_bld, coord, half);
280
281       /* convert to int, compute lerp weight */
282       lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
283       coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
284       break;
285
286    case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
287       if (bld->static_state->normalized_coords) {
288          /* clamp to [0,1] */
289          coord = lp_build_clamp(coord_bld, coord, coord_bld->zero, coord_bld->one);
290          /* mul by tex size and subtract 0.5 */
291          coord = lp_build_mul(coord_bld, coord, length_f);
292          coord = lp_build_sub(coord_bld, coord, half);
293       }
294       /* XXX this is odd normalized ranges from -0.5 to length-0.5 after denorm
295          but non-normalized ranges from to 0.5 to length-0.5 after clamp */
296       else {
297          LLVMValueRef min, max;
298          /* clamp to [0.5, length - 0.5] */
299          min = half;
300          max = lp_build_sub(coord_bld, length_f, min);
301          coord = lp_build_clamp(coord_bld, coord, min, max);
302       }
303       /* convert to int, compute lerp weight */
304       lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
305       coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
306       /* coord0 = max(coord0, 0) */
307       coord0 = lp_build_max(int_coord_bld, coord0, int_coord_bld->zero);
308       /* coord1 = min(coord1, length-1) */
309       coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
310       break;
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                              boolean is_pot,
435                              unsigned wrap_mode)
436 {
437    struct lp_build_context *coord_bld = &bld->coord_bld;
438    struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
439    struct lp_build_context *uint_coord_bld = &bld->uint_coord_bld;
440    LLVMValueRef length_f = lp_build_int_to_float(coord_bld, length);
441    LLVMValueRef length_minus_one = lp_build_sub(uint_coord_bld, length, uint_coord_bld->one);
442    LLVMValueRef icoord;
443    
444    switch(wrap_mode) {
445    case PIPE_TEX_WRAP_REPEAT:
446       coord = lp_build_mul(coord_bld, coord, length_f);
447       icoord = lp_build_ifloor(coord_bld, coord);
448       if (is_pot)
449          icoord = LLVMBuildAnd(bld->builder, icoord, length_minus_one, "");
450       else {
451          /* Add a bias to the texcoord to handle negative coords */
452          LLVMValueRef bias = lp_build_mul_imm(uint_coord_bld, length, 1024);
453          icoord = LLVMBuildAdd(bld->builder, icoord, bias, "");
454          icoord = LLVMBuildURem(bld->builder, icoord, length, "");
455       }
456       break;
457
458    case PIPE_TEX_WRAP_CLAMP:
459    case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
460       if (bld->static_state->normalized_coords) {
461          /* scale coord to length */
462          coord = lp_build_mul(coord_bld, coord, length_f);
463       }
464
465       /* floor */
466       /* use itrunc instead since we clamp to 0 anyway */
467       icoord = lp_build_itrunc(coord_bld, coord);
468
469       /* clamp to [0, length - 1]. */
470       icoord = lp_build_clamp(int_coord_bld, icoord, int_coord_bld->zero,
471                               length_minus_one);
472       break;
473
474    case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
475       /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
476       {
477          LLVMValueRef min, max;
478
479          if (bld->static_state->normalized_coords) {
480             /* scale coord to length */
481             coord = lp_build_mul(coord_bld, coord, length_f);
482          }
483
484          icoord = lp_build_ifloor(coord_bld, coord);
485
486          /* clamp to [-1, length] */
487          min = lp_build_negate(int_coord_bld, int_coord_bld->one);
488          max = length;
489          icoord = lp_build_clamp(int_coord_bld, icoord, min, max);
490       }
491       break;
492
493    case PIPE_TEX_WRAP_MIRROR_REPEAT:
494       /* compute mirror function */
495       coord = lp_build_coord_mirror(bld, coord);
496
497       /* scale coord to length */
498       assert(bld->static_state->normalized_coords);
499       coord = lp_build_mul(coord_bld, coord, length_f);
500
501       /* itrunc == ifloor here */
502       icoord = lp_build_itrunc(coord_bld, coord);
503
504       /* clamp to [0, length - 1] */
505       icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
506       break;
507
508    case PIPE_TEX_WRAP_MIRROR_CLAMP:
509    case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
510       coord = lp_build_abs(coord_bld, coord);
511
512       if (bld->static_state->normalized_coords) {
513          /* scale coord to length */
514          coord = lp_build_mul(coord_bld, coord, length_f);
515       }
516
517       /* itrunc == ifloor here */
518       icoord = lp_build_itrunc(coord_bld, coord);
519
520       /* clamp to [0, length - 1] */
521       icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
522       break;
523
524    case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
525       coord = lp_build_abs(coord_bld, coord);
526
527       if (bld->static_state->normalized_coords) {
528          /* scale coord to length */
529          coord = lp_build_mul(coord_bld, coord, length_f);
530       }
531
532       /* itrunc == ifloor here */
533       icoord = lp_build_itrunc(coord_bld, coord);
534
535       /* clamp to [0, length] */
536       icoord = lp_build_min(int_coord_bld, icoord, length);
537       break;
538
539    default:
540       assert(0);
541       icoord = NULL;
542    }
543
544    return icoord;
545 }
546
547
548 /**
549  * Generate code to sample a mipmap level with nearest filtering.
550  * If sampling a cube texture, r = cube face in [0,5].
551  */
552 static void
553 lp_build_sample_image_nearest(struct lp_build_sample_context *bld,
554                               unsigned unit,
555                               LLVMValueRef width_vec,
556                               LLVMValueRef height_vec,
557                               LLVMValueRef depth_vec,
558                               LLVMValueRef row_stride_vec,
559                               LLVMValueRef img_stride_vec,
560                               LLVMValueRef data_ptr,
561                               LLVMValueRef s,
562                               LLVMValueRef t,
563                               LLVMValueRef r,
564                               LLVMValueRef colors_out[4])
565 {
566    const unsigned dims = bld->dims;
567    LLVMValueRef x, y, z;
568
569    /*
570     * Compute integer texcoords.
571     */
572    x = lp_build_sample_wrap_nearest(bld, s, width_vec,
573                                     bld->static_state->pot_width,
574                                     bld->static_state->wrap_s);
575    lp_build_name(x, "tex.x.wrapped");
576
577    if (dims >= 2) {
578       y = lp_build_sample_wrap_nearest(bld, t, height_vec,
579                                        bld->static_state->pot_height,
580                                        bld->static_state->wrap_t);
581       lp_build_name(y, "tex.y.wrapped");
582
583       if (dims == 3) {
584          z = lp_build_sample_wrap_nearest(bld, r, depth_vec,
585                                           bld->static_state->pot_depth,
586                                           bld->static_state->wrap_r);
587          lp_build_name(z, "tex.z.wrapped");
588       }
589       else if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
590          z = r;
591       }
592       else {
593          z = NULL;
594       }
595    }
596    else {
597       y = z = NULL;
598    }
599
600    /*
601     * Get texture colors.
602     */
603    lp_build_sample_texel_soa(bld, unit,
604                              width_vec, height_vec, depth_vec,
605                              x, y, z,
606                              row_stride_vec, img_stride_vec,
607                              data_ptr, colors_out);
608 }
609
610
611 /**
612  * Generate code to sample a mipmap level with linear filtering.
613  * If sampling a cube texture, r = cube face in [0,5].
614  */
615 static void
616 lp_build_sample_image_linear(struct lp_build_sample_context *bld,
617                              unsigned unit,
618                              LLVMValueRef width_vec,
619                              LLVMValueRef height_vec,
620                              LLVMValueRef depth_vec,
621                              LLVMValueRef row_stride_vec,
622                              LLVMValueRef img_stride_vec,
623                              LLVMValueRef data_ptr,
624                              LLVMValueRef s,
625                              LLVMValueRef t,
626                              LLVMValueRef r,
627                              LLVMValueRef colors_out[4])
628 {
629    const unsigned dims = bld->dims;
630    LLVMValueRef x0, y0, z0, x1, y1, z1;
631    LLVMValueRef s_fpart, t_fpart, r_fpart;
632    LLVMValueRef neighbors[2][2][4];
633    int chan;
634
635    /*
636     * Compute integer texcoords.
637     */
638    lp_build_sample_wrap_linear(bld, s, width_vec,
639                                bld->static_state->pot_width,
640                                bld->static_state->wrap_s,
641                                &x0, &x1, &s_fpart);
642    lp_build_name(x0, "tex.x0.wrapped");
643    lp_build_name(x1, "tex.x1.wrapped");
644
645    if (dims >= 2) {
646       lp_build_sample_wrap_linear(bld, t, height_vec,
647                                   bld->static_state->pot_height,
648                                   bld->static_state->wrap_t,
649                                   &y0, &y1, &t_fpart);
650       lp_build_name(y0, "tex.y0.wrapped");
651       lp_build_name(y1, "tex.y1.wrapped");
652
653       if (dims == 3) {
654          lp_build_sample_wrap_linear(bld, r, depth_vec,
655                                      bld->static_state->pot_depth,
656                                      bld->static_state->wrap_r,
657                                      &z0, &z1, &r_fpart);
658          lp_build_name(z0, "tex.z0.wrapped");
659          lp_build_name(z1, "tex.z1.wrapped");
660       }
661       else if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
662          z0 = z1 = r;  /* cube face */
663          r_fpart = NULL;
664       }
665       else {
666          z0 = z1 = NULL;
667          r_fpart = NULL;
668       }
669    }
670    else {
671       y0 = y1 = t_fpart = NULL;
672       z0 = z1 = r_fpart = NULL;
673    }
674
675    /*
676     * Get texture colors.
677     */
678    /* get x0/x1 texels */
679    lp_build_sample_texel_soa(bld, unit,
680                              width_vec, height_vec, depth_vec,
681                              x0, y0, z0,
682                              row_stride_vec, img_stride_vec,
683                              data_ptr, neighbors[0][0]);
684    lp_build_sample_texel_soa(bld, unit,
685                              width_vec, height_vec, depth_vec,
686                              x1, y0, z0,
687                              row_stride_vec, img_stride_vec,
688                              data_ptr, neighbors[0][1]);
689
690    if (dims == 1) {
691       /* Interpolate two samples from 1D image to produce one color */
692       for (chan = 0; chan < 4; chan++) {
693          colors_out[chan] = lp_build_lerp(&bld->texel_bld, s_fpart,
694                                           neighbors[0][0][chan],
695                                           neighbors[0][1][chan]);
696       }
697    }
698    else {
699       /* 2D/3D texture */
700       LLVMValueRef colors0[4];
701
702       /* get x0/x1 texels at y1 */
703       lp_build_sample_texel_soa(bld, unit,
704                                 width_vec, height_vec, depth_vec,
705                                 x0, y1, z0,
706                                 row_stride_vec, img_stride_vec,
707                                 data_ptr, neighbors[1][0]);
708       lp_build_sample_texel_soa(bld, unit,
709                                 width_vec, height_vec, depth_vec,
710                                 x1, y1, z0,
711                                 row_stride_vec, img_stride_vec,
712                                 data_ptr, neighbors[1][1]);
713
714       /* Bilinear interpolate the four samples from the 2D image / 3D slice */
715       for (chan = 0; chan < 4; chan++) {
716          colors0[chan] = lp_build_lerp_2d(&bld->texel_bld,
717                                           s_fpart, t_fpart,
718                                           neighbors[0][0][chan],
719                                           neighbors[0][1][chan],
720                                           neighbors[1][0][chan],
721                                           neighbors[1][1][chan]);
722       }
723
724       if (dims == 3) {
725          LLVMValueRef neighbors1[2][2][4];
726          LLVMValueRef colors1[4];
727
728          /* get x0/x1/y0/y1 texels at z1 */
729          lp_build_sample_texel_soa(bld, unit,
730                                    width_vec, height_vec, depth_vec,
731                                    x0, y0, z1,
732                                    row_stride_vec, img_stride_vec,
733                                    data_ptr, neighbors1[0][0]);
734          lp_build_sample_texel_soa(bld, unit,
735                                    width_vec, height_vec, depth_vec,
736                                    x1, y0, z1,
737                                    row_stride_vec, img_stride_vec,
738                                    data_ptr, neighbors1[0][1]);
739          lp_build_sample_texel_soa(bld, unit,
740                                    width_vec, height_vec, depth_vec,
741                                    x0, y1, z1,
742                                    row_stride_vec, img_stride_vec,
743                                    data_ptr, neighbors1[1][0]);
744          lp_build_sample_texel_soa(bld, unit,
745                                    width_vec, height_vec, depth_vec,
746                                    x1, y1, z1,
747                                    row_stride_vec, img_stride_vec,
748                                    data_ptr, neighbors1[1][1]);
749
750          /* Bilinear interpolate the four samples from the second Z slice */
751          for (chan = 0; chan < 4; chan++) {
752             colors1[chan] = lp_build_lerp_2d(&bld->texel_bld,
753                                              s_fpart, t_fpart,
754                                              neighbors1[0][0][chan],
755                                              neighbors1[0][1][chan],
756                                              neighbors1[1][0][chan],
757                                              neighbors1[1][1][chan]);
758          }
759
760          /* Linearly interpolate the two samples from the two 3D slices */
761          for (chan = 0; chan < 4; chan++) {
762             colors_out[chan] = lp_build_lerp(&bld->texel_bld,
763                                              r_fpart,
764                                              colors0[chan], colors1[chan]);
765          }
766       }
767       else {
768          /* 2D tex */
769          for (chan = 0; chan < 4; chan++) {
770             colors_out[chan] = colors0[chan];
771          }
772       }
773    }
774 }
775
776
777 /**
778  * Sample the texture/mipmap using given image filter and mip filter.
779  * data0_ptr and data1_ptr point to the two mipmap levels to sample
780  * from.  width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
781  * If we're using nearest miplevel sampling the '1' values will be null/unused.
782  */
783 static void
784 lp_build_sample_mipmap(struct lp_build_sample_context *bld,
785                        unsigned unit,
786                        unsigned img_filter,
787                        unsigned mip_filter,
788                        LLVMValueRef s,
789                        LLVMValueRef t,
790                        LLVMValueRef r,
791                        LLVMValueRef ilevel0,
792                        LLVMValueRef ilevel1,
793                        LLVMValueRef lod_fpart,
794                        LLVMValueRef *colors_out)
795 {
796    LLVMBuilderRef builder = bld->builder;
797    LLVMValueRef width0_vec;
798    LLVMValueRef width1_vec;
799    LLVMValueRef height0_vec;
800    LLVMValueRef height1_vec;
801    LLVMValueRef depth0_vec;
802    LLVMValueRef depth1_vec;
803    LLVMValueRef row_stride0_vec;
804    LLVMValueRef row_stride1_vec;
805    LLVMValueRef img_stride0_vec;
806    LLVMValueRef img_stride1_vec;
807    LLVMValueRef data_ptr0;
808    LLVMValueRef data_ptr1;
809    LLVMValueRef colors0[4], colors1[4];
810    unsigned chan;
811
812    /* sample the first mipmap level */
813    lp_build_mipmap_level_sizes(bld, ilevel0,
814                                &width0_vec, &height0_vec, &depth0_vec,
815                                &row_stride0_vec, &img_stride0_vec);
816    data_ptr0 = lp_build_get_mipmap_level(bld, ilevel0);
817    if (img_filter == PIPE_TEX_FILTER_NEAREST) {
818       lp_build_sample_image_nearest(bld, unit,
819                                     width0_vec, height0_vec, depth0_vec,
820                                     row_stride0_vec, img_stride0_vec,
821                                     data_ptr0, s, t, r,
822                                     colors0);
823    }
824    else {
825       assert(img_filter == PIPE_TEX_FILTER_LINEAR);
826       lp_build_sample_image_linear(bld, unit,
827                                    width0_vec, height0_vec, depth0_vec,
828                                    row_stride0_vec, img_stride0_vec,
829                                    data_ptr0, s, t, r,
830                                    colors0);
831    }
832
833    /* Store the first level's colors in the output variables */
834    for (chan = 0; chan < 4; chan++) {
835        LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
836    }
837
838    if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
839       struct lp_build_flow_context *flow_ctx;
840       struct lp_build_if_state if_ctx;
841       LLVMValueRef need_lerp;
842
843       flow_ctx = lp_build_flow_create(builder);
844
845       /* need_lerp = lod_fpart > 0 */
846       need_lerp = LLVMBuildFCmp(builder, LLVMRealUGT,
847                                 lod_fpart,
848                                 bld->float_bld.zero,
849                                 "need_lerp");
850
851       lp_build_if(&if_ctx, flow_ctx, builder, need_lerp);
852       {
853          /* sample the second mipmap level */
854          lp_build_mipmap_level_sizes(bld, ilevel1,
855                                      &width1_vec, &height1_vec, &depth1_vec,
856                                      &row_stride1_vec, &img_stride1_vec);
857          data_ptr1 = lp_build_get_mipmap_level(bld, ilevel1);
858          if (img_filter == PIPE_TEX_FILTER_NEAREST) {
859             lp_build_sample_image_nearest(bld, unit,
860                                           width1_vec, height1_vec, depth1_vec,
861                                           row_stride1_vec, img_stride1_vec,
862                                           data_ptr1, s, t, r,
863                                           colors1);
864          }
865          else {
866             lp_build_sample_image_linear(bld, unit,
867                                          width1_vec, height1_vec, depth1_vec,
868                                          row_stride1_vec, img_stride1_vec,
869                                          data_ptr1, s, t, r,
870                                          colors1);
871          }
872
873          /* interpolate samples from the two mipmap levels */
874
875          lod_fpart = lp_build_broadcast_scalar(&bld->texel_bld, lod_fpart);
876
877          for (chan = 0; chan < 4; chan++) {
878             colors0[chan] = lp_build_lerp(&bld->texel_bld, lod_fpart,
879                                           colors0[chan], colors1[chan]);
880             LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
881          }
882       }
883       lp_build_endif(&if_ctx);
884
885       lp_build_flow_destroy(flow_ctx);
886    }
887 }
888
889
890
891 /**
892  * General texture sampling codegen.
893  * This function handles texture sampling for all texture targets (1D,
894  * 2D, 3D, cube) and all filtering modes.
895  */
896 static void
897 lp_build_sample_general(struct lp_build_sample_context *bld,
898                         unsigned unit,
899                         LLVMValueRef s,
900                         LLVMValueRef t,
901                         LLVMValueRef r,
902                         const LLVMValueRef *ddx,
903                         const LLVMValueRef *ddy,
904                         LLVMValueRef lod_bias, /* optional */
905                         LLVMValueRef explicit_lod, /* optional */
906                         LLVMValueRef *colors_out)
907 {
908    struct lp_build_context *int_bld = &bld->int_bld;
909    LLVMBuilderRef builder = bld->builder;
910    const unsigned mip_filter = bld->static_state->min_mip_filter;
911    const unsigned min_filter = bld->static_state->min_img_filter;
912    const unsigned mag_filter = bld->static_state->mag_img_filter;
913    LLVMValueRef lod_ipart = NULL, lod_fpart = NULL;
914    LLVMValueRef ilevel0, ilevel1 = NULL;
915    LLVMValueRef face_ddx[4], face_ddy[4];
916    LLVMValueRef texels[4];
917    LLVMTypeRef i32t = LLVMInt32Type();
918    LLVMValueRef i32t_zero = LLVMConstInt(i32t, 0, 0);
919    unsigned chan;
920
921    /*
922    printf("%s mip %d  min %d  mag %d\n", __FUNCTION__,
923           mip_filter, min_filter, mag_filter);
924    */
925
926    /*
927     * Choose cube face, recompute texcoords and derivatives for the chosen face.
928     */
929    if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
930       LLVMValueRef face, face_s, face_t;
931       lp_build_cube_lookup(bld, s, t, r, &face, &face_s, &face_t);
932       s = face_s; /* vec */
933       t = face_t; /* vec */
934       /* use 'r' to indicate cube face */
935       r = lp_build_broadcast_scalar(&bld->int_coord_bld, face); /* vec */
936
937       /* recompute ddx, ddy using the new (s,t) face texcoords */
938       face_ddx[0] = lp_build_ddx(&bld->coord_bld, s);
939       face_ddx[1] = lp_build_ddx(&bld->coord_bld, t);
940       face_ddx[2] = NULL;
941       face_ddx[3] = NULL;
942       face_ddy[0] = lp_build_ddy(&bld->coord_bld, s);
943       face_ddy[1] = lp_build_ddy(&bld->coord_bld, t);
944       face_ddy[2] = NULL;
945       face_ddy[3] = NULL;
946       ddx = face_ddx;
947       ddy = face_ddy;
948    }
949
950    /*
951     * Compute the level of detail (float).
952     */
953    if (min_filter != mag_filter ||
954        mip_filter != PIPE_TEX_MIPFILTER_NONE) {
955       /* Need to compute lod either to choose mipmap levels or to
956        * distinguish between minification/magnification with one mipmap level.
957        */
958       lp_build_lod_selector(bld, unit, ddx, ddy,
959                             lod_bias, explicit_lod,
960                             mip_filter,
961                             &lod_ipart, &lod_fpart);
962    } else {
963       lod_ipart = i32t_zero;
964    }
965
966    /*
967     * Compute integer mipmap level(s) to fetch texels from: ilevel0, ilevel1
968     */
969    switch (mip_filter) {
970    default:
971       assert(0 && "bad mip_filter value in lp_build_sample_soa()");
972       /* fall-through */
973    case PIPE_TEX_MIPFILTER_NONE:
974       /* always use mip level 0 */
975       if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
976          /* XXX this is a work-around for an apparent bug in LLVM 2.7.
977           * We should be able to set ilevel0 = const(0) but that causes
978           * bad x86 code to be emitted.
979           */
980          assert(lod_ipart);
981          lp_build_nearest_mip_level(bld, unit, lod_ipart, &ilevel0);
982       }
983       else {
984          ilevel0 = i32t_zero;
985       }
986       break;
987    case PIPE_TEX_MIPFILTER_NEAREST:
988       assert(lod_ipart);
989       lp_build_nearest_mip_level(bld, unit, lod_ipart, &ilevel0);
990       break;
991    case PIPE_TEX_MIPFILTER_LINEAR:
992       assert(lod_ipart);
993       assert(lod_fpart);
994       lp_build_linear_mip_levels(bld, unit,
995                                  lod_ipart, &lod_fpart,
996                                  &ilevel0, &ilevel1);
997       break;
998    }
999
1000    /*
1001     * Get/interpolate texture colors.
1002     */
1003
1004    for (chan = 0; chan < 4; ++chan) {
1005      texels[chan] = lp_build_alloca(builder, bld->texel_bld.vec_type, "");
1006      lp_build_name(texels[chan], "sampler%u_texel_%c_var", unit, "xyzw"[chan]);
1007    }
1008
1009    if (min_filter == mag_filter) {
1010       /* no need to distinquish between minification and magnification */
1011       lp_build_sample_mipmap(bld, unit,
1012                              min_filter, mip_filter,
1013                              s, t, r,
1014                              ilevel0, ilevel1, lod_fpart,
1015                              texels);
1016    }
1017    else {
1018       /* Emit conditional to choose min image filter or mag image filter
1019        * depending on the lod being > 0 or <= 0, respectively.
1020        */
1021       struct lp_build_flow_context *flow_ctx;
1022       struct lp_build_if_state if_ctx;
1023       LLVMValueRef minify;
1024
1025       flow_ctx = lp_build_flow_create(builder);
1026
1027       /* minify = lod >= 0.0 */
1028       minify = LLVMBuildICmp(builder, LLVMIntSGE,
1029                              lod_ipart, int_bld->zero, "");
1030
1031       lp_build_if(&if_ctx, flow_ctx, builder, minify);
1032       {
1033          /* Use the minification filter */
1034          lp_build_sample_mipmap(bld, unit,
1035                                 min_filter, mip_filter,
1036                                 s, t, r,
1037                                 ilevel0, ilevel1, lod_fpart,
1038                                 texels);
1039       }
1040       lp_build_else(&if_ctx);
1041       {
1042          /* Use the magnification filter */
1043          lp_build_sample_mipmap(bld, unit,
1044                                 mag_filter, PIPE_TEX_MIPFILTER_NONE,
1045                                 s, t, r,
1046                                 i32t_zero, NULL, NULL,
1047                                 texels);
1048       }
1049       lp_build_endif(&if_ctx);
1050
1051       lp_build_flow_destroy(flow_ctx);
1052    }
1053
1054    for (chan = 0; chan < 4; ++chan) {
1055      colors_out[chan] = LLVMBuildLoad(builder, texels[chan], "");
1056      lp_build_name(colors_out[chan], "sampler%u_texel_%c", unit, "xyzw"[chan]);
1057    }
1058 }
1059
1060
1061 /**
1062  * Do shadow test/comparison.
1063  * \param p  the texcoord Z (aka R, aka P) component
1064  * \param texel  the texel to compare against (use the X channel)
1065  */
1066 static void
1067 lp_build_sample_compare(struct lp_build_sample_context *bld,
1068                         LLVMValueRef p,
1069                         LLVMValueRef texel[4])
1070 {
1071    struct lp_build_context *texel_bld = &bld->texel_bld;
1072    LLVMValueRef res;
1073    const unsigned chan = 0;
1074
1075    if (bld->static_state->compare_mode == PIPE_TEX_COMPARE_NONE)
1076       return;
1077
1078    /* debug code */
1079    if (0) {
1080       LLVMValueRef indx = lp_build_const_int32(0);
1081       LLVMValueRef coord = LLVMBuildExtractElement(bld->builder, p, indx, "");
1082       LLVMValueRef tex = LLVMBuildExtractElement(bld->builder,
1083                                                  texel[chan], indx, "");
1084       lp_build_printf(bld->builder, "shadow compare coord %f to texture %f\n",
1085                       coord, tex);
1086    }
1087
1088    /* result = (p FUNC texel) ? 1 : 0 */
1089    res = lp_build_cmp(texel_bld, bld->static_state->compare_func,
1090                       p, texel[chan]);
1091    res = lp_build_select(texel_bld, res, texel_bld->one, texel_bld->zero);
1092
1093    /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1094    texel[0] =
1095    texel[1] =
1096    texel[2] = res;
1097    texel[3] = texel_bld->one;
1098 }
1099
1100
1101 /**
1102  * Just set texels to white instead of actually sampling the texture.
1103  * For debugging.
1104  */
1105 void
1106 lp_build_sample_nop(struct lp_type type,
1107                     LLVMValueRef texel_out[4])
1108 {
1109    LLVMValueRef one = lp_build_one(type);
1110    unsigned chan;
1111
1112    for (chan = 0; chan < 4; chan++) {
1113       texel_out[chan] = one;
1114    }  
1115 }
1116
1117
1118 /**
1119  * Build texture sampling code.
1120  * 'texel' will return a vector of four LLVMValueRefs corresponding to
1121  * R, G, B, A.
1122  * \param type  vector float type to use for coords, etc.
1123  * \param ddx  partial derivatives of (s,t,r,q) with respect to x
1124  * \param ddy  partial derivatives of (s,t,r,q) with respect to y
1125  */
1126 void
1127 lp_build_sample_soa(LLVMBuilderRef builder,
1128                     const struct lp_sampler_static_state *static_state,
1129                     struct lp_sampler_dynamic_state *dynamic_state,
1130                     struct lp_type type,
1131                     unsigned unit,
1132                     unsigned num_coords,
1133                     const LLVMValueRef *coords,
1134                     const LLVMValueRef ddx[4],
1135                     const LLVMValueRef ddy[4],
1136                     LLVMValueRef lod_bias, /* optional */
1137                     LLVMValueRef explicit_lod, /* optional */
1138                     LLVMValueRef texel_out[4])
1139 {
1140    unsigned dims = texture_dims(static_state->target);
1141    struct lp_build_sample_context bld;
1142    LLVMTypeRef i32t = LLVMInt32Type();
1143
1144    LLVMValueRef s;
1145    LLVMValueRef t;
1146    LLVMValueRef r;
1147    struct lp_type float_vec_type;
1148
1149    if (0) {
1150       enum pipe_format fmt = static_state->format;
1151       debug_printf("Sample from %s\n", util_format_name(fmt));
1152    }
1153
1154    assert(type.floating);
1155
1156    /* Setup our build context */
1157    memset(&bld, 0, sizeof bld);
1158    bld.builder = builder;
1159    bld.static_state = static_state;
1160    bld.dynamic_state = dynamic_state;
1161    bld.format_desc = util_format_description(static_state->format);
1162    bld.dims = dims;
1163
1164    bld.float_type = lp_type_float(32);
1165    bld.int_type = lp_type_int(32);
1166    bld.coord_type = type;
1167    bld.uint_coord_type = lp_uint_type(type);
1168    bld.int_coord_type = lp_int_type(type);
1169    bld.float_size_type = lp_type_float(32);
1170    bld.float_size_type.length = dims > 1 ? 4 : 1;
1171    bld.int_size_type = lp_int_type(bld.float_size_type);
1172    bld.texel_type = type;
1173
1174    float_vec_type = lp_type_float_vec(32);
1175
1176    lp_build_context_init(&bld.float_bld, builder, bld.float_type);
1177    lp_build_context_init(&bld.float_vec_bld, builder, float_vec_type);
1178    lp_build_context_init(&bld.int_bld, builder, bld.int_type);
1179    lp_build_context_init(&bld.coord_bld, builder, bld.coord_type);
1180    lp_build_context_init(&bld.uint_coord_bld, builder, bld.uint_coord_type);
1181    lp_build_context_init(&bld.int_coord_bld, builder, bld.int_coord_type);
1182    lp_build_context_init(&bld.int_size_bld, builder, bld.int_size_type);
1183    lp_build_context_init(&bld.float_size_bld, builder, bld.float_size_type);
1184    lp_build_context_init(&bld.texel_bld, builder, bld.texel_type);
1185
1186    /* Get the dynamic state */
1187    bld.width = dynamic_state->width(dynamic_state, builder, unit);
1188    bld.height = dynamic_state->height(dynamic_state, builder, unit);
1189    bld.depth = dynamic_state->depth(dynamic_state, builder, unit);
1190    bld.row_stride_array = dynamic_state->row_stride(dynamic_state, builder, unit);
1191    bld.img_stride_array = dynamic_state->img_stride(dynamic_state, builder, unit);
1192    bld.data_array = dynamic_state->data_ptr(dynamic_state, builder, unit);
1193    /* Note that data_array is an array[level] of pointers to texture images */
1194
1195    s = coords[0];
1196    t = coords[1];
1197    r = coords[2];
1198
1199    /* width, height, depth as single int vector */
1200    if (dims <= 1) {
1201       bld.int_size = bld.width;
1202    }
1203    else {
1204       bld.int_size = LLVMBuildInsertElement(builder, bld.int_size_bld.undef,
1205                                             bld.width, LLVMConstInt(i32t, 0, 0), "");
1206       if (dims >= 2) {
1207          bld.int_size = LLVMBuildInsertElement(builder, bld.int_size,
1208                                                bld.height, LLVMConstInt(i32t, 1, 0), "");
1209          if (dims >= 3) {
1210             bld.int_size = LLVMBuildInsertElement(builder, bld.int_size,
1211                                                   bld.depth, LLVMConstInt(i32t, 2, 0), "");
1212          }
1213       }
1214    }
1215
1216    if (0) {
1217       /* For debug: no-op texture sampling */
1218       lp_build_sample_nop(bld.texel_type, texel_out);
1219    }
1220    else if (util_format_fits_8unorm(bld.format_desc) &&
1221             lp_is_simple_wrap_mode(static_state->wrap_s) &&
1222             lp_is_simple_wrap_mode(static_state->wrap_t)) {
1223       /* do sampling/filtering with fixed pt arithmetic */
1224       lp_build_sample_aos(&bld, unit, s, t, r, ddx, ddy,
1225                           lod_bias, explicit_lod,
1226                           texel_out);
1227    }
1228
1229    else {
1230       if ((gallivm_debug & GALLIVM_DEBUG_PERF) &&
1231           util_format_fits_8unorm(bld.format_desc)) {
1232          debug_printf("%s: using floating point linear filtering for %s\n",
1233                       __FUNCTION__, bld.format_desc->short_name);
1234          debug_printf("  min_img %d  mag_img %d  mip %d  wraps %d  wrapt %d\n",
1235                       static_state->min_img_filter,
1236                       static_state->mag_img_filter,
1237                       static_state->min_mip_filter,
1238                       static_state->wrap_s,
1239                       static_state->wrap_t);
1240       }
1241
1242       lp_build_sample_general(&bld, unit, s, t, r, ddx, ddy,
1243                               lod_bias, explicit_lod,
1244                               texel_out);
1245    }
1246
1247    lp_build_sample_compare(&bld, r, texel_out);
1248 }