OSDN Git Service

gallivm: change cubemaps / derivatives handling, take 55
[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 "pipe/p_shader_tokens.h"
39 #include "util/u_debug.h"
40 #include "util/u_dump.h"
41 #include "util/u_memory.h"
42 #include "util/u_math.h"
43 #include "util/u_format.h"
44 #include "util/u_cpu_detect.h"
45 #include "lp_bld_debug.h"
46 #include "lp_bld_type.h"
47 #include "lp_bld_const.h"
48 #include "lp_bld_conv.h"
49 #include "lp_bld_arit.h"
50 #include "lp_bld_bitarit.h"
51 #include "lp_bld_logic.h"
52 #include "lp_bld_printf.h"
53 #include "lp_bld_swizzle.h"
54 #include "lp_bld_flow.h"
55 #include "lp_bld_gather.h"
56 #include "lp_bld_format.h"
57 #include "lp_bld_sample.h"
58 #include "lp_bld_sample_aos.h"
59 #include "lp_bld_struct.h"
60 #include "lp_bld_quad.h"
61 #include "lp_bld_pack.h"
62
63
64 /**
65  * Generate code to fetch a texel from a texture at int coords (x, y, z).
66  * The computation depends on whether the texture is 1D, 2D or 3D.
67  * The result, texel, will be float vectors:
68  *   texel[0] = red values
69  *   texel[1] = green values
70  *   texel[2] = blue values
71  *   texel[3] = alpha values
72  */
73 static void
74 lp_build_sample_texel_soa(struct lp_build_sample_context *bld,
75                           unsigned sampler_unit,
76                           LLVMValueRef width,
77                           LLVMValueRef height,
78                           LLVMValueRef depth,
79                           LLVMValueRef x,
80                           LLVMValueRef y,
81                           LLVMValueRef z,
82                           LLVMValueRef y_stride,
83                           LLVMValueRef z_stride,
84                           LLVMValueRef data_ptr,
85                           LLVMValueRef mipoffsets,
86                           LLVMValueRef texel_out[4])
87 {
88    const struct lp_static_sampler_state *static_state = bld->static_sampler_state;
89    const unsigned dims = bld->dims;
90    struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
91    LLVMBuilderRef builder = bld->gallivm->builder;
92    LLVMValueRef offset;
93    LLVMValueRef i, j;
94    LLVMValueRef use_border = NULL;
95
96    /* use_border = x < 0 || x >= width || y < 0 || y >= height */
97    if (lp_sampler_wrap_mode_uses_border_color(static_state->wrap_s,
98                                               static_state->min_img_filter,
99                                               static_state->mag_img_filter)) {
100       LLVMValueRef b1, b2;
101       b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, x, int_coord_bld->zero);
102       b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, x, width);
103       use_border = LLVMBuildOr(builder, b1, b2, "b1_or_b2");
104    }
105
106    if (dims >= 2 &&
107        lp_sampler_wrap_mode_uses_border_color(static_state->wrap_t,
108                                               static_state->min_img_filter,
109                                               static_state->mag_img_filter)) {
110       LLVMValueRef b1, b2;
111       b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, y, int_coord_bld->zero);
112       b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, y, height);
113       if (use_border) {
114          use_border = LLVMBuildOr(builder, use_border, b1, "ub_or_b1");
115          use_border = LLVMBuildOr(builder, use_border, b2, "ub_or_b2");
116       }
117       else {
118          use_border = LLVMBuildOr(builder, b1, b2, "b1_or_b2");
119       }
120    }
121
122    if (dims == 3 &&
123        lp_sampler_wrap_mode_uses_border_color(static_state->wrap_r,
124                                               static_state->min_img_filter,
125                                               static_state->mag_img_filter)) {
126       LLVMValueRef b1, b2;
127       b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, z, int_coord_bld->zero);
128       b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, z, depth);
129       if (use_border) {
130          use_border = LLVMBuildOr(builder, use_border, b1, "ub_or_b1");
131          use_border = LLVMBuildOr(builder, use_border, b2, "ub_or_b2");
132       }
133       else {
134          use_border = LLVMBuildOr(builder, b1, b2, "b1_or_b2");
135       }
136    }
137
138    /* convert x,y,z coords to linear offset from start of texture, in bytes */
139    lp_build_sample_offset(&bld->int_coord_bld,
140                           bld->format_desc,
141                           x, y, z, y_stride, z_stride,
142                           &offset, &i, &j);
143    if (mipoffsets) {
144       offset = lp_build_add(&bld->int_coord_bld, offset, mipoffsets);
145    }
146
147    if (use_border) {
148       /* If we can sample the border color, it means that texcoords may
149        * lie outside the bounds of the texture image.  We need to do
150        * something to prevent reading out of bounds and causing a segfault.
151        *
152        * Simply AND the texture coords with !use_border.  This will cause
153        * coords which are out of bounds to become zero.  Zero's guaranteed
154        * to be inside the texture image.
155        */
156       offset = lp_build_andnot(&bld->int_coord_bld, offset, use_border);
157    }
158
159    lp_build_fetch_rgba_soa(bld->gallivm,
160                            bld->format_desc,
161                            bld->texel_type,
162                            data_ptr, offset,
163                            i, j,
164                            texel_out);
165
166    /*
167     * Note: if we find an app which frequently samples the texture border
168     * we might want to implement a true conditional here to avoid sampling
169     * the texture whenever possible (since that's quite a bit of code).
170     * Ex:
171     *   if (use_border) {
172     *      texel = border_color;
173     *   }
174     *   else {
175     *      texel = sample_texture(coord);
176     *   }
177     * As it is now, we always sample the texture, then selectively replace
178     * the texel color results with the border color.
179     */
180
181    if (use_border) {
182       /* select texel color or border color depending on use_border */
183       LLVMValueRef border_color_ptr = 
184          bld->dynamic_state->border_color(bld->dynamic_state,
185                                           bld->gallivm, sampler_unit);
186       int chan;
187       for (chan = 0; chan < 4; chan++) {
188          LLVMValueRef border_chan =
189             lp_build_array_get(bld->gallivm, border_color_ptr,
190                                lp_build_const_int32(bld->gallivm, chan));
191          LLVMValueRef border_chan_vec =
192             lp_build_broadcast_scalar(&bld->float_vec_bld, border_chan);
193
194          if (!bld->texel_type.floating) {
195             border_chan_vec = LLVMBuildBitCast(builder, border_chan_vec,
196                                                bld->texel_bld.vec_type, "");
197          }
198          texel_out[chan] = lp_build_select(&bld->texel_bld, use_border,
199                                            border_chan_vec, texel_out[chan]);
200       }
201    }
202 }
203
204
205 /**
206  * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
207  */
208 static LLVMValueRef
209 lp_build_coord_mirror(struct lp_build_sample_context *bld,
210                       LLVMValueRef coord)
211 {
212    struct lp_build_context *coord_bld = &bld->coord_bld;
213    struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
214    LLVMValueRef fract, flr, isOdd;
215
216    lp_build_ifloor_fract(coord_bld, coord, &flr, &fract);
217
218    /* isOdd = flr & 1 */
219    isOdd = LLVMBuildAnd(bld->gallivm->builder, flr, int_coord_bld->one, "");
220
221    /* make coord positive or negative depending on isOdd */
222    coord = lp_build_set_sign(coord_bld, fract, isOdd);
223
224    /* convert isOdd to float */
225    isOdd = lp_build_int_to_float(coord_bld, isOdd);
226
227    /* add isOdd to coord */
228    coord = lp_build_add(coord_bld, coord, isOdd);
229
230    return coord;
231 }
232
233
234 /**
235  * Helper to compute the first coord and the weight for
236  * linear wrap repeat npot textures
237  */
238 void
239 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context *bld,
240                                   LLVMValueRef coord_f,
241                                   LLVMValueRef length_i,
242                                   LLVMValueRef length_f,
243                                   LLVMValueRef *coord0_i,
244                                   LLVMValueRef *weight_f)
245 {
246    struct lp_build_context *coord_bld = &bld->coord_bld;
247    struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
248    LLVMValueRef half = lp_build_const_vec(bld->gallivm, coord_bld->type, 0.5);
249    LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length_i,
250                                                 int_coord_bld->one);
251    LLVMValueRef mask;
252    /* wrap with normalized floats is just fract */
253    coord_f = lp_build_fract(coord_bld, coord_f);
254    /* mul by size and subtract 0.5 */
255    coord_f = lp_build_mul(coord_bld, coord_f, length_f);
256    coord_f = lp_build_sub(coord_bld, coord_f, half);
257    /*
258     * we avoided the 0.5/length division before the repeat wrap,
259     * now need to fix up edge cases with selects
260     */
261    /* convert to int, compute lerp weight */
262    lp_build_ifloor_fract(coord_bld, coord_f, coord0_i, weight_f);
263    mask = lp_build_compare(int_coord_bld->gallivm, int_coord_bld->type,
264                            PIPE_FUNC_LESS, *coord0_i, int_coord_bld->zero);
265    *coord0_i = lp_build_select(int_coord_bld, mask, length_minus_one, *coord0_i);
266 }
267
268
269 /**
270  * Build LLVM code for texture wrap mode for linear filtering.
271  * \param x0_out  returns first integer texcoord
272  * \param x1_out  returns second integer texcoord
273  * \param weight_out  returns linear interpolation weight
274  */
275 static void
276 lp_build_sample_wrap_linear(struct lp_build_sample_context *bld,
277                             LLVMValueRef coord,
278                             LLVMValueRef length,
279                             LLVMValueRef length_f,
280                             LLVMValueRef offset,
281                             boolean is_pot,
282                             unsigned wrap_mode,
283                             LLVMValueRef *x0_out,
284                             LLVMValueRef *x1_out,
285                             LLVMValueRef *weight_out)
286 {
287    struct lp_build_context *coord_bld = &bld->coord_bld;
288    struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
289    LLVMBuilderRef builder = bld->gallivm->builder;
290    LLVMValueRef half = lp_build_const_vec(bld->gallivm, coord_bld->type, 0.5);
291    LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length, int_coord_bld->one);
292    LLVMValueRef coord0, coord1, weight;
293
294    switch(wrap_mode) {
295    case PIPE_TEX_WRAP_REPEAT:
296       if (is_pot) {
297          /* mul by size and subtract 0.5 */
298          coord = lp_build_mul(coord_bld, coord, length_f);
299          coord = lp_build_sub(coord_bld, coord, half);
300          if (offset) {
301             offset = lp_build_int_to_float(coord_bld, offset);
302             coord = lp_build_add(coord_bld, coord, offset);
303          }
304          /* convert to int, compute lerp weight */
305          lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
306          coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
307          /* repeat wrap */
308          coord0 = LLVMBuildAnd(builder, coord0, length_minus_one, "");
309          coord1 = LLVMBuildAnd(builder, coord1, length_minus_one, "");
310       }
311       else {
312          LLVMValueRef mask;
313          if (offset) {
314             offset = lp_build_int_to_float(coord_bld, offset);
315             offset = lp_build_div(coord_bld, offset, length_f);
316             coord = lp_build_add(coord_bld, coord, offset);
317          }
318          lp_build_coord_repeat_npot_linear(bld, coord,
319                                            length, length_f,
320                                            &coord0, &weight);
321          mask = lp_build_compare(int_coord_bld->gallivm, int_coord_bld->type,
322                                  PIPE_FUNC_NOTEQUAL, coord0, length_minus_one);
323          coord1 = LLVMBuildAnd(builder,
324                                lp_build_add(int_coord_bld, coord0, int_coord_bld->one),
325                                mask, "");
326       }
327       break;
328
329    case PIPE_TEX_WRAP_CLAMP:
330       if (bld->static_sampler_state->normalized_coords) {
331          /* scale coord to length */
332          coord = lp_build_mul(coord_bld, coord, length_f);
333       }
334       if (offset) {
335          offset = lp_build_int_to_float(coord_bld, offset);
336          coord = lp_build_add(coord_bld, coord, offset);
337       }
338
339       /* clamp to [0, length] */
340       coord = lp_build_clamp(coord_bld, coord, coord_bld->zero, length_f);
341
342       coord = lp_build_sub(coord_bld, coord, half);
343
344       /* convert to int, compute lerp weight */
345       lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
346       coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
347       break;
348
349    case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
350       {
351          struct lp_build_context abs_coord_bld = bld->coord_bld;
352          abs_coord_bld.type.sign = FALSE;
353
354          if (bld->static_sampler_state->normalized_coords) {
355             /* mul by tex size */
356             coord = lp_build_mul(coord_bld, coord, length_f);
357          }
358          if (offset) {
359             offset = lp_build_int_to_float(coord_bld, offset);
360             coord = lp_build_add(coord_bld, coord, offset);
361          }
362
363          /* clamp to length max */
364          coord = lp_build_min(coord_bld, coord, length_f);
365          /* subtract 0.5 */
366          coord = lp_build_sub(coord_bld, coord, half);
367          /* clamp to [0, length - 0.5] */
368          coord = lp_build_max(coord_bld, coord, coord_bld->zero);
369          /* convert to int, compute lerp weight */
370          lp_build_ifloor_fract(&abs_coord_bld, coord, &coord0, &weight);
371          coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
372          /* coord1 = min(coord1, length-1) */
373          coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
374          break;
375       }
376
377    case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
378       if (bld->static_sampler_state->normalized_coords) {
379          /* scale coord to length */
380          coord = lp_build_mul(coord_bld, coord, length_f);
381       }
382       if (offset) {
383          offset = lp_build_int_to_float(coord_bld, offset);
384          coord = lp_build_add(coord_bld, coord, offset);
385       }
386       /* was: clamp to [-0.5, length + 0.5], then sub 0.5 */
387       /* can skip clamp (though might not work for very large coord values */
388       coord = lp_build_sub(coord_bld, coord, half);
389       /* convert to int, compute lerp weight */
390       lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
391       coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
392       break;
393
394    case PIPE_TEX_WRAP_MIRROR_REPEAT:
395       /* compute mirror function */
396       coord = lp_build_coord_mirror(bld, coord);
397
398       /* scale coord to length */
399       coord = lp_build_mul(coord_bld, coord, length_f);
400       coord = lp_build_sub(coord_bld, coord, half);
401       if (offset) {
402          offset = lp_build_int_to_float(coord_bld, offset);
403          coord = lp_build_add(coord_bld, coord, offset);
404       }
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       /* coord0 = max(coord0, 0) */
411       coord0 = lp_build_max(int_coord_bld, coord0, int_coord_bld->zero);
412       /* coord1 = min(coord1, length-1) */
413       coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
414       break;
415
416    case PIPE_TEX_WRAP_MIRROR_CLAMP:
417       if (bld->static_sampler_state->normalized_coords) {
418          /* scale coord to length */
419          coord = lp_build_mul(coord_bld, coord, length_f);
420       }
421       if (offset) {
422          offset = lp_build_int_to_float(coord_bld, offset);
423          coord = lp_build_add(coord_bld, coord, offset);
424       }
425       coord = lp_build_abs(coord_bld, coord);
426
427       /* clamp to [0, length] */
428       coord = lp_build_min(coord_bld, coord, length_f);
429
430       coord = lp_build_sub(coord_bld, coord, half);
431
432       /* convert to int, compute lerp weight */
433       lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
434       coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
435       break;
436
437    case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
438       {
439          LLVMValueRef min, max;
440          struct lp_build_context abs_coord_bld = bld->coord_bld;
441          abs_coord_bld.type.sign = FALSE;
442
443          if (bld->static_sampler_state->normalized_coords) {
444             /* scale coord to length */
445             coord = lp_build_mul(coord_bld, coord, length_f);
446          }
447          if (offset) {
448             offset = lp_build_int_to_float(coord_bld, offset);
449             coord = lp_build_add(coord_bld, coord, offset);
450          }
451          coord = lp_build_abs(coord_bld, coord);
452
453          /* clamp to [0.5, length - 0.5] */
454          min = half;
455          max = lp_build_sub(coord_bld, length_f, min);
456          coord = lp_build_clamp(coord_bld, coord, min, max);
457
458          coord = lp_build_sub(coord_bld, coord, half);
459
460          /* convert to int, compute lerp weight */
461          lp_build_ifloor_fract(&abs_coord_bld, coord, &coord0, &weight);
462          coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
463       }
464       break;
465
466    case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
467       {
468          if (bld->static_sampler_state->normalized_coords) {
469             /* scale coord to length */
470             coord = lp_build_mul(coord_bld, coord, length_f);
471          }
472          if (offset) {
473             offset = lp_build_int_to_float(coord_bld, offset);
474             coord = lp_build_add(coord_bld, coord, offset);
475          }
476          coord = lp_build_abs(coord_bld, coord);
477
478          /* was: clamp to [-0.5, length + 0.5] then sub 0.5 */
479          /* skip clamp - always positive, and other side
480             only potentially matters for very large coords */
481          coord = lp_build_sub(coord_bld, coord, half);
482
483          /* convert to int, compute lerp weight */
484          lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
485          coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
486       }
487       break;
488
489    default:
490       assert(0);
491       coord0 = NULL;
492       coord1 = NULL;
493       weight = NULL;
494    }
495
496    *x0_out = coord0;
497    *x1_out = coord1;
498    *weight_out = weight;
499 }
500
501
502 /**
503  * Build LLVM code for texture wrap mode for nearest filtering.
504  * \param coord  the incoming texcoord (nominally in [0,1])
505  * \param length  the texture size along one dimension, as int vector
506  * \param length_f  the texture size along one dimension, as float vector
507  * \param offset  texel offset along one dimension (as int vector)
508  * \param is_pot  if TRUE, length is a power of two
509  * \param wrap_mode  one of PIPE_TEX_WRAP_x
510  */
511 static LLVMValueRef
512 lp_build_sample_wrap_nearest(struct lp_build_sample_context *bld,
513                              LLVMValueRef coord,
514                              LLVMValueRef length,
515                              LLVMValueRef length_f,
516                              LLVMValueRef offset,
517                              boolean is_pot,
518                              unsigned wrap_mode)
519 {
520    struct lp_build_context *coord_bld = &bld->coord_bld;
521    struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
522    LLVMBuilderRef builder = bld->gallivm->builder;
523    LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length, int_coord_bld->one);
524    LLVMValueRef icoord;
525    
526    switch(wrap_mode) {
527    case PIPE_TEX_WRAP_REPEAT:
528       if (is_pot) {
529          coord = lp_build_mul(coord_bld, coord, length_f);
530          icoord = lp_build_ifloor(coord_bld, coord);
531          if (offset) {
532             icoord = lp_build_add(int_coord_bld, icoord, offset);
533          }
534          icoord = LLVMBuildAnd(builder, icoord, length_minus_one, "");
535       }
536       else {
537           if (offset) {
538              offset = lp_build_int_to_float(coord_bld, offset);
539              offset = lp_build_div(coord_bld, offset, length_f);
540              coord = lp_build_add(coord_bld, coord, offset);
541           }
542           /* take fraction, unnormalize */
543           coord = lp_build_fract_safe(coord_bld, coord);
544           coord = lp_build_mul(coord_bld, coord, length_f);
545           icoord = lp_build_itrunc(coord_bld, coord);
546       }
547       break;
548
549    case PIPE_TEX_WRAP_CLAMP:
550    case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
551       if (bld->static_sampler_state->normalized_coords) {
552          /* scale coord to length */
553          coord = lp_build_mul(coord_bld, coord, length_f);
554       }
555
556       /* floor */
557       /* use itrunc instead since we clamp to 0 anyway */
558       icoord = lp_build_itrunc(coord_bld, coord);
559       if (offset) {
560          icoord = lp_build_add(int_coord_bld, icoord, offset);
561       }
562
563       /* clamp to [0, length - 1]. */
564       icoord = lp_build_clamp(int_coord_bld, icoord, int_coord_bld->zero,
565                               length_minus_one);
566       break;
567
568    case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
569       if (bld->static_sampler_state->normalized_coords) {
570          /* scale coord to length */
571          coord = lp_build_mul(coord_bld, coord, length_f);
572       }
573       /* no clamp necessary, border masking will handle this */
574       icoord = lp_build_ifloor(coord_bld, coord);
575       if (offset) {
576          icoord = lp_build_add(int_coord_bld, icoord, offset);
577       }
578       break;
579
580    case PIPE_TEX_WRAP_MIRROR_REPEAT:
581       if (offset) {
582          offset = lp_build_int_to_float(coord_bld, offset);
583          offset = lp_build_div(coord_bld, offset, length_f);
584          coord = lp_build_add(coord_bld, coord, offset);
585       }
586       /* compute mirror function */
587       coord = lp_build_coord_mirror(bld, coord);
588
589       /* scale coord to length */
590       assert(bld->static_sampler_state->normalized_coords);
591       coord = lp_build_mul(coord_bld, coord, length_f);
592
593       /* itrunc == ifloor here */
594       icoord = lp_build_itrunc(coord_bld, coord);
595
596       /* clamp to [0, length - 1] */
597       icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
598       break;
599
600    case PIPE_TEX_WRAP_MIRROR_CLAMP:
601    case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
602       if (bld->static_sampler_state->normalized_coords) {
603          /* scale coord to length */
604          coord = lp_build_mul(coord_bld, coord, length_f);
605       }
606       if (offset) {
607          offset = lp_build_int_to_float(coord_bld, offset);
608          coord = lp_build_add(coord_bld, coord, offset);
609       }
610       coord = lp_build_abs(coord_bld, coord);
611
612       /* itrunc == ifloor here */
613       icoord = lp_build_itrunc(coord_bld, coord);
614
615       /* clamp to [0, length - 1] */
616       icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
617       break;
618
619    case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
620       if (bld->static_sampler_state->normalized_coords) {
621          /* scale coord to length */
622          coord = lp_build_mul(coord_bld, coord, length_f);
623       }
624       if (offset) {
625          offset = lp_build_int_to_float(coord_bld, offset);
626          coord = lp_build_add(coord_bld, coord, offset);
627       }
628       coord = lp_build_abs(coord_bld, coord);
629
630       /* itrunc == ifloor here */
631       icoord = lp_build_itrunc(coord_bld, coord);
632       break;
633
634    default:
635       assert(0);
636       icoord = NULL;
637    }
638
639    return icoord;
640 }
641
642
643 /**
644  * Generate code to sample a mipmap level with nearest filtering.
645  * If sampling a cube texture, r = cube face in [0,5].
646  */
647 static void
648 lp_build_sample_image_nearest(struct lp_build_sample_context *bld,
649                               unsigned sampler_unit,
650                               LLVMValueRef size,
651                               LLVMValueRef row_stride_vec,
652                               LLVMValueRef img_stride_vec,
653                               LLVMValueRef data_ptr,
654                               LLVMValueRef mipoffsets,
655                               LLVMValueRef s,
656                               LLVMValueRef t,
657                               LLVMValueRef r,
658                               const LLVMValueRef *offsets,
659                               LLVMValueRef colors_out[4])
660 {
661    const unsigned dims = bld->dims;
662    LLVMValueRef width_vec;
663    LLVMValueRef height_vec;
664    LLVMValueRef depth_vec;
665    LLVMValueRef flt_size;
666    LLVMValueRef flt_width_vec;
667    LLVMValueRef flt_height_vec;
668    LLVMValueRef flt_depth_vec;
669    LLVMValueRef x, y = NULL, z = NULL;
670
671    lp_build_extract_image_sizes(bld,
672                                 &bld->int_size_bld,
673                                 bld->int_coord_type,
674                                 size,
675                                 &width_vec, &height_vec, &depth_vec);
676
677    flt_size = lp_build_int_to_float(&bld->float_size_bld, size);
678
679    lp_build_extract_image_sizes(bld,
680                                 &bld->float_size_bld,
681                                 bld->coord_type,
682                                 flt_size,
683                                 &flt_width_vec, &flt_height_vec, &flt_depth_vec);
684
685    /*
686     * Compute integer texcoords.
687     */
688    x = lp_build_sample_wrap_nearest(bld, s, width_vec, flt_width_vec, offsets[0],
689                                     bld->static_texture_state->pot_width,
690                                     bld->static_sampler_state->wrap_s);
691    lp_build_name(x, "tex.x.wrapped");
692
693    if (dims >= 2) {
694       y = lp_build_sample_wrap_nearest(bld, t, height_vec, flt_height_vec, offsets[1],
695                                        bld->static_texture_state->pot_height,
696                                        bld->static_sampler_state->wrap_t);
697       lp_build_name(y, "tex.y.wrapped");
698
699       if (dims == 3) {
700          z = lp_build_sample_wrap_nearest(bld, r, depth_vec, flt_depth_vec, offsets[2],
701                                           bld->static_texture_state->pot_depth,
702                                           bld->static_sampler_state->wrap_r);
703          lp_build_name(z, "tex.z.wrapped");
704       }
705    }
706    if (bld->static_texture_state->target == PIPE_TEXTURE_CUBE ||
707        bld->static_texture_state->target == PIPE_TEXTURE_1D_ARRAY ||
708        bld->static_texture_state->target == PIPE_TEXTURE_2D_ARRAY) {
709       z = r;
710       lp_build_name(z, "tex.z.layer");
711    }
712
713    /*
714     * Get texture colors.
715     */
716    lp_build_sample_texel_soa(bld, sampler_unit,
717                              width_vec, height_vec, depth_vec,
718                              x, y, z,
719                              row_stride_vec, img_stride_vec,
720                              data_ptr, mipoffsets, colors_out);
721 }
722
723
724 /**
725  * Generate code to sample a mipmap level with linear filtering.
726  * If sampling a cube texture, r = cube face in [0,5].
727  */
728 static void
729 lp_build_sample_image_linear(struct lp_build_sample_context *bld,
730                              unsigned sampler_unit,
731                              LLVMValueRef size,
732                              LLVMValueRef row_stride_vec,
733                              LLVMValueRef img_stride_vec,
734                              LLVMValueRef data_ptr,
735                              LLVMValueRef mipoffsets,
736                              LLVMValueRef s,
737                              LLVMValueRef t,
738                              LLVMValueRef r,
739                              const LLVMValueRef *offsets,
740                              LLVMValueRef colors_out[4])
741 {
742    const unsigned dims = bld->dims;
743    LLVMValueRef width_vec;
744    LLVMValueRef height_vec;
745    LLVMValueRef depth_vec;
746    LLVMValueRef flt_size;
747    LLVMValueRef flt_width_vec;
748    LLVMValueRef flt_height_vec;
749    LLVMValueRef flt_depth_vec;
750    LLVMValueRef x0, y0 = NULL, z0 = NULL, x1, y1 = NULL, z1 = NULL;
751    LLVMValueRef s_fpart, t_fpart = NULL, r_fpart = NULL;
752    LLVMValueRef neighbors[2][2][4];
753    int chan;
754
755    lp_build_extract_image_sizes(bld,
756                                 &bld->int_size_bld,
757                                 bld->int_coord_type,
758                                 size,
759                                 &width_vec, &height_vec, &depth_vec);
760
761    flt_size = lp_build_int_to_float(&bld->float_size_bld, size);
762
763    lp_build_extract_image_sizes(bld,
764                                 &bld->float_size_bld,
765                                 bld->coord_type,
766                                 flt_size,
767                                 &flt_width_vec, &flt_height_vec, &flt_depth_vec);
768
769    /*
770     * Compute integer texcoords.
771     */
772    lp_build_sample_wrap_linear(bld, s, width_vec, flt_width_vec, offsets[0],
773                                bld->static_texture_state->pot_width,
774                                bld->static_sampler_state->wrap_s,
775                                &x0, &x1, &s_fpart);
776    lp_build_name(x0, "tex.x0.wrapped");
777    lp_build_name(x1, "tex.x1.wrapped");
778
779    if (dims >= 2) {
780       lp_build_sample_wrap_linear(bld, t, height_vec, flt_height_vec, offsets[1],
781                                   bld->static_texture_state->pot_height,
782                                   bld->static_sampler_state->wrap_t,
783                                   &y0, &y1, &t_fpart);
784       lp_build_name(y0, "tex.y0.wrapped");
785       lp_build_name(y1, "tex.y1.wrapped");
786
787       if (dims == 3) {
788          lp_build_sample_wrap_linear(bld, r, depth_vec, flt_depth_vec, offsets[2],
789                                      bld->static_texture_state->pot_depth,
790                                      bld->static_sampler_state->wrap_r,
791                                      &z0, &z1, &r_fpart);
792          lp_build_name(z0, "tex.z0.wrapped");
793          lp_build_name(z1, "tex.z1.wrapped");
794       }
795    }
796    if (bld->static_texture_state->target == PIPE_TEXTURE_CUBE ||
797        bld->static_texture_state->target == PIPE_TEXTURE_1D_ARRAY ||
798        bld->static_texture_state->target == PIPE_TEXTURE_2D_ARRAY) {
799       z0 = z1 = r;  /* cube face or array layer */
800       lp_build_name(z0, "tex.z0.layer");
801       lp_build_name(z1, "tex.z1.layer");
802    }
803
804
805    /*
806     * Get texture colors.
807     */
808    /* get x0/x1 texels */
809    lp_build_sample_texel_soa(bld, sampler_unit,
810                              width_vec, height_vec, depth_vec,
811                              x0, y0, z0,
812                              row_stride_vec, img_stride_vec,
813                              data_ptr, mipoffsets, neighbors[0][0]);
814    lp_build_sample_texel_soa(bld, sampler_unit,
815                              width_vec, height_vec, depth_vec,
816                              x1, y0, z0,
817                              row_stride_vec, img_stride_vec,
818                              data_ptr, mipoffsets, neighbors[0][1]);
819
820    if (dims == 1) {
821       /* Interpolate two samples from 1D image to produce one color */
822       for (chan = 0; chan < 4; chan++) {
823          colors_out[chan] = lp_build_lerp(&bld->texel_bld, s_fpart,
824                                           neighbors[0][0][chan],
825                                           neighbors[0][1][chan]);
826       }
827    }
828    else {
829       /* 2D/3D texture */
830       LLVMValueRef colors0[4];
831
832       /* get x0/x1 texels at y1 */
833       lp_build_sample_texel_soa(bld, sampler_unit,
834                                 width_vec, height_vec, depth_vec,
835                                 x0, y1, z0,
836                                 row_stride_vec, img_stride_vec,
837                                 data_ptr, mipoffsets, neighbors[1][0]);
838       lp_build_sample_texel_soa(bld, sampler_unit,
839                                 width_vec, height_vec, depth_vec,
840                                 x1, y1, z0,
841                                 row_stride_vec, img_stride_vec,
842                                 data_ptr, mipoffsets, neighbors[1][1]);
843
844       /* Bilinear interpolate the four samples from the 2D image / 3D slice */
845       for (chan = 0; chan < 4; chan++) {
846          colors0[chan] = lp_build_lerp_2d(&bld->texel_bld,
847                                           s_fpart, t_fpart,
848                                           neighbors[0][0][chan],
849                                           neighbors[0][1][chan],
850                                           neighbors[1][0][chan],
851                                           neighbors[1][1][chan]);
852       }
853
854       if (dims == 3) {
855          LLVMValueRef neighbors1[2][2][4];
856          LLVMValueRef colors1[4];
857
858          /* get x0/x1/y0/y1 texels at z1 */
859          lp_build_sample_texel_soa(bld, sampler_unit,
860                                    width_vec, height_vec, depth_vec,
861                                    x0, y0, z1,
862                                    row_stride_vec, img_stride_vec,
863                                    data_ptr, mipoffsets, neighbors1[0][0]);
864          lp_build_sample_texel_soa(bld, sampler_unit,
865                                    width_vec, height_vec, depth_vec,
866                                    x1, y0, z1,
867                                    row_stride_vec, img_stride_vec,
868                                    data_ptr, mipoffsets, neighbors1[0][1]);
869          lp_build_sample_texel_soa(bld, sampler_unit,
870                                    width_vec, height_vec, depth_vec,
871                                    x0, y1, z1,
872                                    row_stride_vec, img_stride_vec,
873                                    data_ptr, mipoffsets, neighbors1[1][0]);
874          lp_build_sample_texel_soa(bld, sampler_unit,
875                                    width_vec, height_vec, depth_vec,
876                                    x1, y1, z1,
877                                    row_stride_vec, img_stride_vec,
878                                    data_ptr, mipoffsets, neighbors1[1][1]);
879
880          /* Bilinear interpolate the four samples from the second Z slice */
881          for (chan = 0; chan < 4; chan++) {
882             colors1[chan] = lp_build_lerp_2d(&bld->texel_bld,
883                                              s_fpart, t_fpart,
884                                              neighbors1[0][0][chan],
885                                              neighbors1[0][1][chan],
886                                              neighbors1[1][0][chan],
887                                              neighbors1[1][1][chan]);
888          }
889
890          /* Linearly interpolate the two samples from the two 3D slices */
891          for (chan = 0; chan < 4; chan++) {
892             colors_out[chan] = lp_build_lerp(&bld->texel_bld,
893                                              r_fpart,
894                                              colors0[chan], colors1[chan]);
895          }
896       }
897       else {
898          /* 2D tex */
899          for (chan = 0; chan < 4; chan++) {
900             colors_out[chan] = colors0[chan];
901          }
902       }
903    }
904 }
905
906
907 /**
908  * Sample the texture/mipmap using given image filter and mip filter.
909  * data0_ptr and data1_ptr point to the two mipmap levels to sample
910  * from.  width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
911  * If we're using nearest miplevel sampling the '1' values will be null/unused.
912  */
913 static void
914 lp_build_sample_mipmap(struct lp_build_sample_context *bld,
915                        unsigned sampler_unit,
916                        unsigned img_filter,
917                        unsigned mip_filter,
918                        LLVMValueRef s,
919                        LLVMValueRef t,
920                        LLVMValueRef r,
921                        const LLVMValueRef *offsets,
922                        LLVMValueRef ilevel0,
923                        LLVMValueRef ilevel1,
924                        LLVMValueRef lod_fpart,
925                        LLVMValueRef *colors_out)
926 {
927    LLVMBuilderRef builder = bld->gallivm->builder;
928    LLVMValueRef size0 = NULL;
929    LLVMValueRef size1 = NULL;
930    LLVMValueRef row_stride0_vec = NULL;
931    LLVMValueRef row_stride1_vec = NULL;
932    LLVMValueRef img_stride0_vec = NULL;
933    LLVMValueRef img_stride1_vec = NULL;
934    LLVMValueRef data_ptr0 = NULL;
935    LLVMValueRef data_ptr1 = NULL;
936    LLVMValueRef mipoff0 = NULL;
937    LLVMValueRef mipoff1 = NULL;
938    LLVMValueRef colors0[4], colors1[4];
939    unsigned chan;
940
941    /* sample the first mipmap level */
942    lp_build_mipmap_level_sizes(bld, ilevel0,
943                                &size0,
944                                &row_stride0_vec, &img_stride0_vec);
945    if (bld->num_lods == 1) {
946       data_ptr0 = lp_build_get_mipmap_level(bld, ilevel0);
947    }
948    else {
949       /* This path should work for num_lods 1 too but slightly less efficient */
950       data_ptr0 = bld->base_ptr;
951       mipoff0 = lp_build_get_mip_offsets(bld, ilevel0);
952    }
953    if (img_filter == PIPE_TEX_FILTER_NEAREST) {
954       lp_build_sample_image_nearest(bld, sampler_unit,
955                                     size0,
956                                     row_stride0_vec, img_stride0_vec,
957                                     data_ptr0, mipoff0, s, t, r, offsets,
958                                     colors0);
959    }
960    else {
961       assert(img_filter == PIPE_TEX_FILTER_LINEAR);
962       lp_build_sample_image_linear(bld, sampler_unit,
963                                    size0,
964                                    row_stride0_vec, img_stride0_vec,
965                                    data_ptr0, mipoff0, s, t, r, offsets,
966                                    colors0);
967    }
968
969    /* Store the first level's colors in the output variables */
970    for (chan = 0; chan < 4; chan++) {
971        LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
972    }
973
974    if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
975       struct lp_build_if_state if_ctx;
976       LLVMValueRef need_lerp;
977       unsigned num_quads = bld->coord_bld.type.length / 4;
978
979       /* need_lerp = lod_fpart > 0 */
980       if (num_quads == 1) {
981          need_lerp = LLVMBuildFCmp(builder, LLVMRealUGT,
982                                    lod_fpart, bld->perquadf_bld.zero,
983                                    "need_lerp");
984       }
985       else {
986          /*
987           * We'll do mip filtering if any of the quads need it.
988           * It might be better to split the vectors here and only fetch/filter
989           * quads which need it.
990           */
991          /*
992           * We unfortunately need to clamp lod_fpart here since we can get
993           * negative values which would screw up filtering if not all
994           * lod_fpart values have same sign.
995           */
996          lod_fpart = lp_build_max(&bld->perquadf_bld, lod_fpart,
997                                   bld->perquadf_bld.zero);
998          need_lerp = lp_build_compare(bld->gallivm, bld->perquadf_bld.type,
999                                       PIPE_FUNC_GREATER,
1000                                       lod_fpart, bld->perquadf_bld.zero);
1001          need_lerp = lp_build_any_true_range(&bld->perquadi_bld, num_quads, need_lerp);
1002      }
1003
1004       lp_build_if(&if_ctx, bld->gallivm, need_lerp);
1005       {
1006          /* sample the second mipmap level */
1007          lp_build_mipmap_level_sizes(bld, ilevel1,
1008                                      &size1,
1009                                      &row_stride1_vec, &img_stride1_vec);
1010          if (bld->num_lods == 1) {
1011             data_ptr1 = lp_build_get_mipmap_level(bld, ilevel1);
1012          }
1013          else {
1014             data_ptr1 = bld->base_ptr;
1015             mipoff1 = lp_build_get_mip_offsets(bld, ilevel1);
1016          }
1017          if (img_filter == PIPE_TEX_FILTER_NEAREST) {
1018             lp_build_sample_image_nearest(bld, sampler_unit,
1019                                           size1,
1020                                           row_stride1_vec, img_stride1_vec,
1021                                           data_ptr1, mipoff1, s, t, r, offsets,
1022                                           colors1);
1023          }
1024          else {
1025             lp_build_sample_image_linear(bld, sampler_unit,
1026                                          size1,
1027                                          row_stride1_vec, img_stride1_vec,
1028                                          data_ptr1, mipoff1, s, t, r, offsets,
1029                                          colors1);
1030          }
1031
1032          /* interpolate samples from the two mipmap levels */
1033
1034          lod_fpart = lp_build_unpack_broadcast_aos_scalars(bld->gallivm,
1035                                                            bld->perquadf_bld.type,
1036                                                            bld->texel_bld.type,
1037                                                            lod_fpart);
1038
1039          for (chan = 0; chan < 4; chan++) {
1040             colors0[chan] = lp_build_lerp(&bld->texel_bld, lod_fpart,
1041                                           colors0[chan], colors1[chan]);
1042             LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
1043          }
1044       }
1045       lp_build_endif(&if_ctx);
1046    }
1047 }
1048
1049
1050 /**
1051  * Clamp layer coord to valid values.
1052  */
1053 static LLVMValueRef
1054 lp_build_layer_coord(struct lp_build_sample_context *bld,
1055                      unsigned texture_unit,
1056                      LLVMValueRef layer)
1057 {
1058    LLVMValueRef maxlayer;
1059
1060    maxlayer = bld->dynamic_state->depth(bld->dynamic_state,
1061                                         bld->gallivm, texture_unit);
1062    maxlayer = lp_build_sub(&bld->int_bld, maxlayer, bld->int_bld.one);
1063    maxlayer = lp_build_broadcast_scalar(&bld->int_coord_bld, maxlayer);
1064    return lp_build_clamp(&bld->int_coord_bld, layer,
1065                          bld->int_coord_bld.zero, maxlayer);
1066
1067 }
1068
1069
1070 /**
1071  * Calculate cube face, lod, mip levels.
1072  */
1073 static void
1074 lp_build_sample_common(struct lp_build_sample_context *bld,
1075                        unsigned texture_index,
1076                        unsigned sampler_index,
1077                        LLVMValueRef *s,
1078                        LLVMValueRef *t,
1079                        LLVMValueRef *r,
1080                        const struct lp_derivatives *derivs, /* optional */
1081                        LLVMValueRef lod_bias, /* optional */
1082                        LLVMValueRef explicit_lod, /* optional */
1083                        LLVMValueRef *lod_ipart,
1084                        LLVMValueRef *lod_fpart,
1085                        LLVMValueRef *ilevel0,
1086                        LLVMValueRef *ilevel1)
1087 {
1088    const unsigned mip_filter = bld->static_sampler_state->min_mip_filter;
1089    const unsigned min_filter = bld->static_sampler_state->min_img_filter;
1090    const unsigned mag_filter = bld->static_sampler_state->mag_img_filter;
1091    const unsigned target = bld->static_texture_state->target;
1092    LLVMValueRef first_level, cube_rho = NULL;
1093
1094    /*
1095    printf("%s mip %d  min %d  mag %d\n", __FUNCTION__,
1096           mip_filter, min_filter, mag_filter);
1097    */
1098
1099    /*
1100     * Choose cube face, recompute texcoords for the chosen face and
1101     * compute rho here too (as it requires transform of derivatives).
1102     */
1103    if (target == PIPE_TEXTURE_CUBE) {
1104       LLVMValueRef face, face_s, face_t;
1105       boolean need_derivs;
1106       need_derivs = ((min_filter != mag_filter ||
1107                       mip_filter != PIPE_TEX_MIPFILTER_NONE) &&
1108                       !bld->static_sampler_state->min_max_lod_equal &&
1109                       !explicit_lod);
1110       lp_build_cube_lookup(bld, *s, *t, *r, derivs, &face, &face_s, &face_t,
1111                            &cube_rho, need_derivs);
1112       *s = face_s; /* vec */
1113       *t = face_t; /* vec */
1114       /* use 'r' to indicate cube face */
1115       *r = face; /* vec */
1116    }
1117    else if (target == PIPE_TEXTURE_1D_ARRAY) {
1118       *r = lp_build_iround(&bld->coord_bld, *t);
1119       *r = lp_build_layer_coord(bld, texture_index, *r);
1120    }
1121    else if (target == PIPE_TEXTURE_2D_ARRAY) {
1122       *r = lp_build_iround(&bld->coord_bld, *r);
1123       *r = lp_build_layer_coord(bld, texture_index, *r);
1124    }
1125
1126    /*
1127     * Compute the level of detail (float).
1128     */
1129    if (min_filter != mag_filter ||
1130        mip_filter != PIPE_TEX_MIPFILTER_NONE) {
1131       /* Need to compute lod either to choose mipmap levels or to
1132        * distinguish between minification/magnification with one mipmap level.
1133        */
1134       lp_build_lod_selector(bld, texture_index, sampler_index,
1135                             *s, *t, *r, cube_rho,
1136                             derivs, lod_bias, explicit_lod,
1137                             mip_filter,
1138                             lod_ipart, lod_fpart);
1139    } else {
1140       *lod_ipart = bld->perquadi_bld.zero;
1141    }
1142
1143    /*
1144     * Compute integer mipmap level(s) to fetch texels from: ilevel0, ilevel1
1145     */
1146    switch (mip_filter) {
1147    default:
1148       assert(0 && "bad mip_filter value in lp_build_sample_soa()");
1149       /* fall-through */
1150    case PIPE_TEX_MIPFILTER_NONE:
1151       /* always use mip level 0 */
1152       if (target == PIPE_TEXTURE_CUBE) {
1153          /* XXX this is a work-around for an apparent bug in LLVM 2.7.
1154           * We should be able to set ilevel0 = const(0) but that causes
1155           * bad x86 code to be emitted.
1156           * XXX should probably disable that on other llvm versions.
1157           */
1158          assert(*lod_ipart);
1159          lp_build_nearest_mip_level(bld, texture_index, *lod_ipart, ilevel0);
1160       }
1161       else {
1162          first_level = bld->dynamic_state->first_level(bld->dynamic_state,
1163                                                        bld->gallivm, texture_index);
1164          first_level = lp_build_broadcast_scalar(&bld->perquadi_bld, first_level);
1165          *ilevel0 = first_level;
1166       }
1167       break;
1168    case PIPE_TEX_MIPFILTER_NEAREST:
1169       assert(*lod_ipart);
1170       lp_build_nearest_mip_level(bld, texture_index, *lod_ipart, ilevel0);
1171       break;
1172    case PIPE_TEX_MIPFILTER_LINEAR:
1173       assert(*lod_ipart);
1174       assert(*lod_fpart);
1175       lp_build_linear_mip_levels(bld, texture_index,
1176                                  *lod_ipart, lod_fpart,
1177                                  ilevel0, ilevel1);
1178       break;
1179    }
1180 }
1181
1182 /**
1183  * General texture sampling codegen.
1184  * This function handles texture sampling for all texture targets (1D,
1185  * 2D, 3D, cube) and all filtering modes.
1186  */
1187 static void
1188 lp_build_sample_general(struct lp_build_sample_context *bld,
1189                         unsigned sampler_unit,
1190                         LLVMValueRef s,
1191                         LLVMValueRef t,
1192                         LLVMValueRef r,
1193                         const LLVMValueRef *offsets,
1194                         LLVMValueRef lod_ipart,
1195                         LLVMValueRef lod_fpart,
1196                         LLVMValueRef ilevel0,
1197                         LLVMValueRef ilevel1,
1198                         LLVMValueRef *colors_out)
1199 {
1200    struct lp_build_context *int_bld = &bld->int_bld;
1201    LLVMBuilderRef builder = bld->gallivm->builder;
1202    const unsigned mip_filter = bld->static_sampler_state->min_mip_filter;
1203    const unsigned min_filter = bld->static_sampler_state->min_img_filter;
1204    const unsigned mag_filter = bld->static_sampler_state->mag_img_filter;
1205    LLVMValueRef texels[4];
1206    unsigned chan;
1207
1208    /*
1209     * Get/interpolate texture colors.
1210     */
1211
1212    for (chan = 0; chan < 4; ++chan) {
1213      texels[chan] = lp_build_alloca(bld->gallivm, bld->texel_bld.vec_type, "");
1214      lp_build_name(texels[chan], "sampler%u_texel_%c_var", sampler_unit, "xyzw"[chan]);
1215    }
1216
1217    if (min_filter == mag_filter) {
1218       /* no need to distinguish between minification and magnification */
1219       lp_build_sample_mipmap(bld, sampler_unit,
1220                              min_filter, mip_filter,
1221                              s, t, r, offsets,
1222                              ilevel0, ilevel1, lod_fpart,
1223                              texels);
1224    }
1225    else {
1226       /* Emit conditional to choose min image filter or mag image filter
1227        * depending on the lod being > 0 or <= 0, respectively.
1228        */
1229       struct lp_build_if_state if_ctx;
1230       LLVMValueRef minify;
1231
1232       /*
1233        * XXX this should to all lods into account, if some are min
1234        * some max probably could hack up the coords/weights in the linear
1235        * path with selects to work for nearest.
1236        * If that's just two quads sitting next to each other it seems
1237        * quite ok to do the same filtering method on both though, at
1238        * least unless we have explicit lod (and who uses different
1239        * min/mag filter with that?)
1240        */
1241       if (bld->num_lods > 1)
1242          lod_ipart = LLVMBuildExtractElement(builder, lod_ipart,
1243                                              lp_build_const_int32(bld->gallivm, 0), "");
1244
1245       /* minify = lod >= 0.0 */
1246       minify = LLVMBuildICmp(builder, LLVMIntSGE,
1247                              lod_ipart, int_bld->zero, "");
1248
1249       lp_build_if(&if_ctx, bld->gallivm, minify);
1250       {
1251          /* Use the minification filter */
1252          lp_build_sample_mipmap(bld, sampler_unit,
1253                                 min_filter, mip_filter,
1254                                 s, t, r, offsets,
1255                                 ilevel0, ilevel1, lod_fpart,
1256                                 texels);
1257       }
1258       lp_build_else(&if_ctx);
1259       {
1260          /* Use the magnification filter */
1261          lp_build_sample_mipmap(bld, sampler_unit,
1262                                 mag_filter, PIPE_TEX_MIPFILTER_NONE,
1263                                 s, t, r, offsets,
1264                                 ilevel0, NULL, NULL,
1265                                 texels);
1266       }
1267       lp_build_endif(&if_ctx);
1268    }
1269
1270    for (chan = 0; chan < 4; ++chan) {
1271      colors_out[chan] = LLVMBuildLoad(builder, texels[chan], "");
1272      lp_build_name(colors_out[chan], "sampler%u_texel_%c", sampler_unit, "xyzw"[chan]);
1273    }
1274 }
1275
1276
1277 /**
1278  * Texel fetch function.
1279  * In contrast to general sampling there is no filtering, no coord minification,
1280  * lod (if any) is always explicit uint, coords are uints (in terms of texel units)
1281  * directly to be applied to the selected mip level (after adding texel offsets).
1282  * This function handles texel fetch for all targets where texel fetch is supported
1283  * (no cube maps, but 1d, 2d, 3d are supported, arrays and buffers should be too).
1284  */
1285 static void
1286 lp_build_fetch_texel(struct lp_build_sample_context *bld,
1287                      unsigned texture_unit,
1288                      const LLVMValueRef *coords,
1289                      LLVMValueRef explicit_lod,
1290                      const LLVMValueRef *offsets,
1291                      LLVMValueRef *colors_out)
1292 {
1293    struct lp_build_context *perquadi_bld = &bld->perquadi_bld;
1294    struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
1295    unsigned dims = bld->dims, chan;
1296    unsigned target = bld->static_texture_state->target;
1297    LLVMValueRef size, ilevel;
1298    LLVMValueRef row_stride_vec = NULL, img_stride_vec = NULL;
1299    LLVMValueRef x = coords[0], y = coords[1], z = coords[2];
1300    LLVMValueRef width, height, depth, i, j;
1301    LLVMValueRef offset, out_of_bounds, out1;
1302
1303    /* XXX just like ordinary sampling, we don't handle per-pixel lod (yet). */
1304    if (explicit_lod && bld->static_texture_state->target != PIPE_BUFFER) {
1305       ilevel = lp_build_pack_aos_scalars(bld->gallivm, int_coord_bld->type,
1306                                          perquadi_bld->type, explicit_lod, 0);
1307       lp_build_nearest_mip_level(bld, texture_unit, ilevel, &ilevel);
1308    }
1309    else {
1310       bld->num_lods = 1;
1311       ilevel = lp_build_const_int32(bld->gallivm, 0);
1312    }
1313    lp_build_mipmap_level_sizes(bld, ilevel,
1314                                &size,
1315                                &row_stride_vec, &img_stride_vec);
1316    lp_build_extract_image_sizes(bld, &bld->int_size_bld, int_coord_bld->type,
1317                                 size, &width, &height, &depth);
1318
1319    if (target == PIPE_TEXTURE_1D_ARRAY ||
1320        target == PIPE_TEXTURE_2D_ARRAY) {
1321       if (target == PIPE_TEXTURE_1D_ARRAY) {
1322          z = lp_build_layer_coord(bld, texture_unit, y);
1323       }
1324       else {
1325          z = lp_build_layer_coord(bld, texture_unit, z);
1326       }
1327    }
1328
1329    /* This is a lot like border sampling */
1330    if (offsets[0]) {
1331       /* XXX coords are really unsigned, offsets are signed */
1332       x = lp_build_add(int_coord_bld, x, offsets[0]);
1333    }
1334    out_of_bounds = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, x, int_coord_bld->zero);
1335    out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, x, width);
1336    out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
1337
1338    if (dims >= 2) {
1339       if (offsets[1]) {
1340          y = lp_build_add(int_coord_bld, y, offsets[1]);
1341       }
1342       out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, y, int_coord_bld->zero);
1343       out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
1344       out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, y, height);
1345       out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
1346
1347       if (dims >= 3) {
1348          if (offsets[2]) {
1349             z = lp_build_add(int_coord_bld, z, offsets[2]);
1350          }
1351          out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, z, int_coord_bld->zero);
1352          out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
1353          out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, z, depth);
1354          out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
1355       }
1356    }
1357
1358    lp_build_sample_offset(int_coord_bld,
1359                           bld->format_desc,
1360                           x, y, z, row_stride_vec, img_stride_vec,
1361                           &offset, &i, &j);
1362
1363    if (bld->static_texture_state->target != PIPE_BUFFER) {
1364       offset = lp_build_add(int_coord_bld, offset,
1365                             lp_build_get_mip_offsets(bld, ilevel));
1366    }
1367
1368    offset = lp_build_andnot(int_coord_bld, offset, out_of_bounds);
1369
1370    lp_build_fetch_rgba_soa(bld->gallivm,
1371                            bld->format_desc,
1372                            bld->texel_type,
1373                            bld->base_ptr, offset,
1374                            i, j,
1375                            colors_out);
1376
1377    if (0) {
1378       /*
1379        * Not needed except for ARB_robust_buffer_access_behavior.
1380        * Could use min/max above instead of out-of-bounds comparisons
1381        * (in fact cast to unsigned and min only is sufficient)
1382        * if we don't care about the result returned for out-of-bounds.
1383        */
1384       for (chan = 0; chan < 4; chan++) {
1385          colors_out[chan] = lp_build_select(&bld->texel_bld, out_of_bounds,
1386                                             bld->texel_bld.zero, colors_out[chan]);
1387       }
1388    }
1389 }
1390
1391
1392 /**
1393  * Do shadow test/comparison.
1394  * \param coords  incoming texcoords
1395  * \param texel  the texel to compare against (use the X channel)
1396  * Ideally this should really be done per-sample.
1397  */
1398 static void
1399 lp_build_sample_compare(struct lp_build_sample_context *bld,
1400                         const LLVMValueRef *coords,
1401                         LLVMValueRef texel[4])
1402 {
1403    struct lp_build_context *texel_bld = &bld->texel_bld;
1404    LLVMBuilderRef builder = bld->gallivm->builder;
1405    LLVMValueRef res, p;
1406    const unsigned chan = 0;
1407
1408    if (bld->static_sampler_state->compare_mode == PIPE_TEX_COMPARE_NONE)
1409       return;
1410
1411    if (bld->static_texture_state->target == PIPE_TEXTURE_2D_ARRAY ||
1412        bld->static_texture_state->target == PIPE_TEXTURE_CUBE) {
1413       p = coords[3];
1414    }
1415    else {
1416       p = coords[2];
1417    }
1418
1419    /* debug code */
1420    if (0) {
1421       LLVMValueRef indx = lp_build_const_int32(bld->gallivm, 0);
1422       LLVMValueRef coord = LLVMBuildExtractElement(builder, p, indx, "");
1423       LLVMValueRef tex = LLVMBuildExtractElement(builder, texel[chan], indx, "");
1424       lp_build_printf(bld->gallivm, "shadow compare coord %f to texture %f\n",
1425                       coord, tex);
1426    }
1427
1428    /* Clamp p coords to [0,1] */
1429    p = lp_build_clamp(&bld->coord_bld, p,
1430                       bld->coord_bld.zero,
1431                       bld->coord_bld.one);
1432
1433    /* result = (p FUNC texel) ? 1 : 0 */
1434    res = lp_build_cmp(texel_bld, bld->static_sampler_state->compare_func,
1435                       p, texel[chan]);
1436    res = lp_build_select(texel_bld, res, texel_bld->one, texel_bld->zero);
1437
1438    /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1439    texel[0] =
1440    texel[1] =
1441    texel[2] = res;
1442    texel[3] = texel_bld->one;
1443 }
1444
1445
1446 /**
1447  * Just set texels to white instead of actually sampling the texture.
1448  * For debugging.
1449  */
1450 void
1451 lp_build_sample_nop(struct gallivm_state *gallivm,
1452                     struct lp_type type,
1453                     const LLVMValueRef *coords,
1454                     LLVMValueRef texel_out[4])
1455 {
1456    LLVMValueRef one = lp_build_one(gallivm, type);
1457    unsigned chan;
1458
1459    for (chan = 0; chan < 4; chan++) {
1460       texel_out[chan] = one;
1461    }  
1462 }
1463
1464
1465 /**
1466  * Build texture sampling code.
1467  * 'texel' will return a vector of four LLVMValueRefs corresponding to
1468  * R, G, B, A.
1469  * \param type  vector float type to use for coords, etc.
1470  * \param is_fetch  if this is a texel fetch instruction.
1471  * \param derivs  partial derivatives of (s,t,r,q) with respect to x and y
1472  */
1473 void
1474 lp_build_sample_soa(struct gallivm_state *gallivm,
1475                     const struct lp_static_texture_state *static_texture_state,
1476                     const struct lp_static_sampler_state *static_sampler_state,
1477                     struct lp_sampler_dynamic_state *dynamic_state,
1478                     struct lp_type type,
1479                     boolean is_fetch,
1480                     unsigned texture_index,
1481                     unsigned sampler_index,
1482                     const LLVMValueRef *coords,
1483                     const LLVMValueRef *offsets,
1484                     const struct lp_derivatives *derivs, /* optional */
1485                     LLVMValueRef lod_bias, /* optional */
1486                     LLVMValueRef explicit_lod, /* optional */
1487                     LLVMValueRef texel_out[4])
1488 {
1489    unsigned dims = texture_dims(static_texture_state->target);
1490    unsigned num_quads = type.length / 4;
1491    unsigned mip_filter;
1492    struct lp_build_sample_context bld;
1493    struct lp_static_sampler_state derived_sampler_state = *static_sampler_state;
1494    LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context);
1495    LLVMBuilderRef builder = gallivm->builder;
1496    LLVMValueRef tex_width;
1497    LLVMValueRef s;
1498    LLVMValueRef t;
1499    LLVMValueRef r;
1500
1501    if (0) {
1502       enum pipe_format fmt = static_texture_state->format;
1503       debug_printf("Sample from %s\n", util_format_name(fmt));
1504    }
1505
1506    assert(type.floating);
1507
1508    /* Setup our build context */
1509    memset(&bld, 0, sizeof bld);
1510    bld.gallivm = gallivm;
1511    bld.static_sampler_state = &derived_sampler_state;
1512    bld.static_texture_state = static_texture_state;
1513    bld.dynamic_state = dynamic_state;
1514    bld.format_desc = util_format_description(static_texture_state->format);
1515    bld.dims = dims;
1516
1517    bld.vector_width = lp_type_width(type);
1518
1519    bld.float_type = lp_type_float(32);
1520    bld.int_type = lp_type_int(32);
1521    bld.coord_type = type;
1522    bld.int_coord_type = lp_int_type(type);
1523    bld.float_size_in_type = lp_type_float(32);
1524    bld.float_size_in_type.length = dims > 1 ? 4 : 1;
1525    bld.int_size_in_type = lp_int_type(bld.float_size_in_type);
1526    bld.texel_type = type;
1527    bld.perquadf_type = type;
1528    /* we want native vector size to be able to use our intrinsics */
1529    bld.perquadf_type.length = type.length > 4 ? ((type.length + 15) / 16) * 4 : 1;
1530    bld.perquadi_type = lp_int_type(bld.perquadf_type);
1531
1532    /* always using the first channel hopefully should be safe,
1533     * if not things WILL break in other places anyway.
1534     */
1535    if (bld.format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB &&
1536        bld.format_desc->channel[0].pure_integer) {
1537       if (bld.format_desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
1538          bld.texel_type = lp_type_int_vec(type.width, type.width * type.length);
1539       }
1540       else if (bld.format_desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED) {
1541          bld.texel_type = lp_type_uint_vec(type.width, type.width * type.length);
1542       }
1543    }
1544
1545    if (!static_texture_state->level_zero_only) {
1546       derived_sampler_state.min_mip_filter = static_sampler_state->min_mip_filter;
1547    } else {
1548       derived_sampler_state.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
1549    }
1550    mip_filter = derived_sampler_state.min_mip_filter;
1551
1552    if (0) {
1553       debug_printf("  .min_mip_filter = %u\n", derived_sampler_state.min_mip_filter);
1554    }
1555
1556    /*
1557     * There are other situations where at least the multiple int lods could be
1558     * avoided like min and max lod being equal.
1559     */
1560    if ((is_fetch && explicit_lod && bld.static_texture_state->target != PIPE_BUFFER) ||
1561        (!is_fetch && mip_filter != PIPE_TEX_MIPFILTER_NONE)) {
1562       bld.num_lods = num_quads;
1563    }
1564    else {
1565       bld.num_lods = 1;
1566    }
1567
1568    bld.float_size_type = bld.float_size_in_type;
1569    bld.float_size_type.length = bld.num_lods > 1 ? type.length :
1570                                    bld.float_size_in_type.length;
1571    bld.int_size_type = lp_int_type(bld.float_size_type);
1572
1573    lp_build_context_init(&bld.float_bld, gallivm, bld.float_type);
1574    lp_build_context_init(&bld.float_vec_bld, gallivm, type);
1575    lp_build_context_init(&bld.int_bld, gallivm, bld.int_type);
1576    lp_build_context_init(&bld.coord_bld, gallivm, bld.coord_type);
1577    lp_build_context_init(&bld.int_coord_bld, gallivm, bld.int_coord_type);
1578    lp_build_context_init(&bld.int_size_in_bld, gallivm, bld.int_size_in_type);
1579    lp_build_context_init(&bld.float_size_in_bld, gallivm, bld.float_size_in_type);
1580    lp_build_context_init(&bld.int_size_bld, gallivm, bld.int_size_type);
1581    lp_build_context_init(&bld.float_size_bld, gallivm, bld.float_size_type);
1582    lp_build_context_init(&bld.texel_bld, gallivm, bld.texel_type);
1583    lp_build_context_init(&bld.perquadf_bld, gallivm, bld.perquadf_type);
1584    lp_build_context_init(&bld.perquadi_bld, gallivm, bld.perquadi_type);
1585
1586    /* Get the dynamic state */
1587    tex_width = dynamic_state->width(dynamic_state, gallivm, texture_index);
1588    bld.row_stride_array = dynamic_state->row_stride(dynamic_state, gallivm, texture_index);
1589    bld.img_stride_array = dynamic_state->img_stride(dynamic_state, gallivm, texture_index);
1590    bld.base_ptr = dynamic_state->base_ptr(dynamic_state, gallivm, texture_index);
1591    bld.mip_offsets = dynamic_state->mip_offsets(dynamic_state, gallivm, texture_index);
1592    /* Note that mip_offsets is an array[level] of offsets to texture images */
1593
1594    s = coords[0];
1595    t = coords[1];
1596    r = coords[2];
1597
1598    /* width, height, depth as single int vector */
1599    if (dims <= 1) {
1600       bld.int_size = tex_width;
1601    }
1602    else {
1603       bld.int_size = LLVMBuildInsertElement(builder, bld.int_size_in_bld.undef,
1604                                             tex_width, LLVMConstInt(i32t, 0, 0), "");
1605       if (dims >= 2) {
1606          LLVMValueRef tex_height =
1607             dynamic_state->height(dynamic_state, gallivm, texture_index);
1608          bld.int_size = LLVMBuildInsertElement(builder, bld.int_size,
1609                                                tex_height, LLVMConstInt(i32t, 1, 0), "");
1610          if (dims >= 3) {
1611             LLVMValueRef tex_depth =
1612                dynamic_state->depth(dynamic_state, gallivm, texture_index);
1613             bld.int_size = LLVMBuildInsertElement(builder, bld.int_size,
1614                                                   tex_depth, LLVMConstInt(i32t, 2, 0), "");
1615          }
1616       }
1617    }
1618
1619    if (0) {
1620       /* For debug: no-op texture sampling */
1621       lp_build_sample_nop(gallivm,
1622                           bld.texel_type,
1623                           coords,
1624                           texel_out);
1625    }
1626
1627    else if (is_fetch) {
1628       lp_build_fetch_texel(&bld, texture_index, coords,
1629                            explicit_lod, offsets,
1630                            texel_out);
1631    }
1632
1633    else {
1634       LLVMValueRef lod_ipart = NULL, lod_fpart = NULL;
1635       LLVMValueRef ilevel0 = NULL, ilevel1 = NULL;
1636       boolean use_aos = util_format_fits_8unorm(bld.format_desc) &&
1637                         lp_is_simple_wrap_mode(static_sampler_state->wrap_s) &&
1638                         lp_is_simple_wrap_mode(static_sampler_state->wrap_t);
1639
1640       if ((gallivm_debug & GALLIVM_DEBUG_PERF) &&
1641           !use_aos && util_format_fits_8unorm(bld.format_desc)) {
1642          debug_printf("%s: using floating point linear filtering for %s\n",
1643                       __FUNCTION__, bld.format_desc->short_name);
1644          debug_printf("  min_img %d  mag_img %d  mip %d  wraps %d  wrapt %d\n",
1645                       static_sampler_state->min_img_filter,
1646                       static_sampler_state->mag_img_filter,
1647                       static_sampler_state->min_mip_filter,
1648                       static_sampler_state->wrap_s,
1649                       static_sampler_state->wrap_t);
1650       }
1651
1652       lp_build_sample_common(&bld, texture_index, sampler_index,
1653                              &s, &t, &r,
1654                              derivs, lod_bias, explicit_lod,
1655                              &lod_ipart, &lod_fpart,
1656                              &ilevel0, &ilevel1);
1657
1658       /*
1659        * we only try 8-wide sampling with soa as it appears to
1660        * be a loss with aos with AVX (but it should work).
1661        * (It should be faster if we'd support avx2)
1662        */
1663       if (num_quads == 1 || !use_aos) {
1664
1665          if (num_quads > 1) {
1666             if (mip_filter == PIPE_TEX_MIPFILTER_NONE) {
1667                LLVMValueRef index0 = lp_build_const_int32(gallivm, 0);
1668                /*
1669                 * These parameters are the same for all quads,
1670                 * could probably simplify.
1671                 */
1672                lod_ipart = LLVMBuildExtractElement(builder, lod_ipart, index0, "");
1673                ilevel0 = LLVMBuildExtractElement(builder, ilevel0, index0, "");
1674             }
1675          }
1676          if (use_aos) {
1677             /* do sampling/filtering with fixed pt arithmetic */
1678             lp_build_sample_aos(&bld, sampler_index,
1679                                 s, t, r, offsets,
1680                                 lod_ipart, lod_fpart,
1681                                 ilevel0, ilevel1,
1682                                 texel_out);
1683          }
1684
1685          else {
1686             lp_build_sample_general(&bld, sampler_index,
1687                                     s, t, r, offsets,
1688                                     lod_ipart, lod_fpart,
1689                                     ilevel0, ilevel1,
1690                                     texel_out);
1691          }
1692       }
1693       else {
1694          unsigned j;
1695          struct lp_build_sample_context bld4;
1696          struct lp_type type4 = type;
1697          unsigned i;
1698          LLVMValueRef texelout4[4];
1699          LLVMValueRef texelouttmp[4][LP_MAX_VECTOR_LENGTH/16];
1700
1701          type4.length = 4;
1702
1703          /* Setup our build context */
1704          memset(&bld4, 0, sizeof bld4);
1705          bld4.gallivm = bld.gallivm;
1706          bld4.static_texture_state = bld.static_texture_state;
1707          bld4.static_sampler_state = bld.static_sampler_state;
1708          bld4.dynamic_state = bld.dynamic_state;
1709          bld4.format_desc = bld.format_desc;
1710          bld4.dims = bld.dims;
1711          bld4.row_stride_array = bld.row_stride_array;
1712          bld4.img_stride_array = bld.img_stride_array;
1713          bld4.base_ptr = bld.base_ptr;
1714          bld4.mip_offsets = bld.mip_offsets;
1715          bld4.int_size = bld.int_size;
1716
1717          bld4.vector_width = lp_type_width(type4);
1718
1719          bld4.float_type = lp_type_float(32);
1720          bld4.int_type = lp_type_int(32);
1721          bld4.coord_type = type4;
1722          bld4.int_coord_type = lp_int_type(type4);
1723          bld4.float_size_in_type = lp_type_float(32);
1724          bld4.float_size_in_type.length = dims > 1 ? 4 : 1;
1725          bld4.int_size_in_type = lp_int_type(bld4.float_size_in_type);
1726          bld4.texel_type = bld.texel_type;
1727          bld4.texel_type.length = 4;
1728          bld4.perquadf_type = type4;
1729          /* we want native vector size to be able to use our intrinsics */
1730          bld4.perquadf_type.length = 1;
1731          bld4.perquadi_type = lp_int_type(bld4.perquadf_type);
1732
1733          bld4.num_lods = 1;
1734          bld4.int_size_type = bld4.int_size_in_type;
1735          bld4.float_size_type = bld4.float_size_in_type;
1736
1737          lp_build_context_init(&bld4.float_bld, gallivm, bld4.float_type);
1738          lp_build_context_init(&bld4.float_vec_bld, gallivm, type4);
1739          lp_build_context_init(&bld4.int_bld, gallivm, bld4.int_type);
1740          lp_build_context_init(&bld4.coord_bld, gallivm, bld4.coord_type);
1741          lp_build_context_init(&bld4.int_coord_bld, gallivm, bld4.int_coord_type);
1742          lp_build_context_init(&bld4.int_size_in_bld, gallivm, bld4.int_size_in_type);
1743          lp_build_context_init(&bld4.float_size_in_bld, gallivm, bld4.float_size_in_type);
1744          lp_build_context_init(&bld4.int_size_bld, gallivm, bld4.int_size_type);
1745          lp_build_context_init(&bld4.float_size_bld, gallivm, bld4.float_size_type);
1746          lp_build_context_init(&bld4.texel_bld, gallivm, bld4.texel_type);
1747          lp_build_context_init(&bld4.perquadf_bld, gallivm, bld4.perquadf_type);
1748          lp_build_context_init(&bld4.perquadi_bld, gallivm, bld4.perquadi_type);
1749
1750          for (i = 0; i < num_quads; i++) {
1751             LLVMValueRef s4, t4, r4;
1752             LLVMValueRef lod_iparts, lod_fparts = NULL;
1753             LLVMValueRef ilevel0s, ilevel1s = NULL;
1754             LLVMValueRef indexi = lp_build_const_int32(gallivm, i);
1755             LLVMValueRef offsets4[4] = { NULL };
1756
1757             s4 = lp_build_extract_range(gallivm, s, 4*i, 4);
1758             t4 = lp_build_extract_range(gallivm, t, 4*i, 4);
1759             r4 = lp_build_extract_range(gallivm, r, 4*i, 4);
1760
1761             if (offsets[0]) {
1762                offsets4[0] = lp_build_extract_range(gallivm, offsets[0], 4*i, 4);
1763                if (dims > 1) {
1764                   offsets4[1] = lp_build_extract_range(gallivm, offsets[1], 4*i, 4);
1765                   if (dims > 2) {
1766                      offsets4[2] = lp_build_extract_range(gallivm, offsets[2], 4*i, 4);
1767                   }
1768                }
1769             }
1770             lod_iparts = LLVMBuildExtractElement(builder, lod_ipart, indexi, "");
1771             ilevel0s = LLVMBuildExtractElement(builder, ilevel0, indexi, "");
1772             if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1773                ilevel1s = LLVMBuildExtractElement(builder, ilevel1, indexi, "");
1774                lod_fparts = LLVMBuildExtractElement(builder, lod_fpart, indexi, "");
1775             }
1776
1777             if (use_aos) {
1778                /* do sampling/filtering with fixed pt arithmetic */
1779                lp_build_sample_aos(&bld4, sampler_index,
1780                                    s4, t4, r4, offsets4,
1781                                    lod_iparts, lod_fparts,
1782                                    ilevel0s, ilevel1s,
1783                                    texelout4);
1784             }
1785
1786             else {
1787                lp_build_sample_general(&bld4, sampler_index,
1788                                        s4, t4, r4, offsets4,
1789                                        lod_iparts, lod_fparts,
1790                                        ilevel0s, ilevel1s,
1791                                        texelout4);
1792             }
1793             for (j = 0; j < 4; j++) {
1794                texelouttmp[j][i] = texelout4[j];
1795             }
1796          }
1797
1798          for (j = 0; j < 4; j++) {
1799             texel_out[j] = lp_build_concat(gallivm, texelouttmp[j], type4, num_quads);
1800          }
1801       }
1802
1803       lp_build_sample_compare(&bld, coords, texel_out);
1804    }
1805
1806    if (static_texture_state->target != PIPE_BUFFER) {
1807       apply_sampler_swizzle(&bld, texel_out);
1808    }
1809
1810    /*
1811     * texel type can be a (32bit) int/uint (for pure int formats only),
1812     * however we are expected to always return floats (storage is untyped).
1813     */
1814    if (!bld.texel_type.floating) {
1815       unsigned chan;
1816       for (chan = 0; chan < 4; chan++) {
1817          texel_out[chan] = LLVMBuildBitCast(builder, texel_out[chan],
1818                                             lp_build_vec_type(gallivm, type), "");
1819       }
1820    }
1821 }
1822
1823 void
1824 lp_build_size_query_soa(struct gallivm_state *gallivm,
1825                         const struct lp_static_texture_state *static_state,
1826                         struct lp_sampler_dynamic_state *dynamic_state,
1827                         struct lp_type int_type,
1828                         unsigned texture_unit,
1829                         boolean need_nr_mips,
1830                         LLVMValueRef explicit_lod,
1831                         LLVMValueRef *sizes_out)
1832 {
1833    LLVMValueRef lod;
1834    LLVMValueRef size;
1835    LLVMValueRef first_level = NULL;
1836    int dims, i;
1837    boolean has_array;
1838    struct lp_build_context bld_int_vec;
1839
1840    dims = texture_dims(static_state->target);
1841
1842    switch (static_state->target) {
1843    case PIPE_TEXTURE_1D_ARRAY:
1844    case PIPE_TEXTURE_2D_ARRAY:
1845       has_array = TRUE;
1846       break;
1847    default:
1848       has_array = FALSE;
1849       break;
1850    }
1851
1852    assert(!int_type.floating);
1853
1854    lp_build_context_init(&bld_int_vec, gallivm, lp_type_int_vec(32, 128));
1855
1856    if (explicit_lod) {
1857       lod = LLVMBuildExtractElement(gallivm->builder, explicit_lod, lp_build_const_int32(gallivm, 0), "");
1858       first_level = dynamic_state->first_level(dynamic_state, gallivm, texture_unit);
1859       lod = lp_build_broadcast_scalar(&bld_int_vec,
1860                                       LLVMBuildAdd(gallivm->builder, lod, first_level, "lod"));
1861
1862    } else {
1863       lod = bld_int_vec.zero;
1864    }
1865
1866    if (need_nr_mips) {
1867       size = bld_int_vec.zero;
1868    }
1869    else {
1870       size = bld_int_vec.undef;
1871    }
1872
1873    size = LLVMBuildInsertElement(gallivm->builder, size,
1874                                  dynamic_state->width(dynamic_state, gallivm, texture_unit),
1875                                  lp_build_const_int32(gallivm, 0), "");
1876
1877    if (dims >= 2) {
1878       size = LLVMBuildInsertElement(gallivm->builder, size,
1879                                     dynamic_state->height(dynamic_state, gallivm, texture_unit),
1880                                     lp_build_const_int32(gallivm, 1), "");
1881    }
1882
1883    if (dims >= 3) {
1884       size = LLVMBuildInsertElement(gallivm->builder, size,
1885                                     dynamic_state->depth(dynamic_state, gallivm, texture_unit),
1886                                     lp_build_const_int32(gallivm, 2), "");
1887    }
1888
1889    size = lp_build_minify(&bld_int_vec, size, lod);
1890
1891    if (has_array)
1892       size = LLVMBuildInsertElement(gallivm->builder, size,
1893                                     dynamic_state->depth(dynamic_state, gallivm, texture_unit),
1894                                     lp_build_const_int32(gallivm, dims), "");
1895
1896    /*
1897     * XXX for out-of-bounds lod, should set size to zero vector here
1898     * (for dx10-style only, i.e. need_nr_mips)
1899     */
1900
1901    for (i = 0; i < dims + (has_array ? 1 : 0); i++) {
1902       sizes_out[i] = lp_build_extract_broadcast(gallivm, bld_int_vec.type, int_type,
1903                                                 size,
1904                                                 lp_build_const_int32(gallivm, i));
1905    }
1906
1907    /*
1908     * if there's no explicit_lod (buffers, rects) queries requiring nr of
1909     * mips would be illegal.
1910     */
1911    if (need_nr_mips && explicit_lod) {
1912       struct lp_build_context bld_int_scalar;
1913       LLVMValueRef num_levels;
1914       lp_build_context_init(&bld_int_scalar, gallivm, lp_type_int(32));
1915
1916       if (static_state->level_zero_only) {
1917          num_levels = bld_int_scalar.one;
1918       }
1919       else {
1920          LLVMValueRef last_level;
1921
1922          last_level = dynamic_state->last_level(dynamic_state, gallivm, texture_unit);
1923          num_levels = lp_build_sub(&bld_int_scalar, last_level, first_level);
1924          num_levels = lp_build_add(&bld_int_scalar, num_levels, bld_int_scalar.one);
1925       }
1926       sizes_out[3] = lp_build_broadcast(gallivm, lp_build_vec_type(gallivm, int_type),
1927                                         num_levels);
1928    }
1929 }