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