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