OSDN Git Service

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