mesa: remove unrequired else
[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 "util/u_format_rgb9e5.h"
46 #include "lp_bld_debug.h"
47 #include "lp_bld_type.h"
48 #include "lp_bld_const.h"
49 #include "lp_bld_conv.h"
50 #include "lp_bld_arit.h"
51 #include "lp_bld_bitarit.h"
52 #include "lp_bld_logic.h"
53 #include "lp_bld_printf.h"
54 #include "lp_bld_swizzle.h"
55 #include "lp_bld_flow.h"
56 #include "lp_bld_gather.h"
57 #include "lp_bld_format.h"
58 #include "lp_bld_sample.h"
59 #include "lp_bld_sample_aos.h"
60 #include "lp_bld_struct.h"
61 #include "lp_bld_quad.h"
62 #include "lp_bld_pack.h"
63
64
65 /**
66 * Generate code to fetch a texel from a texture at int coords (x, y, z).
67 * The computation depends on whether the texture is 1D, 2D or 3D.
68 * The result, texel, will be float vectors:
69 * texel[0] = red values
70 * texel[1] = green values
71 * texel[2] = blue values
72 * texel[3] = alpha values
73 */
74 static void
75 lp_build_sample_texel_soa(struct lp_build_sample_context *bld,
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 bld->cache,
165 texel_out);
166
167 /*
168 * Note: if we find an app which frequently samples the texture border
169 * we might want to implement a true conditional here to avoid sampling
170 * the texture whenever possible (since that's quite a bit of code).
171 * Ex:
172 * if (use_border) {
173 * texel = border_color;
174 * }
175 * else {
176 * texel = sample_texture(coord);
177 * }
178 * As it is now, we always sample the texture, then selectively replace
179 * the texel color results with the border color.
180 */
181
182 if (use_border) {
183 /* select texel color or border color depending on use_border. */
184 const struct util_format_description *format_desc = bld->format_desc;
185 int chan;
186 struct lp_type border_type = bld->texel_type;
187 border_type.length = 4;
188 /*
189 * Only replace channels which are actually present. The others should
190 * get optimized away eventually by sampler_view swizzle anyway but it's
191 * easier too.
192 */
193 for (chan = 0; chan < 4; chan++) {
194 unsigned chan_s;
195 /* reverse-map channel... */
196 for (chan_s = 0; chan_s < 4; chan_s++) {
197 if (chan_s == format_desc->swizzle[chan]) {
198 break;
199 }
200 }
201 if (chan_s <= 3) {
202 /* use the already clamped color */
203 LLVMValueRef idx = lp_build_const_int32(bld->gallivm, chan);
204 LLVMValueRef border_chan;
205
206 border_chan = lp_build_extract_broadcast(bld->gallivm,
207 border_type,
208 bld->texel_type,
209 bld->border_color_clamped,
210 idx);
211 texel_out[chan] = lp_build_select(&bld->texel_bld, use_border,
212 border_chan, texel_out[chan]);
213 }
214 }
215 }
216 }
217
218
219 /**
220 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
221 */
222 static LLVMValueRef
223 lp_build_coord_mirror(struct lp_build_sample_context *bld,
224 LLVMValueRef coord)
225 {
226 struct lp_build_context *coord_bld = &bld->coord_bld;
227 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
228 LLVMValueRef fract, flr, isOdd;
229
230 lp_build_ifloor_fract(coord_bld, coord, &flr, &fract);
231
232 /* isOdd = flr & 1 */
233 isOdd = LLVMBuildAnd(bld->gallivm->builder, flr, int_coord_bld->one, "");
234
235 /* make coord positive or negative depending on isOdd */
236 coord = lp_build_set_sign(coord_bld, fract, isOdd);
237
238 /* convert isOdd to float */
239 isOdd = lp_build_int_to_float(coord_bld, isOdd);
240
241 /* add isOdd to coord */
242 coord = lp_build_add(coord_bld, coord, isOdd);
243
244 return coord;
245 }
246
247
248 /**
249 * Helper to compute the first coord and the weight for
250 * linear wrap repeat npot textures
251 */
252 void
253 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context *bld,
254 LLVMValueRef coord_f,
255 LLVMValueRef length_i,
256 LLVMValueRef length_f,
257 LLVMValueRef *coord0_i,
258 LLVMValueRef *weight_f)
259 {
260 struct lp_build_context *coord_bld = &bld->coord_bld;
261 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
262 LLVMValueRef half = lp_build_const_vec(bld->gallivm, coord_bld->type, 0.5);
263 LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length_i,
264 int_coord_bld->one);
265 LLVMValueRef mask;
266 /* wrap with normalized floats is just fract */
267 coord_f = lp_build_fract(coord_bld, coord_f);
268 /* mul by size and subtract 0.5 */
269 coord_f = lp_build_mul(coord_bld, coord_f, length_f);
270 coord_f = lp_build_sub(coord_bld, coord_f, half);
271 /*
272 * we avoided the 0.5/length division before the repeat wrap,
273 * now need to fix up edge cases with selects
274 */
275 /* convert to int, compute lerp weight */
276 lp_build_ifloor_fract(coord_bld, coord_f, coord0_i, weight_f);
277 mask = lp_build_compare(int_coord_bld->gallivm, int_coord_bld->type,
278 PIPE_FUNC_LESS, *coord0_i, int_coord_bld->zero);
279 *coord0_i = lp_build_select(int_coord_bld, mask, length_minus_one, *coord0_i);
280 }
281
282
283 /**
284 * Build LLVM code for texture wrap mode for linear filtering.
285 * \param x0_out returns first integer texcoord
286 * \param x1_out returns second integer texcoord
287 * \param weight_out returns linear interpolation weight
288 */
289 static void
290 lp_build_sample_wrap_linear(struct lp_build_sample_context *bld,
291 LLVMValueRef coord,
292 LLVMValueRef length,
293 LLVMValueRef length_f,
294 LLVMValueRef offset,
295 boolean is_pot,
296 unsigned wrap_mode,
297 LLVMValueRef *x0_out,
298 LLVMValueRef *x1_out,
299 LLVMValueRef *weight_out)
300 {
301 struct lp_build_context *coord_bld = &bld->coord_bld;
302 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
303 LLVMBuilderRef builder = bld->gallivm->builder;
304 LLVMValueRef half = lp_build_const_vec(bld->gallivm, coord_bld->type, 0.5);
305 LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length, int_coord_bld->one);
306 LLVMValueRef coord0, coord1, weight;
307
308 switch(wrap_mode) {
309 case PIPE_TEX_WRAP_REPEAT:
310 if (is_pot) {
311 /* mul by size and subtract 0.5 */
312 coord = lp_build_mul(coord_bld, coord, length_f);
313 coord = lp_build_sub(coord_bld, coord, half);
314 if (offset) {
315 offset = lp_build_int_to_float(coord_bld, offset);
316 coord = lp_build_add(coord_bld, coord, offset);
317 }
318 /* convert to int, compute lerp weight */
319 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
320 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
321 /* repeat wrap */
322 coord0 = LLVMBuildAnd(builder, coord0, length_minus_one, "");
323 coord1 = LLVMBuildAnd(builder, coord1, length_minus_one, "");
324 }
325 else {
326 LLVMValueRef mask;
327 if (offset) {
328 offset = lp_build_int_to_float(coord_bld, offset);
329 offset = lp_build_div(coord_bld, offset, length_f);
330 coord = lp_build_add(coord_bld, coord, offset);
331 }
332 lp_build_coord_repeat_npot_linear(bld, coord,
333 length, length_f,
334 &coord0, &weight);
335 mask = lp_build_compare(int_coord_bld->gallivm, int_coord_bld->type,
336 PIPE_FUNC_NOTEQUAL, coord0, length_minus_one);
337 coord1 = LLVMBuildAnd(builder,
338 lp_build_add(int_coord_bld, coord0, int_coord_bld->one),
339 mask, "");
340 }
341 break;
342
343 case PIPE_TEX_WRAP_CLAMP:
344 if (bld->static_sampler_state->normalized_coords) {
345 /* scale coord to length */
346 coord = lp_build_mul(coord_bld, coord, length_f);
347 }
348 if (offset) {
349 offset = lp_build_int_to_float(coord_bld, offset);
350 coord = lp_build_add(coord_bld, coord, offset);
351 }
352
353 /* clamp to [0, length] */
354 coord = lp_build_clamp(coord_bld, coord, coord_bld->zero, length_f);
355
356 coord = lp_build_sub(coord_bld, coord, half);
357
358 /* convert to int, compute lerp weight */
359 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
360 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
361 break;
362
363 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
364 {
365 struct lp_build_context abs_coord_bld = bld->coord_bld;
366 abs_coord_bld.type.sign = FALSE;
367
368 if (bld->static_sampler_state->normalized_coords) {
369 /* mul by tex size */
370 coord = lp_build_mul(coord_bld, coord, length_f);
371 }
372 if (offset) {
373 offset = lp_build_int_to_float(coord_bld, offset);
374 coord = lp_build_add(coord_bld, coord, offset);
375 }
376
377 /* clamp to length max */
378 coord = lp_build_min(coord_bld, coord, length_f);
379 /* subtract 0.5 */
380 coord = lp_build_sub(coord_bld, coord, half);
381 /* clamp to [0, length - 0.5] */
382 coord = lp_build_max(coord_bld, coord, coord_bld->zero);
383 /* convert to int, compute lerp weight */
384 lp_build_ifloor_fract(&abs_coord_bld, coord, &coord0, &weight);
385 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
386 /* coord1 = min(coord1, length-1) */
387 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
388 break;
389 }
390
391 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
392 if (bld->static_sampler_state->normalized_coords) {
393 /* scale coord to length */
394 coord = lp_build_mul(coord_bld, coord, length_f);
395 }
396 if (offset) {
397 offset = lp_build_int_to_float(coord_bld, offset);
398 coord = lp_build_add(coord_bld, coord, offset);
399 }
400 /* was: clamp to [-0.5, length + 0.5], then sub 0.5 */
401 /* can skip clamp (though might not work for very large coord values */
402 coord = lp_build_sub(coord_bld, coord, half);
403 /* convert to int, compute lerp weight */
404 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
405 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
406 break;
407
408 case PIPE_TEX_WRAP_MIRROR_REPEAT:
409 if (offset) {
410 offset = lp_build_int_to_float(coord_bld, offset);
411 offset = lp_build_div(coord_bld, offset, length_f);
412 coord = lp_build_add(coord_bld, coord, offset);
413 }
414 /* compute mirror function */
415 coord = lp_build_coord_mirror(bld, coord);
416
417 /* scale coord to length */
418 coord = lp_build_mul(coord_bld, coord, length_f);
419 coord = lp_build_sub(coord_bld, coord, half);
420
421 /* convert to int, compute lerp weight */
422 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
423 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
424
425 /* coord0 = max(coord0, 0) */
426 coord0 = lp_build_max(int_coord_bld, coord0, int_coord_bld->zero);
427 /* coord1 = min(coord1, length-1) */
428 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
429 break;
430
431 case PIPE_TEX_WRAP_MIRROR_CLAMP:
432 if (bld->static_sampler_state->normalized_coords) {
433 /* scale coord to length */
434 coord = lp_build_mul(coord_bld, coord, length_f);
435 }
436 if (offset) {
437 offset = lp_build_int_to_float(coord_bld, offset);
438 coord = lp_build_add(coord_bld, coord, offset);
439 }
440 coord = lp_build_abs(coord_bld, coord);
441
442 /* clamp to [0, length] */
443 coord = lp_build_min(coord_bld, coord, length_f);
444
445 coord = lp_build_sub(coord_bld, coord, half);
446
447 /* convert to int, compute lerp weight */
448 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
449 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
450 break;
451
452 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
453 {
454 struct lp_build_context abs_coord_bld = bld->coord_bld;
455 abs_coord_bld.type.sign = FALSE;
456
457 if (bld->static_sampler_state->normalized_coords) {
458 /* scale coord to length */
459 coord = lp_build_mul(coord_bld, coord, length_f);
460 }
461 if (offset) {
462 offset = lp_build_int_to_float(coord_bld, offset);
463 coord = lp_build_add(coord_bld, coord, offset);
464 }
465 coord = lp_build_abs(coord_bld, coord);
466
467 /* clamp to length max */
468 coord = lp_build_min(coord_bld, coord, length_f);
469 /* subtract 0.5 */
470 coord = lp_build_sub(coord_bld, coord, half);
471 /* clamp to [0, length - 0.5] */
472 coord = lp_build_max(coord_bld, coord, coord_bld->zero);
473
474 /* convert to int, compute lerp weight */
475 lp_build_ifloor_fract(&abs_coord_bld, coord, &coord0, &weight);
476 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
477 /* coord1 = min(coord1, length-1) */
478 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
479 }
480 break;
481
482 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
483 {
484 if (bld->static_sampler_state->normalized_coords) {
485 /* scale coord to length */
486 coord = lp_build_mul(coord_bld, coord, length_f);
487 }
488 if (offset) {
489 offset = lp_build_int_to_float(coord_bld, offset);
490 coord = lp_build_add(coord_bld, coord, offset);
491 }
492 coord = lp_build_abs(coord_bld, coord);
493
494 /* was: clamp to [-0.5, length + 0.5] then sub 0.5 */
495 /* skip clamp - always positive, and other side
496 only potentially matters for very large coords */
497 coord = lp_build_sub(coord_bld, coord, half);
498
499 /* convert to int, compute lerp weight */
500 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
501 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
502 }
503 break;
504
505 default:
506 assert(0);
507 coord0 = NULL;
508 coord1 = NULL;
509 weight = NULL;
510 }
511
512 *x0_out = coord0;
513 *x1_out = coord1;
514 *weight_out = weight;
515 }
516
517
518 /**
519 * Build LLVM code for texture wrap mode for nearest filtering.
520 * \param coord the incoming texcoord (nominally in [0,1])
521 * \param length the texture size along one dimension, as int vector
522 * \param length_f the texture size along one dimension, as float vector
523 * \param offset texel offset along one dimension (as int vector)
524 * \param is_pot if TRUE, length is a power of two
525 * \param wrap_mode one of PIPE_TEX_WRAP_x
526 */
527 static LLVMValueRef
528 lp_build_sample_wrap_nearest(struct lp_build_sample_context *bld,
529 LLVMValueRef coord,
530 LLVMValueRef length,
531 LLVMValueRef length_f,
532 LLVMValueRef offset,
533 boolean is_pot,
534 unsigned wrap_mode)
535 {
536 struct lp_build_context *coord_bld = &bld->coord_bld;
537 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
538 LLVMBuilderRef builder = bld->gallivm->builder;
539 LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length, int_coord_bld->one);
540 LLVMValueRef icoord;
541
542 switch(wrap_mode) {
543 case PIPE_TEX_WRAP_REPEAT:
544 if (is_pot) {
545 coord = lp_build_mul(coord_bld, coord, length_f);
546 icoord = lp_build_ifloor(coord_bld, coord);
547 if (offset) {
548 icoord = lp_build_add(int_coord_bld, icoord, offset);
549 }
550 icoord = LLVMBuildAnd(builder, icoord, length_minus_one, "");
551 }
552 else {
553 if (offset) {
554 offset = lp_build_int_to_float(coord_bld, offset);
555 offset = lp_build_div(coord_bld, offset, length_f);
556 coord = lp_build_add(coord_bld, coord, offset);
557 }
558 /* take fraction, unnormalize */
559 coord = lp_build_fract_safe(coord_bld, coord);
560 coord = lp_build_mul(coord_bld, coord, length_f);
561 icoord = lp_build_itrunc(coord_bld, coord);
562 }
563 break;
564
565 case PIPE_TEX_WRAP_CLAMP:
566 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
567 if (bld->static_sampler_state->normalized_coords) {
568 /* scale coord to length */
569 coord = lp_build_mul(coord_bld, coord, length_f);
570 }
571
572 if (offset) {
573 offset = lp_build_int_to_float(coord_bld, offset);
574 coord = lp_build_add(coord_bld, coord, offset);
575 }
576 /* floor */
577 /* use itrunc instead since we clamp to 0 anyway */
578 icoord = lp_build_itrunc(coord_bld, coord);
579
580 /* clamp to [0, length - 1]. */
581 icoord = lp_build_clamp(int_coord_bld, icoord, int_coord_bld->zero,
582 length_minus_one);
583 break;
584
585 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
586 if (bld->static_sampler_state->normalized_coords) {
587 /* scale coord to length */
588 coord = lp_build_mul(coord_bld, coord, length_f);
589 }
590 /* no clamp necessary, border masking will handle this */
591 icoord = lp_build_ifloor(coord_bld, coord);
592 if (offset) {
593 icoord = lp_build_add(int_coord_bld, icoord, offset);
594 }
595 break;
596
597 case PIPE_TEX_WRAP_MIRROR_REPEAT:
598 if (offset) {
599 offset = lp_build_int_to_float(coord_bld, offset);
600 offset = lp_build_div(coord_bld, offset, length_f);
601 coord = lp_build_add(coord_bld, coord, offset);
602 }
603 /* compute mirror function */
604 coord = lp_build_coord_mirror(bld, coord);
605
606 /* scale coord to length */
607 assert(bld->static_sampler_state->normalized_coords);
608 coord = lp_build_mul(coord_bld, coord, length_f);
609
610 /* itrunc == ifloor here */
611 icoord = lp_build_itrunc(coord_bld, coord);
612
613 /* clamp to [0, length - 1] */
614 icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
615 break;
616
617 case PIPE_TEX_WRAP_MIRROR_CLAMP:
618 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
619 if (bld->static_sampler_state->normalized_coords) {
620 /* scale coord to length */
621 coord = lp_build_mul(coord_bld, coord, length_f);
622 }
623 if (offset) {
624 offset = lp_build_int_to_float(coord_bld, offset);
625 coord = lp_build_add(coord_bld, coord, offset);
626 }
627 coord = lp_build_abs(coord_bld, coord);
628
629 /* itrunc == ifloor here */
630 icoord = lp_build_itrunc(coord_bld, coord);
631
632 /* clamp to [0, length - 1] */
633 icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
634 break;
635
636 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
637 if (bld->static_sampler_state->normalized_coords) {
638 /* scale coord to length */
639 coord = lp_build_mul(coord_bld, coord, length_f);
640 }
641 if (offset) {
642 offset = lp_build_int_to_float(coord_bld, offset);
643 coord = lp_build_add(coord_bld, coord, offset);
644 }
645 coord = lp_build_abs(coord_bld, coord);
646
647 /* itrunc == ifloor here */
648 icoord = lp_build_itrunc(coord_bld, coord);
649 break;
650
651 default:
652 assert(0);
653 icoord = NULL;
654 }
655
656 return icoord;
657 }
658
659
660 /**
661 * Do shadow test/comparison.
662 * \param p shadow ref value
663 * \param texel the texel to compare against
664 */
665 static LLVMValueRef
666 lp_build_sample_comparefunc(struct lp_build_sample_context *bld,
667 LLVMValueRef p,
668 LLVMValueRef texel)
669 {
670 struct lp_build_context *texel_bld = &bld->texel_bld;
671 LLVMValueRef res;
672
673 if (0) {
674 //lp_build_print_value(bld->gallivm, "shadow cmp coord", p);
675 lp_build_print_value(bld->gallivm, "shadow cmp texel", texel);
676 }
677
678 /* result = (p FUNC texel) ? 1 : 0 */
679 /*
680 * honor d3d10 floating point rules here, which state that comparisons
681 * are ordered except NOT_EQUAL which is unordered.
682 */
683 if (bld->static_sampler_state->compare_func != PIPE_FUNC_NOTEQUAL) {
684 res = lp_build_cmp_ordered(texel_bld, bld->static_sampler_state->compare_func,
685 p, texel);
686 }
687 else {
688 res = lp_build_cmp(texel_bld, bld->static_sampler_state->compare_func,
689 p, texel);
690 }
691 return res;
692 }
693
694
695 /**
696 * Generate code to sample a mipmap level with nearest filtering.
697 * If sampling a cube texture, r = cube face in [0,5].
698 */
699 static void
700 lp_build_sample_image_nearest(struct lp_build_sample_context *bld,
701 LLVMValueRef size,
702 LLVMValueRef row_stride_vec,
703 LLVMValueRef img_stride_vec,
704 LLVMValueRef data_ptr,
705 LLVMValueRef mipoffsets,
706 LLVMValueRef *coords,
707 const LLVMValueRef *offsets,
708 LLVMValueRef colors_out[4])
709 {
710 const unsigned dims = bld->dims;
711 LLVMValueRef width_vec;
712 LLVMValueRef height_vec;
713 LLVMValueRef depth_vec;
714 LLVMValueRef flt_size;
715 LLVMValueRef flt_width_vec;
716 LLVMValueRef flt_height_vec;
717 LLVMValueRef flt_depth_vec;
718 LLVMValueRef x, y = NULL, z = NULL;
719
720 lp_build_extract_image_sizes(bld,
721 &bld->int_size_bld,
722 bld->int_coord_type,
723 size,
724 &width_vec, &height_vec, &depth_vec);
725
726 flt_size = lp_build_int_to_float(&bld->float_size_bld, size);
727
728 lp_build_extract_image_sizes(bld,
729 &bld->float_size_bld,
730 bld->coord_type,
731 flt_size,
732 &flt_width_vec, &flt_height_vec, &flt_depth_vec);
733
734 /*
735 * Compute integer texcoords.
736 */
737 x = lp_build_sample_wrap_nearest(bld, coords[0], width_vec,
738 flt_width_vec, offsets[0],
739 bld->static_texture_state->pot_width,
740 bld->static_sampler_state->wrap_s);
741 lp_build_name(x, "tex.x.wrapped");
742
743 if (dims >= 2) {
744 y = lp_build_sample_wrap_nearest(bld, coords[1], height_vec,
745 flt_height_vec, offsets[1],
746 bld->static_texture_state->pot_height,
747 bld->static_sampler_state->wrap_t);
748 lp_build_name(y, "tex.y.wrapped");
749
750 if (dims == 3) {
751 z = lp_build_sample_wrap_nearest(bld, coords[2], depth_vec,
752 flt_depth_vec, offsets[2],
753 bld->static_texture_state->pot_depth,
754 bld->static_sampler_state->wrap_r);
755 lp_build_name(z, "tex.z.wrapped");
756 }
757 }
758 if (has_layer_coord(bld->static_texture_state->target)) {
759 if (bld->static_texture_state->target == PIPE_TEXTURE_CUBE_ARRAY) {
760 /* add cube layer to face */
761 z = lp_build_add(&bld->int_coord_bld, coords[2], coords[3]);
762 }
763 else {
764 z = coords[2];
765 }
766 lp_build_name(z, "tex.z.layer");
767 }
768
769 /*
770 * Get texture colors.
771 */
772 lp_build_sample_texel_soa(bld,
773 width_vec, height_vec, depth_vec,
774 x, y, z,
775 row_stride_vec, img_stride_vec,
776 data_ptr, mipoffsets, colors_out);
777
778 if (bld->static_sampler_state->compare_mode != PIPE_TEX_COMPARE_NONE) {
779 LLVMValueRef cmpval;
780 cmpval = lp_build_sample_comparefunc(bld, coords[4], colors_out[0]);
781 /* this is really just a AND 1.0, cmpval but llvm is clever enough */
782 colors_out[0] = lp_build_select(&bld->texel_bld, cmpval,
783 bld->texel_bld.one, bld->texel_bld.zero);
784 colors_out[1] = colors_out[2] = colors_out[3] = colors_out[0];
785 }
786
787 }
788
789
790 /**
791 * Like a lerp, but inputs are 0/~0 masks, so can simplify slightly.
792 */
793 static LLVMValueRef
794 lp_build_masklerp(struct lp_build_context *bld,
795 LLVMValueRef weight,
796 LLVMValueRef mask0,
797 LLVMValueRef mask1)
798 {
799 struct gallivm_state *gallivm = bld->gallivm;
800 LLVMBuilderRef builder = gallivm->builder;
801 LLVMValueRef weight2;
802
803 weight2 = lp_build_sub(bld, bld->one, weight);
804 weight = LLVMBuildBitCast(builder, weight,
805 lp_build_int_vec_type(gallivm, bld->type), "");
806 weight2 = LLVMBuildBitCast(builder, weight2,
807 lp_build_int_vec_type(gallivm, bld->type), "");
808 weight = LLVMBuildAnd(builder, weight, mask1, "");
809 weight2 = LLVMBuildAnd(builder, weight2, mask0, "");
810 weight = LLVMBuildBitCast(builder, weight, bld->vec_type, "");
811 weight2 = LLVMBuildBitCast(builder, weight2, bld->vec_type, "");
812 return lp_build_add(bld, weight, weight2);
813 }
814
815 /**
816 * Like a 2d lerp, but inputs are 0/~0 masks, so can simplify slightly.
817 */
818 static LLVMValueRef
819 lp_build_masklerp2d(struct lp_build_context *bld,
820 LLVMValueRef weight0,
821 LLVMValueRef weight1,
822 LLVMValueRef mask00,
823 LLVMValueRef mask01,
824 LLVMValueRef mask10,
825 LLVMValueRef mask11)
826 {
827 LLVMValueRef val0 = lp_build_masklerp(bld, weight0, mask00, mask01);
828 LLVMValueRef val1 = lp_build_masklerp(bld, weight0, mask10, mask11);
829 return lp_build_lerp(bld, weight1, val0, val1, 0);
830 }
831
832 /*
833 * this is a bit excessive code for something OpenGL just recommends
834 * but does not require.
835 */
836 #define ACCURATE_CUBE_CORNERS 1
837
838 /**
839 * Generate code to sample a mipmap level with linear filtering.
840 * If sampling a cube texture, r = cube face in [0,5].
841 * If linear_mask is present, only pixels having their mask set
842 * will receive linear filtering, the rest will use nearest.
843 */
844 static void
845 lp_build_sample_image_linear(struct lp_build_sample_context *bld,
846 boolean is_gather,
847 LLVMValueRef size,
848 LLVMValueRef linear_mask,
849 LLVMValueRef row_stride_vec,
850 LLVMValueRef img_stride_vec,
851 LLVMValueRef data_ptr,
852 LLVMValueRef mipoffsets,
853 LLVMValueRef *coords,
854 const LLVMValueRef *offsets,
855 LLVMValueRef colors_out[4])
856 {
857 LLVMBuilderRef builder = bld->gallivm->builder;
858 struct lp_build_context *ivec_bld = &bld->int_coord_bld;
859 struct lp_build_context *coord_bld = &bld->coord_bld;
860 struct lp_build_context *texel_bld = &bld->texel_bld;
861 const unsigned dims = bld->dims;
862 LLVMValueRef width_vec;
863 LLVMValueRef height_vec;
864 LLVMValueRef depth_vec;
865 LLVMValueRef flt_size;
866 LLVMValueRef flt_width_vec;
867 LLVMValueRef flt_height_vec;
868 LLVMValueRef flt_depth_vec;
869 LLVMValueRef fall_off[4], have_corners;
870 LLVMValueRef z1 = NULL;
871 LLVMValueRef z00 = NULL, z01 = NULL, z10 = NULL, z11 = NULL;
872 LLVMValueRef x00 = NULL, x01 = NULL, x10 = NULL, x11 = NULL;
873 LLVMValueRef y00 = NULL, y01 = NULL, y10 = NULL, y11 = NULL;
874 LLVMValueRef s_fpart, t_fpart = NULL, r_fpart = NULL;
875 LLVMValueRef xs[4], ys[4], zs[4];
876 LLVMValueRef neighbors[2][2][4];
877 int chan, texel_index;
878 boolean seamless_cube_filter, accurate_cube_corners;
879
880 seamless_cube_filter = (bld->static_texture_state->target == PIPE_TEXTURE_CUBE ||
881 bld->static_texture_state->target == PIPE_TEXTURE_CUBE_ARRAY) &&
882 bld->static_sampler_state->seamless_cube_map;
883 /*
884 * XXX I don't know how this is really supposed to work with gather. From GL
885 * spec wording (not gather specific) it sounds like the 4th missing texel
886 * should be an average of the other 3, hence for gather could return this.
887 * This is however NOT how the code here works, which just fixes up the
888 * weights used for filtering instead. And of course for gather there is
889 * no filter to tweak...
890 */
891 accurate_cube_corners = ACCURATE_CUBE_CORNERS && seamless_cube_filter &&
892 !is_gather;
893
894 lp_build_extract_image_sizes(bld,
895 &bld->int_size_bld,
896 bld->int_coord_type,
897 size,
898 &width_vec, &height_vec, &depth_vec);
899
900 flt_size = lp_build_int_to_float(&bld->float_size_bld, size);
901
902 lp_build_extract_image_sizes(bld,
903 &bld->float_size_bld,
904 bld->coord_type,
905 flt_size,
906 &flt_width_vec, &flt_height_vec, &flt_depth_vec);
907
908 /*
909 * Compute integer texcoords.
910 */
911
912 if (!seamless_cube_filter) {
913 lp_build_sample_wrap_linear(bld, coords[0], width_vec,
914 flt_width_vec, offsets[0],
915 bld->static_texture_state->pot_width,
916 bld->static_sampler_state->wrap_s,
917 &x00, &x01, &s_fpart);
918 lp_build_name(x00, "tex.x0.wrapped");
919 lp_build_name(x01, "tex.x1.wrapped");
920 x10 = x00;
921 x11 = x01;
922
923 if (dims >= 2) {
924 lp_build_sample_wrap_linear(bld, coords[1], height_vec,
925 flt_height_vec, offsets[1],
926 bld->static_texture_state->pot_height,
927 bld->static_sampler_state->wrap_t,
928 &y00, &y10, &t_fpart);
929 lp_build_name(y00, "tex.y0.wrapped");
930 lp_build_name(y10, "tex.y1.wrapped");
931 y01 = y00;
932 y11 = y10;
933
934 if (dims == 3) {
935 lp_build_sample_wrap_linear(bld, coords[2], depth_vec,
936 flt_depth_vec, offsets[2],
937 bld->static_texture_state->pot_depth,
938 bld->static_sampler_state->wrap_r,
939 &z00, &z1, &r_fpart);
940 z01 = z10 = z11 = z00;
941 lp_build_name(z00, "tex.z0.wrapped");
942 lp_build_name(z1, "tex.z1.wrapped");
943 }
944 }
945 if (has_layer_coord(bld->static_texture_state->target)) {
946 if (bld->static_texture_state->target == PIPE_TEXTURE_CUBE_ARRAY) {
947 /* add cube layer to face */
948 z00 = z01 = z10 = z11 = z1 =
949 lp_build_add(&bld->int_coord_bld, coords[2], coords[3]);
950 }
951 else {
952 z00 = z01 = z10 = z11 = z1 = coords[2]; /* cube face or layer */
953 }
954 lp_build_name(z00, "tex.z0.layer");
955 lp_build_name(z1, "tex.z1.layer");
956 }
957 }
958 else {
959 struct lp_build_if_state edge_if;
960 LLVMTypeRef int1t;
961 LLVMValueRef new_faces[4], new_xcoords[4][2], new_ycoords[4][2];
962 LLVMValueRef coord, have_edge, have_corner;
963 LLVMValueRef fall_off_ym_notxm, fall_off_ym_notxp, fall_off_x, fall_off_y;
964 LLVMValueRef fall_off_yp_notxm, fall_off_yp_notxp;
965 LLVMValueRef x0, x1, y0, y1, y0_clamped, y1_clamped;
966 LLVMValueRef face = coords[2];
967 LLVMValueRef half = lp_build_const_vec(bld->gallivm, coord_bld->type, 0.5f);
968 LLVMValueRef length_minus_one = lp_build_sub(ivec_bld, width_vec, ivec_bld->one);
969 /* XXX drop height calcs. Could (should) do this without seamless filtering too */
970 height_vec = width_vec;
971 flt_height_vec = flt_width_vec;
972
973 /* XXX the overflow logic is actually sort of duplicated with trilinear,
974 * since an overflow in one mip should also have a corresponding overflow
975 * in another.
976 */
977 /* should always have normalized coords, and offsets are undefined */
978 assert(bld->static_sampler_state->normalized_coords);
979 coord = lp_build_mul(coord_bld, coords[0], flt_width_vec);
980 /* instead of clamp, build mask if overflowed */
981 coord = lp_build_sub(coord_bld, coord, half);
982 /* convert to int, compute lerp weight */
983 /* not ideal with AVX (and no AVX2) */
984 lp_build_ifloor_fract(coord_bld, coord, &x0, &s_fpart);
985 x1 = lp_build_add(ivec_bld, x0, ivec_bld->one);
986 coord = lp_build_mul(coord_bld, coords[1], flt_height_vec);
987 coord = lp_build_sub(coord_bld, coord, half);
988 lp_build_ifloor_fract(coord_bld, coord, &y0, &t_fpart);
989 y1 = lp_build_add(ivec_bld, y0, ivec_bld->one);
990
991 fall_off[0] = lp_build_cmp(ivec_bld, PIPE_FUNC_LESS, x0, ivec_bld->zero);
992 fall_off[1] = lp_build_cmp(ivec_bld, PIPE_FUNC_GREATER, x1, length_minus_one);
993 fall_off[2] = lp_build_cmp(ivec_bld, PIPE_FUNC_LESS, y0, ivec_bld->zero);
994 fall_off[3] = lp_build_cmp(ivec_bld, PIPE_FUNC_GREATER, y1, length_minus_one);
995
996 fall_off_x = lp_build_or(ivec_bld, fall_off[0], fall_off[1]);
997 fall_off_y = lp_build_or(ivec_bld, fall_off[2], fall_off[3]);
998 have_edge = lp_build_or(ivec_bld, fall_off_x, fall_off_y);
999 have_edge = lp_build_any_true_range(ivec_bld, ivec_bld->type.length, have_edge);
1000
1001 /* needed for accurate corner filtering branch later, rely on 0 init */
1002 int1t = LLVMInt1TypeInContext(bld->gallivm->context);
1003 have_corners = lp_build_alloca(bld->gallivm, int1t, "have_corner");
1004
1005 for (texel_index = 0; texel_index < 4; texel_index++) {
1006 xs[texel_index] = lp_build_alloca(bld->gallivm, ivec_bld->vec_type, "xs");
1007 ys[texel_index] = lp_build_alloca(bld->gallivm, ivec_bld->vec_type, "ys");
1008 zs[texel_index] = lp_build_alloca(bld->gallivm, ivec_bld->vec_type, "zs");
1009 }
1010
1011 lp_build_if(&edge_if, bld->gallivm, have_edge);
1012
1013 have_corner = lp_build_and(ivec_bld, fall_off_x, fall_off_y);
1014 have_corner = lp_build_any_true_range(ivec_bld, ivec_bld->type.length, have_corner);
1015 LLVMBuildStore(builder, have_corner, have_corners);
1016
1017 /*
1018 * Need to feed clamped values here for cheap corner handling,
1019 * but only for y coord (as when falling off both edges we only
1020 * fall off the x one) - this should be sufficient.
1021 */
1022 y0_clamped = lp_build_max(ivec_bld, y0, ivec_bld->zero);
1023 y1_clamped = lp_build_min(ivec_bld, y1, length_minus_one);
1024
1025 /*
1026 * Get all possible new coords.
1027 */
1028 lp_build_cube_new_coords(ivec_bld, face,
1029 x0, x1, y0_clamped, y1_clamped,
1030 length_minus_one,
1031 new_faces, new_xcoords, new_ycoords);
1032
1033 /* handle fall off x-, x+ direction */
1034 /* determine new coords, face (not both fall_off vars can be true at same time) */
1035 x00 = lp_build_select(ivec_bld, fall_off[0], new_xcoords[0][0], x0);
1036 y00 = lp_build_select(ivec_bld, fall_off[0], new_ycoords[0][0], y0_clamped);
1037 x10 = lp_build_select(ivec_bld, fall_off[0], new_xcoords[0][1], x0);
1038 y10 = lp_build_select(ivec_bld, fall_off[0], new_ycoords[0][1], y1_clamped);
1039 x01 = lp_build_select(ivec_bld, fall_off[1], new_xcoords[1][0], x1);
1040 y01 = lp_build_select(ivec_bld, fall_off[1], new_ycoords[1][0], y0_clamped);
1041 x11 = lp_build_select(ivec_bld, fall_off[1], new_xcoords[1][1], x1);
1042 y11 = lp_build_select(ivec_bld, fall_off[1], new_ycoords[1][1], y1_clamped);
1043
1044 z00 = z10 = lp_build_select(ivec_bld, fall_off[0], new_faces[0], face);
1045 z01 = z11 = lp_build_select(ivec_bld, fall_off[1], new_faces[1], face);
1046
1047 /* handle fall off y-, y+ direction */
1048 /*
1049 * Cheap corner logic: just hack up things so a texel doesn't fall
1050 * off both sides (which means filter weights will be wrong but we'll only
1051 * use valid texels in the filter).
1052 * This means however (y) coords must additionally be clamped (see above).
1053 * This corner handling should be fully OpenGL (but not d3d10) compliant.
1054 */
1055 fall_off_ym_notxm = lp_build_andnot(ivec_bld, fall_off[2], fall_off[0]);
1056 fall_off_ym_notxp = lp_build_andnot(ivec_bld, fall_off[2], fall_off[1]);
1057 fall_off_yp_notxm = lp_build_andnot(ivec_bld, fall_off[3], fall_off[0]);
1058 fall_off_yp_notxp = lp_build_andnot(ivec_bld, fall_off[3], fall_off[1]);
1059
1060 x00 = lp_build_select(ivec_bld, fall_off_ym_notxm, new_xcoords[2][0], x00);
1061 y00 = lp_build_select(ivec_bld, fall_off_ym_notxm, new_ycoords[2][0], y00);
1062 x01 = lp_build_select(ivec_bld, fall_off_ym_notxp, new_xcoords[2][1], x01);
1063 y01 = lp_build_select(ivec_bld, fall_off_ym_notxp, new_ycoords[2][1], y01);
1064 x10 = lp_build_select(ivec_bld, fall_off_yp_notxm, new_xcoords[3][0], x10);
1065 y10 = lp_build_select(ivec_bld, fall_off_yp_notxm, new_ycoords[3][0], y10);
1066 x11 = lp_build_select(ivec_bld, fall_off_yp_notxp, new_xcoords[3][1], x11);
1067 y11 = lp_build_select(ivec_bld, fall_off_yp_notxp, new_ycoords[3][1], y11);
1068
1069 z00 = lp_build_select(ivec_bld, fall_off_ym_notxm, new_faces[2], z00);
1070 z01 = lp_build_select(ivec_bld, fall_off_ym_notxp, new_faces[2], z01);
1071 z10 = lp_build_select(ivec_bld, fall_off_yp_notxm, new_faces[3], z10);
1072 z11 = lp_build_select(ivec_bld, fall_off_yp_notxp, new_faces[3], z11);
1073
1074 if (bld->static_texture_state->target == PIPE_TEXTURE_CUBE_ARRAY) {
1075 /* now can add cube layer to face (per sample) */
1076 z00 = lp_build_add(ivec_bld, z00, coords[3]);
1077 z01 = lp_build_add(ivec_bld, z01, coords[3]);
1078 z10 = lp_build_add(ivec_bld, z10, coords[3]);
1079 z11 = lp_build_add(ivec_bld, z11, coords[3]);
1080 }
1081
1082 LLVMBuildStore(builder, x00, xs[0]);
1083 LLVMBuildStore(builder, x01, xs[1]);
1084 LLVMBuildStore(builder, x10, xs[2]);
1085 LLVMBuildStore(builder, x11, xs[3]);
1086 LLVMBuildStore(builder, y00, ys[0]);
1087 LLVMBuildStore(builder, y01, ys[1]);
1088 LLVMBuildStore(builder, y10, ys[2]);
1089 LLVMBuildStore(builder, y11, ys[3]);
1090 LLVMBuildStore(builder, z00, zs[0]);
1091 LLVMBuildStore(builder, z01, zs[1]);
1092 LLVMBuildStore(builder, z10, zs[2]);
1093 LLVMBuildStore(builder, z11, zs[3]);
1094
1095 lp_build_else(&edge_if);
1096
1097 LLVMBuildStore(builder, x0, xs[0]);
1098 LLVMBuildStore(builder, x1, xs[1]);
1099 LLVMBuildStore(builder, x0, xs[2]);
1100 LLVMBuildStore(builder, x1, xs[3]);
1101 LLVMBuildStore(builder, y0, ys[0]);
1102 LLVMBuildStore(builder, y0, ys[1]);
1103 LLVMBuildStore(builder, y1, ys[2]);
1104 LLVMBuildStore(builder, y1, ys[3]);
1105 if (bld->static_texture_state->target == PIPE_TEXTURE_CUBE_ARRAY) {
1106 LLVMValueRef cube_layer = lp_build_add(ivec_bld, face, coords[3]);
1107 LLVMBuildStore(builder, cube_layer, zs[0]);
1108 LLVMBuildStore(builder, cube_layer, zs[1]);
1109 LLVMBuildStore(builder, cube_layer, zs[2]);
1110 LLVMBuildStore(builder, cube_layer, zs[3]);
1111 }
1112 else {
1113 LLVMBuildStore(builder, face, zs[0]);
1114 LLVMBuildStore(builder, face, zs[1]);
1115 LLVMBuildStore(builder, face, zs[2]);
1116 LLVMBuildStore(builder, face, zs[3]);
1117 }
1118
1119 lp_build_endif(&edge_if);
1120
1121 x00 = LLVMBuildLoad(builder, xs[0], "");
1122 x01 = LLVMBuildLoad(builder, xs[1], "");
1123 x10 = LLVMBuildLoad(builder, xs[2], "");
1124 x11 = LLVMBuildLoad(builder, xs[3], "");
1125 y00 = LLVMBuildLoad(builder, ys[0], "");
1126 y01 = LLVMBuildLoad(builder, ys[1], "");
1127 y10 = LLVMBuildLoad(builder, ys[2], "");
1128 y11 = LLVMBuildLoad(builder, ys[3], "");
1129 z00 = LLVMBuildLoad(builder, zs[0], "");
1130 z01 = LLVMBuildLoad(builder, zs[1], "");
1131 z10 = LLVMBuildLoad(builder, zs[2], "");
1132 z11 = LLVMBuildLoad(builder, zs[3], "");
1133 }
1134
1135 if (linear_mask) {
1136 /*
1137 * Whack filter weights into place. Whatever texel had more weight is
1138 * the one which should have been selected by nearest filtering hence
1139 * just use 100% weight for it.
1140 */
1141 struct lp_build_context *c_bld = &bld->coord_bld;
1142 LLVMValueRef w1_mask, w1_weight;
1143 LLVMValueRef half = lp_build_const_vec(bld->gallivm, c_bld->type, 0.5f);
1144
1145 w1_mask = lp_build_cmp(c_bld, PIPE_FUNC_GREATER, s_fpart, half);
1146 /* this select is really just a "and" */
1147 w1_weight = lp_build_select(c_bld, w1_mask, c_bld->one, c_bld->zero);
1148 s_fpart = lp_build_select(c_bld, linear_mask, s_fpart, w1_weight);
1149 if (dims >= 2) {
1150 w1_mask = lp_build_cmp(c_bld, PIPE_FUNC_GREATER, t_fpart, half);
1151 w1_weight = lp_build_select(c_bld, w1_mask, c_bld->one, c_bld->zero);
1152 t_fpart = lp_build_select(c_bld, linear_mask, t_fpart, w1_weight);
1153 if (dims == 3) {
1154 w1_mask = lp_build_cmp(c_bld, PIPE_FUNC_GREATER, r_fpart, half);
1155 w1_weight = lp_build_select(c_bld, w1_mask, c_bld->one, c_bld->zero);
1156 r_fpart = lp_build_select(c_bld, linear_mask, r_fpart, w1_weight);
1157 }
1158 }
1159 }
1160
1161 /*
1162 * Get texture colors.
1163 */
1164 /* get x0/x1 texels */
1165 lp_build_sample_texel_soa(bld,
1166 width_vec, height_vec, depth_vec,
1167 x00, y00, z00,
1168 row_stride_vec, img_stride_vec,
1169 data_ptr, mipoffsets, neighbors[0][0]);
1170 lp_build_sample_texel_soa(bld,
1171 width_vec, height_vec, depth_vec,
1172 x01, y01, z01,
1173 row_stride_vec, img_stride_vec,
1174 data_ptr, mipoffsets, neighbors[0][1]);
1175
1176 if (dims == 1) {
1177 assert(!is_gather);
1178 if (bld->static_sampler_state->compare_mode == PIPE_TEX_COMPARE_NONE) {
1179 /* Interpolate two samples from 1D image to produce one color */
1180 for (chan = 0; chan < 4; chan++) {
1181 colors_out[chan] = lp_build_lerp(texel_bld, s_fpart,
1182 neighbors[0][0][chan],
1183 neighbors[0][1][chan],
1184 0);
1185 }
1186 }
1187 else {
1188 LLVMValueRef cmpval0, cmpval1;
1189 cmpval0 = lp_build_sample_comparefunc(bld, coords[4], neighbors[0][0][0]);
1190 cmpval1 = lp_build_sample_comparefunc(bld, coords[4], neighbors[0][1][0]);
1191 /* simplified lerp, AND mask with weight and add */
1192 colors_out[0] = lp_build_masklerp(texel_bld, s_fpart,
1193 cmpval0, cmpval1);
1194 colors_out[1] = colors_out[2] = colors_out[3] = colors_out[0];
1195 }
1196 }
1197 else {
1198 /* 2D/3D texture */
1199 struct lp_build_if_state corner_if;
1200 LLVMValueRef colors0[4], colorss[4];
1201
1202 /* get x0/x1 texels at y1 */
1203 lp_build_sample_texel_soa(bld,
1204 width_vec, height_vec, depth_vec,
1205 x10, y10, z10,
1206 row_stride_vec, img_stride_vec,
1207 data_ptr, mipoffsets, neighbors[1][0]);
1208 lp_build_sample_texel_soa(bld,
1209 width_vec, height_vec, depth_vec,
1210 x11, y11, z11,
1211 row_stride_vec, img_stride_vec,
1212 data_ptr, mipoffsets, neighbors[1][1]);
1213
1214 /*
1215 * To avoid having to duplicate linear_mask / fetch code use
1216 * another branch (with corner condition though edge would work
1217 * as well) here.
1218 */
1219 if (accurate_cube_corners) {
1220 LLVMValueRef w00, w01, w10, w11, wx0, wy0;
1221 LLVMValueRef c_weight, c00, c01, c10, c11;
1222 LLVMValueRef have_corner, one_third, tmp;
1223
1224 colorss[0] = lp_build_alloca(bld->gallivm, coord_bld->vec_type, "cs");
1225 colorss[1] = lp_build_alloca(bld->gallivm, coord_bld->vec_type, "cs");
1226 colorss[2] = lp_build_alloca(bld->gallivm, coord_bld->vec_type, "cs");
1227 colorss[3] = lp_build_alloca(bld->gallivm, coord_bld->vec_type, "cs");
1228
1229 have_corner = LLVMBuildLoad(builder, have_corners, "");
1230
1231 lp_build_if(&corner_if, bld->gallivm, have_corner);
1232
1233 /*
1234 * we can't use standard 2d lerp as we need per-element weight
1235 * in case of corners, so just calculate bilinear result as
1236 * w00*s00 + w01*s01 + w10*s10 + w11*s11.
1237 * (This is actually less work than using 2d lerp, 7 vs. 9 instructions,
1238 * however calculating the weights needs another 6, so actually probably
1239 * not slower than 2d lerp only for 4 channels as weights only need
1240 * to be calculated once - of course fixing the weights has additional cost.)
1241 */
1242 wx0 = lp_build_sub(coord_bld, coord_bld->one, s_fpart);
1243 wy0 = lp_build_sub(coord_bld, coord_bld->one, t_fpart);
1244 w00 = lp_build_mul(coord_bld, wx0, wy0);
1245 w01 = lp_build_mul(coord_bld, s_fpart, wy0);
1246 w10 = lp_build_mul(coord_bld, wx0, t_fpart);
1247 w11 = lp_build_mul(coord_bld, s_fpart, t_fpart);
1248
1249 /* find corner weight */
1250 c00 = lp_build_and(ivec_bld, fall_off[0], fall_off[2]);
1251 c_weight = lp_build_select(coord_bld, c00, w00, coord_bld->zero);
1252 c01 = lp_build_and(ivec_bld, fall_off[1], fall_off[2]);
1253 c_weight = lp_build_select(coord_bld, c01, w01, c_weight);
1254 c10 = lp_build_and(ivec_bld, fall_off[0], fall_off[3]);
1255 c_weight = lp_build_select(coord_bld, c10, w10, c_weight);
1256 c11 = lp_build_and(ivec_bld, fall_off[1], fall_off[3]);
1257 c_weight = lp_build_select(coord_bld, c11, w11, c_weight);
1258
1259 /*
1260 * add 1/3 of the corner weight to each of the 3 other samples
1261 * and null out corner weight
1262 */
1263 one_third = lp_build_const_vec(bld->gallivm, coord_bld->type, 1.0f/3.0f);
1264 c_weight = lp_build_mul(coord_bld, c_weight, one_third);
1265 w00 = lp_build_add(coord_bld, w00, c_weight);
1266 c00 = LLVMBuildBitCast(builder, c00, coord_bld->vec_type, "");
1267 w00 = lp_build_andnot(coord_bld, w00, c00);
1268 w01 = lp_build_add(coord_bld, w01, c_weight);
1269 c01 = LLVMBuildBitCast(builder, c01, coord_bld->vec_type, "");
1270 w01 = lp_build_andnot(coord_bld, w01, c01);
1271 w10 = lp_build_add(coord_bld, w10, c_weight);
1272 c10 = LLVMBuildBitCast(builder, c10, coord_bld->vec_type, "");
1273 w10 = lp_build_andnot(coord_bld, w10, c10);
1274 w11 = lp_build_add(coord_bld, w11, c_weight);
1275 c11 = LLVMBuildBitCast(builder, c11, coord_bld->vec_type, "");
1276 w11 = lp_build_andnot(coord_bld, w11, c11);
1277
1278 if (bld->static_sampler_state->compare_mode == PIPE_TEX_COMPARE_NONE) {
1279 for (chan = 0; chan < 4; chan++) {
1280 colors0[chan] = lp_build_mul(coord_bld, w00, neighbors[0][0][chan]);
1281 tmp = lp_build_mul(coord_bld, w01, neighbors[0][1][chan]);
1282 colors0[chan] = lp_build_add(coord_bld, tmp, colors0[chan]);
1283 tmp = lp_build_mul(coord_bld, w10, neighbors[1][0][chan]);
1284 colors0[chan] = lp_build_add(coord_bld, tmp, colors0[chan]);
1285 tmp = lp_build_mul(coord_bld, w11, neighbors[1][1][chan]);
1286 colors0[chan] = lp_build_add(coord_bld, tmp, colors0[chan]);
1287 }
1288 }
1289 else {
1290 LLVMValueRef cmpval00, cmpval01, cmpval10, cmpval11;
1291 cmpval00 = lp_build_sample_comparefunc(bld, coords[4], neighbors[0][0][0]);
1292 cmpval01 = lp_build_sample_comparefunc(bld, coords[4], neighbors[0][1][0]);
1293 cmpval10 = lp_build_sample_comparefunc(bld, coords[4], neighbors[1][0][0]);
1294 cmpval11 = lp_build_sample_comparefunc(bld, coords[4], neighbors[1][1][0]);
1295 /* inputs to interpolation are just masks so just add masked weights together */
1296 cmpval00 = LLVMBuildBitCast(builder, cmpval00, coord_bld->vec_type, "");
1297 cmpval01 = LLVMBuildBitCast(builder, cmpval01, coord_bld->vec_type, "");
1298 cmpval10 = LLVMBuildBitCast(builder, cmpval10, coord_bld->vec_type, "");
1299 cmpval11 = LLVMBuildBitCast(builder, cmpval11, coord_bld->vec_type, "");
1300 colors0[0] = lp_build_and(coord_bld, w00, cmpval00);
1301 tmp = lp_build_and(coord_bld, w01, cmpval01);
1302 colors0[0] = lp_build_add(coord_bld, tmp, colors0[0]);
1303 tmp = lp_build_and(coord_bld, w10, cmpval10);
1304 colors0[0] = lp_build_add(coord_bld, tmp, colors0[0]);
1305 tmp = lp_build_and(coord_bld, w11, cmpval11);
1306 colors0[0] = lp_build_add(coord_bld, tmp, colors0[0]);
1307 colors0[1] = colors0[2] = colors0[3] = colors0[0];
1308 }
1309
1310 LLVMBuildStore(builder, colors0[0], colorss[0]);
1311 LLVMBuildStore(builder, colors0[1], colorss[1]);
1312 LLVMBuildStore(builder, colors0[2], colorss[2]);
1313 LLVMBuildStore(builder, colors0[3], colorss[3]);
1314
1315 lp_build_else(&corner_if);
1316 }
1317
1318 if (bld->static_sampler_state->compare_mode == PIPE_TEX_COMPARE_NONE) {
1319 if (is_gather) {
1320 /*
1321 * Just assign the red channel (no component selection yet).
1322 * This is a bit hackish, we usually do the swizzle at the
1323 * end of sampling (much less values to swizzle), but this
1324 * obviously cannot work when using gather.
1325 */
1326 unsigned chan_swiz = bld->static_texture_state->swizzle_r;
1327 colors0[0] = lp_build_swizzle_soa_channel(texel_bld,
1328 neighbors[1][0],
1329 chan_swiz);
1330 colors0[1] = lp_build_swizzle_soa_channel(texel_bld,
1331 neighbors[1][1],
1332 chan_swiz);
1333 colors0[2] = lp_build_swizzle_soa_channel(texel_bld,
1334 neighbors[0][1],
1335 chan_swiz);
1336 colors0[3] = lp_build_swizzle_soa_channel(texel_bld,
1337 neighbors[0][0],
1338 chan_swiz);
1339 }
1340 else {
1341 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
1342 for (chan = 0; chan < 4; chan++) {
1343 colors0[chan] = lp_build_lerp_2d(texel_bld,
1344 s_fpart, t_fpart,
1345 neighbors[0][0][chan],
1346 neighbors[0][1][chan],
1347 neighbors[1][0][chan],
1348 neighbors[1][1][chan],
1349 0);
1350 }
1351 }
1352 }
1353 else {
1354 LLVMValueRef cmpval00, cmpval01, cmpval10, cmpval11;
1355 cmpval00 = lp_build_sample_comparefunc(bld, coords[4], neighbors[0][0][0]);
1356 cmpval01 = lp_build_sample_comparefunc(bld, coords[4], neighbors[0][1][0]);
1357 cmpval10 = lp_build_sample_comparefunc(bld, coords[4], neighbors[1][0][0]);
1358 cmpval11 = lp_build_sample_comparefunc(bld, coords[4], neighbors[1][1][0]);
1359
1360 if (is_gather) {
1361 /* more hacks for swizzling, should be X, ONE or ZERO... */
1362 unsigned chan_swiz = bld->static_texture_state->swizzle_r;
1363 if (chan_swiz <= PIPE_SWIZZLE_ALPHA) {
1364 colors0[0] = lp_build_select(texel_bld, cmpval10,
1365 texel_bld->one, texel_bld->zero);
1366 colors0[1] = lp_build_select(texel_bld, cmpval11,
1367 texel_bld->one, texel_bld->zero);
1368 colors0[2] = lp_build_select(texel_bld, cmpval01,
1369 texel_bld->one, texel_bld->zero);
1370 colors0[3] = lp_build_select(texel_bld, cmpval00,
1371 texel_bld->one, texel_bld->zero);
1372 }
1373 else if (chan_swiz == PIPE_SWIZZLE_ZERO) {
1374 colors0[0] = colors0[1] = colors0[2] = colors0[3] =
1375 texel_bld->zero;
1376 }
1377 else {
1378 colors0[0] = colors0[1] = colors0[2] = colors0[3] =
1379 texel_bld->one;
1380 }
1381 }
1382 else {
1383 colors0[0] = lp_build_masklerp2d(texel_bld, s_fpart, t_fpart,
1384 cmpval00, cmpval01, cmpval10, cmpval11);
1385 colors0[1] = colors0[2] = colors0[3] = colors0[0];
1386 }
1387 }
1388
1389 if (accurate_cube_corners) {
1390 LLVMBuildStore(builder, colors0[0], colorss[0]);
1391 LLVMBuildStore(builder, colors0[1], colorss[1]);
1392 LLVMBuildStore(builder, colors0[2], colorss[2]);
1393 LLVMBuildStore(builder, colors0[3], colorss[3]);
1394
1395 lp_build_endif(&corner_if);
1396
1397 colors0[0] = LLVMBuildLoad(builder, colorss[0], "");
1398 colors0[1] = LLVMBuildLoad(builder, colorss[1], "");
1399 colors0[2] = LLVMBuildLoad(builder, colorss[2], "");
1400 colors0[3] = LLVMBuildLoad(builder, colorss[3], "");
1401 }
1402
1403 if (dims == 3) {
1404 LLVMValueRef neighbors1[2][2][4];
1405 LLVMValueRef colors1[4];
1406
1407 assert(!is_gather);
1408
1409 /* get x0/x1/y0/y1 texels at z1 */
1410 lp_build_sample_texel_soa(bld,
1411 width_vec, height_vec, depth_vec,
1412 x00, y00, z1,
1413 row_stride_vec, img_stride_vec,
1414 data_ptr, mipoffsets, neighbors1[0][0]);
1415 lp_build_sample_texel_soa(bld,
1416 width_vec, height_vec, depth_vec,
1417 x01, y01, z1,
1418 row_stride_vec, img_stride_vec,
1419 data_ptr, mipoffsets, neighbors1[0][1]);
1420 lp_build_sample_texel_soa(bld,
1421 width_vec, height_vec, depth_vec,
1422 x10, y10, z1,
1423 row_stride_vec, img_stride_vec,
1424 data_ptr, mipoffsets, neighbors1[1][0]);
1425 lp_build_sample_texel_soa(bld,
1426 width_vec, height_vec, depth_vec,
1427 x11, y11, z1,
1428 row_stride_vec, img_stride_vec,
1429 data_ptr, mipoffsets, neighbors1[1][1]);
1430
1431 if (bld->static_sampler_state->compare_mode == PIPE_TEX_COMPARE_NONE) {
1432 /* Bilinear interpolate the four samples from the second Z slice */
1433 for (chan = 0; chan < 4; chan++) {
1434 colors1[chan] = lp_build_lerp_2d(texel_bld,
1435 s_fpart, t_fpart,
1436 neighbors1[0][0][chan],
1437 neighbors1[0][1][chan],
1438 neighbors1[1][0][chan],
1439 neighbors1[1][1][chan],
1440 0);
1441 }
1442 /* Linearly interpolate the two samples from the two 3D slices */
1443 for (chan = 0; chan < 4; chan++) {
1444 colors_out[chan] = lp_build_lerp(texel_bld,
1445 r_fpart,
1446 colors0[chan], colors1[chan],
1447 0);
1448 }
1449 }
1450 else {
1451 LLVMValueRef cmpval00, cmpval01, cmpval10, cmpval11;
1452 cmpval00 = lp_build_sample_comparefunc(bld, coords[4], neighbors[0][0][0]);
1453 cmpval01 = lp_build_sample_comparefunc(bld, coords[4], neighbors[0][1][0]);
1454 cmpval10 = lp_build_sample_comparefunc(bld, coords[4], neighbors[1][0][0]);
1455 cmpval11 = lp_build_sample_comparefunc(bld, coords[4], neighbors[1][1][0]);
1456 colors1[0] = lp_build_masklerp2d(texel_bld, s_fpart, t_fpart,
1457 cmpval00, cmpval01, cmpval10, cmpval11);
1458 /* Linearly interpolate the two samples from the two 3D slices */
1459 colors_out[0] = lp_build_lerp(texel_bld,
1460 r_fpart,
1461 colors0[0], colors1[0],
1462 0);
1463 colors_out[1] = colors_out[2] = colors_out[3] = colors_out[0];
1464 }
1465 }
1466 else {
1467 /* 2D tex */
1468 for (chan = 0; chan < 4; chan++) {
1469 colors_out[chan] = colors0[chan];
1470 }
1471 }
1472 }
1473 }
1474
1475
1476 /**
1477 * Sample the texture/mipmap using given image filter and mip filter.
1478 * ilevel0 and ilevel1 indicate the two mipmap levels to sample
1479 * from (vectors or scalars).
1480 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1481 */
1482 static void
1483 lp_build_sample_mipmap(struct lp_build_sample_context *bld,
1484 unsigned img_filter,
1485 unsigned mip_filter,
1486 boolean is_gather,
1487 LLVMValueRef *coords,
1488 const LLVMValueRef *offsets,
1489 LLVMValueRef ilevel0,
1490 LLVMValueRef ilevel1,
1491 LLVMValueRef lod_fpart,
1492 LLVMValueRef *colors_out)
1493 {
1494 LLVMBuilderRef builder = bld->gallivm->builder;
1495 LLVMValueRef size0 = NULL;
1496 LLVMValueRef size1 = NULL;
1497 LLVMValueRef row_stride0_vec = NULL;
1498 LLVMValueRef row_stride1_vec = NULL;
1499 LLVMValueRef img_stride0_vec = NULL;
1500 LLVMValueRef img_stride1_vec = NULL;
1501 LLVMValueRef data_ptr0 = NULL;
1502 LLVMValueRef data_ptr1 = NULL;
1503 LLVMValueRef mipoff0 = NULL;
1504 LLVMValueRef mipoff1 = NULL;
1505 LLVMValueRef colors0[4], colors1[4];
1506 unsigned chan;
1507
1508 /* sample the first mipmap level */
1509 lp_build_mipmap_level_sizes(bld, ilevel0,
1510 &size0,
1511 &row_stride0_vec, &img_stride0_vec);
1512 if (bld->num_mips == 1) {
1513 data_ptr0 = lp_build_get_mipmap_level(bld, ilevel0);
1514 }
1515 else {
1516 /* This path should work for num_lods 1 too but slightly less efficient */
1517 data_ptr0 = bld->base_ptr;
1518 mipoff0 = lp_build_get_mip_offsets(bld, ilevel0);
1519 }
1520 if (img_filter == PIPE_TEX_FILTER_NEAREST) {
1521 lp_build_sample_image_nearest(bld, size0,
1522 row_stride0_vec, img_stride0_vec,
1523 data_ptr0, mipoff0, coords, offsets,
1524 colors0);
1525 }
1526 else {
1527 assert(img_filter == PIPE_TEX_FILTER_LINEAR);
1528 lp_build_sample_image_linear(bld, is_gather, size0, NULL,
1529 row_stride0_vec, img_stride0_vec,
1530 data_ptr0, mipoff0, coords, offsets,
1531 colors0);
1532 }
1533
1534 /* Store the first level's colors in the output variables */
1535 for (chan = 0; chan < 4; chan++) {
1536 LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
1537 }
1538
1539 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1540 struct lp_build_if_state if_ctx;
1541 LLVMValueRef need_lerp;
1542
1543 /* need_lerp = lod_fpart > 0 */
1544 if (bld->num_lods == 1) {
1545 need_lerp = LLVMBuildFCmp(builder, LLVMRealUGT,
1546 lod_fpart, bld->lodf_bld.zero,
1547 "need_lerp");
1548 }
1549 else {
1550 /*
1551 * We'll do mip filtering if any of the quads (or individual
1552 * pixel in case of per-pixel lod) need it.
1553 * It might be better to split the vectors here and only fetch/filter
1554 * quads which need it (if there's one lod per quad).
1555 */
1556 need_lerp = lp_build_compare(bld->gallivm, bld->lodf_bld.type,
1557 PIPE_FUNC_GREATER,
1558 lod_fpart, bld->lodf_bld.zero);
1559 need_lerp = lp_build_any_true_range(&bld->lodi_bld, bld->num_lods, need_lerp);
1560 }
1561
1562 lp_build_if(&if_ctx, bld->gallivm, need_lerp);
1563 {
1564 /*
1565 * We unfortunately need to clamp lod_fpart here since we can get
1566 * negative values which would screw up filtering if not all
1567 * lod_fpart values have same sign.
1568 */
1569 lod_fpart = lp_build_max(&bld->lodf_bld, lod_fpart,
1570 bld->lodf_bld.zero);
1571 /* sample the second mipmap level */
1572 lp_build_mipmap_level_sizes(bld, ilevel1,
1573 &size1,
1574 &row_stride1_vec, &img_stride1_vec);
1575 if (bld->num_mips == 1) {
1576 data_ptr1 = lp_build_get_mipmap_level(bld, ilevel1);
1577 }
1578 else {
1579 data_ptr1 = bld->base_ptr;
1580 mipoff1 = lp_build_get_mip_offsets(bld, ilevel1);
1581 }
1582 if (img_filter == PIPE_TEX_FILTER_NEAREST) {
1583 lp_build_sample_image_nearest(bld, size1,
1584 row_stride1_vec, img_stride1_vec,
1585 data_ptr1, mipoff1, coords, offsets,
1586 colors1);
1587 }
1588 else {
1589 lp_build_sample_image_linear(bld, FALSE, size1, NULL,
1590 row_stride1_vec, img_stride1_vec,
1591 data_ptr1, mipoff1, coords, offsets,
1592 colors1);
1593 }
1594
1595 /* interpolate samples from the two mipmap levels */
1596
1597 if (bld->num_lods != bld->coord_type.length)
1598 lod_fpart = lp_build_unpack_broadcast_aos_scalars(bld->gallivm,
1599 bld->lodf_bld.type,
1600 bld->texel_bld.type,
1601 lod_fpart);
1602
1603 for (chan = 0; chan < 4; chan++) {
1604 colors0[chan] = lp_build_lerp(&bld->texel_bld, lod_fpart,
1605 colors0[chan], colors1[chan],
1606 0);
1607 LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
1608 }
1609 }
1610 lp_build_endif(&if_ctx);
1611 }
1612 }
1613
1614
1615 /**
1616 * Sample the texture/mipmap using given mip filter, and using
1617 * both nearest and linear filtering at the same time depending
1618 * on linear_mask.
1619 * lod can be per quad but linear_mask is always per pixel.
1620 * ilevel0 and ilevel1 indicate the two mipmap levels to sample
1621 * from (vectors or scalars).
1622 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1623 */
1624 static void
1625 lp_build_sample_mipmap_both(struct lp_build_sample_context *bld,
1626 LLVMValueRef linear_mask,
1627 unsigned mip_filter,
1628 LLVMValueRef *coords,
1629 const LLVMValueRef *offsets,
1630 LLVMValueRef ilevel0,
1631 LLVMValueRef ilevel1,
1632 LLVMValueRef lod_fpart,
1633 LLVMValueRef lod_positive,
1634 LLVMValueRef *colors_out)
1635 {
1636 LLVMBuilderRef builder = bld->gallivm->builder;
1637 LLVMValueRef size0 = NULL;
1638 LLVMValueRef size1 = NULL;
1639 LLVMValueRef row_stride0_vec = NULL;
1640 LLVMValueRef row_stride1_vec = NULL;
1641 LLVMValueRef img_stride0_vec = NULL;
1642 LLVMValueRef img_stride1_vec = NULL;
1643 LLVMValueRef data_ptr0 = NULL;
1644 LLVMValueRef data_ptr1 = NULL;
1645 LLVMValueRef mipoff0 = NULL;
1646 LLVMValueRef mipoff1 = NULL;
1647 LLVMValueRef colors0[4], colors1[4];
1648 unsigned chan;
1649
1650 /* sample the first mipmap level */
1651 lp_build_mipmap_level_sizes(bld, ilevel0,
1652 &size0,
1653 &row_stride0_vec, &img_stride0_vec);
1654 if (bld->num_mips == 1) {
1655 data_ptr0 = lp_build_get_mipmap_level(bld, ilevel0);
1656 }
1657 else {
1658 /* This path should work for num_lods 1 too but slightly less efficient */
1659 data_ptr0 = bld->base_ptr;
1660 mipoff0 = lp_build_get_mip_offsets(bld, ilevel0);
1661 }
1662
1663 lp_build_sample_image_linear(bld, FALSE, size0, linear_mask,
1664 row_stride0_vec, img_stride0_vec,
1665 data_ptr0, mipoff0, coords, offsets,
1666 colors0);
1667
1668 /* Store the first level's colors in the output variables */
1669 for (chan = 0; chan < 4; chan++) {
1670 LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
1671 }
1672
1673 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1674 struct lp_build_if_state if_ctx;
1675 LLVMValueRef need_lerp;
1676
1677 /*
1678 * We'll do mip filtering if any of the quads (or individual
1679 * pixel in case of per-pixel lod) need it.
1680 * Note using lod_positive here not lod_fpart since it may be the same
1681 * condition as that used in the outer "if" in the caller hence llvm
1682 * should be able to merge the branches in this case.
1683 */
1684 need_lerp = lp_build_any_true_range(&bld->lodi_bld, bld->num_lods, lod_positive);
1685
1686 lp_build_if(&if_ctx, bld->gallivm, need_lerp);
1687 {
1688 /*
1689 * We unfortunately need to clamp lod_fpart here since we can get
1690 * negative values which would screw up filtering if not all
1691 * lod_fpart values have same sign.
1692 */
1693 lod_fpart = lp_build_max(&bld->lodf_bld, lod_fpart,
1694 bld->lodf_bld.zero);
1695 /* sample the second mipmap level */
1696 lp_build_mipmap_level_sizes(bld, ilevel1,
1697 &size1,
1698 &row_stride1_vec, &img_stride1_vec);
1699 if (bld->num_mips == 1) {
1700 data_ptr1 = lp_build_get_mipmap_level(bld, ilevel1);
1701 }
1702 else {
1703 data_ptr1 = bld->base_ptr;
1704 mipoff1 = lp_build_get_mip_offsets(bld, ilevel1);
1705 }
1706
1707 lp_build_sample_image_linear(bld, FALSE, size1, linear_mask,
1708 row_stride1_vec, img_stride1_vec,
1709 data_ptr1, mipoff1, coords, offsets,
1710 colors1);
1711
1712 /* interpolate samples from the two mipmap levels */
1713
1714 if (bld->num_lods != bld->coord_type.length)
1715 lod_fpart = lp_build_unpack_broadcast_aos_scalars(bld->gallivm,
1716 bld->lodf_bld.type,
1717 bld->texel_bld.type,
1718 lod_fpart);
1719
1720 for (chan = 0; chan < 4; chan++) {
1721 colors0[chan] = lp_build_lerp(&bld->texel_bld, lod_fpart,
1722 colors0[chan], colors1[chan],
1723 0);
1724 LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
1725 }
1726 }
1727 lp_build_endif(&if_ctx);
1728 }
1729 }
1730
1731
1732 /**
1733 * Build (per-coord) layer value.
1734 * Either clamp layer to valid values or fill in optional out_of_bounds
1735 * value and just return value unclamped.
1736 */
1737 static LLVMValueRef
1738 lp_build_layer_coord(struct lp_build_sample_context *bld,
1739 unsigned texture_unit,
1740 boolean is_cube_array,
1741 LLVMValueRef layer,
1742 LLVMValueRef *out_of_bounds)
1743 {
1744 LLVMValueRef num_layers;
1745 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
1746
1747 num_layers = bld->dynamic_state->depth(bld->dynamic_state, bld->gallivm,
1748 bld->context_ptr, texture_unit);
1749
1750 if (out_of_bounds) {
1751 LLVMValueRef out1, out;
1752 assert(!is_cube_array);
1753 num_layers = lp_build_broadcast_scalar(int_coord_bld, num_layers);
1754 out = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, layer, int_coord_bld->zero);
1755 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, layer, num_layers);
1756 *out_of_bounds = lp_build_or(int_coord_bld, out, out1);
1757 return layer;
1758 }
1759 else {
1760 LLVMValueRef maxlayer;
1761 LLVMValueRef s = is_cube_array ? lp_build_const_int32(bld->gallivm, 6) :
1762 bld->int_bld.one;
1763 maxlayer = lp_build_sub(&bld->int_bld, num_layers, s);
1764 maxlayer = lp_build_broadcast_scalar(int_coord_bld, maxlayer);
1765 return lp_build_clamp(int_coord_bld, layer, int_coord_bld->zero, maxlayer);
1766 }
1767 }
1768
1769
1770 /**
1771 * Calculate cube face, lod, mip levels.
1772 */
1773 static void
1774 lp_build_sample_common(struct lp_build_sample_context *bld,
1775 unsigned texture_index,
1776 unsigned sampler_index,
1777 LLVMValueRef *coords,
1778 const struct lp_derivatives *derivs, /* optional */
1779 LLVMValueRef lod_bias, /* optional */
1780 LLVMValueRef explicit_lod, /* optional */
1781 LLVMValueRef *lod_pos_or_zero,
1782 LLVMValueRef *lod_fpart,
1783 LLVMValueRef *ilevel0,
1784 LLVMValueRef *ilevel1)
1785 {
1786 const unsigned mip_filter = bld->static_sampler_state->min_mip_filter;
1787 const unsigned min_filter = bld->static_sampler_state->min_img_filter;
1788 const unsigned mag_filter = bld->static_sampler_state->mag_img_filter;
1789 const unsigned target = bld->static_texture_state->target;
1790 LLVMValueRef first_level, cube_rho = NULL;
1791 LLVMValueRef lod_ipart = NULL;
1792 struct lp_derivatives cube_derivs;
1793
1794 /*
1795 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1796 mip_filter, min_filter, mag_filter);
1797 */
1798
1799 /*
1800 * Choose cube face, recompute texcoords for the chosen face and
1801 * compute rho here too (as it requires transform of derivatives).
1802 */
1803 if (target == PIPE_TEXTURE_CUBE || target == PIPE_TEXTURE_CUBE_ARRAY) {
1804 boolean need_derivs;
1805 need_derivs = ((min_filter != mag_filter ||
1806 mip_filter != PIPE_TEX_MIPFILTER_NONE) &&
1807 !bld->static_sampler_state->min_max_lod_equal &&
1808 !explicit_lod);
1809 lp_build_cube_lookup(bld, coords, derivs, &cube_rho, &cube_derivs, need_derivs);
1810 derivs = &cube_derivs;
1811 if (target == PIPE_TEXTURE_CUBE_ARRAY) {
1812 /* calculate cube layer coord now */
1813 LLVMValueRef layer = lp_build_iround(&bld->coord_bld, coords[3]);
1814 LLVMValueRef six = lp_build_const_int_vec(bld->gallivm, bld->int_coord_type, 6);
1815 layer = lp_build_mul(&bld->int_coord_bld, layer, six);
1816 coords[3] = lp_build_layer_coord(bld, texture_index, TRUE, layer, NULL);
1817 /* because of seamless filtering can't add it to face (coords[2]) here. */
1818 }
1819 }
1820 else if (target == PIPE_TEXTURE_1D_ARRAY ||
1821 target == PIPE_TEXTURE_2D_ARRAY) {
1822 coords[2] = lp_build_iround(&bld->coord_bld, coords[2]);
1823 coords[2] = lp_build_layer_coord(bld, texture_index, FALSE, coords[2], NULL);
1824 }
1825
1826 if (bld->static_sampler_state->compare_mode != PIPE_TEX_COMPARE_NONE) {
1827 /*
1828 * Clamp p coords to [0,1] for fixed function depth texture format here.
1829 * Technically this is not entirely correct for unorm depth as the ref value
1830 * should be converted to the depth format (quantization!) and comparison
1831 * then done in texture format. This would actually help performance (since
1832 * only need to do it once and could save the per-sample conversion of texels
1833 * to floats instead), but it would need more messy code (would need to push
1834 * at least some bits down to actual fetch so conversion could be skipped,
1835 * and would have ugly interaction with border color, would need to convert
1836 * border color to that format too or do some other tricks to make it work).
1837 */
1838 const struct util_format_description *format_desc = bld->format_desc;
1839 unsigned chan_type;
1840 /* not entirely sure we couldn't end up with non-valid swizzle here */
1841 chan_type = format_desc->swizzle[0] <= UTIL_FORMAT_SWIZZLE_W ?
1842 format_desc->channel[format_desc->swizzle[0]].type :
1843 UTIL_FORMAT_TYPE_FLOAT;
1844 if (chan_type != UTIL_FORMAT_TYPE_FLOAT) {
1845 coords[4] = lp_build_clamp(&bld->coord_bld, coords[4],
1846 bld->coord_bld.zero, bld->coord_bld.one);
1847 }
1848 }
1849
1850 /*
1851 * Compute the level of detail (float).
1852 */
1853 if (min_filter != mag_filter ||
1854 mip_filter != PIPE_TEX_MIPFILTER_NONE) {
1855 /* Need to compute lod either to choose mipmap levels or to
1856 * distinguish between minification/magnification with one mipmap level.
1857 */
1858 lp_build_lod_selector(bld, texture_index, sampler_index,
1859 coords[0], coords[1], coords[2], cube_rho,
1860 derivs, lod_bias, explicit_lod,
1861 mip_filter,
1862 &lod_ipart, lod_fpart, lod_pos_or_zero);
1863 } else {
1864 lod_ipart = bld->lodi_bld.zero;
1865 *lod_pos_or_zero = bld->lodi_bld.zero;
1866 }
1867
1868 if (bld->num_lods != bld->num_mips) {
1869 /* only makes sense if there's just a single mip level */
1870 assert(bld->num_mips == 1);
1871 lod_ipart = lp_build_extract_range(bld->gallivm, lod_ipart, 0, 1);
1872 }
1873
1874 /*
1875 * Compute integer mipmap level(s) to fetch texels from: ilevel0, ilevel1
1876 */
1877 switch (mip_filter) {
1878 default:
1879 assert(0 && "bad mip_filter value in lp_build_sample_soa()");
1880 /* fall-through */
1881 case PIPE_TEX_MIPFILTER_NONE:
1882 /* always use mip level 0 */
1883 first_level = bld->dynamic_state->first_level(bld->dynamic_state,
1884 bld->gallivm, bld->context_ptr,
1885 texture_index);
1886 first_level = lp_build_broadcast_scalar(&bld->leveli_bld, first_level);
1887 *ilevel0 = first_level;
1888 break;
1889 case PIPE_TEX_MIPFILTER_NEAREST:
1890 assert(lod_ipart);
1891 lp_build_nearest_mip_level(bld, texture_index, lod_ipart, ilevel0, NULL);
1892 break;
1893 case PIPE_TEX_MIPFILTER_LINEAR:
1894 assert(lod_ipart);
1895 assert(*lod_fpart);
1896 lp_build_linear_mip_levels(bld, texture_index,
1897 lod_ipart, lod_fpart,
1898 ilevel0, ilevel1);
1899 break;
1900 }
1901 }
1902
1903 static void
1904 lp_build_clamp_border_color(struct lp_build_sample_context *bld,
1905 unsigned sampler_unit)
1906 {
1907 struct gallivm_state *gallivm = bld->gallivm;
1908 LLVMBuilderRef builder = gallivm->builder;
1909 LLVMValueRef border_color_ptr =
1910 bld->dynamic_state->border_color(bld->dynamic_state, gallivm,
1911 bld->context_ptr, sampler_unit);
1912 LLVMValueRef border_color;
1913 const struct util_format_description *format_desc = bld->format_desc;
1914 struct lp_type vec4_type = bld->texel_type;
1915 struct lp_build_context vec4_bld;
1916 LLVMValueRef min_clamp = NULL;
1917 LLVMValueRef max_clamp = NULL;
1918
1919 /*
1920 * For normalized format need to clamp border color (technically
1921 * probably should also quantize the data). Really sucks doing this
1922 * here but can't avoid at least for now since this is part of
1923 * sampler state and texture format is part of sampler_view state.
1924 * GL expects also expects clamping for uint/sint formats too so
1925 * do that as well (d3d10 can't end up here with uint/sint since it
1926 * only supports them with ld).
1927 */
1928 vec4_type.length = 4;
1929 lp_build_context_init(&vec4_bld, gallivm, vec4_type);
1930
1931 /*
1932 * Vectorized clamping of border color. Loading is a bit of a hack since
1933 * we just cast the pointer to float array to pointer to vec4
1934 * (int or float).
1935 */
1936 border_color_ptr = lp_build_array_get_ptr(gallivm, border_color_ptr,
1937 lp_build_const_int32(gallivm, 0));
1938 border_color_ptr = LLVMBuildBitCast(builder, border_color_ptr,
1939 LLVMPointerType(vec4_bld.vec_type, 0), "");
1940 border_color = LLVMBuildLoad(builder, border_color_ptr, "");
1941 /* we don't have aligned type in the dynamic state unfortunately */
1942 lp_set_load_alignment(border_color, 4);
1943
1944 /*
1945 * Instead of having some incredibly complex logic which will try to figure out
1946 * clamping necessary for each channel, simply use the first channel, and treat
1947 * mixed signed/unsigned normalized formats specially.
1948 * (Mixed non-normalized, which wouldn't work at all here, do not exist for a
1949 * good reason.)
1950 */
1951 if (format_desc->layout == UTIL_FORMAT_LAYOUT_PLAIN) {
1952 int chan;
1953 /* d/s needs special handling because both present means just sampling depth */
1954 if (util_format_is_depth_and_stencil(format_desc->format)) {
1955 chan = format_desc->swizzle[0];
1956 }
1957 else {
1958 chan = util_format_get_first_non_void_channel(format_desc->format);
1959 }
1960 if (chan >= 0 && chan <= UTIL_FORMAT_SWIZZLE_W) {
1961 unsigned chan_type = format_desc->channel[chan].type;
1962 unsigned chan_norm = format_desc->channel[chan].normalized;
1963 unsigned chan_pure = format_desc->channel[chan].pure_integer;
1964 if (chan_type == UTIL_FORMAT_TYPE_SIGNED) {
1965 if (chan_norm) {
1966 min_clamp = lp_build_const_vec(gallivm, vec4_type, -1.0F);
1967 max_clamp = vec4_bld.one;
1968 }
1969 else if (chan_pure) {
1970 /*
1971 * Border color was stored as int, hence need min/max clamp
1972 * only if chan has less than 32 bits..
1973 */
1974 unsigned chan_size = format_desc->channel[chan].size;
1975 if (chan_size < 32) {
1976 min_clamp = lp_build_const_int_vec(gallivm, vec4_type,
1977 0 - (1 << (chan_size - 1)));
1978 max_clamp = lp_build_const_int_vec(gallivm, vec4_type,
1979 (1 << (chan_size - 1)) - 1);
1980 }
1981 }
1982 /* TODO: no idea about non-pure, non-normalized! */
1983 }
1984 else if (chan_type == UTIL_FORMAT_TYPE_UNSIGNED) {
1985 if (chan_norm) {
1986 min_clamp = vec4_bld.zero;
1987 max_clamp = vec4_bld.one;
1988 }
1989 /*
1990 * Need a ugly hack here, because we don't have Z32_FLOAT_X8X24
1991 * we use Z32_FLOAT_S8X24 to imply sampling depth component
1992 * and ignoring stencil, which will blow up here if we try to
1993 * do a uint clamp in a float texel build...
1994 * And even if we had that format, mesa st also thinks using z24s8
1995 * means depth sampling ignoring stencil.
1996 */
1997 else if (chan_pure) {
1998 /*
1999 * Border color was stored as uint, hence never need min
2000 * clamp, and only need max clamp if chan has less than 32 bits.
2001 */
2002 unsigned chan_size = format_desc->channel[chan].size;
2003 if (chan_size < 32) {
2004 max_clamp = lp_build_const_int_vec(gallivm, vec4_type,
2005 (1 << chan_size) - 1);
2006 }
2007 /* TODO: no idea about non-pure, non-normalized! */
2008 }
2009 }
2010 else if (chan_type == UTIL_FORMAT_TYPE_FIXED) {
2011 /* TODO: I have no idea what clamp this would need if any! */
2012 }
2013 }
2014 /* mixed plain formats (or different pure size) */
2015 switch (format_desc->format) {
2016 case PIPE_FORMAT_B10G10R10A2_UINT:
2017 case PIPE_FORMAT_R10G10B10A2_UINT:
2018 {
2019 unsigned max10 = (1 << 10) - 1;
2020 max_clamp = lp_build_const_aos(gallivm, vec4_type, max10, max10,
2021 max10, (1 << 2) - 1, NULL);
2022 }
2023 break;
2024 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
2025 min_clamp = lp_build_const_aos(gallivm, vec4_type, -1.0F, -1.0F,
2026 -1.0F, 0.0F, NULL);
2027 max_clamp = vec4_bld.one;
2028 break;
2029 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
2030 case PIPE_FORMAT_R5SG5SB6U_NORM:
2031 min_clamp = lp_build_const_aos(gallivm, vec4_type, -1.0F, -1.0F,
2032 0.0F, 0.0F, NULL);
2033 max_clamp = vec4_bld.one;
2034 break;
2035 default:
2036 break;
2037 }
2038 }
2039 else {
2040 /* cannot figure this out from format description */
2041 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
2042 /* s3tc formats are always unorm */
2043 min_clamp = vec4_bld.zero;
2044 max_clamp = vec4_bld.one;
2045 }
2046 else if (format_desc->layout == UTIL_FORMAT_LAYOUT_RGTC ||
2047 format_desc->layout == UTIL_FORMAT_LAYOUT_ETC) {
2048 switch (format_desc->format) {
2049 case PIPE_FORMAT_RGTC1_UNORM:
2050 case PIPE_FORMAT_RGTC2_UNORM:
2051 case PIPE_FORMAT_LATC1_UNORM:
2052 case PIPE_FORMAT_LATC2_UNORM:
2053 case PIPE_FORMAT_ETC1_RGB8:
2054 min_clamp = vec4_bld.zero;
2055 max_clamp = vec4_bld.one;
2056 break;
2057 case PIPE_FORMAT_RGTC1_SNORM:
2058 case PIPE_FORMAT_RGTC2_SNORM:
2059 case PIPE_FORMAT_LATC1_SNORM:
2060 case PIPE_FORMAT_LATC2_SNORM:
2061 min_clamp = lp_build_const_vec(gallivm, vec4_type, -1.0F);
2062 max_clamp = vec4_bld.one;
2063 break;
2064 default:
2065 assert(0);
2066 break;
2067 }
2068 }
2069 /*
2070 * all others from subsampled/other group, though we don't care
2071 * about yuv (and should not have any from zs here)
2072 */
2073 else if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_YUV){
2074 switch (format_desc->format) {
2075 case PIPE_FORMAT_R8G8_B8G8_UNORM:
2076 case PIPE_FORMAT_G8R8_G8B8_UNORM:
2077 case PIPE_FORMAT_G8R8_B8R8_UNORM:
2078 case PIPE_FORMAT_R8G8_R8B8_UNORM:
2079 case PIPE_FORMAT_R1_UNORM: /* doesn't make sense but ah well */
2080 min_clamp = vec4_bld.zero;
2081 max_clamp = vec4_bld.one;
2082 break;
2083 case PIPE_FORMAT_R8G8Bx_SNORM:
2084 min_clamp = lp_build_const_vec(gallivm, vec4_type, -1.0F);
2085 max_clamp = vec4_bld.one;
2086 break;
2087 /*
2088 * Note smallfloat formats usually don't need clamping
2089 * (they still have infinite range) however this is not
2090 * true for r11g11b10 and r9g9b9e5, which can't represent
2091 * negative numbers (and additionally r9g9b9e5 can't represent
2092 * very large numbers). d3d10 seems happy without clamping in
2093 * this case, but gl spec is pretty clear: "for floating
2094 * point and integer formats, border values are clamped to
2095 * the representable range of the format" so do that here.
2096 */
2097 case PIPE_FORMAT_R11G11B10_FLOAT:
2098 min_clamp = vec4_bld.zero;
2099 break;
2100 case PIPE_FORMAT_R9G9B9E5_FLOAT:
2101 min_clamp = vec4_bld.zero;
2102 max_clamp = lp_build_const_vec(gallivm, vec4_type, MAX_RGB9E5);
2103 break;
2104 default:
2105 assert(0);
2106 break;
2107 }
2108 }
2109 }
2110
2111 if (min_clamp) {
2112 border_color = lp_build_max(&vec4_bld, border_color, min_clamp);
2113 }
2114 if (max_clamp) {
2115 border_color = lp_build_min(&vec4_bld, border_color, max_clamp);
2116 }
2117
2118 bld->border_color_clamped = border_color;
2119 }
2120
2121
2122 /**
2123 * General texture sampling codegen.
2124 * This function handles texture sampling for all texture targets (1D,
2125 * 2D, 3D, cube) and all filtering modes.
2126 */
2127 static void
2128 lp_build_sample_general(struct lp_build_sample_context *bld,
2129 unsigned sampler_unit,
2130 boolean is_gather,
2131 LLVMValueRef *coords,
2132 const LLVMValueRef *offsets,
2133 LLVMValueRef lod_positive,
2134 LLVMValueRef lod_fpart,
2135 LLVMValueRef ilevel0,
2136 LLVMValueRef ilevel1,
2137 LLVMValueRef *colors_out)
2138 {
2139 LLVMBuilderRef builder = bld->gallivm->builder;
2140 const struct lp_static_sampler_state *sampler_state = bld->static_sampler_state;
2141 const unsigned mip_filter = sampler_state->min_mip_filter;
2142 const unsigned min_filter = sampler_state->min_img_filter;
2143 const unsigned mag_filter = sampler_state->mag_img_filter;
2144 LLVMValueRef texels[4];
2145 unsigned chan;
2146
2147 /* if we need border color, (potentially) clamp it now */
2148 if (lp_sampler_wrap_mode_uses_border_color(sampler_state->wrap_s,
2149 min_filter,
2150 mag_filter) ||
2151 (bld->dims > 1 &&
2152 lp_sampler_wrap_mode_uses_border_color(sampler_state->wrap_t,
2153 min_filter,
2154 mag_filter)) ||
2155 (bld->dims > 2 &&
2156 lp_sampler_wrap_mode_uses_border_color(sampler_state->wrap_r,
2157 min_filter,
2158 mag_filter))) {
2159 lp_build_clamp_border_color(bld, sampler_unit);
2160 }
2161
2162
2163 /*
2164 * Get/interpolate texture colors.
2165 */
2166
2167 for (chan = 0; chan < 4; ++chan) {
2168 texels[chan] = lp_build_alloca(bld->gallivm, bld->texel_bld.vec_type, "");
2169 lp_build_name(texels[chan], "sampler%u_texel_%c_var", sampler_unit, "xyzw"[chan]);
2170 }
2171
2172 if (min_filter == mag_filter) {
2173 /* no need to distinguish between minification and magnification */
2174 lp_build_sample_mipmap(bld, min_filter, mip_filter,
2175 is_gather,
2176 coords, offsets,
2177 ilevel0, ilevel1, lod_fpart,
2178 texels);
2179 }
2180 else {
2181 /*
2182 * Could also get rid of the if-logic and always use mipmap_both, both
2183 * for the single lod and multi-lod case if nothing really uses this.
2184 */
2185 if (bld->num_lods == 1) {
2186 /* Emit conditional to choose min image filter or mag image filter
2187 * depending on the lod being > 0 or <= 0, respectively.
2188 */
2189 struct lp_build_if_state if_ctx;
2190
2191 lod_positive = LLVMBuildTrunc(builder, lod_positive,
2192 LLVMInt1TypeInContext(bld->gallivm->context), "");
2193
2194 lp_build_if(&if_ctx, bld->gallivm, lod_positive);
2195 {
2196 /* Use the minification filter */
2197 lp_build_sample_mipmap(bld, min_filter, mip_filter, FALSE,
2198 coords, offsets,
2199 ilevel0, ilevel1, lod_fpart,
2200 texels);
2201 }
2202 lp_build_else(&if_ctx);
2203 {
2204 /* Use the magnification filter */
2205 lp_build_sample_mipmap(bld, mag_filter, PIPE_TEX_MIPFILTER_NONE,
2206 FALSE,
2207 coords, offsets,
2208 ilevel0, NULL, NULL,
2209 texels);
2210 }
2211 lp_build_endif(&if_ctx);
2212 }
2213 else {
2214 LLVMValueRef need_linear, linear_mask;
2215 unsigned mip_filter_for_nearest;
2216 struct lp_build_if_state if_ctx;
2217
2218 if (min_filter == PIPE_TEX_FILTER_LINEAR) {
2219 linear_mask = lod_positive;
2220 mip_filter_for_nearest = PIPE_TEX_MIPFILTER_NONE;
2221 }
2222 else {
2223 linear_mask = lp_build_not(&bld->lodi_bld, lod_positive);
2224 mip_filter_for_nearest = mip_filter;
2225 }
2226 need_linear = lp_build_any_true_range(&bld->lodi_bld, bld->num_lods,
2227 linear_mask);
2228
2229 if (bld->num_lods != bld->coord_type.length) {
2230 linear_mask = lp_build_unpack_broadcast_aos_scalars(bld->gallivm,
2231 bld->lodi_type,
2232 bld->int_coord_type,
2233 linear_mask);
2234 }
2235
2236 lp_build_if(&if_ctx, bld->gallivm, need_linear);
2237 {
2238 /*
2239 * Do sampling with both filters simultaneously. This means using
2240 * a linear filter and doing some tricks (with weights) for the pixels
2241 * which need nearest filter.
2242 * Note that it's probably rare some pixels need nearest and some
2243 * linear filter but the fixups required for the nearest pixels
2244 * aren't all that complicated so just always run a combined path
2245 * if at least some pixels require linear.
2246 */
2247 lp_build_sample_mipmap_both(bld, linear_mask, mip_filter,
2248 coords, offsets,
2249 ilevel0, ilevel1,
2250 lod_fpart, lod_positive,
2251 texels);
2252 }
2253 lp_build_else(&if_ctx);
2254 {
2255 /*
2256 * All pixels require just nearest filtering, which is way
2257 * cheaper than linear, hence do a separate path for that.
2258 */
2259 lp_build_sample_mipmap(bld, PIPE_TEX_FILTER_NEAREST, FALSE,
2260 mip_filter_for_nearest,
2261 coords, offsets,
2262 ilevel0, ilevel1, lod_fpart,
2263 texels);
2264 }
2265 lp_build_endif(&if_ctx);
2266 }
2267 }
2268
2269 for (chan = 0; chan < 4; ++chan) {
2270 colors_out[chan] = LLVMBuildLoad(builder, texels[chan], "");
2271 lp_build_name(colors_out[chan], "sampler%u_texel_%c", sampler_unit, "xyzw"[chan]);
2272 }
2273 }
2274
2275
2276 /**
2277 * Texel fetch function.
2278 * In contrast to general sampling there is no filtering, no coord minification,
2279 * lod (if any) is always explicit uint, coords are uints (in terms of texel units)
2280 * directly to be applied to the selected mip level (after adding texel offsets).
2281 * This function handles texel fetch for all targets where texel fetch is supported
2282 * (no cube maps, but 1d, 2d, 3d are supported, arrays and buffers should be too).
2283 */
2284 static void
2285 lp_build_fetch_texel(struct lp_build_sample_context *bld,
2286 unsigned texture_unit,
2287 const LLVMValueRef *coords,
2288 LLVMValueRef explicit_lod,
2289 const LLVMValueRef *offsets,
2290 LLVMValueRef *colors_out)
2291 {
2292 struct lp_build_context *perquadi_bld = &bld->lodi_bld;
2293 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
2294 unsigned dims = bld->dims, chan;
2295 unsigned target = bld->static_texture_state->target;
2296 boolean out_of_bound_ret_zero = TRUE;
2297 LLVMValueRef size, ilevel;
2298 LLVMValueRef row_stride_vec = NULL, img_stride_vec = NULL;
2299 LLVMValueRef x = coords[0], y = coords[1], z = coords[2];
2300 LLVMValueRef width, height, depth, i, j;
2301 LLVMValueRef offset, out_of_bounds, out1;
2302
2303 out_of_bounds = int_coord_bld->zero;
2304
2305 if (explicit_lod && bld->static_texture_state->target != PIPE_BUFFER) {
2306 if (bld->num_mips != int_coord_bld->type.length) {
2307 ilevel = lp_build_pack_aos_scalars(bld->gallivm, int_coord_bld->type,
2308 perquadi_bld->type, explicit_lod, 0);
2309 }
2310 else {
2311 ilevel = explicit_lod;
2312 }
2313 lp_build_nearest_mip_level(bld, texture_unit, ilevel, &ilevel,
2314 out_of_bound_ret_zero ? &out_of_bounds : NULL);
2315 }
2316 else {
2317 assert(bld->num_mips == 1);
2318 if (bld->static_texture_state->target != PIPE_BUFFER) {
2319 ilevel = bld->dynamic_state->first_level(bld->dynamic_state, bld->gallivm,
2320 bld->context_ptr, texture_unit);
2321 }
2322 else {
2323 ilevel = lp_build_const_int32(bld->gallivm, 0);
2324 }
2325 }
2326 lp_build_mipmap_level_sizes(bld, ilevel,
2327 &size,
2328 &row_stride_vec, &img_stride_vec);
2329 lp_build_extract_image_sizes(bld, &bld->int_size_bld, int_coord_bld->type,
2330 size, &width, &height, &depth);
2331
2332 if (target == PIPE_TEXTURE_1D_ARRAY ||
2333 target == PIPE_TEXTURE_2D_ARRAY) {
2334 if (out_of_bound_ret_zero) {
2335 z = lp_build_layer_coord(bld, texture_unit, FALSE, z, &out1);
2336 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
2337 }
2338 else {
2339 z = lp_build_layer_coord(bld, texture_unit, FALSE, z, NULL);
2340 }
2341 }
2342
2343 /* This is a lot like border sampling */
2344 if (offsets[0]) {
2345 /*
2346 * coords are really unsigned, offsets are signed, but I don't think
2347 * exceeding 31 bits is possible
2348 */
2349 x = lp_build_add(int_coord_bld, x, offsets[0]);
2350 }
2351 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, x, int_coord_bld->zero);
2352 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
2353 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, x, width);
2354 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
2355
2356 if (dims >= 2) {
2357 if (offsets[1]) {
2358 y = lp_build_add(int_coord_bld, y, offsets[1]);
2359 }
2360 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, y, int_coord_bld->zero);
2361 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
2362 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, y, height);
2363 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
2364
2365 if (dims >= 3) {
2366 if (offsets[2]) {
2367 z = lp_build_add(int_coord_bld, z, offsets[2]);
2368 }
2369 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, z, int_coord_bld->zero);
2370 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
2371 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, z, depth);
2372 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
2373 }
2374 }
2375
2376 lp_build_sample_offset(int_coord_bld,
2377 bld->format_desc,
2378 x, y, z, row_stride_vec, img_stride_vec,
2379 &offset, &i, &j);
2380
2381 if (bld->static_texture_state->target != PIPE_BUFFER) {
2382 offset = lp_build_add(int_coord_bld, offset,
2383 lp_build_get_mip_offsets(bld, ilevel));
2384 }
2385
2386 offset = lp_build_andnot(int_coord_bld, offset, out_of_bounds);
2387
2388 lp_build_fetch_rgba_soa(bld->gallivm,
2389 bld->format_desc,
2390 bld->texel_type,
2391 bld->base_ptr, offset,
2392 i, j,
2393 bld->cache,
2394 colors_out);
2395
2396 if (out_of_bound_ret_zero) {
2397 /*
2398 * Only needed for ARB_robust_buffer_access_behavior and d3d10.
2399 * Could use min/max above instead of out-of-bounds comparisons
2400 * if we don't care about the result returned for out-of-bounds.
2401 */
2402 for (chan = 0; chan < 4; chan++) {
2403 colors_out[chan] = lp_build_select(&bld->texel_bld, out_of_bounds,
2404 bld->texel_bld.zero, colors_out[chan]);
2405 }
2406 }
2407 }
2408
2409
2410 /**
2411 * Just set texels to white instead of actually sampling the texture.
2412 * For debugging.
2413 */
2414 void
2415 lp_build_sample_nop(struct gallivm_state *gallivm,
2416 struct lp_type type,
2417 const LLVMValueRef *coords,
2418 LLVMValueRef texel_out[4])
2419 {
2420 LLVMValueRef one = lp_build_one(gallivm, type);
2421 unsigned chan;
2422
2423 for (chan = 0; chan < 4; chan++) {
2424 texel_out[chan] = one;
2425 }
2426 }
2427
2428
2429 /**
2430 * Build the actual texture sampling code.
2431 * 'texel' will return a vector of four LLVMValueRefs corresponding to
2432 * R, G, B, A.
2433 * \param type vector float type to use for coords, etc.
2434 * \param sample_key
2435 * \param derivs partial derivatives of (s,t,r,q) with respect to x and y
2436 */
2437 static void
2438 lp_build_sample_soa_code(struct gallivm_state *gallivm,
2439 const struct lp_static_texture_state *static_texture_state,
2440 const struct lp_static_sampler_state *static_sampler_state,
2441 struct lp_sampler_dynamic_state *dynamic_state,
2442 struct lp_type type,
2443 unsigned sample_key,
2444 unsigned texture_index,
2445 unsigned sampler_index,
2446 LLVMValueRef context_ptr,
2447 LLVMValueRef thread_data_ptr,
2448 const LLVMValueRef *coords,
2449 const LLVMValueRef *offsets,
2450 const struct lp_derivatives *derivs, /* optional */
2451 LLVMValueRef lod, /* optional */
2452 LLVMValueRef texel_out[4])
2453 {
2454 unsigned target = static_texture_state->target;
2455 unsigned dims = texture_dims(target);
2456 unsigned num_quads = type.length / 4;
2457 unsigned mip_filter, min_img_filter, mag_img_filter, i;
2458 struct lp_build_sample_context bld;
2459 struct lp_static_sampler_state derived_sampler_state = *static_sampler_state;
2460 LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context);
2461 LLVMBuilderRef builder = gallivm->builder;
2462 LLVMValueRef tex_width, newcoords[5];
2463 enum lp_sampler_lod_property lod_property;
2464 enum lp_sampler_lod_control lod_control;
2465 enum lp_sampler_op_type op_type;
2466 LLVMValueRef lod_bias = NULL;
2467 LLVMValueRef explicit_lod = NULL;
2468 boolean op_is_tex;
2469
2470 if (0) {
2471 enum pipe_format fmt = static_texture_state->format;
2472 debug_printf("Sample from %s\n", util_format_name(fmt));
2473 }
2474
2475 lod_property = (sample_key & LP_SAMPLER_LOD_PROPERTY_MASK) >>
2476 LP_SAMPLER_LOD_PROPERTY_SHIFT;
2477 lod_control = (sample_key & LP_SAMPLER_LOD_CONTROL_MASK) >>
2478 LP_SAMPLER_LOD_CONTROL_SHIFT;
2479 op_type = (sample_key & LP_SAMPLER_OP_TYPE_MASK) >>
2480 LP_SAMPLER_OP_TYPE_SHIFT;
2481
2482 op_is_tex = op_type == LP_SAMPLER_OP_TEXTURE;
2483
2484 if (lod_control == LP_SAMPLER_LOD_BIAS) {
2485 lod_bias = lod;
2486 assert(lod);
2487 assert(derivs == NULL);
2488 }
2489 else if (lod_control == LP_SAMPLER_LOD_EXPLICIT) {
2490 explicit_lod = lod;
2491 assert(lod);
2492 assert(derivs == NULL);
2493 }
2494 else if (lod_control == LP_SAMPLER_LOD_DERIVATIVES) {
2495 assert(derivs);
2496 assert(lod == NULL);
2497 }
2498 else {
2499 assert(derivs == NULL);
2500 assert(lod == NULL);
2501 }
2502
2503 if (static_texture_state->format == PIPE_FORMAT_NONE) {
2504 /*
2505 * If there's nothing bound, format is NONE, and we must return
2506 * all zero as mandated by d3d10 in this case.
2507 */
2508 unsigned chan;
2509 LLVMValueRef zero = lp_build_zero(gallivm, type);
2510 for (chan = 0; chan < 4; chan++) {
2511 texel_out[chan] = zero;
2512 }
2513 return;
2514 }
2515
2516 assert(type.floating);
2517
2518 /* Setup our build context */
2519 memset(&bld, 0, sizeof bld);
2520 bld.gallivm = gallivm;
2521 bld.context_ptr = context_ptr;
2522 bld.static_sampler_state = &derived_sampler_state;
2523 bld.static_texture_state = static_texture_state;
2524 bld.dynamic_state = dynamic_state;
2525 bld.format_desc = util_format_description(static_texture_state->format);
2526 bld.dims = dims;
2527
2528 bld.vector_width = lp_type_width(type);
2529
2530 bld.float_type = lp_type_float(32);
2531 bld.int_type = lp_type_int(32);
2532 bld.coord_type = type;
2533 bld.int_coord_type = lp_int_type(type);
2534 bld.float_size_in_type = lp_type_float(32);
2535 bld.float_size_in_type.length = dims > 1 ? 4 : 1;
2536 bld.int_size_in_type = lp_int_type(bld.float_size_in_type);
2537 bld.texel_type = type;
2538
2539 /* always using the first channel hopefully should be safe,
2540 * if not things WILL break in other places anyway.
2541 */
2542 if (bld.format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB &&
2543 bld.format_desc->channel[0].pure_integer) {
2544 if (bld.format_desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
2545 bld.texel_type = lp_type_int_vec(type.width, type.width * type.length);
2546 }
2547 else if (bld.format_desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED) {
2548 bld.texel_type = lp_type_uint_vec(type.width, type.width * type.length);
2549 }
2550 }
2551 else if (util_format_has_stencil(bld.format_desc) &&
2552 !util_format_has_depth(bld.format_desc)) {
2553 /* for stencil only formats, sample stencil (uint) */
2554 bld.texel_type = lp_type_int_vec(type.width, type.width * type.length);
2555 }
2556
2557 if (!static_texture_state->level_zero_only) {
2558 derived_sampler_state.min_mip_filter = static_sampler_state->min_mip_filter;
2559 } else {
2560 derived_sampler_state.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
2561 }
2562 if (op_type == LP_SAMPLER_OP_GATHER) {
2563 /*
2564 * gather4 is exactly like GL_LINEAR filtering but in the end skipping
2565 * the actual filtering. Using mostly the same paths, so cube face
2566 * selection, coord wrapping etc. all naturally uses the same code.
2567 */
2568 derived_sampler_state.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
2569 derived_sampler_state.min_img_filter = PIPE_TEX_FILTER_LINEAR;
2570 derived_sampler_state.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
2571 }
2572 mip_filter = derived_sampler_state.min_mip_filter;
2573
2574 if (0) {
2575 debug_printf(" .min_mip_filter = %u\n", derived_sampler_state.min_mip_filter);
2576 }
2577
2578 if (static_texture_state->target == PIPE_TEXTURE_CUBE ||
2579 static_texture_state->target == PIPE_TEXTURE_CUBE_ARRAY)
2580 {
2581 /*
2582 * Seamless filtering ignores wrap modes.
2583 * Setting to CLAMP_TO_EDGE is correct for nearest filtering, for
2584 * bilinear it's not correct but way better than using for instance repeat.
2585 * Note we even set this for non-seamless. Technically GL allows any wrap
2586 * mode, which made sense when supporting true borders (can get seamless
2587 * effect with border and CLAMP_TO_BORDER), but gallium doesn't support
2588 * borders and d3d9 requires wrap modes to be ignored and it's a pain to fix
2589 * up the sampler state (as it makes it texture dependent).
2590 */
2591 derived_sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
2592 derived_sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
2593 }
2594 /*
2595 * We could force CLAMP to CLAMP_TO_EDGE here if min/mag filter is nearest,
2596 * so AoS path could be used. Not sure it's worth the trouble...
2597 */
2598
2599 min_img_filter = derived_sampler_state.min_img_filter;
2600 mag_img_filter = derived_sampler_state.mag_img_filter;
2601
2602
2603 /*
2604 * This is all a bit complicated different paths are chosen for performance
2605 * reasons.
2606 * Essentially, there can be 1 lod per element, 1 lod per quad or 1 lod for
2607 * everything (the last two options are equivalent for 4-wide case).
2608 * If there's per-quad lod but we split to 4-wide so we can use AoS, per-quad
2609 * lod is calculated then the lod value extracted afterwards so making this
2610 * case basically the same as far as lod handling is concerned for the
2611 * further sample/filter code as the 1 lod for everything case.
2612 * Different lod handling mostly shows up when building mipmap sizes
2613 * (lp_build_mipmap_level_sizes() and friends) and also in filtering
2614 * (getting the fractional part of the lod to the right texels).
2615 */
2616
2617 /*
2618 * There are other situations where at least the multiple int lods could be
2619 * avoided like min and max lod being equal.
2620 */
2621 bld.num_mips = bld.num_lods = 1;
2622
2623 if ((gallivm_debug & GALLIVM_DEBUG_NO_QUAD_LOD) &&
2624 (gallivm_debug & GALLIVM_DEBUG_NO_RHO_APPROX) &&
2625 (static_texture_state->target == PIPE_TEXTURE_CUBE ||
2626 static_texture_state->target == PIPE_TEXTURE_CUBE_ARRAY) &&
2627 (op_is_tex && mip_filter != PIPE_TEX_MIPFILTER_NONE)) {
2628 /*
2629 * special case for using per-pixel lod even for implicit lod,
2630 * which is generally never required (ok by APIs) except to please
2631 * some (somewhat broken imho) tests (because per-pixel face selection
2632 * can cause derivatives to be different for pixels outside the primitive
2633 * due to the major axis division even if pre-project derivatives are
2634 * looking normal).
2635 */
2636 bld.num_mips = type.length;
2637 bld.num_lods = type.length;
2638 }
2639 else if (lod_property == LP_SAMPLER_LOD_PER_ELEMENT ||
2640 (explicit_lod || lod_bias || derivs)) {
2641 if ((!op_is_tex && target != PIPE_BUFFER) ||
2642 (op_is_tex && mip_filter != PIPE_TEX_MIPFILTER_NONE)) {
2643 bld.num_mips = type.length;
2644 bld.num_lods = type.length;
2645 }
2646 else if (op_is_tex && min_img_filter != mag_img_filter) {
2647 bld.num_mips = 1;
2648 bld.num_lods = type.length;
2649 }
2650 }
2651 /* TODO: for true scalar_lod should only use 1 lod value */
2652 else if ((!op_is_tex && explicit_lod && target != PIPE_BUFFER) ||
2653 (op_is_tex && mip_filter != PIPE_TEX_MIPFILTER_NONE)) {
2654 bld.num_mips = num_quads;
2655 bld.num_lods = num_quads;
2656 }
2657 else if (op_is_tex && min_img_filter != mag_img_filter) {
2658 bld.num_mips = 1;
2659 bld.num_lods = num_quads;
2660 }
2661
2662
2663 bld.lodf_type = type;
2664 /* we want native vector size to be able to use our intrinsics */
2665 if (bld.num_lods != type.length) {
2666 /* TODO: this currently always has to be per-quad or per-element */
2667 bld.lodf_type.length = type.length > 4 ? ((type.length + 15) / 16) * 4 : 1;
2668 }
2669 bld.lodi_type = lp_int_type(bld.lodf_type);
2670 bld.levelf_type = bld.lodf_type;
2671 if (bld.num_mips == 1) {
2672 bld.levelf_type.length = 1;
2673 }
2674 bld.leveli_type = lp_int_type(bld.levelf_type);
2675 bld.float_size_type = bld.float_size_in_type;
2676 /* Note: size vectors may not be native. They contain minified w/h/d/_ values,
2677 * with per-element lod that is w0/h0/d0/_/w1/h1/d1_/... so up to 8x4f32 */
2678 if (bld.num_mips > 1) {
2679 bld.float_size_type.length = bld.num_mips == type.length ?
2680 bld.num_mips * bld.float_size_in_type.length :
2681 type.length;
2682 }
2683 bld.int_size_type = lp_int_type(bld.float_size_type);
2684
2685 lp_build_context_init(&bld.float_bld, gallivm, bld.float_type);
2686 lp_build_context_init(&bld.float_vec_bld, gallivm, type);
2687 lp_build_context_init(&bld.int_bld, gallivm, bld.int_type);
2688 lp_build_context_init(&bld.coord_bld, gallivm, bld.coord_type);
2689 lp_build_context_init(&bld.int_coord_bld, gallivm, bld.int_coord_type);
2690 lp_build_context_init(&bld.int_size_in_bld, gallivm, bld.int_size_in_type);
2691 lp_build_context_init(&bld.float_size_in_bld, gallivm, bld.float_size_in_type);
2692 lp_build_context_init(&bld.int_size_bld, gallivm, bld.int_size_type);
2693 lp_build_context_init(&bld.float_size_bld, gallivm, bld.float_size_type);
2694 lp_build_context_init(&bld.texel_bld, gallivm, bld.texel_type);
2695 lp_build_context_init(&bld.levelf_bld, gallivm, bld.levelf_type);
2696 lp_build_context_init(&bld.leveli_bld, gallivm, bld.leveli_type);
2697 lp_build_context_init(&bld.lodf_bld, gallivm, bld.lodf_type);
2698 lp_build_context_init(&bld.lodi_bld, gallivm, bld.lodi_type);
2699
2700 /* Get the dynamic state */
2701 tex_width = dynamic_state->width(dynamic_state, gallivm,
2702 context_ptr, texture_index);
2703 bld.row_stride_array = dynamic_state->row_stride(dynamic_state, gallivm,
2704 context_ptr, texture_index);
2705 bld.img_stride_array = dynamic_state->img_stride(dynamic_state, gallivm,
2706 context_ptr, texture_index);
2707 bld.base_ptr = dynamic_state->base_ptr(dynamic_state, gallivm,
2708 context_ptr, texture_index);
2709 bld.mip_offsets = dynamic_state->mip_offsets(dynamic_state, gallivm,
2710 context_ptr, texture_index);
2711 /* Note that mip_offsets is an array[level] of offsets to texture images */
2712
2713 if (dynamic_state->cache_ptr && thread_data_ptr) {
2714 bld.cache = dynamic_state->cache_ptr(dynamic_state, gallivm,
2715 thread_data_ptr, texture_index);
2716 }
2717
2718 /* width, height, depth as single int vector */
2719 if (dims <= 1) {
2720 bld.int_size = tex_width;
2721 }
2722 else {
2723 bld.int_size = LLVMBuildInsertElement(builder, bld.int_size_in_bld.undef,
2724 tex_width,
2725 LLVMConstInt(i32t, 0, 0), "");
2726 if (dims >= 2) {
2727 LLVMValueRef tex_height =
2728 dynamic_state->height(dynamic_state, gallivm,
2729 context_ptr, texture_index);
2730 bld.int_size = LLVMBuildInsertElement(builder, bld.int_size,
2731 tex_height,
2732 LLVMConstInt(i32t, 1, 0), "");
2733 if (dims >= 3) {
2734 LLVMValueRef tex_depth =
2735 dynamic_state->depth(dynamic_state, gallivm, context_ptr,
2736 texture_index);
2737 bld.int_size = LLVMBuildInsertElement(builder, bld.int_size,
2738 tex_depth,
2739 LLVMConstInt(i32t, 2, 0), "");
2740 }
2741 }
2742 }
2743
2744 for (i = 0; i < 5; i++) {
2745 newcoords[i] = coords[i];
2746 }
2747
2748 if (0) {
2749 /* For debug: no-op texture sampling */
2750 lp_build_sample_nop(gallivm,
2751 bld.texel_type,
2752 newcoords,
2753 texel_out);
2754 }
2755
2756 else if (op_type == LP_SAMPLER_OP_FETCH) {
2757 lp_build_fetch_texel(&bld, texture_index, newcoords,
2758 lod, offsets,
2759 texel_out);
2760 }
2761
2762 else {
2763 LLVMValueRef lod_fpart = NULL, lod_positive = NULL;
2764 LLVMValueRef ilevel0 = NULL, ilevel1 = NULL;
2765 boolean use_aos;
2766
2767 if (util_format_is_pure_integer(static_texture_state->format) &&
2768 !util_format_has_depth(bld.format_desc) &&
2769 (static_sampler_state->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR ||
2770 static_sampler_state->min_img_filter == PIPE_TEX_FILTER_LINEAR ||
2771 static_sampler_state->mag_img_filter == PIPE_TEX_FILTER_LINEAR)) {
2772 /*
2773 * Bail if impossible filtering is specified (the awkard additional
2774 * depth check is because it is legal in gallium to have things like S8Z24
2775 * here which would say it's pure int despite such formats should sample
2776 * the depth component).
2777 * In GL such filters make the texture incomplete, this makes it robust
2778 * against state trackers which set this up regardless (we'd crash in the
2779 * lerp later (except for gather)).
2780 * Must do this after fetch_texel code since with GL state tracker we'll
2781 * get some junk sampler for buffer textures.
2782 */
2783 unsigned chan;
2784 LLVMValueRef zero = lp_build_zero(gallivm, type);
2785 for (chan = 0; chan < 4; chan++) {
2786 texel_out[chan] = zero;
2787 }
2788 return;
2789 }
2790
2791 use_aos = util_format_fits_8unorm(bld.format_desc) &&
2792 op_is_tex &&
2793 /* not sure this is strictly needed or simply impossible */
2794 derived_sampler_state.compare_mode == PIPE_TEX_COMPARE_NONE &&
2795 lp_is_simple_wrap_mode(derived_sampler_state.wrap_s);
2796
2797 use_aos &= bld.num_lods <= num_quads ||
2798 derived_sampler_state.min_img_filter ==
2799 derived_sampler_state.mag_img_filter;
2800 if (dims > 1) {
2801 use_aos &= lp_is_simple_wrap_mode(derived_sampler_state.wrap_t);
2802 if (dims > 2) {
2803 use_aos &= lp_is_simple_wrap_mode(derived_sampler_state.wrap_r);
2804 }
2805 }
2806 if ((static_texture_state->target == PIPE_TEXTURE_CUBE ||
2807 static_texture_state->target == PIPE_TEXTURE_CUBE_ARRAY) &&
2808 derived_sampler_state.seamless_cube_map &&
2809 (derived_sampler_state.min_img_filter == PIPE_TEX_FILTER_LINEAR ||
2810 derived_sampler_state.mag_img_filter == PIPE_TEX_FILTER_LINEAR)) {
2811 /* theoretically possible with AoS filtering but not implemented (complex!) */
2812 use_aos = 0;
2813 }
2814
2815 if ((gallivm_debug & GALLIVM_DEBUG_PERF) &&
2816 !use_aos && util_format_fits_8unorm(bld.format_desc)) {
2817 debug_printf("%s: using floating point linear filtering for %s\n",
2818 __FUNCTION__, bld.format_desc->short_name);
2819 debug_printf(" min_img %d mag_img %d mip %d target %d seamless %d"
2820 " wraps %d wrapt %d wrapr %d\n",
2821 derived_sampler_state.min_img_filter,
2822 derived_sampler_state.mag_img_filter,
2823 derived_sampler_state.min_mip_filter,
2824 static_texture_state->target,
2825 derived_sampler_state.seamless_cube_map,
2826 derived_sampler_state.wrap_s,
2827 derived_sampler_state.wrap_t,
2828 derived_sampler_state.wrap_r);
2829 }
2830
2831 lp_build_sample_common(&bld, texture_index, sampler_index,
2832 newcoords,
2833 derivs, lod_bias, explicit_lod,
2834 &lod_positive, &lod_fpart,
2835 &ilevel0, &ilevel1);
2836
2837 if (use_aos && static_texture_state->target == PIPE_TEXTURE_CUBE_ARRAY) {
2838 /* The aos path doesn't do seamless filtering so simply add cube layer
2839 * to face now.
2840 */
2841 newcoords[2] = lp_build_add(&bld.int_coord_bld, newcoords[2], newcoords[3]);
2842 }
2843
2844 /*
2845 * we only try 8-wide sampling with soa as it appears to
2846 * be a loss with aos with AVX (but it should work, except
2847 * for conformance if min_filter != mag_filter if num_lods > 1).
2848 * (It should be faster if we'd support avx2)
2849 */
2850 if (num_quads == 1 || !use_aos) {
2851 if (use_aos) {
2852 /* do sampling/filtering with fixed pt arithmetic */
2853 lp_build_sample_aos(&bld, sampler_index,
2854 newcoords[0], newcoords[1],
2855 newcoords[2],
2856 offsets, lod_positive, lod_fpart,
2857 ilevel0, ilevel1,
2858 texel_out);
2859 }
2860
2861 else {
2862 lp_build_sample_general(&bld, sampler_index,
2863 op_type == LP_SAMPLER_OP_GATHER,
2864 newcoords, offsets,
2865 lod_positive, lod_fpart,
2866 ilevel0, ilevel1,
2867 texel_out);
2868 }
2869 }
2870 else {
2871 unsigned j;
2872 struct lp_build_sample_context bld4;
2873 struct lp_type type4 = type;
2874 unsigned i;
2875 LLVMValueRef texelout4[4];
2876 LLVMValueRef texelouttmp[4][LP_MAX_VECTOR_LENGTH/16];
2877
2878 type4.length = 4;
2879
2880 /* Setup our build context */
2881 memset(&bld4, 0, sizeof bld4);
2882 bld4.gallivm = bld.gallivm;
2883 bld4.context_ptr = bld.context_ptr;
2884 bld4.static_texture_state = bld.static_texture_state;
2885 bld4.static_sampler_state = bld.static_sampler_state;
2886 bld4.dynamic_state = bld.dynamic_state;
2887 bld4.format_desc = bld.format_desc;
2888 bld4.dims = bld.dims;
2889 bld4.row_stride_array = bld.row_stride_array;
2890 bld4.img_stride_array = bld.img_stride_array;
2891 bld4.base_ptr = bld.base_ptr;
2892 bld4.mip_offsets = bld.mip_offsets;
2893 bld4.int_size = bld.int_size;
2894 bld4.cache = bld.cache;
2895
2896 bld4.vector_width = lp_type_width(type4);
2897
2898 bld4.float_type = lp_type_float(32);
2899 bld4.int_type = lp_type_int(32);
2900 bld4.coord_type = type4;
2901 bld4.int_coord_type = lp_int_type(type4);
2902 bld4.float_size_in_type = lp_type_float(32);
2903 bld4.float_size_in_type.length = dims > 1 ? 4 : 1;
2904 bld4.int_size_in_type = lp_int_type(bld4.float_size_in_type);
2905 bld4.texel_type = bld.texel_type;
2906 bld4.texel_type.length = 4;
2907
2908 bld4.num_mips = bld4.num_lods = 1;
2909 if ((gallivm_debug & GALLIVM_DEBUG_NO_QUAD_LOD) &&
2910 (gallivm_debug & GALLIVM_DEBUG_NO_RHO_APPROX) &&
2911 (static_texture_state->target == PIPE_TEXTURE_CUBE ||
2912 static_texture_state->target == PIPE_TEXTURE_CUBE_ARRAY) &&
2913 (op_is_tex && mip_filter != PIPE_TEX_MIPFILTER_NONE)) {
2914 bld4.num_mips = type4.length;
2915 bld4.num_lods = type4.length;
2916 }
2917 if (lod_property == LP_SAMPLER_LOD_PER_ELEMENT &&
2918 (explicit_lod || lod_bias || derivs)) {
2919 if ((!op_is_tex && target != PIPE_BUFFER) ||
2920 (op_is_tex && mip_filter != PIPE_TEX_MIPFILTER_NONE)) {
2921 bld4.num_mips = type4.length;
2922 bld4.num_lods = type4.length;
2923 }
2924 else if (op_is_tex && min_img_filter != mag_img_filter) {
2925 bld4.num_mips = 1;
2926 bld4.num_lods = type4.length;
2927 }
2928 }
2929
2930 /* we want native vector size to be able to use our intrinsics */
2931 bld4.lodf_type = type4;
2932 if (bld4.num_lods != type4.length) {
2933 bld4.lodf_type.length = 1;
2934 }
2935 bld4.lodi_type = lp_int_type(bld4.lodf_type);
2936 bld4.levelf_type = type4;
2937 if (bld4.num_mips != type4.length) {
2938 bld4.levelf_type.length = 1;
2939 }
2940 bld4.leveli_type = lp_int_type(bld4.levelf_type);
2941 bld4.float_size_type = bld4.float_size_in_type;
2942 if (bld4.num_mips > 1) {
2943 bld4.float_size_type.length = bld4.num_mips == type4.length ?
2944 bld4.num_mips * bld4.float_size_in_type.length :
2945 type4.length;
2946 }
2947 bld4.int_size_type = lp_int_type(bld4.float_size_type);
2948
2949 lp_build_context_init(&bld4.float_bld, gallivm, bld4.float_type);
2950 lp_build_context_init(&bld4.float_vec_bld, gallivm, type4);
2951 lp_build_context_init(&bld4.int_bld, gallivm, bld4.int_type);
2952 lp_build_context_init(&bld4.coord_bld, gallivm, bld4.coord_type);
2953 lp_build_context_init(&bld4.int_coord_bld, gallivm, bld4.int_coord_type);
2954 lp_build_context_init(&bld4.int_size_in_bld, gallivm, bld4.int_size_in_type);
2955 lp_build_context_init(&bld4.float_size_in_bld, gallivm, bld4.float_size_in_type);
2956 lp_build_context_init(&bld4.int_size_bld, gallivm, bld4.int_size_type);
2957 lp_build_context_init(&bld4.float_size_bld, gallivm, bld4.float_size_type);
2958 lp_build_context_init(&bld4.texel_bld, gallivm, bld4.texel_type);
2959 lp_build_context_init(&bld4.levelf_bld, gallivm, bld4.levelf_type);
2960 lp_build_context_init(&bld4.leveli_bld, gallivm, bld4.leveli_type);
2961 lp_build_context_init(&bld4.lodf_bld, gallivm, bld4.lodf_type);
2962 lp_build_context_init(&bld4.lodi_bld, gallivm, bld4.lodi_type);
2963
2964 for (i = 0; i < num_quads; i++) {
2965 LLVMValueRef s4, t4, r4;
2966 LLVMValueRef lod_positive4, lod_fpart4 = NULL;
2967 LLVMValueRef ilevel04, ilevel14 = NULL;
2968 LLVMValueRef offsets4[4] = { NULL };
2969 unsigned num_lods = bld4.num_lods;
2970
2971 s4 = lp_build_extract_range(gallivm, newcoords[0], 4*i, 4);
2972 t4 = lp_build_extract_range(gallivm, newcoords[1], 4*i, 4);
2973 r4 = lp_build_extract_range(gallivm, newcoords[2], 4*i, 4);
2974
2975 if (offsets[0]) {
2976 offsets4[0] = lp_build_extract_range(gallivm, offsets[0], 4*i, 4);
2977 if (dims > 1) {
2978 offsets4[1] = lp_build_extract_range(gallivm, offsets[1], 4*i, 4);
2979 if (dims > 2) {
2980 offsets4[2] = lp_build_extract_range(gallivm, offsets[2], 4*i, 4);
2981 }
2982 }
2983 }
2984 lod_positive4 = lp_build_extract_range(gallivm, lod_positive, num_lods * i, num_lods);
2985 ilevel04 = bld.num_mips == 1 ? ilevel0 :
2986 lp_build_extract_range(gallivm, ilevel0, num_lods * i, num_lods);
2987 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
2988 ilevel14 = lp_build_extract_range(gallivm, ilevel1, num_lods * i, num_lods);
2989 lod_fpart4 = lp_build_extract_range(gallivm, lod_fpart, num_lods * i, num_lods);
2990 }
2991
2992 if (use_aos) {
2993 /* do sampling/filtering with fixed pt arithmetic */
2994 lp_build_sample_aos(&bld4, sampler_index,
2995 s4, t4, r4, offsets4,
2996 lod_positive4, lod_fpart4,
2997 ilevel04, ilevel14,
2998 texelout4);
2999 }
3000
3001 else {
3002 /* this path is currently unreachable and hence might break easily... */
3003 LLVMValueRef newcoords4[5];
3004 newcoords4[0] = s4;
3005 newcoords4[1] = t4;
3006 newcoords4[2] = r4;
3007 newcoords4[3] = lp_build_extract_range(gallivm, newcoords[3], 4*i, 4);
3008 newcoords4[4] = lp_build_extract_range(gallivm, newcoords[4], 4*i, 4);
3009
3010 lp_build_sample_general(&bld4, sampler_index,
3011 op_type == LP_SAMPLER_OP_GATHER,
3012 newcoords4, offsets4,
3013 lod_positive4, lod_fpart4,
3014 ilevel04, ilevel14,
3015 texelout4);
3016 }
3017 for (j = 0; j < 4; j++) {
3018 texelouttmp[j][i] = texelout4[j];
3019 }
3020 }
3021
3022 for (j = 0; j < 4; j++) {
3023 texel_out[j] = lp_build_concat(gallivm, texelouttmp[j], type4, num_quads);
3024 }
3025 }
3026 }
3027
3028 if (target != PIPE_BUFFER && op_type != LP_SAMPLER_OP_GATHER) {
3029 apply_sampler_swizzle(&bld, texel_out);
3030 }
3031
3032 /*
3033 * texel type can be a (32bit) int/uint (for pure int formats only),
3034 * however we are expected to always return floats (storage is untyped).
3035 */
3036 if (!bld.texel_type.floating) {
3037 unsigned chan;
3038 for (chan = 0; chan < 4; chan++) {
3039 texel_out[chan] = LLVMBuildBitCast(builder, texel_out[chan],
3040 lp_build_vec_type(gallivm, type), "");
3041 }
3042 }
3043 }
3044
3045
3046 #define USE_TEX_FUNC_CALL 1
3047
3048 #define LP_MAX_TEX_FUNC_ARGS 32
3049
3050 static inline void
3051 get_target_info(enum pipe_texture_target target,
3052 unsigned *num_coords, unsigned *num_derivs,
3053 unsigned *num_offsets, unsigned *layer)
3054 {
3055 unsigned dims = texture_dims(target);
3056 *num_coords = dims;
3057 *num_offsets = dims;
3058 *num_derivs = (target == PIPE_TEXTURE_CUBE ||
3059 target == PIPE_TEXTURE_CUBE_ARRAY) ? 3 : dims;
3060 *layer = has_layer_coord(target) ? 2: 0;
3061 if (target == PIPE_TEXTURE_CUBE_ARRAY) {
3062 /*
3063 * dims doesn't include r coord for cubes - this is handled
3064 * by layer instead, but need to fix up for cube arrays...
3065 */
3066 *layer = 3;
3067 *num_coords = 3;
3068 }
3069 }
3070
3071
3072 /**
3073 * Generate the function body for a texture sampling function.
3074 */
3075 static void
3076 lp_build_sample_gen_func(struct gallivm_state *gallivm,
3077 const struct lp_static_texture_state *static_texture_state,
3078 const struct lp_static_sampler_state *static_sampler_state,
3079 struct lp_sampler_dynamic_state *dynamic_state,
3080 struct lp_type type,
3081 unsigned texture_index,
3082 unsigned sampler_index,
3083 LLVMValueRef function,
3084 unsigned num_args,
3085 unsigned sample_key)
3086 {
3087 LLVMBuilderRef old_builder;
3088 LLVMBasicBlockRef block;
3089 LLVMValueRef coords[5];
3090 LLVMValueRef offsets[3] = { NULL };
3091 LLVMValueRef lod = NULL;
3092 LLVMValueRef context_ptr;
3093 LLVMValueRef thread_data_ptr = NULL;
3094 LLVMValueRef texel_out[4];
3095 struct lp_derivatives derivs;
3096 struct lp_derivatives *deriv_ptr = NULL;
3097 unsigned num_param = 0;
3098 unsigned i, num_coords, num_derivs, num_offsets, layer;
3099 enum lp_sampler_lod_control lod_control;
3100 boolean need_cache = FALSE;
3101
3102 lod_control = (sample_key & LP_SAMPLER_LOD_CONTROL_MASK) >>
3103 LP_SAMPLER_LOD_CONTROL_SHIFT;
3104
3105 get_target_info(static_texture_state->target,
3106 &num_coords, &num_derivs, &num_offsets, &layer);
3107
3108 if (dynamic_state->cache_ptr) {
3109 const struct util_format_description *format_desc;
3110 format_desc = util_format_description(static_texture_state->format);
3111 if (format_desc && format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
3112 need_cache = TRUE;
3113 }
3114 }
3115
3116 /* "unpack" arguments */
3117 context_ptr = LLVMGetParam(function, num_param++);
3118 if (need_cache) {
3119 thread_data_ptr = LLVMGetParam(function, num_param++);
3120 }
3121 for (i = 0; i < num_coords; i++) {
3122 coords[i] = LLVMGetParam(function, num_param++);
3123 }
3124 for (i = num_coords; i < 5; i++) {
3125 /* This is rather unfortunate... */
3126 coords[i] = lp_build_undef(gallivm, type);
3127 }
3128 if (layer) {
3129 coords[layer] = LLVMGetParam(function, num_param++);
3130 }
3131 if (sample_key & LP_SAMPLER_SHADOW) {
3132 coords[4] = LLVMGetParam(function, num_param++);
3133 }
3134 if (sample_key & LP_SAMPLER_OFFSETS) {
3135 for (i = 0; i < num_offsets; i++) {
3136 offsets[i] = LLVMGetParam(function, num_param++);
3137 }
3138 }
3139 if (lod_control == LP_SAMPLER_LOD_BIAS ||
3140 lod_control == LP_SAMPLER_LOD_EXPLICIT) {
3141 lod = LLVMGetParam(function, num_param++);
3142 }
3143 else if (lod_control == LP_SAMPLER_LOD_DERIVATIVES) {
3144 for (i = 0; i < num_derivs; i++) {
3145 derivs.ddx[i] = LLVMGetParam(function, num_param++);
3146 derivs.ddy[i] = LLVMGetParam(function, num_param++);
3147 }
3148 deriv_ptr = &derivs;
3149 }
3150
3151 assert(num_args == num_param);
3152
3153 /*
3154 * Function body
3155 */
3156
3157 old_builder = gallivm->builder;
3158 block = LLVMAppendBasicBlockInContext(gallivm->context, function, "entry");
3159 gallivm->builder = LLVMCreateBuilderInContext(gallivm->context);
3160 LLVMPositionBuilderAtEnd(gallivm->builder, block);
3161
3162 lp_build_sample_soa_code(gallivm,
3163 static_texture_state,
3164 static_sampler_state,
3165 dynamic_state,
3166 type,
3167 sample_key,
3168 texture_index,
3169 sampler_index,
3170 context_ptr,
3171 thread_data_ptr,
3172 coords,
3173 offsets,
3174 deriv_ptr,
3175 lod,
3176 texel_out);
3177
3178 LLVMBuildAggregateRet(gallivm->builder, texel_out, 4);
3179
3180 LLVMDisposeBuilder(gallivm->builder);
3181 gallivm->builder = old_builder;
3182
3183 gallivm_verify_function(gallivm, function);
3184 }
3185
3186
3187 /**
3188 * Call the matching function for texture sampling.
3189 * If there's no match, generate a new one.
3190 */
3191 static void
3192 lp_build_sample_soa_func(struct gallivm_state *gallivm,
3193 const struct lp_static_texture_state *static_texture_state,
3194 const struct lp_static_sampler_state *static_sampler_state,
3195 struct lp_sampler_dynamic_state *dynamic_state,
3196 const struct lp_sampler_params *params)
3197 {
3198 LLVMBuilderRef builder = gallivm->builder;
3199 LLVMModuleRef module = LLVMGetGlobalParent(LLVMGetBasicBlockParent(
3200 LLVMGetInsertBlock(builder)));
3201 LLVMValueRef function, inst;
3202 LLVMValueRef args[LP_MAX_TEX_FUNC_ARGS];
3203 LLVMBasicBlockRef bb;
3204 LLVMValueRef tex_ret;
3205 unsigned num_args = 0;
3206 char func_name[64];
3207 unsigned i, num_coords, num_derivs, num_offsets, layer;
3208 unsigned texture_index = params->texture_index;
3209 unsigned sampler_index = params->sampler_index;
3210 unsigned sample_key = params->sample_key;
3211 const LLVMValueRef *coords = params->coords;
3212 const LLVMValueRef *offsets = params->offsets;
3213 const struct lp_derivatives *derivs = params->derivs;
3214 enum lp_sampler_lod_control lod_control;
3215 boolean need_cache = FALSE;
3216
3217 lod_control = (sample_key & LP_SAMPLER_LOD_CONTROL_MASK) >>
3218 LP_SAMPLER_LOD_CONTROL_SHIFT;
3219
3220 get_target_info(static_texture_state->target,
3221 &num_coords, &num_derivs, &num_offsets, &layer);
3222
3223 if (dynamic_state->cache_ptr) {
3224 const struct util_format_description *format_desc;
3225 format_desc = util_format_description(static_texture_state->format);
3226 if (format_desc && format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
3227 /*
3228 * This is not 100% correct, if we have cache but the
3229 * util_format_s3tc_prefer is true the cache won't get used
3230 * regardless (could hook up the block decode there...) */
3231 need_cache = TRUE;
3232 }
3233 }
3234 /*
3235 * texture function matches are found by name.
3236 * Thus the name has to include both the texture and sampler unit
3237 * (which covers all static state) plus the actual texture function
3238 * (including things like offsets, shadow coord, lod control).
3239 * Additionally lod_property has to be included too.
3240 */
3241
3242 util_snprintf(func_name, sizeof(func_name), "texfunc_res_%d_sam_%d_%x",
3243 texture_index, sampler_index, sample_key);
3244
3245 function = LLVMGetNamedFunction(module, func_name);
3246
3247 if(!function) {
3248 LLVMTypeRef arg_types[LP_MAX_TEX_FUNC_ARGS];
3249 LLVMTypeRef ret_type;
3250 LLVMTypeRef function_type;
3251 LLVMTypeRef val_type[4];
3252 unsigned num_param = 0;
3253
3254 /*
3255 * Generate the function prototype.
3256 */
3257
3258 arg_types[num_param++] = LLVMTypeOf(params->context_ptr);
3259 if (need_cache) {
3260 arg_types[num_param++] = LLVMTypeOf(params->thread_data_ptr);
3261 }
3262 for (i = 0; i < num_coords; i++) {
3263 arg_types[num_param++] = LLVMTypeOf(coords[0]);
3264 assert(LLVMTypeOf(coords[0]) == LLVMTypeOf(coords[i]));
3265 }
3266 if (layer) {
3267 arg_types[num_param++] = LLVMTypeOf(coords[layer]);
3268 assert(LLVMTypeOf(coords[0]) == LLVMTypeOf(coords[layer]));
3269 }
3270 if (sample_key & LP_SAMPLER_SHADOW) {
3271 arg_types[num_param++] = LLVMTypeOf(coords[0]);
3272 }
3273 if (sample_key & LP_SAMPLER_OFFSETS) {
3274 for (i = 0; i < num_offsets; i++) {
3275 arg_types[num_param++] = LLVMTypeOf(offsets[0]);
3276 assert(LLVMTypeOf(offsets[0]) == LLVMTypeOf(offsets[i]));
3277 }
3278 }
3279 if (lod_control == LP_SAMPLER_LOD_BIAS ||
3280 lod_control == LP_SAMPLER_LOD_EXPLICIT) {
3281 arg_types[num_param++] = LLVMTypeOf(params->lod);
3282 }
3283 else if (lod_control == LP_SAMPLER_LOD_DERIVATIVES) {
3284 for (i = 0; i < num_derivs; i++) {
3285 arg_types[num_param++] = LLVMTypeOf(derivs->ddx[i]);
3286 arg_types[num_param++] = LLVMTypeOf(derivs->ddy[i]);
3287 assert(LLVMTypeOf(derivs->ddx[0]) == LLVMTypeOf(derivs->ddx[i]));
3288 assert(LLVMTypeOf(derivs->ddy[0]) == LLVMTypeOf(derivs->ddy[i]));
3289 }
3290 }
3291
3292 val_type[0] = val_type[1] = val_type[2] = val_type[3] =
3293 lp_build_vec_type(gallivm, params->type);
3294 ret_type = LLVMStructTypeInContext(gallivm->context, val_type, 4, 0);
3295 function_type = LLVMFunctionType(ret_type, arg_types, num_param, 0);
3296 function = LLVMAddFunction(module, func_name, function_type);
3297
3298 for (i = 0; i < num_param; ++i) {
3299 if(LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind) {
3300 LLVMAddAttribute(LLVMGetParam(function, i), LLVMNoAliasAttribute);
3301 }
3302 }
3303
3304 LLVMSetFunctionCallConv(function, LLVMFastCallConv);
3305 LLVMSetLinkage(function, LLVMPrivateLinkage);
3306
3307 lp_build_sample_gen_func(gallivm,
3308 static_texture_state,
3309 static_sampler_state,
3310 dynamic_state,
3311 params->type,
3312 texture_index,
3313 sampler_index,
3314 function,
3315 num_param,
3316 sample_key);
3317 }
3318
3319 num_args = 0;
3320 args[num_args++] = params->context_ptr;
3321 if (need_cache) {
3322 args[num_args++] = params->thread_data_ptr;
3323 }
3324 for (i = 0; i < num_coords; i++) {
3325 args[num_args++] = coords[i];
3326 }
3327 if (layer) {
3328 args[num_args++] = coords[layer];
3329 }
3330 if (sample_key & LP_SAMPLER_SHADOW) {
3331 args[num_args++] = coords[4];
3332 }
3333 if (sample_key & LP_SAMPLER_OFFSETS) {
3334 for (i = 0; i < num_offsets; i++) {
3335 args[num_args++] = offsets[i];
3336 }
3337 }
3338 if (lod_control == LP_SAMPLER_LOD_BIAS ||
3339 lod_control == LP_SAMPLER_LOD_EXPLICIT) {
3340 args[num_args++] = params->lod;
3341 }
3342 else if (lod_control == LP_SAMPLER_LOD_DERIVATIVES) {
3343 for (i = 0; i < num_derivs; i++) {
3344 args[num_args++] = derivs->ddx[i];
3345 args[num_args++] = derivs->ddy[i];
3346 }
3347 }
3348
3349 assert(num_args <= LP_MAX_TEX_FUNC_ARGS);
3350
3351 tex_ret = LLVMBuildCall(builder, function, args, num_args, "");
3352 bb = LLVMGetInsertBlock(builder);
3353 inst = LLVMGetLastInstruction(bb);
3354 LLVMSetInstructionCallConv(inst, LLVMFastCallConv);
3355
3356 for (i = 0; i < 4; i++) {
3357 params->texel[i] = LLVMBuildExtractValue(gallivm->builder, tex_ret, i, "");
3358 }
3359 }
3360
3361
3362 /**
3363 * Build texture sampling code.
3364 * Either via a function call or inline it directly.
3365 */
3366 void
3367 lp_build_sample_soa(const struct lp_static_texture_state *static_texture_state,
3368 const struct lp_static_sampler_state *static_sampler_state,
3369 struct lp_sampler_dynamic_state *dynamic_state,
3370 struct gallivm_state *gallivm,
3371 const struct lp_sampler_params *params)
3372 {
3373 boolean use_tex_func = FALSE;
3374
3375 /*
3376 * Do not use a function call if the sampling is "simple enough".
3377 * We define this by
3378 * a) format
3379 * b) no mips (either one level only or no mip filter)
3380 * No mips will definitely make the code smaller, though
3381 * the format requirement is a bit iffy - there's some (SoA) formats
3382 * which definitely generate less code. This does happen to catch
3383 * some important cases though which are hurt quite a bit by using
3384 * a call (though not really because of the call overhead but because
3385 * they are reusing the same texture unit with some of the same
3386 * parameters).
3387 * Ideally we'd let llvm recognize this stuff by doing IPO passes.
3388 */
3389
3390 if (USE_TEX_FUNC_CALL) {
3391 const struct util_format_description *format_desc;
3392 boolean simple_format;
3393 boolean simple_tex;
3394 enum lp_sampler_op_type op_type;
3395 format_desc = util_format_description(static_texture_state->format);
3396 simple_format = !format_desc ||
3397 (util_format_is_rgba8_variant(format_desc) &&
3398 format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB);
3399
3400 op_type = (params->sample_key & LP_SAMPLER_OP_TYPE_MASK) >>
3401 LP_SAMPLER_OP_TYPE_SHIFT;
3402 simple_tex =
3403 op_type != LP_SAMPLER_OP_TEXTURE ||
3404 ((static_sampler_state->min_mip_filter == PIPE_TEX_MIPFILTER_NONE ||
3405 static_texture_state->level_zero_only == TRUE) &&
3406 static_sampler_state->min_img_filter == static_sampler_state->mag_img_filter);
3407
3408 use_tex_func = format_desc && !(simple_format && simple_tex);
3409 }
3410
3411 if (use_tex_func) {
3412 lp_build_sample_soa_func(gallivm,
3413 static_texture_state,
3414 static_sampler_state,
3415 dynamic_state,
3416 params);
3417 }
3418 else {
3419 lp_build_sample_soa_code(gallivm,
3420 static_texture_state,
3421 static_sampler_state,
3422 dynamic_state,
3423 params->type,
3424 params->sample_key,
3425 params->texture_index,
3426 params->sampler_index,
3427 params->context_ptr,
3428 params->thread_data_ptr,
3429 params->coords,
3430 params->offsets,
3431 params->derivs,
3432 params->lod,
3433 params->texel);
3434 }
3435 }
3436
3437
3438 void
3439 lp_build_size_query_soa(struct gallivm_state *gallivm,
3440 const struct lp_static_texture_state *static_state,
3441 struct lp_sampler_dynamic_state *dynamic_state,
3442 struct lp_type int_type,
3443 unsigned texture_unit,
3444 unsigned target,
3445 LLVMValueRef context_ptr,
3446 boolean is_sviewinfo,
3447 enum lp_sampler_lod_property lod_property,
3448 LLVMValueRef explicit_lod,
3449 LLVMValueRef *sizes_out)
3450 {
3451 LLVMValueRef lod, level, size;
3452 LLVMValueRef first_level = NULL;
3453 int dims, i;
3454 boolean has_array;
3455 unsigned num_lods = 1;
3456 struct lp_build_context bld_int_vec4;
3457
3458 if (static_state->format == PIPE_FORMAT_NONE) {
3459 /*
3460 * If there's nothing bound, format is NONE, and we must return
3461 * all zero as mandated by d3d10 in this case.
3462 */
3463 unsigned chan;
3464 LLVMValueRef zero = lp_build_const_vec(gallivm, int_type, 0.0F);
3465 for (chan = 0; chan < 4; chan++) {
3466 sizes_out[chan] = zero;
3467 }
3468 return;
3469 }
3470
3471 /*
3472 * Do some sanity verification about bound texture and shader dcl target.
3473 * Not entirely sure what's possible but assume array/non-array
3474 * always compatible (probably not ok for OpenGL but d3d10 has no
3475 * distinction of arrays at the resource level).
3476 * Everything else looks bogus (though not entirely sure about rect/2d).
3477 * Currently disabled because it causes assertion failures if there's
3478 * nothing bound (or rather a dummy texture, not that this case would
3479 * return the right values).
3480 */
3481 if (0 && static_state->target != target) {
3482 if (static_state->target == PIPE_TEXTURE_1D)
3483 assert(target == PIPE_TEXTURE_1D_ARRAY);
3484 else if (static_state->target == PIPE_TEXTURE_1D_ARRAY)
3485 assert(target == PIPE_TEXTURE_1D);
3486 else if (static_state->target == PIPE_TEXTURE_2D)
3487 assert(target == PIPE_TEXTURE_2D_ARRAY);
3488 else if (static_state->target == PIPE_TEXTURE_2D_ARRAY)
3489 assert(target == PIPE_TEXTURE_2D);
3490 else if (static_state->target == PIPE_TEXTURE_CUBE)
3491 assert(target == PIPE_TEXTURE_CUBE_ARRAY);
3492 else if (static_state->target == PIPE_TEXTURE_CUBE_ARRAY)
3493 assert(target == PIPE_TEXTURE_CUBE);
3494 else
3495 assert(0);
3496 }
3497
3498 dims = texture_dims(target);
3499
3500 switch (target) {
3501 case PIPE_TEXTURE_1D_ARRAY:
3502 case PIPE_TEXTURE_2D_ARRAY:
3503 case PIPE_TEXTURE_CUBE_ARRAY:
3504 has_array = TRUE;
3505 break;
3506 default:
3507 has_array = FALSE;
3508 break;
3509 }
3510
3511 assert(!int_type.floating);
3512
3513 lp_build_context_init(&bld_int_vec4, gallivm, lp_type_int_vec(32, 128));
3514
3515 if (explicit_lod) {
3516 /* FIXME: this needs to honor per-element lod */
3517 lod = LLVMBuildExtractElement(gallivm->builder, explicit_lod,
3518 lp_build_const_int32(gallivm, 0), "");
3519 first_level = dynamic_state->first_level(dynamic_state, gallivm,
3520 context_ptr, texture_unit);
3521 level = LLVMBuildAdd(gallivm->builder, lod, first_level, "level");
3522 lod = lp_build_broadcast_scalar(&bld_int_vec4, level);
3523 } else {
3524 lod = bld_int_vec4.zero;
3525 }
3526
3527 size = bld_int_vec4.undef;
3528
3529 size = LLVMBuildInsertElement(gallivm->builder, size,
3530 dynamic_state->width(dynamic_state, gallivm,
3531 context_ptr, texture_unit),
3532 lp_build_const_int32(gallivm, 0), "");
3533
3534 if (dims >= 2) {
3535 size = LLVMBuildInsertElement(gallivm->builder, size,
3536 dynamic_state->height(dynamic_state, gallivm,
3537 context_ptr, texture_unit),
3538 lp_build_const_int32(gallivm, 1), "");
3539 }
3540
3541 if (dims >= 3) {
3542 size = LLVMBuildInsertElement(gallivm->builder, size,
3543 dynamic_state->depth(dynamic_state, gallivm,
3544 context_ptr, texture_unit),
3545 lp_build_const_int32(gallivm, 2), "");
3546 }
3547
3548 size = lp_build_minify(&bld_int_vec4, size, lod, TRUE);
3549
3550 if (has_array) {
3551 LLVMValueRef layers = dynamic_state->depth(dynamic_state, gallivm,
3552 context_ptr, texture_unit);
3553 if (target == PIPE_TEXTURE_CUBE_ARRAY) {
3554 /*
3555 * It looks like GL wants number of cubes, d3d10.1 has it undefined?
3556 * Could avoid this by passing in number of cubes instead of total
3557 * number of layers (might make things easier elsewhere too).
3558 */
3559 LLVMValueRef six = lp_build_const_int32(gallivm, 6);
3560 layers = LLVMBuildSDiv(gallivm->builder, layers, six, "");
3561 }
3562 size = LLVMBuildInsertElement(gallivm->builder, size, layers,
3563 lp_build_const_int32(gallivm, dims), "");
3564 }
3565
3566 /*
3567 * d3d10 requires zero for x/y/z values (but not w, i.e. mip levels)
3568 * if level is out of bounds (note this can't cover unbound texture
3569 * here, which also requires returning zero).
3570 */
3571 if (explicit_lod && is_sviewinfo) {
3572 LLVMValueRef last_level, out, out1;
3573 struct lp_build_context leveli_bld;
3574
3575 /* everything is scalar for now */
3576 lp_build_context_init(&leveli_bld, gallivm, lp_type_int_vec(32, 32));
3577 last_level = dynamic_state->last_level(dynamic_state, gallivm,
3578 context_ptr, texture_unit);
3579
3580 out = lp_build_cmp(&leveli_bld, PIPE_FUNC_LESS, level, first_level);
3581 out1 = lp_build_cmp(&leveli_bld, PIPE_FUNC_GREATER, level, last_level);
3582 out = lp_build_or(&leveli_bld, out, out1);
3583 if (num_lods == 1) {
3584 out = lp_build_broadcast_scalar(&bld_int_vec4, out);
3585 }
3586 else {
3587 /* TODO */
3588 assert(0);
3589 }
3590 size = lp_build_andnot(&bld_int_vec4, size, out);
3591 }
3592 for (i = 0; i < dims + (has_array ? 1 : 0); i++) {
3593 sizes_out[i] = lp_build_extract_broadcast(gallivm, bld_int_vec4.type, int_type,
3594 size,
3595 lp_build_const_int32(gallivm, i));
3596 }
3597 if (is_sviewinfo) {
3598 for (; i < 4; i++) {
3599 sizes_out[i] = lp_build_const_vec(gallivm, int_type, 0.0);
3600 }
3601 }
3602
3603 /*
3604 * if there's no explicit_lod (buffers, rects) queries requiring nr of
3605 * mips would be illegal.
3606 */
3607 if (is_sviewinfo && explicit_lod) {
3608 struct lp_build_context bld_int_scalar;
3609 LLVMValueRef num_levels;
3610 lp_build_context_init(&bld_int_scalar, gallivm, lp_type_int(32));
3611
3612 if (static_state->level_zero_only) {
3613 num_levels = bld_int_scalar.one;
3614 }
3615 else {
3616 LLVMValueRef last_level;
3617
3618 last_level = dynamic_state->last_level(dynamic_state, gallivm,
3619 context_ptr, texture_unit);
3620 num_levels = lp_build_sub(&bld_int_scalar, last_level, first_level);
3621 num_levels = lp_build_add(&bld_int_scalar, num_levels, bld_int_scalar.one);
3622 }
3623 sizes_out[3] = lp_build_broadcast(gallivm, lp_build_vec_type(gallivm, int_type),
3624 num_levels);
3625 }
3626 }