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