gallivm: honor d3d10 floating point rules for shadow comparisons
[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 struct lp_build_context abs_coord_bld = bld->coord_bld;
440 abs_coord_bld.type.sign = FALSE;
441
442 if (bld->static_sampler_state->normalized_coords) {
443 /* scale coord to length */
444 coord = lp_build_mul(coord_bld, coord, length_f);
445 }
446 if (offset) {
447 offset = lp_build_int_to_float(coord_bld, offset);
448 coord = lp_build_add(coord_bld, coord, offset);
449 }
450 coord = lp_build_abs(coord_bld, coord);
451
452 /* clamp to length max */
453 coord = lp_build_min(coord_bld, coord, length_f);
454 /* subtract 0.5 */
455 coord = lp_build_sub(coord_bld, coord, half);
456 /* clamp to [0, length - 0.5] */
457 coord = lp_build_max(coord_bld, coord, coord_bld->zero);
458
459 /* convert to int, compute lerp weight */
460 lp_build_ifloor_fract(&abs_coord_bld, coord, &coord0, &weight);
461 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
462 /* coord1 = min(coord1, length-1) */
463 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
464 }
465 break;
466
467 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
468 {
469 if (bld->static_sampler_state->normalized_coords) {
470 /* scale coord to length */
471 coord = lp_build_mul(coord_bld, coord, length_f);
472 }
473 if (offset) {
474 offset = lp_build_int_to_float(coord_bld, offset);
475 coord = lp_build_add(coord_bld, coord, offset);
476 }
477 coord = lp_build_abs(coord_bld, coord);
478
479 /* was: clamp to [-0.5, length + 0.5] then sub 0.5 */
480 /* skip clamp - always positive, and other side
481 only potentially matters for very large coords */
482 coord = lp_build_sub(coord_bld, coord, half);
483
484 /* convert to int, compute lerp weight */
485 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
486 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
487 }
488 break;
489
490 default:
491 assert(0);
492 coord0 = NULL;
493 coord1 = NULL;
494 weight = NULL;
495 }
496
497 *x0_out = coord0;
498 *x1_out = coord1;
499 *weight_out = weight;
500 }
501
502
503 /**
504 * Build LLVM code for texture wrap mode for nearest filtering.
505 * \param coord the incoming texcoord (nominally in [0,1])
506 * \param length the texture size along one dimension, as int vector
507 * \param length_f the texture size along one dimension, as float vector
508 * \param offset texel offset along one dimension (as int vector)
509 * \param is_pot if TRUE, length is a power of two
510 * \param wrap_mode one of PIPE_TEX_WRAP_x
511 */
512 static LLVMValueRef
513 lp_build_sample_wrap_nearest(struct lp_build_sample_context *bld,
514 LLVMValueRef coord,
515 LLVMValueRef length,
516 LLVMValueRef length_f,
517 LLVMValueRef offset,
518 boolean is_pot,
519 unsigned wrap_mode)
520 {
521 struct lp_build_context *coord_bld = &bld->coord_bld;
522 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
523 LLVMBuilderRef builder = bld->gallivm->builder;
524 LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length, int_coord_bld->one);
525 LLVMValueRef icoord;
526
527 switch(wrap_mode) {
528 case PIPE_TEX_WRAP_REPEAT:
529 if (is_pot) {
530 coord = lp_build_mul(coord_bld, coord, length_f);
531 icoord = lp_build_ifloor(coord_bld, coord);
532 if (offset) {
533 icoord = lp_build_add(int_coord_bld, icoord, offset);
534 }
535 icoord = LLVMBuildAnd(builder, icoord, length_minus_one, "");
536 }
537 else {
538 if (offset) {
539 offset = lp_build_int_to_float(coord_bld, offset);
540 offset = lp_build_div(coord_bld, offset, length_f);
541 coord = lp_build_add(coord_bld, coord, offset);
542 }
543 /* take fraction, unnormalize */
544 coord = lp_build_fract_safe(coord_bld, coord);
545 coord = lp_build_mul(coord_bld, coord, length_f);
546 icoord = lp_build_itrunc(coord_bld, coord);
547 }
548 break;
549
550 case PIPE_TEX_WRAP_CLAMP:
551 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
552 if (bld->static_sampler_state->normalized_coords) {
553 /* scale coord to length */
554 coord = lp_build_mul(coord_bld, coord, length_f);
555 }
556
557 /* floor */
558 /* use itrunc instead since we clamp to 0 anyway */
559 icoord = lp_build_itrunc(coord_bld, coord);
560 if (offset) {
561 icoord = lp_build_add(int_coord_bld, icoord, offset);
562 }
563
564 /* clamp to [0, length - 1]. */
565 icoord = lp_build_clamp(int_coord_bld, icoord, int_coord_bld->zero,
566 length_minus_one);
567 break;
568
569 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
570 if (bld->static_sampler_state->normalized_coords) {
571 /* scale coord to length */
572 coord = lp_build_mul(coord_bld, coord, length_f);
573 }
574 /* no clamp necessary, border masking will handle this */
575 icoord = lp_build_ifloor(coord_bld, coord);
576 if (offset) {
577 icoord = lp_build_add(int_coord_bld, icoord, offset);
578 }
579 break;
580
581 case PIPE_TEX_WRAP_MIRROR_REPEAT:
582 if (offset) {
583 offset = lp_build_int_to_float(coord_bld, offset);
584 offset = lp_build_div(coord_bld, offset, length_f);
585 coord = lp_build_add(coord_bld, coord, offset);
586 }
587 /* compute mirror function */
588 coord = lp_build_coord_mirror(bld, coord);
589
590 /* scale coord to length */
591 assert(bld->static_sampler_state->normalized_coords);
592 coord = lp_build_mul(coord_bld, coord, length_f);
593
594 /* itrunc == ifloor here */
595 icoord = lp_build_itrunc(coord_bld, coord);
596
597 /* clamp to [0, length - 1] */
598 icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
599 break;
600
601 case PIPE_TEX_WRAP_MIRROR_CLAMP:
602 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
603 if (bld->static_sampler_state->normalized_coords) {
604 /* scale coord to length */
605 coord = lp_build_mul(coord_bld, coord, length_f);
606 }
607 if (offset) {
608 offset = lp_build_int_to_float(coord_bld, offset);
609 coord = lp_build_add(coord_bld, coord, offset);
610 }
611 coord = lp_build_abs(coord_bld, coord);
612
613 /* itrunc == ifloor here */
614 icoord = lp_build_itrunc(coord_bld, coord);
615
616 /* clamp to [0, length - 1] */
617 icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
618 break;
619
620 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
621 if (bld->static_sampler_state->normalized_coords) {
622 /* scale coord to length */
623 coord = lp_build_mul(coord_bld, coord, length_f);
624 }
625 if (offset) {
626 offset = lp_build_int_to_float(coord_bld, offset);
627 coord = lp_build_add(coord_bld, coord, offset);
628 }
629 coord = lp_build_abs(coord_bld, coord);
630
631 /* itrunc == ifloor here */
632 icoord = lp_build_itrunc(coord_bld, coord);
633 break;
634
635 default:
636 assert(0);
637 icoord = NULL;
638 }
639
640 return icoord;
641 }
642
643
644 /**
645 * Generate code to sample a mipmap level with nearest filtering.
646 * If sampling a cube texture, r = cube face in [0,5].
647 */
648 static void
649 lp_build_sample_image_nearest(struct lp_build_sample_context *bld,
650 unsigned sampler_unit,
651 LLVMValueRef size,
652 LLVMValueRef row_stride_vec,
653 LLVMValueRef img_stride_vec,
654 LLVMValueRef data_ptr,
655 LLVMValueRef mipoffsets,
656 LLVMValueRef s,
657 LLVMValueRef t,
658 LLVMValueRef r,
659 const LLVMValueRef *offsets,
660 LLVMValueRef colors_out[4])
661 {
662 const unsigned dims = bld->dims;
663 LLVMValueRef width_vec;
664 LLVMValueRef height_vec;
665 LLVMValueRef depth_vec;
666 LLVMValueRef flt_size;
667 LLVMValueRef flt_width_vec;
668 LLVMValueRef flt_height_vec;
669 LLVMValueRef flt_depth_vec;
670 LLVMValueRef x, y = NULL, z = NULL;
671
672 lp_build_extract_image_sizes(bld,
673 &bld->int_size_bld,
674 bld->int_coord_type,
675 size,
676 &width_vec, &height_vec, &depth_vec);
677
678 flt_size = lp_build_int_to_float(&bld->float_size_bld, size);
679
680 lp_build_extract_image_sizes(bld,
681 &bld->float_size_bld,
682 bld->coord_type,
683 flt_size,
684 &flt_width_vec, &flt_height_vec, &flt_depth_vec);
685
686 /*
687 * Compute integer texcoords.
688 */
689 x = lp_build_sample_wrap_nearest(bld, s, width_vec, flt_width_vec, offsets[0],
690 bld->static_texture_state->pot_width,
691 bld->static_sampler_state->wrap_s);
692 lp_build_name(x, "tex.x.wrapped");
693
694 if (dims >= 2) {
695 y = lp_build_sample_wrap_nearest(bld, t, height_vec, flt_height_vec, offsets[1],
696 bld->static_texture_state->pot_height,
697 bld->static_sampler_state->wrap_t);
698 lp_build_name(y, "tex.y.wrapped");
699
700 if (dims == 3) {
701 z = lp_build_sample_wrap_nearest(bld, r, depth_vec, flt_depth_vec, offsets[2],
702 bld->static_texture_state->pot_depth,
703 bld->static_sampler_state->wrap_r);
704 lp_build_name(z, "tex.z.wrapped");
705 }
706 }
707 if (bld->static_texture_state->target == PIPE_TEXTURE_CUBE ||
708 bld->static_texture_state->target == PIPE_TEXTURE_1D_ARRAY ||
709 bld->static_texture_state->target == PIPE_TEXTURE_2D_ARRAY) {
710 z = r;
711 lp_build_name(z, "tex.z.layer");
712 }
713
714 /*
715 * Get texture colors.
716 */
717 lp_build_sample_texel_soa(bld, sampler_unit,
718 width_vec, height_vec, depth_vec,
719 x, y, z,
720 row_stride_vec, img_stride_vec,
721 data_ptr, mipoffsets, colors_out);
722 }
723
724
725 /**
726 * Generate code to sample a mipmap level with linear filtering.
727 * If sampling a cube texture, r = cube face in [0,5].
728 */
729 static void
730 lp_build_sample_image_linear(struct lp_build_sample_context *bld,
731 unsigned sampler_unit,
732 LLVMValueRef size,
733 LLVMValueRef row_stride_vec,
734 LLVMValueRef img_stride_vec,
735 LLVMValueRef data_ptr,
736 LLVMValueRef mipoffsets,
737 LLVMValueRef s,
738 LLVMValueRef t,
739 LLVMValueRef r,
740 const LLVMValueRef *offsets,
741 LLVMValueRef colors_out[4])
742 {
743 const unsigned dims = bld->dims;
744 LLVMValueRef width_vec;
745 LLVMValueRef height_vec;
746 LLVMValueRef depth_vec;
747 LLVMValueRef flt_size;
748 LLVMValueRef flt_width_vec;
749 LLVMValueRef flt_height_vec;
750 LLVMValueRef flt_depth_vec;
751 LLVMValueRef x0, y0 = NULL, z0 = NULL, x1, y1 = NULL, z1 = NULL;
752 LLVMValueRef s_fpart, t_fpart = NULL, r_fpart = NULL;
753 LLVMValueRef neighbors[2][2][4];
754 int chan;
755
756 lp_build_extract_image_sizes(bld,
757 &bld->int_size_bld,
758 bld->int_coord_type,
759 size,
760 &width_vec, &height_vec, &depth_vec);
761
762 flt_size = lp_build_int_to_float(&bld->float_size_bld, size);
763
764 lp_build_extract_image_sizes(bld,
765 &bld->float_size_bld,
766 bld->coord_type,
767 flt_size,
768 &flt_width_vec, &flt_height_vec, &flt_depth_vec);
769
770 /*
771 * Compute integer texcoords.
772 */
773 lp_build_sample_wrap_linear(bld, s, width_vec, flt_width_vec, offsets[0],
774 bld->static_texture_state->pot_width,
775 bld->static_sampler_state->wrap_s,
776 &x0, &x1, &s_fpart);
777 lp_build_name(x0, "tex.x0.wrapped");
778 lp_build_name(x1, "tex.x1.wrapped");
779
780 if (dims >= 2) {
781 lp_build_sample_wrap_linear(bld, t, height_vec, flt_height_vec, offsets[1],
782 bld->static_texture_state->pot_height,
783 bld->static_sampler_state->wrap_t,
784 &y0, &y1, &t_fpart);
785 lp_build_name(y0, "tex.y0.wrapped");
786 lp_build_name(y1, "tex.y1.wrapped");
787
788 if (dims == 3) {
789 lp_build_sample_wrap_linear(bld, r, depth_vec, flt_depth_vec, offsets[2],
790 bld->static_texture_state->pot_depth,
791 bld->static_sampler_state->wrap_r,
792 &z0, &z1, &r_fpart);
793 lp_build_name(z0, "tex.z0.wrapped");
794 lp_build_name(z1, "tex.z1.wrapped");
795 }
796 }
797 if (bld->static_texture_state->target == PIPE_TEXTURE_CUBE ||
798 bld->static_texture_state->target == PIPE_TEXTURE_1D_ARRAY ||
799 bld->static_texture_state->target == PIPE_TEXTURE_2D_ARRAY) {
800 z0 = z1 = r; /* cube face or array layer */
801 lp_build_name(z0, "tex.z0.layer");
802 lp_build_name(z1, "tex.z1.layer");
803 }
804
805
806 /*
807 * Get texture colors.
808 */
809 /* get x0/x1 texels */
810 lp_build_sample_texel_soa(bld, sampler_unit,
811 width_vec, height_vec, depth_vec,
812 x0, y0, z0,
813 row_stride_vec, img_stride_vec,
814 data_ptr, mipoffsets, neighbors[0][0]);
815 lp_build_sample_texel_soa(bld, sampler_unit,
816 width_vec, height_vec, depth_vec,
817 x1, y0, z0,
818 row_stride_vec, img_stride_vec,
819 data_ptr, mipoffsets, neighbors[0][1]);
820
821 if (dims == 1) {
822 /* Interpolate two samples from 1D image to produce one color */
823 for (chan = 0; chan < 4; chan++) {
824 colors_out[chan] = lp_build_lerp(&bld->texel_bld, s_fpart,
825 neighbors[0][0][chan],
826 neighbors[0][1][chan],
827 0);
828 }
829 }
830 else {
831 /* 2D/3D texture */
832 LLVMValueRef colors0[4];
833
834 /* get x0/x1 texels at y1 */
835 lp_build_sample_texel_soa(bld, sampler_unit,
836 width_vec, height_vec, depth_vec,
837 x0, y1, z0,
838 row_stride_vec, img_stride_vec,
839 data_ptr, mipoffsets, neighbors[1][0]);
840 lp_build_sample_texel_soa(bld, sampler_unit,
841 width_vec, height_vec, depth_vec,
842 x1, y1, z0,
843 row_stride_vec, img_stride_vec,
844 data_ptr, mipoffsets, neighbors[1][1]);
845
846 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
847 for (chan = 0; chan < 4; chan++) {
848 colors0[chan] = lp_build_lerp_2d(&bld->texel_bld,
849 s_fpart, t_fpart,
850 neighbors[0][0][chan],
851 neighbors[0][1][chan],
852 neighbors[1][0][chan],
853 neighbors[1][1][chan],
854 0);
855 }
856
857 if (dims == 3) {
858 LLVMValueRef neighbors1[2][2][4];
859 LLVMValueRef colors1[4];
860
861 /* get x0/x1/y0/y1 texels at z1 */
862 lp_build_sample_texel_soa(bld, sampler_unit,
863 width_vec, height_vec, depth_vec,
864 x0, y0, z1,
865 row_stride_vec, img_stride_vec,
866 data_ptr, mipoffsets, neighbors1[0][0]);
867 lp_build_sample_texel_soa(bld, sampler_unit,
868 width_vec, height_vec, depth_vec,
869 x1, y0, z1,
870 row_stride_vec, img_stride_vec,
871 data_ptr, mipoffsets, neighbors1[0][1]);
872 lp_build_sample_texel_soa(bld, sampler_unit,
873 width_vec, height_vec, depth_vec,
874 x0, y1, z1,
875 row_stride_vec, img_stride_vec,
876 data_ptr, mipoffsets, neighbors1[1][0]);
877 lp_build_sample_texel_soa(bld, sampler_unit,
878 width_vec, height_vec, depth_vec,
879 x1, y1, z1,
880 row_stride_vec, img_stride_vec,
881 data_ptr, mipoffsets, neighbors1[1][1]);
882
883 /* Bilinear interpolate the four samples from the second Z slice */
884 for (chan = 0; chan < 4; chan++) {
885 colors1[chan] = lp_build_lerp_2d(&bld->texel_bld,
886 s_fpart, t_fpart,
887 neighbors1[0][0][chan],
888 neighbors1[0][1][chan],
889 neighbors1[1][0][chan],
890 neighbors1[1][1][chan],
891 0);
892 }
893
894 /* Linearly interpolate the two samples from the two 3D slices */
895 for (chan = 0; chan < 4; chan++) {
896 colors_out[chan] = lp_build_lerp(&bld->texel_bld,
897 r_fpart,
898 colors0[chan], colors1[chan],
899 0);
900 }
901 }
902 else {
903 /* 2D tex */
904 for (chan = 0; chan < 4; chan++) {
905 colors_out[chan] = colors0[chan];
906 }
907 }
908 }
909 }
910
911
912 /**
913 * Sample the texture/mipmap using given image filter and mip filter.
914 * data0_ptr and data1_ptr point to the two mipmap levels to sample
915 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
916 * If we're using nearest miplevel sampling the '1' values will be null/unused.
917 */
918 static void
919 lp_build_sample_mipmap(struct lp_build_sample_context *bld,
920 unsigned sampler_unit,
921 unsigned img_filter,
922 unsigned mip_filter,
923 LLVMValueRef s,
924 LLVMValueRef t,
925 LLVMValueRef r,
926 const LLVMValueRef *offsets,
927 LLVMValueRef ilevel0,
928 LLVMValueRef ilevel1,
929 LLVMValueRef lod_fpart,
930 LLVMValueRef *colors_out)
931 {
932 LLVMBuilderRef builder = bld->gallivm->builder;
933 LLVMValueRef size0 = NULL;
934 LLVMValueRef size1 = NULL;
935 LLVMValueRef row_stride0_vec = NULL;
936 LLVMValueRef row_stride1_vec = NULL;
937 LLVMValueRef img_stride0_vec = NULL;
938 LLVMValueRef img_stride1_vec = NULL;
939 LLVMValueRef data_ptr0 = NULL;
940 LLVMValueRef data_ptr1 = NULL;
941 LLVMValueRef mipoff0 = NULL;
942 LLVMValueRef mipoff1 = NULL;
943 LLVMValueRef colors0[4], colors1[4];
944 unsigned chan;
945
946 /* sample the first mipmap level */
947 lp_build_mipmap_level_sizes(bld, ilevel0,
948 &size0,
949 &row_stride0_vec, &img_stride0_vec);
950 if (bld->num_lods == 1) {
951 data_ptr0 = lp_build_get_mipmap_level(bld, ilevel0);
952 }
953 else {
954 /* This path should work for num_lods 1 too but slightly less efficient */
955 data_ptr0 = bld->base_ptr;
956 mipoff0 = lp_build_get_mip_offsets(bld, ilevel0);
957 }
958 if (img_filter == PIPE_TEX_FILTER_NEAREST) {
959 lp_build_sample_image_nearest(bld, sampler_unit,
960 size0,
961 row_stride0_vec, img_stride0_vec,
962 data_ptr0, mipoff0, s, t, r, offsets,
963 colors0);
964 }
965 else {
966 assert(img_filter == PIPE_TEX_FILTER_LINEAR);
967 lp_build_sample_image_linear(bld, sampler_unit,
968 size0,
969 row_stride0_vec, img_stride0_vec,
970 data_ptr0, mipoff0, s, t, r, offsets,
971 colors0);
972 }
973
974 /* Store the first level's colors in the output variables */
975 for (chan = 0; chan < 4; chan++) {
976 LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
977 }
978
979 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
980 struct lp_build_if_state if_ctx;
981 LLVMValueRef need_lerp;
982
983 /* need_lerp = lod_fpart > 0 */
984 if (bld->num_lods == 1) {
985 need_lerp = LLVMBuildFCmp(builder, LLVMRealUGT,
986 lod_fpart, bld->levelf_bld.zero,
987 "need_lerp");
988 }
989 else {
990 /*
991 * We'll do mip filtering if any of the quads (or individual
992 * pixel in case of per-pixel lod) need it.
993 * It might be better to split the vectors here and only fetch/filter
994 * quads which need it.
995 */
996 /*
997 * We unfortunately need to clamp lod_fpart here since we can get
998 * negative values which would screw up filtering if not all
999 * lod_fpart values have same sign.
1000 */
1001 lod_fpart = lp_build_max(&bld->levelf_bld, lod_fpart,
1002 bld->levelf_bld.zero);
1003 need_lerp = lp_build_compare(bld->gallivm, bld->levelf_bld.type,
1004 PIPE_FUNC_GREATER,
1005 lod_fpart, bld->levelf_bld.zero);
1006 need_lerp = lp_build_any_true_range(&bld->leveli_bld, bld->num_lods, need_lerp);
1007 }
1008
1009 lp_build_if(&if_ctx, bld->gallivm, need_lerp);
1010 {
1011 /* sample the second mipmap level */
1012 lp_build_mipmap_level_sizes(bld, ilevel1,
1013 &size1,
1014 &row_stride1_vec, &img_stride1_vec);
1015 if (bld->num_lods == 1) {
1016 data_ptr1 = lp_build_get_mipmap_level(bld, ilevel1);
1017 }
1018 else {
1019 data_ptr1 = bld->base_ptr;
1020 mipoff1 = lp_build_get_mip_offsets(bld, ilevel1);
1021 }
1022 if (img_filter == PIPE_TEX_FILTER_NEAREST) {
1023 lp_build_sample_image_nearest(bld, sampler_unit,
1024 size1,
1025 row_stride1_vec, img_stride1_vec,
1026 data_ptr1, mipoff1, s, t, r, offsets,
1027 colors1);
1028 }
1029 else {
1030 lp_build_sample_image_linear(bld, sampler_unit,
1031 size1,
1032 row_stride1_vec, img_stride1_vec,
1033 data_ptr1, mipoff1, s, t, r, offsets,
1034 colors1);
1035 }
1036
1037 /* interpolate samples from the two mipmap levels */
1038
1039 if (bld->num_lods != bld->coord_type.length)
1040 lod_fpart = lp_build_unpack_broadcast_aos_scalars(bld->gallivm,
1041 bld->levelf_bld.type,
1042 bld->texel_bld.type,
1043 lod_fpart);
1044
1045 for (chan = 0; chan < 4; chan++) {
1046 colors0[chan] = lp_build_lerp(&bld->texel_bld, lod_fpart,
1047 colors0[chan], colors1[chan],
1048 0);
1049 LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
1050 }
1051 }
1052 lp_build_endif(&if_ctx);
1053 }
1054 }
1055
1056
1057 /**
1058 * Build (per-coord) layer value.
1059 * Either clamp layer to valid values or fill in optional out_of_bounds
1060 * value and just return value unclamped.
1061 */
1062 static LLVMValueRef
1063 lp_build_layer_coord(struct lp_build_sample_context *bld,
1064 unsigned texture_unit,
1065 LLVMValueRef layer,
1066 LLVMValueRef *out_of_bounds)
1067 {
1068 LLVMValueRef num_layers;
1069 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
1070
1071 num_layers = bld->dynamic_state->depth(bld->dynamic_state,
1072 bld->gallivm, texture_unit);
1073
1074 if (out_of_bounds) {
1075 LLVMValueRef out1, out;
1076 num_layers = lp_build_broadcast_scalar(int_coord_bld, num_layers);
1077 out = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, layer, int_coord_bld->zero);
1078 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, layer, num_layers);
1079 *out_of_bounds = lp_build_or(int_coord_bld, out, out1);
1080 return layer;
1081 }
1082 else {
1083 LLVMValueRef maxlayer;
1084 maxlayer = lp_build_sub(&bld->int_bld, num_layers, bld->int_bld.one);
1085 maxlayer = lp_build_broadcast_scalar(int_coord_bld, maxlayer);
1086 return lp_build_clamp(int_coord_bld, layer, int_coord_bld->zero, maxlayer);
1087 }
1088 }
1089
1090
1091 /**
1092 * Calculate cube face, lod, mip levels.
1093 */
1094 static void
1095 lp_build_sample_common(struct lp_build_sample_context *bld,
1096 unsigned texture_index,
1097 unsigned sampler_index,
1098 LLVMValueRef *s,
1099 LLVMValueRef *t,
1100 LLVMValueRef *r,
1101 const struct lp_derivatives *derivs, /* optional */
1102 LLVMValueRef lod_bias, /* optional */
1103 LLVMValueRef explicit_lod, /* optional */
1104 LLVMValueRef *lod_ipart,
1105 LLVMValueRef *lod_fpart,
1106 LLVMValueRef *ilevel0,
1107 LLVMValueRef *ilevel1)
1108 {
1109 const unsigned mip_filter = bld->static_sampler_state->min_mip_filter;
1110 const unsigned min_filter = bld->static_sampler_state->min_img_filter;
1111 const unsigned mag_filter = bld->static_sampler_state->mag_img_filter;
1112 const unsigned target = bld->static_texture_state->target;
1113 LLVMValueRef first_level, cube_rho = NULL;
1114
1115 /*
1116 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1117 mip_filter, min_filter, mag_filter);
1118 */
1119
1120 /*
1121 * Choose cube face, recompute texcoords for the chosen face and
1122 * compute rho here too (as it requires transform of derivatives).
1123 */
1124 if (target == PIPE_TEXTURE_CUBE) {
1125 LLVMValueRef face, face_s, face_t;
1126 boolean need_derivs;
1127 need_derivs = ((min_filter != mag_filter ||
1128 mip_filter != PIPE_TEX_MIPFILTER_NONE) &&
1129 !bld->static_sampler_state->min_max_lod_equal &&
1130 !explicit_lod);
1131 lp_build_cube_lookup(bld, *s, *t, *r, derivs, &face, &face_s, &face_t,
1132 &cube_rho, need_derivs);
1133 *s = face_s; /* vec */
1134 *t = face_t; /* vec */
1135 /* use 'r' to indicate cube face */
1136 *r = face; /* vec */
1137 }
1138 else if (target == PIPE_TEXTURE_1D_ARRAY) {
1139 *r = lp_build_iround(&bld->coord_bld, *t);
1140 *r = lp_build_layer_coord(bld, texture_index, *r, NULL);
1141 }
1142 else if (target == PIPE_TEXTURE_2D_ARRAY) {
1143 *r = lp_build_iround(&bld->coord_bld, *r);
1144 *r = lp_build_layer_coord(bld, texture_index, *r, NULL);
1145 }
1146
1147 /*
1148 * Compute the level of detail (float).
1149 */
1150 if (min_filter != mag_filter ||
1151 mip_filter != PIPE_TEX_MIPFILTER_NONE) {
1152 /* Need to compute lod either to choose mipmap levels or to
1153 * distinguish between minification/magnification with one mipmap level.
1154 */
1155 lp_build_lod_selector(bld, texture_index, sampler_index,
1156 *s, *t, *r, cube_rho,
1157 derivs, lod_bias, explicit_lod,
1158 mip_filter,
1159 lod_ipart, lod_fpart);
1160 } else {
1161 *lod_ipart = bld->leveli_bld.zero;
1162 }
1163
1164 /*
1165 * Compute integer mipmap level(s) to fetch texels from: ilevel0, ilevel1
1166 */
1167 switch (mip_filter) {
1168 default:
1169 assert(0 && "bad mip_filter value in lp_build_sample_soa()");
1170 /* fall-through */
1171 case PIPE_TEX_MIPFILTER_NONE:
1172 /* always use mip level 0 */
1173 if (HAVE_LLVM == 0x0207 && target == PIPE_TEXTURE_CUBE) {
1174 /* XXX this is a work-around for an apparent bug in LLVM 2.7.
1175 * We should be able to set ilevel0 = const(0) but that causes
1176 * bad x86 code to be emitted.
1177 */
1178 assert(*lod_ipart);
1179 lp_build_nearest_mip_level(bld, texture_index, *lod_ipart, ilevel0, NULL);
1180 }
1181 else {
1182 first_level = bld->dynamic_state->first_level(bld->dynamic_state,
1183 bld->gallivm, texture_index);
1184 first_level = lp_build_broadcast_scalar(&bld->leveli_bld, first_level);
1185 *ilevel0 = first_level;
1186 }
1187 break;
1188 case PIPE_TEX_MIPFILTER_NEAREST:
1189 assert(*lod_ipart);
1190 lp_build_nearest_mip_level(bld, texture_index, *lod_ipart, ilevel0, NULL);
1191 break;
1192 case PIPE_TEX_MIPFILTER_LINEAR:
1193 assert(*lod_ipart);
1194 assert(*lod_fpart);
1195 lp_build_linear_mip_levels(bld, texture_index,
1196 *lod_ipart, lod_fpart,
1197 ilevel0, ilevel1);
1198 break;
1199 }
1200 }
1201
1202 /**
1203 * General texture sampling codegen.
1204 * This function handles texture sampling for all texture targets (1D,
1205 * 2D, 3D, cube) and all filtering modes.
1206 */
1207 static void
1208 lp_build_sample_general(struct lp_build_sample_context *bld,
1209 unsigned sampler_unit,
1210 LLVMValueRef s,
1211 LLVMValueRef t,
1212 LLVMValueRef r,
1213 const LLVMValueRef *offsets,
1214 LLVMValueRef lod_ipart,
1215 LLVMValueRef lod_fpart,
1216 LLVMValueRef ilevel0,
1217 LLVMValueRef ilevel1,
1218 LLVMValueRef *colors_out)
1219 {
1220 struct lp_build_context *int_bld = &bld->int_bld;
1221 LLVMBuilderRef builder = bld->gallivm->builder;
1222 const unsigned mip_filter = bld->static_sampler_state->min_mip_filter;
1223 const unsigned min_filter = bld->static_sampler_state->min_img_filter;
1224 const unsigned mag_filter = bld->static_sampler_state->mag_img_filter;
1225 LLVMValueRef texels[4];
1226 unsigned chan;
1227
1228 /*
1229 * Get/interpolate texture colors.
1230 */
1231
1232 for (chan = 0; chan < 4; ++chan) {
1233 texels[chan] = lp_build_alloca(bld->gallivm, bld->texel_bld.vec_type, "");
1234 lp_build_name(texels[chan], "sampler%u_texel_%c_var", sampler_unit, "xyzw"[chan]);
1235 }
1236
1237 if (min_filter == mag_filter) {
1238 /* no need to distinguish between minification and magnification */
1239 lp_build_sample_mipmap(bld, sampler_unit,
1240 min_filter, mip_filter,
1241 s, t, r, offsets,
1242 ilevel0, ilevel1, lod_fpart,
1243 texels);
1244 }
1245 else {
1246 /* Emit conditional to choose min image filter or mag image filter
1247 * depending on the lod being > 0 or <= 0, respectively.
1248 */
1249 struct lp_build_if_state if_ctx;
1250 LLVMValueRef minify;
1251
1252 /*
1253 * XXX this should to all lods into account, if some are min
1254 * some max probably could hack up the coords/weights in the linear
1255 * path with selects to work for nearest.
1256 * If that's just two quads sitting next to each other it seems
1257 * quite ok to do the same filtering method on both though, at
1258 * least unless we have explicit lod (and who uses different
1259 * min/mag filter with that?)
1260 */
1261 if (bld->num_lods > 1)
1262 lod_ipart = LLVMBuildExtractElement(builder, lod_ipart,
1263 lp_build_const_int32(bld->gallivm, 0), "");
1264
1265 /* minify = lod >= 0.0 */
1266 minify = LLVMBuildICmp(builder, LLVMIntSGE,
1267 lod_ipart, int_bld->zero, "");
1268
1269 lp_build_if(&if_ctx, bld->gallivm, minify);
1270 {
1271 /* Use the minification filter */
1272 lp_build_sample_mipmap(bld, sampler_unit,
1273 min_filter, mip_filter,
1274 s, t, r, offsets,
1275 ilevel0, ilevel1, lod_fpart,
1276 texels);
1277 }
1278 lp_build_else(&if_ctx);
1279 {
1280 /* Use the magnification filter */
1281 lp_build_sample_mipmap(bld, sampler_unit,
1282 mag_filter, PIPE_TEX_MIPFILTER_NONE,
1283 s, t, r, offsets,
1284 ilevel0, NULL, NULL,
1285 texels);
1286 }
1287 lp_build_endif(&if_ctx);
1288 }
1289
1290 for (chan = 0; chan < 4; ++chan) {
1291 colors_out[chan] = LLVMBuildLoad(builder, texels[chan], "");
1292 lp_build_name(colors_out[chan], "sampler%u_texel_%c", sampler_unit, "xyzw"[chan]);
1293 }
1294 }
1295
1296
1297 /**
1298 * Texel fetch function.
1299 * In contrast to general sampling there is no filtering, no coord minification,
1300 * lod (if any) is always explicit uint, coords are uints (in terms of texel units)
1301 * directly to be applied to the selected mip level (after adding texel offsets).
1302 * This function handles texel fetch for all targets where texel fetch is supported
1303 * (no cube maps, but 1d, 2d, 3d are supported, arrays and buffers should be too).
1304 */
1305 static void
1306 lp_build_fetch_texel(struct lp_build_sample_context *bld,
1307 unsigned texture_unit,
1308 const LLVMValueRef *coords,
1309 LLVMValueRef explicit_lod,
1310 const LLVMValueRef *offsets,
1311 LLVMValueRef *colors_out)
1312 {
1313 struct lp_build_context *perquadi_bld = &bld->leveli_bld;
1314 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
1315 unsigned dims = bld->dims, chan;
1316 unsigned target = bld->static_texture_state->target;
1317 boolean out_of_bound_ret_zero = TRUE;
1318 LLVMValueRef size, ilevel;
1319 LLVMValueRef row_stride_vec = NULL, img_stride_vec = NULL;
1320 LLVMValueRef x = coords[0], y = coords[1], z = coords[2];
1321 LLVMValueRef width, height, depth, i, j;
1322 LLVMValueRef offset, out_of_bounds, out1;
1323
1324 out_of_bounds = int_coord_bld->zero;
1325
1326 if (explicit_lod && bld->static_texture_state->target != PIPE_BUFFER) {
1327 if (bld->num_lods != int_coord_bld->type.length) {
1328 ilevel = lp_build_pack_aos_scalars(bld->gallivm, int_coord_bld->type,
1329 perquadi_bld->type, explicit_lod, 0);
1330 }
1331 else {
1332 ilevel = explicit_lod;
1333 }
1334 lp_build_nearest_mip_level(bld, texture_unit, ilevel, &ilevel,
1335 out_of_bound_ret_zero ? &out_of_bounds : NULL);
1336 }
1337 else {
1338 assert(bld->num_lods == 1);
1339 if (bld->static_texture_state->target != PIPE_BUFFER) {
1340 ilevel = bld->dynamic_state->first_level(bld->dynamic_state,
1341 bld->gallivm, texture_unit);
1342 }
1343 else {
1344 ilevel = lp_build_const_int32(bld->gallivm, 0);
1345 }
1346 }
1347 lp_build_mipmap_level_sizes(bld, ilevel,
1348 &size,
1349 &row_stride_vec, &img_stride_vec);
1350 lp_build_extract_image_sizes(bld, &bld->int_size_bld, int_coord_bld->type,
1351 size, &width, &height, &depth);
1352
1353 if (target == PIPE_TEXTURE_1D_ARRAY ||
1354 target == PIPE_TEXTURE_2D_ARRAY) {
1355 if (target == PIPE_TEXTURE_1D_ARRAY) {
1356 z = y;
1357 }
1358 if (out_of_bound_ret_zero) {
1359 z = lp_build_layer_coord(bld, texture_unit, z, &out1);
1360 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
1361 }
1362 else {
1363 z = lp_build_layer_coord(bld, texture_unit, z, NULL);
1364 }
1365 }
1366
1367 /* This is a lot like border sampling */
1368 if (offsets[0]) {
1369 /*
1370 * coords are really unsigned, offsets are signed, but I don't think
1371 * exceeding 31 bits is possible
1372 */
1373 x = lp_build_add(int_coord_bld, x, offsets[0]);
1374 }
1375 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, x, int_coord_bld->zero);
1376 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
1377 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, x, width);
1378 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
1379
1380 if (dims >= 2) {
1381 if (offsets[1]) {
1382 y = lp_build_add(int_coord_bld, y, offsets[1]);
1383 }
1384 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, y, int_coord_bld->zero);
1385 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
1386 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, y, height);
1387 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
1388
1389 if (dims >= 3) {
1390 if (offsets[2]) {
1391 z = lp_build_add(int_coord_bld, z, offsets[2]);
1392 }
1393 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, z, int_coord_bld->zero);
1394 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
1395 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, z, depth);
1396 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
1397 }
1398 }
1399
1400 lp_build_sample_offset(int_coord_bld,
1401 bld->format_desc,
1402 x, y, z, row_stride_vec, img_stride_vec,
1403 &offset, &i, &j);
1404
1405 if (bld->static_texture_state->target != PIPE_BUFFER) {
1406 offset = lp_build_add(int_coord_bld, offset,
1407 lp_build_get_mip_offsets(bld, ilevel));
1408 }
1409
1410 offset = lp_build_andnot(int_coord_bld, offset, out_of_bounds);
1411
1412 lp_build_fetch_rgba_soa(bld->gallivm,
1413 bld->format_desc,
1414 bld->texel_type,
1415 bld->base_ptr, offset,
1416 i, j,
1417 colors_out);
1418
1419 if (out_of_bound_ret_zero) {
1420 /*
1421 * Only needed for ARB_robust_buffer_access_behavior and d3d10.
1422 * Could use min/max above instead of out-of-bounds comparisons
1423 * if we don't care about the result returned for out-of-bounds.
1424 */
1425 for (chan = 0; chan < 4; chan++) {
1426 colors_out[chan] = lp_build_select(&bld->texel_bld, out_of_bounds,
1427 bld->texel_bld.zero, colors_out[chan]);
1428 }
1429 }
1430 }
1431
1432
1433 /**
1434 * Do shadow test/comparison.
1435 * \param coords incoming texcoords
1436 * \param texel the texel to compare against (use the X channel)
1437 * Ideally this should really be done per-sample.
1438 */
1439 static void
1440 lp_build_sample_compare(struct lp_build_sample_context *bld,
1441 const LLVMValueRef *coords,
1442 LLVMValueRef texel[4])
1443 {
1444 struct lp_build_context *texel_bld = &bld->texel_bld;
1445 LLVMBuilderRef builder = bld->gallivm->builder;
1446 LLVMValueRef res, p;
1447 const unsigned chan = 0;
1448 unsigned chan_type;
1449 const struct util_format_description *format_desc;
1450
1451 if (bld->static_sampler_state->compare_mode == PIPE_TEX_COMPARE_NONE)
1452 return;
1453
1454 if (bld->static_texture_state->target == PIPE_TEXTURE_2D_ARRAY ||
1455 bld->static_texture_state->target == PIPE_TEXTURE_CUBE) {
1456 p = coords[3];
1457 }
1458 else {
1459 p = coords[2];
1460 }
1461
1462 /* debug code */
1463 if (0) {
1464 LLVMValueRef indx = lp_build_const_int32(bld->gallivm, 0);
1465 LLVMValueRef coord = LLVMBuildExtractElement(builder, p, indx, "");
1466 LLVMValueRef tex = LLVMBuildExtractElement(builder, texel[chan], indx, "");
1467 lp_build_printf(bld->gallivm, "shadow compare coord %f to texture %f\n",
1468 coord, tex);
1469 }
1470
1471 /* Clamp p coords to [0,1] for fixed function depth texture format */
1472 format_desc = util_format_description(bld->static_texture_state->format);
1473 /* not entirely sure we couldn't end up with non-valid swizzle here */
1474 chan_type = format_desc->swizzle[0] <= UTIL_FORMAT_SWIZZLE_W ?
1475 format_desc->channel[format_desc->swizzle[0]].type :
1476 UTIL_FORMAT_TYPE_FLOAT;
1477 if (chan_type != UTIL_FORMAT_TYPE_FLOAT) {
1478 p = lp_build_clamp(&bld->coord_bld, p,
1479 bld->coord_bld.zero, bld->coord_bld.one);
1480 }
1481
1482 /*
1483 * technically this is not entirely correct for unorm depth as the ref value
1484 * should be converted to the depth format (quantization!) and comparison
1485 * then done in texture format.
1486 */
1487
1488 /* result = (p FUNC texel) ? 1 : 0 */
1489 /*
1490 * honor d3d10 floating point rules here, which state that comparisons
1491 * are ordered except NOT_EQUAL which is unordered.
1492 */
1493 if (bld->static_sampler_state->compare_func != PIPE_FUNC_NOTEQUAL) {
1494 res = lp_build_cmp_ordered(texel_bld, bld->static_sampler_state->compare_func,
1495 p, texel[chan]);
1496 }
1497 else {
1498 res = lp_build_cmp(texel_bld, bld->static_sampler_state->compare_func,
1499 p, texel[chan]);
1500 }
1501 res = lp_build_select(texel_bld, res, texel_bld->one, texel_bld->zero);
1502
1503 /*
1504 * returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE.
1505 * This should be ok because sampler swizzle is applied on top of it.
1506 */
1507 texel[0] =
1508 texel[1] =
1509 texel[2] = res;
1510 texel[3] = texel_bld->one;
1511 }
1512
1513
1514 /**
1515 * Just set texels to white instead of actually sampling the texture.
1516 * For debugging.
1517 */
1518 void
1519 lp_build_sample_nop(struct gallivm_state *gallivm,
1520 struct lp_type type,
1521 const LLVMValueRef *coords,
1522 LLVMValueRef texel_out[4])
1523 {
1524 LLVMValueRef one = lp_build_one(gallivm, type);
1525 unsigned chan;
1526
1527 for (chan = 0; chan < 4; chan++) {
1528 texel_out[chan] = one;
1529 }
1530 }
1531
1532
1533 /**
1534 * Build texture sampling code.
1535 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1536 * R, G, B, A.
1537 * \param type vector float type to use for coords, etc.
1538 * \param is_fetch if this is a texel fetch instruction.
1539 * \param derivs partial derivatives of (s,t,r,q) with respect to x and y
1540 */
1541 void
1542 lp_build_sample_soa(struct gallivm_state *gallivm,
1543 const struct lp_static_texture_state *static_texture_state,
1544 const struct lp_static_sampler_state *static_sampler_state,
1545 struct lp_sampler_dynamic_state *dynamic_state,
1546 struct lp_type type,
1547 boolean is_fetch,
1548 unsigned texture_index,
1549 unsigned sampler_index,
1550 const LLVMValueRef *coords,
1551 const LLVMValueRef *offsets,
1552 const struct lp_derivatives *derivs, /* optional */
1553 LLVMValueRef lod_bias, /* optional */
1554 LLVMValueRef explicit_lod, /* optional */
1555 boolean scalar_lod,
1556 LLVMValueRef texel_out[4])
1557 {
1558 unsigned dims = texture_dims(static_texture_state->target);
1559 unsigned num_quads = type.length / 4;
1560 unsigned mip_filter;
1561 struct lp_build_sample_context bld;
1562 struct lp_static_sampler_state derived_sampler_state = *static_sampler_state;
1563 LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context);
1564 LLVMBuilderRef builder = gallivm->builder;
1565 LLVMValueRef tex_width;
1566 LLVMValueRef s;
1567 LLVMValueRef t;
1568 LLVMValueRef r;
1569
1570 if (0) {
1571 enum pipe_format fmt = static_texture_state->format;
1572 debug_printf("Sample from %s\n", util_format_name(fmt));
1573 }
1574
1575 assert(type.floating);
1576
1577 /* Setup our build context */
1578 memset(&bld, 0, sizeof bld);
1579 bld.gallivm = gallivm;
1580 bld.static_sampler_state = &derived_sampler_state;
1581 bld.static_texture_state = static_texture_state;
1582 bld.dynamic_state = dynamic_state;
1583 bld.format_desc = util_format_description(static_texture_state->format);
1584 bld.dims = dims;
1585
1586 bld.vector_width = lp_type_width(type);
1587
1588 bld.float_type = lp_type_float(32);
1589 bld.int_type = lp_type_int(32);
1590 bld.coord_type = type;
1591 bld.int_coord_type = lp_int_type(type);
1592 bld.float_size_in_type = lp_type_float(32);
1593 bld.float_size_in_type.length = dims > 1 ? 4 : 1;
1594 bld.int_size_in_type = lp_int_type(bld.float_size_in_type);
1595 bld.texel_type = type;
1596
1597 /* always using the first channel hopefully should be safe,
1598 * if not things WILL break in other places anyway.
1599 */
1600 if (bld.format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB &&
1601 bld.format_desc->channel[0].pure_integer) {
1602 if (bld.format_desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
1603 bld.texel_type = lp_type_int_vec(type.width, type.width * type.length);
1604 }
1605 else if (bld.format_desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED) {
1606 bld.texel_type = lp_type_uint_vec(type.width, type.width * type.length);
1607 }
1608 }
1609 else if (util_format_has_stencil(bld.format_desc) &&
1610 !util_format_has_depth(bld.format_desc)) {
1611 /* for stencil only formats, sample stencil (uint) */
1612 bld.texel_type = lp_type_int_vec(type.width, type.width * type.length);
1613 }
1614
1615 if (!static_texture_state->level_zero_only) {
1616 derived_sampler_state.min_mip_filter = static_sampler_state->min_mip_filter;
1617 } else {
1618 derived_sampler_state.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
1619 }
1620 mip_filter = derived_sampler_state.min_mip_filter;
1621
1622 if (0) {
1623 debug_printf(" .min_mip_filter = %u\n", derived_sampler_state.min_mip_filter);
1624 }
1625
1626 /*
1627 * This is all a bit complicated different paths are chosen for performance
1628 * reasons.
1629 * Essentially, there can be 1 lod per element, 1 lod per quad or 1 lod for
1630 * everything (the last two options are equivalent for 4-wide case).
1631 * If there's per-quad lod but we split to 4-wide so we can use AoS, per-quad
1632 * lod is calculated then the lod value extracted afterwards so making this
1633 * case basically the same as far as lod handling is concerned for the
1634 * further sample/filter code as the 1 lod for everything case.
1635 * Different lod handling mostly shows up when building mipmap sizes
1636 * (lp_build_mipmap_level_sizes() and friends) and also in filtering
1637 * (getting the fractional part of the lod to the right texels).
1638 */
1639
1640 /*
1641 * There are other situations where at least the multiple int lods could be
1642 * avoided like min and max lod being equal.
1643 */
1644 if (explicit_lod && !scalar_lod &&
1645 ((is_fetch && bld.static_texture_state->target != PIPE_BUFFER) ||
1646 (!is_fetch && mip_filter != PIPE_TEX_MIPFILTER_NONE)))
1647 bld.num_lods = type.length;
1648 /* TODO: for true scalar_lod should only use 1 lod value */
1649 else if ((is_fetch && explicit_lod && bld.static_texture_state->target != PIPE_BUFFER ) ||
1650 (!is_fetch && mip_filter != PIPE_TEX_MIPFILTER_NONE)) {
1651 bld.num_lods = num_quads;
1652 }
1653 else {
1654 bld.num_lods = 1;
1655 }
1656
1657 bld.levelf_type = type;
1658 /* we want native vector size to be able to use our intrinsics */
1659 if (bld.num_lods != type.length) {
1660 bld.levelf_type.length = type.length > 4 ? ((type.length + 15) / 16) * 4 : 1;
1661 }
1662 bld.leveli_type = lp_int_type(bld.levelf_type);
1663 bld.float_size_type = bld.float_size_in_type;
1664 /* Note: size vectors may not be native. They contain minified w/h/d/_ values,
1665 * with per-element lod that is w0/h0/d0/_/w1/h1/d1_/... so up to 8x4f32 */
1666 if (bld.num_lods > 1) {
1667 bld.float_size_type.length = bld.num_lods == type.length ?
1668 bld.num_lods * bld.float_size_in_type.length :
1669 type.length;
1670 }
1671 bld.int_size_type = lp_int_type(bld.float_size_type);
1672
1673 lp_build_context_init(&bld.float_bld, gallivm, bld.float_type);
1674 lp_build_context_init(&bld.float_vec_bld, gallivm, type);
1675 lp_build_context_init(&bld.int_bld, gallivm, bld.int_type);
1676 lp_build_context_init(&bld.coord_bld, gallivm, bld.coord_type);
1677 lp_build_context_init(&bld.int_coord_bld, gallivm, bld.int_coord_type);
1678 lp_build_context_init(&bld.int_size_in_bld, gallivm, bld.int_size_in_type);
1679 lp_build_context_init(&bld.float_size_in_bld, gallivm, bld.float_size_in_type);
1680 lp_build_context_init(&bld.int_size_bld, gallivm, bld.int_size_type);
1681 lp_build_context_init(&bld.float_size_bld, gallivm, bld.float_size_type);
1682 lp_build_context_init(&bld.texel_bld, gallivm, bld.texel_type);
1683 lp_build_context_init(&bld.levelf_bld, gallivm, bld.levelf_type);
1684 lp_build_context_init(&bld.leveli_bld, gallivm, bld.leveli_type);
1685
1686 /* Get the dynamic state */
1687 tex_width = dynamic_state->width(dynamic_state, gallivm, texture_index);
1688 bld.row_stride_array = dynamic_state->row_stride(dynamic_state, gallivm, texture_index);
1689 bld.img_stride_array = dynamic_state->img_stride(dynamic_state, gallivm, texture_index);
1690 bld.base_ptr = dynamic_state->base_ptr(dynamic_state, gallivm, texture_index);
1691 bld.mip_offsets = dynamic_state->mip_offsets(dynamic_state, gallivm, texture_index);
1692 /* Note that mip_offsets is an array[level] of offsets to texture images */
1693
1694 s = coords[0];
1695 t = coords[1];
1696 r = coords[2];
1697
1698 /* width, height, depth as single int vector */
1699 if (dims <= 1) {
1700 bld.int_size = tex_width;
1701 }
1702 else {
1703 bld.int_size = LLVMBuildInsertElement(builder, bld.int_size_in_bld.undef,
1704 tex_width, LLVMConstInt(i32t, 0, 0), "");
1705 if (dims >= 2) {
1706 LLVMValueRef tex_height =
1707 dynamic_state->height(dynamic_state, gallivm, texture_index);
1708 bld.int_size = LLVMBuildInsertElement(builder, bld.int_size,
1709 tex_height, LLVMConstInt(i32t, 1, 0), "");
1710 if (dims >= 3) {
1711 LLVMValueRef tex_depth =
1712 dynamic_state->depth(dynamic_state, gallivm, texture_index);
1713 bld.int_size = LLVMBuildInsertElement(builder, bld.int_size,
1714 tex_depth, LLVMConstInt(i32t, 2, 0), "");
1715 }
1716 }
1717 }
1718
1719 if (0) {
1720 /* For debug: no-op texture sampling */
1721 lp_build_sample_nop(gallivm,
1722 bld.texel_type,
1723 coords,
1724 texel_out);
1725 }
1726
1727 else if (is_fetch) {
1728 lp_build_fetch_texel(&bld, texture_index, coords,
1729 explicit_lod, offsets,
1730 texel_out);
1731 }
1732
1733 else {
1734 LLVMValueRef lod_ipart = NULL, lod_fpart = NULL;
1735 LLVMValueRef ilevel0 = NULL, ilevel1 = NULL;
1736 boolean use_aos = util_format_fits_8unorm(bld.format_desc) &&
1737 lp_is_simple_wrap_mode(static_sampler_state->wrap_s) &&
1738 lp_is_simple_wrap_mode(static_sampler_state->wrap_t);
1739
1740 if ((gallivm_debug & GALLIVM_DEBUG_PERF) &&
1741 !use_aos && util_format_fits_8unorm(bld.format_desc)) {
1742 debug_printf("%s: using floating point linear filtering for %s\n",
1743 __FUNCTION__, bld.format_desc->short_name);
1744 debug_printf(" min_img %d mag_img %d mip %d wraps %d wrapt %d\n",
1745 static_sampler_state->min_img_filter,
1746 static_sampler_state->mag_img_filter,
1747 static_sampler_state->min_mip_filter,
1748 static_sampler_state->wrap_s,
1749 static_sampler_state->wrap_t);
1750 }
1751
1752 lp_build_sample_common(&bld, texture_index, sampler_index,
1753 &s, &t, &r,
1754 derivs, lod_bias, explicit_lod,
1755 &lod_ipart, &lod_fpart,
1756 &ilevel0, &ilevel1);
1757
1758 /*
1759 * we only try 8-wide sampling with soa as it appears to
1760 * be a loss with aos with AVX (but it should work).
1761 * (It should be faster if we'd support avx2)
1762 */
1763 if (num_quads == 1 || !use_aos) {
1764
1765 if (num_quads > 1) {
1766 if (mip_filter == PIPE_TEX_MIPFILTER_NONE) {
1767 LLVMValueRef index0 = lp_build_const_int32(gallivm, 0);
1768 /*
1769 * These parameters are the same for all quads,
1770 * could probably simplify.
1771 */
1772 lod_ipart = LLVMBuildExtractElement(builder, lod_ipart, index0, "");
1773 ilevel0 = LLVMBuildExtractElement(builder, ilevel0, index0, "");
1774 }
1775 }
1776 if (use_aos) {
1777 /* do sampling/filtering with fixed pt arithmetic */
1778 lp_build_sample_aos(&bld, sampler_index,
1779 s, t, r, offsets,
1780 lod_ipart, lod_fpart,
1781 ilevel0, ilevel1,
1782 texel_out);
1783 }
1784
1785 else {
1786 lp_build_sample_general(&bld, sampler_index,
1787 s, t, r, offsets,
1788 lod_ipart, lod_fpart,
1789 ilevel0, ilevel1,
1790 texel_out);
1791 }
1792 }
1793 else {
1794 unsigned j;
1795 struct lp_build_sample_context bld4;
1796 struct lp_type type4 = type;
1797 unsigned i;
1798 LLVMValueRef texelout4[4];
1799 LLVMValueRef texelouttmp[4][LP_MAX_VECTOR_LENGTH/16];
1800
1801 type4.length = 4;
1802
1803 /* Setup our build context */
1804 memset(&bld4, 0, sizeof bld4);
1805 bld4.gallivm = bld.gallivm;
1806 bld4.static_texture_state = bld.static_texture_state;
1807 bld4.static_sampler_state = bld.static_sampler_state;
1808 bld4.dynamic_state = bld.dynamic_state;
1809 bld4.format_desc = bld.format_desc;
1810 bld4.dims = bld.dims;
1811 bld4.row_stride_array = bld.row_stride_array;
1812 bld4.img_stride_array = bld.img_stride_array;
1813 bld4.base_ptr = bld.base_ptr;
1814 bld4.mip_offsets = bld.mip_offsets;
1815 bld4.int_size = bld.int_size;
1816
1817 bld4.vector_width = lp_type_width(type4);
1818
1819 bld4.float_type = lp_type_float(32);
1820 bld4.int_type = lp_type_int(32);
1821 bld4.coord_type = type4;
1822 bld4.int_coord_type = lp_int_type(type4);
1823 bld4.float_size_in_type = lp_type_float(32);
1824 bld4.float_size_in_type.length = dims > 1 ? 4 : 1;
1825 bld4.int_size_in_type = lp_int_type(bld4.float_size_in_type);
1826 bld4.texel_type = bld.texel_type;
1827 bld4.texel_type.length = 4;
1828 bld4.levelf_type = type4;
1829 /* we want native vector size to be able to use our intrinsics */
1830 bld4.levelf_type.length = 1;
1831 bld4.leveli_type = lp_int_type(bld4.levelf_type);
1832
1833 if (explicit_lod && !scalar_lod &&
1834 ((is_fetch && bld.static_texture_state->target != PIPE_BUFFER) ||
1835 (!is_fetch && mip_filter != PIPE_TEX_MIPFILTER_NONE)))
1836 bld4.num_lods = type4.length;
1837 else
1838 bld4.num_lods = 1;
1839
1840 bld4.levelf_type = type4;
1841 /* we want native vector size to be able to use our intrinsics */
1842 if (bld4.num_lods != type4.length) {
1843 bld4.levelf_type.length = 1;
1844 }
1845 bld4.leveli_type = lp_int_type(bld4.levelf_type);
1846 bld4.float_size_type = bld4.float_size_in_type;
1847 if (bld4.num_lods > 1) {
1848 bld4.float_size_type.length = bld4.num_lods == type4.length ?
1849 bld4.num_lods * bld4.float_size_in_type.length :
1850 type4.length;
1851 }
1852 bld4.int_size_type = lp_int_type(bld4.float_size_type);
1853
1854 lp_build_context_init(&bld4.float_bld, gallivm, bld4.float_type);
1855 lp_build_context_init(&bld4.float_vec_bld, gallivm, type4);
1856 lp_build_context_init(&bld4.int_bld, gallivm, bld4.int_type);
1857 lp_build_context_init(&bld4.coord_bld, gallivm, bld4.coord_type);
1858 lp_build_context_init(&bld4.int_coord_bld, gallivm, bld4.int_coord_type);
1859 lp_build_context_init(&bld4.int_size_in_bld, gallivm, bld4.int_size_in_type);
1860 lp_build_context_init(&bld4.float_size_in_bld, gallivm, bld4.float_size_in_type);
1861 lp_build_context_init(&bld4.int_size_bld, gallivm, bld4.int_size_type);
1862 lp_build_context_init(&bld4.float_size_bld, gallivm, bld4.float_size_type);
1863 lp_build_context_init(&bld4.texel_bld, gallivm, bld4.texel_type);
1864 lp_build_context_init(&bld4.levelf_bld, gallivm, bld4.levelf_type);
1865 lp_build_context_init(&bld4.leveli_bld, gallivm, bld4.leveli_type);
1866
1867 for (i = 0; i < num_quads; i++) {
1868 LLVMValueRef s4, t4, r4;
1869 LLVMValueRef lod_ipart4, lod_fpart4 = NULL;
1870 LLVMValueRef ilevel04, ilevel14 = NULL;
1871 LLVMValueRef offsets4[4] = { NULL };
1872 unsigned num_lods = bld4.num_lods;
1873
1874 s4 = lp_build_extract_range(gallivm, s, 4*i, 4);
1875 t4 = lp_build_extract_range(gallivm, t, 4*i, 4);
1876 r4 = lp_build_extract_range(gallivm, r, 4*i, 4);
1877
1878 if (offsets[0]) {
1879 offsets4[0] = lp_build_extract_range(gallivm, offsets[0], 4*i, 4);
1880 if (dims > 1) {
1881 offsets4[1] = lp_build_extract_range(gallivm, offsets[1], 4*i, 4);
1882 if (dims > 2) {
1883 offsets4[2] = lp_build_extract_range(gallivm, offsets[2], 4*i, 4);
1884 }
1885 }
1886 }
1887 lod_ipart4 = lp_build_extract_range(gallivm, lod_ipart, num_lods * i, num_lods);
1888 ilevel04 = lp_build_extract_range(gallivm, ilevel0, num_lods * i, num_lods);
1889 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1890 ilevel14 = lp_build_extract_range(gallivm, ilevel1, num_lods * i, num_lods);
1891 lod_fpart4 = lp_build_extract_range(gallivm, lod_fpart, num_lods * i, num_lods);
1892 }
1893
1894 if (use_aos) {
1895 /* do sampling/filtering with fixed pt arithmetic */
1896 lp_build_sample_aos(&bld4, sampler_index,
1897 s4, t4, r4, offsets4,
1898 lod_ipart4, lod_fpart4,
1899 ilevel04, ilevel14,
1900 texelout4);
1901 }
1902
1903 else {
1904 lp_build_sample_general(&bld4, sampler_index,
1905 s4, t4, r4, offsets4,
1906 lod_ipart4, lod_fpart4,
1907 ilevel04, ilevel14,
1908 texelout4);
1909 }
1910 for (j = 0; j < 4; j++) {
1911 texelouttmp[j][i] = texelout4[j];
1912 }
1913 }
1914
1915 for (j = 0; j < 4; j++) {
1916 texel_out[j] = lp_build_concat(gallivm, texelouttmp[j], type4, num_quads);
1917 }
1918 }
1919
1920 lp_build_sample_compare(&bld, coords, texel_out);
1921 }
1922
1923 if (static_texture_state->target != PIPE_BUFFER) {
1924 apply_sampler_swizzle(&bld, texel_out);
1925 }
1926
1927 /*
1928 * texel type can be a (32bit) int/uint (for pure int formats only),
1929 * however we are expected to always return floats (storage is untyped).
1930 */
1931 if (!bld.texel_type.floating) {
1932 unsigned chan;
1933 for (chan = 0; chan < 4; chan++) {
1934 texel_out[chan] = LLVMBuildBitCast(builder, texel_out[chan],
1935 lp_build_vec_type(gallivm, type), "");
1936 }
1937 }
1938 }
1939
1940 void
1941 lp_build_size_query_soa(struct gallivm_state *gallivm,
1942 const struct lp_static_texture_state *static_state,
1943 struct lp_sampler_dynamic_state *dynamic_state,
1944 struct lp_type int_type,
1945 unsigned texture_unit,
1946 boolean need_nr_mips,
1947 boolean scalar_lod,
1948 LLVMValueRef explicit_lod,
1949 LLVMValueRef *sizes_out)
1950 {
1951 LLVMValueRef lod;
1952 LLVMValueRef size;
1953 LLVMValueRef first_level = NULL;
1954 int dims, i;
1955 boolean has_array;
1956 struct lp_build_context bld_int_vec;
1957
1958 dims = texture_dims(static_state->target);
1959
1960 switch (static_state->target) {
1961 case PIPE_TEXTURE_1D_ARRAY:
1962 case PIPE_TEXTURE_2D_ARRAY:
1963 has_array = TRUE;
1964 break;
1965 default:
1966 has_array = FALSE;
1967 break;
1968 }
1969
1970 assert(!int_type.floating);
1971
1972 lp_build_context_init(&bld_int_vec, gallivm, lp_type_int_vec(32, 128));
1973
1974 if (explicit_lod) {
1975 /* FIXME: this needs to honor per-element lod */
1976 lod = LLVMBuildExtractElement(gallivm->builder, explicit_lod, lp_build_const_int32(gallivm, 0), "");
1977 first_level = dynamic_state->first_level(dynamic_state, gallivm, texture_unit);
1978 lod = lp_build_broadcast_scalar(&bld_int_vec,
1979 LLVMBuildAdd(gallivm->builder, lod, first_level, "lod"));
1980
1981 } else {
1982 lod = bld_int_vec.zero;
1983 }
1984
1985 if (need_nr_mips) {
1986 size = bld_int_vec.zero;
1987 }
1988 else {
1989 size = bld_int_vec.undef;
1990 }
1991
1992 size = LLVMBuildInsertElement(gallivm->builder, size,
1993 dynamic_state->width(dynamic_state, gallivm, texture_unit),
1994 lp_build_const_int32(gallivm, 0), "");
1995
1996 if (dims >= 2) {
1997 size = LLVMBuildInsertElement(gallivm->builder, size,
1998 dynamic_state->height(dynamic_state, gallivm, texture_unit),
1999 lp_build_const_int32(gallivm, 1), "");
2000 }
2001
2002 if (dims >= 3) {
2003 size = LLVMBuildInsertElement(gallivm->builder, size,
2004 dynamic_state->depth(dynamic_state, gallivm, texture_unit),
2005 lp_build_const_int32(gallivm, 2), "");
2006 }
2007
2008 size = lp_build_minify(&bld_int_vec, size, lod);
2009
2010 if (has_array)
2011 size = LLVMBuildInsertElement(gallivm->builder, size,
2012 dynamic_state->depth(dynamic_state, gallivm, texture_unit),
2013 lp_build_const_int32(gallivm, dims), "");
2014
2015 /*
2016 * XXX for out-of-bounds lod, should set size to zero vector here
2017 * (for dx10-style only, i.e. need_nr_mips)
2018 */
2019
2020 for (i = 0; i < dims + (has_array ? 1 : 0); i++) {
2021 sizes_out[i] = lp_build_extract_broadcast(gallivm, bld_int_vec.type, int_type,
2022 size,
2023 lp_build_const_int32(gallivm, i));
2024 }
2025
2026 /*
2027 * if there's no explicit_lod (buffers, rects) queries requiring nr of
2028 * mips would be illegal.
2029 */
2030 if (need_nr_mips && explicit_lod) {
2031 struct lp_build_context bld_int_scalar;
2032 LLVMValueRef num_levels;
2033 lp_build_context_init(&bld_int_scalar, gallivm, lp_type_int(32));
2034
2035 if (static_state->level_zero_only) {
2036 num_levels = bld_int_scalar.one;
2037 }
2038 else {
2039 LLVMValueRef last_level;
2040
2041 last_level = dynamic_state->last_level(dynamic_state, gallivm, texture_unit);
2042 num_levels = lp_build_sub(&bld_int_scalar, last_level, first_level);
2043 num_levels = lp_build_add(&bld_int_scalar, num_levels, bld_int_scalar.one);
2044 }
2045 sizes_out[3] = lp_build_broadcast(gallivm, lp_build_vec_type(gallivm, int_type),
2046 num_levels);
2047 }
2048 }