llvmpipe: added lp_build_sample_nop() for debugging
[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_flow.h"
52 #include "lp_bld_format.h"
53 #include "lp_bld_sample.h"
54
55
56 /**
57 * Keep all information for sampling code generation in a single place.
58 */
59 struct lp_build_sample_context
60 {
61 LLVMBuilderRef builder;
62
63 const struct lp_sampler_static_state *static_state;
64
65 struct lp_sampler_dynamic_state *dynamic_state;
66
67 const struct util_format_description *format_desc;
68
69 /** regular scalar float type */
70 struct lp_type float_type;
71 struct lp_build_context float_bld;
72
73 /** regular scalar float type */
74 struct lp_type int_type;
75 struct lp_build_context int_bld;
76
77 /** Incoming coordinates type and build context */
78 struct lp_type coord_type;
79 struct lp_build_context coord_bld;
80
81 /** Unsigned integer coordinates */
82 struct lp_type uint_coord_type;
83 struct lp_build_context uint_coord_bld;
84
85 /** Signed integer coordinates */
86 struct lp_type int_coord_type;
87 struct lp_build_context int_coord_bld;
88
89 /** Output texels type and build context */
90 struct lp_type texel_type;
91 struct lp_build_context texel_bld;
92 };
93
94
95 /**
96 * Does the given texture wrap mode allow sampling the texture border color?
97 * XXX maybe move this into gallium util code.
98 */
99 static boolean
100 wrap_mode_uses_border_color(unsigned mode)
101 {
102 switch (mode) {
103 case PIPE_TEX_WRAP_REPEAT:
104 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
105 case PIPE_TEX_WRAP_MIRROR_REPEAT:
106 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
107 return FALSE;
108 case PIPE_TEX_WRAP_CLAMP:
109 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
110 case PIPE_TEX_WRAP_MIRROR_CLAMP:
111 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
112 return TRUE;
113 default:
114 assert(0 && "unexpected wrap mode");
115 return FALSE;
116 }
117 }
118
119
120 static LLVMValueRef
121 lp_build_get_mipmap_level(struct lp_build_sample_context *bld,
122 LLVMValueRef data_array, LLVMValueRef level)
123 {
124 LLVMValueRef indexes[2], data_ptr;
125 indexes[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
126 indexes[1] = level;
127 data_ptr = LLVMBuildGEP(bld->builder, data_array, indexes, 2, "");
128 data_ptr = LLVMBuildLoad(bld->builder, data_ptr, "");
129 return data_ptr;
130 }
131
132
133 static LLVMValueRef
134 lp_build_get_const_mipmap_level(struct lp_build_sample_context *bld,
135 LLVMValueRef data_array, int level)
136 {
137 LLVMValueRef lvl = LLVMConstInt(LLVMInt32Type(), level, 0);
138 return lp_build_get_mipmap_level(bld, data_array, lvl);
139 }
140
141
142 /**
143 * Dereference stride_array[mipmap_level] array to get a stride.
144 * Return stride as a vector.
145 */
146 static LLVMValueRef
147 lp_build_get_level_stride_vec(struct lp_build_sample_context *bld,
148 LLVMValueRef stride_array, LLVMValueRef level)
149 {
150 LLVMValueRef indexes[2], stride;
151 indexes[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
152 indexes[1] = level;
153 stride = LLVMBuildGEP(bld->builder, stride_array, indexes, 2, "");
154 stride = LLVMBuildLoad(bld->builder, stride, "");
155 stride = lp_build_broadcast_scalar(&bld->int_coord_bld, stride);
156 return stride;
157 }
158
159
160 /** Dereference stride_array[0] array to get a stride (as vector). */
161 static LLVMValueRef
162 lp_build_get_const_level_stride_vec(struct lp_build_sample_context *bld,
163 LLVMValueRef stride_array, int level)
164 {
165 LLVMValueRef lvl = LLVMConstInt(LLVMInt32Type(), level, 0);
166 return lp_build_get_level_stride_vec(bld, stride_array, lvl);
167 }
168
169
170 static int
171 texture_dims(enum pipe_texture_target tex)
172 {
173 switch (tex) {
174 case PIPE_TEXTURE_1D:
175 return 1;
176 case PIPE_TEXTURE_2D:
177 case PIPE_TEXTURE_CUBE:
178 return 2;
179 case PIPE_TEXTURE_3D:
180 return 3;
181 default:
182 assert(0 && "bad texture target in texture_dims()");
183 return 2;
184 }
185 }
186
187
188 static LLVMValueRef
189 lp_build_swizzle_chan_soa(struct lp_type type,
190 const LLVMValueRef *unswizzled,
191 enum util_format_swizzle swizzle)
192 {
193 switch (swizzle) {
194 case PIPE_SWIZZLE_RED:
195 case PIPE_SWIZZLE_GREEN:
196 case PIPE_SWIZZLE_BLUE:
197 case PIPE_SWIZZLE_ALPHA:
198 return unswizzled[swizzle];
199 case PIPE_SWIZZLE_ZERO:
200 return lp_build_zero(type);
201 case PIPE_SWIZZLE_ONE:
202 return lp_build_one(type);
203 default:
204 assert(0);
205 return lp_build_undef(type);
206 }
207 }
208
209
210 static void
211 lp_build_swizzle_soa(struct lp_build_sample_context *bld,
212 LLVMValueRef *texel)
213 {
214 LLVMValueRef unswizzled[4];
215 unsigned char swizzles[4];
216 unsigned chan;
217
218 for (chan = 0; chan < 4; ++chan) {
219 unswizzled[chan] = texel[chan];
220 }
221
222 swizzles[0] = bld->static_state->swizzle_r;
223 swizzles[1] = bld->static_state->swizzle_g;
224 swizzles[2] = bld->static_state->swizzle_b;
225 swizzles[3] = bld->static_state->swizzle_a;
226
227 for (chan = 0; chan < 4; ++chan) {
228 unsigned swizzle = swizzles[chan];
229 texel[chan] = lp_build_swizzle_chan_soa(bld->texel_type,
230 unswizzled, swizzle);
231 }
232 }
233
234
235
236 /**
237 * Generate code to fetch a texel from a texture at int coords (x, y, z).
238 * The computation depends on whether the texture is 1D, 2D or 3D.
239 * The result, texel, will be:
240 * texel[0] = red values
241 * texel[1] = green values
242 * texel[2] = blue values
243 * texel[3] = alpha values
244 */
245 static void
246 lp_build_sample_texel_soa(struct lp_build_sample_context *bld,
247 LLVMValueRef width,
248 LLVMValueRef height,
249 LLVMValueRef depth,
250 LLVMValueRef x,
251 LLVMValueRef y,
252 LLVMValueRef z,
253 LLVMValueRef y_stride,
254 LLVMValueRef z_stride,
255 LLVMValueRef data_ptr,
256 LLVMValueRef *texel)
257 {
258 const int dims = texture_dims(bld->static_state->target);
259 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
260 LLVMValueRef offset;
261 LLVMValueRef i, j;
262 LLVMValueRef use_border = NULL;
263
264 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
265 if (wrap_mode_uses_border_color(bld->static_state->wrap_s)) {
266 LLVMValueRef b1, b2;
267 b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, x, int_coord_bld->zero);
268 b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, x, width);
269 use_border = LLVMBuildOr(bld->builder, b1, b2, "b1_or_b2");
270 }
271
272 if (dims >= 2 && wrap_mode_uses_border_color(bld->static_state->wrap_t)) {
273 LLVMValueRef b1, b2;
274 b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, y, int_coord_bld->zero);
275 b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, y, height);
276 if (use_border) {
277 use_border = LLVMBuildOr(bld->builder, use_border, b1, "ub_or_b1");
278 use_border = LLVMBuildOr(bld->builder, use_border, b2, "ub_or_b2");
279 }
280 else {
281 use_border = LLVMBuildOr(bld->builder, b1, b2, "b1_or_b2");
282 }
283 }
284
285 if (dims == 3 && wrap_mode_uses_border_color(bld->static_state->wrap_r)) {
286 LLVMValueRef b1, b2;
287 b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, z, int_coord_bld->zero);
288 b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, z, depth);
289 if (use_border) {
290 use_border = LLVMBuildOr(bld->builder, use_border, b1, "ub_or_b1");
291 use_border = LLVMBuildOr(bld->builder, use_border, b2, "ub_or_b2");
292 }
293 else {
294 use_border = LLVMBuildOr(bld->builder, b1, b2, "b1_or_b2");
295 }
296 }
297
298 /*
299 * Describe the coordinates in terms of pixel blocks.
300 *
301 * TODO: pixel blocks are power of two. LLVM should convert rem/div to
302 * bit arithmetic. Verify this.
303 */
304
305 if (bld->format_desc->block.width == 1) {
306 i = bld->uint_coord_bld.zero;
307 }
308 else {
309 LLVMValueRef block_width = lp_build_const_int_vec(bld->uint_coord_bld.type, bld->format_desc->block.width);
310 i = LLVMBuildURem(bld->builder, x, block_width, "");
311 x = LLVMBuildUDiv(bld->builder, x, block_width, "");
312 }
313
314 if (bld->format_desc->block.height == 1) {
315 j = bld->uint_coord_bld.zero;
316 }
317 else {
318 LLVMValueRef block_height = lp_build_const_int_vec(bld->uint_coord_bld.type, bld->format_desc->block.height);
319 j = LLVMBuildURem(bld->builder, y, block_height, "");
320 y = LLVMBuildUDiv(bld->builder, y, block_height, "");
321 }
322
323 /* convert x,y,z coords to linear offset from start of texture, in bytes */
324 offset = lp_build_sample_offset(&bld->uint_coord_bld,
325 bld->format_desc,
326 x, y, z, y_stride, z_stride);
327
328 lp_build_fetch_rgba_soa(bld->builder,
329 bld->format_desc,
330 bld->texel_type,
331 data_ptr, offset,
332 i, j,
333 texel);
334
335 lp_build_swizzle_soa(bld, texel);
336
337 /*
338 * Note: if we find an app which frequently samples the texture border
339 * we might want to implement a true conditional here to avoid sampling
340 * the texture whenever possible (since that's quite a bit of code).
341 * Ex:
342 * if (use_border) {
343 * texel = border_color;
344 * }
345 * else {
346 * texel = sample_texture(coord);
347 * }
348 * As it is now, we always sample the texture, then selectively replace
349 * the texel color results with the border color.
350 */
351
352 if (use_border) {
353 /* select texel color or border color depending on use_border */
354 int chan;
355 for (chan = 0; chan < 4; chan++) {
356 LLVMValueRef border_chan =
357 lp_build_const_vec(bld->texel_type,
358 bld->static_state->border_color[chan]);
359 texel[chan] = lp_build_select(&bld->texel_bld, use_border,
360 border_chan, texel[chan]);
361 }
362 }
363 }
364
365
366 static LLVMValueRef
367 lp_build_sample_packed(struct lp_build_sample_context *bld,
368 LLVMValueRef x,
369 LLVMValueRef y,
370 LLVMValueRef y_stride,
371 LLVMValueRef data_array)
372 {
373 LLVMValueRef offset;
374 LLVMValueRef data_ptr;
375
376 offset = lp_build_sample_offset(&bld->uint_coord_bld,
377 bld->format_desc,
378 x, y, NULL, y_stride, NULL);
379
380 assert(bld->format_desc->block.width == 1);
381 assert(bld->format_desc->block.height == 1);
382 assert(bld->format_desc->block.bits <= bld->texel_type.width);
383
384 /* get pointer to mipmap level 0 data */
385 data_ptr = lp_build_get_const_mipmap_level(bld, data_array, 0);
386
387 return lp_build_gather(bld->builder,
388 bld->texel_type.length,
389 bld->format_desc->block.bits,
390 bld->texel_type.width,
391 data_ptr, offset);
392 }
393
394
395 /**
396 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
397 */
398 static LLVMValueRef
399 lp_build_coord_mirror(struct lp_build_sample_context *bld,
400 LLVMValueRef coord)
401 {
402 struct lp_build_context *coord_bld = &bld->coord_bld;
403 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
404 LLVMValueRef fract, flr, isOdd;
405
406 /* fract = coord - floor(coord) */
407 fract = lp_build_sub(coord_bld, coord, lp_build_floor(coord_bld, coord));
408
409 /* flr = ifloor(coord); */
410 flr = lp_build_ifloor(coord_bld, coord);
411
412 /* isOdd = flr & 1 */
413 isOdd = LLVMBuildAnd(bld->builder, flr, int_coord_bld->one, "");
414
415 /* make coord positive or negative depending on isOdd */
416 coord = lp_build_set_sign(coord_bld, fract, isOdd);
417
418 /* convert isOdd to float */
419 isOdd = lp_build_int_to_float(coord_bld, isOdd);
420
421 /* add isOdd to coord */
422 coord = lp_build_add(coord_bld, coord, isOdd);
423
424 return coord;
425 }
426
427
428 /**
429 * We only support a few wrap modes in lp_build_sample_wrap_int() at this time.
430 * Return whether the given mode is supported by that function.
431 */
432 static boolean
433 is_simple_wrap_mode(unsigned mode)
434 {
435 switch (mode) {
436 case PIPE_TEX_WRAP_REPEAT:
437 case PIPE_TEX_WRAP_CLAMP:
438 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
439 return TRUE;
440 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
441 default:
442 return FALSE;
443 }
444 }
445
446
447 /**
448 * Build LLVM code for texture wrap mode, for scaled integer texcoords.
449 * \param coord the incoming texcoord (s,t,r or q) scaled to the texture size
450 * \param length the texture size along one dimension
451 * \param is_pot if TRUE, length is a power of two
452 * \param wrap_mode one of PIPE_TEX_WRAP_x
453 */
454 static LLVMValueRef
455 lp_build_sample_wrap_int(struct lp_build_sample_context *bld,
456 LLVMValueRef coord,
457 LLVMValueRef length,
458 boolean is_pot,
459 unsigned wrap_mode)
460 {
461 struct lp_build_context *uint_coord_bld = &bld->uint_coord_bld;
462 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
463 LLVMValueRef length_minus_one;
464
465 length_minus_one = lp_build_sub(uint_coord_bld, length, uint_coord_bld->one);
466
467 switch(wrap_mode) {
468 case PIPE_TEX_WRAP_REPEAT:
469 if(is_pot)
470 coord = LLVMBuildAnd(bld->builder, coord, length_minus_one, "");
471 else
472 /* Signed remainder won't give the right results for negative
473 * dividends but unsigned remainder does.*/
474 coord = LLVMBuildURem(bld->builder, coord, length, "");
475 break;
476
477 case PIPE_TEX_WRAP_CLAMP:
478 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
479 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
480 coord = lp_build_max(int_coord_bld, coord, int_coord_bld->zero);
481 coord = lp_build_min(int_coord_bld, coord, length_minus_one);
482 break;
483
484 case PIPE_TEX_WRAP_MIRROR_REPEAT:
485 case PIPE_TEX_WRAP_MIRROR_CLAMP:
486 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
487 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
488 /* FIXME */
489 _debug_printf("llvmpipe: failed to translate texture wrap mode %s\n",
490 util_dump_tex_wrap(wrap_mode, TRUE));
491 coord = lp_build_max(uint_coord_bld, coord, uint_coord_bld->zero);
492 coord = lp_build_min(uint_coord_bld, coord, length_minus_one);
493 break;
494
495 default:
496 assert(0);
497 }
498
499 return coord;
500 }
501
502
503 /**
504 * Build LLVM code for texture wrap mode for linear filtering.
505 * \param x0_out returns first integer texcoord
506 * \param x1_out returns second integer texcoord
507 * \param weight_out returns linear interpolation weight
508 */
509 static void
510 lp_build_sample_wrap_linear(struct lp_build_sample_context *bld,
511 LLVMValueRef coord,
512 LLVMValueRef length,
513 boolean is_pot,
514 unsigned wrap_mode,
515 LLVMValueRef *x0_out,
516 LLVMValueRef *x1_out,
517 LLVMValueRef *weight_out)
518 {
519 struct lp_build_context *coord_bld = &bld->coord_bld;
520 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
521 struct lp_build_context *uint_coord_bld = &bld->uint_coord_bld;
522 LLVMValueRef two = lp_build_const_vec(coord_bld->type, 2.0);
523 LLVMValueRef half = lp_build_const_vec(coord_bld->type, 0.5);
524 LLVMValueRef length_f = lp_build_int_to_float(coord_bld, length);
525 LLVMValueRef length_minus_one = lp_build_sub(uint_coord_bld, length, uint_coord_bld->one);
526 LLVMValueRef length_f_minus_one = lp_build_sub(coord_bld, length_f, coord_bld->one);
527 LLVMValueRef coord0, coord1, weight;
528
529 switch(wrap_mode) {
530 case PIPE_TEX_WRAP_REPEAT:
531 /* mul by size and subtract 0.5 */
532 coord = lp_build_mul(coord_bld, coord, length_f);
533 coord = lp_build_sub(coord_bld, coord, half);
534 /* convert to int */
535 coord0 = lp_build_ifloor(coord_bld, coord);
536 coord1 = lp_build_add(uint_coord_bld, coord0, uint_coord_bld->one);
537 /* compute lerp weight */
538 weight = lp_build_fract(coord_bld, coord);
539 /* repeat wrap */
540 if (is_pot) {
541 coord0 = LLVMBuildAnd(bld->builder, coord0, length_minus_one, "");
542 coord1 = LLVMBuildAnd(bld->builder, coord1, length_minus_one, "");
543 }
544 else {
545 /* Signed remainder won't give the right results for negative
546 * dividends but unsigned remainder does.*/
547 coord0 = LLVMBuildURem(bld->builder, coord0, length, "");
548 coord1 = LLVMBuildURem(bld->builder, coord1, length, "");
549 }
550 break;
551
552 case PIPE_TEX_WRAP_CLAMP:
553 if (bld->static_state->normalized_coords) {
554 coord = lp_build_mul(coord_bld, coord, length_f);
555 }
556 weight = lp_build_fract(coord_bld, coord);
557 coord0 = lp_build_clamp(coord_bld, coord, coord_bld->zero,
558 length_f_minus_one);
559 coord1 = lp_build_add(coord_bld, coord, coord_bld->one);
560 coord1 = lp_build_clamp(coord_bld, coord1, coord_bld->zero,
561 length_f_minus_one);
562 coord0 = lp_build_ifloor(coord_bld, coord0);
563 coord1 = lp_build_ifloor(coord_bld, coord1);
564 break;
565
566 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
567 if (bld->static_state->normalized_coords) {
568 /* clamp to [0,1] */
569 coord = lp_build_clamp(coord_bld, coord, coord_bld->zero, coord_bld->one);
570 /* mul by tex size and subtract 0.5 */
571 coord = lp_build_mul(coord_bld, coord, length_f);
572 coord = lp_build_sub(coord_bld, coord, half);
573 }
574 else {
575 LLVMValueRef min, max;
576 /* clamp to [0.5, length - 0.5] */
577 min = lp_build_const_vec(coord_bld->type, 0.5F);
578 max = lp_build_sub(coord_bld, length_f, min);
579 coord = lp_build_clamp(coord_bld, coord, min, max);
580 }
581 /* compute lerp weight */
582 weight = lp_build_fract(coord_bld, coord);
583 /* coord0 = floor(coord); */
584 coord0 = lp_build_ifloor(coord_bld, coord);
585 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
586 /* coord0 = max(coord0, 0) */
587 coord0 = lp_build_max(int_coord_bld, coord0, int_coord_bld->zero);
588 /* coord1 = min(coord1, length-1) */
589 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
590 break;
591
592 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
593 {
594 LLVMValueRef min, max;
595 if (bld->static_state->normalized_coords) {
596 /* min = -1.0 / (2 * length) = -0.5 / length */
597 min = lp_build_mul(coord_bld,
598 lp_build_const_vec(coord_bld->type, -0.5F),
599 lp_build_rcp(coord_bld, length_f));
600 /* max = 1.0 - min */
601 max = lp_build_sub(coord_bld, coord_bld->one, min);
602 /* coord = clamp(coord, min, max) */
603 coord = lp_build_clamp(coord_bld, coord, min, max);
604 /* scale coord to length (and sub 0.5?) */
605 coord = lp_build_mul(coord_bld, coord, length_f);
606 coord = lp_build_sub(coord_bld, coord, half);
607 }
608 else {
609 /* clamp to [-0.5, length + 0.5] */
610 min = lp_build_const_vec(coord_bld->type, -0.5F);
611 max = lp_build_sub(coord_bld, length_f, min);
612 coord = lp_build_clamp(coord_bld, coord, min, max);
613 coord = lp_build_sub(coord_bld, coord, half);
614 }
615 /* compute lerp weight */
616 weight = lp_build_fract(coord_bld, coord);
617 /* convert to int */
618 coord0 = lp_build_ifloor(coord_bld, coord);
619 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
620 }
621 break;
622
623 case PIPE_TEX_WRAP_MIRROR_REPEAT:
624 /* compute mirror function */
625 coord = lp_build_coord_mirror(bld, coord);
626
627 /* scale coord to length */
628 coord = lp_build_mul(coord_bld, coord, length_f);
629 coord = lp_build_sub(coord_bld, coord, half);
630
631 /* compute lerp weight */
632 weight = lp_build_fract(coord_bld, coord);
633
634 /* convert to int coords */
635 coord0 = lp_build_ifloor(coord_bld, coord);
636 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
637
638 /* coord0 = max(coord0, 0) */
639 coord0 = lp_build_max(int_coord_bld, coord0, int_coord_bld->zero);
640 /* coord1 = min(coord1, length-1) */
641 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
642 break;
643
644 case PIPE_TEX_WRAP_MIRROR_CLAMP:
645 {
646 LLVMValueRef min, max;
647 /* min = 1.0 / (2 * length) */
648 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
649 /* max = 1.0 - min */
650 max = lp_build_sub(coord_bld, coord_bld->one, min);
651
652 coord = lp_build_abs(coord_bld, coord);
653 coord = lp_build_clamp(coord_bld, coord, min, max);
654 coord = lp_build_mul(coord_bld, coord, length_f);
655 if(0)coord = lp_build_sub(coord_bld, coord, half);
656 weight = lp_build_fract(coord_bld, coord);
657 coord0 = lp_build_ifloor(coord_bld, coord);
658 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
659 }
660 break;
661
662 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
663 {
664 LLVMValueRef min, max;
665 /* min = 1.0 / (2 * length) */
666 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
667 /* max = 1.0 - min */
668 max = lp_build_sub(coord_bld, coord_bld->one, min);
669
670 coord = lp_build_abs(coord_bld, coord);
671 coord = lp_build_clamp(coord_bld, coord, min, max);
672 coord = lp_build_mul(coord_bld, coord, length_f);
673 coord = lp_build_sub(coord_bld, coord, half);
674 weight = lp_build_fract(coord_bld, coord);
675 coord0 = lp_build_ifloor(coord_bld, coord);
676 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
677 }
678 break;
679
680 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
681 {
682 LLVMValueRef min, max;
683 /* min = -1.0 / (2 * length) = -0.5 / length */
684 min = lp_build_mul(coord_bld,
685 lp_build_const_vec(coord_bld->type, -0.5F),
686 lp_build_rcp(coord_bld, length_f));
687 /* max = 1.0 - min */
688 max = lp_build_sub(coord_bld, coord_bld->one, min);
689
690 coord = lp_build_abs(coord_bld, coord);
691 coord = lp_build_clamp(coord_bld, coord, min, max);
692 coord = lp_build_mul(coord_bld, coord, length_f);
693 coord = lp_build_sub(coord_bld, coord, half);
694 weight = lp_build_fract(coord_bld, coord);
695 coord0 = lp_build_ifloor(coord_bld, coord);
696 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
697 }
698 break;
699
700 default:
701 assert(0);
702 coord0 = NULL;
703 coord1 = NULL;
704 weight = NULL;
705 }
706
707 *x0_out = coord0;
708 *x1_out = coord1;
709 *weight_out = weight;
710 }
711
712
713 /**
714 * Build LLVM code for texture wrap mode for nearest filtering.
715 * \param coord the incoming texcoord (nominally in [0,1])
716 * \param length the texture size along one dimension, as int
717 * \param is_pot if TRUE, length is a power of two
718 * \param wrap_mode one of PIPE_TEX_WRAP_x
719 */
720 static LLVMValueRef
721 lp_build_sample_wrap_nearest(struct lp_build_sample_context *bld,
722 LLVMValueRef coord,
723 LLVMValueRef length,
724 boolean is_pot,
725 unsigned wrap_mode)
726 {
727 struct lp_build_context *coord_bld = &bld->coord_bld;
728 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
729 struct lp_build_context *uint_coord_bld = &bld->uint_coord_bld;
730 LLVMValueRef two = lp_build_const_vec(coord_bld->type, 2.0);
731 LLVMValueRef length_f = lp_build_int_to_float(coord_bld, length);
732 LLVMValueRef length_minus_one = lp_build_sub(uint_coord_bld, length, uint_coord_bld->one);
733 LLVMValueRef length_f_minus_one = lp_build_sub(coord_bld, length_f, coord_bld->one);
734 LLVMValueRef icoord;
735
736 switch(wrap_mode) {
737 case PIPE_TEX_WRAP_REPEAT:
738 coord = lp_build_mul(coord_bld, coord, length_f);
739 icoord = lp_build_ifloor(coord_bld, coord);
740 if (is_pot)
741 icoord = LLVMBuildAnd(bld->builder, icoord, length_minus_one, "");
742 else
743 /* Signed remainder won't give the right results for negative
744 * dividends but unsigned remainder does.*/
745 icoord = LLVMBuildURem(bld->builder, icoord, length, "");
746 break;
747
748 case PIPE_TEX_WRAP_CLAMP:
749 /* mul by size */
750 if (bld->static_state->normalized_coords) {
751 coord = lp_build_mul(coord_bld, coord, length_f);
752 }
753 /* floor */
754 icoord = lp_build_ifloor(coord_bld, coord);
755 /* clamp to [0, size-1]. Note: int coord builder type */
756 icoord = lp_build_clamp(int_coord_bld, icoord, int_coord_bld->zero,
757 length_minus_one);
758 break;
759
760 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
761 {
762 LLVMValueRef min, max;
763 if (bld->static_state->normalized_coords) {
764 /* min = 1.0 / (2 * length) */
765 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
766 /* max = length - min */
767 max = lp_build_sub(coord_bld, length_f, min);
768 /* scale coord to length */
769 coord = lp_build_mul(coord_bld, coord, length_f);
770 }
771 else {
772 /* clamp to [0.5, length - 0.5] */
773 min = lp_build_const_vec(coord_bld->type, 0.5F);
774 max = lp_build_sub(coord_bld, length_f, min);
775 }
776 /* coord = clamp(coord, min, max) */
777 coord = lp_build_clamp(coord_bld, coord, min, max);
778 icoord = lp_build_ifloor(coord_bld, coord);
779 }
780 break;
781
782 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
783 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
784 {
785 LLVMValueRef min, max;
786 if (bld->static_state->normalized_coords) {
787 /* min = -1.0 / (2 * length) = -0.5 / length */
788 min = lp_build_mul(coord_bld,
789 lp_build_const_vec(coord_bld->type, -0.5F),
790 lp_build_rcp(coord_bld, length_f));
791 /* max = length - min */
792 max = lp_build_sub(coord_bld, length_f, min);
793 /* scale coord to length */
794 coord = lp_build_mul(coord_bld, coord, length_f);
795 }
796 else {
797 /* clamp to [-0.5, length + 0.5] */
798 min = lp_build_const_vec(coord_bld->type, -0.5F);
799 max = lp_build_sub(coord_bld, length_f, min);
800 }
801 /* coord = clamp(coord, min, max) */
802 coord = lp_build_clamp(coord_bld, coord, min, max);
803 icoord = lp_build_ifloor(coord_bld, coord);
804 }
805 break;
806
807 case PIPE_TEX_WRAP_MIRROR_REPEAT:
808 {
809 LLVMValueRef min, max;
810 /* min = 1.0 / (2 * length) */
811 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
812 /* max = length - min */
813 max = lp_build_sub(coord_bld, length_f, min);
814
815 /* compute mirror function */
816 coord = lp_build_coord_mirror(bld, coord);
817
818 /* scale coord to length */
819 coord = lp_build_mul(coord_bld, coord, length_f);
820
821 /* coord = clamp(coord, min, max) */
822 coord = lp_build_clamp(coord_bld, coord, min, max);
823 icoord = lp_build_ifloor(coord_bld, coord);
824 }
825 break;
826
827 case PIPE_TEX_WRAP_MIRROR_CLAMP:
828 coord = lp_build_abs(coord_bld, coord);
829 coord = lp_build_mul(coord_bld, coord, length_f);
830 coord = lp_build_clamp(coord_bld, coord, coord_bld->zero, length_f_minus_one);
831 icoord = lp_build_ifloor(coord_bld, coord);
832 break;
833
834 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
835 {
836 LLVMValueRef min, max;
837 /* min = 1.0 / (2 * length) */
838 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
839 /* max = length - min */
840 max = lp_build_sub(coord_bld, length_f, min);
841
842 coord = lp_build_abs(coord_bld, coord);
843 coord = lp_build_mul(coord_bld, coord, length_f);
844 coord = lp_build_clamp(coord_bld, coord, min, max);
845 icoord = lp_build_ifloor(coord_bld, coord);
846 }
847 break;
848
849 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
850 {
851 LLVMValueRef min, max;
852 /* min = 1.0 / (2 * length) */
853 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
854 min = lp_build_negate(coord_bld, min);
855 /* max = length - min */
856 max = lp_build_sub(coord_bld, length_f, min);
857
858 coord = lp_build_abs(coord_bld, coord);
859 coord = lp_build_mul(coord_bld, coord, length_f);
860 coord = lp_build_clamp(coord_bld, coord, min, max);
861 icoord = lp_build_ifloor(coord_bld, coord);
862 }
863 break;
864
865 default:
866 assert(0);
867 icoord = NULL;
868 }
869
870 return icoord;
871 }
872
873
874 /**
875 * Codegen equivalent for u_minify().
876 * Return max(1, base_size >> level);
877 */
878 static LLVMValueRef
879 lp_build_minify(struct lp_build_sample_context *bld,
880 LLVMValueRef base_size,
881 LLVMValueRef level)
882 {
883 LLVMValueRef size = LLVMBuildAShr(bld->builder, base_size, level, "minify");
884 size = lp_build_max(&bld->int_coord_bld, size, bld->int_coord_bld.one);
885 return size;
886 }
887
888
889 /**
890 * Generate code to compute texture level of detail (lambda).
891 * \param s vector of texcoord s values
892 * \param t vector of texcoord t values
893 * \param r vector of texcoord r values
894 * \param shader_lod_bias vector float with the shader lod bias,
895 * \param width scalar int texture width
896 * \param height scalar int texture height
897 * \param depth scalar int texture depth
898 */
899 static LLVMValueRef
900 lp_build_lod_selector(struct lp_build_sample_context *bld,
901 LLVMValueRef s,
902 LLVMValueRef t,
903 LLVMValueRef r,
904 LLVMValueRef shader_lod_bias,
905 LLVMValueRef width,
906 LLVMValueRef height,
907 LLVMValueRef depth)
908
909 {
910 if (bld->static_state->min_lod == bld->static_state->max_lod) {
911 /* User is forcing sampling from a particular mipmap level.
912 * This is hit during mipmap generation.
913 */
914 return LLVMConstReal(LLVMFloatType(), bld->static_state->min_lod);
915 }
916 else {
917 const int dims = texture_dims(bld->static_state->target);
918 struct lp_build_context *float_bld = &bld->float_bld;
919 LLVMValueRef sampler_lod_bias = LLVMConstReal(LLVMFloatType(),
920 bld->static_state->lod_bias);
921 LLVMValueRef min_lod = LLVMConstReal(LLVMFloatType(),
922 bld->static_state->min_lod);
923 LLVMValueRef max_lod = LLVMConstReal(LLVMFloatType(),
924 bld->static_state->max_lod);
925
926 LLVMValueRef index0 = LLVMConstInt(LLVMInt32Type(), 0, 0);
927 LLVMValueRef index1 = LLVMConstInt(LLVMInt32Type(), 1, 0);
928 LLVMValueRef index2 = LLVMConstInt(LLVMInt32Type(), 2, 0);
929
930 LLVMValueRef s0, s1, s2;
931 LLVMValueRef t0, t1, t2;
932 LLVMValueRef r0, r1, r2;
933 LLVMValueRef dsdx, dsdy, dtdx, dtdy, drdx, drdy;
934 LLVMValueRef rho, lod;
935
936 /*
937 * dsdx = abs(s[1] - s[0]);
938 * dsdy = abs(s[2] - s[0]);
939 * dtdx = abs(t[1] - t[0]);
940 * dtdy = abs(t[2] - t[0]);
941 * drdx = abs(r[1] - r[0]);
942 * drdy = abs(r[2] - r[0]);
943 * XXX we're assuming a four-element quad in 2x2 layout here.
944 */
945 s0 = LLVMBuildExtractElement(bld->builder, s, index0, "s0");
946 s1 = LLVMBuildExtractElement(bld->builder, s, index1, "s1");
947 s2 = LLVMBuildExtractElement(bld->builder, s, index2, "s2");
948 dsdx = LLVMBuildSub(bld->builder, s1, s0, "");
949 dsdx = lp_build_abs(float_bld, dsdx);
950 dsdy = LLVMBuildSub(bld->builder, s2, s0, "");
951 dsdy = lp_build_abs(float_bld, dsdy);
952 if (dims > 1) {
953 t0 = LLVMBuildExtractElement(bld->builder, t, index0, "t0");
954 t1 = LLVMBuildExtractElement(bld->builder, t, index1, "t1");
955 t2 = LLVMBuildExtractElement(bld->builder, t, index2, "t2");
956 dtdx = LLVMBuildSub(bld->builder, t1, t0, "");
957 dtdx = lp_build_abs(float_bld, dtdx);
958 dtdy = LLVMBuildSub(bld->builder, t2, t0, "");
959 dtdy = lp_build_abs(float_bld, dtdy);
960 if (dims > 2) {
961 r0 = LLVMBuildExtractElement(bld->builder, r, index0, "r0");
962 r1 = LLVMBuildExtractElement(bld->builder, r, index1, "r1");
963 r2 = LLVMBuildExtractElement(bld->builder, r, index2, "r2");
964 drdx = LLVMBuildSub(bld->builder, r1, r0, "");
965 drdx = lp_build_abs(float_bld, drdx);
966 drdy = LLVMBuildSub(bld->builder, r2, r0, "");
967 drdy = lp_build_abs(float_bld, drdy);
968 }
969 }
970
971 /* Compute rho = max of all partial derivatives scaled by texture size.
972 * XXX this could be vectorized somewhat
973 */
974 rho = LLVMBuildMul(bld->builder,
975 lp_build_max(float_bld, dsdx, dsdy),
976 lp_build_int_to_float(float_bld, width), "");
977 if (dims > 1) {
978 LLVMValueRef max;
979 max = LLVMBuildMul(bld->builder,
980 lp_build_max(float_bld, dtdx, dtdy),
981 lp_build_int_to_float(float_bld, height), "");
982 rho = lp_build_max(float_bld, rho, max);
983 if (dims > 2) {
984 max = LLVMBuildMul(bld->builder,
985 lp_build_max(float_bld, drdx, drdy),
986 lp_build_int_to_float(float_bld, depth), "");
987 rho = lp_build_max(float_bld, rho, max);
988 }
989 }
990
991 /* compute lod = log2(rho) */
992 lod = lp_build_log2(float_bld, rho);
993
994 /* add sampler lod bias */
995 lod = LLVMBuildAdd(bld->builder, lod, sampler_lod_bias, "sampler LOD bias");
996
997 /* add shader lod bias */
998 /* XXX for now we take only the first element since our lod is scalar */
999 shader_lod_bias = LLVMBuildExtractElement(bld->builder, shader_lod_bias,
1000 LLVMConstInt(LLVMInt32Type(), 0, 0), "");
1001 lod = LLVMBuildAdd(bld->builder, lod, shader_lod_bias, "shader LOD bias");
1002
1003 /* clamp lod */
1004 lod = lp_build_clamp(float_bld, lod, min_lod, max_lod);
1005
1006 return lod;
1007 }
1008 }
1009
1010
1011 /**
1012 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
1013 * mipmap level index.
1014 * Note: this is all scalar code.
1015 * \param lod scalar float texture level of detail
1016 * \param level_out returns integer
1017 */
1018 static void
1019 lp_build_nearest_mip_level(struct lp_build_sample_context *bld,
1020 unsigned unit,
1021 LLVMValueRef lod,
1022 LLVMValueRef *level_out)
1023 {
1024 struct lp_build_context *float_bld = &bld->float_bld;
1025 struct lp_build_context *int_bld = &bld->int_bld;
1026 LLVMValueRef last_level, level;
1027
1028 LLVMValueRef zero = LLVMConstInt(LLVMInt32Type(), 0, 0);
1029
1030 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
1031 bld->builder, unit);
1032
1033 /* convert float lod to integer */
1034 level = lp_build_iround(float_bld, lod);
1035
1036 /* clamp level to legal range of levels */
1037 *level_out = lp_build_clamp(int_bld, level, zero, last_level);
1038 }
1039
1040
1041 /**
1042 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
1043 * two (adjacent) mipmap level indexes. Later, we'll sample from those
1044 * two mipmap levels and interpolate between them.
1045 */
1046 static void
1047 lp_build_linear_mip_levels(struct lp_build_sample_context *bld,
1048 unsigned unit,
1049 LLVMValueRef lod,
1050 LLVMValueRef *level0_out,
1051 LLVMValueRef *level1_out,
1052 LLVMValueRef *weight_out)
1053 {
1054 struct lp_build_context *float_bld = &bld->float_bld;
1055 struct lp_build_context *int_bld = &bld->int_bld;
1056 LLVMValueRef last_level, level;
1057
1058 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
1059 bld->builder, unit);
1060
1061 /* convert float lod to integer */
1062 level = lp_build_ifloor(float_bld, lod);
1063
1064 /* compute level 0 and clamp to legal range of levels */
1065 *level0_out = lp_build_clamp(int_bld, level,
1066 int_bld->zero,
1067 last_level);
1068 /* compute level 1 and clamp to legal range of levels */
1069 *level1_out = lp_build_add(int_bld, *level0_out, int_bld->one);
1070 *level1_out = lp_build_min(int_bld, *level1_out, last_level);
1071
1072 *weight_out = lp_build_fract(float_bld, lod);
1073 }
1074
1075
1076 /**
1077 * Generate code to sample a mipmap level with nearest filtering.
1078 * If sampling a cube texture, r = cube face in [0,5].
1079 */
1080 static void
1081 lp_build_sample_image_nearest(struct lp_build_sample_context *bld,
1082 LLVMValueRef width_vec,
1083 LLVMValueRef height_vec,
1084 LLVMValueRef depth_vec,
1085 LLVMValueRef row_stride_vec,
1086 LLVMValueRef img_stride_vec,
1087 LLVMValueRef data_ptr,
1088 LLVMValueRef s,
1089 LLVMValueRef t,
1090 LLVMValueRef r,
1091 LLVMValueRef colors_out[4])
1092 {
1093 const int dims = texture_dims(bld->static_state->target);
1094 LLVMValueRef x, y, z;
1095
1096 /*
1097 * Compute integer texcoords.
1098 */
1099 x = lp_build_sample_wrap_nearest(bld, s, width_vec,
1100 bld->static_state->pot_width,
1101 bld->static_state->wrap_s);
1102 lp_build_name(x, "tex.x.wrapped");
1103
1104 if (dims >= 2) {
1105 y = lp_build_sample_wrap_nearest(bld, t, height_vec,
1106 bld->static_state->pot_height,
1107 bld->static_state->wrap_t);
1108 lp_build_name(y, "tex.y.wrapped");
1109
1110 if (dims == 3) {
1111 z = lp_build_sample_wrap_nearest(bld, r, depth_vec,
1112 bld->static_state->pot_height,
1113 bld->static_state->wrap_r);
1114 lp_build_name(z, "tex.z.wrapped");
1115 }
1116 else if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
1117 z = r;
1118 }
1119 else {
1120 z = NULL;
1121 }
1122 }
1123 else {
1124 y = z = NULL;
1125 }
1126
1127 /*
1128 * Get texture colors.
1129 */
1130 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1131 x, y, z,
1132 row_stride_vec, img_stride_vec,
1133 data_ptr, colors_out);
1134 }
1135
1136
1137 /**
1138 * Generate code to sample a mipmap level with linear filtering.
1139 * If sampling a cube texture, r = cube face in [0,5].
1140 */
1141 static void
1142 lp_build_sample_image_linear(struct lp_build_sample_context *bld,
1143 LLVMValueRef width_vec,
1144 LLVMValueRef height_vec,
1145 LLVMValueRef depth_vec,
1146 LLVMValueRef row_stride_vec,
1147 LLVMValueRef img_stride_vec,
1148 LLVMValueRef data_ptr,
1149 LLVMValueRef s,
1150 LLVMValueRef t,
1151 LLVMValueRef r,
1152 LLVMValueRef colors_out[4])
1153 {
1154 const int dims = texture_dims(bld->static_state->target);
1155 LLVMValueRef x0, y0, z0, x1, y1, z1;
1156 LLVMValueRef s_fpart, t_fpart, r_fpart;
1157 LLVMValueRef neighbors[2][2][4];
1158 int chan;
1159
1160 /*
1161 * Compute integer texcoords.
1162 */
1163 lp_build_sample_wrap_linear(bld, s, width_vec,
1164 bld->static_state->pot_width,
1165 bld->static_state->wrap_s,
1166 &x0, &x1, &s_fpart);
1167 lp_build_name(x0, "tex.x0.wrapped");
1168 lp_build_name(x1, "tex.x1.wrapped");
1169
1170 if (dims >= 2) {
1171 lp_build_sample_wrap_linear(bld, t, height_vec,
1172 bld->static_state->pot_height,
1173 bld->static_state->wrap_t,
1174 &y0, &y1, &t_fpart);
1175 lp_build_name(y0, "tex.y0.wrapped");
1176 lp_build_name(y1, "tex.y1.wrapped");
1177
1178 if (dims == 3) {
1179 lp_build_sample_wrap_linear(bld, r, depth_vec,
1180 bld->static_state->pot_depth,
1181 bld->static_state->wrap_r,
1182 &z0, &z1, &r_fpart);
1183 lp_build_name(z0, "tex.z0.wrapped");
1184 lp_build_name(z1, "tex.z1.wrapped");
1185 }
1186 else if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
1187 z0 = z1 = r; /* cube face */
1188 r_fpart = NULL;
1189 }
1190 else {
1191 z0 = z1 = NULL;
1192 r_fpart = NULL;
1193 }
1194 }
1195 else {
1196 y0 = y1 = t_fpart = NULL;
1197 z0 = z1 = r_fpart = NULL;
1198 }
1199
1200 /*
1201 * Get texture colors.
1202 */
1203 /* get x0/x1 texels */
1204 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1205 x0, y0, z0,
1206 row_stride_vec, img_stride_vec,
1207 data_ptr, neighbors[0][0]);
1208 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1209 x1, y0, z0,
1210 row_stride_vec, img_stride_vec,
1211 data_ptr, neighbors[0][1]);
1212
1213 if (dims == 1) {
1214 /* Interpolate two samples from 1D image to produce one color */
1215 for (chan = 0; chan < 4; chan++) {
1216 colors_out[chan] = lp_build_lerp(&bld->texel_bld, s_fpart,
1217 neighbors[0][0][chan],
1218 neighbors[0][1][chan]);
1219 }
1220 }
1221 else {
1222 /* 2D/3D texture */
1223 LLVMValueRef colors0[4];
1224
1225 /* get x0/x1 texels at y1 */
1226 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1227 x0, y1, z0,
1228 row_stride_vec, img_stride_vec,
1229 data_ptr, neighbors[1][0]);
1230 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1231 x1, y1, z0,
1232 row_stride_vec, img_stride_vec,
1233 data_ptr, neighbors[1][1]);
1234
1235 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
1236 for (chan = 0; chan < 4; chan++) {
1237 colors0[chan] = lp_build_lerp_2d(&bld->texel_bld,
1238 s_fpart, t_fpart,
1239 neighbors[0][0][chan],
1240 neighbors[0][1][chan],
1241 neighbors[1][0][chan],
1242 neighbors[1][1][chan]);
1243 }
1244
1245 if (dims == 3) {
1246 LLVMValueRef neighbors1[2][2][4];
1247 LLVMValueRef colors1[4];
1248
1249 /* get x0/x1/y0/y1 texels at z1 */
1250 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1251 x0, y0, z1,
1252 row_stride_vec, img_stride_vec,
1253 data_ptr, neighbors1[0][0]);
1254 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1255 x1, y0, z1,
1256 row_stride_vec, img_stride_vec,
1257 data_ptr, neighbors1[0][1]);
1258 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1259 x0, y1, z1,
1260 row_stride_vec, img_stride_vec,
1261 data_ptr, neighbors1[1][0]);
1262 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1263 x1, y1, z1,
1264 row_stride_vec, img_stride_vec,
1265 data_ptr, neighbors1[1][1]);
1266
1267 /* Bilinear interpolate the four samples from the second Z slice */
1268 for (chan = 0; chan < 4; chan++) {
1269 colors1[chan] = lp_build_lerp_2d(&bld->texel_bld,
1270 s_fpart, t_fpart,
1271 neighbors1[0][0][chan],
1272 neighbors1[0][1][chan],
1273 neighbors1[1][0][chan],
1274 neighbors1[1][1][chan]);
1275 }
1276
1277 /* Linearly interpolate the two samples from the two 3D slices */
1278 for (chan = 0; chan < 4; chan++) {
1279 colors_out[chan] = lp_build_lerp(&bld->texel_bld,
1280 r_fpart,
1281 colors0[chan], colors1[chan]);
1282 }
1283 }
1284 else {
1285 /* 2D tex */
1286 for (chan = 0; chan < 4; chan++) {
1287 colors_out[chan] = colors0[chan];
1288 }
1289 }
1290 }
1291 }
1292
1293
1294 /** Helper used by lp_build_cube_lookup() */
1295 static LLVMValueRef
1296 lp_build_cube_ima(struct lp_build_context *coord_bld, LLVMValueRef coord)
1297 {
1298 /* ima = -0.5 / abs(coord); */
1299 LLVMValueRef negHalf = lp_build_const_vec(coord_bld->type, -0.5);
1300 LLVMValueRef absCoord = lp_build_abs(coord_bld, coord);
1301 LLVMValueRef ima = lp_build_mul(coord_bld, negHalf,
1302 lp_build_rcp(coord_bld, absCoord));
1303 return ima;
1304 }
1305
1306
1307 /**
1308 * Helper used by lp_build_cube_lookup()
1309 * \param sign scalar +1 or -1
1310 * \param coord float vector
1311 * \param ima float vector
1312 */
1313 static LLVMValueRef
1314 lp_build_cube_coord(struct lp_build_context *coord_bld,
1315 LLVMValueRef sign, int negate_coord,
1316 LLVMValueRef coord, LLVMValueRef ima)
1317 {
1318 /* return negate(coord) * ima * sign + 0.5; */
1319 LLVMValueRef half = lp_build_const_vec(coord_bld->type, 0.5);
1320 LLVMValueRef res;
1321
1322 assert(negate_coord == +1 || negate_coord == -1);
1323
1324 if (negate_coord == -1) {
1325 coord = lp_build_negate(coord_bld, coord);
1326 }
1327
1328 res = lp_build_mul(coord_bld, coord, ima);
1329 if (sign) {
1330 sign = lp_build_broadcast_scalar(coord_bld, sign);
1331 res = lp_build_mul(coord_bld, res, sign);
1332 }
1333 res = lp_build_add(coord_bld, res, half);
1334
1335 return res;
1336 }
1337
1338
1339 /** Helper used by lp_build_cube_lookup()
1340 * Return (major_coord >= 0) ? pos_face : neg_face;
1341 */
1342 static LLVMValueRef
1343 lp_build_cube_face(struct lp_build_sample_context *bld,
1344 LLVMValueRef major_coord,
1345 unsigned pos_face, unsigned neg_face)
1346 {
1347 LLVMValueRef cmp = LLVMBuildFCmp(bld->builder, LLVMRealUGE,
1348 major_coord,
1349 bld->float_bld.zero, "");
1350 LLVMValueRef pos = LLVMConstInt(LLVMInt32Type(), pos_face, 0);
1351 LLVMValueRef neg = LLVMConstInt(LLVMInt32Type(), neg_face, 0);
1352 LLVMValueRef res = LLVMBuildSelect(bld->builder, cmp, pos, neg, "");
1353 return res;
1354 }
1355
1356
1357
1358 /**
1359 * Generate code to do cube face selection and per-face texcoords.
1360 */
1361 static void
1362 lp_build_cube_lookup(struct lp_build_sample_context *bld,
1363 LLVMValueRef s,
1364 LLVMValueRef t,
1365 LLVMValueRef r,
1366 LLVMValueRef *face,
1367 LLVMValueRef *face_s,
1368 LLVMValueRef *face_t)
1369 {
1370 struct lp_build_context *float_bld = &bld->float_bld;
1371 struct lp_build_context *coord_bld = &bld->coord_bld;
1372 LLVMValueRef rx, ry, rz;
1373 LLVMValueRef arx, ary, arz;
1374 LLVMValueRef c25 = LLVMConstReal(LLVMFloatType(), 0.25);
1375 LLVMValueRef arx_ge_ary, arx_ge_arz;
1376 LLVMValueRef ary_ge_arx, ary_ge_arz;
1377 LLVMValueRef arx_ge_ary_arz, ary_ge_arx_arz;
1378 LLVMValueRef rx_pos, ry_pos, rz_pos;
1379
1380 assert(bld->coord_bld.type.length == 4);
1381
1382 /*
1383 * Use the average of the four pixel's texcoords to choose the face.
1384 */
1385 rx = lp_build_mul(float_bld, c25,
1386 lp_build_sum_vector(&bld->coord_bld, s));
1387 ry = lp_build_mul(float_bld, c25,
1388 lp_build_sum_vector(&bld->coord_bld, t));
1389 rz = lp_build_mul(float_bld, c25,
1390 lp_build_sum_vector(&bld->coord_bld, r));
1391
1392 arx = lp_build_abs(float_bld, rx);
1393 ary = lp_build_abs(float_bld, ry);
1394 arz = lp_build_abs(float_bld, rz);
1395
1396 /*
1397 * Compare sign/magnitude of rx,ry,rz to determine face
1398 */
1399 arx_ge_ary = LLVMBuildFCmp(bld->builder, LLVMRealUGE, arx, ary, "");
1400 arx_ge_arz = LLVMBuildFCmp(bld->builder, LLVMRealUGE, arx, arz, "");
1401 ary_ge_arx = LLVMBuildFCmp(bld->builder, LLVMRealUGE, ary, arx, "");
1402 ary_ge_arz = LLVMBuildFCmp(bld->builder, LLVMRealUGE, ary, arz, "");
1403
1404 arx_ge_ary_arz = LLVMBuildAnd(bld->builder, arx_ge_ary, arx_ge_arz, "");
1405 ary_ge_arx_arz = LLVMBuildAnd(bld->builder, ary_ge_arx, ary_ge_arz, "");
1406
1407 rx_pos = LLVMBuildFCmp(bld->builder, LLVMRealUGE, rx, float_bld->zero, "");
1408 ry_pos = LLVMBuildFCmp(bld->builder, LLVMRealUGE, ry, float_bld->zero, "");
1409 rz_pos = LLVMBuildFCmp(bld->builder, LLVMRealUGE, rz, float_bld->zero, "");
1410
1411 {
1412 struct lp_build_flow_context *flow_ctx;
1413 struct lp_build_if_state if_ctx;
1414
1415 flow_ctx = lp_build_flow_create(bld->builder);
1416 lp_build_flow_scope_begin(flow_ctx);
1417
1418 *face_s = bld->coord_bld.undef;
1419 *face_t = bld->coord_bld.undef;
1420 *face = bld->int_bld.undef;
1421
1422 lp_build_name(*face_s, "face_s");
1423 lp_build_name(*face_t, "face_t");
1424 lp_build_name(*face, "face");
1425
1426 lp_build_flow_scope_declare(flow_ctx, face_s);
1427 lp_build_flow_scope_declare(flow_ctx, face_t);
1428 lp_build_flow_scope_declare(flow_ctx, face);
1429
1430 lp_build_if(&if_ctx, flow_ctx, bld->builder, arx_ge_ary_arz);
1431 {
1432 /* +/- X face */
1433 LLVMValueRef sign = lp_build_sgn(float_bld, rx);
1434 LLVMValueRef ima = lp_build_cube_ima(coord_bld, s);
1435 *face_s = lp_build_cube_coord(coord_bld, sign, +1, r, ima);
1436 *face_t = lp_build_cube_coord(coord_bld, NULL, +1, t, ima);
1437 *face = lp_build_cube_face(bld, rx,
1438 PIPE_TEX_FACE_POS_X,
1439 PIPE_TEX_FACE_NEG_X);
1440 }
1441 lp_build_else(&if_ctx);
1442 {
1443 struct lp_build_flow_context *flow_ctx2;
1444 struct lp_build_if_state if_ctx2;
1445
1446 LLVMValueRef face_s2 = bld->coord_bld.undef;
1447 LLVMValueRef face_t2 = bld->coord_bld.undef;
1448 LLVMValueRef face2 = bld->int_bld.undef;
1449
1450 flow_ctx2 = lp_build_flow_create(bld->builder);
1451 lp_build_flow_scope_begin(flow_ctx2);
1452 lp_build_flow_scope_declare(flow_ctx2, &face_s2);
1453 lp_build_flow_scope_declare(flow_ctx2, &face_t2);
1454 lp_build_flow_scope_declare(flow_ctx2, &face2);
1455
1456 ary_ge_arx_arz = LLVMBuildAnd(bld->builder, ary_ge_arx, ary_ge_arz, "");
1457
1458 lp_build_if(&if_ctx2, flow_ctx2, bld->builder, ary_ge_arx_arz);
1459 {
1460 /* +/- Y face */
1461 LLVMValueRef sign = lp_build_sgn(float_bld, ry);
1462 LLVMValueRef ima = lp_build_cube_ima(coord_bld, t);
1463 face_s2 = lp_build_cube_coord(coord_bld, NULL, -1, s, ima);
1464 face_t2 = lp_build_cube_coord(coord_bld, sign, -1, r, ima);
1465 face2 = lp_build_cube_face(bld, ry,
1466 PIPE_TEX_FACE_POS_Y,
1467 PIPE_TEX_FACE_NEG_Y);
1468 }
1469 lp_build_else(&if_ctx2);
1470 {
1471 /* +/- Z face */
1472 LLVMValueRef sign = lp_build_sgn(float_bld, rz);
1473 LLVMValueRef ima = lp_build_cube_ima(coord_bld, r);
1474 face_s2 = lp_build_cube_coord(coord_bld, sign, -1, s, ima);
1475 face_t2 = lp_build_cube_coord(coord_bld, NULL, +1, t, ima);
1476 face2 = lp_build_cube_face(bld, rz,
1477 PIPE_TEX_FACE_POS_Z,
1478 PIPE_TEX_FACE_NEG_Z);
1479 }
1480 lp_build_endif(&if_ctx2);
1481 lp_build_flow_scope_end(flow_ctx2);
1482 lp_build_flow_destroy(flow_ctx2);
1483
1484 *face_s = face_s2;
1485 *face_t = face_t2;
1486 *face = face2;
1487 }
1488
1489 lp_build_endif(&if_ctx);
1490 lp_build_flow_scope_end(flow_ctx);
1491 lp_build_flow_destroy(flow_ctx);
1492 }
1493 }
1494
1495
1496
1497 /**
1498 * Sample the texture/mipmap using given image filter and mip filter.
1499 * data0_ptr and data1_ptr point to the two mipmap levels to sample
1500 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
1501 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1502 */
1503 static void
1504 lp_build_sample_mipmap(struct lp_build_sample_context *bld,
1505 unsigned img_filter,
1506 unsigned mip_filter,
1507 LLVMValueRef s,
1508 LLVMValueRef t,
1509 LLVMValueRef r,
1510 LLVMValueRef lod_fpart,
1511 LLVMValueRef width0_vec,
1512 LLVMValueRef width1_vec,
1513 LLVMValueRef height0_vec,
1514 LLVMValueRef height1_vec,
1515 LLVMValueRef depth0_vec,
1516 LLVMValueRef depth1_vec,
1517 LLVMValueRef row_stride0_vec,
1518 LLVMValueRef row_stride1_vec,
1519 LLVMValueRef img_stride0_vec,
1520 LLVMValueRef img_stride1_vec,
1521 LLVMValueRef data_ptr0,
1522 LLVMValueRef data_ptr1,
1523 LLVMValueRef *colors_out)
1524 {
1525 LLVMValueRef colors0[4], colors1[4];
1526 int chan;
1527
1528 if (img_filter == PIPE_TEX_FILTER_NEAREST) {
1529 lp_build_sample_image_nearest(bld,
1530 width0_vec, height0_vec, depth0_vec,
1531 row_stride0_vec, img_stride0_vec,
1532 data_ptr0, s, t, r, colors0);
1533
1534 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1535 /* sample the second mipmap level, and interp */
1536 lp_build_sample_image_nearest(bld,
1537 width1_vec, height1_vec, depth1_vec,
1538 row_stride1_vec, img_stride1_vec,
1539 data_ptr1, s, t, r, colors1);
1540 }
1541 }
1542 else {
1543 assert(img_filter == PIPE_TEX_FILTER_LINEAR);
1544
1545 lp_build_sample_image_linear(bld,
1546 width0_vec, height0_vec, depth0_vec,
1547 row_stride0_vec, img_stride0_vec,
1548 data_ptr0, s, t, r, colors0);
1549
1550 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1551 /* sample the second mipmap level, and interp */
1552 lp_build_sample_image_linear(bld,
1553 width1_vec, height1_vec, depth1_vec,
1554 row_stride1_vec, img_stride1_vec,
1555 data_ptr1, s, t, r, colors1);
1556 }
1557 }
1558
1559 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1560 /* interpolate samples from the two mipmap levels */
1561 for (chan = 0; chan < 4; chan++) {
1562 colors_out[chan] = lp_build_lerp(&bld->texel_bld, lod_fpart,
1563 colors0[chan], colors1[chan]);
1564 }
1565 }
1566 else {
1567 /* use first/only level's colors */
1568 for (chan = 0; chan < 4; chan++) {
1569 colors_out[chan] = colors0[chan];
1570 }
1571 }
1572 }
1573
1574
1575
1576 /**
1577 * General texture sampling codegen.
1578 * This function handles texture sampling for all texture targets (1D,
1579 * 2D, 3D, cube) and all filtering modes.
1580 */
1581 static void
1582 lp_build_sample_general(struct lp_build_sample_context *bld,
1583 unsigned unit,
1584 LLVMValueRef s,
1585 LLVMValueRef t,
1586 LLVMValueRef r,
1587 LLVMValueRef lodbias,
1588 LLVMValueRef width,
1589 LLVMValueRef height,
1590 LLVMValueRef depth,
1591 LLVMValueRef width_vec,
1592 LLVMValueRef height_vec,
1593 LLVMValueRef depth_vec,
1594 LLVMValueRef row_stride_array,
1595 LLVMValueRef img_stride_array,
1596 LLVMValueRef data_array,
1597 LLVMValueRef *colors_out)
1598 {
1599 struct lp_build_context *float_bld = &bld->float_bld;
1600 const unsigned mip_filter = bld->static_state->min_mip_filter;
1601 const unsigned min_filter = bld->static_state->min_img_filter;
1602 const unsigned mag_filter = bld->static_state->mag_img_filter;
1603 const int dims = texture_dims(bld->static_state->target);
1604 LLVMValueRef lod = NULL, lod_fpart = NULL;
1605 LLVMValueRef ilevel0, ilevel1 = NULL, ilevel0_vec, ilevel1_vec = NULL;
1606 LLVMValueRef width0_vec = NULL, height0_vec = NULL, depth0_vec = NULL;
1607 LLVMValueRef width1_vec = NULL, height1_vec = NULL, depth1_vec = NULL;
1608 LLVMValueRef row_stride0_vec = NULL, row_stride1_vec = NULL;
1609 LLVMValueRef img_stride0_vec = NULL, img_stride1_vec = NULL;
1610 LLVMValueRef data_ptr0, data_ptr1 = NULL;
1611
1612 /*
1613 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1614 mip_filter, min_filter, mag_filter);
1615 */
1616
1617 /*
1618 * Compute the level of detail (float).
1619 */
1620 if (min_filter != mag_filter ||
1621 mip_filter != PIPE_TEX_MIPFILTER_NONE) {
1622 /* Need to compute lod either to choose mipmap levels or to
1623 * distinguish between minification/magnification with one mipmap level.
1624 */
1625 lod = lp_build_lod_selector(bld, s, t, r, lodbias, width, height, depth);
1626 }
1627
1628 /*
1629 * Compute integer mipmap level(s) to fetch texels from.
1630 */
1631 if (mip_filter == PIPE_TEX_MIPFILTER_NONE) {
1632 /* always use mip level 0 */
1633 ilevel0 = LLVMConstInt(LLVMInt32Type(), 0, 0);
1634 }
1635 else {
1636 if (mip_filter == PIPE_TEX_MIPFILTER_NEAREST) {
1637 lp_build_nearest_mip_level(bld, unit, lod, &ilevel0);
1638 }
1639 else {
1640 assert(mip_filter == PIPE_TEX_MIPFILTER_LINEAR);
1641 lp_build_linear_mip_levels(bld, unit, lod, &ilevel0, &ilevel1,
1642 &lod_fpart);
1643 lod_fpart = lp_build_broadcast_scalar(&bld->coord_bld, lod_fpart);
1644 }
1645 }
1646
1647 /*
1648 * Convert scalar integer mipmap levels into vectors.
1649 */
1650 ilevel0_vec = lp_build_broadcast_scalar(&bld->int_coord_bld, ilevel0);
1651 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
1652 ilevel1_vec = lp_build_broadcast_scalar(&bld->int_coord_bld, ilevel1);
1653
1654 /*
1655 * Compute width, height at mipmap level 'ilevel0'
1656 */
1657 width0_vec = lp_build_minify(bld, width_vec, ilevel0_vec);
1658 if (dims >= 2) {
1659 height0_vec = lp_build_minify(bld, height_vec, ilevel0_vec);
1660 row_stride0_vec = lp_build_get_level_stride_vec(bld, row_stride_array,
1661 ilevel0);
1662 if (dims == 3 || bld->static_state->target == PIPE_TEXTURE_CUBE) {
1663 img_stride0_vec = lp_build_get_level_stride_vec(bld,
1664 img_stride_array,
1665 ilevel0);
1666 if (dims == 3) {
1667 depth0_vec = lp_build_minify(bld, depth_vec, ilevel0_vec);
1668 }
1669 }
1670 }
1671 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1672 /* compute width, height, depth for second mipmap level at 'ilevel1' */
1673 width1_vec = lp_build_minify(bld, width_vec, ilevel1_vec);
1674 if (dims >= 2) {
1675 height1_vec = lp_build_minify(bld, height_vec, ilevel1_vec);
1676 row_stride1_vec = lp_build_get_level_stride_vec(bld, row_stride_array,
1677 ilevel1);
1678 if (dims == 3 || bld->static_state->target == PIPE_TEXTURE_CUBE) {
1679 img_stride1_vec = lp_build_get_level_stride_vec(bld,
1680 img_stride_array,
1681 ilevel1);
1682 if (dims ==3) {
1683 depth1_vec = lp_build_minify(bld, depth_vec, ilevel1_vec);
1684 }
1685 }
1686 }
1687 }
1688
1689 /*
1690 * Choose cube face, recompute per-face texcoords.
1691 */
1692 if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
1693 LLVMValueRef face, face_s, face_t;
1694 lp_build_cube_lookup(bld, s, t, r, &face, &face_s, &face_t);
1695 s = face_s; /* vec */
1696 t = face_t; /* vec */
1697 /* use 'r' to indicate cube face */
1698 r = lp_build_broadcast_scalar(&bld->int_coord_bld, face); /* vec */
1699 }
1700
1701 /*
1702 * Get pointer(s) to image data for mipmap level(s).
1703 */
1704 data_ptr0 = lp_build_get_mipmap_level(bld, data_array, ilevel0);
1705 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1706 data_ptr1 = lp_build_get_mipmap_level(bld, data_array, ilevel1);
1707 }
1708
1709 /*
1710 * Get/interpolate texture colors.
1711 */
1712 if (min_filter == mag_filter) {
1713 /* no need to distinquish between minification and magnification */
1714 lp_build_sample_mipmap(bld, min_filter, mip_filter, s, t, r, lod_fpart,
1715 width0_vec, width1_vec,
1716 height0_vec, height1_vec,
1717 depth0_vec, depth1_vec,
1718 row_stride0_vec, row_stride1_vec,
1719 img_stride0_vec, img_stride1_vec,
1720 data_ptr0, data_ptr1,
1721 colors_out);
1722 }
1723 else {
1724 /* Emit conditional to choose min image filter or mag image filter
1725 * depending on the lod being >0 or <= 0, respectively.
1726 */
1727 struct lp_build_flow_context *flow_ctx;
1728 struct lp_build_if_state if_ctx;
1729 LLVMValueRef minify;
1730
1731 flow_ctx = lp_build_flow_create(bld->builder);
1732 lp_build_flow_scope_begin(flow_ctx);
1733
1734 lp_build_flow_scope_declare(flow_ctx, &colors_out[0]);
1735 lp_build_flow_scope_declare(flow_ctx, &colors_out[1]);
1736 lp_build_flow_scope_declare(flow_ctx, &colors_out[2]);
1737 lp_build_flow_scope_declare(flow_ctx, &colors_out[3]);
1738
1739 /* minify = lod > 0.0 */
1740 minify = LLVMBuildFCmp(bld->builder, LLVMRealUGE,
1741 lod, float_bld->zero, "");
1742
1743 lp_build_if(&if_ctx, flow_ctx, bld->builder, minify);
1744 {
1745 /* Use the minification filter */
1746 lp_build_sample_mipmap(bld, min_filter, mip_filter,
1747 s, t, r, lod_fpart,
1748 width0_vec, width1_vec,
1749 height0_vec, height1_vec,
1750 depth0_vec, depth1_vec,
1751 row_stride0_vec, row_stride1_vec,
1752 img_stride0_vec, img_stride1_vec,
1753 data_ptr0, data_ptr1,
1754 colors_out);
1755 }
1756 lp_build_else(&if_ctx);
1757 {
1758 /* Use the magnification filter */
1759 lp_build_sample_mipmap(bld, mag_filter, mip_filter,
1760 s, t, r, lod_fpart,
1761 width0_vec, width1_vec,
1762 height0_vec, height1_vec,
1763 depth0_vec, depth1_vec,
1764 row_stride0_vec, row_stride1_vec,
1765 img_stride0_vec, img_stride1_vec,
1766 data_ptr0, data_ptr1,
1767 colors_out);
1768 }
1769 lp_build_endif(&if_ctx);
1770
1771 lp_build_flow_scope_end(flow_ctx);
1772 lp_build_flow_destroy(flow_ctx);
1773 }
1774 }
1775
1776
1777
1778 static void
1779 lp_build_rgba8_to_f32_soa(LLVMBuilderRef builder,
1780 struct lp_type dst_type,
1781 LLVMValueRef packed,
1782 LLVMValueRef *rgba)
1783 {
1784 LLVMValueRef mask = lp_build_const_int_vec(dst_type, 0xff);
1785 unsigned chan;
1786
1787 /* Decode the input vector components */
1788 for (chan = 0; chan < 4; ++chan) {
1789 unsigned start = chan*8;
1790 unsigned stop = start + 8;
1791 LLVMValueRef input;
1792
1793 input = packed;
1794
1795 if(start)
1796 input = LLVMBuildLShr(builder, input, lp_build_const_int_vec(dst_type, start), "");
1797
1798 if(stop < 32)
1799 input = LLVMBuildAnd(builder, input, mask, "");
1800
1801 input = lp_build_unsigned_norm_to_float(builder, 8, dst_type, input);
1802
1803 rgba[chan] = input;
1804 }
1805 }
1806
1807
1808 static void
1809 lp_build_sample_2d_linear_aos(struct lp_build_sample_context *bld,
1810 LLVMValueRef s,
1811 LLVMValueRef t,
1812 LLVMValueRef width,
1813 LLVMValueRef height,
1814 LLVMValueRef stride_array,
1815 LLVMValueRef data_array,
1816 LLVMValueRef *texel)
1817 {
1818 LLVMBuilderRef builder = bld->builder;
1819 struct lp_build_context i32, h16, u8n;
1820 LLVMTypeRef i32_vec_type, h16_vec_type, u8n_vec_type;
1821 LLVMValueRef i32_c8, i32_c128, i32_c255;
1822 LLVMValueRef s_ipart, s_fpart, s_fpart_lo, s_fpart_hi;
1823 LLVMValueRef t_ipart, t_fpart, t_fpart_lo, t_fpart_hi;
1824 LLVMValueRef x0, x1;
1825 LLVMValueRef y0, y1;
1826 LLVMValueRef neighbors[2][2];
1827 LLVMValueRef neighbors_lo[2][2];
1828 LLVMValueRef neighbors_hi[2][2];
1829 LLVMValueRef packed, packed_lo, packed_hi;
1830 LLVMValueRef unswizzled[4];
1831 LLVMValueRef stride;
1832
1833 assert(bld->static_state->target == PIPE_TEXTURE_2D);
1834 assert(bld->static_state->min_img_filter == PIPE_TEX_FILTER_LINEAR);
1835 assert(bld->static_state->mag_img_filter == PIPE_TEX_FILTER_LINEAR);
1836 assert(bld->static_state->min_mip_filter == PIPE_TEX_MIPFILTER_NONE);
1837
1838 lp_build_context_init(&i32, builder, lp_type_int_vec(32));
1839 lp_build_context_init(&h16, builder, lp_type_ufixed(16));
1840 lp_build_context_init(&u8n, builder, lp_type_unorm(8));
1841
1842 i32_vec_type = lp_build_vec_type(i32.type);
1843 h16_vec_type = lp_build_vec_type(h16.type);
1844 u8n_vec_type = lp_build_vec_type(u8n.type);
1845
1846 if (bld->static_state->normalized_coords) {
1847 LLVMTypeRef coord_vec_type = lp_build_vec_type(bld->coord_type);
1848 LLVMValueRef fp_width = LLVMBuildSIToFP(bld->builder, width, coord_vec_type, "");
1849 LLVMValueRef fp_height = LLVMBuildSIToFP(bld->builder, height, coord_vec_type, "");
1850 s = lp_build_mul(&bld->coord_bld, s, fp_width);
1851 t = lp_build_mul(&bld->coord_bld, t, fp_height);
1852 }
1853
1854 /* scale coords by 256 (8 fractional bits) */
1855 s = lp_build_mul_imm(&bld->coord_bld, s, 256);
1856 t = lp_build_mul_imm(&bld->coord_bld, t, 256);
1857
1858 /* convert float to int */
1859 s = LLVMBuildFPToSI(builder, s, i32_vec_type, "");
1860 t = LLVMBuildFPToSI(builder, t, i32_vec_type, "");
1861
1862 /* subtract 0.5 (add -128) */
1863 i32_c128 = lp_build_const_int_vec(i32.type, -128);
1864 s = LLVMBuildAdd(builder, s, i32_c128, "");
1865 t = LLVMBuildAdd(builder, t, i32_c128, "");
1866
1867 /* compute floor (shift right 8) */
1868 i32_c8 = lp_build_const_int_vec(i32.type, 8);
1869 s_ipart = LLVMBuildAShr(builder, s, i32_c8, "");
1870 t_ipart = LLVMBuildAShr(builder, t, i32_c8, "");
1871
1872 /* compute fractional part (AND with 0xff) */
1873 i32_c255 = lp_build_const_int_vec(i32.type, 255);
1874 s_fpart = LLVMBuildAnd(builder, s, i32_c255, "");
1875 t_fpart = LLVMBuildAnd(builder, t, i32_c255, "");
1876
1877 x0 = s_ipart;
1878 y0 = t_ipart;
1879
1880 x1 = lp_build_add(&bld->int_coord_bld, x0, bld->int_coord_bld.one);
1881 y1 = lp_build_add(&bld->int_coord_bld, y0, bld->int_coord_bld.one);
1882
1883 x0 = lp_build_sample_wrap_int(bld, x0, width, bld->static_state->pot_width,
1884 bld->static_state->wrap_s);
1885 y0 = lp_build_sample_wrap_int(bld, y0, height, bld->static_state->pot_height,
1886 bld->static_state->wrap_t);
1887
1888 x1 = lp_build_sample_wrap_int(bld, x1, width, bld->static_state->pot_width,
1889 bld->static_state->wrap_s);
1890 y1 = lp_build_sample_wrap_int(bld, y1, height, bld->static_state->pot_height,
1891 bld->static_state->wrap_t);
1892
1893 /*
1894 * Transform 4 x i32 in
1895 *
1896 * s_fpart = {s0, s1, s2, s3}
1897 *
1898 * into 8 x i16
1899 *
1900 * s_fpart = {00, s0, 00, s1, 00, s2, 00, s3}
1901 *
1902 * into two 8 x i16
1903 *
1904 * s_fpart_lo = {s0, s0, s0, s0, s1, s1, s1, s1}
1905 * s_fpart_hi = {s2, s2, s2, s2, s3, s3, s3, s3}
1906 *
1907 * and likewise for t_fpart. There is no risk of loosing precision here
1908 * since the fractional parts only use the lower 8bits.
1909 */
1910
1911 s_fpart = LLVMBuildBitCast(builder, s_fpart, h16_vec_type, "");
1912 t_fpart = LLVMBuildBitCast(builder, t_fpart, h16_vec_type, "");
1913
1914 {
1915 LLVMTypeRef elem_type = LLVMInt32Type();
1916 LLVMValueRef shuffles_lo[LP_MAX_VECTOR_LENGTH];
1917 LLVMValueRef shuffles_hi[LP_MAX_VECTOR_LENGTH];
1918 LLVMValueRef shuffle_lo;
1919 LLVMValueRef shuffle_hi;
1920 unsigned i, j;
1921
1922 for(j = 0; j < h16.type.length; j += 4) {
1923 unsigned subindex = util_cpu_caps.little_endian ? 0 : 1;
1924 LLVMValueRef index;
1925
1926 index = LLVMConstInt(elem_type, j/2 + subindex, 0);
1927 for(i = 0; i < 4; ++i)
1928 shuffles_lo[j + i] = index;
1929
1930 index = LLVMConstInt(elem_type, h16.type.length/2 + j/2 + subindex, 0);
1931 for(i = 0; i < 4; ++i)
1932 shuffles_hi[j + i] = index;
1933 }
1934
1935 shuffle_lo = LLVMConstVector(shuffles_lo, h16.type.length);
1936 shuffle_hi = LLVMConstVector(shuffles_hi, h16.type.length);
1937
1938 s_fpart_lo = LLVMBuildShuffleVector(builder, s_fpart, h16.undef, shuffle_lo, "");
1939 t_fpart_lo = LLVMBuildShuffleVector(builder, t_fpart, h16.undef, shuffle_lo, "");
1940 s_fpart_hi = LLVMBuildShuffleVector(builder, s_fpart, h16.undef, shuffle_hi, "");
1941 t_fpart_hi = LLVMBuildShuffleVector(builder, t_fpart, h16.undef, shuffle_hi, "");
1942 }
1943
1944 stride = lp_build_get_const_level_stride_vec(bld, stride_array, 0);
1945
1946 /*
1947 * Fetch the pixels as 4 x 32bit (rgba order might differ):
1948 *
1949 * rgba0 rgba1 rgba2 rgba3
1950 *
1951 * bit cast them into 16 x u8
1952 *
1953 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
1954 *
1955 * unpack them into two 8 x i16:
1956 *
1957 * r0 g0 b0 a0 r1 g1 b1 a1
1958 * r2 g2 b2 a2 r3 g3 b3 a3
1959 *
1960 * The higher 8 bits of the resulting elements will be zero.
1961 */
1962
1963 neighbors[0][0] = lp_build_sample_packed(bld, x0, y0, stride, data_array);
1964 neighbors[0][1] = lp_build_sample_packed(bld, x1, y0, stride, data_array);
1965 neighbors[1][0] = lp_build_sample_packed(bld, x0, y1, stride, data_array);
1966 neighbors[1][1] = lp_build_sample_packed(bld, x1, y1, stride, data_array);
1967
1968 neighbors[0][0] = LLVMBuildBitCast(builder, neighbors[0][0], u8n_vec_type, "");
1969 neighbors[0][1] = LLVMBuildBitCast(builder, neighbors[0][1], u8n_vec_type, "");
1970 neighbors[1][0] = LLVMBuildBitCast(builder, neighbors[1][0], u8n_vec_type, "");
1971 neighbors[1][1] = LLVMBuildBitCast(builder, neighbors[1][1], u8n_vec_type, "");
1972
1973 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[0][0], &neighbors_lo[0][0], &neighbors_hi[0][0]);
1974 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[0][1], &neighbors_lo[0][1], &neighbors_hi[0][1]);
1975 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[1][0], &neighbors_lo[1][0], &neighbors_hi[1][0]);
1976 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[1][1], &neighbors_lo[1][1], &neighbors_hi[1][1]);
1977
1978 /*
1979 * Linear interpolate with 8.8 fixed point.
1980 */
1981
1982 packed_lo = lp_build_lerp_2d(&h16,
1983 s_fpart_lo, t_fpart_lo,
1984 neighbors_lo[0][0],
1985 neighbors_lo[0][1],
1986 neighbors_lo[1][0],
1987 neighbors_lo[1][1]);
1988
1989 packed_hi = lp_build_lerp_2d(&h16,
1990 s_fpart_hi, t_fpart_hi,
1991 neighbors_hi[0][0],
1992 neighbors_hi[0][1],
1993 neighbors_hi[1][0],
1994 neighbors_hi[1][1]);
1995
1996 packed = lp_build_pack2(builder, h16.type, u8n.type, packed_lo, packed_hi);
1997
1998 /*
1999 * Convert to SoA and swizzle.
2000 */
2001
2002 packed = LLVMBuildBitCast(builder, packed, i32_vec_type, "");
2003
2004 lp_build_rgba8_to_f32_soa(bld->builder,
2005 bld->texel_type,
2006 packed, unswizzled);
2007
2008 lp_build_format_swizzle_soa(bld->format_desc,
2009 bld->texel_type, unswizzled,
2010 texel);
2011
2012 lp_build_swizzle_soa(bld, texel);
2013 }
2014
2015
2016 static void
2017 lp_build_sample_compare(struct lp_build_sample_context *bld,
2018 LLVMValueRef p,
2019 LLVMValueRef *texel)
2020 {
2021 struct lp_build_context *texel_bld = &bld->texel_bld;
2022 LLVMValueRef res;
2023 unsigned chan;
2024
2025 if(bld->static_state->compare_mode == PIPE_TEX_COMPARE_NONE)
2026 return;
2027
2028 /* TODO: Compare before swizzling, to avoid redundant computations */
2029 res = NULL;
2030 for(chan = 0; chan < 4; ++chan) {
2031 LLVMValueRef cmp;
2032 cmp = lp_build_cmp(texel_bld, bld->static_state->compare_func, p, texel[chan]);
2033 cmp = lp_build_select(texel_bld, cmp, texel_bld->one, texel_bld->zero);
2034
2035 if(res)
2036 res = lp_build_add(texel_bld, res, cmp);
2037 else
2038 res = cmp;
2039 }
2040
2041 assert(res);
2042 res = lp_build_mul(texel_bld, res, lp_build_const_vec(texel_bld->type, 0.25));
2043
2044 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
2045 for(chan = 0; chan < 3; ++chan)
2046 texel[chan] = res;
2047 texel[3] = texel_bld->one;
2048 }
2049
2050
2051 /**
2052 * Just set texels to white instead of actually sampling the texture.
2053 * For debugging.
2054 */
2055 static void
2056 lp_build_sample_nop(struct lp_build_sample_context *bld,
2057 LLVMValueRef *texel)
2058 {
2059 struct lp_build_context *texel_bld = &bld->texel_bld;
2060 unsigned chan;
2061
2062 for (chan = 0; chan < 4; chan++) {
2063 /*lp_bld_mov(texel_bld, texel, texel_bld->one);*/
2064 texel[chan] = texel_bld->one;
2065 }
2066 }
2067
2068
2069 /**
2070 * Build texture sampling code.
2071 * 'texel' will return a vector of four LLVMValueRefs corresponding to
2072 * R, G, B, A.
2073 * \param type vector float type to use for coords, etc.
2074 */
2075 void
2076 lp_build_sample_soa(LLVMBuilderRef builder,
2077 const struct lp_sampler_static_state *static_state,
2078 struct lp_sampler_dynamic_state *dynamic_state,
2079 struct lp_type type,
2080 unsigned unit,
2081 unsigned num_coords,
2082 const LLVMValueRef *coords,
2083 LLVMValueRef lodbias,
2084 LLVMValueRef *texel)
2085 {
2086 struct lp_build_sample_context bld;
2087 LLVMValueRef width, width_vec;
2088 LLVMValueRef height, height_vec;
2089 LLVMValueRef depth, depth_vec;
2090 LLVMValueRef row_stride_array, img_stride_array;
2091 LLVMValueRef data_array;
2092 LLVMValueRef s;
2093 LLVMValueRef t;
2094 LLVMValueRef r;
2095
2096 /* Setup our build context */
2097 memset(&bld, 0, sizeof bld);
2098 bld.builder = builder;
2099 bld.static_state = static_state;
2100 bld.dynamic_state = dynamic_state;
2101 bld.format_desc = util_format_description(static_state->format);
2102
2103 bld.float_type = lp_type_float(32);
2104 bld.int_type = lp_type_int(32);
2105 bld.coord_type = type;
2106 bld.uint_coord_type = lp_uint_type(type);
2107 bld.int_coord_type = lp_int_type(type);
2108 bld.texel_type = type;
2109
2110 lp_build_context_init(&bld.float_bld, builder, bld.float_type);
2111 lp_build_context_init(&bld.int_bld, builder, bld.int_type);
2112 lp_build_context_init(&bld.coord_bld, builder, bld.coord_type);
2113 lp_build_context_init(&bld.uint_coord_bld, builder, bld.uint_coord_type);
2114 lp_build_context_init(&bld.int_coord_bld, builder, bld.int_coord_type);
2115 lp_build_context_init(&bld.texel_bld, builder, bld.texel_type);
2116
2117 /* Get the dynamic state */
2118 width = dynamic_state->width(dynamic_state, builder, unit);
2119 height = dynamic_state->height(dynamic_state, builder, unit);
2120 depth = dynamic_state->depth(dynamic_state, builder, unit);
2121 row_stride_array = dynamic_state->row_stride(dynamic_state, builder, unit);
2122 img_stride_array = dynamic_state->img_stride(dynamic_state, builder, unit);
2123 data_array = dynamic_state->data_ptr(dynamic_state, builder, unit);
2124 /* Note that data_array is an array[level] of pointers to texture images */
2125
2126 s = coords[0];
2127 t = coords[1];
2128 r = coords[2];
2129
2130 width_vec = lp_build_broadcast_scalar(&bld.uint_coord_bld, width);
2131 height_vec = lp_build_broadcast_scalar(&bld.uint_coord_bld, height);
2132 depth_vec = lp_build_broadcast_scalar(&bld.uint_coord_bld, depth);
2133
2134 if (0) {
2135 /* For debug: no-op texture sampling */
2136 lp_build_sample_nop(&bld, texel);
2137 }
2138 else if (util_format_is_rgba8_variant(bld.format_desc) &&
2139 static_state->target == PIPE_TEXTURE_2D &&
2140 static_state->min_img_filter == PIPE_TEX_FILTER_LINEAR &&
2141 static_state->mag_img_filter == PIPE_TEX_FILTER_LINEAR &&
2142 static_state->min_mip_filter == PIPE_TEX_MIPFILTER_NONE &&
2143 is_simple_wrap_mode(static_state->wrap_s) &&
2144 is_simple_wrap_mode(static_state->wrap_t)) {
2145 /* special case */
2146 lp_build_sample_2d_linear_aos(&bld, s, t, width_vec, height_vec,
2147 row_stride_array, data_array, texel);
2148 }
2149 else {
2150 lp_build_sample_general(&bld, unit, s, t, r, lodbias,
2151 width, height, depth,
2152 width_vec, height_vec, depth_vec,
2153 row_stride_array, img_stride_array,
2154 data_array,
2155 texel);
2156 }
2157
2158 lp_build_sample_compare(&bld, r, texel);
2159 }