gallivm: implement seamless cube filtering
[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 texel_out);
165
166 /*
167 * Note: if we find an app which frequently samples the texture border
168 * we might want to implement a true conditional here to avoid sampling
169 * the texture whenever possible (since that's quite a bit of code).
170 * Ex:
171 * if (use_border) {
172 * texel = border_color;
173 * }
174 * else {
175 * texel = sample_texture(coord);
176 * }
177 * As it is now, we always sample the texture, then selectively replace
178 * the texel color results with the border color.
179 */
180
181 if (use_border) {
182 /* select texel color or border color depending on use_border. */
183 const struct util_format_description *format_desc = bld->format_desc;
184 int chan;
185 struct lp_type border_type = bld->texel_type;
186 border_type.length = 4;
187 /*
188 * Only replace channels which are actually present. The others should
189 * get optimized away eventually by sampler_view swizzle anyway but it's
190 * easier too.
191 */
192 for (chan = 0; chan < 4; chan++) {
193 unsigned chan_s;
194 /* reverse-map channel... */
195 for (chan_s = 0; chan_s < 4; chan_s++) {
196 if (chan_s == format_desc->swizzle[chan]) {
197 break;
198 }
199 }
200 if (chan_s <= 3) {
201 /* use the already clamped color */
202 LLVMValueRef idx = lp_build_const_int32(bld->gallivm, chan);
203 LLVMValueRef border_chan;
204
205 border_chan = lp_build_extract_broadcast(bld->gallivm,
206 border_type,
207 bld->texel_type,
208 bld->border_color_clamped,
209 idx);
210 texel_out[chan] = lp_build_select(&bld->texel_bld, use_border,
211 border_chan, texel_out[chan]);
212 }
213 }
214 }
215 }
216
217
218 /**
219 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
220 */
221 static LLVMValueRef
222 lp_build_coord_mirror(struct lp_build_sample_context *bld,
223 LLVMValueRef coord)
224 {
225 struct lp_build_context *coord_bld = &bld->coord_bld;
226 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
227 LLVMValueRef fract, flr, isOdd;
228
229 lp_build_ifloor_fract(coord_bld, coord, &flr, &fract);
230
231 /* isOdd = flr & 1 */
232 isOdd = LLVMBuildAnd(bld->gallivm->builder, flr, int_coord_bld->one, "");
233
234 /* make coord positive or negative depending on isOdd */
235 coord = lp_build_set_sign(coord_bld, fract, isOdd);
236
237 /* convert isOdd to float */
238 isOdd = lp_build_int_to_float(coord_bld, isOdd);
239
240 /* add isOdd to coord */
241 coord = lp_build_add(coord_bld, coord, isOdd);
242
243 return coord;
244 }
245
246
247 /**
248 * Helper to compute the first coord and the weight for
249 * linear wrap repeat npot textures
250 */
251 void
252 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context *bld,
253 LLVMValueRef coord_f,
254 LLVMValueRef length_i,
255 LLVMValueRef length_f,
256 LLVMValueRef *coord0_i,
257 LLVMValueRef *weight_f)
258 {
259 struct lp_build_context *coord_bld = &bld->coord_bld;
260 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
261 LLVMValueRef half = lp_build_const_vec(bld->gallivm, coord_bld->type, 0.5);
262 LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length_i,
263 int_coord_bld->one);
264 LLVMValueRef mask;
265 /* wrap with normalized floats is just fract */
266 coord_f = lp_build_fract(coord_bld, coord_f);
267 /* mul by size and subtract 0.5 */
268 coord_f = lp_build_mul(coord_bld, coord_f, length_f);
269 coord_f = lp_build_sub(coord_bld, coord_f, half);
270 /*
271 * we avoided the 0.5/length division before the repeat wrap,
272 * now need to fix up edge cases with selects
273 */
274 /* convert to int, compute lerp weight */
275 lp_build_ifloor_fract(coord_bld, coord_f, coord0_i, weight_f);
276 mask = lp_build_compare(int_coord_bld->gallivm, int_coord_bld->type,
277 PIPE_FUNC_LESS, *coord0_i, int_coord_bld->zero);
278 *coord0_i = lp_build_select(int_coord_bld, mask, length_minus_one, *coord0_i);
279 }
280
281
282 /**
283 * Build LLVM code for texture wrap mode for linear filtering.
284 * \param x0_out returns first integer texcoord
285 * \param x1_out returns second integer texcoord
286 * \param weight_out returns linear interpolation weight
287 */
288 static void
289 lp_build_sample_wrap_linear(struct lp_build_sample_context *bld,
290 LLVMValueRef coord,
291 LLVMValueRef length,
292 LLVMValueRef length_f,
293 LLVMValueRef offset,
294 boolean is_pot,
295 unsigned wrap_mode,
296 LLVMValueRef *x0_out,
297 LLVMValueRef *x1_out,
298 LLVMValueRef *weight_out)
299 {
300 struct lp_build_context *coord_bld = &bld->coord_bld;
301 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
302 LLVMBuilderRef builder = bld->gallivm->builder;
303 LLVMValueRef half = lp_build_const_vec(bld->gallivm, coord_bld->type, 0.5);
304 LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length, int_coord_bld->one);
305 LLVMValueRef coord0, coord1, weight;
306
307 switch(wrap_mode) {
308 case PIPE_TEX_WRAP_REPEAT:
309 if (is_pot) {
310 /* mul by size and subtract 0.5 */
311 coord = lp_build_mul(coord_bld, coord, length_f);
312 coord = lp_build_sub(coord_bld, coord, half);
313 if (offset) {
314 offset = lp_build_int_to_float(coord_bld, offset);
315 coord = lp_build_add(coord_bld, coord, offset);
316 }
317 /* convert to int, compute lerp weight */
318 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
319 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
320 /* repeat wrap */
321 coord0 = LLVMBuildAnd(builder, coord0, length_minus_one, "");
322 coord1 = LLVMBuildAnd(builder, coord1, length_minus_one, "");
323 }
324 else {
325 LLVMValueRef mask;
326 if (offset) {
327 offset = lp_build_int_to_float(coord_bld, offset);
328 offset = lp_build_div(coord_bld, offset, length_f);
329 coord = lp_build_add(coord_bld, coord, offset);
330 }
331 lp_build_coord_repeat_npot_linear(bld, coord,
332 length, length_f,
333 &coord0, &weight);
334 mask = lp_build_compare(int_coord_bld->gallivm, int_coord_bld->type,
335 PIPE_FUNC_NOTEQUAL, coord0, length_minus_one);
336 coord1 = LLVMBuildAnd(builder,
337 lp_build_add(int_coord_bld, coord0, int_coord_bld->one),
338 mask, "");
339 }
340 break;
341
342 case PIPE_TEX_WRAP_CLAMP:
343 if (bld->static_sampler_state->normalized_coords) {
344 /* scale coord to length */
345 coord = lp_build_mul(coord_bld, coord, length_f);
346 }
347 if (offset) {
348 offset = lp_build_int_to_float(coord_bld, offset);
349 coord = lp_build_add(coord_bld, coord, offset);
350 }
351
352 /* clamp to [0, length] */
353 coord = lp_build_clamp(coord_bld, coord, coord_bld->zero, length_f);
354
355 coord = lp_build_sub(coord_bld, coord, half);
356
357 /* convert to int, compute lerp weight */
358 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
359 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
360 break;
361
362 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
363 {
364 struct lp_build_context abs_coord_bld = bld->coord_bld;
365 abs_coord_bld.type.sign = FALSE;
366
367 if (bld->static_sampler_state->normalized_coords) {
368 /* mul by tex size */
369 coord = lp_build_mul(coord_bld, coord, length_f);
370 }
371 if (offset) {
372 offset = lp_build_int_to_float(coord_bld, offset);
373 coord = lp_build_add(coord_bld, coord, offset);
374 }
375
376 /* clamp to length max */
377 coord = lp_build_min(coord_bld, coord, length_f);
378 /* subtract 0.5 */
379 coord = lp_build_sub(coord_bld, coord, half);
380 /* clamp to [0, length - 0.5] */
381 coord = lp_build_max(coord_bld, coord, coord_bld->zero);
382 /* convert to int, compute lerp weight */
383 lp_build_ifloor_fract(&abs_coord_bld, coord, &coord0, &weight);
384 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
385 /* coord1 = min(coord1, length-1) */
386 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
387 break;
388 }
389
390 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
391 if (bld->static_sampler_state->normalized_coords) {
392 /* scale coord to length */
393 coord = lp_build_mul(coord_bld, coord, length_f);
394 }
395 if (offset) {
396 offset = lp_build_int_to_float(coord_bld, offset);
397 coord = lp_build_add(coord_bld, coord, offset);
398 }
399 /* was: clamp to [-0.5, length + 0.5], then sub 0.5 */
400 /* can skip clamp (though might not work for very large coord values */
401 coord = lp_build_sub(coord_bld, coord, half);
402 /* convert to int, compute lerp weight */
403 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
404 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
405 break;
406
407 case PIPE_TEX_WRAP_MIRROR_REPEAT:
408 /* compute mirror function */
409 coord = lp_build_coord_mirror(bld, coord);
410
411 /* scale coord to length */
412 coord = lp_build_mul(coord_bld, coord, length_f);
413 coord = lp_build_sub(coord_bld, coord, half);
414 if (offset) {
415 offset = lp_build_int_to_float(coord_bld, offset);
416 coord = lp_build_add(coord_bld, coord, offset);
417 }
418
419 /* convert to int, compute lerp weight */
420 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
421 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
422
423 /* coord0 = max(coord0, 0) */
424 coord0 = lp_build_max(int_coord_bld, coord0, int_coord_bld->zero);
425 /* coord1 = min(coord1, length-1) */
426 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
427 break;
428
429 case PIPE_TEX_WRAP_MIRROR_CLAMP:
430 if (bld->static_sampler_state->normalized_coords) {
431 /* scale coord to length */
432 coord = lp_build_mul(coord_bld, coord, length_f);
433 }
434 if (offset) {
435 offset = lp_build_int_to_float(coord_bld, offset);
436 coord = lp_build_add(coord_bld, coord, offset);
437 }
438 coord = lp_build_abs(coord_bld, coord);
439
440 /* clamp to [0, length] */
441 coord = lp_build_min(coord_bld, coord, length_f);
442
443 coord = lp_build_sub(coord_bld, coord, half);
444
445 /* convert to int, compute lerp weight */
446 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
447 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
448 break;
449
450 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
451 {
452 struct lp_build_context abs_coord_bld = bld->coord_bld;
453 abs_coord_bld.type.sign = FALSE;
454
455 if (bld->static_sampler_state->normalized_coords) {
456 /* scale coord to length */
457 coord = lp_build_mul(coord_bld, coord, length_f);
458 }
459 if (offset) {
460 offset = lp_build_int_to_float(coord_bld, offset);
461 coord = lp_build_add(coord_bld, coord, offset);
462 }
463 coord = lp_build_abs(coord_bld, coord);
464
465 /* clamp to length max */
466 coord = lp_build_min(coord_bld, coord, length_f);
467 /* subtract 0.5 */
468 coord = lp_build_sub(coord_bld, coord, half);
469 /* clamp to [0, length - 0.5] */
470 coord = lp_build_max(coord_bld, coord, coord_bld->zero);
471
472 /* convert to int, compute lerp weight */
473 lp_build_ifloor_fract(&abs_coord_bld, coord, &coord0, &weight);
474 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
475 /* coord1 = min(coord1, length-1) */
476 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
477 }
478 break;
479
480 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
481 {
482 if (bld->static_sampler_state->normalized_coords) {
483 /* scale coord to length */
484 coord = lp_build_mul(coord_bld, coord, length_f);
485 }
486 if (offset) {
487 offset = lp_build_int_to_float(coord_bld, offset);
488 coord = lp_build_add(coord_bld, coord, offset);
489 }
490 coord = lp_build_abs(coord_bld, coord);
491
492 /* was: clamp to [-0.5, length + 0.5] then sub 0.5 */
493 /* skip clamp - always positive, and other side
494 only potentially matters for very large coords */
495 coord = lp_build_sub(coord_bld, coord, half);
496
497 /* convert to int, compute lerp weight */
498 lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
499 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
500 }
501 break;
502
503 default:
504 assert(0);
505 coord0 = NULL;
506 coord1 = NULL;
507 weight = NULL;
508 }
509
510 *x0_out = coord0;
511 *x1_out = coord1;
512 *weight_out = weight;
513 }
514
515
516 /**
517 * Build LLVM code for texture wrap mode for nearest filtering.
518 * \param coord the incoming texcoord (nominally in [0,1])
519 * \param length the texture size along one dimension, as int vector
520 * \param length_f the texture size along one dimension, as float vector
521 * \param offset texel offset along one dimension (as int vector)
522 * \param is_pot if TRUE, length is a power of two
523 * \param wrap_mode one of PIPE_TEX_WRAP_x
524 */
525 static LLVMValueRef
526 lp_build_sample_wrap_nearest(struct lp_build_sample_context *bld,
527 LLVMValueRef coord,
528 LLVMValueRef length,
529 LLVMValueRef length_f,
530 LLVMValueRef offset,
531 boolean is_pot,
532 unsigned wrap_mode)
533 {
534 struct lp_build_context *coord_bld = &bld->coord_bld;
535 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
536 LLVMBuilderRef builder = bld->gallivm->builder;
537 LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length, int_coord_bld->one);
538 LLVMValueRef icoord;
539
540 switch(wrap_mode) {
541 case PIPE_TEX_WRAP_REPEAT:
542 if (is_pot) {
543 coord = lp_build_mul(coord_bld, coord, length_f);
544 icoord = lp_build_ifloor(coord_bld, coord);
545 if (offset) {
546 icoord = lp_build_add(int_coord_bld, icoord, offset);
547 }
548 icoord = LLVMBuildAnd(builder, icoord, length_minus_one, "");
549 }
550 else {
551 if (offset) {
552 offset = lp_build_int_to_float(coord_bld, offset);
553 offset = lp_build_div(coord_bld, offset, length_f);
554 coord = lp_build_add(coord_bld, coord, offset);
555 }
556 /* take fraction, unnormalize */
557 coord = lp_build_fract_safe(coord_bld, coord);
558 coord = lp_build_mul(coord_bld, coord, length_f);
559 icoord = lp_build_itrunc(coord_bld, coord);
560 }
561 break;
562
563 case PIPE_TEX_WRAP_CLAMP:
564 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
565 if (bld->static_sampler_state->normalized_coords) {
566 /* scale coord to length */
567 coord = lp_build_mul(coord_bld, coord, length_f);
568 }
569
570 /* floor */
571 /* use itrunc instead since we clamp to 0 anyway */
572 icoord = lp_build_itrunc(coord_bld, coord);
573 if (offset) {
574 icoord = lp_build_add(int_coord_bld, icoord, offset);
575 }
576
577 /* clamp to [0, length - 1]. */
578 icoord = lp_build_clamp(int_coord_bld, icoord, int_coord_bld->zero,
579 length_minus_one);
580 break;
581
582 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
583 if (bld->static_sampler_state->normalized_coords) {
584 /* scale coord to length */
585 coord = lp_build_mul(coord_bld, coord, length_f);
586 }
587 /* no clamp necessary, border masking will handle this */
588 icoord = lp_build_ifloor(coord_bld, coord);
589 if (offset) {
590 icoord = lp_build_add(int_coord_bld, icoord, offset);
591 }
592 break;
593
594 case PIPE_TEX_WRAP_MIRROR_REPEAT:
595 if (offset) {
596 offset = lp_build_int_to_float(coord_bld, offset);
597 offset = lp_build_div(coord_bld, offset, length_f);
598 coord = lp_build_add(coord_bld, coord, offset);
599 }
600 /* compute mirror function */
601 coord = lp_build_coord_mirror(bld, coord);
602
603 /* scale coord to length */
604 assert(bld->static_sampler_state->normalized_coords);
605 coord = lp_build_mul(coord_bld, coord, length_f);
606
607 /* itrunc == ifloor here */
608 icoord = lp_build_itrunc(coord_bld, coord);
609
610 /* clamp to [0, length - 1] */
611 icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
612 break;
613
614 case PIPE_TEX_WRAP_MIRROR_CLAMP:
615 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
616 if (bld->static_sampler_state->normalized_coords) {
617 /* scale coord to length */
618 coord = lp_build_mul(coord_bld, coord, length_f);
619 }
620 if (offset) {
621 offset = lp_build_int_to_float(coord_bld, offset);
622 coord = lp_build_add(coord_bld, coord, offset);
623 }
624 coord = lp_build_abs(coord_bld, coord);
625
626 /* itrunc == ifloor here */
627 icoord = lp_build_itrunc(coord_bld, coord);
628
629 /* clamp to [0, length - 1] */
630 icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
631 break;
632
633 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
634 if (bld->static_sampler_state->normalized_coords) {
635 /* scale coord to length */
636 coord = lp_build_mul(coord_bld, coord, length_f);
637 }
638 if (offset) {
639 offset = lp_build_int_to_float(coord_bld, offset);
640 coord = lp_build_add(coord_bld, coord, offset);
641 }
642 coord = lp_build_abs(coord_bld, coord);
643
644 /* itrunc == ifloor here */
645 icoord = lp_build_itrunc(coord_bld, coord);
646 break;
647
648 default:
649 assert(0);
650 icoord = NULL;
651 }
652
653 return icoord;
654 }
655
656
657 /**
658 * Do shadow test/comparison.
659 * \param p shadow ref value
660 * \param texel the texel to compare against
661 */
662 static LLVMValueRef
663 lp_build_sample_comparefunc(struct lp_build_sample_context *bld,
664 LLVMValueRef p,
665 LLVMValueRef texel)
666 {
667 struct lp_build_context *texel_bld = &bld->texel_bld;
668 LLVMValueRef res;
669
670 if (0) {
671 //lp_build_print_value(bld->gallivm, "shadow cmp coord", p);
672 lp_build_print_value(bld->gallivm, "shadow cmp texel", texel);
673 }
674
675 /* result = (p FUNC texel) ? 1 : 0 */
676 /*
677 * honor d3d10 floating point rules here, which state that comparisons
678 * are ordered except NOT_EQUAL which is unordered.
679 */
680 if (bld->static_sampler_state->compare_func != PIPE_FUNC_NOTEQUAL) {
681 res = lp_build_cmp_ordered(texel_bld, bld->static_sampler_state->compare_func,
682 p, texel);
683 }
684 else {
685 res = lp_build_cmp(texel_bld, bld->static_sampler_state->compare_func,
686 p, texel);
687 }
688 return res;
689 }
690
691
692 /**
693 * Generate code to sample a mipmap level with nearest filtering.
694 * If sampling a cube texture, r = cube face in [0,5].
695 */
696 static void
697 lp_build_sample_image_nearest(struct lp_build_sample_context *bld,
698 LLVMValueRef size,
699 LLVMValueRef row_stride_vec,
700 LLVMValueRef img_stride_vec,
701 LLVMValueRef data_ptr,
702 LLVMValueRef mipoffsets,
703 LLVMValueRef *coords,
704 const LLVMValueRef *offsets,
705 LLVMValueRef colors_out[4])
706 {
707 const unsigned dims = bld->dims;
708 LLVMValueRef width_vec;
709 LLVMValueRef height_vec;
710 LLVMValueRef depth_vec;
711 LLVMValueRef flt_size;
712 LLVMValueRef flt_width_vec;
713 LLVMValueRef flt_height_vec;
714 LLVMValueRef flt_depth_vec;
715 LLVMValueRef x, y = NULL, z = NULL;
716
717 lp_build_extract_image_sizes(bld,
718 &bld->int_size_bld,
719 bld->int_coord_type,
720 size,
721 &width_vec, &height_vec, &depth_vec);
722
723 flt_size = lp_build_int_to_float(&bld->float_size_bld, size);
724
725 lp_build_extract_image_sizes(bld,
726 &bld->float_size_bld,
727 bld->coord_type,
728 flt_size,
729 &flt_width_vec, &flt_height_vec, &flt_depth_vec);
730
731 /*
732 * Compute integer texcoords.
733 */
734 x = lp_build_sample_wrap_nearest(bld, coords[0], width_vec,
735 flt_width_vec, offsets[0],
736 bld->static_texture_state->pot_width,
737 bld->static_sampler_state->wrap_s);
738 lp_build_name(x, "tex.x.wrapped");
739
740 if (dims >= 2) {
741 y = lp_build_sample_wrap_nearest(bld, coords[1], height_vec,
742 flt_height_vec, offsets[1],
743 bld->static_texture_state->pot_height,
744 bld->static_sampler_state->wrap_t);
745 lp_build_name(y, "tex.y.wrapped");
746
747 if (dims == 3) {
748 z = lp_build_sample_wrap_nearest(bld, coords[2], depth_vec,
749 flt_depth_vec, offsets[2],
750 bld->static_texture_state->pot_depth,
751 bld->static_sampler_state->wrap_r);
752 lp_build_name(z, "tex.z.wrapped");
753 }
754 }
755 if (bld->static_texture_state->target == PIPE_TEXTURE_CUBE ||
756 bld->static_texture_state->target == PIPE_TEXTURE_1D_ARRAY ||
757 bld->static_texture_state->target == PIPE_TEXTURE_2D_ARRAY) {
758 z = coords[2];
759 lp_build_name(z, "tex.z.layer");
760 }
761
762 /*
763 * Get texture colors.
764 */
765 lp_build_sample_texel_soa(bld,
766 width_vec, height_vec, depth_vec,
767 x, y, z,
768 row_stride_vec, img_stride_vec,
769 data_ptr, mipoffsets, colors_out);
770
771 if (bld->static_sampler_state->compare_mode != PIPE_TEX_COMPARE_NONE) {
772 LLVMValueRef cmpval;
773 cmpval = lp_build_sample_comparefunc(bld, coords[4], colors_out[0]);
774 /* this is really just a AND 1.0, cmpval but llvm is clever enough */
775 colors_out[0] = lp_build_select(&bld->texel_bld, cmpval,
776 bld->texel_bld.one, bld->texel_bld.zero);
777 colors_out[1] = colors_out[2] = colors_out[3] = colors_out[0];
778 }
779
780 }
781
782
783 /**
784 * Like a lerp, but inputs are 0/~0 masks, so can simplify slightly.
785 */
786 static LLVMValueRef
787 lp_build_masklerp(struct lp_build_context *bld,
788 LLVMValueRef weight,
789 LLVMValueRef mask0,
790 LLVMValueRef mask1)
791 {
792 struct gallivm_state *gallivm = bld->gallivm;
793 LLVMBuilderRef builder = gallivm->builder;
794 LLVMValueRef weight2;
795
796 weight2 = lp_build_sub(bld, bld->one, weight);
797 weight = LLVMBuildBitCast(builder, weight,
798 lp_build_int_vec_type(gallivm, bld->type), "");
799 weight2 = LLVMBuildBitCast(builder, weight2,
800 lp_build_int_vec_type(gallivm, bld->type), "");
801 weight = LLVMBuildAnd(builder, weight, mask1, "");
802 weight2 = LLVMBuildAnd(builder, weight2, mask0, "");
803 weight = LLVMBuildBitCast(builder, weight, bld->vec_type, "");
804 weight2 = LLVMBuildBitCast(builder, weight2, bld->vec_type, "");
805 return lp_build_add(bld, weight, weight2);
806 }
807
808 /**
809 * Like a 2d lerp, but inputs are 0/~0 masks, so can simplify slightly.
810 */
811 static LLVMValueRef
812 lp_build_masklerp2d(struct lp_build_context *bld,
813 LLVMValueRef weight0,
814 LLVMValueRef weight1,
815 LLVMValueRef mask00,
816 LLVMValueRef mask01,
817 LLVMValueRef mask10,
818 LLVMValueRef mask11)
819 {
820 LLVMValueRef val0 = lp_build_masklerp(bld, weight0, mask00, mask01);
821 LLVMValueRef val1 = lp_build_masklerp(bld, weight0, mask10, mask11);
822 return lp_build_lerp(bld, weight1, val0, val1, 0);
823 }
824
825 /**
826 * Generate code to sample a mipmap level with linear filtering.
827 * If sampling a cube texture, r = cube face in [0,5].
828 * If linear_mask is present, only pixels having their mask set
829 * will receive linear filtering, the rest will use nearest.
830 */
831 static void
832 lp_build_sample_image_linear(struct lp_build_sample_context *bld,
833 LLVMValueRef size,
834 LLVMValueRef linear_mask,
835 LLVMValueRef row_stride_vec,
836 LLVMValueRef img_stride_vec,
837 LLVMValueRef data_ptr,
838 LLVMValueRef mipoffsets,
839 LLVMValueRef *coords,
840 const LLVMValueRef *offsets,
841 LLVMValueRef colors_out[4])
842 {
843 const unsigned dims = bld->dims;
844 LLVMValueRef width_vec;
845 LLVMValueRef height_vec;
846 LLVMValueRef depth_vec;
847 LLVMValueRef flt_size;
848 LLVMValueRef flt_width_vec;
849 LLVMValueRef flt_height_vec;
850 LLVMValueRef flt_depth_vec;
851 LLVMValueRef z1 = NULL;
852 LLVMValueRef z00 = NULL, z01 = NULL, z10 = NULL, z11 = NULL;
853 LLVMValueRef x00 = NULL, x01 = NULL, x10 = NULL, x11 = NULL;
854 LLVMValueRef y00 = NULL, y01 = NULL, y10 = NULL, y11 = NULL;
855 LLVMValueRef s_fpart, t_fpart = NULL, r_fpart = NULL;
856 LLVMValueRef xs[4], ys[4], zs[4];
857 LLVMValueRef neighbors[2][2][4];
858 int chan, texel_index;
859
860 lp_build_extract_image_sizes(bld,
861 &bld->int_size_bld,
862 bld->int_coord_type,
863 size,
864 &width_vec, &height_vec, &depth_vec);
865
866 flt_size = lp_build_int_to_float(&bld->float_size_bld, size);
867
868 lp_build_extract_image_sizes(bld,
869 &bld->float_size_bld,
870 bld->coord_type,
871 flt_size,
872 &flt_width_vec, &flt_height_vec, &flt_depth_vec);
873
874 /*
875 * Compute integer texcoords.
876 */
877
878 if (bld->static_texture_state->target != PIPE_TEXTURE_CUBE ||
879 !bld->static_sampler_state->seamless_cube_map) {
880 lp_build_sample_wrap_linear(bld, coords[0], width_vec,
881 flt_width_vec, offsets[0],
882 bld->static_texture_state->pot_width,
883 bld->static_sampler_state->wrap_s,
884 &x00, &x01, &s_fpart);
885 lp_build_name(x00, "tex.x0.wrapped");
886 lp_build_name(x01, "tex.x1.wrapped");
887 x10 = x00;
888 x11 = x01;
889
890 if (dims >= 2) {
891 lp_build_sample_wrap_linear(bld, coords[1], height_vec,
892 flt_height_vec, offsets[1],
893 bld->static_texture_state->pot_height,
894 bld->static_sampler_state->wrap_t,
895 &y00, &y10, &t_fpart);
896 lp_build_name(y00, "tex.y0.wrapped");
897 lp_build_name(y10, "tex.y1.wrapped");
898 y01 = y00;
899 y11 = y10;
900
901 if (dims == 3) {
902 lp_build_sample_wrap_linear(bld, coords[2], depth_vec,
903 flt_depth_vec, offsets[2],
904 bld->static_texture_state->pot_depth,
905 bld->static_sampler_state->wrap_r,
906 &z00, &z1, &r_fpart);
907 z01 = z10 = z11 = z00;
908 lp_build_name(z00, "tex.z0.wrapped");
909 lp_build_name(z1, "tex.z1.wrapped");
910 }
911 }
912 if (bld->static_texture_state->target == PIPE_TEXTURE_CUBE ||
913 bld->static_texture_state->target == PIPE_TEXTURE_1D_ARRAY ||
914 bld->static_texture_state->target == PIPE_TEXTURE_2D_ARRAY) {
915 z00 = z01 = z10 = z11 = z1 = coords[2]; /* cube face or layer */
916 lp_build_name(z00, "tex.z0.layer");
917 lp_build_name(z1, "tex.z1.layer");
918 }
919 }
920 else {
921 LLVMBuilderRef builder = bld->gallivm->builder;
922 struct lp_build_context *ivec_bld = &bld->int_coord_bld;
923 struct lp_build_context *coord_bld = &bld->coord_bld;
924 struct lp_build_if_state edge_if;
925 LLVMValueRef new_faces[4], new_xcoords[4][2], new_ycoords[4][2];
926 LLVMValueRef fall_off[4], coord, have_edge;
927 LLVMValueRef fall_off_ym_notxm, fall_off_ym_notxp;
928 LLVMValueRef fall_off_yp_notxm, fall_off_yp_notxp;
929 LLVMValueRef x0, x1, y0, y1, y0_clamped, y1_clamped;
930 LLVMValueRef face = coords[2];
931 LLVMValueRef half = lp_build_const_vec(bld->gallivm, coord_bld->type, 0.5f);
932 LLVMValueRef length_minus_one = lp_build_sub(ivec_bld, width_vec, ivec_bld->one);
933 /* XXX drop height calcs. Could (should) do this without seamless filtering too */
934 height_vec = width_vec;
935 flt_height_vec = flt_width_vec;
936
937 /* XXX the overflow logic is actually sort of duplicated with trilinear,
938 * since an overflow in one mip should also have a corresponding overflow
939 * in another.
940 */
941 /* should always have normalized coords, and offsets are undefined */
942 assert(bld->static_sampler_state->normalized_coords);
943 coord = lp_build_mul(coord_bld, coords[0], flt_width_vec);
944 /* instead of clamp, build mask if overflowed */
945 coord = lp_build_sub(coord_bld, coord, half);
946 /* convert to int, compute lerp weight */
947 /* not ideal with AVX (and no AVX2) */
948 lp_build_ifloor_fract(coord_bld, coord, &x0, &s_fpart);
949 x1 = lp_build_add(ivec_bld, x0, ivec_bld->one);
950 coord = lp_build_mul(coord_bld, coords[1], flt_height_vec);
951 coord = lp_build_sub(coord_bld, coord, half);
952 lp_build_ifloor_fract(coord_bld, coord, &y0, &t_fpart);
953 y1 = lp_build_add(ivec_bld, y0, ivec_bld->one);
954
955 fall_off[0] = lp_build_cmp(ivec_bld, PIPE_FUNC_LESS, x0, ivec_bld->zero);
956 fall_off[1] = lp_build_cmp(ivec_bld, PIPE_FUNC_GREATER, x1, length_minus_one);
957 fall_off[2] = lp_build_cmp(ivec_bld, PIPE_FUNC_LESS, y0, ivec_bld->zero);
958 fall_off[3] = lp_build_cmp(ivec_bld, PIPE_FUNC_GREATER, y1, length_minus_one);
959
960 have_edge = lp_build_or(ivec_bld, fall_off[0], fall_off[1]);
961 have_edge = lp_build_or(ivec_bld, have_edge, fall_off[2]);
962 have_edge = lp_build_or(ivec_bld, have_edge, fall_off[3]);
963
964 have_edge = lp_build_any_true_range(ivec_bld, ivec_bld->type.length, have_edge);
965
966 for (texel_index = 0; texel_index < 4; texel_index++) {
967 xs[texel_index] = lp_build_alloca(bld->gallivm, ivec_bld->vec_type, "xs");
968 ys[texel_index] = lp_build_alloca(bld->gallivm, ivec_bld->vec_type, "ys");
969 zs[texel_index] = lp_build_alloca(bld->gallivm, ivec_bld->vec_type, "zs");
970 }
971
972 lp_build_if(&edge_if, bld->gallivm, have_edge);
973
974 /*
975 * Need to feed clamped values here for cheap corner handling,
976 * but only for y coord (as when falling off both edges we only
977 * fall off the x one) - this should be sufficient.
978 */
979 y0_clamped = lp_build_max(ivec_bld, y0, ivec_bld->zero);
980 y1_clamped = lp_build_min(ivec_bld, y1, length_minus_one);
981
982 /*
983 * Get all possible new coords.
984 */
985 lp_build_cube_new_coords(ivec_bld, face,
986 x0, x1, y0_clamped, y1_clamped,
987 length_minus_one,
988 new_faces, new_xcoords, new_ycoords);
989
990 /* handle fall off x-, x+ direction */
991 /* determine new coords, face (not both fall_off vars can be true at same time) */
992 x00 = lp_build_select(ivec_bld, fall_off[0], new_xcoords[0][0], x0);
993 y00 = lp_build_select(ivec_bld, fall_off[0], new_ycoords[0][0], y0_clamped);
994 x10 = lp_build_select(ivec_bld, fall_off[0], new_xcoords[0][1], x0);
995 y10 = lp_build_select(ivec_bld, fall_off[0], new_ycoords[0][1], y1_clamped);
996 x01 = lp_build_select(ivec_bld, fall_off[1], new_xcoords[1][0], x1);
997 y01 = lp_build_select(ivec_bld, fall_off[1], new_ycoords[1][0], y0_clamped);
998 x11 = lp_build_select(ivec_bld, fall_off[1], new_xcoords[1][1], x1);
999 y11 = lp_build_select(ivec_bld, fall_off[1], new_ycoords[1][1], y1_clamped);
1000
1001 z00 = z10 = lp_build_select(ivec_bld, fall_off[0], new_faces[0], face);
1002 z01 = z11 = lp_build_select(ivec_bld, fall_off[1], new_faces[1], face);
1003
1004 /* handle fall off y-, y+ direction */
1005 /*
1006 * Cheap corner logic: just hack up things so a texel doesn't fall
1007 * off both sides (which means filter weights will be wrong but we'll only
1008 * use valid texels in the filter).
1009 * This means however (y) coords must additionally be clamped (see above).
1010 * This corner handling should be fully OpenGL (but not d3d10) compliant.
1011 */
1012 fall_off_ym_notxm = lp_build_andnot(ivec_bld, fall_off[2], fall_off[0]);
1013 fall_off_ym_notxp = lp_build_andnot(ivec_bld, fall_off[2], fall_off[1]);
1014 fall_off_yp_notxm = lp_build_andnot(ivec_bld, fall_off[3], fall_off[0]);
1015 fall_off_yp_notxp = lp_build_andnot(ivec_bld, fall_off[3], fall_off[1]);
1016
1017 x00 = lp_build_select(ivec_bld, fall_off_ym_notxm, new_xcoords[2][0], x00);
1018 y00 = lp_build_select(ivec_bld, fall_off_ym_notxm, new_ycoords[2][0], y00);
1019 x01 = lp_build_select(ivec_bld, fall_off_ym_notxp, new_xcoords[2][1], x01);
1020 y01 = lp_build_select(ivec_bld, fall_off_ym_notxp, new_ycoords[2][1], y01);
1021 x10 = lp_build_select(ivec_bld, fall_off_yp_notxm, new_xcoords[3][0], x10);
1022 y10 = lp_build_select(ivec_bld, fall_off_yp_notxm, new_ycoords[3][0], y10);
1023 x11 = lp_build_select(ivec_bld, fall_off_yp_notxp, new_xcoords[3][1], x11);
1024 y11 = lp_build_select(ivec_bld, fall_off_yp_notxp, new_ycoords[3][1], y11);
1025
1026 z00 = lp_build_select(ivec_bld, fall_off_ym_notxm, new_faces[2], z00);
1027 z01 = lp_build_select(ivec_bld, fall_off_ym_notxp, new_faces[2], z01);
1028 z10 = lp_build_select(ivec_bld, fall_off_yp_notxm, new_faces[3], z10);
1029 z11 = lp_build_select(ivec_bld, fall_off_yp_notxp, new_faces[3], z11);
1030
1031 LLVMBuildStore(builder, x00, xs[0]);
1032 LLVMBuildStore(builder, x01, xs[1]);
1033 LLVMBuildStore(builder, x10, xs[2]);
1034 LLVMBuildStore(builder, x11, xs[3]);
1035 LLVMBuildStore(builder, y00, ys[0]);
1036 LLVMBuildStore(builder, y01, ys[1]);
1037 LLVMBuildStore(builder, y10, ys[2]);
1038 LLVMBuildStore(builder, y11, ys[3]);
1039 LLVMBuildStore(builder, z00, zs[0]);
1040 LLVMBuildStore(builder, z01, zs[1]);
1041 LLVMBuildStore(builder, z10, zs[2]);
1042 LLVMBuildStore(builder, z11, zs[3]);
1043
1044 lp_build_else(&edge_if);
1045
1046 LLVMBuildStore(builder, x0, xs[0]);
1047 LLVMBuildStore(builder, x1, xs[1]);
1048 LLVMBuildStore(builder, x0, xs[2]);
1049 LLVMBuildStore(builder, x1, xs[3]);
1050 LLVMBuildStore(builder, y0, ys[0]);
1051 LLVMBuildStore(builder, y0, ys[1]);
1052 LLVMBuildStore(builder, y1, ys[2]);
1053 LLVMBuildStore(builder, y1, ys[3]);
1054 LLVMBuildStore(builder, face, zs[0]);
1055 LLVMBuildStore(builder, face, zs[1]);
1056 LLVMBuildStore(builder, face, zs[2]);
1057 LLVMBuildStore(builder, face, zs[3]);
1058
1059 lp_build_endif(&edge_if);
1060
1061 x00 = LLVMBuildLoad(builder, xs[0], "");
1062 x01 = LLVMBuildLoad(builder, xs[1], "");
1063 x10 = LLVMBuildLoad(builder, xs[2], "");
1064 x11 = LLVMBuildLoad(builder, xs[3], "");
1065 y00 = LLVMBuildLoad(builder, ys[0], "");
1066 y01 = LLVMBuildLoad(builder, ys[1], "");
1067 y10 = LLVMBuildLoad(builder, ys[2], "");
1068 y11 = LLVMBuildLoad(builder, ys[3], "");
1069 z00 = LLVMBuildLoad(builder, zs[0], "");
1070 z01 = LLVMBuildLoad(builder, zs[1], "");
1071 z10 = LLVMBuildLoad(builder, zs[2], "");
1072 z11 = LLVMBuildLoad(builder, zs[3], "");
1073 }
1074
1075 if (linear_mask) {
1076 /*
1077 * Whack filter weights into place. Whatever pixel had more weight is
1078 * the one which should have been selected by nearest filtering hence
1079 * just use 100% weight for it.
1080 */
1081 struct lp_build_context *c_bld = &bld->coord_bld;
1082 LLVMValueRef w1_mask, w1_weight;
1083 LLVMValueRef half = lp_build_const_vec(bld->gallivm, c_bld->type, 0.5f);
1084
1085 w1_mask = lp_build_cmp(c_bld, PIPE_FUNC_GREATER, s_fpart, half);
1086 /* this select is really just a "and" */
1087 w1_weight = lp_build_select(c_bld, w1_mask, c_bld->one, c_bld->zero);
1088 s_fpart = lp_build_select(c_bld, linear_mask, s_fpart, w1_weight);
1089 if (dims >= 2) {
1090 w1_mask = lp_build_cmp(c_bld, PIPE_FUNC_GREATER, t_fpart, half);
1091 w1_weight = lp_build_select(c_bld, w1_mask, c_bld->one, c_bld->zero);
1092 t_fpart = lp_build_select(c_bld, linear_mask, t_fpart, w1_weight);
1093 if (dims == 3) {
1094 w1_mask = lp_build_cmp(c_bld, PIPE_FUNC_GREATER, r_fpart, half);
1095 w1_weight = lp_build_select(c_bld, w1_mask, c_bld->one, c_bld->zero);
1096 r_fpart = lp_build_select(c_bld, linear_mask, r_fpart, w1_weight);
1097 }
1098 }
1099 }
1100
1101 /*
1102 * Get texture colors.
1103 */
1104 /* get x0/x1 texels */
1105 lp_build_sample_texel_soa(bld,
1106 width_vec, height_vec, depth_vec,
1107 x00, y00, z00,
1108 row_stride_vec, img_stride_vec,
1109 data_ptr, mipoffsets, neighbors[0][0]);
1110 lp_build_sample_texel_soa(bld,
1111 width_vec, height_vec, depth_vec,
1112 x01, y01, z01,
1113 row_stride_vec, img_stride_vec,
1114 data_ptr, mipoffsets, neighbors[0][1]);
1115
1116 if (dims == 1) {
1117 if (bld->static_sampler_state->compare_mode == PIPE_TEX_COMPARE_NONE) {
1118 /* Interpolate two samples from 1D image to produce one color */
1119 for (chan = 0; chan < 4; chan++) {
1120 colors_out[chan] = lp_build_lerp(&bld->texel_bld, s_fpart,
1121 neighbors[0][0][chan],
1122 neighbors[0][1][chan],
1123 0);
1124 }
1125 }
1126 else {
1127 LLVMValueRef cmpval0, cmpval1;
1128 cmpval0 = lp_build_sample_comparefunc(bld, coords[4], neighbors[0][0][0]);
1129 cmpval1 = lp_build_sample_comparefunc(bld, coords[4], neighbors[0][1][0]);
1130 /* simplified lerp, AND mask with weight and add */
1131 colors_out[0] = lp_build_masklerp(&bld->texel_bld, s_fpart,
1132 cmpval0, cmpval1);
1133 colors_out[1] = colors_out[2] = colors_out[3] = colors_out[0];
1134 }
1135 }
1136 else {
1137 /* 2D/3D texture */
1138 LLVMValueRef colors0[4];
1139
1140 /* get x0/x1 texels at y1 */
1141 lp_build_sample_texel_soa(bld,
1142 width_vec, height_vec, depth_vec,
1143 x10, y10, z10,
1144 row_stride_vec, img_stride_vec,
1145 data_ptr, mipoffsets, neighbors[1][0]);
1146 lp_build_sample_texel_soa(bld,
1147 width_vec, height_vec, depth_vec,
1148 x11, y11, z11,
1149 row_stride_vec, img_stride_vec,
1150 data_ptr, mipoffsets, neighbors[1][1]);
1151
1152 if (bld->static_sampler_state->compare_mode == PIPE_TEX_COMPARE_NONE) {
1153 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
1154 for (chan = 0; chan < 4; chan++) {
1155 colors0[chan] = lp_build_lerp_2d(&bld->texel_bld,
1156 s_fpart, t_fpart,
1157 neighbors[0][0][chan],
1158 neighbors[0][1][chan],
1159 neighbors[1][0][chan],
1160 neighbors[1][1][chan],
1161 0);
1162 }
1163 }
1164 else {
1165 LLVMValueRef cmpval00, cmpval01, cmpval10, cmpval11;
1166 cmpval00 = lp_build_sample_comparefunc(bld, coords[4], neighbors[0][0][0]);
1167 cmpval01 = lp_build_sample_comparefunc(bld, coords[4], neighbors[0][1][0]);
1168 cmpval10 = lp_build_sample_comparefunc(bld, coords[4], neighbors[1][0][0]);
1169 cmpval11 = lp_build_sample_comparefunc(bld, coords[4], neighbors[1][1][0]);
1170 colors0[0] = lp_build_masklerp2d(&bld->texel_bld, s_fpart, t_fpart,
1171 cmpval00, cmpval01, cmpval10, cmpval11);
1172 colors0[1] = colors0[2] = colors0[3] = colors0[0];
1173 }
1174
1175 if (dims == 3) {
1176 LLVMValueRef neighbors1[2][2][4];
1177 LLVMValueRef colors1[4];
1178
1179 /* get x0/x1/y0/y1 texels at z1 */
1180 lp_build_sample_texel_soa(bld,
1181 width_vec, height_vec, depth_vec,
1182 x00, y00, z1,
1183 row_stride_vec, img_stride_vec,
1184 data_ptr, mipoffsets, neighbors1[0][0]);
1185 lp_build_sample_texel_soa(bld,
1186 width_vec, height_vec, depth_vec,
1187 x01, y01, z1,
1188 row_stride_vec, img_stride_vec,
1189 data_ptr, mipoffsets, neighbors1[0][1]);
1190 lp_build_sample_texel_soa(bld,
1191 width_vec, height_vec, depth_vec,
1192 x10, y10, z1,
1193 row_stride_vec, img_stride_vec,
1194 data_ptr, mipoffsets, neighbors1[1][0]);
1195 lp_build_sample_texel_soa(bld,
1196 width_vec, height_vec, depth_vec,
1197 x11, y11, z1,
1198 row_stride_vec, img_stride_vec,
1199 data_ptr, mipoffsets, neighbors1[1][1]);
1200
1201 if (bld->static_sampler_state->compare_mode == PIPE_TEX_COMPARE_NONE) {
1202 /* Bilinear interpolate the four samples from the second Z slice */
1203 for (chan = 0; chan < 4; chan++) {
1204 colors1[chan] = lp_build_lerp_2d(&bld->texel_bld,
1205 s_fpart, t_fpart,
1206 neighbors1[0][0][chan],
1207 neighbors1[0][1][chan],
1208 neighbors1[1][0][chan],
1209 neighbors1[1][1][chan],
1210 0);
1211 }
1212 /* Linearly interpolate the two samples from the two 3D slices */
1213 for (chan = 0; chan < 4; chan++) {
1214 colors_out[chan] = lp_build_lerp(&bld->texel_bld,
1215 r_fpart,
1216 colors0[chan], colors1[chan],
1217 0);
1218 }
1219 }
1220 else {
1221 LLVMValueRef cmpval00, cmpval01, cmpval10, cmpval11;
1222 cmpval00 = lp_build_sample_comparefunc(bld, coords[4], neighbors[0][0][0]);
1223 cmpval01 = lp_build_sample_comparefunc(bld, coords[4], neighbors[0][1][0]);
1224 cmpval10 = lp_build_sample_comparefunc(bld, coords[4], neighbors[1][0][0]);
1225 cmpval11 = lp_build_sample_comparefunc(bld, coords[4], neighbors[1][1][0]);
1226 colors1[0] = lp_build_masklerp2d(&bld->texel_bld, s_fpart, t_fpart,
1227 cmpval00, cmpval01, cmpval10, cmpval11);
1228 /* Linearly interpolate the two samples from the two 3D slices */
1229 colors_out[0] = lp_build_lerp(&bld->texel_bld,
1230 r_fpart,
1231 colors0[0], colors1[0],
1232 0);
1233 colors_out[1] = colors_out[2] = colors_out[3] = colors_out[0];
1234 }
1235 }
1236 else {
1237 /* 2D tex */
1238 for (chan = 0; chan < 4; chan++) {
1239 colors_out[chan] = colors0[chan];
1240 }
1241 }
1242 }
1243 }
1244
1245
1246 /**
1247 * Sample the texture/mipmap using given image filter and mip filter.
1248 * ilevel0 and ilevel1 indicate the two mipmap levels to sample
1249 * from (vectors or scalars).
1250 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1251 */
1252 static void
1253 lp_build_sample_mipmap(struct lp_build_sample_context *bld,
1254 unsigned img_filter,
1255 unsigned mip_filter,
1256 LLVMValueRef *coords,
1257 const LLVMValueRef *offsets,
1258 LLVMValueRef ilevel0,
1259 LLVMValueRef ilevel1,
1260 LLVMValueRef lod_fpart,
1261 LLVMValueRef *colors_out)
1262 {
1263 LLVMBuilderRef builder = bld->gallivm->builder;
1264 LLVMValueRef size0 = NULL;
1265 LLVMValueRef size1 = NULL;
1266 LLVMValueRef row_stride0_vec = NULL;
1267 LLVMValueRef row_stride1_vec = NULL;
1268 LLVMValueRef img_stride0_vec = NULL;
1269 LLVMValueRef img_stride1_vec = NULL;
1270 LLVMValueRef data_ptr0 = NULL;
1271 LLVMValueRef data_ptr1 = NULL;
1272 LLVMValueRef mipoff0 = NULL;
1273 LLVMValueRef mipoff1 = NULL;
1274 LLVMValueRef colors0[4], colors1[4];
1275 unsigned chan;
1276
1277 /* sample the first mipmap level */
1278 lp_build_mipmap_level_sizes(bld, ilevel0,
1279 &size0,
1280 &row_stride0_vec, &img_stride0_vec);
1281 if (bld->num_mips == 1) {
1282 data_ptr0 = lp_build_get_mipmap_level(bld, ilevel0);
1283 }
1284 else {
1285 /* This path should work for num_lods 1 too but slightly less efficient */
1286 data_ptr0 = bld->base_ptr;
1287 mipoff0 = lp_build_get_mip_offsets(bld, ilevel0);
1288 }
1289 if (img_filter == PIPE_TEX_FILTER_NEAREST) {
1290 lp_build_sample_image_nearest(bld, size0,
1291 row_stride0_vec, img_stride0_vec,
1292 data_ptr0, mipoff0, coords, offsets,
1293 colors0);
1294 }
1295 else {
1296 assert(img_filter == PIPE_TEX_FILTER_LINEAR);
1297 lp_build_sample_image_linear(bld, size0, NULL,
1298 row_stride0_vec, img_stride0_vec,
1299 data_ptr0, mipoff0, coords, offsets,
1300 colors0);
1301 }
1302
1303 /* Store the first level's colors in the output variables */
1304 for (chan = 0; chan < 4; chan++) {
1305 LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
1306 }
1307
1308 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1309 struct lp_build_if_state if_ctx;
1310 LLVMValueRef need_lerp;
1311
1312 /* need_lerp = lod_fpart > 0 */
1313 if (bld->num_lods == 1) {
1314 need_lerp = LLVMBuildFCmp(builder, LLVMRealUGT,
1315 lod_fpart, bld->lodf_bld.zero,
1316 "need_lerp");
1317 }
1318 else {
1319 /*
1320 * We'll do mip filtering if any of the quads (or individual
1321 * pixel in case of per-pixel lod) need it.
1322 * It might be better to split the vectors here and only fetch/filter
1323 * quads which need it (if there's one lod per quad).
1324 */
1325 need_lerp = lp_build_compare(bld->gallivm, bld->lodf_bld.type,
1326 PIPE_FUNC_GREATER,
1327 lod_fpart, bld->lodf_bld.zero);
1328 need_lerp = lp_build_any_true_range(&bld->lodi_bld, bld->num_lods, need_lerp);
1329 }
1330
1331 lp_build_if(&if_ctx, bld->gallivm, need_lerp);
1332 {
1333 /*
1334 * We unfortunately need to clamp lod_fpart here since we can get
1335 * negative values which would screw up filtering if not all
1336 * lod_fpart values have same sign.
1337 */
1338 lod_fpart = lp_build_max(&bld->lodf_bld, lod_fpart,
1339 bld->lodf_bld.zero);
1340 /* sample the second mipmap level */
1341 lp_build_mipmap_level_sizes(bld, ilevel1,
1342 &size1,
1343 &row_stride1_vec, &img_stride1_vec);
1344 if (bld->num_mips == 1) {
1345 data_ptr1 = lp_build_get_mipmap_level(bld, ilevel1);
1346 }
1347 else {
1348 data_ptr1 = bld->base_ptr;
1349 mipoff1 = lp_build_get_mip_offsets(bld, ilevel1);
1350 }
1351 if (img_filter == PIPE_TEX_FILTER_NEAREST) {
1352 lp_build_sample_image_nearest(bld, size1,
1353 row_stride1_vec, img_stride1_vec,
1354 data_ptr1, mipoff1, coords, offsets,
1355 colors1);
1356 }
1357 else {
1358 lp_build_sample_image_linear(bld, size1, NULL,
1359 row_stride1_vec, img_stride1_vec,
1360 data_ptr1, mipoff1, coords, offsets,
1361 colors1);
1362 }
1363
1364 /* interpolate samples from the two mipmap levels */
1365
1366 if (bld->num_lods != bld->coord_type.length)
1367 lod_fpart = lp_build_unpack_broadcast_aos_scalars(bld->gallivm,
1368 bld->lodf_bld.type,
1369 bld->texel_bld.type,
1370 lod_fpart);
1371
1372 for (chan = 0; chan < 4; chan++) {
1373 colors0[chan] = lp_build_lerp(&bld->texel_bld, lod_fpart,
1374 colors0[chan], colors1[chan],
1375 0);
1376 LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
1377 }
1378 }
1379 lp_build_endif(&if_ctx);
1380 }
1381 }
1382
1383
1384 /**
1385 * Sample the texture/mipmap using given mip filter, and using
1386 * both nearest and linear filtering at the same time depending
1387 * on linear_mask.
1388 * lod can be per quad but linear_mask is always per pixel.
1389 * ilevel0 and ilevel1 indicate the two mipmap levels to sample
1390 * from (vectors or scalars).
1391 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1392 */
1393 static void
1394 lp_build_sample_mipmap_both(struct lp_build_sample_context *bld,
1395 LLVMValueRef linear_mask,
1396 unsigned mip_filter,
1397 LLVMValueRef *coords,
1398 const LLVMValueRef *offsets,
1399 LLVMValueRef ilevel0,
1400 LLVMValueRef ilevel1,
1401 LLVMValueRef lod_fpart,
1402 LLVMValueRef lod_positive,
1403 LLVMValueRef *colors_out)
1404 {
1405 LLVMBuilderRef builder = bld->gallivm->builder;
1406 LLVMValueRef size0 = NULL;
1407 LLVMValueRef size1 = NULL;
1408 LLVMValueRef row_stride0_vec = NULL;
1409 LLVMValueRef row_stride1_vec = NULL;
1410 LLVMValueRef img_stride0_vec = NULL;
1411 LLVMValueRef img_stride1_vec = NULL;
1412 LLVMValueRef data_ptr0 = NULL;
1413 LLVMValueRef data_ptr1 = NULL;
1414 LLVMValueRef mipoff0 = NULL;
1415 LLVMValueRef mipoff1 = NULL;
1416 LLVMValueRef colors0[4], colors1[4];
1417 unsigned chan;
1418
1419 /* sample the first mipmap level */
1420 lp_build_mipmap_level_sizes(bld, ilevel0,
1421 &size0,
1422 &row_stride0_vec, &img_stride0_vec);
1423 if (bld->num_mips == 1) {
1424 data_ptr0 = lp_build_get_mipmap_level(bld, ilevel0);
1425 }
1426 else {
1427 /* This path should work for num_lods 1 too but slightly less efficient */
1428 data_ptr0 = bld->base_ptr;
1429 mipoff0 = lp_build_get_mip_offsets(bld, ilevel0);
1430 }
1431
1432 lp_build_sample_image_linear(bld, size0, linear_mask,
1433 row_stride0_vec, img_stride0_vec,
1434 data_ptr0, mipoff0, coords, offsets,
1435 colors0);
1436
1437 /* Store the first level's colors in the output variables */
1438 for (chan = 0; chan < 4; chan++) {
1439 LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
1440 }
1441
1442 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1443 struct lp_build_if_state if_ctx;
1444 LLVMValueRef need_lerp;
1445
1446 /*
1447 * We'll do mip filtering if any of the quads (or individual
1448 * pixel in case of per-pixel lod) need it.
1449 * Note using lod_positive here not lod_fpart since it may be the same
1450 * condition as that used in the outer "if" in the caller hence llvm
1451 * should be able to merge the branches in this case.
1452 */
1453 need_lerp = lp_build_any_true_range(&bld->lodi_bld, bld->num_lods, lod_positive);
1454
1455 lp_build_if(&if_ctx, bld->gallivm, need_lerp);
1456 {
1457 /*
1458 * We unfortunately need to clamp lod_fpart here since we can get
1459 * negative values which would screw up filtering if not all
1460 * lod_fpart values have same sign.
1461 */
1462 lod_fpart = lp_build_max(&bld->lodf_bld, lod_fpart,
1463 bld->lodf_bld.zero);
1464 /* sample the second mipmap level */
1465 lp_build_mipmap_level_sizes(bld, ilevel1,
1466 &size1,
1467 &row_stride1_vec, &img_stride1_vec);
1468 if (bld->num_mips == 1) {
1469 data_ptr1 = lp_build_get_mipmap_level(bld, ilevel1);
1470 }
1471 else {
1472 data_ptr1 = bld->base_ptr;
1473 mipoff1 = lp_build_get_mip_offsets(bld, ilevel1);
1474 }
1475
1476 lp_build_sample_image_linear(bld, size1, linear_mask,
1477 row_stride1_vec, img_stride1_vec,
1478 data_ptr1, mipoff1, coords, offsets,
1479 colors1);
1480
1481 /* interpolate samples from the two mipmap levels */
1482
1483 if (bld->num_lods != bld->coord_type.length)
1484 lod_fpart = lp_build_unpack_broadcast_aos_scalars(bld->gallivm,
1485 bld->lodf_bld.type,
1486 bld->texel_bld.type,
1487 lod_fpart);
1488
1489 for (chan = 0; chan < 4; chan++) {
1490 colors0[chan] = lp_build_lerp(&bld->texel_bld, lod_fpart,
1491 colors0[chan], colors1[chan],
1492 0);
1493 LLVMBuildStore(builder, colors0[chan], colors_out[chan]);
1494 }
1495 }
1496 lp_build_endif(&if_ctx);
1497 }
1498 }
1499
1500
1501 /**
1502 * Build (per-coord) layer value.
1503 * Either clamp layer to valid values or fill in optional out_of_bounds
1504 * value and just return value unclamped.
1505 */
1506 static LLVMValueRef
1507 lp_build_layer_coord(struct lp_build_sample_context *bld,
1508 unsigned texture_unit,
1509 LLVMValueRef layer,
1510 LLVMValueRef *out_of_bounds)
1511 {
1512 LLVMValueRef num_layers;
1513 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
1514
1515 num_layers = bld->dynamic_state->depth(bld->dynamic_state,
1516 bld->gallivm, texture_unit);
1517
1518 if (out_of_bounds) {
1519 LLVMValueRef out1, out;
1520 num_layers = lp_build_broadcast_scalar(int_coord_bld, num_layers);
1521 out = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, layer, int_coord_bld->zero);
1522 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, layer, num_layers);
1523 *out_of_bounds = lp_build_or(int_coord_bld, out, out1);
1524 return layer;
1525 }
1526 else {
1527 LLVMValueRef maxlayer;
1528 maxlayer = lp_build_sub(&bld->int_bld, num_layers, bld->int_bld.one);
1529 maxlayer = lp_build_broadcast_scalar(int_coord_bld, maxlayer);
1530 return lp_build_clamp(int_coord_bld, layer, int_coord_bld->zero, maxlayer);
1531 }
1532 }
1533
1534
1535 /**
1536 * Calculate cube face, lod, mip levels.
1537 */
1538 static void
1539 lp_build_sample_common(struct lp_build_sample_context *bld,
1540 unsigned texture_index,
1541 unsigned sampler_index,
1542 LLVMValueRef *coords,
1543 const struct lp_derivatives *derivs, /* optional */
1544 LLVMValueRef lod_bias, /* optional */
1545 LLVMValueRef explicit_lod, /* optional */
1546 LLVMValueRef *lod_pos_or_zero,
1547 LLVMValueRef *lod_fpart,
1548 LLVMValueRef *ilevel0,
1549 LLVMValueRef *ilevel1)
1550 {
1551 const unsigned mip_filter = bld->static_sampler_state->min_mip_filter;
1552 const unsigned min_filter = bld->static_sampler_state->min_img_filter;
1553 const unsigned mag_filter = bld->static_sampler_state->mag_img_filter;
1554 const unsigned target = bld->static_texture_state->target;
1555 LLVMValueRef first_level, cube_rho = NULL;
1556 LLVMValueRef lod_ipart = NULL;
1557 struct lp_derivatives cube_derivs;
1558
1559 /*
1560 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1561 mip_filter, min_filter, mag_filter);
1562 */
1563
1564 /*
1565 * Choose cube face, recompute texcoords for the chosen face and
1566 * compute rho here too (as it requires transform of derivatives).
1567 */
1568 if (target == PIPE_TEXTURE_CUBE) {
1569 boolean need_derivs;
1570 need_derivs = ((min_filter != mag_filter ||
1571 mip_filter != PIPE_TEX_MIPFILTER_NONE) &&
1572 !bld->static_sampler_state->min_max_lod_equal &&
1573 !explicit_lod);
1574 lp_build_cube_lookup(bld, coords, derivs, &cube_rho, &cube_derivs, need_derivs);
1575 derivs = &cube_derivs;
1576 }
1577 else if (target == PIPE_TEXTURE_1D_ARRAY ||
1578 target == PIPE_TEXTURE_2D_ARRAY) {
1579 coords[2] = lp_build_iround(&bld->coord_bld, coords[2]);
1580 coords[2] = lp_build_layer_coord(bld, texture_index, coords[2], NULL);
1581 }
1582
1583 if (bld->static_sampler_state->compare_mode != PIPE_TEX_COMPARE_NONE) {
1584 /*
1585 * Clamp p coords to [0,1] for fixed function depth texture format here.
1586 * Technically this is not entirely correct for unorm depth as the ref value
1587 * should be converted to the depth format (quantization!) and comparison
1588 * then done in texture format. This would actually help performance (since
1589 * only need to do it once and could save the per-sample conversion of texels
1590 * to floats instead), but it would need more messy code (would need to push
1591 * at least some bits down to actual fetch so conversion could be skipped,
1592 * and would have ugly interaction with border color, would need to convert
1593 * border color to that format too or do some other tricks to make it work).
1594 */
1595 const struct util_format_description *format_desc = bld->format_desc;
1596 unsigned chan_type;
1597 /* not entirely sure we couldn't end up with non-valid swizzle here */
1598 chan_type = format_desc->swizzle[0] <= UTIL_FORMAT_SWIZZLE_W ?
1599 format_desc->channel[format_desc->swizzle[0]].type :
1600 UTIL_FORMAT_TYPE_FLOAT;
1601 if (chan_type != UTIL_FORMAT_TYPE_FLOAT) {
1602 coords[4] = lp_build_clamp(&bld->coord_bld, coords[4],
1603 bld->coord_bld.zero, bld->coord_bld.one);
1604 }
1605 }
1606
1607 /*
1608 * Compute the level of detail (float).
1609 */
1610 if (min_filter != mag_filter ||
1611 mip_filter != PIPE_TEX_MIPFILTER_NONE) {
1612 /* Need to compute lod either to choose mipmap levels or to
1613 * distinguish between minification/magnification with one mipmap level.
1614 */
1615 lp_build_lod_selector(bld, texture_index, sampler_index,
1616 coords[0], coords[1], coords[2], cube_rho,
1617 derivs, lod_bias, explicit_lod,
1618 mip_filter,
1619 &lod_ipart, lod_fpart, lod_pos_or_zero);
1620 } else {
1621 lod_ipart = bld->lodi_bld.zero;
1622 *lod_pos_or_zero = bld->lodi_bld.zero;
1623 }
1624
1625 if (bld->num_lods != bld->num_mips) {
1626 /* only makes sense if there's just a single mip level */
1627 assert(bld->num_mips == 1);
1628 lod_ipart = lp_build_extract_range(bld->gallivm, lod_ipart, 0, 1);
1629 }
1630
1631 /*
1632 * Compute integer mipmap level(s) to fetch texels from: ilevel0, ilevel1
1633 */
1634 switch (mip_filter) {
1635 default:
1636 assert(0 && "bad mip_filter value in lp_build_sample_soa()");
1637 /* fall-through */
1638 case PIPE_TEX_MIPFILTER_NONE:
1639 /* always use mip level 0 */
1640 if (HAVE_LLVM == 0x0207 && target == PIPE_TEXTURE_CUBE) {
1641 /* XXX this is a work-around for an apparent bug in LLVM 2.7.
1642 * We should be able to set ilevel0 = const(0) but that causes
1643 * bad x86 code to be emitted.
1644 */
1645 assert(lod_ipart);
1646 lp_build_nearest_mip_level(bld, texture_index, lod_ipart, ilevel0, NULL);
1647 }
1648 else {
1649 first_level = bld->dynamic_state->first_level(bld->dynamic_state,
1650 bld->gallivm, texture_index);
1651 first_level = lp_build_broadcast_scalar(&bld->leveli_bld, first_level);
1652 *ilevel0 = first_level;
1653 }
1654 break;
1655 case PIPE_TEX_MIPFILTER_NEAREST:
1656 assert(lod_ipart);
1657 lp_build_nearest_mip_level(bld, texture_index, lod_ipart, ilevel0, NULL);
1658 break;
1659 case PIPE_TEX_MIPFILTER_LINEAR:
1660 assert(lod_ipart);
1661 assert(*lod_fpart);
1662 lp_build_linear_mip_levels(bld, texture_index,
1663 lod_ipart, lod_fpart,
1664 ilevel0, ilevel1);
1665 break;
1666 }
1667 }
1668
1669 static void
1670 lp_build_clamp_border_color(struct lp_build_sample_context *bld,
1671 unsigned sampler_unit)
1672 {
1673 struct gallivm_state *gallivm = bld->gallivm;
1674 LLVMBuilderRef builder = gallivm->builder;
1675 LLVMValueRef border_color_ptr =
1676 bld->dynamic_state->border_color(bld->dynamic_state,
1677 gallivm, sampler_unit);
1678 LLVMValueRef border_color;
1679 const struct util_format_description *format_desc = bld->format_desc;
1680 struct lp_type vec4_type = bld->texel_type;
1681 struct lp_build_context vec4_bld;
1682 LLVMValueRef min_clamp = NULL;
1683 LLVMValueRef max_clamp = NULL;
1684
1685 /*
1686 * For normalized format need to clamp border color (technically
1687 * probably should also quantize the data). Really sucks doing this
1688 * here but can't avoid at least for now since this is part of
1689 * sampler state and texture format is part of sampler_view state.
1690 * GL expects also expects clamping for uint/sint formats too so
1691 * do that as well (d3d10 can't end up here with uint/sint since it
1692 * only supports them with ld).
1693 */
1694 vec4_type.length = 4;
1695 lp_build_context_init(&vec4_bld, gallivm, vec4_type);
1696
1697 /*
1698 * Vectorized clamping of border color. Loading is a bit of a hack since
1699 * we just cast the pointer to float array to pointer to vec4
1700 * (int or float).
1701 */
1702 border_color_ptr = lp_build_array_get_ptr(gallivm, border_color_ptr,
1703 lp_build_const_int32(gallivm, 0));
1704 border_color_ptr = LLVMBuildBitCast(builder, border_color_ptr,
1705 LLVMPointerType(vec4_bld.vec_type, 0), "");
1706 border_color = LLVMBuildLoad(builder, border_color_ptr, "");
1707 /* we don't have aligned type in the dynamic state unfortunately */
1708 lp_set_load_alignment(border_color, 4);
1709
1710 /*
1711 * Instead of having some incredibly complex logic which will try to figure out
1712 * clamping necessary for each channel, simply use the first channel, and treat
1713 * mixed signed/unsigned normalized formats specially.
1714 * (Mixed non-normalized, which wouldn't work at all here, do not exist for a
1715 * good reason.)
1716 */
1717 if (format_desc->layout == UTIL_FORMAT_LAYOUT_PLAIN) {
1718 int chan;
1719 /* d/s needs special handling because both present means just sampling depth */
1720 if (util_format_is_depth_and_stencil(format_desc->format)) {
1721 chan = format_desc->swizzle[0];
1722 }
1723 else {
1724 chan = util_format_get_first_non_void_channel(format_desc->format);
1725 }
1726 if (chan >= 0 && chan <= UTIL_FORMAT_SWIZZLE_W) {
1727 unsigned chan_type = format_desc->channel[chan].type;
1728 unsigned chan_norm = format_desc->channel[chan].normalized;
1729 unsigned chan_pure = format_desc->channel[chan].pure_integer;
1730 if (chan_type == UTIL_FORMAT_TYPE_SIGNED) {
1731 if (chan_norm) {
1732 min_clamp = lp_build_const_vec(gallivm, vec4_type, -1.0F);
1733 max_clamp = vec4_bld.one;
1734 }
1735 else if (chan_pure) {
1736 /*
1737 * Border color was stored as int, hence need min/max clamp
1738 * only if chan has less than 32 bits..
1739 */
1740 unsigned chan_size = format_desc->channel[chan].size;
1741 if (chan_size < 32) {
1742 min_clamp = lp_build_const_int_vec(gallivm, vec4_type,
1743 0 - (1 << (chan_size - 1)));
1744 max_clamp = lp_build_const_int_vec(gallivm, vec4_type,
1745 (1 << (chan_size - 1)) - 1);
1746 }
1747 }
1748 /* TODO: no idea about non-pure, non-normalized! */
1749 }
1750 else if (chan_type == UTIL_FORMAT_TYPE_UNSIGNED) {
1751 if (chan_norm) {
1752 min_clamp = vec4_bld.zero;
1753 max_clamp = vec4_bld.one;
1754 }
1755 /*
1756 * Need a ugly hack here, because we don't have Z32_FLOAT_X8X24
1757 * we use Z32_FLOAT_S8X24 to imply sampling depth component
1758 * and ignoring stencil, which will blow up here if we try to
1759 * do a uint clamp in a float texel build...
1760 * And even if we had that format, mesa st also thinks using z24s8
1761 * means depth sampling ignoring stencil.
1762 */
1763 else if (chan_pure) {
1764 /*
1765 * Border color was stored as uint, hence never need min
1766 * clamp, and only need max clamp if chan has less than 32 bits.
1767 */
1768 unsigned chan_size = format_desc->channel[chan].size;
1769 if (chan_size < 32) {
1770 max_clamp = lp_build_const_int_vec(gallivm, vec4_type,
1771 (1 << chan_size) - 1);
1772 }
1773 /* TODO: no idea about non-pure, non-normalized! */
1774 }
1775 }
1776 else if (chan_type == UTIL_FORMAT_TYPE_FIXED) {
1777 /* TODO: I have no idea what clamp this would need if any! */
1778 }
1779 }
1780 /* mixed plain formats (or different pure size) */
1781 switch (format_desc->format) {
1782 case PIPE_FORMAT_B10G10R10A2_UINT:
1783 case PIPE_FORMAT_R10G10B10A2_UINT:
1784 {
1785 unsigned max10 = (1 << 10) - 1;
1786 max_clamp = lp_build_const_aos(gallivm, vec4_type, max10, max10,
1787 max10, (1 << 2) - 1, NULL);
1788 }
1789 break;
1790 case PIPE_FORMAT_R10SG10SB10SA2U_NORM:
1791 min_clamp = lp_build_const_aos(gallivm, vec4_type, -1.0F, -1.0F,
1792 -1.0F, 0.0F, NULL);
1793 max_clamp = vec4_bld.one;
1794 break;
1795 case PIPE_FORMAT_R8SG8SB8UX8U_NORM:
1796 case PIPE_FORMAT_R5SG5SB6U_NORM:
1797 min_clamp = lp_build_const_aos(gallivm, vec4_type, -1.0F, -1.0F,
1798 0.0F, 0.0F, NULL);
1799 max_clamp = vec4_bld.one;
1800 break;
1801 default:
1802 break;
1803 }
1804 }
1805 else {
1806 /* cannot figure this out from format description */
1807 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
1808 /* s3tc formats are always unorm */
1809 min_clamp = vec4_bld.zero;
1810 max_clamp = vec4_bld.one;
1811 }
1812 else if (format_desc->layout == UTIL_FORMAT_LAYOUT_RGTC ||
1813 format_desc->layout == UTIL_FORMAT_LAYOUT_ETC) {
1814 switch (format_desc->format) {
1815 case PIPE_FORMAT_RGTC1_UNORM:
1816 case PIPE_FORMAT_RGTC2_UNORM:
1817 case PIPE_FORMAT_LATC1_UNORM:
1818 case PIPE_FORMAT_LATC2_UNORM:
1819 case PIPE_FORMAT_ETC1_RGB8:
1820 min_clamp = vec4_bld.zero;
1821 max_clamp = vec4_bld.one;
1822 break;
1823 case PIPE_FORMAT_RGTC1_SNORM:
1824 case PIPE_FORMAT_RGTC2_SNORM:
1825 case PIPE_FORMAT_LATC1_SNORM:
1826 case PIPE_FORMAT_LATC2_SNORM:
1827 min_clamp = lp_build_const_vec(gallivm, vec4_type, -1.0F);
1828 max_clamp = vec4_bld.one;
1829 break;
1830 default:
1831 assert(0);
1832 break;
1833 }
1834 }
1835 /*
1836 * all others from subsampled/other group, though we don't care
1837 * about yuv (and should not have any from zs here)
1838 */
1839 else if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_YUV){
1840 switch (format_desc->format) {
1841 case PIPE_FORMAT_R8G8_B8G8_UNORM:
1842 case PIPE_FORMAT_G8R8_G8B8_UNORM:
1843 case PIPE_FORMAT_G8R8_B8R8_UNORM:
1844 case PIPE_FORMAT_R8G8_R8B8_UNORM:
1845 case PIPE_FORMAT_R1_UNORM: /* doesn't make sense but ah well */
1846 min_clamp = vec4_bld.zero;
1847 max_clamp = vec4_bld.one;
1848 break;
1849 case PIPE_FORMAT_R8G8Bx_SNORM:
1850 min_clamp = lp_build_const_vec(gallivm, vec4_type, -1.0F);
1851 max_clamp = vec4_bld.one;
1852 break;
1853 /*
1854 * Note smallfloat formats usually don't need clamping
1855 * (they still have infinite range) however this is not
1856 * true for r11g11b10 and r9g9b9e5, which can't represent
1857 * negative numbers (and additionally r9g9b9e5 can't represent
1858 * very large numbers). d3d10 seems happy without clamping in
1859 * this case, but gl spec is pretty clear: "for floating
1860 * point and integer formats, border values are clamped to
1861 * the representable range of the format" so do that here.
1862 */
1863 case PIPE_FORMAT_R11G11B10_FLOAT:
1864 min_clamp = vec4_bld.zero;
1865 break;
1866 case PIPE_FORMAT_R9G9B9E5_FLOAT:
1867 min_clamp = vec4_bld.zero;
1868 max_clamp = lp_build_const_vec(gallivm, vec4_type, MAX_RGB9E5);
1869 break;
1870 default:
1871 assert(0);
1872 break;
1873 }
1874 }
1875 }
1876
1877 if (min_clamp) {
1878 border_color = lp_build_max(&vec4_bld, border_color, min_clamp);
1879 }
1880 if (max_clamp) {
1881 border_color = lp_build_min(&vec4_bld, border_color, max_clamp);
1882 }
1883
1884 bld->border_color_clamped = border_color;
1885 }
1886
1887
1888 /**
1889 * General texture sampling codegen.
1890 * This function handles texture sampling for all texture targets (1D,
1891 * 2D, 3D, cube) and all filtering modes.
1892 */
1893 static void
1894 lp_build_sample_general(struct lp_build_sample_context *bld,
1895 unsigned sampler_unit,
1896 LLVMValueRef *coords,
1897 const LLVMValueRef *offsets,
1898 LLVMValueRef lod_positive,
1899 LLVMValueRef lod_fpart,
1900 LLVMValueRef ilevel0,
1901 LLVMValueRef ilevel1,
1902 LLVMValueRef *colors_out)
1903 {
1904 LLVMBuilderRef builder = bld->gallivm->builder;
1905 const struct lp_static_sampler_state *sampler_state = bld->static_sampler_state;
1906 const unsigned mip_filter = sampler_state->min_mip_filter;
1907 const unsigned min_filter = sampler_state->min_img_filter;
1908 const unsigned mag_filter = sampler_state->mag_img_filter;
1909 LLVMValueRef texels[4];
1910 unsigned chan;
1911
1912 /* if we need border color, (potentially) clamp it now */
1913 if (lp_sampler_wrap_mode_uses_border_color(sampler_state->wrap_s,
1914 min_filter,
1915 mag_filter) ||
1916 (bld->dims > 1 &&
1917 lp_sampler_wrap_mode_uses_border_color(sampler_state->wrap_t,
1918 min_filter,
1919 mag_filter)) ||
1920 (bld->dims > 2 &&
1921 lp_sampler_wrap_mode_uses_border_color(sampler_state->wrap_r,
1922 min_filter,
1923 mag_filter))) {
1924 lp_build_clamp_border_color(bld, sampler_unit);
1925 }
1926
1927
1928 /*
1929 * Get/interpolate texture colors.
1930 */
1931
1932 for (chan = 0; chan < 4; ++chan) {
1933 texels[chan] = lp_build_alloca(bld->gallivm, bld->texel_bld.vec_type, "");
1934 lp_build_name(texels[chan], "sampler%u_texel_%c_var", sampler_unit, "xyzw"[chan]);
1935 }
1936
1937 if (min_filter == mag_filter) {
1938 /* no need to distinguish between minification and magnification */
1939 lp_build_sample_mipmap(bld, min_filter, mip_filter,
1940 coords, offsets,
1941 ilevel0, ilevel1, lod_fpart,
1942 texels);
1943 }
1944 else {
1945 /*
1946 * Could also get rid of the if-logic and always use mipmap_both, both
1947 * for the single lod and multi-lod case if nothing really uses this.
1948 */
1949 if (bld->num_lods == 1) {
1950 /* Emit conditional to choose min image filter or mag image filter
1951 * depending on the lod being > 0 or <= 0, respectively.
1952 */
1953 struct lp_build_if_state if_ctx;
1954
1955 lod_positive = LLVMBuildTrunc(builder, lod_positive,
1956 LLVMInt1TypeInContext(bld->gallivm->context), "");
1957
1958 lp_build_if(&if_ctx, bld->gallivm, lod_positive);
1959 {
1960 /* Use the minification filter */
1961 lp_build_sample_mipmap(bld, min_filter, mip_filter,
1962 coords, offsets,
1963 ilevel0, ilevel1, lod_fpart,
1964 texels);
1965 }
1966 lp_build_else(&if_ctx);
1967 {
1968 /* Use the magnification filter */
1969 lp_build_sample_mipmap(bld, mag_filter, PIPE_TEX_MIPFILTER_NONE,
1970 coords, offsets,
1971 ilevel0, NULL, NULL,
1972 texels);
1973 }
1974 lp_build_endif(&if_ctx);
1975 }
1976 else {
1977 LLVMValueRef need_linear, linear_mask;
1978 unsigned mip_filter_for_nearest;
1979 struct lp_build_if_state if_ctx;
1980
1981 if (min_filter == PIPE_TEX_FILTER_LINEAR) {
1982 linear_mask = lod_positive;
1983 mip_filter_for_nearest = PIPE_TEX_MIPFILTER_NONE;
1984 }
1985 else {
1986 linear_mask = lp_build_not(&bld->lodi_bld, lod_positive);
1987 mip_filter_for_nearest = mip_filter;
1988 }
1989 need_linear = lp_build_any_true_range(&bld->lodi_bld, bld->num_lods,
1990 linear_mask);
1991
1992 if (bld->num_lods != bld->coord_type.length) {
1993 linear_mask = lp_build_unpack_broadcast_aos_scalars(bld->gallivm,
1994 bld->lodi_type,
1995 bld->int_coord_type,
1996 linear_mask);
1997 }
1998
1999 lp_build_if(&if_ctx, bld->gallivm, need_linear);
2000 {
2001 /*
2002 * Do sampling with both filters simultaneously. This means using
2003 * a linear filter and doing some tricks (with weights) for the pixels
2004 * which need nearest filter.
2005 * Note that it's probably rare some pixels need nearest and some
2006 * linear filter but the fixups required for the nearest pixels
2007 * aren't all that complicated so just always run a combined path
2008 * if at least some pixels require linear.
2009 */
2010 lp_build_sample_mipmap_both(bld, linear_mask, mip_filter,
2011 coords, offsets,
2012 ilevel0, ilevel1,
2013 lod_fpart, lod_positive,
2014 texels);
2015 }
2016 lp_build_else(&if_ctx);
2017 {
2018 /*
2019 * All pixels require just nearest filtering, which is way
2020 * cheaper than linear, hence do a separate path for that.
2021 */
2022 lp_build_sample_mipmap(bld, PIPE_TEX_FILTER_NEAREST,
2023 mip_filter_for_nearest,
2024 coords, offsets,
2025 ilevel0, ilevel1, lod_fpart,
2026 texels);
2027 }
2028 lp_build_endif(&if_ctx);
2029 }
2030 }
2031
2032 for (chan = 0; chan < 4; ++chan) {
2033 colors_out[chan] = LLVMBuildLoad(builder, texels[chan], "");
2034 lp_build_name(colors_out[chan], "sampler%u_texel_%c", sampler_unit, "xyzw"[chan]);
2035 }
2036 }
2037
2038
2039 /**
2040 * Texel fetch function.
2041 * In contrast to general sampling there is no filtering, no coord minification,
2042 * lod (if any) is always explicit uint, coords are uints (in terms of texel units)
2043 * directly to be applied to the selected mip level (after adding texel offsets).
2044 * This function handles texel fetch for all targets where texel fetch is supported
2045 * (no cube maps, but 1d, 2d, 3d are supported, arrays and buffers should be too).
2046 */
2047 static void
2048 lp_build_fetch_texel(struct lp_build_sample_context *bld,
2049 unsigned texture_unit,
2050 const LLVMValueRef *coords,
2051 LLVMValueRef explicit_lod,
2052 const LLVMValueRef *offsets,
2053 LLVMValueRef *colors_out)
2054 {
2055 struct lp_build_context *perquadi_bld = &bld->lodi_bld;
2056 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
2057 unsigned dims = bld->dims, chan;
2058 unsigned target = bld->static_texture_state->target;
2059 boolean out_of_bound_ret_zero = TRUE;
2060 LLVMValueRef size, ilevel;
2061 LLVMValueRef row_stride_vec = NULL, img_stride_vec = NULL;
2062 LLVMValueRef x = coords[0], y = coords[1], z = coords[2];
2063 LLVMValueRef width, height, depth, i, j;
2064 LLVMValueRef offset, out_of_bounds, out1;
2065
2066 out_of_bounds = int_coord_bld->zero;
2067
2068 if (explicit_lod && bld->static_texture_state->target != PIPE_BUFFER) {
2069 if (bld->num_mips != int_coord_bld->type.length) {
2070 ilevel = lp_build_pack_aos_scalars(bld->gallivm, int_coord_bld->type,
2071 perquadi_bld->type, explicit_lod, 0);
2072 }
2073 else {
2074 ilevel = explicit_lod;
2075 }
2076 lp_build_nearest_mip_level(bld, texture_unit, ilevel, &ilevel,
2077 out_of_bound_ret_zero ? &out_of_bounds : NULL);
2078 }
2079 else {
2080 assert(bld->num_mips == 1);
2081 if (bld->static_texture_state->target != PIPE_BUFFER) {
2082 ilevel = bld->dynamic_state->first_level(bld->dynamic_state,
2083 bld->gallivm, texture_unit);
2084 }
2085 else {
2086 ilevel = lp_build_const_int32(bld->gallivm, 0);
2087 }
2088 }
2089 lp_build_mipmap_level_sizes(bld, ilevel,
2090 &size,
2091 &row_stride_vec, &img_stride_vec);
2092 lp_build_extract_image_sizes(bld, &bld->int_size_bld, int_coord_bld->type,
2093 size, &width, &height, &depth);
2094
2095 if (target == PIPE_TEXTURE_1D_ARRAY ||
2096 target == PIPE_TEXTURE_2D_ARRAY) {
2097 if (out_of_bound_ret_zero) {
2098 z = lp_build_layer_coord(bld, texture_unit, z, &out1);
2099 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
2100 }
2101 else {
2102 z = lp_build_layer_coord(bld, texture_unit, z, NULL);
2103 }
2104 }
2105
2106 /* This is a lot like border sampling */
2107 if (offsets[0]) {
2108 /*
2109 * coords are really unsigned, offsets are signed, but I don't think
2110 * exceeding 31 bits is possible
2111 */
2112 x = lp_build_add(int_coord_bld, x, offsets[0]);
2113 }
2114 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, x, int_coord_bld->zero);
2115 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
2116 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, x, width);
2117 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
2118
2119 if (dims >= 2) {
2120 if (offsets[1]) {
2121 y = lp_build_add(int_coord_bld, y, offsets[1]);
2122 }
2123 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, y, int_coord_bld->zero);
2124 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
2125 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, y, height);
2126 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
2127
2128 if (dims >= 3) {
2129 if (offsets[2]) {
2130 z = lp_build_add(int_coord_bld, z, offsets[2]);
2131 }
2132 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, z, int_coord_bld->zero);
2133 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
2134 out1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, z, depth);
2135 out_of_bounds = lp_build_or(int_coord_bld, out_of_bounds, out1);
2136 }
2137 }
2138
2139 lp_build_sample_offset(int_coord_bld,
2140 bld->format_desc,
2141 x, y, z, row_stride_vec, img_stride_vec,
2142 &offset, &i, &j);
2143
2144 if (bld->static_texture_state->target != PIPE_BUFFER) {
2145 offset = lp_build_add(int_coord_bld, offset,
2146 lp_build_get_mip_offsets(bld, ilevel));
2147 }
2148
2149 offset = lp_build_andnot(int_coord_bld, offset, out_of_bounds);
2150
2151 lp_build_fetch_rgba_soa(bld->gallivm,
2152 bld->format_desc,
2153 bld->texel_type,
2154 bld->base_ptr, offset,
2155 i, j,
2156 colors_out);
2157
2158 if (out_of_bound_ret_zero) {
2159 /*
2160 * Only needed for ARB_robust_buffer_access_behavior and d3d10.
2161 * Could use min/max above instead of out-of-bounds comparisons
2162 * if we don't care about the result returned for out-of-bounds.
2163 */
2164 for (chan = 0; chan < 4; chan++) {
2165 colors_out[chan] = lp_build_select(&bld->texel_bld, out_of_bounds,
2166 bld->texel_bld.zero, colors_out[chan]);
2167 }
2168 }
2169 }
2170
2171
2172 /**
2173 * Just set texels to white instead of actually sampling the texture.
2174 * For debugging.
2175 */
2176 void
2177 lp_build_sample_nop(struct gallivm_state *gallivm,
2178 struct lp_type type,
2179 const LLVMValueRef *coords,
2180 LLVMValueRef texel_out[4])
2181 {
2182 LLVMValueRef one = lp_build_one(gallivm, type);
2183 unsigned chan;
2184
2185 for (chan = 0; chan < 4; chan++) {
2186 texel_out[chan] = one;
2187 }
2188 }
2189
2190
2191 /**
2192 * Build texture sampling code.
2193 * 'texel' will return a vector of four LLVMValueRefs corresponding to
2194 * R, G, B, A.
2195 * \param type vector float type to use for coords, etc.
2196 * \param is_fetch if this is a texel fetch instruction.
2197 * \param derivs partial derivatives of (s,t,r,q) with respect to x and y
2198 */
2199 void
2200 lp_build_sample_soa(struct gallivm_state *gallivm,
2201 const struct lp_static_texture_state *static_texture_state,
2202 const struct lp_static_sampler_state *static_sampler_state,
2203 struct lp_sampler_dynamic_state *dynamic_state,
2204 struct lp_type type,
2205 boolean is_fetch,
2206 unsigned texture_index,
2207 unsigned sampler_index,
2208 const LLVMValueRef *coords,
2209 const LLVMValueRef *offsets,
2210 const struct lp_derivatives *derivs, /* optional */
2211 LLVMValueRef lod_bias, /* optional */
2212 LLVMValueRef explicit_lod, /* optional */
2213 enum lp_sampler_lod_property lod_property,
2214 LLVMValueRef texel_out[4])
2215 {
2216 unsigned target = static_texture_state->target;
2217 unsigned dims = texture_dims(target);
2218 unsigned num_quads = type.length / 4;
2219 unsigned mip_filter, min_img_filter, mag_img_filter, i;
2220 struct lp_build_sample_context bld;
2221 struct lp_static_sampler_state derived_sampler_state = *static_sampler_state;
2222 LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context);
2223 LLVMBuilderRef builder = gallivm->builder;
2224 LLVMValueRef tex_width, newcoords[5];
2225
2226 if (0) {
2227 enum pipe_format fmt = static_texture_state->format;
2228 debug_printf("Sample from %s\n", util_format_name(fmt));
2229 }
2230
2231 if (static_texture_state->format == PIPE_FORMAT_NONE) {
2232 /*
2233 * If there's nothing bound, format is NONE, and we must return
2234 * all zero as mandated by d3d10 in this case.
2235 */
2236 unsigned chan;
2237 LLVMValueRef zero = lp_build_const_vec(gallivm, type, 0.0F);
2238 for (chan = 0; chan < 4; chan++) {
2239 texel_out[chan] = zero;
2240 }
2241 return;
2242 }
2243
2244 assert(type.floating);
2245
2246 /* Setup our build context */
2247 memset(&bld, 0, sizeof bld);
2248 bld.gallivm = gallivm;
2249 bld.static_sampler_state = &derived_sampler_state;
2250 bld.static_texture_state = static_texture_state;
2251 bld.dynamic_state = dynamic_state;
2252 bld.format_desc = util_format_description(static_texture_state->format);
2253 bld.dims = dims;
2254
2255 bld.vector_width = lp_type_width(type);
2256
2257 bld.float_type = lp_type_float(32);
2258 bld.int_type = lp_type_int(32);
2259 bld.coord_type = type;
2260 bld.int_coord_type = lp_int_type(type);
2261 bld.float_size_in_type = lp_type_float(32);
2262 bld.float_size_in_type.length = dims > 1 ? 4 : 1;
2263 bld.int_size_in_type = lp_int_type(bld.float_size_in_type);
2264 bld.texel_type = type;
2265
2266 /* always using the first channel hopefully should be safe,
2267 * if not things WILL break in other places anyway.
2268 */
2269 if (bld.format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB &&
2270 bld.format_desc->channel[0].pure_integer) {
2271 if (bld.format_desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
2272 bld.texel_type = lp_type_int_vec(type.width, type.width * type.length);
2273 }
2274 else if (bld.format_desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED) {
2275 bld.texel_type = lp_type_uint_vec(type.width, type.width * type.length);
2276 }
2277 }
2278 else if (util_format_has_stencil(bld.format_desc) &&
2279 !util_format_has_depth(bld.format_desc)) {
2280 /* for stencil only formats, sample stencil (uint) */
2281 bld.texel_type = lp_type_int_vec(type.width, type.width * type.length);
2282 }
2283
2284 if (!static_texture_state->level_zero_only) {
2285 derived_sampler_state.min_mip_filter = static_sampler_state->min_mip_filter;
2286 } else {
2287 derived_sampler_state.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
2288 }
2289 mip_filter = derived_sampler_state.min_mip_filter;
2290
2291 if (0) {
2292 debug_printf(" .min_mip_filter = %u\n", derived_sampler_state.min_mip_filter);
2293 }
2294
2295 if (static_texture_state->target == PIPE_TEXTURE_CUBE ||
2296 static_texture_state->target == PIPE_TEXTURE_CUBE_ARRAY)
2297 {
2298 /*
2299 * Seamless filtering ignores wrap modes.
2300 * Setting to CLAMP_TO_EDGE is correct for nearest filtering, for
2301 * bilinear it's not correct but way better than using for instance repeat.
2302 * Note we even set this for non-seamless. Technically GL allows any wrap
2303 * mode, which made sense when supporting true borders (can get seamless
2304 * effect with border and CLAMP_TO_BORDER), but gallium doesn't support
2305 * borders and d3d9 requires wrap modes to be ignored and it's a pain to fix
2306 * up the sampler state (as it makes it texture dependent).
2307 */
2308 derived_sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
2309 derived_sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
2310 }
2311
2312 min_img_filter = derived_sampler_state.min_img_filter;
2313 mag_img_filter = derived_sampler_state.mag_img_filter;
2314
2315
2316 /*
2317 * This is all a bit complicated different paths are chosen for performance
2318 * reasons.
2319 * Essentially, there can be 1 lod per element, 1 lod per quad or 1 lod for
2320 * everything (the last two options are equivalent for 4-wide case).
2321 * If there's per-quad lod but we split to 4-wide so we can use AoS, per-quad
2322 * lod is calculated then the lod value extracted afterwards so making this
2323 * case basically the same as far as lod handling is concerned for the
2324 * further sample/filter code as the 1 lod for everything case.
2325 * Different lod handling mostly shows up when building mipmap sizes
2326 * (lp_build_mipmap_level_sizes() and friends) and also in filtering
2327 * (getting the fractional part of the lod to the right texels).
2328 */
2329
2330 /*
2331 * There are other situations where at least the multiple int lods could be
2332 * avoided like min and max lod being equal.
2333 */
2334 bld.num_mips = bld.num_lods = 1;
2335
2336 if ((gallivm_debug & GALLIVM_DEBUG_NO_QUAD_LOD) &&
2337 (gallivm_debug & GALLIVM_DEBUG_NO_RHO_APPROX) &&
2338 (static_texture_state->target == PIPE_TEXTURE_CUBE) &&
2339 (!is_fetch && mip_filter != PIPE_TEX_MIPFILTER_NONE)) {
2340 /*
2341 * special case for using per-pixel lod even for implicit lod,
2342 * which is generally never required (ok by APIs) except to please
2343 * some (somewhat broken imho) tests (because per-pixel face selection
2344 * can cause derivatives to be different for pixels outside the primitive
2345 * due to the major axis division even if pre-project derivatives are
2346 * looking normal).
2347 */
2348 bld.num_mips = type.length;
2349 bld.num_lods = type.length;
2350 }
2351 else if (lod_property == LP_SAMPLER_LOD_PER_ELEMENT ||
2352 (explicit_lod || lod_bias || derivs)) {
2353 if ((is_fetch && target != PIPE_BUFFER) ||
2354 (!is_fetch && mip_filter != PIPE_TEX_MIPFILTER_NONE)) {
2355 bld.num_mips = type.length;
2356 bld.num_lods = type.length;
2357 }
2358 else if (!is_fetch && min_img_filter != mag_img_filter) {
2359 bld.num_mips = 1;
2360 bld.num_lods = type.length;
2361 }
2362 }
2363 /* TODO: for true scalar_lod should only use 1 lod value */
2364 else if ((is_fetch && explicit_lod && target != PIPE_BUFFER) ||
2365 (!is_fetch && mip_filter != PIPE_TEX_MIPFILTER_NONE)) {
2366 bld.num_mips = num_quads;
2367 bld.num_lods = num_quads;
2368 }
2369 else if (!is_fetch && min_img_filter != mag_img_filter) {
2370 bld.num_mips = 1;
2371 bld.num_lods = num_quads;
2372 }
2373
2374
2375 bld.lodf_type = type;
2376 /* we want native vector size to be able to use our intrinsics */
2377 if (bld.num_lods != type.length) {
2378 /* TODO: this currently always has to be per-quad or per-element */
2379 bld.lodf_type.length = type.length > 4 ? ((type.length + 15) / 16) * 4 : 1;
2380 }
2381 bld.lodi_type = lp_int_type(bld.lodf_type);
2382 bld.levelf_type = bld.lodf_type;
2383 if (bld.num_mips == 1) {
2384 bld.levelf_type.length = 1;
2385 }
2386 bld.leveli_type = lp_int_type(bld.levelf_type);
2387 bld.float_size_type = bld.float_size_in_type;
2388 /* Note: size vectors may not be native. They contain minified w/h/d/_ values,
2389 * with per-element lod that is w0/h0/d0/_/w1/h1/d1_/... so up to 8x4f32 */
2390 if (bld.num_mips > 1) {
2391 bld.float_size_type.length = bld.num_mips == type.length ?
2392 bld.num_mips * bld.float_size_in_type.length :
2393 type.length;
2394 }
2395 bld.int_size_type = lp_int_type(bld.float_size_type);
2396
2397 lp_build_context_init(&bld.float_bld, gallivm, bld.float_type);
2398 lp_build_context_init(&bld.float_vec_bld, gallivm, type);
2399 lp_build_context_init(&bld.int_bld, gallivm, bld.int_type);
2400 lp_build_context_init(&bld.coord_bld, gallivm, bld.coord_type);
2401 lp_build_context_init(&bld.int_coord_bld, gallivm, bld.int_coord_type);
2402 lp_build_context_init(&bld.int_size_in_bld, gallivm, bld.int_size_in_type);
2403 lp_build_context_init(&bld.float_size_in_bld, gallivm, bld.float_size_in_type);
2404 lp_build_context_init(&bld.int_size_bld, gallivm, bld.int_size_type);
2405 lp_build_context_init(&bld.float_size_bld, gallivm, bld.float_size_type);
2406 lp_build_context_init(&bld.texel_bld, gallivm, bld.texel_type);
2407 lp_build_context_init(&bld.levelf_bld, gallivm, bld.levelf_type);
2408 lp_build_context_init(&bld.leveli_bld, gallivm, bld.leveli_type);
2409 lp_build_context_init(&bld.lodf_bld, gallivm, bld.lodf_type);
2410 lp_build_context_init(&bld.lodi_bld, gallivm, bld.lodi_type);
2411
2412 /* Get the dynamic state */
2413 tex_width = dynamic_state->width(dynamic_state, gallivm, texture_index);
2414 bld.row_stride_array = dynamic_state->row_stride(dynamic_state, gallivm, texture_index);
2415 bld.img_stride_array = dynamic_state->img_stride(dynamic_state, gallivm, texture_index);
2416 bld.base_ptr = dynamic_state->base_ptr(dynamic_state, gallivm, texture_index);
2417 bld.mip_offsets = dynamic_state->mip_offsets(dynamic_state, gallivm, texture_index);
2418 /* Note that mip_offsets is an array[level] of offsets to texture images */
2419
2420 /* width, height, depth as single int vector */
2421 if (dims <= 1) {
2422 bld.int_size = tex_width;
2423 }
2424 else {
2425 bld.int_size = LLVMBuildInsertElement(builder, bld.int_size_in_bld.undef,
2426 tex_width, LLVMConstInt(i32t, 0, 0), "");
2427 if (dims >= 2) {
2428 LLVMValueRef tex_height =
2429 dynamic_state->height(dynamic_state, gallivm, texture_index);
2430 bld.int_size = LLVMBuildInsertElement(builder, bld.int_size,
2431 tex_height, LLVMConstInt(i32t, 1, 0), "");
2432 if (dims >= 3) {
2433 LLVMValueRef tex_depth =
2434 dynamic_state->depth(dynamic_state, gallivm, texture_index);
2435 bld.int_size = LLVMBuildInsertElement(builder, bld.int_size,
2436 tex_depth, LLVMConstInt(i32t, 2, 0), "");
2437 }
2438 }
2439 }
2440
2441 for (i = 0; i < 5; i++) {
2442 newcoords[i] = coords[i];
2443 }
2444
2445 if (0) {
2446 /* For debug: no-op texture sampling */
2447 lp_build_sample_nop(gallivm,
2448 bld.texel_type,
2449 newcoords,
2450 texel_out);
2451 }
2452
2453 else if (is_fetch) {
2454 lp_build_fetch_texel(&bld, texture_index, newcoords,
2455 explicit_lod, offsets,
2456 texel_out);
2457 }
2458
2459 else {
2460 LLVMValueRef lod_fpart = NULL, lod_positive = NULL;
2461 LLVMValueRef ilevel0 = NULL, ilevel1 = NULL;
2462 boolean use_aos = util_format_fits_8unorm(bld.format_desc) &&
2463 /* not sure this is strictly needed or simply impossible */
2464 derived_sampler_state.compare_mode == PIPE_TEX_COMPARE_NONE &&
2465 lp_is_simple_wrap_mode(derived_sampler_state.wrap_s);
2466
2467 use_aos &= bld.num_lods <= num_quads ||
2468 derived_sampler_state.min_img_filter ==
2469 derived_sampler_state.mag_img_filter;
2470 if (dims > 1) {
2471 use_aos &= lp_is_simple_wrap_mode(derived_sampler_state.wrap_t);
2472 if (dims > 2) {
2473 use_aos &= lp_is_simple_wrap_mode(derived_sampler_state.wrap_r);
2474 }
2475 }
2476 if (static_texture_state->target == PIPE_TEXTURE_CUBE &&
2477 derived_sampler_state.seamless_cube_map &&
2478 (derived_sampler_state.min_img_filter == PIPE_TEX_FILTER_LINEAR ||
2479 derived_sampler_state.mag_img_filter == PIPE_TEX_FILTER_LINEAR)) {
2480 /* theoretically possible with AoS filtering but not implemented (complex!) */
2481 use_aos = 0;
2482 }
2483
2484 if ((gallivm_debug & GALLIVM_DEBUG_PERF) &&
2485 !use_aos && util_format_fits_8unorm(bld.format_desc)) {
2486 debug_printf("%s: using floating point linear filtering for %s\n",
2487 __FUNCTION__, bld.format_desc->short_name);
2488 debug_printf(" min_img %d mag_img %d mip %d target %d seamless %d"
2489 " wraps %d wrapt %d wrapr %d\n",
2490 derived_sampler_state.min_img_filter,
2491 derived_sampler_state.mag_img_filter,
2492 derived_sampler_state.min_mip_filter,
2493 static_texture_state->target,
2494 derived_sampler_state.seamless_cube_map,
2495 derived_sampler_state.wrap_s,
2496 derived_sampler_state.wrap_t,
2497 derived_sampler_state.wrap_r);
2498 }
2499
2500 lp_build_sample_common(&bld, texture_index, sampler_index,
2501 newcoords,
2502 derivs, lod_bias, explicit_lod,
2503 &lod_positive, &lod_fpart,
2504 &ilevel0, &ilevel1);
2505
2506 /*
2507 * we only try 8-wide sampling with soa as it appears to
2508 * be a loss with aos with AVX (but it should work, except
2509 * for conformance if min_filter != mag_filter if num_lods > 1).
2510 * (It should be faster if we'd support avx2)
2511 */
2512 if (num_quads == 1 || !use_aos) {
2513 if (use_aos) {
2514 /* do sampling/filtering with fixed pt arithmetic */
2515 lp_build_sample_aos(&bld, sampler_index,
2516 newcoords[0], newcoords[1],
2517 newcoords[2],
2518 offsets, lod_positive, lod_fpart,
2519 ilevel0, ilevel1,
2520 texel_out);
2521 }
2522
2523 else {
2524 lp_build_sample_general(&bld, sampler_index,
2525 newcoords, offsets,
2526 lod_positive, lod_fpart,
2527 ilevel0, ilevel1,
2528 texel_out);
2529 }
2530 }
2531 else {
2532 unsigned j;
2533 struct lp_build_sample_context bld4;
2534 struct lp_type type4 = type;
2535 unsigned i;
2536 LLVMValueRef texelout4[4];
2537 LLVMValueRef texelouttmp[4][LP_MAX_VECTOR_LENGTH/16];
2538
2539 type4.length = 4;
2540
2541 /* Setup our build context */
2542 memset(&bld4, 0, sizeof bld4);
2543 bld4.gallivm = bld.gallivm;
2544 bld4.static_texture_state = bld.static_texture_state;
2545 bld4.static_sampler_state = bld.static_sampler_state;
2546 bld4.dynamic_state = bld.dynamic_state;
2547 bld4.format_desc = bld.format_desc;
2548 bld4.dims = bld.dims;
2549 bld4.row_stride_array = bld.row_stride_array;
2550 bld4.img_stride_array = bld.img_stride_array;
2551 bld4.base_ptr = bld.base_ptr;
2552 bld4.mip_offsets = bld.mip_offsets;
2553 bld4.int_size = bld.int_size;
2554
2555 bld4.vector_width = lp_type_width(type4);
2556
2557 bld4.float_type = lp_type_float(32);
2558 bld4.int_type = lp_type_int(32);
2559 bld4.coord_type = type4;
2560 bld4.int_coord_type = lp_int_type(type4);
2561 bld4.float_size_in_type = lp_type_float(32);
2562 bld4.float_size_in_type.length = dims > 1 ? 4 : 1;
2563 bld4.int_size_in_type = lp_int_type(bld4.float_size_in_type);
2564 bld4.texel_type = bld.texel_type;
2565 bld4.texel_type.length = 4;
2566
2567 bld4.num_mips = bld4.num_lods = 1;
2568 if ((gallivm_debug & GALLIVM_DEBUG_NO_QUAD_LOD) &&
2569 (gallivm_debug & GALLIVM_DEBUG_NO_RHO_APPROX) &&
2570 (static_texture_state->target == PIPE_TEXTURE_CUBE) &&
2571 (!is_fetch && mip_filter != PIPE_TEX_MIPFILTER_NONE)) {
2572 bld4.num_mips = type4.length;
2573 bld4.num_lods = type4.length;
2574 }
2575 if (lod_property == LP_SAMPLER_LOD_PER_ELEMENT &&
2576 (explicit_lod || lod_bias || derivs)) {
2577 if ((is_fetch && target != PIPE_BUFFER) ||
2578 (!is_fetch && mip_filter != PIPE_TEX_MIPFILTER_NONE)) {
2579 bld4.num_mips = type4.length;
2580 bld4.num_lods = type4.length;
2581 }
2582 else if (!is_fetch && min_img_filter != mag_img_filter) {
2583 bld4.num_mips = 1;
2584 bld4.num_lods = type4.length;
2585 }
2586 }
2587
2588 /* we want native vector size to be able to use our intrinsics */
2589 bld4.lodf_type = type4;
2590 if (bld4.num_lods != type4.length) {
2591 bld4.lodf_type.length = 1;
2592 }
2593 bld4.lodi_type = lp_int_type(bld4.lodf_type);
2594 bld4.levelf_type = type4;
2595 if (bld4.num_mips != type4.length) {
2596 bld4.levelf_type.length = 1;
2597 }
2598 bld4.leveli_type = lp_int_type(bld4.levelf_type);
2599 bld4.float_size_type = bld4.float_size_in_type;
2600 if (bld4.num_mips > 1) {
2601 bld4.float_size_type.length = bld4.num_mips == type4.length ?
2602 bld4.num_mips * bld4.float_size_in_type.length :
2603 type4.length;
2604 }
2605 bld4.int_size_type = lp_int_type(bld4.float_size_type);
2606
2607 lp_build_context_init(&bld4.float_bld, gallivm, bld4.float_type);
2608 lp_build_context_init(&bld4.float_vec_bld, gallivm, type4);
2609 lp_build_context_init(&bld4.int_bld, gallivm, bld4.int_type);
2610 lp_build_context_init(&bld4.coord_bld, gallivm, bld4.coord_type);
2611 lp_build_context_init(&bld4.int_coord_bld, gallivm, bld4.int_coord_type);
2612 lp_build_context_init(&bld4.int_size_in_bld, gallivm, bld4.int_size_in_type);
2613 lp_build_context_init(&bld4.float_size_in_bld, gallivm, bld4.float_size_in_type);
2614 lp_build_context_init(&bld4.int_size_bld, gallivm, bld4.int_size_type);
2615 lp_build_context_init(&bld4.float_size_bld, gallivm, bld4.float_size_type);
2616 lp_build_context_init(&bld4.texel_bld, gallivm, bld4.texel_type);
2617 lp_build_context_init(&bld4.levelf_bld, gallivm, bld4.levelf_type);
2618 lp_build_context_init(&bld4.leveli_bld, gallivm, bld4.leveli_type);
2619 lp_build_context_init(&bld4.lodf_bld, gallivm, bld4.lodf_type);
2620 lp_build_context_init(&bld4.lodi_bld, gallivm, bld4.lodi_type);
2621
2622 for (i = 0; i < num_quads; i++) {
2623 LLVMValueRef s4, t4, r4;
2624 LLVMValueRef lod_positive4, lod_fpart4 = NULL;
2625 LLVMValueRef ilevel04, ilevel14 = NULL;
2626 LLVMValueRef offsets4[4] = { NULL };
2627 unsigned num_lods = bld4.num_lods;
2628
2629 s4 = lp_build_extract_range(gallivm, newcoords[0], 4*i, 4);
2630 t4 = lp_build_extract_range(gallivm, newcoords[1], 4*i, 4);
2631 r4 = lp_build_extract_range(gallivm, newcoords[2], 4*i, 4);
2632
2633 if (offsets[0]) {
2634 offsets4[0] = lp_build_extract_range(gallivm, offsets[0], 4*i, 4);
2635 if (dims > 1) {
2636 offsets4[1] = lp_build_extract_range(gallivm, offsets[1], 4*i, 4);
2637 if (dims > 2) {
2638 offsets4[2] = lp_build_extract_range(gallivm, offsets[2], 4*i, 4);
2639 }
2640 }
2641 }
2642 lod_positive4 = lp_build_extract_range(gallivm, lod_positive, num_lods * i, num_lods);
2643 ilevel04 = bld.num_mips == 1 ? ilevel0 :
2644 lp_build_extract_range(gallivm, ilevel0, num_lods * i, num_lods);
2645 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
2646 ilevel14 = lp_build_extract_range(gallivm, ilevel1, num_lods * i, num_lods);
2647 lod_fpart4 = lp_build_extract_range(gallivm, lod_fpart, num_lods * i, num_lods);
2648 }
2649
2650 if (use_aos) {
2651 /* do sampling/filtering with fixed pt arithmetic */
2652 lp_build_sample_aos(&bld4, sampler_index,
2653 s4, t4, r4, offsets4,
2654 lod_positive4, lod_fpart4,
2655 ilevel04, ilevel14,
2656 texelout4);
2657 }
2658
2659 else {
2660 /* this path is currently unreachable and hence might break easily... */
2661 LLVMValueRef newcoords4[5];
2662 newcoords4[0] = s4;
2663 newcoords4[1] = t4;
2664 newcoords4[2] = r4;
2665 newcoords4[3] = lp_build_extract_range(gallivm, newcoords[3], 4*i, 4);
2666 newcoords4[4] = lp_build_extract_range(gallivm, newcoords[4], 4*i, 4);
2667
2668 lp_build_sample_general(&bld4, sampler_index,
2669 newcoords4, offsets4,
2670 lod_positive4, lod_fpart4,
2671 ilevel04, ilevel14,
2672 texelout4);
2673 }
2674 for (j = 0; j < 4; j++) {
2675 texelouttmp[j][i] = texelout4[j];
2676 }
2677 }
2678
2679 for (j = 0; j < 4; j++) {
2680 texel_out[j] = lp_build_concat(gallivm, texelouttmp[j], type4, num_quads);
2681 }
2682 }
2683 }
2684
2685 if (target != PIPE_BUFFER) {
2686 apply_sampler_swizzle(&bld, texel_out);
2687 }
2688
2689 /*
2690 * texel type can be a (32bit) int/uint (for pure int formats only),
2691 * however we are expected to always return floats (storage is untyped).
2692 */
2693 if (!bld.texel_type.floating) {
2694 unsigned chan;
2695 for (chan = 0; chan < 4; chan++) {
2696 texel_out[chan] = LLVMBuildBitCast(builder, texel_out[chan],
2697 lp_build_vec_type(gallivm, type), "");
2698 }
2699 }
2700 }
2701
2702 void
2703 lp_build_size_query_soa(struct gallivm_state *gallivm,
2704 const struct lp_static_texture_state *static_state,
2705 struct lp_sampler_dynamic_state *dynamic_state,
2706 struct lp_type int_type,
2707 unsigned texture_unit,
2708 unsigned target,
2709 boolean is_sviewinfo,
2710 enum lp_sampler_lod_property lod_property,
2711 LLVMValueRef explicit_lod,
2712 LLVMValueRef *sizes_out)
2713 {
2714 LLVMValueRef lod, level, size;
2715 LLVMValueRef first_level = NULL;
2716 int dims, i;
2717 boolean has_array;
2718 unsigned num_lods = 1;
2719 struct lp_build_context bld_int_vec4;
2720
2721 if (static_state->format == PIPE_FORMAT_NONE) {
2722 /*
2723 * If there's nothing bound, format is NONE, and we must return
2724 * all zero as mandated by d3d10 in this case.
2725 */
2726 unsigned chan;
2727 LLVMValueRef zero = lp_build_const_vec(gallivm, int_type, 0.0F);
2728 for (chan = 0; chan < 4; chan++) {
2729 sizes_out[chan] = zero;
2730 }
2731 return;
2732 }
2733
2734 /*
2735 * Do some sanity verification about bound texture and shader dcl target.
2736 * Not entirely sure what's possible but assume array/non-array
2737 * always compatible (probably not ok for OpenGL but d3d10 has no
2738 * distinction of arrays at the resource level).
2739 * Everything else looks bogus (though not entirely sure about rect/2d).
2740 * Currently disabled because it causes assertion failures if there's
2741 * nothing bound (or rather a dummy texture, not that this case would
2742 * return the right values).
2743 */
2744 if (0 && static_state->target != target) {
2745 if (static_state->target == PIPE_TEXTURE_1D)
2746 assert(target == PIPE_TEXTURE_1D_ARRAY);
2747 else if (static_state->target == PIPE_TEXTURE_1D_ARRAY)
2748 assert(target == PIPE_TEXTURE_1D);
2749 else if (static_state->target == PIPE_TEXTURE_2D)
2750 assert(target == PIPE_TEXTURE_2D_ARRAY);
2751 else if (static_state->target == PIPE_TEXTURE_2D_ARRAY)
2752 assert(target == PIPE_TEXTURE_2D);
2753 else if (static_state->target == PIPE_TEXTURE_CUBE)
2754 assert(target == PIPE_TEXTURE_CUBE_ARRAY);
2755 else if (static_state->target == PIPE_TEXTURE_CUBE_ARRAY)
2756 assert(target == PIPE_TEXTURE_CUBE);
2757 else
2758 assert(0);
2759 }
2760
2761 dims = texture_dims(target);
2762
2763 switch (target) {
2764 case PIPE_TEXTURE_1D_ARRAY:
2765 case PIPE_TEXTURE_2D_ARRAY:
2766 has_array = TRUE;
2767 break;
2768 default:
2769 has_array = FALSE;
2770 break;
2771 }
2772
2773 assert(!int_type.floating);
2774
2775 lp_build_context_init(&bld_int_vec4, gallivm, lp_type_int_vec(32, 128));
2776
2777 if (explicit_lod) {
2778 /* FIXME: this needs to honor per-element lod */
2779 lod = LLVMBuildExtractElement(gallivm->builder, explicit_lod, lp_build_const_int32(gallivm, 0), "");
2780 first_level = dynamic_state->first_level(dynamic_state, gallivm, texture_unit);
2781 level = LLVMBuildAdd(gallivm->builder, lod, first_level, "level");
2782 lod = lp_build_broadcast_scalar(&bld_int_vec4, level);
2783 } else {
2784 lod = bld_int_vec4.zero;
2785 }
2786
2787 size = bld_int_vec4.undef;
2788
2789 size = LLVMBuildInsertElement(gallivm->builder, size,
2790 dynamic_state->width(dynamic_state, gallivm, texture_unit),
2791 lp_build_const_int32(gallivm, 0), "");
2792
2793 if (dims >= 2) {
2794 size = LLVMBuildInsertElement(gallivm->builder, size,
2795 dynamic_state->height(dynamic_state, gallivm, texture_unit),
2796 lp_build_const_int32(gallivm, 1), "");
2797 }
2798
2799 if (dims >= 3) {
2800 size = LLVMBuildInsertElement(gallivm->builder, size,
2801 dynamic_state->depth(dynamic_state, gallivm, texture_unit),
2802 lp_build_const_int32(gallivm, 2), "");
2803 }
2804
2805 size = lp_build_minify(&bld_int_vec4, size, lod);
2806
2807 if (has_array)
2808 size = LLVMBuildInsertElement(gallivm->builder, size,
2809 dynamic_state->depth(dynamic_state, gallivm, texture_unit),
2810 lp_build_const_int32(gallivm, dims), "");
2811
2812 /*
2813 * d3d10 requires zero for x/y/z values (but not w, i.e. mip levels)
2814 * if level is out of bounds (note this can't cover unbound texture
2815 * here, which also requires returning zero).
2816 */
2817 if (explicit_lod && is_sviewinfo) {
2818 LLVMValueRef last_level, out, out1;
2819 struct lp_build_context leveli_bld;
2820
2821 /* everything is scalar for now */
2822 lp_build_context_init(&leveli_bld, gallivm, lp_type_int_vec(32, 32));
2823 last_level = dynamic_state->last_level(dynamic_state, gallivm, texture_unit);
2824
2825 out = lp_build_cmp(&leveli_bld, PIPE_FUNC_LESS, level, first_level);
2826 out1 = lp_build_cmp(&leveli_bld, PIPE_FUNC_GREATER, level, last_level);
2827 out = lp_build_or(&leveli_bld, out, out1);
2828 if (num_lods == 1) {
2829 out = lp_build_broadcast_scalar(&bld_int_vec4, out);
2830 }
2831 else {
2832 /* TODO */
2833 assert(0);
2834 }
2835 size = lp_build_andnot(&bld_int_vec4, size, out);
2836 }
2837 for (i = 0; i < dims + (has_array ? 1 : 0); i++) {
2838 sizes_out[i] = lp_build_extract_broadcast(gallivm, bld_int_vec4.type, int_type,
2839 size,
2840 lp_build_const_int32(gallivm, i));
2841 }
2842 if (is_sviewinfo) {
2843 for (; i < 4; i++) {
2844 sizes_out[i] = lp_build_const_vec(gallivm, int_type, 0.0);
2845 }
2846 }
2847
2848 /*
2849 * if there's no explicit_lod (buffers, rects) queries requiring nr of
2850 * mips would be illegal.
2851 */
2852 if (is_sviewinfo && explicit_lod) {
2853 struct lp_build_context bld_int_scalar;
2854 LLVMValueRef num_levels;
2855 lp_build_context_init(&bld_int_scalar, gallivm, lp_type_int(32));
2856
2857 if (static_state->level_zero_only) {
2858 num_levels = bld_int_scalar.one;
2859 }
2860 else {
2861 LLVMValueRef last_level;
2862
2863 last_level = dynamic_state->last_level(dynamic_state, gallivm, texture_unit);
2864 num_levels = lp_build_sub(&bld_int_scalar, last_level, first_level);
2865 num_levels = lp_build_add(&bld_int_scalar, num_levels, bld_int_scalar.one);
2866 }
2867 sizes_out[3] = lp_build_broadcast(gallivm, lp_build_vec_type(gallivm, int_type),
2868 num_levels);
2869 }
2870 }