winsys/drm: Handle circular dependencies in Makefile.egl.
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_sample_soa.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Texture sampling -- SoA.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35 #include "pipe/p_defines.h"
36 #include "pipe/p_state.h"
37 #include "util/u_debug.h"
38 #include "util/u_dump.h"
39 #include "util/u_memory.h"
40 #include "util/u_math.h"
41 #include "util/u_format.h"
42 #include "util/u_cpu_detect.h"
43 #include "lp_bld_debug.h"
44 #include "lp_bld_type.h"
45 #include "lp_bld_const.h"
46 #include "lp_bld_conv.h"
47 #include "lp_bld_arit.h"
48 #include "lp_bld_logic.h"
49 #include "lp_bld_swizzle.h"
50 #include "lp_bld_pack.h"
51 #include "lp_bld_format.h"
52 #include "lp_bld_sample.h"
53
54
55 /**
56 * Keep all information for sampling code generation in a single place.
57 */
58 struct lp_build_sample_context
59 {
60 LLVMBuilderRef builder;
61
62 const struct lp_sampler_static_state *static_state;
63
64 struct lp_sampler_dynamic_state *dynamic_state;
65
66 const struct util_format_description *format_desc;
67
68 /** Incoming coordinates type and build context */
69 struct lp_type coord_type;
70 struct lp_build_context coord_bld;
71
72 /** Unsigned integer coordinates */
73 struct lp_type uint_coord_type;
74 struct lp_build_context uint_coord_bld;
75
76 /** Signed integer coordinates */
77 struct lp_type int_coord_type;
78 struct lp_build_context int_coord_bld;
79
80 /** Output texels type and build context */
81 struct lp_type texel_type;
82 struct lp_build_context texel_bld;
83 };
84
85
86 /**
87 * Does the given texture wrap mode allow sampling the texture border color?
88 * XXX maybe move this into gallium util code.
89 */
90 static boolean
91 wrap_mode_uses_border_color(unsigned mode)
92 {
93 switch (mode) {
94 case PIPE_TEX_WRAP_REPEAT:
95 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
96 case PIPE_TEX_WRAP_MIRROR_REPEAT:
97 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
98 return FALSE;
99 case PIPE_TEX_WRAP_CLAMP:
100 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
101 case PIPE_TEX_WRAP_MIRROR_CLAMP:
102 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
103 return TRUE;
104 default:
105 assert(0 && "unexpected wrap mode");
106 return FALSE;
107 }
108 }
109
110
111
112 /**
113 * Gen code to fetch a texel from a texture at int coords (x, y).
114 * The result, texel, will be:
115 * texel[0] = red values
116 * texel[1] = green values
117 * texel[2] = blue values
118 * texel[3] = alpha values
119 */
120 static void
121 lp_build_sample_texel_soa(struct lp_build_sample_context *bld,
122 LLVMValueRef width,
123 LLVMValueRef height,
124 LLVMValueRef x,
125 LLVMValueRef y,
126 LLVMValueRef y_stride,
127 LLVMValueRef data_array,
128 LLVMValueRef *texel)
129 {
130 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
131 LLVMValueRef offset;
132 LLVMValueRef packed;
133 LLVMValueRef use_border = NULL;
134 LLVMValueRef data_ptr;
135
136 /* use_border = x < 0 || x >= width || y < 0 || y >= height */
137 if (wrap_mode_uses_border_color(bld->static_state->wrap_s)) {
138 LLVMValueRef b1, b2;
139 b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, x, int_coord_bld->zero);
140 b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, x, width);
141 use_border = LLVMBuildOr(bld->builder, b1, b2, "b1_or_b2");
142 }
143
144 if (wrap_mode_uses_border_color(bld->static_state->wrap_t)) {
145 LLVMValueRef b1, b2;
146 b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, y, int_coord_bld->zero);
147 b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, y, height);
148 if (use_border) {
149 use_border = LLVMBuildOr(bld->builder, use_border, b1, "ub_or_b1");
150 use_border = LLVMBuildOr(bld->builder, use_border, b2, "ub_or_b2");
151 }
152 else {
153 use_border = LLVMBuildOr(bld->builder, b1, b2, "b1_or_b2");
154 }
155 }
156
157 /* XXX always use mipmap level 0 for now */
158 {
159 const int level = 0;
160 LLVMValueRef indexes[2];
161 indexes[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
162 indexes[1] = LLVMConstInt(LLVMInt32Type(), level, 0);
163 data_ptr = LLVMBuildGEP(bld->builder, data_array, indexes, 2, "");
164 data_ptr = LLVMBuildLoad(bld->builder, data_ptr, "");
165 }
166
167 /*
168 * Note: if we find an app which frequently samples the texture border
169 * we might want to implement a true conditional here to avoid sampling
170 * the texture whenever possible (since that's quite a bit of code).
171 * Ex:
172 * if (use_border) {
173 * texel = border_color;
174 * }
175 * else {
176 * texel = sample_texture(coord);
177 * }
178 * As it is now, we always sample the texture, then selectively replace
179 * the texel color results with the border color.
180 */
181
182 /* convert x,y coords to linear offset from start of texture, in bytes */
183 offset = lp_build_sample_offset(&bld->uint_coord_bld,
184 bld->format_desc,
185 x, y, y_stride);
186
187 assert(bld->format_desc->block.width == 1);
188 assert(bld->format_desc->block.height == 1);
189 assert(bld->format_desc->block.bits <= bld->texel_type.width);
190
191 /* gather the texels from the texture */
192 packed = lp_build_gather(bld->builder,
193 bld->texel_type.length,
194 bld->format_desc->block.bits,
195 bld->texel_type.width,
196 data_ptr, offset);
197
198 /* convert texels to float rgba */
199 lp_build_unpack_rgba_soa(bld->builder,
200 bld->format_desc,
201 bld->texel_type,
202 packed, texel);
203
204 if (use_border) {
205 /* select texel color or border color depending on use_border */
206 int chan;
207 for (chan = 0; chan < 4; chan++) {
208 LLVMValueRef border_chan =
209 lp_build_const_scalar(bld->texel_type,
210 bld->static_state->border_color[chan]);
211 texel[chan] = lp_build_select(&bld->texel_bld, use_border,
212 border_chan, texel[chan]);
213 }
214 }
215 }
216
217
218 static LLVMValueRef
219 lp_build_sample_packed(struct lp_build_sample_context *bld,
220 LLVMValueRef x,
221 LLVMValueRef y,
222 LLVMValueRef y_stride,
223 LLVMValueRef data_array)
224 {
225 LLVMValueRef offset;
226 LLVMValueRef data_ptr;
227
228 offset = lp_build_sample_offset(&bld->uint_coord_bld,
229 bld->format_desc,
230 x, y, y_stride);
231
232 assert(bld->format_desc->block.width == 1);
233 assert(bld->format_desc->block.height == 1);
234 assert(bld->format_desc->block.bits <= bld->texel_type.width);
235
236 /* XXX always use mipmap level 0 for now */
237 {
238 const int level = 0;
239 LLVMValueRef indexes[2];
240 /* get data_ptr[level] */
241 indexes[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
242 indexes[1] = LLVMConstInt(LLVMInt32Type(), level, 0);
243 data_ptr = LLVMBuildGEP(bld->builder, data_array, indexes, 2, "");
244 /* load texture base address */
245 data_ptr = LLVMBuildLoad(bld->builder, data_ptr, "");
246 }
247
248 return lp_build_gather(bld->builder,
249 bld->texel_type.length,
250 bld->format_desc->block.bits,
251 bld->texel_type.width,
252 data_ptr, offset);
253 }
254
255
256 /**
257 * Helper to compute the mirror function for the PIPE_WRAP_MIRROR modes.
258 */
259 static LLVMValueRef
260 lp_build_coord_mirror(struct lp_build_sample_context *bld,
261 LLVMValueRef coord)
262 {
263 struct lp_build_context *coord_bld = &bld->coord_bld;
264 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
265 LLVMValueRef fract, flr, isOdd;
266
267 /* fract = coord - floor(coord) */
268 fract = lp_build_sub(coord_bld, coord, lp_build_floor(coord_bld, coord));
269
270 /* flr = ifloor(coord); */
271 flr = lp_build_ifloor(coord_bld, coord);
272
273 /* isOdd = flr & 1 */
274 isOdd = LLVMBuildAnd(bld->builder, flr, int_coord_bld->one, "");
275
276 /* make coord positive or negative depending on isOdd */
277 coord = lp_build_set_sign(coord_bld, fract, isOdd);
278
279 /* convert isOdd to float */
280 isOdd = lp_build_int_to_float(coord_bld, isOdd);
281
282 /* add isOdd to coord */
283 coord = lp_build_add(coord_bld, coord, isOdd);
284
285 return coord;
286 }
287
288
289 /**
290 * We only support a few wrap modes in lp_build_sample_wrap_int() at this time.
291 * Return whether the given mode is supported by that function.
292 */
293 static boolean
294 is_simple_wrap_mode(unsigned mode)
295 {
296 switch (mode) {
297 case PIPE_TEX_WRAP_REPEAT:
298 case PIPE_TEX_WRAP_CLAMP:
299 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
300 return TRUE;
301 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
302 default:
303 return FALSE;
304 }
305 }
306
307
308 /**
309 * Build LLVM code for texture wrap mode, for scaled integer texcoords.
310 * \param coord the incoming texcoord (s,t,r or q) scaled to the texture size
311 * \param length the texture size along one dimension
312 * \param is_pot if TRUE, length is a power of two
313 * \param wrap_mode one of PIPE_TEX_WRAP_x
314 */
315 static LLVMValueRef
316 lp_build_sample_wrap_int(struct lp_build_sample_context *bld,
317 LLVMValueRef coord,
318 LLVMValueRef length,
319 boolean is_pot,
320 unsigned wrap_mode)
321 {
322 struct lp_build_context *uint_coord_bld = &bld->uint_coord_bld;
323 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
324 LLVMValueRef length_minus_one;
325
326 length_minus_one = lp_build_sub(uint_coord_bld, length, uint_coord_bld->one);
327
328 switch(wrap_mode) {
329 case PIPE_TEX_WRAP_REPEAT:
330 if(is_pot)
331 coord = LLVMBuildAnd(bld->builder, coord, length_minus_one, "");
332 else
333 /* Signed remainder won't give the right results for negative
334 * dividends but unsigned remainder does.*/
335 coord = LLVMBuildURem(bld->builder, coord, length, "");
336 break;
337
338 case PIPE_TEX_WRAP_CLAMP:
339 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
340 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
341 coord = lp_build_max(int_coord_bld, coord, int_coord_bld->zero);
342 coord = lp_build_min(int_coord_bld, coord, length_minus_one);
343 break;
344
345 case PIPE_TEX_WRAP_MIRROR_REPEAT:
346 case PIPE_TEX_WRAP_MIRROR_CLAMP:
347 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
348 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
349 /* FIXME */
350 _debug_printf("llvmpipe: failed to translate texture wrap mode %s\n",
351 util_dump_tex_wrap(wrap_mode, TRUE));
352 coord = lp_build_max(uint_coord_bld, coord, uint_coord_bld->zero);
353 coord = lp_build_min(uint_coord_bld, coord, length_minus_one);
354 break;
355
356 default:
357 assert(0);
358 }
359
360 return coord;
361 }
362
363
364 /**
365 * Build LLVM code for texture wrap mode for linear filtering.
366 * \param x0_out returns first integer texcoord
367 * \param x1_out returns second integer texcoord
368 * \param weight_out returns linear interpolation weight
369 */
370 static void
371 lp_build_sample_wrap_linear(struct lp_build_sample_context *bld,
372 LLVMValueRef coord,
373 LLVMValueRef length,
374 boolean is_pot,
375 unsigned wrap_mode,
376 LLVMValueRef *x0_out,
377 LLVMValueRef *x1_out,
378 LLVMValueRef *weight_out)
379 {
380 struct lp_build_context *coord_bld = &bld->coord_bld;
381 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
382 struct lp_build_context *uint_coord_bld = &bld->uint_coord_bld;
383 LLVMValueRef two = lp_build_const_scalar(coord_bld->type, 2.0);
384 LLVMValueRef half = lp_build_const_scalar(coord_bld->type, 0.5);
385 LLVMValueRef length_f = lp_build_int_to_float(coord_bld, length);
386 LLVMValueRef length_minus_one = lp_build_sub(uint_coord_bld, length, uint_coord_bld->one);
387 LLVMValueRef length_f_minus_one = lp_build_sub(coord_bld, length_f, coord_bld->one);
388 LLVMValueRef coord0, coord1, weight;
389
390 switch(wrap_mode) {
391 case PIPE_TEX_WRAP_REPEAT:
392 /* mul by size and subtract 0.5 */
393 coord = lp_build_mul(coord_bld, coord, length_f);
394 coord = lp_build_sub(coord_bld, coord, half);
395 /* convert to int */
396 coord0 = lp_build_ifloor(coord_bld, coord);
397 coord1 = lp_build_add(uint_coord_bld, coord0, uint_coord_bld->one);
398 /* compute lerp weight */
399 weight = lp_build_fract(coord_bld, coord);
400 /* repeat wrap */
401 if (is_pot) {
402 coord0 = LLVMBuildAnd(bld->builder, coord0, length_minus_one, "");
403 coord1 = LLVMBuildAnd(bld->builder, coord1, length_minus_one, "");
404 }
405 else {
406 /* Signed remainder won't give the right results for negative
407 * dividends but unsigned remainder does.*/
408 coord0 = LLVMBuildURem(bld->builder, coord0, length, "");
409 coord1 = LLVMBuildURem(bld->builder, coord1, length, "");
410 }
411 break;
412
413 case PIPE_TEX_WRAP_CLAMP:
414 if (bld->static_state->normalized_coords) {
415 coord = lp_build_mul(coord_bld, coord, length_f);
416 }
417 weight = lp_build_fract(coord_bld, coord);
418 coord0 = lp_build_clamp(coord_bld, coord, coord_bld->zero,
419 length_f_minus_one);
420 coord1 = lp_build_add(coord_bld, coord, coord_bld->one);
421 coord1 = lp_build_clamp(coord_bld, coord1, coord_bld->zero,
422 length_f_minus_one);
423 coord0 = lp_build_ifloor(coord_bld, coord0);
424 coord1 = lp_build_ifloor(coord_bld, coord1);
425 break;
426
427 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
428 if (bld->static_state->normalized_coords) {
429 /* clamp to [0,1] */
430 coord = lp_build_clamp(coord_bld, coord, coord_bld->zero, coord_bld->one);
431 /* mul by tex size and subtract 0.5 */
432 coord = lp_build_mul(coord_bld, coord, length_f);
433 coord = lp_build_sub(coord_bld, coord, half);
434 }
435 else {
436 LLVMValueRef min, max;
437 /* clamp to [0.5, length - 0.5] */
438 min = lp_build_const_scalar(coord_bld->type, 0.5F);
439 max = lp_build_sub(coord_bld, length_f, min);
440 coord = lp_build_clamp(coord_bld, coord, min, max);
441 }
442 /* compute lerp weight */
443 weight = lp_build_fract(coord_bld, coord);
444 /* coord0 = floor(coord); */
445 coord0 = lp_build_ifloor(coord_bld, coord);
446 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
447 /* coord0 = max(coord0, 0) */
448 coord0 = lp_build_max(int_coord_bld, coord0, int_coord_bld->zero);
449 /* coord1 = min(coord1, length-1) */
450 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
451 break;
452
453 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
454 {
455 LLVMValueRef min, max;
456 if (bld->static_state->normalized_coords) {
457 /* min = -1.0 / (2 * length) = -0.5 / length */
458 min = lp_build_mul(coord_bld,
459 lp_build_const_scalar(coord_bld->type, -0.5F),
460 lp_build_rcp(coord_bld, length_f));
461 /* max = 1.0 - min */
462 max = lp_build_sub(coord_bld, coord_bld->one, min);
463 /* coord = clamp(coord, min, max) */
464 coord = lp_build_clamp(coord_bld, coord, min, max);
465 /* scale coord to length (and sub 0.5?) */
466 coord = lp_build_mul(coord_bld, coord, length_f);
467 coord = lp_build_sub(coord_bld, coord, half);
468 }
469 else {
470 /* clamp to [-0.5, length + 0.5] */
471 min = lp_build_const_scalar(coord_bld->type, -0.5F);
472 max = lp_build_sub(coord_bld, length_f, min);
473 coord = lp_build_clamp(coord_bld, coord, min, max);
474 coord = lp_build_sub(coord_bld, coord, half);
475 }
476 /* compute lerp weight */
477 weight = lp_build_fract(coord_bld, coord);
478 /* convert to int */
479 coord0 = lp_build_ifloor(coord_bld, coord);
480 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
481 }
482 break;
483
484 case PIPE_TEX_WRAP_MIRROR_REPEAT:
485 /* compute mirror function */
486 coord = lp_build_coord_mirror(bld, coord);
487
488 /* scale coord to length */
489 coord = lp_build_mul(coord_bld, coord, length_f);
490 coord = lp_build_sub(coord_bld, coord, half);
491
492 /* compute lerp weight */
493 weight = lp_build_fract(coord_bld, coord);
494
495 /* convert to int coords */
496 coord0 = lp_build_ifloor(coord_bld, coord);
497 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
498
499 /* coord0 = max(coord0, 0) */
500 coord0 = lp_build_max(int_coord_bld, coord0, int_coord_bld->zero);
501 /* coord1 = min(coord1, length-1) */
502 coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
503 break;
504
505 case PIPE_TEX_WRAP_MIRROR_CLAMP:
506 {
507 LLVMValueRef min, max;
508 /* min = 1.0 / (2 * length) */
509 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
510 /* max = 1.0 - min */
511 max = lp_build_sub(coord_bld, coord_bld->one, min);
512
513 coord = lp_build_abs(coord_bld, coord);
514 coord = lp_build_clamp(coord_bld, coord, min, max);
515 coord = lp_build_mul(coord_bld, coord, length_f);
516 if(0)coord = lp_build_sub(coord_bld, coord, half);
517 weight = lp_build_fract(coord_bld, coord);
518 coord0 = lp_build_ifloor(coord_bld, coord);
519 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
520 }
521 break;
522
523 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
524 {
525 LLVMValueRef min, max;
526 /* min = 1.0 / (2 * length) */
527 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
528 /* max = 1.0 - min */
529 max = lp_build_sub(coord_bld, coord_bld->one, min);
530
531 coord = lp_build_abs(coord_bld, coord);
532 coord = lp_build_clamp(coord_bld, coord, min, max);
533 coord = lp_build_mul(coord_bld, coord, length_f);
534 coord = lp_build_sub(coord_bld, coord, half);
535 weight = lp_build_fract(coord_bld, coord);
536 coord0 = lp_build_ifloor(coord_bld, coord);
537 coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
538 }
539 break;
540
541 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
542 {
543 LLVMValueRef min, max;
544 /* min = -1.0 / (2 * length) = -0.5 / length */
545 min = lp_build_mul(coord_bld,
546 lp_build_const_scalar(coord_bld->type, -0.5F),
547 lp_build_rcp(coord_bld, length_f));
548 /* max = 1.0 - min */
549 max = lp_build_sub(coord_bld, coord_bld->one, min);
550
551 coord = lp_build_abs(coord_bld, coord);
552 coord = lp_build_clamp(coord_bld, coord, min, max);
553 coord = lp_build_mul(coord_bld, coord, length_f);
554 coord = lp_build_sub(coord_bld, coord, half);
555 weight = lp_build_fract(coord_bld, coord);
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 default:
562 assert(0);
563 coord0 = NULL;
564 coord1 = NULL;
565 weight = NULL;
566 }
567
568 *x0_out = coord0;
569 *x1_out = coord1;
570 *weight_out = weight;
571 }
572
573
574 /**
575 * Build LLVM code for texture wrap mode for nearest filtering.
576 * \param coord the incoming texcoord (nominally in [0,1])
577 * \param length the texture size along one dimension, as int
578 * \param is_pot if TRUE, length is a power of two
579 * \param wrap_mode one of PIPE_TEX_WRAP_x
580 */
581 static LLVMValueRef
582 lp_build_sample_wrap_nearest(struct lp_build_sample_context *bld,
583 LLVMValueRef coord,
584 LLVMValueRef length,
585 boolean is_pot,
586 unsigned wrap_mode)
587 {
588 struct lp_build_context *coord_bld = &bld->coord_bld;
589 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
590 struct lp_build_context *uint_coord_bld = &bld->uint_coord_bld;
591 LLVMValueRef two = lp_build_const_scalar(coord_bld->type, 2.0);
592 LLVMValueRef length_f = lp_build_int_to_float(coord_bld, length);
593 LLVMValueRef length_minus_one = lp_build_sub(uint_coord_bld, length, uint_coord_bld->one);
594 LLVMValueRef length_f_minus_one = lp_build_sub(coord_bld, length_f, coord_bld->one);
595 LLVMValueRef icoord;
596
597 switch(wrap_mode) {
598 case PIPE_TEX_WRAP_REPEAT:
599 coord = lp_build_mul(coord_bld, coord, length_f);
600 icoord = lp_build_ifloor(coord_bld, coord);
601 if (is_pot)
602 icoord = LLVMBuildAnd(bld->builder, icoord, length_minus_one, "");
603 else
604 /* Signed remainder won't give the right results for negative
605 * dividends but unsigned remainder does.*/
606 icoord = LLVMBuildURem(bld->builder, icoord, length, "");
607 break;
608
609 case PIPE_TEX_WRAP_CLAMP:
610 /* mul by size */
611 if (bld->static_state->normalized_coords) {
612 coord = lp_build_mul(coord_bld, coord, length_f);
613 }
614 /* floor */
615 icoord = lp_build_ifloor(coord_bld, coord);
616 /* clamp to [0, size-1]. Note: int coord builder type */
617 icoord = lp_build_clamp(int_coord_bld, icoord, int_coord_bld->zero,
618 length_minus_one);
619 break;
620
621 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
622 {
623 LLVMValueRef min, max;
624 if (bld->static_state->normalized_coords) {
625 /* min = 1.0 / (2 * length) */
626 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
627 /* max = length - min */
628 max = lp_build_sub(coord_bld, length_f, min);
629 /* scale coord to length */
630 coord = lp_build_mul(coord_bld, coord, length_f);
631 }
632 else {
633 /* clamp to [0.5, length - 0.5] */
634 min = lp_build_const_scalar(coord_bld->type, 0.5F);
635 max = lp_build_sub(coord_bld, length_f, min);
636 }
637 /* coord = clamp(coord, min, max) */
638 coord = lp_build_clamp(coord_bld, coord, min, max);
639 icoord = lp_build_ifloor(coord_bld, coord);
640 }
641 break;
642
643 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
644 /* Note: this is the same as CLAMP_TO_EDGE, except min = -min */
645 {
646 LLVMValueRef min, max;
647 if (bld->static_state->normalized_coords) {
648 /* min = -1.0 / (2 * length) = -0.5 / length */
649 min = lp_build_mul(coord_bld,
650 lp_build_const_scalar(coord_bld->type, -0.5F),
651 lp_build_rcp(coord_bld, length_f));
652 /* max = length - min */
653 max = lp_build_sub(coord_bld, length_f, min);
654 /* scale coord to length */
655 coord = lp_build_mul(coord_bld, coord, length_f);
656 }
657 else {
658 /* clamp to [-0.5, length + 0.5] */
659 min = lp_build_const_scalar(coord_bld->type, -0.5F);
660 max = lp_build_sub(coord_bld, length_f, min);
661 }
662 /* coord = clamp(coord, min, max) */
663 coord = lp_build_clamp(coord_bld, coord, min, max);
664 icoord = lp_build_ifloor(coord_bld, coord);
665 }
666 break;
667
668 case PIPE_TEX_WRAP_MIRROR_REPEAT:
669 {
670 LLVMValueRef min, max;
671 /* min = 1.0 / (2 * length) */
672 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
673 /* max = length - min */
674 max = lp_build_sub(coord_bld, length_f, min);
675
676 /* compute mirror function */
677 coord = lp_build_coord_mirror(bld, coord);
678
679 /* scale coord to length */
680 coord = lp_build_mul(coord_bld, coord, length_f);
681
682 /* coord = clamp(coord, min, max) */
683 coord = lp_build_clamp(coord_bld, coord, min, max);
684 icoord = lp_build_ifloor(coord_bld, coord);
685 }
686 break;
687
688 case PIPE_TEX_WRAP_MIRROR_CLAMP:
689 coord = lp_build_abs(coord_bld, coord);
690 coord = lp_build_mul(coord_bld, coord, length_f);
691 coord = lp_build_clamp(coord_bld, coord, coord_bld->zero, length_f_minus_one);
692 icoord = lp_build_ifloor(coord_bld, coord);
693 break;
694
695 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
696 {
697 LLVMValueRef min, max;
698 /* min = 1.0 / (2 * length) */
699 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
700 /* max = length - min */
701 max = lp_build_sub(coord_bld, length_f, min);
702
703 coord = lp_build_abs(coord_bld, coord);
704 coord = lp_build_mul(coord_bld, coord, length_f);
705 coord = lp_build_clamp(coord_bld, coord, min, max);
706 icoord = lp_build_ifloor(coord_bld, coord);
707 }
708 break;
709
710 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
711 {
712 LLVMValueRef min, max;
713 /* min = 1.0 / (2 * length) */
714 min = lp_build_rcp(coord_bld, lp_build_mul(coord_bld, two, length_f));
715 min = lp_build_negate(coord_bld, min);
716 /* max = length - min */
717 max = lp_build_sub(coord_bld, length_f, min);
718
719 coord = lp_build_abs(coord_bld, coord);
720 coord = lp_build_mul(coord_bld, coord, length_f);
721 coord = lp_build_clamp(coord_bld, coord, min, max);
722 icoord = lp_build_ifloor(coord_bld, coord);
723 }
724 break;
725
726 default:
727 assert(0);
728 icoord = NULL;
729 }
730
731 return icoord;
732 }
733
734
735 /**
736 * Sample 2D texture with nearest filtering.
737 */
738 static void
739 lp_build_sample_2d_nearest_soa(struct lp_build_sample_context *bld,
740 LLVMValueRef s,
741 LLVMValueRef t,
742 LLVMValueRef width,
743 LLVMValueRef height,
744 LLVMValueRef stride,
745 LLVMValueRef data_array,
746 LLVMValueRef *texel)
747 {
748 LLVMValueRef x, y;
749
750 x = lp_build_sample_wrap_nearest(bld, s, width,
751 bld->static_state->pot_width,
752 bld->static_state->wrap_s);
753 y = lp_build_sample_wrap_nearest(bld, t, height,
754 bld->static_state->pot_height,
755 bld->static_state->wrap_t);
756
757 lp_build_name(x, "tex.x.wrapped");
758 lp_build_name(y, "tex.y.wrapped");
759
760 lp_build_sample_texel_soa(bld, width, height, x, y, stride, data_array, texel);
761 }
762
763
764 /**
765 * Sample 2D texture with bilinear filtering.
766 */
767 static void
768 lp_build_sample_2d_linear_soa(struct lp_build_sample_context *bld,
769 LLVMValueRef s,
770 LLVMValueRef t,
771 LLVMValueRef width,
772 LLVMValueRef height,
773 LLVMValueRef stride,
774 LLVMValueRef data_array,
775 LLVMValueRef *texel)
776 {
777 LLVMValueRef s_fpart;
778 LLVMValueRef t_fpart;
779 LLVMValueRef x0, x1;
780 LLVMValueRef y0, y1;
781 LLVMValueRef neighbors[2][2][4];
782 unsigned chan;
783
784 lp_build_sample_wrap_linear(bld, s, width, bld->static_state->pot_width,
785 bld->static_state->wrap_s, &x0, &x1, &s_fpart);
786 lp_build_sample_wrap_linear(bld, t, height, bld->static_state->pot_height,
787 bld->static_state->wrap_t, &y0, &y1, &t_fpart);
788
789 lp_build_sample_texel_soa(bld, width, height, x0, y0, stride, data_array, neighbors[0][0]);
790 lp_build_sample_texel_soa(bld, width, height, x1, y0, stride, data_array, neighbors[0][1]);
791 lp_build_sample_texel_soa(bld, width, height, x0, y1, stride, data_array, neighbors[1][0]);
792 lp_build_sample_texel_soa(bld, width, height, x1, y1, stride, data_array, neighbors[1][1]);
793
794 /* TODO: Don't interpolate missing channels */
795 for(chan = 0; chan < 4; ++chan) {
796 texel[chan] = lp_build_lerp_2d(&bld->texel_bld,
797 s_fpart, t_fpart,
798 neighbors[0][0][chan],
799 neighbors[0][1][chan],
800 neighbors[1][0][chan],
801 neighbors[1][1][chan]);
802 }
803 }
804
805
806 static void
807 lp_build_rgba8_to_f32_soa(LLVMBuilderRef builder,
808 struct lp_type dst_type,
809 LLVMValueRef packed,
810 LLVMValueRef *rgba)
811 {
812 LLVMValueRef mask = lp_build_int_const_scalar(dst_type, 0xff);
813 unsigned chan;
814
815 /* Decode the input vector components */
816 for (chan = 0; chan < 4; ++chan) {
817 unsigned start = chan*8;
818 unsigned stop = start + 8;
819 LLVMValueRef input;
820
821 input = packed;
822
823 if(start)
824 input = LLVMBuildLShr(builder, input, lp_build_int_const_scalar(dst_type, start), "");
825
826 if(stop < 32)
827 input = LLVMBuildAnd(builder, input, mask, "");
828
829 input = lp_build_unsigned_norm_to_float(builder, 8, dst_type, input);
830
831 rgba[chan] = input;
832 }
833 }
834
835
836 static void
837 lp_build_sample_2d_linear_aos(struct lp_build_sample_context *bld,
838 LLVMValueRef s,
839 LLVMValueRef t,
840 LLVMValueRef width,
841 LLVMValueRef height,
842 LLVMValueRef stride,
843 LLVMValueRef data_array,
844 LLVMValueRef *texel)
845 {
846 LLVMBuilderRef builder = bld->builder;
847 struct lp_build_context i32, h16, u8n;
848 LLVMTypeRef i32_vec_type, h16_vec_type, u8n_vec_type;
849 LLVMValueRef i32_c8, i32_c128, i32_c255;
850 LLVMValueRef s_ipart, s_fpart, s_fpart_lo, s_fpart_hi;
851 LLVMValueRef t_ipart, t_fpart, t_fpart_lo, t_fpart_hi;
852 LLVMValueRef x0, x1;
853 LLVMValueRef y0, y1;
854 LLVMValueRef neighbors[2][2];
855 LLVMValueRef neighbors_lo[2][2];
856 LLVMValueRef neighbors_hi[2][2];
857 LLVMValueRef packed, packed_lo, packed_hi;
858 LLVMValueRef unswizzled[4];
859
860 lp_build_context_init(&i32, builder, lp_type_int(32));
861 lp_build_context_init(&h16, builder, lp_type_ufixed(16));
862 lp_build_context_init(&u8n, builder, lp_type_unorm(8));
863
864 i32_vec_type = lp_build_vec_type(i32.type);
865 h16_vec_type = lp_build_vec_type(h16.type);
866 u8n_vec_type = lp_build_vec_type(u8n.type);
867
868 if (bld->static_state->normalized_coords) {
869 LLVMTypeRef coord_vec_type = lp_build_vec_type(bld->coord_type);
870 LLVMValueRef fp_width = LLVMBuildSIToFP(bld->builder, width, coord_vec_type, "");
871 LLVMValueRef fp_height = LLVMBuildSIToFP(bld->builder, height, coord_vec_type, "");
872 s = lp_build_mul(&bld->coord_bld, s, fp_width);
873 t = lp_build_mul(&bld->coord_bld, t, fp_height);
874 }
875
876 /* scale coords by 256 (8 fractional bits) */
877 s = lp_build_mul_imm(&bld->coord_bld, s, 256);
878 t = lp_build_mul_imm(&bld->coord_bld, t, 256);
879
880 /* convert float to int */
881 s = LLVMBuildFPToSI(builder, s, i32_vec_type, "");
882 t = LLVMBuildFPToSI(builder, t, i32_vec_type, "");
883
884 /* subtract 0.5 (add -128) */
885 i32_c128 = lp_build_int_const_scalar(i32.type, -128);
886 s = LLVMBuildAdd(builder, s, i32_c128, "");
887 t = LLVMBuildAdd(builder, t, i32_c128, "");
888
889 /* compute floor (shift right 8) */
890 i32_c8 = lp_build_int_const_scalar(i32.type, 8);
891 s_ipart = LLVMBuildAShr(builder, s, i32_c8, "");
892 t_ipart = LLVMBuildAShr(builder, t, i32_c8, "");
893
894 /* compute fractional part (AND with 0xff) */
895 i32_c255 = lp_build_int_const_scalar(i32.type, 255);
896 s_fpart = LLVMBuildAnd(builder, s, i32_c255, "");
897 t_fpart = LLVMBuildAnd(builder, t, i32_c255, "");
898
899 x0 = s_ipart;
900 y0 = t_ipart;
901
902 x1 = lp_build_add(&bld->int_coord_bld, x0, bld->int_coord_bld.one);
903 y1 = lp_build_add(&bld->int_coord_bld, y0, bld->int_coord_bld.one);
904
905 x0 = lp_build_sample_wrap_int(bld, x0, width, bld->static_state->pot_width,
906 bld->static_state->wrap_s);
907 y0 = lp_build_sample_wrap_int(bld, y0, height, bld->static_state->pot_height,
908 bld->static_state->wrap_t);
909
910 x1 = lp_build_sample_wrap_int(bld, x1, width, bld->static_state->pot_width,
911 bld->static_state->wrap_s);
912 y1 = lp_build_sample_wrap_int(bld, y1, height, bld->static_state->pot_height,
913 bld->static_state->wrap_t);
914
915 /*
916 * Transform 4 x i32 in
917 *
918 * s_fpart = {s0, s1, s2, s3}
919 *
920 * into 8 x i16
921 *
922 * s_fpart = {00, s0, 00, s1, 00, s2, 00, s3}
923 *
924 * into two 8 x i16
925 *
926 * s_fpart_lo = {s0, s0, s0, s0, s1, s1, s1, s1}
927 * s_fpart_hi = {s2, s2, s2, s2, s3, s3, s3, s3}
928 *
929 * and likewise for t_fpart. There is no risk of loosing precision here
930 * since the fractional parts only use the lower 8bits.
931 */
932
933 s_fpart = LLVMBuildBitCast(builder, s_fpart, h16_vec_type, "");
934 t_fpart = LLVMBuildBitCast(builder, t_fpart, h16_vec_type, "");
935
936 {
937 LLVMTypeRef elem_type = LLVMInt32Type();
938 LLVMValueRef shuffles_lo[LP_MAX_VECTOR_LENGTH];
939 LLVMValueRef shuffles_hi[LP_MAX_VECTOR_LENGTH];
940 LLVMValueRef shuffle_lo;
941 LLVMValueRef shuffle_hi;
942 unsigned i, j;
943
944 for(j = 0; j < h16.type.length; j += 4) {
945 unsigned subindex = util_cpu_caps.little_endian ? 0 : 1;
946 LLVMValueRef index;
947
948 index = LLVMConstInt(elem_type, j/2 + subindex, 0);
949 for(i = 0; i < 4; ++i)
950 shuffles_lo[j + i] = index;
951
952 index = LLVMConstInt(elem_type, h16.type.length/2 + j/2 + subindex, 0);
953 for(i = 0; i < 4; ++i)
954 shuffles_hi[j + i] = index;
955 }
956
957 shuffle_lo = LLVMConstVector(shuffles_lo, h16.type.length);
958 shuffle_hi = LLVMConstVector(shuffles_hi, h16.type.length);
959
960 s_fpart_lo = LLVMBuildShuffleVector(builder, s_fpart, h16.undef, shuffle_lo, "");
961 t_fpart_lo = LLVMBuildShuffleVector(builder, t_fpart, h16.undef, shuffle_lo, "");
962 s_fpart_hi = LLVMBuildShuffleVector(builder, s_fpart, h16.undef, shuffle_hi, "");
963 t_fpart_hi = LLVMBuildShuffleVector(builder, t_fpart, h16.undef, shuffle_hi, "");
964 }
965
966 /*
967 * Fetch the pixels as 4 x 32bit (rgba order might differ):
968 *
969 * rgba0 rgba1 rgba2 rgba3
970 *
971 * bit cast them into 16 x u8
972 *
973 * r0 g0 b0 a0 r1 g1 b1 a1 r2 g2 b2 a2 r3 g3 b3 a3
974 *
975 * unpack them into two 8 x i16:
976 *
977 * r0 g0 b0 a0 r1 g1 b1 a1
978 * r2 g2 b2 a2 r3 g3 b3 a3
979 *
980 * The higher 8 bits of the resulting elements will be zero.
981 */
982
983 neighbors[0][0] = lp_build_sample_packed(bld, x0, y0, stride, data_array);
984 neighbors[0][1] = lp_build_sample_packed(bld, x1, y0, stride, data_array);
985 neighbors[1][0] = lp_build_sample_packed(bld, x0, y1, stride, data_array);
986 neighbors[1][1] = lp_build_sample_packed(bld, x1, y1, stride, data_array);
987
988 neighbors[0][0] = LLVMBuildBitCast(builder, neighbors[0][0], u8n_vec_type, "");
989 neighbors[0][1] = LLVMBuildBitCast(builder, neighbors[0][1], u8n_vec_type, "");
990 neighbors[1][0] = LLVMBuildBitCast(builder, neighbors[1][0], u8n_vec_type, "");
991 neighbors[1][1] = LLVMBuildBitCast(builder, neighbors[1][1], u8n_vec_type, "");
992
993 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[0][0], &neighbors_lo[0][0], &neighbors_hi[0][0]);
994 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[0][1], &neighbors_lo[0][1], &neighbors_hi[0][1]);
995 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[1][0], &neighbors_lo[1][0], &neighbors_hi[1][0]);
996 lp_build_unpack2(builder, u8n.type, h16.type, neighbors[1][1], &neighbors_lo[1][1], &neighbors_hi[1][1]);
997
998 /*
999 * Linear interpolate with 8.8 fixed point.
1000 */
1001
1002 packed_lo = lp_build_lerp_2d(&h16,
1003 s_fpart_lo, t_fpart_lo,
1004 neighbors_lo[0][0],
1005 neighbors_lo[0][1],
1006 neighbors_lo[1][0],
1007 neighbors_lo[1][1]);
1008
1009 packed_hi = lp_build_lerp_2d(&h16,
1010 s_fpart_hi, t_fpart_hi,
1011 neighbors_hi[0][0],
1012 neighbors_hi[0][1],
1013 neighbors_hi[1][0],
1014 neighbors_hi[1][1]);
1015
1016 packed = lp_build_pack2(builder, h16.type, u8n.type, packed_lo, packed_hi);
1017
1018 /*
1019 * Convert to SoA and swizzle.
1020 */
1021
1022 packed = LLVMBuildBitCast(builder, packed, i32_vec_type, "");
1023
1024 lp_build_rgba8_to_f32_soa(bld->builder,
1025 bld->texel_type,
1026 packed, unswizzled);
1027
1028 lp_build_format_swizzle_soa(bld->format_desc,
1029 bld->texel_type, unswizzled,
1030 texel);
1031 }
1032
1033
1034 static void
1035 lp_build_sample_compare(struct lp_build_sample_context *bld,
1036 LLVMValueRef p,
1037 LLVMValueRef *texel)
1038 {
1039 struct lp_build_context *texel_bld = &bld->texel_bld;
1040 LLVMValueRef res;
1041 unsigned chan;
1042
1043 if(bld->static_state->compare_mode == PIPE_TEX_COMPARE_NONE)
1044 return;
1045
1046 /* TODO: Compare before swizzling, to avoid redundant computations */
1047 res = NULL;
1048 for(chan = 0; chan < 4; ++chan) {
1049 LLVMValueRef cmp;
1050 cmp = lp_build_cmp(texel_bld, bld->static_state->compare_func, p, texel[chan]);
1051 cmp = lp_build_select(texel_bld, cmp, texel_bld->one, texel_bld->zero);
1052
1053 if(res)
1054 res = lp_build_add(texel_bld, res, cmp);
1055 else
1056 res = cmp;
1057 }
1058
1059 assert(res);
1060 res = lp_build_mul(texel_bld, res, lp_build_const_scalar(texel_bld->type, 0.25));
1061
1062 /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1063 for(chan = 0; chan < 3; ++chan)
1064 texel[chan] = res;
1065 texel[3] = texel_bld->one;
1066 }
1067
1068
1069 static int
1070 texture_dims(enum pipe_texture_target tex)
1071 {
1072 switch (tex) {
1073 case PIPE_TEXTURE_1D:
1074 return 1;
1075 case PIPE_TEXTURE_2D:
1076 case PIPE_TEXTURE_CUBE:
1077 return 2;
1078 case PIPE_TEXTURE_3D:
1079 return 3;
1080 default:
1081 assert(0 && "bad texture target in texture_dims()");
1082 return 2;
1083 }
1084 }
1085
1086
1087 /**
1088 * Generate code to compute texture level of detail (lambda).
1089 * \param s vector of texcoord s values
1090 * \param t vector of texcoord t values
1091 * \param r vector of texcoord r values
1092 * \param width scalar int texture width
1093 * \param height scalar int texture height
1094 * \param depth scalar int texture depth
1095 */
1096 static LLVMValueRef
1097 lp_build_lod_selector(struct lp_build_sample_context *bld,
1098 LLVMValueRef s,
1099 LLVMValueRef t,
1100 LLVMValueRef r,
1101 LLVMValueRef width,
1102 LLVMValueRef height,
1103 LLVMValueRef depth)
1104
1105 {
1106 const int dims = texture_dims(bld->static_state->target);
1107 struct lp_build_context *coord_bld = &bld->coord_bld;
1108
1109 LLVMValueRef lod_bias = lp_build_const_scalar(bld->coord_bld.type,
1110 bld->static_state->lod_bias);
1111 LLVMValueRef min_lod = lp_build_const_scalar(bld->coord_bld.type,
1112 bld->static_state->min_lod);
1113 LLVMValueRef max_lod = lp_build_const_scalar(bld->coord_bld.type,
1114 bld->static_state->max_lod);
1115
1116 LLVMValueRef index0 = LLVMConstInt(LLVMInt32Type(), 0, 0);
1117 LLVMValueRef index1 = LLVMConstInt(LLVMInt32Type(), 1, 0);
1118 LLVMValueRef index2 = LLVMConstInt(LLVMInt32Type(), 2, 0);
1119
1120 LLVMValueRef s0, s1, s2;
1121 LLVMValueRef t0, t1, t2;
1122 LLVMValueRef r0, r1, r2;
1123 LLVMValueRef dsdx, dsdy, dtdx, dtdy, drdx, drdy;
1124 LLVMValueRef rho, lod;
1125
1126 /*
1127 * dsdx = abs(s[1] - s[0]);
1128 * dsdy = abs(s[2] - s[0]);
1129 * dtdx = abs(t[1] - t[0]);
1130 * dtdy = abs(t[2] - t[0]);
1131 * drdx = abs(r[1] - r[0]);
1132 * drdy = abs(r[2] - r[0]);
1133 * XXX we're assuming a four-element quad in 2x2 layout here.
1134 */
1135 s0 = LLVMBuildExtractElement(bld->builder, s, index0, "s0");
1136 s1 = LLVMBuildExtractElement(bld->builder, s, index1, "s1");
1137 s2 = LLVMBuildExtractElement(bld->builder, s, index2, "s2");
1138 dsdx = lp_build_abs(coord_bld, lp_build_sub(coord_bld, s1, s0));
1139 dsdy = lp_build_abs(coord_bld, lp_build_sub(coord_bld, s2, s0));
1140 if (dims > 1) {
1141 t0 = LLVMBuildExtractElement(bld->builder, t, index0, "t0");
1142 t1 = LLVMBuildExtractElement(bld->builder, t, index1, "t1");
1143 t2 = LLVMBuildExtractElement(bld->builder, t, index2, "t2");
1144 dtdx = lp_build_abs(coord_bld, lp_build_sub(coord_bld, t1, t0));
1145 dtdy = lp_build_abs(coord_bld, lp_build_sub(coord_bld, t2, t0));
1146 if (dims > 2) {
1147 r0 = LLVMBuildExtractElement(bld->builder, r, index0, "r0");
1148 r1 = LLVMBuildExtractElement(bld->builder, r, index1, "r1");
1149 r2 = LLVMBuildExtractElement(bld->builder, r, index2, "r2");
1150 drdx = lp_build_abs(coord_bld, lp_build_sub(coord_bld, r1, r0));
1151 drdy = lp_build_abs(coord_bld, lp_build_sub(coord_bld, r2, r0));
1152 }
1153 }
1154
1155 /* Compute rho = max of all partial derivatives scaled by texture size.
1156 * XXX this can be vectorized somewhat
1157 */
1158 rho = lp_build_mul(coord_bld,
1159 lp_build_max(coord_bld, dsdx, dsdy),
1160 lp_build_int_to_float(coord_bld, width));
1161 if (dims > 1) {
1162 LLVMValueRef max;
1163 max = lp_build_mul(coord_bld,
1164 lp_build_max(coord_bld, dtdx, dtdy),
1165 lp_build_int_to_float(coord_bld, height));
1166 rho = lp_build_max(coord_bld, rho, max);
1167 if (dims > 2) {
1168 max = lp_build_mul(coord_bld,
1169 lp_build_max(coord_bld, drdx, drdy),
1170 lp_build_int_to_float(coord_bld, depth));
1171 rho = lp_build_max(coord_bld, rho, max);
1172 }
1173 }
1174
1175 /* compute lod = log2(rho) */
1176 lod = lp_build_log2(coord_bld, rho);
1177
1178 /* add lod bias */
1179 lod = lp_build_add(coord_bld, lod, lod_bias);
1180
1181 /* clamp lod */
1182 lod = lp_build_clamp(coord_bld, lod, min_lod, max_lod);
1183
1184 return lod;
1185 }
1186
1187
1188 /**
1189 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
1190 * mipmap level index.
1191 * \param lod scalar float texture level of detail
1192 * \param level_out returns integer
1193 */
1194 static void
1195 lp_build_nearest_mip_level(struct lp_build_sample_context *bld,
1196 unsigned unit,
1197 LLVMValueRef lod,
1198 LLVMValueRef *level_out)
1199 {
1200 struct lp_build_context *coord_bld = &bld->coord_bld;
1201 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
1202 LLVMValueRef last_level, level;
1203
1204 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
1205 bld->builder, unit);
1206
1207 /* convert float lod to integer */
1208 level = lp_build_iround(coord_bld, lod);
1209
1210 /* clamp level to legal range of levels */
1211 *level_out = lp_build_clamp(int_coord_bld, level,
1212 int_coord_bld->zero,
1213 last_level);
1214 }
1215
1216
1217 /**
1218 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
1219 * two (adjacent) mipmap level indexes. Later, we'll sample from those
1220 * two mipmap levels and interpolate between them.
1221 */
1222 static void
1223 lp_build_linear_mip_levels(struct lp_build_sample_context *bld,
1224 unsigned unit,
1225 LLVMValueRef lod,
1226 LLVMValueRef *level0_out,
1227 LLVMValueRef *level1_out,
1228 LLVMValueRef *weight_out)
1229 {
1230 struct lp_build_context *coord_bld = &bld->coord_bld;
1231 struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
1232 LLVMValueRef last_level, level;
1233
1234 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
1235 bld->builder, unit);
1236
1237 /* convert float lod to integer */
1238 level = lp_build_ifloor(coord_bld, lod);
1239
1240 /* compute level 0 and clamp to legal range of levels */
1241 *level0_out = lp_build_clamp(int_coord_bld, level,
1242 int_coord_bld->zero,
1243 last_level);
1244 /* compute level 1 and clamp to legal range of levels */
1245 *level1_out = lp_build_add(int_coord_bld, *level0_out, int_coord_bld->one);
1246 *level1_out = lp_build_min(int_coord_bld, *level1_out, int_coord_bld->zero);
1247
1248 *weight_out = lp_build_fract(coord_bld, lod);
1249 }
1250
1251
1252
1253 /**
1254 * Build texture sampling code.
1255 * 'texel' will return a vector of four LLVMValueRefs corresponding to
1256 * R, G, B, A.
1257 */
1258 void
1259 lp_build_sample_soa(LLVMBuilderRef builder,
1260 const struct lp_sampler_static_state *static_state,
1261 struct lp_sampler_dynamic_state *dynamic_state,
1262 struct lp_type type,
1263 unsigned unit,
1264 unsigned num_coords,
1265 const LLVMValueRef *coords,
1266 LLVMValueRef lodbias,
1267 LLVMValueRef *texel)
1268 {
1269 struct lp_build_sample_context bld;
1270 LLVMValueRef width;
1271 LLVMValueRef height;
1272 LLVMValueRef stride;
1273 LLVMValueRef data_array;
1274 LLVMValueRef s;
1275 LLVMValueRef t;
1276 LLVMValueRef r;
1277
1278 (void) lp_build_lod_selector; /* temporary to silence warning */
1279 (void) lp_build_nearest_mip_level;
1280 (void) lp_build_linear_mip_levels;
1281
1282 /* Setup our build context */
1283 memset(&bld, 0, sizeof bld);
1284 bld.builder = builder;
1285 bld.static_state = static_state;
1286 bld.dynamic_state = dynamic_state;
1287 bld.format_desc = util_format_description(static_state->format);
1288 bld.coord_type = type;
1289 bld.uint_coord_type = lp_uint_type(type);
1290 bld.int_coord_type = lp_int_type(type);
1291 bld.texel_type = type;
1292 lp_build_context_init(&bld.coord_bld, builder, bld.coord_type);
1293 lp_build_context_init(&bld.uint_coord_bld, builder, bld.uint_coord_type);
1294 lp_build_context_init(&bld.int_coord_bld, builder, bld.int_coord_type);
1295 lp_build_context_init(&bld.texel_bld, builder, bld.texel_type);
1296
1297 /* Get the dynamic state */
1298 width = dynamic_state->width(dynamic_state, builder, unit);
1299 height = dynamic_state->height(dynamic_state, builder, unit);
1300 stride = dynamic_state->stride(dynamic_state, builder, unit);
1301 data_array = dynamic_state->data_ptr(dynamic_state, builder, unit);
1302 /* Note that data_array is an array[level] of pointers to texture images */
1303
1304 s = coords[0];
1305 t = coords[1];
1306 r = coords[2];
1307
1308 width = lp_build_broadcast_scalar(&bld.uint_coord_bld, width);
1309 height = lp_build_broadcast_scalar(&bld.uint_coord_bld, height);
1310 stride = lp_build_broadcast_scalar(&bld.uint_coord_bld, stride);
1311
1312 if(static_state->target == PIPE_TEXTURE_1D)
1313 t = bld.coord_bld.zero;
1314
1315 switch (static_state->min_img_filter) {
1316 case PIPE_TEX_FILTER_NEAREST:
1317 lp_build_sample_2d_nearest_soa(&bld, s, t, width, height,
1318 stride, data_array, texel);
1319 break;
1320 case PIPE_TEX_FILTER_LINEAR:
1321 if(lp_format_is_rgba8(bld.format_desc) &&
1322 is_simple_wrap_mode(static_state->wrap_s) &&
1323 is_simple_wrap_mode(static_state->wrap_t))
1324 lp_build_sample_2d_linear_aos(&bld, s, t, width, height,
1325 stride, data_array, texel);
1326 else
1327 lp_build_sample_2d_linear_soa(&bld, s, t, width, height,
1328 stride, data_array, texel);
1329 break;
1330 default:
1331 assert(0);
1332 }
1333
1334 /* FIXME: respect static_state->min_mip_filter */;
1335 /* FIXME: respect static_state->mag_img_filter */;
1336
1337 lp_build_sample_compare(&bld, r, texel);
1338 }