df8453c473485f9857e9979046b1063687e42796
[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_out[4])
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_out);
315
316 apply_sampler_swizzle(bld, texel_out);
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_out[chan] = lp_build_select(&bld->texel_bld, use_border,
341 border_chan, texel_out[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 length_f = lp_build_int_to_float(coord_bld, length);
712 LLVMValueRef length_minus_one = lp_build_sub(uint_coord_bld, length, uint_coord_bld->one);
713 LLVMValueRef length_f_minus_one = lp_build_sub(coord_bld, length_f, coord_bld->one);
714 LLVMValueRef icoord;
715
716 switch(wrap_mode) {
717 case PIPE_TEX_WRAP_REPEAT:
718 coord = lp_build_mul(coord_bld, coord, length_f);
719 icoord = lp_build_ifloor(coord_bld, coord);
720 if (is_pot)
721 icoord = LLVMBuildAnd(bld->builder, icoord, length_minus_one, "");
722 else
723 /* Signed remainder won't give the right results for negative
724 * dividends but unsigned remainder does.*/
725 icoord = LLVMBuildURem(bld->builder, icoord, length, "");
726 break;
727
728 case PIPE_TEX_WRAP_CLAMP:
729 /* mul by size */
730 if (bld->static_state->normalized_coords) {
731 coord = lp_build_mul(coord_bld, coord, length_f);
732 }
733 /* floor */
734 icoord = lp_build_ifloor(coord_bld, coord);
735 /* clamp to [0, size-1]. Note: int coord builder type */
736 icoord = lp_build_clamp(int_coord_bld, icoord, int_coord_bld->zero,
737 length_minus_one);
738 break;
739
740 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
741 {
742 LLVMValueRef min, max;
743
744 if (bld->static_state->normalized_coords) {
745 /* scale coord to length */
746 coord = lp_build_mul(coord_bld, coord, length_f);
747 }
748
749 /* clamp to [0.5, length - 0.5] */
750 min = lp_build_const_vec(coord_bld->type, 0.5F);
751 max = lp_build_sub(coord_bld, length_f, min);
752 coord = lp_build_clamp(coord_bld, coord, min, max);
753
754 icoord = lp_build_ifloor(coord_bld, coord);
755 }
756 break;
757
758 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
759 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
760 {
761 LLVMValueRef min, max;
762
763 if (bld->static_state->normalized_coords) {
764 /* scale coord to length */
765 coord = lp_build_mul(coord_bld, coord, length_f);
766 }
767
768 /* clamp to [-0.5, length + 0.5] */
769 min = lp_build_const_vec(coord_bld->type, -0.5F);
770 max = lp_build_sub(coord_bld, length_f, min);
771 coord = lp_build_clamp(coord_bld, coord, min, max);
772
773 icoord = lp_build_ifloor(coord_bld, coord);
774 }
775 break;
776
777 case PIPE_TEX_WRAP_MIRROR_REPEAT:
778 {
779 LLVMValueRef min, max;
780
781 /* compute mirror function */
782 coord = lp_build_coord_mirror(bld, coord);
783
784 /* scale coord to length */
785 assert(bld->static_state->normalized_coords);
786 coord = lp_build_mul(coord_bld, coord, length_f);
787
788 /* clamp to [0.5, length - 0.5] */
789 min = lp_build_const_vec(coord_bld->type, 0.5F);
790 max = lp_build_sub(coord_bld, length_f, min);
791 coord = lp_build_clamp(coord_bld, coord, min, max);
792
793 icoord = lp_build_ifloor(coord_bld, coord);
794 }
795 break;
796
797 case PIPE_TEX_WRAP_MIRROR_CLAMP:
798 coord = lp_build_abs(coord_bld, coord);
799
800 /* scale coord to length */
801 assert(bld->static_state->normalized_coords);
802 coord = lp_build_mul(coord_bld, coord, length_f);
803
804 /* clamp to [0, length - 1] */
805 coord = lp_build_min(coord_bld, coord, length_f_minus_one);
806
807 icoord = lp_build_ifloor(coord_bld, coord);
808 break;
809
810 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
811 {
812 LLVMValueRef min, max;
813
814 coord = lp_build_abs(coord_bld, coord);
815
816 if (bld->static_state->normalized_coords) {
817 /* scale coord to length */
818 coord = lp_build_mul(coord_bld, coord, length_f);
819 }
820
821 /* clamp to [0.5, length - 0.5] */
822 min = lp_build_const_vec(coord_bld->type, 0.5F);
823 max = lp_build_sub(coord_bld, length_f, min);
824 coord = lp_build_clamp(coord_bld, coord, min, max);
825
826 icoord = lp_build_ifloor(coord_bld, coord);
827 }
828 break;
829
830 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
831 {
832 LLVMValueRef max;
833
834 coord = lp_build_abs(coord_bld, coord);
835
836 if (bld->static_state->normalized_coords) {
837 /* scale coord to length */
838 coord = lp_build_mul(coord_bld, coord, length_f);
839 }
840
841 /* clamp to [-0.5, length + 0.5] */
842 max = lp_build_const_vec(coord_bld->type, 0.5F);
843 max = lp_build_add(coord_bld, length_f, max);
844 coord = lp_build_min(coord_bld, coord, max);
845
846 icoord = lp_build_ifloor(coord_bld, coord);
847 }
848 break;
849
850 default:
851 assert(0);
852 icoord = NULL;
853 }
854
855 return icoord;
856 }
857
858
859 /**
860 * Codegen equivalent for u_minify().
861 * Return max(1, base_size >> level);
862 */
863 static LLVMValueRef
864 lp_build_minify(struct lp_build_sample_context *bld,
865 LLVMValueRef base_size,
866 LLVMValueRef level)
867 {
868 LLVMValueRef size = LLVMBuildAShr(bld->builder, base_size, level, "minify");
869 size = lp_build_max(&bld->int_coord_bld, size, bld->int_coord_bld.one);
870 return size;
871 }
872
873
874 /**
875 * Generate code to compute texture level of detail (lambda).
876 * \param s vector of texcoord s values
877 * \param t vector of texcoord t values
878 * \param r vector of texcoord r values
879 * \param lod_bias optional float vector with the shader lod bias
880 * \param explicit_lod optional float vector with the explicit lod
881 * \param width scalar int texture width
882 * \param height scalar int texture height
883 * \param depth scalar int texture depth
884 *
885 * XXX: The resulting lod is scalar, so ignore all but the first element of
886 * derivatives, lod_bias, etc that are passed by the shader.
887 */
888 static LLVMValueRef
889 lp_build_lod_selector(struct lp_build_sample_context *bld,
890 LLVMValueRef s,
891 LLVMValueRef t,
892 LLVMValueRef r,
893 const LLVMValueRef *ddx,
894 const LLVMValueRef *ddy,
895 LLVMValueRef lod_bias, /* optional */
896 LLVMValueRef explicit_lod, /* optional */
897 LLVMValueRef width,
898 LLVMValueRef height,
899 LLVMValueRef depth)
900
901 {
902 if (bld->static_state->min_lod == bld->static_state->max_lod) {
903 /* User is forcing sampling from a particular mipmap level.
904 * This is hit during mipmap generation.
905 */
906 return LLVMConstReal(LLVMFloatType(), bld->static_state->min_lod);
907 }
908 else {
909 struct lp_build_context *float_bld = &bld->float_bld;
910 LLVMValueRef sampler_lod_bias = LLVMConstReal(LLVMFloatType(),
911 bld->static_state->lod_bias);
912 LLVMValueRef min_lod = LLVMConstReal(LLVMFloatType(),
913 bld->static_state->min_lod);
914 LLVMValueRef max_lod = LLVMConstReal(LLVMFloatType(),
915 bld->static_state->max_lod);
916 LLVMValueRef index0 = LLVMConstInt(LLVMInt32Type(), 0, 0);
917 LLVMValueRef lod;
918
919 if (explicit_lod) {
920 lod = LLVMBuildExtractElement(bld->builder, explicit_lod,
921 index0, "");
922 }
923 else {
924 const int dims = texture_dims(bld->static_state->target);
925 LLVMValueRef dsdx, dsdy;
926 LLVMValueRef dtdx = NULL, dtdy = NULL, drdx = NULL, drdy = NULL;
927 LLVMValueRef rho;
928
929 /*
930 * dsdx = abs(s[1] - s[0]);
931 * dsdy = abs(s[2] - s[0]);
932 * dtdx = abs(t[1] - t[0]);
933 * dtdy = abs(t[2] - t[0]);
934 * drdx = abs(r[1] - r[0]);
935 * drdy = abs(r[2] - r[0]);
936 */
937 dsdx = LLVMBuildExtractElement(bld->builder, ddx[0], index0, "dsdx");
938 dsdx = lp_build_abs(float_bld, dsdx);
939 dsdy = LLVMBuildExtractElement(bld->builder, ddy[0], index0, "dsdy");
940 dsdy = lp_build_abs(float_bld, dsdy);
941 if (dims > 1) {
942 dtdx = LLVMBuildExtractElement(bld->builder, ddx[1], index0, "dtdx");
943 dtdx = lp_build_abs(float_bld, dtdx);
944 dtdy = LLVMBuildExtractElement(bld->builder, ddy[1], index0, "dtdy");
945 dtdy = lp_build_abs(float_bld, dtdy);
946 if (dims > 2) {
947 drdx = LLVMBuildExtractElement(bld->builder, ddx[2], index0, "drdx");
948 drdx = lp_build_abs(float_bld, drdx);
949 drdy = LLVMBuildExtractElement(bld->builder, ddy[2], index0, "drdy");
950 drdy = lp_build_abs(float_bld, drdy);
951 }
952 }
953
954 /* Compute rho = max of all partial derivatives scaled by texture size.
955 * XXX this could be vectorized somewhat
956 */
957 rho = LLVMBuildMul(bld->builder,
958 lp_build_max(float_bld, dsdx, dsdy),
959 lp_build_int_to_float(float_bld, width), "");
960 if (dims > 1) {
961 LLVMValueRef max;
962 max = LLVMBuildMul(bld->builder,
963 lp_build_max(float_bld, dtdx, dtdy),
964 lp_build_int_to_float(float_bld, height), "");
965 rho = lp_build_max(float_bld, rho, max);
966 if (dims > 2) {
967 max = LLVMBuildMul(bld->builder,
968 lp_build_max(float_bld, drdx, drdy),
969 lp_build_int_to_float(float_bld, depth), "");
970 rho = lp_build_max(float_bld, rho, max);
971 }
972 }
973
974 /* compute lod = log2(rho) */
975 lod = lp_build_log2(float_bld, rho);
976
977 /* add shader lod bias */
978 if (lod_bias) {
979 lod_bias = LLVMBuildExtractElement(bld->builder, lod_bias,
980 index0, "");
981 lod = LLVMBuildAdd(bld->builder, lod, lod_bias, "shader_lod_bias");
982 }
983 }
984
985 /* add sampler lod bias */
986 lod = LLVMBuildAdd(bld->builder, lod, sampler_lod_bias, "sampler_lod_bias");
987
988 /* clamp lod */
989 lod = lp_build_clamp(float_bld, lod, min_lod, max_lod);
990
991 return lod;
992 }
993 }
994
995
996 /**
997 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
998 * mipmap level index.
999 * Note: this is all scalar code.
1000 * \param lod scalar float texture level of detail
1001 * \param level_out returns integer
1002 */
1003 static void
1004 lp_build_nearest_mip_level(struct lp_build_sample_context *bld,
1005 unsigned unit,
1006 LLVMValueRef lod,
1007 LLVMValueRef *level_out)
1008 {
1009 struct lp_build_context *float_bld = &bld->float_bld;
1010 struct lp_build_context *int_bld = &bld->int_bld;
1011 LLVMValueRef last_level, level;
1012
1013 LLVMValueRef zero = LLVMConstInt(LLVMInt32Type(), 0, 0);
1014
1015 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
1016 bld->builder, unit);
1017
1018 /* convert float lod to integer */
1019 level = lp_build_iround(float_bld, lod);
1020
1021 /* clamp level to legal range of levels */
1022 *level_out = lp_build_clamp(int_bld, level, zero, last_level);
1023 }
1024
1025
1026 /**
1027 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
1028 * two (adjacent) mipmap level indexes. Later, we'll sample from those
1029 * two mipmap levels and interpolate between them.
1030 */
1031 static void
1032 lp_build_linear_mip_levels(struct lp_build_sample_context *bld,
1033 unsigned unit,
1034 LLVMValueRef lod,
1035 LLVMValueRef *level0_out,
1036 LLVMValueRef *level1_out,
1037 LLVMValueRef *weight_out)
1038 {
1039 struct lp_build_context *float_bld = &bld->float_bld;
1040 struct lp_build_context *int_bld = &bld->int_bld;
1041 LLVMValueRef last_level, level;
1042
1043 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
1044 bld->builder, unit);
1045
1046 /* convert float lod to integer */
1047 level = lp_build_ifloor(float_bld, lod);
1048
1049 /* compute level 0 and clamp to legal range of levels */
1050 *level0_out = lp_build_clamp(int_bld, level,
1051 int_bld->zero,
1052 last_level);
1053 /* compute level 1 and clamp to legal range of levels */
1054 level = lp_build_add(int_bld, level, int_bld->one);
1055 *level1_out = lp_build_clamp(int_bld, level,
1056 int_bld->zero,
1057 last_level);
1058
1059 *weight_out = lp_build_fract(float_bld, lod);
1060 }
1061
1062
1063 /**
1064 * Generate code to sample a mipmap level with nearest filtering.
1065 * If sampling a cube texture, r = cube face in [0,5].
1066 */
1067 static void
1068 lp_build_sample_image_nearest(struct lp_build_sample_context *bld,
1069 LLVMValueRef width_vec,
1070 LLVMValueRef height_vec,
1071 LLVMValueRef depth_vec,
1072 LLVMValueRef row_stride_vec,
1073 LLVMValueRef img_stride_vec,
1074 LLVMValueRef data_ptr,
1075 LLVMValueRef s,
1076 LLVMValueRef t,
1077 LLVMValueRef r,
1078 LLVMValueRef colors_out[4])
1079 {
1080 const int dims = texture_dims(bld->static_state->target);
1081 LLVMValueRef x, y, z;
1082
1083 /*
1084 * Compute integer texcoords.
1085 */
1086 x = lp_build_sample_wrap_nearest(bld, s, width_vec,
1087 bld->static_state->pot_width,
1088 bld->static_state->wrap_s);
1089 lp_build_name(x, "tex.x.wrapped");
1090
1091 if (dims >= 2) {
1092 y = lp_build_sample_wrap_nearest(bld, t, height_vec,
1093 bld->static_state->pot_height,
1094 bld->static_state->wrap_t);
1095 lp_build_name(y, "tex.y.wrapped");
1096
1097 if (dims == 3) {
1098 z = lp_build_sample_wrap_nearest(bld, r, depth_vec,
1099 bld->static_state->pot_height,
1100 bld->static_state->wrap_r);
1101 lp_build_name(z, "tex.z.wrapped");
1102 }
1103 else if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
1104 z = r;
1105 }
1106 else {
1107 z = NULL;
1108 }
1109 }
1110 else {
1111 y = z = NULL;
1112 }
1113
1114 /*
1115 * Get texture colors.
1116 */
1117 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1118 x, y, z,
1119 row_stride_vec, img_stride_vec,
1120 data_ptr, colors_out);
1121 }
1122
1123
1124 /**
1125 * Generate code to sample a mipmap level with linear filtering.
1126 * If sampling a cube texture, r = cube face in [0,5].
1127 */
1128 static void
1129 lp_build_sample_image_linear(struct lp_build_sample_context *bld,
1130 LLVMValueRef width_vec,
1131 LLVMValueRef height_vec,
1132 LLVMValueRef depth_vec,
1133 LLVMValueRef row_stride_vec,
1134 LLVMValueRef img_stride_vec,
1135 LLVMValueRef data_ptr,
1136 LLVMValueRef s,
1137 LLVMValueRef t,
1138 LLVMValueRef r,
1139 LLVMValueRef colors_out[4])
1140 {
1141 const int dims = texture_dims(bld->static_state->target);
1142 LLVMValueRef x0, y0, z0, x1, y1, z1;
1143 LLVMValueRef s_fpart, t_fpart, r_fpart;
1144 LLVMValueRef neighbors[2][2][4];
1145 int chan;
1146
1147 /*
1148 * Compute integer texcoords.
1149 */
1150 lp_build_sample_wrap_linear(bld, s, width_vec,
1151 bld->static_state->pot_width,
1152 bld->static_state->wrap_s,
1153 &x0, &x1, &s_fpart);
1154 lp_build_name(x0, "tex.x0.wrapped");
1155 lp_build_name(x1, "tex.x1.wrapped");
1156
1157 if (dims >= 2) {
1158 lp_build_sample_wrap_linear(bld, t, height_vec,
1159 bld->static_state->pot_height,
1160 bld->static_state->wrap_t,
1161 &y0, &y1, &t_fpart);
1162 lp_build_name(y0, "tex.y0.wrapped");
1163 lp_build_name(y1, "tex.y1.wrapped");
1164
1165 if (dims == 3) {
1166 lp_build_sample_wrap_linear(bld, r, depth_vec,
1167 bld->static_state->pot_depth,
1168 bld->static_state->wrap_r,
1169 &z0, &z1, &r_fpart);
1170 lp_build_name(z0, "tex.z0.wrapped");
1171 lp_build_name(z1, "tex.z1.wrapped");
1172 }
1173 else if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
1174 z0 = z1 = r; /* cube face */
1175 r_fpart = NULL;
1176 }
1177 else {
1178 z0 = z1 = NULL;
1179 r_fpart = NULL;
1180 }
1181 }
1182 else {
1183 y0 = y1 = t_fpart = NULL;
1184 z0 = z1 = r_fpart = NULL;
1185 }
1186
1187 /*
1188 * Get texture colors.
1189 */
1190 /* get x0/x1 texels */
1191 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1192 x0, y0, z0,
1193 row_stride_vec, img_stride_vec,
1194 data_ptr, neighbors[0][0]);
1195 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1196 x1, y0, z0,
1197 row_stride_vec, img_stride_vec,
1198 data_ptr, neighbors[0][1]);
1199
1200 if (dims == 1) {
1201 /* Interpolate two samples from 1D image to produce one color */
1202 for (chan = 0; chan < 4; chan++) {
1203 colors_out[chan] = lp_build_lerp(&bld->texel_bld, s_fpart,
1204 neighbors[0][0][chan],
1205 neighbors[0][1][chan]);
1206 }
1207 }
1208 else {
1209 /* 2D/3D texture */
1210 LLVMValueRef colors0[4];
1211
1212 /* get x0/x1 texels at y1 */
1213 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1214 x0, y1, z0,
1215 row_stride_vec, img_stride_vec,
1216 data_ptr, neighbors[1][0]);
1217 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1218 x1, y1, z0,
1219 row_stride_vec, img_stride_vec,
1220 data_ptr, neighbors[1][1]);
1221
1222 /* Bilinear interpolate the four samples from the 2D image / 3D slice */
1223 for (chan = 0; chan < 4; chan++) {
1224 colors0[chan] = lp_build_lerp_2d(&bld->texel_bld,
1225 s_fpart, t_fpart,
1226 neighbors[0][0][chan],
1227 neighbors[0][1][chan],
1228 neighbors[1][0][chan],
1229 neighbors[1][1][chan]);
1230 }
1231
1232 if (dims == 3) {
1233 LLVMValueRef neighbors1[2][2][4];
1234 LLVMValueRef colors1[4];
1235
1236 /* get x0/x1/y0/y1 texels at z1 */
1237 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1238 x0, y0, z1,
1239 row_stride_vec, img_stride_vec,
1240 data_ptr, neighbors1[0][0]);
1241 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1242 x1, y0, z1,
1243 row_stride_vec, img_stride_vec,
1244 data_ptr, neighbors1[0][1]);
1245 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1246 x0, y1, z1,
1247 row_stride_vec, img_stride_vec,
1248 data_ptr, neighbors1[1][0]);
1249 lp_build_sample_texel_soa(bld, width_vec, height_vec, depth_vec,
1250 x1, y1, z1,
1251 row_stride_vec, img_stride_vec,
1252 data_ptr, neighbors1[1][1]);
1253
1254 /* Bilinear interpolate the four samples from the second Z slice */
1255 for (chan = 0; chan < 4; chan++) {
1256 colors1[chan] = lp_build_lerp_2d(&bld->texel_bld,
1257 s_fpart, t_fpart,
1258 neighbors1[0][0][chan],
1259 neighbors1[0][1][chan],
1260 neighbors1[1][0][chan],
1261 neighbors1[1][1][chan]);
1262 }
1263
1264 /* Linearly interpolate the two samples from the two 3D slices */
1265 for (chan = 0; chan < 4; chan++) {
1266 colors_out[chan] = lp_build_lerp(&bld->texel_bld,
1267 r_fpart,
1268 colors0[chan], colors1[chan]);
1269 }
1270 }
1271 else {
1272 /* 2D tex */
1273 for (chan = 0; chan < 4; chan++) {
1274 colors_out[chan] = colors0[chan];
1275 }
1276 }
1277 }
1278 }
1279
1280
1281 /** Helper used by lp_build_cube_lookup() */
1282 static LLVMValueRef
1283 lp_build_cube_ima(struct lp_build_context *coord_bld, LLVMValueRef coord)
1284 {
1285 /* ima = -0.5 / abs(coord); */
1286 LLVMValueRef negHalf = lp_build_const_vec(coord_bld->type, -0.5);
1287 LLVMValueRef absCoord = lp_build_abs(coord_bld, coord);
1288 LLVMValueRef ima = lp_build_mul(coord_bld, negHalf,
1289 lp_build_rcp(coord_bld, absCoord));
1290 return ima;
1291 }
1292
1293
1294 /**
1295 * Helper used by lp_build_cube_lookup()
1296 * \param sign scalar +1 or -1
1297 * \param coord float vector
1298 * \param ima float vector
1299 */
1300 static LLVMValueRef
1301 lp_build_cube_coord(struct lp_build_context *coord_bld,
1302 LLVMValueRef sign, int negate_coord,
1303 LLVMValueRef coord, LLVMValueRef ima)
1304 {
1305 /* return negate(coord) * ima * sign + 0.5; */
1306 LLVMValueRef half = lp_build_const_vec(coord_bld->type, 0.5);
1307 LLVMValueRef res;
1308
1309 assert(negate_coord == +1 || negate_coord == -1);
1310
1311 if (negate_coord == -1) {
1312 coord = lp_build_negate(coord_bld, coord);
1313 }
1314
1315 res = lp_build_mul(coord_bld, coord, ima);
1316 if (sign) {
1317 sign = lp_build_broadcast_scalar(coord_bld, sign);
1318 res = lp_build_mul(coord_bld, res, sign);
1319 }
1320 res = lp_build_add(coord_bld, res, half);
1321
1322 return res;
1323 }
1324
1325
1326 /** Helper used by lp_build_cube_lookup()
1327 * Return (major_coord >= 0) ? pos_face : neg_face;
1328 */
1329 static LLVMValueRef
1330 lp_build_cube_face(struct lp_build_sample_context *bld,
1331 LLVMValueRef major_coord,
1332 unsigned pos_face, unsigned neg_face)
1333 {
1334 LLVMValueRef cmp = LLVMBuildFCmp(bld->builder, LLVMRealUGE,
1335 major_coord,
1336 bld->float_bld.zero, "");
1337 LLVMValueRef pos = LLVMConstInt(LLVMInt32Type(), pos_face, 0);
1338 LLVMValueRef neg = LLVMConstInt(LLVMInt32Type(), neg_face, 0);
1339 LLVMValueRef res = LLVMBuildSelect(bld->builder, cmp, pos, neg, "");
1340 return res;
1341 }
1342
1343
1344
1345 /**
1346 * Generate code to do cube face selection and per-face texcoords.
1347 */
1348 static void
1349 lp_build_cube_lookup(struct lp_build_sample_context *bld,
1350 LLVMValueRef s,
1351 LLVMValueRef t,
1352 LLVMValueRef r,
1353 LLVMValueRef *face,
1354 LLVMValueRef *face_s,
1355 LLVMValueRef *face_t)
1356 {
1357 struct lp_build_context *float_bld = &bld->float_bld;
1358 struct lp_build_context *coord_bld = &bld->coord_bld;
1359 LLVMValueRef rx, ry, rz;
1360 LLVMValueRef arx, ary, arz;
1361 LLVMValueRef c25 = LLVMConstReal(LLVMFloatType(), 0.25);
1362 LLVMValueRef arx_ge_ary, arx_ge_arz;
1363 LLVMValueRef ary_ge_arx, ary_ge_arz;
1364 LLVMValueRef arx_ge_ary_arz, ary_ge_arx_arz;
1365 LLVMValueRef rx_pos, ry_pos, rz_pos;
1366
1367 assert(bld->coord_bld.type.length == 4);
1368
1369 /*
1370 * Use the average of the four pixel's texcoords to choose the face.
1371 */
1372 rx = lp_build_mul(float_bld, c25,
1373 lp_build_sum_vector(&bld->coord_bld, s));
1374 ry = lp_build_mul(float_bld, c25,
1375 lp_build_sum_vector(&bld->coord_bld, t));
1376 rz = lp_build_mul(float_bld, c25,
1377 lp_build_sum_vector(&bld->coord_bld, r));
1378
1379 arx = lp_build_abs(float_bld, rx);
1380 ary = lp_build_abs(float_bld, ry);
1381 arz = lp_build_abs(float_bld, rz);
1382
1383 /*
1384 * Compare sign/magnitude of rx,ry,rz to determine face
1385 */
1386 arx_ge_ary = LLVMBuildFCmp(bld->builder, LLVMRealUGE, arx, ary, "");
1387 arx_ge_arz = LLVMBuildFCmp(bld->builder, LLVMRealUGE, arx, arz, "");
1388 ary_ge_arx = LLVMBuildFCmp(bld->builder, LLVMRealUGE, ary, arx, "");
1389 ary_ge_arz = LLVMBuildFCmp(bld->builder, LLVMRealUGE, ary, arz, "");
1390
1391 arx_ge_ary_arz = LLVMBuildAnd(bld->builder, arx_ge_ary, arx_ge_arz, "");
1392 ary_ge_arx_arz = LLVMBuildAnd(bld->builder, ary_ge_arx, ary_ge_arz, "");
1393
1394 rx_pos = LLVMBuildFCmp(bld->builder, LLVMRealUGE, rx, float_bld->zero, "");
1395 ry_pos = LLVMBuildFCmp(bld->builder, LLVMRealUGE, ry, float_bld->zero, "");
1396 rz_pos = LLVMBuildFCmp(bld->builder, LLVMRealUGE, rz, float_bld->zero, "");
1397
1398 {
1399 struct lp_build_flow_context *flow_ctx;
1400 struct lp_build_if_state if_ctx;
1401
1402 flow_ctx = lp_build_flow_create(bld->builder);
1403 lp_build_flow_scope_begin(flow_ctx);
1404
1405 *face_s = bld->coord_bld.undef;
1406 *face_t = bld->coord_bld.undef;
1407 *face = bld->int_bld.undef;
1408
1409 lp_build_name(*face_s, "face_s");
1410 lp_build_name(*face_t, "face_t");
1411 lp_build_name(*face, "face");
1412
1413 lp_build_flow_scope_declare(flow_ctx, face_s);
1414 lp_build_flow_scope_declare(flow_ctx, face_t);
1415 lp_build_flow_scope_declare(flow_ctx, face);
1416
1417 lp_build_if(&if_ctx, flow_ctx, bld->builder, arx_ge_ary_arz);
1418 {
1419 /* +/- X face */
1420 LLVMValueRef sign = lp_build_sgn(float_bld, rx);
1421 LLVMValueRef ima = lp_build_cube_ima(coord_bld, s);
1422 *face_s = lp_build_cube_coord(coord_bld, sign, +1, r, ima);
1423 *face_t = lp_build_cube_coord(coord_bld, NULL, +1, t, ima);
1424 *face = lp_build_cube_face(bld, rx,
1425 PIPE_TEX_FACE_POS_X,
1426 PIPE_TEX_FACE_NEG_X);
1427 }
1428 lp_build_else(&if_ctx);
1429 {
1430 struct lp_build_flow_context *flow_ctx2;
1431 struct lp_build_if_state if_ctx2;
1432
1433 LLVMValueRef face_s2 = bld->coord_bld.undef;
1434 LLVMValueRef face_t2 = bld->coord_bld.undef;
1435 LLVMValueRef face2 = bld->int_bld.undef;
1436
1437 flow_ctx2 = lp_build_flow_create(bld->builder);
1438 lp_build_flow_scope_begin(flow_ctx2);
1439 lp_build_flow_scope_declare(flow_ctx2, &face_s2);
1440 lp_build_flow_scope_declare(flow_ctx2, &face_t2);
1441 lp_build_flow_scope_declare(flow_ctx2, &face2);
1442
1443 ary_ge_arx_arz = LLVMBuildAnd(bld->builder, ary_ge_arx, ary_ge_arz, "");
1444
1445 lp_build_if(&if_ctx2, flow_ctx2, bld->builder, ary_ge_arx_arz);
1446 {
1447 /* +/- Y face */
1448 LLVMValueRef sign = lp_build_sgn(float_bld, ry);
1449 LLVMValueRef ima = lp_build_cube_ima(coord_bld, t);
1450 face_s2 = lp_build_cube_coord(coord_bld, NULL, -1, s, ima);
1451 face_t2 = lp_build_cube_coord(coord_bld, sign, -1, r, ima);
1452 face2 = lp_build_cube_face(bld, ry,
1453 PIPE_TEX_FACE_POS_Y,
1454 PIPE_TEX_FACE_NEG_Y);
1455 }
1456 lp_build_else(&if_ctx2);
1457 {
1458 /* +/- Z face */
1459 LLVMValueRef sign = lp_build_sgn(float_bld, rz);
1460 LLVMValueRef ima = lp_build_cube_ima(coord_bld, r);
1461 face_s2 = lp_build_cube_coord(coord_bld, sign, -1, s, ima);
1462 face_t2 = lp_build_cube_coord(coord_bld, NULL, +1, t, ima);
1463 face2 = lp_build_cube_face(bld, rz,
1464 PIPE_TEX_FACE_POS_Z,
1465 PIPE_TEX_FACE_NEG_Z);
1466 }
1467 lp_build_endif(&if_ctx2);
1468 lp_build_flow_scope_end(flow_ctx2);
1469 lp_build_flow_destroy(flow_ctx2);
1470
1471 *face_s = face_s2;
1472 *face_t = face_t2;
1473 *face = face2;
1474 }
1475
1476 lp_build_endif(&if_ctx);
1477 lp_build_flow_scope_end(flow_ctx);
1478 lp_build_flow_destroy(flow_ctx);
1479 }
1480 }
1481
1482
1483
1484 /**
1485 * Sample the texture/mipmap using given image filter and mip filter.
1486 * data0_ptr and data1_ptr point to the two mipmap levels to sample
1487 * from. width0/1_vec, height0/1_vec, depth0/1_vec indicate their sizes.
1488 * If we're using nearest miplevel sampling the '1' values will be null/unused.
1489 */
1490 static void
1491 lp_build_sample_mipmap(struct lp_build_sample_context *bld,
1492 unsigned img_filter,
1493 unsigned mip_filter,
1494 LLVMValueRef s,
1495 LLVMValueRef t,
1496 LLVMValueRef r,
1497 LLVMValueRef lod_fpart,
1498 LLVMValueRef width0_vec,
1499 LLVMValueRef width1_vec,
1500 LLVMValueRef height0_vec,
1501 LLVMValueRef height1_vec,
1502 LLVMValueRef depth0_vec,
1503 LLVMValueRef depth1_vec,
1504 LLVMValueRef row_stride0_vec,
1505 LLVMValueRef row_stride1_vec,
1506 LLVMValueRef img_stride0_vec,
1507 LLVMValueRef img_stride1_vec,
1508 LLVMValueRef data_ptr0,
1509 LLVMValueRef data_ptr1,
1510 LLVMValueRef *colors_out)
1511 {
1512 LLVMValueRef colors0[4], colors1[4];
1513 int chan;
1514
1515 if (img_filter == PIPE_TEX_FILTER_NEAREST) {
1516 lp_build_sample_image_nearest(bld,
1517 width0_vec, height0_vec, depth0_vec,
1518 row_stride0_vec, img_stride0_vec,
1519 data_ptr0, s, t, r, colors0);
1520
1521 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1522 /* sample the second mipmap level, and interp */
1523 lp_build_sample_image_nearest(bld,
1524 width1_vec, height1_vec, depth1_vec,
1525 row_stride1_vec, img_stride1_vec,
1526 data_ptr1, s, t, r, colors1);
1527 }
1528 }
1529 else {
1530 assert(img_filter == PIPE_TEX_FILTER_LINEAR);
1531
1532 lp_build_sample_image_linear(bld,
1533 width0_vec, height0_vec, depth0_vec,
1534 row_stride0_vec, img_stride0_vec,
1535 data_ptr0, s, t, r, colors0);
1536
1537 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1538 /* sample the second mipmap level, and interp */
1539 lp_build_sample_image_linear(bld,
1540 width1_vec, height1_vec, depth1_vec,
1541 row_stride1_vec, img_stride1_vec,
1542 data_ptr1, s, t, r, colors1);
1543 }
1544 }
1545
1546 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1547 /* interpolate samples from the two mipmap levels */
1548 for (chan = 0; chan < 4; chan++) {
1549 colors_out[chan] = lp_build_lerp(&bld->texel_bld, lod_fpart,
1550 colors0[chan], colors1[chan]);
1551 }
1552 }
1553 else {
1554 /* use first/only level's colors */
1555 for (chan = 0; chan < 4; chan++) {
1556 colors_out[chan] = colors0[chan];
1557 }
1558 }
1559 }
1560
1561
1562
1563 /**
1564 * General texture sampling codegen.
1565 * This function handles texture sampling for all texture targets (1D,
1566 * 2D, 3D, cube) and all filtering modes.
1567 */
1568 static void
1569 lp_build_sample_general(struct lp_build_sample_context *bld,
1570 unsigned unit,
1571 LLVMValueRef s,
1572 LLVMValueRef t,
1573 LLVMValueRef r,
1574 const LLVMValueRef *ddx,
1575 const LLVMValueRef *ddy,
1576 LLVMValueRef lod_bias, /* optional */
1577 LLVMValueRef explicit_lod, /* optional */
1578 LLVMValueRef width,
1579 LLVMValueRef height,
1580 LLVMValueRef depth,
1581 LLVMValueRef width_vec,
1582 LLVMValueRef height_vec,
1583 LLVMValueRef depth_vec,
1584 LLVMValueRef row_stride_array,
1585 LLVMValueRef img_stride_array,
1586 LLVMValueRef data_array,
1587 LLVMValueRef *colors_out)
1588 {
1589 struct lp_build_context *float_bld = &bld->float_bld;
1590 const unsigned mip_filter = bld->static_state->min_mip_filter;
1591 const unsigned min_filter = bld->static_state->min_img_filter;
1592 const unsigned mag_filter = bld->static_state->mag_img_filter;
1593 const int dims = texture_dims(bld->static_state->target);
1594 LLVMValueRef lod = NULL, lod_fpart = NULL;
1595 LLVMValueRef ilevel0, ilevel1 = NULL, ilevel0_vec, ilevel1_vec = NULL;
1596 LLVMValueRef width0_vec = NULL, height0_vec = NULL, depth0_vec = NULL;
1597 LLVMValueRef width1_vec = NULL, height1_vec = NULL, depth1_vec = NULL;
1598 LLVMValueRef row_stride0_vec = NULL, row_stride1_vec = NULL;
1599 LLVMValueRef img_stride0_vec = NULL, img_stride1_vec = NULL;
1600 LLVMValueRef data_ptr0, data_ptr1 = NULL;
1601
1602 /*
1603 printf("%s mip %d min %d mag %d\n", __FUNCTION__,
1604 mip_filter, min_filter, mag_filter);
1605 */
1606
1607 /*
1608 * Compute the level of detail (float).
1609 */
1610 if (min_filter != mag_filter ||
1611 mip_filter != PIPE_TEX_MIPFILTER_NONE) {
1612 /* Need to compute lod either to choose mipmap levels or to
1613 * distinguish between minification/magnification with one mipmap level.
1614 */
1615 lod = lp_build_lod_selector(bld, s, t, r, ddx, ddy,
1616 lod_bias, explicit_lod,
1617 width, height, depth);
1618 }
1619
1620 /*
1621 * Compute integer mipmap level(s) to fetch texels from.
1622 */
1623 if (mip_filter == PIPE_TEX_MIPFILTER_NONE) {
1624 /* always use mip level 0 */
1625 ilevel0 = LLVMConstInt(LLVMInt32Type(), 0, 0);
1626 }
1627 else {
1628 if (mip_filter == PIPE_TEX_MIPFILTER_NEAREST) {
1629 lp_build_nearest_mip_level(bld, unit, lod, &ilevel0);
1630 }
1631 else {
1632 assert(mip_filter == PIPE_TEX_MIPFILTER_LINEAR);
1633 lp_build_linear_mip_levels(bld, unit, lod, &ilevel0, &ilevel1,
1634 &lod_fpart);
1635 lod_fpart = lp_build_broadcast_scalar(&bld->coord_bld, lod_fpart);
1636 }
1637 }
1638
1639 /*
1640 * Convert scalar integer mipmap levels into vectors.
1641 */
1642 ilevel0_vec = lp_build_broadcast_scalar(&bld->int_coord_bld, ilevel0);
1643 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
1644 ilevel1_vec = lp_build_broadcast_scalar(&bld->int_coord_bld, ilevel1);
1645
1646 /*
1647 * Compute width, height at mipmap level 'ilevel0'
1648 */
1649 width0_vec = lp_build_minify(bld, width_vec, ilevel0_vec);
1650 if (dims >= 2) {
1651 height0_vec = lp_build_minify(bld, height_vec, ilevel0_vec);
1652 row_stride0_vec = lp_build_get_level_stride_vec(bld, row_stride_array,
1653 ilevel0);
1654 if (dims == 3 || bld->static_state->target == PIPE_TEXTURE_CUBE) {
1655 img_stride0_vec = lp_build_get_level_stride_vec(bld,
1656 img_stride_array,
1657 ilevel0);
1658 if (dims == 3) {
1659 depth0_vec = lp_build_minify(bld, depth_vec, ilevel0_vec);
1660 }
1661 }
1662 }
1663 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1664 /* compute width, height, depth for second mipmap level at 'ilevel1' */
1665 width1_vec = lp_build_minify(bld, width_vec, ilevel1_vec);
1666 if (dims >= 2) {
1667 height1_vec = lp_build_minify(bld, height_vec, ilevel1_vec);
1668 row_stride1_vec = lp_build_get_level_stride_vec(bld, row_stride_array,
1669 ilevel1);
1670 if (dims == 3 || bld->static_state->target == PIPE_TEXTURE_CUBE) {
1671 img_stride1_vec = lp_build_get_level_stride_vec(bld,
1672 img_stride_array,
1673 ilevel1);
1674 if (dims ==3) {
1675 depth1_vec = lp_build_minify(bld, depth_vec, ilevel1_vec);
1676 }
1677 }
1678 }
1679 }
1680
1681 /*
1682 * Choose cube face, recompute per-face texcoords.
1683 */
1684 if (bld->static_state->target == PIPE_TEXTURE_CUBE) {
1685 LLVMValueRef face, face_s, face_t;
1686 lp_build_cube_lookup(bld, s, t, r, &face, &face_s, &face_t);
1687 s = face_s; /* vec */
1688 t = face_t; /* vec */
1689 /* use 'r' to indicate cube face */
1690 r = lp_build_broadcast_scalar(&bld->int_coord_bld, face); /* vec */
1691 }
1692
1693 /*
1694 * Get pointer(s) to image data for mipmap level(s).
1695 */
1696 data_ptr0 = lp_build_get_mipmap_level(bld, data_array, ilevel0);
1697 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1698 data_ptr1 = lp_build_get_mipmap_level(bld, data_array, ilevel1);
1699 }
1700
1701 /*
1702 * Get/interpolate texture colors.
1703 */
1704 if (min_filter == mag_filter) {
1705 /* no need to distinquish between minification and magnification */
1706 lp_build_sample_mipmap(bld, min_filter, mip_filter, s, t, r, lod_fpart,
1707 width0_vec, width1_vec,
1708 height0_vec, height1_vec,
1709 depth0_vec, depth1_vec,
1710 row_stride0_vec, row_stride1_vec,
1711 img_stride0_vec, img_stride1_vec,
1712 data_ptr0, data_ptr1,
1713 colors_out);
1714 }
1715 else {
1716 /* Emit conditional to choose min image filter or mag image filter
1717 * depending on the lod being >0 or <= 0, respectively.
1718 */
1719 struct lp_build_flow_context *flow_ctx;
1720 struct lp_build_if_state if_ctx;
1721 LLVMValueRef minify;
1722
1723 flow_ctx = lp_build_flow_create(bld->builder);
1724 lp_build_flow_scope_begin(flow_ctx);
1725
1726 lp_build_flow_scope_declare(flow_ctx, &colors_out[0]);
1727 lp_build_flow_scope_declare(flow_ctx, &colors_out[1]);
1728 lp_build_flow_scope_declare(flow_ctx, &colors_out[2]);
1729 lp_build_flow_scope_declare(flow_ctx, &colors_out[3]);
1730
1731 /* minify = lod > 0.0 */
1732 minify = LLVMBuildFCmp(bld->builder, LLVMRealUGE,
1733 lod, float_bld->zero, "");
1734
1735 lp_build_if(&if_ctx, flow_ctx, bld->builder, minify);
1736 {
1737 /* Use the minification filter */
1738 lp_build_sample_mipmap(bld, min_filter, mip_filter,
1739 s, t, r, lod_fpart,
1740 width0_vec, width1_vec,
1741 height0_vec, height1_vec,
1742 depth0_vec, depth1_vec,
1743 row_stride0_vec, row_stride1_vec,
1744 img_stride0_vec, img_stride1_vec,
1745 data_ptr0, data_ptr1,
1746 colors_out);
1747 }
1748 lp_build_else(&if_ctx);
1749 {
1750 /* Use the magnification filter */
1751 lp_build_sample_mipmap(bld, mag_filter, mip_filter,
1752 s, t, r, lod_fpart,
1753 width0_vec, width1_vec,
1754 height0_vec, height1_vec,
1755 depth0_vec, depth1_vec,
1756 row_stride0_vec, row_stride1_vec,
1757 img_stride0_vec, img_stride1_vec,
1758 data_ptr0, data_ptr1,
1759 colors_out);
1760 }
1761 lp_build_endif(&if_ctx);
1762
1763 lp_build_flow_scope_end(flow_ctx);
1764 lp_build_flow_destroy(flow_ctx);
1765 }
1766 }
1767
1768
1769
1770 static void
1771 lp_build_rgba8_to_f32_soa(LLVMBuilderRef builder,
1772 struct lp_type dst_type,
1773 LLVMValueRef packed,
1774 LLVMValueRef *rgba)
1775 {
1776 LLVMValueRef mask = lp_build_const_int_vec(dst_type, 0xff);
1777 unsigned chan;
1778
1779 /* Decode the input vector components */
1780 for (chan = 0; chan < 4; ++chan) {
1781 unsigned start = chan*8;
1782 unsigned stop = start + 8;
1783 LLVMValueRef input;
1784
1785 input = packed;
1786
1787 if(start)
1788 input = LLVMBuildLShr(builder, input, lp_build_const_int_vec(dst_type, start), "");
1789
1790 if(stop < 32)
1791 input = LLVMBuildAnd(builder, input, mask, "");
1792
1793 input = lp_build_unsigned_norm_to_float(builder, 8, dst_type, input);
1794
1795 rgba[chan] = input;
1796 }
1797 }
1798
1799
1800 static void
1801 lp_build_sample_2d_linear_aos(struct lp_build_sample_context *bld,
1802 LLVMValueRef s,
1803 LLVMValueRef t,
1804 LLVMValueRef width,
1805 LLVMValueRef height,
1806 LLVMValueRef stride_array,
1807 LLVMValueRef data_array,
1808 LLVMValueRef texel_out[4])
1809 {
1810 LLVMBuilderRef builder = bld->builder;
1811 struct lp_build_context i32, h16, u8n;
1812 LLVMTypeRef i32_vec_type, h16_vec_type, u8n_vec_type;
1813 LLVMValueRef i32_c8, i32_c128, i32_c255;
1814 LLVMValueRef s_ipart, s_fpart, s_fpart_lo, s_fpart_hi;
1815 LLVMValueRef t_ipart, t_fpart, t_fpart_lo, t_fpart_hi;
1816 LLVMValueRef x0, x1;
1817 LLVMValueRef y0, y1;
1818 LLVMValueRef neighbors[2][2];
1819 LLVMValueRef neighbors_lo[2][2];
1820 LLVMValueRef neighbors_hi[2][2];
1821 LLVMValueRef packed, packed_lo, packed_hi;
1822 LLVMValueRef unswizzled[4];
1823 LLVMValueRef stride;
1824
1825 assert(bld->static_state->target == PIPE_TEXTURE_2D);
1826 assert(bld->static_state->min_img_filter == PIPE_TEX_FILTER_LINEAR);
1827 assert(bld->static_state->mag_img_filter == PIPE_TEX_FILTER_LINEAR);
1828 assert(bld->static_state->min_mip_filter == PIPE_TEX_MIPFILTER_NONE);
1829
1830 lp_build_context_init(&i32, builder, lp_type_int_vec(32));
1831 lp_build_context_init(&h16, builder, lp_type_ufixed(16));
1832 lp_build_context_init(&u8n, builder, lp_type_unorm(8));
1833
1834 i32_vec_type = lp_build_vec_type(i32.type);
1835 h16_vec_type = lp_build_vec_type(h16.type);
1836 u8n_vec_type = lp_build_vec_type(u8n.type);
1837
1838 if (bld->static_state->normalized_coords) {
1839 LLVMTypeRef coord_vec_type = lp_build_vec_type(bld->coord_type);
1840 LLVMValueRef fp_width = LLVMBuildSIToFP(bld->builder, width, coord_vec_type, "");
1841 LLVMValueRef fp_height = LLVMBuildSIToFP(bld->builder, height, coord_vec_type, "");
1842 s = lp_build_mul(&bld->coord_bld, s, fp_width);
1843 t = lp_build_mul(&bld->coord_bld, t, fp_height);
1844 }
1845
1846 /* scale coords by 256 (8 fractional bits) */
1847 s = lp_build_mul_imm(&bld->coord_bld, s, 256);
1848 t = lp_build_mul_imm(&bld->coord_bld, t, 256);
1849
1850 /* convert float to int */
1851 s = LLVMBuildFPToSI(builder, s, i32_vec_type, "");
1852 t = LLVMBuildFPToSI(builder, t, i32_vec_type, "");
1853
1854 /* subtract 0.5 (add -128) */
1855 i32_c128 = lp_build_const_int_vec(i32.type, -128);
1856 s = LLVMBuildAdd(builder, s, i32_c128, "");
1857 t = LLVMBuildAdd(builder, t, i32_c128, "");
1858
1859 /* compute floor (shift right 8) */
1860 i32_c8 = lp_build_const_int_vec(i32.type, 8);
1861 s_ipart = LLVMBuildAShr(builder, s, i32_c8, "");
1862 t_ipart = LLVMBuildAShr(builder, t, i32_c8, "");
1863
1864 /* compute fractional part (AND with 0xff) */
1865 i32_c255 = lp_build_const_int_vec(i32.type, 255);
1866 s_fpart = LLVMBuildAnd(builder, s, i32_c255, "");
1867 t_fpart = LLVMBuildAnd(builder, t, i32_c255, "");
1868
1869 x0 = s_ipart;
1870 y0 = t_ipart;
1871
1872 x1 = lp_build_add(&bld->int_coord_bld, x0, bld->int_coord_bld.one);
1873 y1 = lp_build_add(&bld->int_coord_bld, y0, bld->int_coord_bld.one);
1874
1875 x0 = lp_build_sample_wrap_int(bld, x0, width, bld->static_state->pot_width,
1876 bld->static_state->wrap_s);
1877 y0 = lp_build_sample_wrap_int(bld, y0, height, bld->static_state->pot_height,
1878 bld->static_state->wrap_t);
1879
1880 x1 = lp_build_sample_wrap_int(bld, x1, width, bld->static_state->pot_width,
1881 bld->static_state->wrap_s);
1882 y1 = lp_build_sample_wrap_int(bld, y1, height, bld->static_state->pot_height,
1883 bld->static_state->wrap_t);
1884
1885 /*
1886 * Transform 4 x i32 in
1887 *
1888 * s_fpart = {s0, s1, s2, s3}
1889 *
1890 * into 8 x i16
1891 *
1892 * s_fpart = {00, s0, 00, s1, 00, s2, 00, s3}
1893 *
1894 * into two 8 x i16
1895 *
1896 * s_fpart_lo = {s0, s0, s0, s0, s1, s1, s1, s1}
1897 * s_fpart_hi = {s2, s2, s2, s2, s3, s3, s3, s3}
1898 *
1899 * and likewise for t_fpart. There is no risk of loosing precision here
1900 * since the fractional parts only use the lower 8bits.
1901 */
1902
1903 s_fpart = LLVMBuildBitCast(builder, s_fpart, h16_vec_type, "");
1904 t_fpart = LLVMBuildBitCast(builder, t_fpart, h16_vec_type, "");
1905
1906 {
1907 LLVMTypeRef elem_type = LLVMInt32Type();
1908 LLVMValueRef shuffles_lo[LP_MAX_VECTOR_LENGTH];
1909 LLVMValueRef shuffles_hi[LP_MAX_VECTOR_LENGTH];
1910 LLVMValueRef shuffle_lo;
1911 LLVMValueRef shuffle_hi;
1912 unsigned i, j;
1913
1914 for(j = 0; j < h16.type.length; j += 4) {
1915 unsigned subindex = util_cpu_caps.little_endian ? 0 : 1;
1916 LLVMValueRef index;
1917
1918 index = LLVMConstInt(elem_type, j/2 + subindex, 0);
1919 for(i = 0; i < 4; ++i)
1920 shuffles_lo[j + i] = index;
1921
1922 index = LLVMConstInt(elem_type, h16.type.length/2 + j/2 + subindex, 0);
1923 for(i = 0; i < 4; ++i)
1924 shuffles_hi[j + i] = index;
1925 }
1926
1927 shuffle_lo = LLVMConstVector(shuffles_lo, h16.type.length);
1928 shuffle_hi = LLVMConstVector(shuffles_hi, h16.type.length);
1929
1930 s_fpart_lo = LLVMBuildShuffleVector(builder, s_fpart, h16.undef, shuffle_lo, "");
1931 t_fpart_lo = LLVMBuildShuffleVector(builder, t_fpart, h16.undef, shuffle_lo, "");
1932 s_fpart_hi = LLVMBuildShuffleVector(builder, s_fpart, h16.undef, shuffle_hi, "");
1933 t_fpart_hi = LLVMBuildShuffleVector(builder, t_fpart, h16.undef, shuffle_hi, "");
1934 }
1935
1936 stride = lp_build_get_const_level_stride_vec(bld, stride_array, 0);
1937
1938 /*
1939 * Fetch the pixels as 4 x 32bit (rgba order might differ):
1940 *
1941 * rgba0 rgba1 rgba2 rgba3
1942 *
1943 * bit cast them into 16 x u8
1944 *
1945 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
1946 *
1947 * unpack them into two 8 x i16:
1948 *
1949 * r0 g0 b0 a0 r1 g1 b1 a1
1950 * r2 g2 b2 a2 r3 g3 b3 a3
1951 *
1952 * The higher 8 bits of the resulting elements will be zero.
1953 */
1954
1955 neighbors[0][0] = lp_build_sample_packed(bld, x0, y0, stride, data_array);
1956 neighbors[0][1] = lp_build_sample_packed(bld, x1, y0, stride, data_array);
1957 neighbors[1][0] = lp_build_sample_packed(bld, x0, y1, stride, data_array);
1958 neighbors[1][1] = lp_build_sample_packed(bld, x1, y1, stride, data_array);
1959
1960 neighbors[0][0] = LLVMBuildBitCast(builder, neighbors[0][0], u8n_vec_type, "");
1961 neighbors[0][1] = LLVMBuildBitCast(builder, neighbors[0][1], u8n_vec_type, "");
1962 neighbors[1][0] = LLVMBuildBitCast(builder, neighbors[1][0], u8n_vec_type, "");
1963 neighbors[1][1] = LLVMBuildBitCast(builder, neighbors[1][1], u8n_vec_type, "");
1964
1965 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[0][0], &neighbors_lo[0][0], &neighbors_hi[0][0]);
1966 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[0][1], &neighbors_lo[0][1], &neighbors_hi[0][1]);
1967 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[1][0], &neighbors_lo[1][0], &neighbors_hi[1][0]);
1968 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[1][1], &neighbors_lo[1][1], &neighbors_hi[1][1]);
1969
1970 /*
1971 * Linear interpolate with 8.8 fixed point.
1972 */
1973
1974 packed_lo = lp_build_lerp_2d(&h16,
1975 s_fpart_lo, t_fpart_lo,
1976 neighbors_lo[0][0],
1977 neighbors_lo[0][1],
1978 neighbors_lo[1][0],
1979 neighbors_lo[1][1]);
1980
1981 packed_hi = lp_build_lerp_2d(&h16,
1982 s_fpart_hi, t_fpart_hi,
1983 neighbors_hi[0][0],
1984 neighbors_hi[0][1],
1985 neighbors_hi[1][0],
1986 neighbors_hi[1][1]);
1987
1988 packed = lp_build_pack2(builder, h16.type, u8n.type, packed_lo, packed_hi);
1989
1990 /*
1991 * Convert to SoA and swizzle.
1992 */
1993
1994 packed = LLVMBuildBitCast(builder, packed, i32_vec_type, "");
1995
1996 lp_build_rgba8_to_f32_soa(bld->builder,
1997 bld->texel_type,
1998 packed, unswizzled);
1999
2000 lp_build_format_swizzle_soa(bld->format_desc,
2001 &bld->texel_bld,
2002 unswizzled, texel_out);
2003
2004 apply_sampler_swizzle(bld, texel_out);
2005 }
2006
2007
2008 static void
2009 lp_build_sample_compare(struct lp_build_sample_context *bld,
2010 LLVMValueRef p,
2011 LLVMValueRef texel[4])
2012 {
2013 struct lp_build_context *texel_bld = &bld->texel_bld;
2014 LLVMValueRef res;
2015 unsigned chan;
2016
2017 if(bld->static_state->compare_mode == PIPE_TEX_COMPARE_NONE)
2018 return;
2019
2020 /* TODO: Compare before swizzling, to avoid redundant computations */
2021 res = NULL;
2022 for(chan = 0; chan < 4; ++chan) {
2023 LLVMValueRef cmp;
2024 cmp = lp_build_cmp(texel_bld, bld->static_state->compare_func, p, texel[chan]);
2025 cmp = lp_build_select(texel_bld, cmp, texel_bld->one, texel_bld->zero);
2026
2027 if(res)
2028 res = lp_build_add(texel_bld, res, cmp);
2029 else
2030 res = cmp;
2031 }
2032
2033 assert(res);
2034 res = lp_build_mul(texel_bld, res, lp_build_const_vec(texel_bld->type, 0.25));
2035
2036 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
2037 for(chan = 0; chan < 3; ++chan)
2038 texel[chan] = res;
2039 texel[3] = texel_bld->one;
2040 }
2041
2042
2043 /**
2044 * Just set texels to white instead of actually sampling the texture.
2045 * For debugging.
2046 */
2047 static void
2048 lp_build_sample_nop(struct lp_build_sample_context *bld,
2049 LLVMValueRef texel_out[4])
2050 {
2051 struct lp_build_context *texel_bld = &bld->texel_bld;
2052 unsigned chan;
2053
2054 for (chan = 0; chan < 4; chan++) {
2055 /*lp_bld_mov(texel_bld, texel, texel_bld->one);*/
2056 texel_out[chan] = texel_bld->one;
2057 }
2058 }
2059
2060
2061 /**
2062 * Build texture sampling code.
2063 * 'texel' will return a vector of four LLVMValueRefs corresponding to
2064 * R, G, B, A.
2065 * \param type vector float type to use for coords, etc.
2066 */
2067 void
2068 lp_build_sample_soa(LLVMBuilderRef builder,
2069 const struct lp_sampler_static_state *static_state,
2070 struct lp_sampler_dynamic_state *dynamic_state,
2071 struct lp_type type,
2072 unsigned unit,
2073 unsigned num_coords,
2074 const LLVMValueRef *coords,
2075 const LLVMValueRef *ddx,
2076 const LLVMValueRef *ddy,
2077 LLVMValueRef lod_bias, /* optional */
2078 LLVMValueRef explicit_lod, /* optional */
2079 LLVMValueRef texel_out[4])
2080 {
2081 struct lp_build_sample_context bld;
2082 LLVMValueRef width, width_vec;
2083 LLVMValueRef height, height_vec;
2084 LLVMValueRef depth, depth_vec;
2085 LLVMValueRef row_stride_array, img_stride_array;
2086 LLVMValueRef data_array;
2087 LLVMValueRef s;
2088 LLVMValueRef t;
2089 LLVMValueRef r;
2090
2091 if (0) {
2092 enum pipe_format fmt = static_state->format;
2093 debug_printf("Sample from %s\n", util_format_name(fmt));
2094 }
2095
2096 /* Setup our build context */
2097 memset(&bld, 0, sizeof bld);
2098 bld.builder = builder;
2099 bld.static_state = static_state;
2100 bld.dynamic_state = dynamic_state;
2101 bld.format_desc = util_format_description(static_state->format);
2102
2103 bld.float_type = lp_type_float(32);
2104 bld.int_type = lp_type_int(32);
2105 bld.coord_type = type;
2106 bld.uint_coord_type = lp_uint_type(type);
2107 bld.int_coord_type = lp_int_type(type);
2108 bld.texel_type = type;
2109
2110 lp_build_context_init(&bld.float_bld, builder, bld.float_type);
2111 lp_build_context_init(&bld.int_bld, builder, bld.int_type);
2112 lp_build_context_init(&bld.coord_bld, builder, bld.coord_type);
2113 lp_build_context_init(&bld.uint_coord_bld, builder, bld.uint_coord_type);
2114 lp_build_context_init(&bld.int_coord_bld, builder, bld.int_coord_type);
2115 lp_build_context_init(&bld.texel_bld, builder, bld.texel_type);
2116
2117 /* Get the dynamic state */
2118 width = dynamic_state->width(dynamic_state, builder, unit);
2119 height = dynamic_state->height(dynamic_state, builder, unit);
2120 depth = dynamic_state->depth(dynamic_state, builder, unit);
2121 row_stride_array = dynamic_state->row_stride(dynamic_state, builder, unit);
2122 img_stride_array = dynamic_state->img_stride(dynamic_state, builder, unit);
2123 data_array = dynamic_state->data_ptr(dynamic_state, builder, unit);
2124 /* Note that data_array is an array[level] of pointers to texture images */
2125
2126 s = coords[0];
2127 t = coords[1];
2128 r = coords[2];
2129
2130 width_vec = lp_build_broadcast_scalar(&bld.uint_coord_bld, width);
2131 height_vec = lp_build_broadcast_scalar(&bld.uint_coord_bld, height);
2132 depth_vec = lp_build_broadcast_scalar(&bld.uint_coord_bld, depth);
2133
2134 if (0) {
2135 /* For debug: no-op texture sampling */
2136 lp_build_sample_nop(&bld, texel_out);
2137 }
2138 else if (util_format_is_rgba8_variant(bld.format_desc) &&
2139 static_state->target == PIPE_TEXTURE_2D &&
2140 static_state->min_img_filter == PIPE_TEX_FILTER_LINEAR &&
2141 static_state->mag_img_filter == PIPE_TEX_FILTER_LINEAR &&
2142 static_state->min_mip_filter == PIPE_TEX_MIPFILTER_NONE &&
2143 is_simple_wrap_mode(static_state->wrap_s) &&
2144 is_simple_wrap_mode(static_state->wrap_t)) {
2145 /* special case */
2146 lp_build_sample_2d_linear_aos(&bld, s, t, width_vec, height_vec,
2147 row_stride_array, data_array, texel_out);
2148 }
2149 else {
2150 lp_build_sample_general(&bld, unit, s, t, r, ddx, ddy,
2151 lod_bias, explicit_lod,
2152 width, height, depth,
2153 width_vec, height_vec, depth_vec,
2154 row_stride_array, img_stride_array,
2155 data_array,
2156 texel_out);
2157 }
2158
2159 lp_build_sample_compare(&bld, r, texel_out);
2160 }