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