gallivm: fix some bugs on the 1D texture paths
[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 */
34
35 #include "pipe/p_defines.h"
36 #include "pipe/p_state.h"
37 #include "util/u_debug.h"
38 #include "util/u_dump.h"
39 #include "util/u_memory.h"
40 #include "util/u_math.h"
41 #include "util/u_format.h"
42 #include "util/u_cpu_detect.h"
43 #include "lp_bld_debug.h"
44 #include "lp_bld_type.h"
45 #include "lp_bld_const.h"
46 #include "lp_bld_conv.h"
47 #include "lp_bld_arit.h"
48 #include "lp_bld_logic.h"
49 #include "lp_bld_swizzle.h"
50 #include "lp_bld_pack.h"
51 #include "lp_bld_format.h"
52 #include "lp_bld_sample.h"
53
54
55 /**
56 * Keep all information for sampling code generation in a single place.
57 */
58 struct lp_build_sample_context
59 {
60 LLVMBuilderRef builder;
61
62 const struct lp_sampler_static_state *static_state;
63
64 struct lp_sampler_dynamic_state *dynamic_state;
65
66 const struct util_format_description *format_desc;
67
68 /** regular scalar float type */
69 struct lp_type float_type;
70 struct lp_build_context float_bld;
71
72 /** regular scalar float type */
73 struct lp_type int_type;
74 struct lp_build_context int_bld;
75
76 /** Incoming coordinates type and build context */
77 struct lp_type coord_type;
78 struct lp_build_context coord_bld;
79
80 /** Unsigned integer coordinates */
81 struct lp_type uint_coord_type;
82 struct lp_build_context uint_coord_bld;
83
84 /** Signed integer coordinates */
85 struct lp_type int_coord_type;
86 struct lp_build_context int_coord_bld;
87
88 /** Output texels type and build context */
89 struct lp_type texel_type;
90 struct lp_build_context texel_bld;
91 };
92
93
94 /**
95 * Does the given texture wrap mode allow sampling the texture border color?
96 * XXX maybe move this into gallium util code.
97 */
98 static boolean
99 wrap_mode_uses_border_color(unsigned mode)
100 {
101 switch (mode) {
102 case PIPE_TEX_WRAP_REPEAT:
103 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
104 case PIPE_TEX_WRAP_MIRROR_REPEAT:
105 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
106 return FALSE;
107 case PIPE_TEX_WRAP_CLAMP:
108 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
109 case PIPE_TEX_WRAP_MIRROR_CLAMP:
110 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
111 return TRUE;
112 default:
113 assert(0 && "unexpected wrap mode");
114 return FALSE;
115 }
116 }
117
118
119 static LLVMValueRef
120 lp_build_get_mipmap_level(struct lp_build_sample_context *bld,
121 LLVMValueRef data_array, LLVMValueRef level)
122 {
123 LLVMValueRef indexes[2], data_ptr;
124 indexes[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
125 indexes[1] = level;
126 data_ptr = LLVMBuildGEP(bld->builder, data_array, indexes, 2, "");
127 data_ptr = LLVMBuildLoad(bld->builder, data_ptr, "");
128 return data_ptr;
129 }
130
131
132 static LLVMValueRef
133 lp_build_get_const_mipmap_level(struct lp_build_sample_context *bld,
134 LLVMValueRef data_array, int level)
135 {
136 LLVMValueRef lvl = LLVMConstInt(LLVMInt32Type(), level, 0);
137 return lp_build_get_mipmap_level(bld, data_array, lvl);
138 }
139
140
141 static int
142 texture_dims(enum pipe_texture_target tex)
143 {
144 switch (tex) {
145 case PIPE_TEXTURE_1D:
146 return 1;
147 case PIPE_TEXTURE_2D:
148 case PIPE_TEXTURE_CUBE:
149 return 2;
150 case PIPE_TEXTURE_3D:
151 return 3;
152 default:
153 assert(0 && "bad texture target in texture_dims()");
154 return 2;
155 }
156 }
157
158
159
160 /**
161 * Generate code to fetch a texel from a texture at int coords (x, y, z).
162 * The computation depends on whether the texture is 1D, 2D or 3D.
163 * The result, texel, will be:
164 * texel[0] = red values
165 * texel[1] = green values
166 * texel[2] = blue values
167 * texel[3] = alpha values
168 */
169 static void
170 lp_build_sample_texel_soa(struct lp_build_sample_context *bld,
171 LLVMValueRef width,
172 LLVMValueRef height,
173 LLVMValueRef depth,
174 LLVMValueRef x,
175 LLVMValueRef y,
176 LLVMValueRef z,
177 LLVMValueRef y_stride,
178 LLVMValueRef z_stride,
179 LLVMValueRef data_ptr,
180 LLVMValueRef *texel)
181 {
182 const int dims = texture_dims(bld->static_state->target);
183 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
184 LLVMValueRef offset;
185 LLVMValueRef packed;
186 LLVMValueRef use_border = NULL;
187
188 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
189 if (wrap_mode_uses_border_color(bld->static_state->wrap_s)) {
190 LLVMValueRef b1, b2;
191 b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, x, int_coord_bld->zero);
192 b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, x, width);
193 use_border = LLVMBuildOr(bld->builder, b1, b2, "b1_or_b2");
194 }
195
196 if (dims >= 2 && wrap_mode_uses_border_color(bld->static_state->wrap_t)) {
197 LLVMValueRef b1, b2;
198 b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, y, int_coord_bld->zero);
199 b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, y, height);
200 if (use_border) {
201 use_border = LLVMBuildOr(bld->builder, use_border, b1, "ub_or_b1");
202 use_border = LLVMBuildOr(bld->builder, use_border, b2, "ub_or_b2");
203 }
204 else {
205 use_border = LLVMBuildOr(bld->builder, b1, b2, "b1_or_b2");
206 }
207 }
208
209 if (dims == 3 && wrap_mode_uses_border_color(bld->static_state->wrap_r)) {
210 LLVMValueRef b1, b2;
211 b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, z, int_coord_bld->zero);
212 b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, z, depth);
213 if (use_border) {
214 use_border = LLVMBuildOr(bld->builder, use_border, b1, "ub_or_b1");
215 use_border = LLVMBuildOr(bld->builder, use_border, b2, "ub_or_b2");
216 }
217 else {
218 use_border = LLVMBuildOr(bld->builder, b1, b2, "b1_or_b2");
219 }
220 }
221
222 /*
223 * Note: if we find an app which frequently samples the texture border
224 * we might want to implement a true conditional here to avoid sampling
225 * the texture whenever possible (since that's quite a bit of code).
226 * Ex:
227 * if (use_border) {
228 * texel = border_color;
229 * }
230 * else {
231 * texel = sample_texture(coord);
232 * }
233 * As it is now, we always sample the texture, then selectively replace
234 * the texel color results with the border color.
235 */
236
237 /* convert x,y,z coords to linear offset from start of texture, in bytes */
238 offset = lp_build_sample_offset(&bld->uint_coord_bld,
239 bld->format_desc,
240 x, y, z, y_stride, z_stride);
241
242 assert(bld->format_desc->block.width == 1);
243 assert(bld->format_desc->block.height == 1);
244 assert(bld->format_desc->block.bits <= bld->texel_type.width);
245
246 /* gather the texels from the texture */
247 packed = lp_build_gather(bld->builder,
248 bld->texel_type.length,
249 bld->format_desc->block.bits,
250 bld->texel_type.width,
251 data_ptr, offset);
252
253 texel[0] = texel[1] = texel[2] = texel[3] = NULL;
254
255 /* convert texels to float rgba */
256 lp_build_unpack_rgba_soa(bld->builder,
257 bld->format_desc,
258 bld->texel_type,
259 packed, texel);
260
261 if (use_border) {
262 /* select texel color or border color depending on use_border */
263 int chan;
264 for (chan = 0; chan < 4; chan++) {
265 LLVMValueRef border_chan =
266 lp_build_const_scalar(bld->texel_type,
267 bld->static_state->border_color[chan]);
268 texel[chan] = lp_build_select(&bld->texel_bld, use_border,
269 border_chan, texel[chan]);
270 }
271 }
272 }
273
274
275 static LLVMValueRef
276 lp_build_sample_packed(struct lp_build_sample_context *bld,
277 LLVMValueRef x,
278 LLVMValueRef y,
279 LLVMValueRef y_stride,
280 LLVMValueRef data_array)
281 {
282 LLVMValueRef offset;
283 LLVMValueRef data_ptr;
284
285 offset = lp_build_sample_offset(&bld->uint_coord_bld,
286 bld->format_desc,
287 x, y, NULL, y_stride, NULL);
288
289 assert(bld->format_desc->block.width == 1);
290 assert(bld->format_desc->block.height == 1);
291 assert(bld->format_desc->block.bits <= bld->texel_type.width);
292
293 /* get pointer to mipmap level 0 data */
294 data_ptr = lp_build_get_const_mipmap_level(bld, data_array, 0);
295
296 return lp_build_gather(bld->builder,
297 bld->texel_type.length,
298 bld->format_desc->block.bits,
299 bld->texel_type.width,
300 data_ptr, offset);
301 }
302
303
304 /**
305 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
306 */
307 static LLVMValueRef
308 lp_build_coord_mirror(struct lp_build_sample_context *bld,
309 LLVMValueRef coord)
310 {
311 struct lp_build_context *coord_bld = &bld->coord_bld;
312 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
313 LLVMValueRef fract, flr, isOdd;
314
315 /* fract = coord - floor(coord) */
316 fract = lp_build_sub(coord_bld, coord, lp_build_floor(coord_bld, coord));
317
318 /* flr = ifloor(coord); */
319 flr = lp_build_ifloor(coord_bld, coord);
320
321 /* isOdd = flr & 1 */
322 isOdd = LLVMBuildAnd(bld->builder, flr, int_coord_bld->one, "");
323
324 /* make coord positive or negative depending on isOdd */
325 coord = lp_build_set_sign(coord_bld, fract, isOdd);
326
327 /* convert isOdd to float */
328 isOdd = lp_build_int_to_float(coord_bld, isOdd);
329
330 /* add isOdd to coord */
331 coord = lp_build_add(coord_bld, coord, isOdd);
332
333 return coord;
334 }
335
336
337 /**
338 * We only support a few wrap modes in lp_build_sample_wrap_int() at this time.
339 * Return whether the given mode is supported by that function.
340 */
341 static boolean
342 is_simple_wrap_mode(unsigned mode)
343 {
344 switch (mode) {
345 case PIPE_TEX_WRAP_REPEAT:
346 case PIPE_TEX_WRAP_CLAMP:
347 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
348 return TRUE;
349 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
350 default:
351 return FALSE;
352 }
353 }
354
355
356 /**
357 * Build LLVM code for texture wrap mode, for scaled integer texcoords.
358 * \param coord the incoming texcoord (s,t,r or q) scaled to the texture size
359 * \param length the texture size along one dimension
360 * \param is_pot if TRUE, length is a power of two
361 * \param wrap_mode one of PIPE_TEX_WRAP_x
362 */
363 static LLVMValueRef
364 lp_build_sample_wrap_int(struct lp_build_sample_context *bld,
365 LLVMValueRef coord,
366 LLVMValueRef length,
367 boolean is_pot,
368 unsigned wrap_mode)
369 {
370 struct lp_build_context *uint_coord_bld = &bld->uint_coord_bld;
371 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
372 LLVMValueRef length_minus_one;
373
374 length_minus_one = lp_build_sub(uint_coord_bld, length, uint_coord_bld->one);
375
376 switch(wrap_mode) {
377 case PIPE_TEX_WRAP_REPEAT:
378 if(is_pot)
379 coord = LLVMBuildAnd(bld->builder, coord, length_minus_one, "");
380 else
381 /* Signed remainder won't give the right results for negative
382 * dividends but unsigned remainder does.*/
383 coord = LLVMBuildURem(bld->builder, coord, length, "");
384 break;
385
386 case PIPE_TEX_WRAP_CLAMP:
387 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
388 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
389 coord = lp_build_max(int_coord_bld, coord, int_coord_bld->zero);
390 coord = lp_build_min(int_coord_bld, coord, length_minus_one);
391 break;
392
393 case PIPE_TEX_WRAP_MIRROR_REPEAT:
394 case PIPE_TEX_WRAP_MIRROR_CLAMP:
395 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
396 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
397 /* FIXME */
398 _debug_printf("llvmpipe: failed to translate texture wrap mode %s\n",
399 util_dump_tex_wrap(wrap_mode, TRUE));
400 coord = lp_build_max(uint_coord_bld, coord, uint_coord_bld->zero);
401 coord = lp_build_min(uint_coord_bld, coord, length_minus_one);
402 break;
403
404 default:
405 assert(0);
406 }
407
408 return coord;
409 }
410
411
412 /**
413 * Build LLVM code for texture wrap mode for linear filtering.
414 * \param x0_out returns first integer texcoord
415 * \param x1_out returns second integer texcoord
416 * \param weight_out returns linear interpolation weight
417 */
418 static void
419 lp_build_sample_wrap_linear(struct lp_build_sample_context *bld,
420 LLVMValueRef coord,
421 LLVMValueRef length,
422 boolean is_pot,
423 unsigned wrap_mode,
424 LLVMValueRef *x0_out,
425 LLVMValueRef *x1_out,
426 LLVMValueRef *weight_out)
427 {
428 struct lp_build_context *coord_bld = &bld->coord_bld;
429 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
430 struct lp_build_context *uint_coord_bld = &bld->uint_coord_bld;
431 LLVMValueRef two = lp_build_const_scalar(coord_bld->type, 2.0);
432 LLVMValueRef half = lp_build_const_scalar(coord_bld->type, 0.5);
433 LLVMValueRef length_f = lp_build_int_to_float(coord_bld, length);
434 LLVMValueRef length_minus_one = lp_build_sub(uint_coord_bld, length, uint_coord_bld->one);
435 LLVMValueRef length_f_minus_one = lp_build_sub(coord_bld, length_f, coord_bld->one);
436 LLVMValueRef coord0, coord1, weight;
437
438 switch(wrap_mode) {
439 case PIPE_TEX_WRAP_REPEAT:
440 /* mul by size and subtract 0.5 */
441 coord = lp_build_mul(coord_bld, coord, length_f);
442 coord = lp_build_sub(coord_bld, coord, half);
443 /* convert to int */
444 coord0 = lp_build_ifloor(coord_bld, coord);
445 coord1 = lp_build_add(uint_coord_bld, coord0, uint_coord_bld->one);
446 /* compute lerp weight */
447 weight = lp_build_fract(coord_bld, coord);
448 /* repeat wrap */
449 if (is_pot) {
450 coord0 = LLVMBuildAnd(bld->builder, coord0, length_minus_one, "");
451 coord1 = LLVMBuildAnd(bld->builder, coord1, length_minus_one, "");
452 }
453 else {
454 /* Signed remainder won't give the right results for negative
455 * dividends but unsigned remainder does.*/
456 coord0 = LLVMBuildURem(bld->builder, coord0, length, "");
457 coord1 = LLVMBuildURem(bld->builder, coord1, length, "");
458 }
459 break;
460
461 case PIPE_TEX_WRAP_CLAMP:
462 if (bld->static_state->normalized_coords) {
463 coord = lp_build_mul(coord_bld, coord, length_f);
464 }
465 weight = lp_build_fract(coord_bld, coord);
466 coord0 = lp_build_clamp(coord_bld, coord, coord_bld->zero,
467 length_f_minus_one);
468 coord1 = lp_build_add(coord_bld, coord, coord_bld->one);
469 coord1 = lp_build_clamp(coord_bld, coord1, coord_bld->zero,
470 length_f_minus_one);
471 coord0 = lp_build_ifloor(coord_bld, coord0);
472 coord1 = lp_build_ifloor(coord_bld, coord1);
473 break;
474
475 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
476 if (bld->static_state->normalized_coords) {
477 /* clamp to [0,1] */
478 coord = lp_build_clamp(coord_bld, coord, coord_bld->zero, coord_bld->one);
479 /* mul by tex size and subtract 0.5 */
480 coord = lp_build_mul(coord_bld, coord, length_f);
481 coord = lp_build_sub(coord_bld, coord, half);
482 }
483 else {
484 LLVMValueRef min, max;
485 /* clamp to [0.5, length - 0.5] */
486 min = lp_build_const_scalar(coord_bld->type, 0.5F);
487 max = lp_build_sub(coord_bld, length_f, min);
488 coord = lp_build_clamp(coord_bld, coord, min, max);
489 }
490 /* compute lerp weight */
491 weight = lp_build_fract(coord_bld, coord);
492 /* coord0 = floor(coord); */
493 coord0 = lp_build_ifloor(coord_bld, coord);
494 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
495 /* coord0 = max(coord0, 0) */
496 coord0 = lp_build_max(int_coord_bld, coord0, int_coord_bld->zero);
497 /* coord1 = min(coord1, length-1) */
498 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
499 break;
500
501 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
502 {
503 LLVMValueRef min, max;
504 if (bld->static_state->normalized_coords) {
505 /* min = -1.0 / (2 * length) = -0.5 / length */
506 min = lp_build_mul(coord_bld,
507 lp_build_const_scalar(coord_bld->type, -0.5F),
508 lp_build_rcp(coord_bld, length_f));
509 /* max = 1.0 - min */
510 max = lp_build_sub(coord_bld, coord_bld->one, min);
511 /* coord = clamp(coord, min, max) */
512 coord = lp_build_clamp(coord_bld, coord, min, max);
513 /* scale coord to length (and sub 0.5?) */
514 coord = lp_build_mul(coord_bld, coord, length_f);
515 coord = lp_build_sub(coord_bld, coord, half);
516 }
517 else {
518 /* clamp to [-0.5, length + 0.5] */
519 min = lp_build_const_scalar(coord_bld->type, -0.5F);
520 max = lp_build_sub(coord_bld, length_f, min);
521 coord = lp_build_clamp(coord_bld, coord, min, max);
522 coord = lp_build_sub(coord_bld, coord, half);
523 }
524 /* compute lerp weight */
525 weight = lp_build_fract(coord_bld, coord);
526 /* convert to int */
527 coord0 = lp_build_ifloor(coord_bld, coord);
528 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
529 }
530 break;
531
532 case PIPE_TEX_WRAP_MIRROR_REPEAT:
533 /* compute mirror function */
534 coord = lp_build_coord_mirror(bld, coord);
535
536 /* scale coord to length */
537 coord = lp_build_mul(coord_bld, coord, length_f);
538 coord = lp_build_sub(coord_bld, coord, half);
539
540 /* compute lerp weight */
541 weight = lp_build_fract(coord_bld, coord);
542
543 /* convert to int coords */
544 coord0 = lp_build_ifloor(coord_bld, coord);
545 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
546
547 /* coord0 = max(coord0, 0) */
548 coord0 = lp_build_max(int_coord_bld, coord0, int_coord_bld->zero);
549 /* coord1 = min(coord1, length-1) */
550 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
551 break;
552
553 case PIPE_TEX_WRAP_MIRROR_CLAMP:
554 {
555 LLVMValueRef min, max;
556 /* min = 1.0 / (2 * length) */
557 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
558 /* max = 1.0 - min */
559 max = lp_build_sub(coord_bld, coord_bld->one, min);
560
561 coord = lp_build_abs(coord_bld, coord);
562 coord = lp_build_clamp(coord_bld, coord, min, max);
563 coord = lp_build_mul(coord_bld, coord, length_f);
564 if(0)coord = lp_build_sub(coord_bld, coord, half);
565 weight = lp_build_fract(coord_bld, coord);
566 coord0 = lp_build_ifloor(coord_bld, coord);
567 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
568 }
569 break;
570
571 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
572 {
573 LLVMValueRef min, max;
574 /* min = 1.0 / (2 * length) */
575 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
576 /* max = 1.0 - min */
577 max = lp_build_sub(coord_bld, coord_bld->one, min);
578
579 coord = lp_build_abs(coord_bld, coord);
580 coord = lp_build_clamp(coord_bld, coord, min, max);
581 coord = lp_build_mul(coord_bld, coord, length_f);
582 coord = lp_build_sub(coord_bld, coord, half);
583 weight = lp_build_fract(coord_bld, coord);
584 coord0 = lp_build_ifloor(coord_bld, coord);
585 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
586 }
587 break;
588
589 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
590 {
591 LLVMValueRef min, max;
592 /* min = -1.0 / (2 * length) = -0.5 / length */
593 min = lp_build_mul(coord_bld,
594 lp_build_const_scalar(coord_bld->type, -0.5F),
595 lp_build_rcp(coord_bld, length_f));
596 /* max = 1.0 - min */
597 max = lp_build_sub(coord_bld, coord_bld->one, min);
598
599 coord = lp_build_abs(coord_bld, coord);
600 coord = lp_build_clamp(coord_bld, coord, min, max);
601 coord = lp_build_mul(coord_bld, coord, length_f);
602 coord = lp_build_sub(coord_bld, coord, half);
603 weight = lp_build_fract(coord_bld, coord);
604 coord0 = lp_build_ifloor(coord_bld, coord);
605 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
606 }
607 break;
608
609 default:
610 assert(0);
611 coord0 = NULL;
612 coord1 = NULL;
613 weight = NULL;
614 }
615
616 *x0_out = coord0;
617 *x1_out = coord1;
618 *weight_out = weight;
619 }
620
621
622 /**
623 * Build LLVM code for texture wrap mode for nearest filtering.
624 * \param coord the incoming texcoord (nominally in [0,1])
625 * \param length the texture size along one dimension, as int
626 * \param is_pot if TRUE, length is a power of two
627 * \param wrap_mode one of PIPE_TEX_WRAP_x
628 */
629 static LLVMValueRef
630 lp_build_sample_wrap_nearest(struct lp_build_sample_context *bld,
631 LLVMValueRef coord,
632 LLVMValueRef length,
633 boolean is_pot,
634 unsigned wrap_mode)
635 {
636 struct lp_build_context *coord_bld = &bld->coord_bld;
637 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
638 struct lp_build_context *uint_coord_bld = &bld->uint_coord_bld;
639 LLVMValueRef two = lp_build_const_scalar(coord_bld->type, 2.0);
640 LLVMValueRef length_f = lp_build_int_to_float(coord_bld, length);
641 LLVMValueRef length_minus_one = lp_build_sub(uint_coord_bld, length, uint_coord_bld->one);
642 LLVMValueRef length_f_minus_one = lp_build_sub(coord_bld, length_f, coord_bld->one);
643 LLVMValueRef icoord;
644
645 switch(wrap_mode) {
646 case PIPE_TEX_WRAP_REPEAT:
647 coord = lp_build_mul(coord_bld, coord, length_f);
648 icoord = lp_build_ifloor(coord_bld, coord);
649 if (is_pot)
650 icoord = LLVMBuildAnd(bld->builder, icoord, length_minus_one, "");
651 else
652 /* Signed remainder won't give the right results for negative
653 * dividends but unsigned remainder does.*/
654 icoord = LLVMBuildURem(bld->builder, icoord, length, "");
655 break;
656
657 case PIPE_TEX_WRAP_CLAMP:
658 /* mul by size */
659 if (bld->static_state->normalized_coords) {
660 coord = lp_build_mul(coord_bld, coord, length_f);
661 }
662 /* floor */
663 icoord = lp_build_ifloor(coord_bld, coord);
664 /* clamp to [0, size-1]. Note: int coord builder type */
665 icoord = lp_build_clamp(int_coord_bld, icoord, int_coord_bld->zero,
666 length_minus_one);
667 break;
668
669 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
670 {
671 LLVMValueRef min, max;
672 if (bld->static_state->normalized_coords) {
673 /* min = 1.0 / (2 * length) */
674 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
675 /* max = length - min */
676 max = lp_build_sub(coord_bld, length_f, min);
677 /* scale coord to length */
678 coord = lp_build_mul(coord_bld, coord, length_f);
679 }
680 else {
681 /* clamp to [0.5, length - 0.5] */
682 min = lp_build_const_scalar(coord_bld->type, 0.5F);
683 max = lp_build_sub(coord_bld, length_f, min);
684 }
685 /* coord = clamp(coord, min, max) */
686 coord = lp_build_clamp(coord_bld, coord, min, max);
687 icoord = lp_build_ifloor(coord_bld, coord);
688 }
689 break;
690
691 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
692 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
693 {
694 LLVMValueRef min, max;
695 if (bld->static_state->normalized_coords) {
696 /* min = -1.0 / (2 * length) = -0.5 / length */
697 min = lp_build_mul(coord_bld,
698 lp_build_const_scalar(coord_bld->type, -0.5F),
699 lp_build_rcp(coord_bld, length_f));
700 /* max = length - min */
701 max = lp_build_sub(coord_bld, length_f, min);
702 /* scale coord to length */
703 coord = lp_build_mul(coord_bld, coord, length_f);
704 }
705 else {
706 /* clamp to [-0.5, length + 0.5] */
707 min = lp_build_const_scalar(coord_bld->type, -0.5F);
708 max = lp_build_sub(coord_bld, length_f, min);
709 }
710 /* coord = clamp(coord, min, max) */
711 coord = lp_build_clamp(coord_bld, coord, min, max);
712 icoord = lp_build_ifloor(coord_bld, coord);
713 }
714 break;
715
716 case PIPE_TEX_WRAP_MIRROR_REPEAT:
717 {
718 LLVMValueRef min, max;
719 /* min = 1.0 / (2 * length) */
720 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
721 /* max = length - min */
722 max = lp_build_sub(coord_bld, length_f, min);
723
724 /* compute mirror function */
725 coord = lp_build_coord_mirror(bld, coord);
726
727 /* scale coord to length */
728 coord = lp_build_mul(coord_bld, coord, length_f);
729
730 /* coord = clamp(coord, min, max) */
731 coord = lp_build_clamp(coord_bld, coord, min, max);
732 icoord = lp_build_ifloor(coord_bld, coord);
733 }
734 break;
735
736 case PIPE_TEX_WRAP_MIRROR_CLAMP:
737 coord = lp_build_abs(coord_bld, coord);
738 coord = lp_build_mul(coord_bld, coord, length_f);
739 coord = lp_build_clamp(coord_bld, coord, coord_bld->zero, length_f_minus_one);
740 icoord = lp_build_ifloor(coord_bld, coord);
741 break;
742
743 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
744 {
745 LLVMValueRef min, max;
746 /* min = 1.0 / (2 * length) */
747 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
748 /* max = length - min */
749 max = lp_build_sub(coord_bld, length_f, min);
750
751 coord = lp_build_abs(coord_bld, coord);
752 coord = lp_build_mul(coord_bld, coord, length_f);
753 coord = lp_build_clamp(coord_bld, coord, min, max);
754 icoord = lp_build_ifloor(coord_bld, coord);
755 }
756 break;
757
758 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
759 {
760 LLVMValueRef min, max;
761 /* min = 1.0 / (2 * length) */
762 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
763 min = lp_build_negate(coord_bld, min);
764 /* max = length - min */
765 max = lp_build_sub(coord_bld, length_f, min);
766
767 coord = lp_build_abs(coord_bld, coord);
768 coord = lp_build_mul(coord_bld, coord, length_f);
769 coord = lp_build_clamp(coord_bld, coord, min, max);
770 icoord = lp_build_ifloor(coord_bld, coord);
771 }
772 break;
773
774 default:
775 assert(0);
776 icoord = NULL;
777 }
778
779 return icoord;
780 }
781
782
783 /**
784 * Codegen equivalent for u_minify().
785 * Return max(1, base_size >> level);
786 */
787 static LLVMValueRef
788 lp_build_minify(struct lp_build_sample_context *bld,
789 LLVMValueRef base_size,
790 LLVMValueRef level)
791 {
792 LLVMValueRef size = LLVMBuildAShr(bld->builder, base_size, level, "minify");
793 size = lp_build_max(&bld->int_coord_bld, size, bld->int_coord_bld.one);
794 return size;
795 }
796
797
798 /**
799 * Generate code to compute texture level of detail (lambda).
800 * \param s vector of texcoord s values
801 * \param t vector of texcoord t values
802 * \param r vector of texcoord r values
803 * \param width scalar int texture width
804 * \param height scalar int texture height
805 * \param depth scalar int texture depth
806 */
807 static LLVMValueRef
808 lp_build_lod_selector(struct lp_build_sample_context *bld,
809 LLVMValueRef s,
810 LLVMValueRef t,
811 LLVMValueRef r,
812 LLVMValueRef width,
813 LLVMValueRef height,
814 LLVMValueRef depth)
815
816 {
817 const int dims = texture_dims(bld->static_state->target);
818 struct lp_build_context *float_bld = &bld->float_bld;
819 LLVMValueRef lod_bias = LLVMConstReal(LLVMFloatType(), bld->static_state->lod_bias);
820 LLVMValueRef min_lod = LLVMConstReal(LLVMFloatType(), bld->static_state->min_lod);
821 LLVMValueRef max_lod = LLVMConstReal(LLVMFloatType(), bld->static_state->max_lod);
822
823 LLVMValueRef index0 = LLVMConstInt(LLVMInt32Type(), 0, 0);
824 LLVMValueRef index1 = LLVMConstInt(LLVMInt32Type(), 1, 0);
825 LLVMValueRef index2 = LLVMConstInt(LLVMInt32Type(), 2, 0);
826
827 LLVMValueRef s0, s1, s2;
828 LLVMValueRef t0, t1, t2;
829 LLVMValueRef r0, r1, r2;
830 LLVMValueRef dsdx, dsdy, dtdx, dtdy, drdx, drdy;
831 LLVMValueRef rho, lod;
832
833 /*
834 * dsdx = abs(s[1] - s[0]);
835 * dsdy = abs(s[2] - s[0]);
836 * dtdx = abs(t[1] - t[0]);
837 * dtdy = abs(t[2] - t[0]);
838 * drdx = abs(r[1] - r[0]);
839 * drdy = abs(r[2] - r[0]);
840 * XXX we're assuming a four-element quad in 2x2 layout here.
841 */
842 s0 = LLVMBuildExtractElement(bld->builder, s, index0, "s0");
843 s1 = LLVMBuildExtractElement(bld->builder, s, index1, "s1");
844 s2 = LLVMBuildExtractElement(bld->builder, s, index2, "s2");
845 dsdx = LLVMBuildSub(bld->builder, s1, s0, "");
846 dsdx = lp_build_abs(float_bld, dsdx);
847 dsdy = LLVMBuildSub(bld->builder, s2, s0, "");
848 dsdy = lp_build_abs(float_bld, dsdy);
849 if (dims > 1) {
850 t0 = LLVMBuildExtractElement(bld->builder, t, index0, "t0");
851 t1 = LLVMBuildExtractElement(bld->builder, t, index1, "t1");
852 t2 = LLVMBuildExtractElement(bld->builder, t, index2, "t2");
853 dtdx = LLVMBuildSub(bld->builder, t1, t0, "");
854 dtdx = lp_build_abs(float_bld, dtdx);
855 dtdy = LLVMBuildSub(bld->builder, t2, t0, "");
856 dtdy = lp_build_abs(float_bld, dtdy);
857 if (dims > 2) {
858 r0 = LLVMBuildExtractElement(bld->builder, r, index0, "r0");
859 r1 = LLVMBuildExtractElement(bld->builder, r, index1, "r1");
860 r2 = LLVMBuildExtractElement(bld->builder, r, index2, "r2");
861 drdx = LLVMBuildSub(bld->builder, r1, r0, "");
862 drdx = lp_build_abs(float_bld, drdx);
863 drdy = LLVMBuildSub(bld->builder, r2, r0, "");
864 drdy = lp_build_abs(float_bld, drdy);
865 }
866 }
867
868 /* Compute rho = max of all partial derivatives scaled by texture size.
869 * XXX this could be vectorized somewhat
870 */
871 rho = LLVMBuildMul(bld->builder,
872 lp_build_max(float_bld, dsdx, dsdy),
873 lp_build_int_to_float(float_bld, width), "");
874 if (dims > 1) {
875 LLVMValueRef max;
876 max = LLVMBuildMul(bld->builder,
877 lp_build_max(float_bld, dtdx, dtdy),
878 lp_build_int_to_float(float_bld, height), "");
879 rho = lp_build_max(float_bld, rho, max);
880 if (dims > 2) {
881 max = LLVMBuildMul(bld->builder,
882 lp_build_max(float_bld, drdx, drdy),
883 lp_build_int_to_float(float_bld, depth), "");
884 rho = lp_build_max(float_bld, rho, max);
885 }
886 }
887
888 /* compute lod = log2(rho) */
889 lod = lp_build_log2(float_bld, rho);
890
891 /* add lod bias */
892 lod = LLVMBuildAdd(bld->builder, lod, lod_bias, "LOD bias");
893
894 /* clamp lod */
895 lod = lp_build_clamp(float_bld, lod, min_lod, max_lod);
896
897 return lod;
898 }
899
900
901 /**
902 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
903 * mipmap level index.
904 * Note: this is all scalar code.
905 * \param lod scalar float texture level of detail
906 * \param level_out returns integer
907 */
908 static void
909 lp_build_nearest_mip_level(struct lp_build_sample_context *bld,
910 unsigned unit,
911 LLVMValueRef lod,
912 LLVMValueRef *level_out)
913 {
914 struct lp_build_context *float_bld = &bld->float_bld;
915 struct lp_build_context *int_bld = &bld->int_bld;
916 LLVMValueRef last_level, level;
917
918 LLVMValueRef zero = LLVMConstInt(LLVMInt32Type(), 0, 0);
919
920 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
921 bld->builder, unit);
922
923 /* convert float lod to integer */
924 level = lp_build_iround(float_bld, lod);
925
926 /* clamp level to legal range of levels */
927 *level_out = lp_build_clamp(int_bld, level, zero, last_level);
928 }
929
930
931 /**
932 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
933 * two (adjacent) mipmap level indexes. Later, we'll sample from those
934 * two mipmap levels and interpolate between them.
935 */
936 static void
937 lp_build_linear_mip_levels(struct lp_build_sample_context *bld,
938 unsigned unit,
939 LLVMValueRef lod,
940 LLVMValueRef *level0_out,
941 LLVMValueRef *level1_out,
942 LLVMValueRef *weight_out)
943 {
944 struct lp_build_context *float_bld = &bld->float_bld;
945 struct lp_build_context *int_bld = &bld->int_bld;
946 LLVMValueRef last_level, level;
947
948 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
949 bld->builder, unit);
950
951 /* convert float lod to integer */
952 level = lp_build_ifloor(float_bld, lod);
953
954 /* compute level 0 and clamp to legal range of levels */
955 *level0_out = lp_build_clamp(int_bld, level,
956 int_bld->zero,
957 last_level);
958 /* compute level 1 and clamp to legal range of levels */
959 *level1_out = lp_build_add(int_bld, *level0_out, int_bld->one);
960 *level1_out = lp_build_min(int_bld, *level1_out, int_bld->zero);
961
962 *weight_out = lp_build_fract(float_bld, lod);
963 }
964
965
966 /**
967 * Generate code to sample a mipmap level with nearest filtering.
968 */
969 static void
970 lp_build_sample_image_nearest(struct lp_build_sample_context *bld,
971 LLVMValueRef width_vec,
972 LLVMValueRef height_vec,
973 LLVMValueRef depth_vec,
974 LLVMValueRef row_stride_vec,
975 LLVMValueRef img_stride_vec,
976 LLVMValueRef data_ptr,
977 LLVMValueRef s,
978 LLVMValueRef t,
979 LLVMValueRef r,
980 LLVMValueRef colors_out[4])
981 {
982 const int dims = texture_dims(bld->static_state->target);
983 LLVMValueRef x, y, z;
984
985 /*
986 * Compute integer texcoords.
987 */
988 x = lp_build_sample_wrap_nearest(bld, s, width_vec,
989 bld->static_state->pot_width,
990 bld->static_state->wrap_s);
991 lp_build_name(x, "tex.x.wrapped");
992
993 if (dims >= 2) {
994 y = lp_build_sample_wrap_nearest(bld, t, height_vec,
995 bld->static_state->pot_height,
996 bld->static_state->wrap_t);
997 lp_build_name(y, "tex.y.wrapped");
998
999 if (dims == 3) {
1000 z = lp_build_sample_wrap_nearest(bld, r, depth_vec,
1001 bld->static_state->pot_height,
1002 bld->static_state->wrap_r);
1003 lp_build_name(z, "tex.z.wrapped");
1004 }
1005 else {
1006 z = NULL;
1007 }
1008 }
1009 else {
1010 y = z = NULL;
1011 }
1012
1013 /*
1014 * Get texture colors.
1015 */
1016 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1017 x, y, z,
1018 row_stride_vec, img_stride_vec,
1019 data_ptr, colors_out);
1020 }
1021
1022
1023 /**
1024 * Generate code to sample a mipmap level with linear filtering.
1025 * 1D, 2D and 3D images are suppored.
1026 */
1027 static void
1028 lp_build_sample_image_linear(struct lp_build_sample_context *bld,
1029 LLVMValueRef width_vec,
1030 LLVMValueRef height_vec,
1031 LLVMValueRef depth_vec,
1032 LLVMValueRef row_stride_vec,
1033 LLVMValueRef img_stride_vec,
1034 LLVMValueRef data_ptr,
1035 LLVMValueRef s,
1036 LLVMValueRef t,
1037 LLVMValueRef r,
1038 LLVMValueRef colors_out[4])
1039 {
1040 const int dims = texture_dims(bld->static_state->target);
1041 LLVMValueRef x0, y0, z0, x1, y1, z1;
1042 LLVMValueRef s_fpart, t_fpart, r_fpart;
1043 LLVMValueRef neighbors[2][2][4];
1044 int chan;
1045
1046 /*
1047 * Compute integer texcoords.
1048 */
1049 lp_build_sample_wrap_linear(bld, s, width_vec,
1050 bld->static_state->pot_width,
1051 bld->static_state->wrap_s,
1052 &x0, &x1, &s_fpart);
1053 lp_build_name(x0, "tex.x0.wrapped");
1054 lp_build_name(x1, "tex.x1.wrapped");
1055
1056 if (dims >= 2) {
1057 lp_build_sample_wrap_linear(bld, t, height_vec,
1058 bld->static_state->pot_height,
1059 bld->static_state->wrap_t,
1060 &y0, &y1, &t_fpart);
1061 lp_build_name(y0, "tex.y0.wrapped");
1062 lp_build_name(y1, "tex.y1.wrapped");
1063
1064 if (dims == 3) {
1065 lp_build_sample_wrap_linear(bld, r, depth_vec,
1066 bld->static_state->pot_depth,
1067 bld->static_state->wrap_r,
1068 &z0, &z1, &r_fpart);
1069 lp_build_name(z0, "tex.z0.wrapped");
1070 lp_build_name(z1, "tex.z1.wrapped");
1071 }
1072 else {
1073 z0 = z1 = r_fpart = NULL;
1074 }
1075 }
1076 else {
1077 y0 = y1 = t_fpart = NULL;
1078 z0 = z1 = r_fpart = NULL;
1079 }
1080
1081 /*
1082 * Get texture colors.
1083 */
1084 /* get x0/x1 texels */
1085 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1086 x0, y0, z0,
1087 row_stride_vec, img_stride_vec,
1088 data_ptr, neighbors[0][0]);
1089 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1090 x1, y0, z0,
1091 row_stride_vec, img_stride_vec,
1092 data_ptr, neighbors[0][1]);
1093
1094 if (dims == 1) {
1095 /* Interpolate two samples from 1D image to produce one color */
1096 for (chan = 0; chan < 4; chan++) {
1097 colors_out[chan] = lp_build_lerp(&bld->texel_bld, s_fpart,
1098 neighbors[0][0][chan],
1099 neighbors[0][1][chan]);
1100 }
1101 }
1102 else {
1103 /* 2D/3D texture */
1104 LLVMValueRef colors0[4];
1105
1106 /* get x0/x1 texels at y1 */
1107 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1108 x0, y1, z0,
1109 row_stride_vec, img_stride_vec,
1110 data_ptr, neighbors[1][0]);
1111 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1112 x1, y1, z0,
1113 row_stride_vec, img_stride_vec,
1114 data_ptr, neighbors[1][1]);
1115
1116 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
1117 for (chan = 0; chan < 4; chan++) {
1118 colors0[chan] = lp_build_lerp_2d(&bld->texel_bld,
1119 s_fpart, t_fpart,
1120 neighbors[0][0][chan],
1121 neighbors[0][1][chan],
1122 neighbors[1][0][chan],
1123 neighbors[1][1][chan]);
1124 }
1125
1126 if (dims == 3) {
1127 LLVMValueRef neighbors1[2][2][4];
1128 LLVMValueRef colors1[4];
1129
1130 /* get x0/x1/y0/y1 texels at z1 */
1131 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1132 x0, y0, z1,
1133 row_stride_vec, img_stride_vec,
1134 data_ptr, neighbors1[0][0]);
1135 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1136 x1, y0, z1,
1137 row_stride_vec, img_stride_vec,
1138 data_ptr, neighbors1[0][1]);
1139 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1140 x0, y1, z1,
1141 row_stride_vec, img_stride_vec,
1142 data_ptr, neighbors1[1][0]);
1143 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1144 x1, y1, z1,
1145 row_stride_vec, img_stride_vec,
1146 data_ptr, neighbors1[1][1]);
1147
1148 /* Bilinear interpolate the four samples from the second Z slice */
1149 for (chan = 0; chan < 4; chan++) {
1150 colors1[chan] = lp_build_lerp_2d(&bld->texel_bld,
1151 s_fpart, t_fpart,
1152 neighbors1[0][0][chan],
1153 neighbors1[0][1][chan],
1154 neighbors1[1][0][chan],
1155 neighbors1[1][1][chan]);
1156 }
1157
1158 /* Linearly interpolate the two samples from the two 3D slices */
1159 for (chan = 0; chan < 4; chan++) {
1160 colors_out[chan] = lp_build_lerp(&bld->texel_bld,
1161 r_fpart,
1162 colors0[chan], colors1[chan]);
1163 }
1164 }
1165 else {
1166 /* 2D tex */
1167 for (chan = 0; chan < 4; chan++) {
1168 colors_out[chan] = colors0[chan];
1169 }
1170 }
1171 }
1172 }
1173
1174
1175
1176 /**
1177 * General texture sampling codegen.
1178 * This function handles texture sampling for all texture targets (1D,
1179 * 2D, 3D, cube) and all filtering modes.
1180 */
1181 static void
1182 lp_build_sample_general(struct lp_build_sample_context *bld,
1183 unsigned unit,
1184 LLVMValueRef s,
1185 LLVMValueRef t,
1186 LLVMValueRef r,
1187 LLVMValueRef width,
1188 LLVMValueRef height,
1189 LLVMValueRef depth,
1190 LLVMValueRef width_vec,
1191 LLVMValueRef height_vec,
1192 LLVMValueRef depth_vec,
1193 LLVMValueRef row_stride_vec,
1194 LLVMValueRef img_stride_vec,
1195 LLVMValueRef data_array,
1196 LLVMValueRef *colors_out)
1197 {
1198 const unsigned mip_filter = bld->static_state->min_mip_filter;
1199 const unsigned min_filter = bld->static_state->min_img_filter;
1200 const unsigned mag_filter = bld->static_state->mag_img_filter;
1201 const int dims = texture_dims(bld->static_state->target);
1202 LLVMValueRef lod, lod_fpart;
1203 LLVMValueRef ilevel0, ilevel1, ilevel0_vec, ilevel1_vec;
1204 LLVMValueRef width0_vec = NULL, height0_vec = NULL, depth0_vec = NULL;
1205 LLVMValueRef width1_vec = NULL, height1_vec = NULL, depth1_vec = NULL;
1206 LLVMValueRef row_stride0_vec = NULL, row_stride1_vec = NULL;
1207 LLVMValueRef img_stride0_vec = NULL, img_stride1_vec = NULL;
1208 LLVMValueRef data_ptr0, data_ptr1;
1209 int chan;
1210
1211 /*
1212 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1213 mip_filter, min_filter, mag_filter);
1214 */
1215
1216 /*
1217 * Compute the level of detail (mipmap level index(es)).
1218 */
1219 if (mip_filter == PIPE_TEX_MIPFILTER_NONE) {
1220 /* always use mip level 0 */
1221 ilevel0 = LLVMConstInt(LLVMInt32Type(), 0, 0);
1222 }
1223 else {
1224 /* compute float LOD */
1225 lod = lp_build_lod_selector(bld, s, t, r, width, height, depth);
1226
1227 if (mip_filter == PIPE_TEX_MIPFILTER_NEAREST) {
1228 lp_build_nearest_mip_level(bld, unit, lod, &ilevel0);
1229 }
1230 else {
1231 assert(mip_filter == PIPE_TEX_MIPFILTER_LINEAR);
1232 lp_build_linear_mip_levels(bld, unit, lod, &ilevel0, &ilevel1,
1233 &lod_fpart);
1234 lod_fpart = lp_build_broadcast_scalar(&bld->coord_bld, lod_fpart);
1235 }
1236 }
1237
1238 /*
1239 * Convert scalar integer mipmap levels into vectors.
1240 */
1241 ilevel0_vec = lp_build_broadcast_scalar(&bld->int_coord_bld, ilevel0);
1242 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
1243 ilevel1_vec = lp_build_broadcast_scalar(&bld->int_coord_bld, ilevel1);
1244
1245 /*
1246 * Compute width, height at mipmap level 'ilevel0'
1247 */
1248 width0_vec = lp_build_minify(bld, width_vec, ilevel0_vec);
1249 if (dims >= 2) {
1250 height0_vec = lp_build_minify(bld, height_vec, ilevel0_vec);
1251 row_stride0_vec = lp_build_minify(bld, row_stride_vec, ilevel0_vec);
1252 if (dims == 3) {
1253 depth0_vec = lp_build_minify(bld, depth_vec, ilevel0_vec);
1254 }
1255 }
1256 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1257 /* compute width, height, depth for second mipmap level at ilevel1 */
1258 width1_vec = lp_build_minify(bld, width_vec, ilevel1_vec);
1259 if (dims >= 2) {
1260 height1_vec = lp_build_minify(bld, height_vec, ilevel1_vec);
1261 row_stride1_vec = lp_build_minify(bld, row_stride_vec, ilevel1_vec);
1262 if (dims == 3) {
1263 depth1_vec = lp_build_minify(bld, depth_vec, ilevel1_vec);
1264 }
1265 }
1266 }
1267
1268 /*
1269 * Choose cube face, recompute texcoords.
1270 */
1271 if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
1272
1273 }
1274
1275 /*
1276 * Get pointer(s) to image data for mipmap level(s).
1277 */
1278 data_ptr0 = lp_build_get_mipmap_level(bld, data_array, ilevel0);
1279 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1280 data_ptr1 = lp_build_get_mipmap_level(bld, data_array, ilevel1);
1281 }
1282
1283 /*
1284 * Get/interpolate texture colors.
1285 */
1286 /* XXX temporarily force this path: */
1287 if (1 /*min_filter == mag_filter*/) {
1288 /* same filter for minification or magnification */
1289 LLVMValueRef colors0[4], colors1[4];
1290
1291 if (min_filter == PIPE_TEX_FILTER_NEAREST) {
1292 lp_build_sample_image_nearest(bld,
1293 width0_vec, height0_vec, depth0_vec,
1294 row_stride0_vec, img_stride0_vec,
1295 data_ptr0, s, t, r, colors0);
1296
1297 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1298 /* sample the second mipmap level, and interp */
1299 lp_build_sample_image_nearest(bld,
1300 width1_vec, height1_vec, depth1_vec,
1301 row_stride1_vec, img_stride1_vec,
1302 data_ptr1, s, t, r, colors1);
1303 }
1304 }
1305 else {
1306 assert(min_filter == PIPE_TEX_FILTER_LINEAR);
1307
1308 lp_build_sample_image_linear(bld,
1309 width0_vec, height0_vec, depth0_vec,
1310 row_stride0_vec, img_stride0_vec,
1311 data_ptr0, s, t, r, colors0);
1312
1313
1314 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1315 /* sample the second mipmap level, and interp */
1316 lp_build_sample_image_linear(bld,
1317 width1_vec, height1_vec, depth1_vec,
1318 row_stride1_vec, img_stride1_vec,
1319 data_ptr1, s, t, r, colors1);
1320 }
1321 }
1322
1323 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1324 /* interpolate samples from the two mipmap levels */
1325 for (chan = 0; chan < 4; chan++) {
1326 colors_out[chan] = lp_build_lerp(&bld->texel_bld, lod_fpart,
1327 colors0[chan], colors1[chan]);
1328 }
1329 }
1330 else {
1331 /* use first/only level's colors */
1332 for (chan = 0; chan < 4; chan++) {
1333 colors_out[chan] = colors0[chan];
1334 }
1335 }
1336 }
1337 else {
1338 /* emit conditional to choose min image filter or mag image filter
1339 * depending on the lod being >0 or <= 0, respectively.
1340 */
1341 abort();
1342 }
1343 }
1344
1345
1346
1347 static void
1348 lp_build_rgba8_to_f32_soa(LLVMBuilderRef builder,
1349 struct lp_type dst_type,
1350 LLVMValueRef packed,
1351 LLVMValueRef *rgba)
1352 {
1353 LLVMValueRef mask = lp_build_int_const_scalar(dst_type, 0xff);
1354 unsigned chan;
1355
1356 /* Decode the input vector components */
1357 for (chan = 0; chan < 4; ++chan) {
1358 unsigned start = chan*8;
1359 unsigned stop = start + 8;
1360 LLVMValueRef input;
1361
1362 input = packed;
1363
1364 if(start)
1365 input = LLVMBuildLShr(builder, input, lp_build_int_const_scalar(dst_type, start), "");
1366
1367 if(stop < 32)
1368 input = LLVMBuildAnd(builder, input, mask, "");
1369
1370 input = lp_build_unsigned_norm_to_float(builder, 8, dst_type, input);
1371
1372 rgba[chan] = input;
1373 }
1374 }
1375
1376
1377 static void
1378 lp_build_sample_2d_linear_aos(struct lp_build_sample_context *bld,
1379 LLVMValueRef s,
1380 LLVMValueRef t,
1381 LLVMValueRef width,
1382 LLVMValueRef height,
1383 LLVMValueRef stride,
1384 LLVMValueRef data_array,
1385 LLVMValueRef *texel)
1386 {
1387 LLVMBuilderRef builder = bld->builder;
1388 struct lp_build_context i32, h16, u8n;
1389 LLVMTypeRef i32_vec_type, h16_vec_type, u8n_vec_type;
1390 LLVMValueRef i32_c8, i32_c128, i32_c255;
1391 LLVMValueRef s_ipart, s_fpart, s_fpart_lo, s_fpart_hi;
1392 LLVMValueRef t_ipart, t_fpart, t_fpart_lo, t_fpart_hi;
1393 LLVMValueRef x0, x1;
1394 LLVMValueRef y0, y1;
1395 LLVMValueRef neighbors[2][2];
1396 LLVMValueRef neighbors_lo[2][2];
1397 LLVMValueRef neighbors_hi[2][2];
1398 LLVMValueRef packed, packed_lo, packed_hi;
1399 LLVMValueRef unswizzled[4];
1400
1401 lp_build_context_init(&i32, builder, lp_type_int_vec(32));
1402 lp_build_context_init(&h16, builder, lp_type_ufixed(16));
1403 lp_build_context_init(&u8n, builder, lp_type_unorm(8));
1404
1405 i32_vec_type = lp_build_vec_type(i32.type);
1406 h16_vec_type = lp_build_vec_type(h16.type);
1407 u8n_vec_type = lp_build_vec_type(u8n.type);
1408
1409 if (bld->static_state->normalized_coords) {
1410 LLVMTypeRef coord_vec_type = lp_build_vec_type(bld->coord_type);
1411 LLVMValueRef fp_width = LLVMBuildSIToFP(bld->builder, width, coord_vec_type, "");
1412 LLVMValueRef fp_height = LLVMBuildSIToFP(bld->builder, height, coord_vec_type, "");
1413 s = lp_build_mul(&bld->coord_bld, s, fp_width);
1414 t = lp_build_mul(&bld->coord_bld, t, fp_height);
1415 }
1416
1417 /* scale coords by 256 (8 fractional bits) */
1418 s = lp_build_mul_imm(&bld->coord_bld, s, 256);
1419 t = lp_build_mul_imm(&bld->coord_bld, t, 256);
1420
1421 /* convert float to int */
1422 s = LLVMBuildFPToSI(builder, s, i32_vec_type, "");
1423 t = LLVMBuildFPToSI(builder, t, i32_vec_type, "");
1424
1425 /* subtract 0.5 (add -128) */
1426 i32_c128 = lp_build_int_const_scalar(i32.type, -128);
1427 s = LLVMBuildAdd(builder, s, i32_c128, "");
1428 t = LLVMBuildAdd(builder, t, i32_c128, "");
1429
1430 /* compute floor (shift right 8) */
1431 i32_c8 = lp_build_int_const_scalar(i32.type, 8);
1432 s_ipart = LLVMBuildAShr(builder, s, i32_c8, "");
1433 t_ipart = LLVMBuildAShr(builder, t, i32_c8, "");
1434
1435 /* compute fractional part (AND with 0xff) */
1436 i32_c255 = lp_build_int_const_scalar(i32.type, 255);
1437 s_fpart = LLVMBuildAnd(builder, s, i32_c255, "");
1438 t_fpart = LLVMBuildAnd(builder, t, i32_c255, "");
1439
1440 x0 = s_ipart;
1441 y0 = t_ipart;
1442
1443 x1 = lp_build_add(&bld->int_coord_bld, x0, bld->int_coord_bld.one);
1444 y1 = lp_build_add(&bld->int_coord_bld, y0, bld->int_coord_bld.one);
1445
1446 x0 = lp_build_sample_wrap_int(bld, x0, width, bld->static_state->pot_width,
1447 bld->static_state->wrap_s);
1448 y0 = lp_build_sample_wrap_int(bld, y0, height, bld->static_state->pot_height,
1449 bld->static_state->wrap_t);
1450
1451 x1 = lp_build_sample_wrap_int(bld, x1, width, bld->static_state->pot_width,
1452 bld->static_state->wrap_s);
1453 y1 = lp_build_sample_wrap_int(bld, y1, height, bld->static_state->pot_height,
1454 bld->static_state->wrap_t);
1455
1456 /*
1457 * Transform 4 x i32 in
1458 *
1459 * s_fpart = {s0, s1, s2, s3}
1460 *
1461 * into 8 x i16
1462 *
1463 * s_fpart = {00, s0, 00, s1, 00, s2, 00, s3}
1464 *
1465 * into two 8 x i16
1466 *
1467 * s_fpart_lo = {s0, s0, s0, s0, s1, s1, s1, s1}
1468 * s_fpart_hi = {s2, s2, s2, s2, s3, s3, s3, s3}
1469 *
1470 * and likewise for t_fpart. There is no risk of loosing precision here
1471 * since the fractional parts only use the lower 8bits.
1472 */
1473
1474 s_fpart = LLVMBuildBitCast(builder, s_fpart, h16_vec_type, "");
1475 t_fpart = LLVMBuildBitCast(builder, t_fpart, h16_vec_type, "");
1476
1477 {
1478 LLVMTypeRef elem_type = LLVMInt32Type();
1479 LLVMValueRef shuffles_lo[LP_MAX_VECTOR_LENGTH];
1480 LLVMValueRef shuffles_hi[LP_MAX_VECTOR_LENGTH];
1481 LLVMValueRef shuffle_lo;
1482 LLVMValueRef shuffle_hi;
1483 unsigned i, j;
1484
1485 for(j = 0; j < h16.type.length; j += 4) {
1486 unsigned subindex = util_cpu_caps.little_endian ? 0 : 1;
1487 LLVMValueRef index;
1488
1489 index = LLVMConstInt(elem_type, j/2 + subindex, 0);
1490 for(i = 0; i < 4; ++i)
1491 shuffles_lo[j + i] = index;
1492
1493 index = LLVMConstInt(elem_type, h16.type.length/2 + j/2 + subindex, 0);
1494 for(i = 0; i < 4; ++i)
1495 shuffles_hi[j + i] = index;
1496 }
1497
1498 shuffle_lo = LLVMConstVector(shuffles_lo, h16.type.length);
1499 shuffle_hi = LLVMConstVector(shuffles_hi, h16.type.length);
1500
1501 s_fpart_lo = LLVMBuildShuffleVector(builder, s_fpart, h16.undef, shuffle_lo, "");
1502 t_fpart_lo = LLVMBuildShuffleVector(builder, t_fpart, h16.undef, shuffle_lo, "");
1503 s_fpart_hi = LLVMBuildShuffleVector(builder, s_fpart, h16.undef, shuffle_hi, "");
1504 t_fpart_hi = LLVMBuildShuffleVector(builder, t_fpart, h16.undef, shuffle_hi, "");
1505 }
1506
1507 /*
1508 * Fetch the pixels as 4 x 32bit (rgba order might differ):
1509 *
1510 * rgba0 rgba1 rgba2 rgba3
1511 *
1512 * bit cast them into 16 x u8
1513 *
1514 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
1515 *
1516 * unpack them into two 8 x i16:
1517 *
1518 * r0 g0 b0 a0 r1 g1 b1 a1
1519 * r2 g2 b2 a2 r3 g3 b3 a3
1520 *
1521 * The higher 8 bits of the resulting elements will be zero.
1522 */
1523
1524 neighbors[0][0] = lp_build_sample_packed(bld, x0, y0, stride, data_array);
1525 neighbors[0][1] = lp_build_sample_packed(bld, x1, y0, stride, data_array);
1526 neighbors[1][0] = lp_build_sample_packed(bld, x0, y1, stride, data_array);
1527 neighbors[1][1] = lp_build_sample_packed(bld, x1, y1, stride, data_array);
1528
1529 neighbors[0][0] = LLVMBuildBitCast(builder, neighbors[0][0], u8n_vec_type, "");
1530 neighbors[0][1] = LLVMBuildBitCast(builder, neighbors[0][1], u8n_vec_type, "");
1531 neighbors[1][0] = LLVMBuildBitCast(builder, neighbors[1][0], u8n_vec_type, "");
1532 neighbors[1][1] = LLVMBuildBitCast(builder, neighbors[1][1], u8n_vec_type, "");
1533
1534 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[0][0], &neighbors_lo[0][0], &neighbors_hi[0][0]);
1535 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[0][1], &neighbors_lo[0][1], &neighbors_hi[0][1]);
1536 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[1][0], &neighbors_lo[1][0], &neighbors_hi[1][0]);
1537 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[1][1], &neighbors_lo[1][1], &neighbors_hi[1][1]);
1538
1539 /*
1540 * Linear interpolate with 8.8 fixed point.
1541 */
1542
1543 packed_lo = lp_build_lerp_2d(&h16,
1544 s_fpart_lo, t_fpart_lo,
1545 neighbors_lo[0][0],
1546 neighbors_lo[0][1],
1547 neighbors_lo[1][0],
1548 neighbors_lo[1][1]);
1549
1550 packed_hi = lp_build_lerp_2d(&h16,
1551 s_fpart_hi, t_fpart_hi,
1552 neighbors_hi[0][0],
1553 neighbors_hi[0][1],
1554 neighbors_hi[1][0],
1555 neighbors_hi[1][1]);
1556
1557 packed = lp_build_pack2(builder, h16.type, u8n.type, packed_lo, packed_hi);
1558
1559 /*
1560 * Convert to SoA and swizzle.
1561 */
1562
1563 packed = LLVMBuildBitCast(builder, packed, i32_vec_type, "");
1564
1565 lp_build_rgba8_to_f32_soa(bld->builder,
1566 bld->texel_type,
1567 packed, unswizzled);
1568
1569 lp_build_format_swizzle_soa(bld->format_desc,
1570 bld->texel_type, unswizzled,
1571 texel);
1572 }
1573
1574
1575 static void
1576 lp_build_sample_compare(struct lp_build_sample_context *bld,
1577 LLVMValueRef p,
1578 LLVMValueRef *texel)
1579 {
1580 struct lp_build_context *texel_bld = &bld->texel_bld;
1581 LLVMValueRef res;
1582 unsigned chan;
1583
1584 if(bld->static_state->compare_mode == PIPE_TEX_COMPARE_NONE)
1585 return;
1586
1587 /* TODO: Compare before swizzling, to avoid redundant computations */
1588 res = NULL;
1589 for(chan = 0; chan < 4; ++chan) {
1590 LLVMValueRef cmp;
1591 cmp = lp_build_cmp(texel_bld, bld->static_state->compare_func, p, texel[chan]);
1592 cmp = lp_build_select(texel_bld, cmp, texel_bld->one, texel_bld->zero);
1593
1594 if(res)
1595 res = lp_build_add(texel_bld, res, cmp);
1596 else
1597 res = cmp;
1598 }
1599
1600 assert(res);
1601 res = lp_build_mul(texel_bld, res, lp_build_const_scalar(texel_bld->type, 0.25));
1602
1603 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1604 for(chan = 0; chan < 3; ++chan)
1605 texel[chan] = res;
1606 texel[3] = texel_bld->one;
1607 }
1608
1609
1610 /**
1611 * Build texture sampling code.
1612 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1613 * R, G, B, A.
1614 * \param type vector float type to use for coords, etc.
1615 */
1616 void
1617 lp_build_sample_soa(LLVMBuilderRef builder,
1618 const struct lp_sampler_static_state *static_state,
1619 struct lp_sampler_dynamic_state *dynamic_state,
1620 struct lp_type type,
1621 unsigned unit,
1622 unsigned num_coords,
1623 const LLVMValueRef *coords,
1624 LLVMValueRef lodbias,
1625 LLVMValueRef *texel)
1626 {
1627 struct lp_build_sample_context bld;
1628 LLVMValueRef width, width_vec;
1629 LLVMValueRef height, height_vec;
1630 LLVMValueRef depth, depth_vec;
1631 LLVMValueRef stride, stride_vec;
1632 LLVMValueRef data_array;
1633 LLVMValueRef s;
1634 LLVMValueRef t;
1635 LLVMValueRef r;
1636
1637 (void) lp_build_lod_selector; /* temporary to silence warning */
1638 (void) lp_build_nearest_mip_level;
1639 (void) lp_build_linear_mip_levels;
1640 (void) lp_build_minify;
1641
1642 /* Setup our build context */
1643 memset(&bld, 0, sizeof bld);
1644 bld.builder = builder;
1645 bld.static_state = static_state;
1646 bld.dynamic_state = dynamic_state;
1647 bld.format_desc = util_format_description(static_state->format);
1648
1649 bld.float_type = lp_type_float(32);
1650 bld.int_type = lp_type_int(32);
1651 bld.coord_type = type;
1652 bld.uint_coord_type = lp_uint_type(type);
1653 bld.int_coord_type = lp_int_type(type);
1654 bld.texel_type = type;
1655
1656 lp_build_context_init(&bld.float_bld, builder, bld.float_type);
1657 lp_build_context_init(&bld.int_bld, builder, bld.int_type);
1658 lp_build_context_init(&bld.coord_bld, builder, bld.coord_type);
1659 lp_build_context_init(&bld.uint_coord_bld, builder, bld.uint_coord_type);
1660 lp_build_context_init(&bld.int_coord_bld, builder, bld.int_coord_type);
1661 lp_build_context_init(&bld.texel_bld, builder, bld.texel_type);
1662
1663 /* Get the dynamic state */
1664 width = dynamic_state->width(dynamic_state, builder, unit);
1665 height = dynamic_state->height(dynamic_state, builder, unit);
1666 depth = dynamic_state->depth(dynamic_state, builder, unit);
1667 stride = dynamic_state->stride(dynamic_state, builder, unit);
1668 data_array = dynamic_state->data_ptr(dynamic_state, builder, unit);
1669 /* Note that data_array is an array[level] of pointers to texture images */
1670
1671 s = coords[0];
1672 t = coords[1];
1673 r = coords[2];
1674
1675 width_vec = lp_build_broadcast_scalar(&bld.uint_coord_bld, width);
1676 height_vec = lp_build_broadcast_scalar(&bld.uint_coord_bld, height);
1677 depth_vec = lp_build_broadcast_scalar(&bld.uint_coord_bld, depth);
1678 stride_vec = lp_build_broadcast_scalar(&bld.uint_coord_bld, stride);
1679
1680 if (lp_format_is_rgba8(bld.format_desc) &&
1681 static_state->min_img_filter == PIPE_TEX_FILTER_LINEAR &&
1682 static_state->mag_img_filter == PIPE_TEX_FILTER_LINEAR &&
1683 static_state->min_mip_filter == PIPE_TEX_MIPFILTER_NONE &&
1684 is_simple_wrap_mode(static_state->wrap_s) &&
1685 is_simple_wrap_mode(static_state->wrap_t)) {
1686 /* special case */
1687 lp_build_sample_2d_linear_aos(&bld, s, t, width_vec, height_vec,
1688 stride_vec, data_array, texel);
1689 }
1690 else {
1691 lp_build_sample_general(&bld, unit, s, t, r,
1692 width, height, depth,
1693 width_vec, height_vec, depth_vec,
1694 stride_vec, NULL, data_array,
1695 texel);
1696 }
1697
1698 lp_build_sample_compare(&bld, r, texel);
1699 }