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