Merge remote branch 'origin/master' into lp-setup-llvm
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_sample.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 -- common code.
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_format.h"
38 #include "util/u_math.h"
39 #include "lp_bld_arit.h"
40 #include "lp_bld_const.h"
41 #include "lp_bld_debug.h"
42 #include "lp_bld_printf.h"
43 #include "lp_bld_flow.h"
44 #include "lp_bld_sample.h"
45 #include "lp_bld_swizzle.h"
46 #include "lp_bld_type.h"
47
48
49 /*
50 * Bri-linear factor. Should be greater than one.
51 */
52 #define BRILINEAR_FACTOR 2
53
54
55 /**
56 * Does the given texture wrap mode allow sampling the texture border color?
57 * XXX maybe move this into gallium util code.
58 */
59 boolean
60 lp_sampler_wrap_mode_uses_border_color(unsigned mode,
61 unsigned min_img_filter,
62 unsigned mag_img_filter)
63 {
64 switch (mode) {
65 case PIPE_TEX_WRAP_REPEAT:
66 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
67 case PIPE_TEX_WRAP_MIRROR_REPEAT:
68 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
69 return FALSE;
70 case PIPE_TEX_WRAP_CLAMP:
71 case PIPE_TEX_WRAP_MIRROR_CLAMP:
72 if (min_img_filter == PIPE_TEX_FILTER_NEAREST &&
73 mag_img_filter == PIPE_TEX_FILTER_NEAREST) {
74 return FALSE;
75 } else {
76 return TRUE;
77 }
78 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
79 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
80 return TRUE;
81 default:
82 assert(0 && "unexpected wrap mode");
83 return FALSE;
84 }
85 }
86
87
88 /**
89 * Initialize lp_sampler_static_state object with the gallium sampler
90 * and texture state.
91 * The former is considered to be static and the later dynamic.
92 */
93 void
94 lp_sampler_static_state(struct lp_sampler_static_state *state,
95 const struct pipe_sampler_view *view,
96 const struct pipe_sampler_state *sampler)
97 {
98 const struct pipe_resource *texture = view->texture;
99
100 memset(state, 0, sizeof *state);
101
102 if(!texture)
103 return;
104
105 if(!sampler)
106 return;
107
108 /*
109 * We don't copy sampler state over unless it is actually enabled, to avoid
110 * spurious recompiles, as the sampler static state is part of the shader
111 * key.
112 *
113 * Ideally the state tracker or cso_cache module would make all state
114 * canonical, but until that happens it's better to be safe than sorry here.
115 *
116 * XXX: Actually there's much more than can be done here, especially
117 * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
118 */
119
120 state->format = view->format;
121 state->swizzle_r = view->swizzle_r;
122 state->swizzle_g = view->swizzle_g;
123 state->swizzle_b = view->swizzle_b;
124 state->swizzle_a = view->swizzle_a;
125
126 state->target = texture->target;
127 state->pot_width = util_is_power_of_two(texture->width0);
128 state->pot_height = util_is_power_of_two(texture->height0);
129 state->pot_depth = util_is_power_of_two(texture->depth0);
130
131 state->wrap_s = sampler->wrap_s;
132 state->wrap_t = sampler->wrap_t;
133 state->wrap_r = sampler->wrap_r;
134 state->min_img_filter = sampler->min_img_filter;
135 state->mag_img_filter = sampler->mag_img_filter;
136
137 if (view->last_level && sampler->max_lod > 0.0f) {
138 state->min_mip_filter = sampler->min_mip_filter;
139 } else {
140 state->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
141 }
142
143 if (state->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
144 if (sampler->lod_bias != 0.0f) {
145 state->lod_bias_non_zero = 1;
146 }
147
148 /* If min_lod == max_lod we can greatly simplify mipmap selection.
149 * This is a case that occurs during automatic mipmap generation.
150 */
151 if (sampler->min_lod == sampler->max_lod) {
152 state->min_max_lod_equal = 1;
153 } else {
154 if (sampler->min_lod > 0.0f) {
155 state->apply_min_lod = 1;
156 }
157
158 if (sampler->max_lod < (float)view->last_level) {
159 state->apply_max_lod = 1;
160 }
161 }
162 }
163
164 state->compare_mode = sampler->compare_mode;
165 if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) {
166 state->compare_func = sampler->compare_func;
167 }
168
169 state->normalized_coords = sampler->normalized_coords;
170
171 /*
172 * FIXME: Handle the remainder of pipe_sampler_view.
173 */
174 }
175
176
177 /**
178 * Generate code to compute coordinate gradient (rho).
179 * \param ddx partial derivatives of (s, t, r, q) with respect to X
180 * \param ddy partial derivatives of (s, t, r, q) with respect to Y
181 *
182 * XXX: The resulting rho is scalar, so we ignore all but the first element of
183 * derivatives that are passed by the shader.
184 */
185 static LLVMValueRef
186 lp_build_rho(struct lp_build_sample_context *bld,
187 const LLVMValueRef ddx[4],
188 const LLVMValueRef ddy[4])
189 {
190 struct lp_build_context *float_size_bld = &bld->float_size_bld;
191 struct lp_build_context *float_bld = &bld->float_bld;
192 const unsigned dims = bld->dims;
193 LLVMTypeRef i32t = LLVMInt32Type();
194 LLVMValueRef index0 = LLVMConstInt(i32t, 0, 0);
195 LLVMValueRef index1 = LLVMConstInt(i32t, 1, 0);
196 LLVMValueRef index2 = LLVMConstInt(i32t, 2, 0);
197 LLVMValueRef dsdx, dsdy, dtdx, dtdy, drdx, drdy;
198 LLVMValueRef rho_x, rho_y;
199 LLVMValueRef rho_vec;
200 LLVMValueRef float_size;
201 LLVMValueRef rho;
202
203 dsdx = ddx[0];
204 dsdy = ddy[0];
205
206 if (dims <= 1) {
207 rho_x = dsdx;
208 rho_y = dsdy;
209 }
210 else {
211 rho_x = float_size_bld->undef;
212 rho_y = float_size_bld->undef;
213
214 rho_x = LLVMBuildInsertElement(bld->builder, rho_x, dsdx, index0, "");
215 rho_y = LLVMBuildInsertElement(bld->builder, rho_y, dsdy, index0, "");
216
217 dtdx = ddx[1];
218 dtdy = ddy[1];
219
220 rho_x = LLVMBuildInsertElement(bld->builder, rho_x, dtdx, index1, "");
221 rho_y = LLVMBuildInsertElement(bld->builder, rho_y, dtdy, index1, "");
222
223 if (dims >= 3) {
224 drdx = ddx[2];
225 drdy = ddy[2];
226
227 rho_x = LLVMBuildInsertElement(bld->builder, rho_x, drdx, index2, "");
228 rho_y = LLVMBuildInsertElement(bld->builder, rho_y, drdy, index2, "");
229 }
230 }
231
232 rho_x = lp_build_abs(float_size_bld, rho_x);
233 rho_y = lp_build_abs(float_size_bld, rho_y);
234
235 rho_vec = lp_build_max(float_size_bld, rho_x, rho_y);
236
237 float_size = lp_build_int_to_float(float_size_bld, bld->int_size);
238
239 rho_vec = lp_build_mul(float_size_bld, rho_vec, float_size);
240
241 if (dims <= 1) {
242 rho = rho_vec;
243 }
244 else {
245 if (dims >= 2) {
246 LLVMValueRef rho_s, rho_t, rho_r;
247
248 rho_s = LLVMBuildExtractElement(bld->builder, rho_vec, index0, "");
249 rho_t = LLVMBuildExtractElement(bld->builder, rho_vec, index1, "");
250
251 rho = lp_build_max(float_bld, rho_s, rho_t);
252
253 if (dims >= 3) {
254 rho_r = LLVMBuildExtractElement(bld->builder, rho_vec, index0, "");
255 rho = lp_build_max(float_bld, rho, rho_r);
256 }
257 }
258 }
259
260 return rho;
261 }
262
263
264 /*
265 * Bri-linear lod computation
266 *
267 * Use a piece-wise linear approximation of log2 such that:
268 * - round to nearest, for values in the neighborhood of -1, 0, 1, 2, etc.
269 * - linear approximation for values in the neighborhood of 0.5, 1.5., etc,
270 * with the steepness specified in 'factor'
271 * - exact result for 0.5, 1.5, etc.
272 *
273 *
274 * 1.0 - /----*
275 * /
276 * /
277 * /
278 * 0.5 - *
279 * /
280 * /
281 * /
282 * 0.0 - *----/
283 *
284 * | |
285 * 2^0 2^1
286 *
287 * This is a technique also commonly used in hardware:
288 * - http://ixbtlabs.com/articles2/gffx/nv40-rx800-3.html
289 *
290 * TODO: For correctness, this should only be applied when texture is known to
291 * have regular mipmaps, i.e., mipmaps derived from the base level.
292 *
293 * TODO: This could be done in fixed point, where applicable.
294 */
295 static void
296 lp_build_brilinear_lod(struct lp_build_context *bld,
297 LLVMValueRef lod,
298 double factor,
299 LLVMValueRef *out_lod_ipart,
300 LLVMValueRef *out_lod_fpart)
301 {
302 LLVMValueRef lod_fpart;
303 double pre_offset = (factor - 0.5)/factor - 0.5;
304 double post_offset = 1 - factor;
305
306 if (0) {
307 lp_build_printf(bld->builder, "lod = %f\n", lod);
308 }
309
310 lod = lp_build_add(bld, lod,
311 lp_build_const_vec(bld->type, pre_offset));
312
313 lp_build_ifloor_fract(bld, lod, out_lod_ipart, &lod_fpart);
314
315 lod_fpart = lp_build_mul(bld, lod_fpart,
316 lp_build_const_vec(bld->type, factor));
317
318 lod_fpart = lp_build_add(bld, lod_fpart,
319 lp_build_const_vec(bld->type, post_offset));
320
321 /*
322 * It's not necessary to clamp lod_fpart since:
323 * - the above expression will never produce numbers greater than one.
324 * - the mip filtering branch is only taken if lod_fpart is positive
325 */
326
327 *out_lod_fpart = lod_fpart;
328
329 if (0) {
330 lp_build_printf(bld->builder, "lod_ipart = %i\n", *out_lod_ipart);
331 lp_build_printf(bld->builder, "lod_fpart = %f\n\n", *out_lod_fpart);
332 }
333 }
334
335
336 /*
337 * Combined log2 and brilinear lod computation.
338 *
339 * It's in all identical to calling lp_build_fast_log2() and
340 * lp_build_brilinear_lod() above, but by combining we can compute the interger
341 * and fractional part independently.
342 */
343 static void
344 lp_build_brilinear_rho(struct lp_build_context *bld,
345 LLVMValueRef rho,
346 double factor,
347 LLVMValueRef *out_lod_ipart,
348 LLVMValueRef *out_lod_fpart)
349 {
350 LLVMValueRef lod_ipart;
351 LLVMValueRef lod_fpart;
352
353 const double pre_factor = (2*factor - 0.5)/(M_SQRT2*factor);
354 const double post_offset = 1 - 2*factor;
355
356 assert(bld->type.floating);
357
358 assert(lp_check_value(bld->type, rho));
359
360 /*
361 * The pre factor will make the intersections with the exact powers of two
362 * happen precisely where we want then to be, which means that the integer
363 * part will not need any post adjustments.
364 */
365 rho = lp_build_mul(bld, rho,
366 lp_build_const_vec(bld->type, pre_factor));
367
368 /* ipart = ifloor(log2(rho)) */
369 lod_ipart = lp_build_extract_exponent(bld, rho, 0);
370
371 /* fpart = rho / 2**ipart */
372 lod_fpart = lp_build_extract_mantissa(bld, rho);
373
374 lod_fpart = lp_build_mul(bld, lod_fpart,
375 lp_build_const_vec(bld->type, factor));
376
377 lod_fpart = lp_build_add(bld, lod_fpart,
378 lp_build_const_vec(bld->type, post_offset));
379
380 /*
381 * Like lp_build_brilinear_lod, it's not necessary to clamp lod_fpart since:
382 * - the above expression will never produce numbers greater than one.
383 * - the mip filtering branch is only taken if lod_fpart is positive
384 */
385
386 *out_lod_ipart = lod_ipart;
387 *out_lod_fpart = lod_fpart;
388 }
389
390
391 /**
392 * Generate code to compute texture level of detail (lambda).
393 * \param ddx partial derivatives of (s, t, r, q) with respect to X
394 * \param ddy partial derivatives of (s, t, r, q) with respect to Y
395 * \param lod_bias optional float vector with the shader lod bias
396 * \param explicit_lod optional float vector with the explicit lod
397 * \param width scalar int texture width
398 * \param height scalar int texture height
399 * \param depth scalar int texture depth
400 *
401 * XXX: The resulting lod is scalar, so ignore all but the first element of
402 * derivatives, lod_bias, etc that are passed by the shader.
403 */
404 void
405 lp_build_lod_selector(struct lp_build_sample_context *bld,
406 unsigned unit,
407 const LLVMValueRef ddx[4],
408 const LLVMValueRef ddy[4],
409 LLVMValueRef lod_bias, /* optional */
410 LLVMValueRef explicit_lod, /* optional */
411 unsigned mip_filter,
412 LLVMValueRef *out_lod_ipart,
413 LLVMValueRef *out_lod_fpart)
414
415 {
416 struct lp_build_context *float_bld = &bld->float_bld;
417 LLVMValueRef lod;
418
419 *out_lod_ipart = bld->int_bld.zero;
420 *out_lod_fpart = bld->float_bld.zero;
421
422 if (bld->static_state->min_max_lod_equal) {
423 /* User is forcing sampling from a particular mipmap level.
424 * This is hit during mipmap generation.
425 */
426 LLVMValueRef min_lod =
427 bld->dynamic_state->min_lod(bld->dynamic_state, bld->builder, unit);
428
429 lod = min_lod;
430 }
431 else {
432 LLVMValueRef sampler_lod_bias =
433 bld->dynamic_state->lod_bias(bld->dynamic_state, bld->builder, unit);
434 LLVMValueRef index0 = LLVMConstInt(LLVMInt32Type(), 0, 0);
435
436 if (explicit_lod) {
437 lod = LLVMBuildExtractElement(bld->builder, explicit_lod,
438 index0, "");
439 }
440 else {
441 LLVMValueRef rho;
442
443 rho = lp_build_rho(bld, ddx, ddy);
444
445 /*
446 * Compute lod = log2(rho)
447 */
448
449 if (!lod_bias &&
450 !bld->static_state->lod_bias_non_zero &&
451 !bld->static_state->apply_max_lod &&
452 !bld->static_state->apply_min_lod) {
453 /*
454 * Special case when there are no post-log2 adjustments, which
455 * saves instructions but keeping the integer and fractional lod
456 * computations separate from the start.
457 */
458
459 if (mip_filter == PIPE_TEX_MIPFILTER_NONE ||
460 mip_filter == PIPE_TEX_MIPFILTER_NEAREST) {
461 *out_lod_ipart = lp_build_ilog2(float_bld, rho);
462 *out_lod_fpart = bld->float_bld.zero;
463 return;
464 }
465 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR &&
466 !(gallivm_debug & GALLIVM_DEBUG_NO_BRILINEAR)) {
467 lp_build_brilinear_rho(float_bld, rho, BRILINEAR_FACTOR,
468 out_lod_ipart, out_lod_fpart);
469 return;
470 }
471 }
472
473 if (0) {
474 lod = lp_build_log2(float_bld, rho);
475 }
476 else {
477 lod = lp_build_fast_log2(float_bld, rho);
478 }
479
480 /* add shader lod bias */
481 if (lod_bias) {
482 lod_bias = LLVMBuildExtractElement(bld->builder, lod_bias,
483 index0, "");
484 lod = LLVMBuildFAdd(bld->builder, lod, lod_bias, "shader_lod_bias");
485 }
486 }
487
488 /* add sampler lod bias */
489 if (bld->static_state->lod_bias_non_zero)
490 lod = LLVMBuildFAdd(bld->builder, lod, sampler_lod_bias, "sampler_lod_bias");
491
492
493 /* clamp lod */
494 if (bld->static_state->apply_max_lod) {
495 LLVMValueRef max_lod =
496 bld->dynamic_state->max_lod(bld->dynamic_state, bld->builder, unit);
497
498 lod = lp_build_min(float_bld, lod, max_lod);
499 }
500 if (bld->static_state->apply_min_lod) {
501 LLVMValueRef min_lod =
502 bld->dynamic_state->min_lod(bld->dynamic_state, bld->builder, unit);
503
504 lod = lp_build_max(float_bld, lod, min_lod);
505 }
506 }
507
508 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
509 if (!(gallivm_debug & GALLIVM_DEBUG_NO_BRILINEAR)) {
510 lp_build_brilinear_lod(float_bld, lod, BRILINEAR_FACTOR,
511 out_lod_ipart, out_lod_fpart);
512 }
513 else {
514 lp_build_ifloor_fract(float_bld, lod, out_lod_ipart, out_lod_fpart);
515 }
516
517 lp_build_name(*out_lod_fpart, "lod_fpart");
518 }
519 else {
520 *out_lod_ipart = lp_build_iround(float_bld, lod);
521 }
522
523 lp_build_name(*out_lod_ipart, "lod_ipart");
524
525 return;
526 }
527
528
529 /**
530 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
531 * mipmap level index.
532 * Note: this is all scalar code.
533 * \param lod scalar float texture level of detail
534 * \param level_out returns integer
535 */
536 void
537 lp_build_nearest_mip_level(struct lp_build_sample_context *bld,
538 unsigned unit,
539 LLVMValueRef lod_ipart,
540 LLVMValueRef *level_out)
541 {
542 struct lp_build_context *int_bld = &bld->int_bld;
543 LLVMValueRef last_level, level;
544
545 LLVMValueRef zero = LLVMConstInt(LLVMInt32Type(), 0, 0);
546
547 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
548 bld->builder, unit);
549
550 /* convert float lod to integer */
551 level = lod_ipart;
552
553 /* clamp level to legal range of levels */
554 *level_out = lp_build_clamp(int_bld, level, zero, last_level);
555 }
556
557
558 /**
559 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
560 * two (adjacent) mipmap level indexes. Later, we'll sample from those
561 * two mipmap levels and interpolate between them.
562 */
563 void
564 lp_build_linear_mip_levels(struct lp_build_sample_context *bld,
565 unsigned unit,
566 LLVMValueRef lod_ipart,
567 LLVMValueRef *lod_fpart_inout,
568 LLVMValueRef *level0_out,
569 LLVMValueRef *level1_out)
570 {
571 LLVMBuilderRef builder = bld->builder;
572 struct lp_build_context *int_bld = &bld->int_bld;
573 struct lp_build_context *float_bld = &bld->float_bld;
574 LLVMValueRef last_level;
575 LLVMValueRef clamp_min;
576 LLVMValueRef clamp_max;
577
578 *level0_out = lod_ipart;
579 *level1_out = lp_build_add(int_bld, lod_ipart, int_bld->one);
580
581 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
582 bld->builder, unit);
583
584 /*
585 * Clamp both lod_ipart and lod_ipart + 1 to [0, last_level], with the
586 * minimum number of comparisons, and zeroing lod_fpart in the extreme
587 * ends in the process.
588 */
589
590 /* lod_ipart < 0 */
591 clamp_min = LLVMBuildICmp(builder, LLVMIntSLT,
592 lod_ipart, int_bld->zero,
593 "clamp_lod_to_zero");
594
595 *level0_out = LLVMBuildSelect(builder, clamp_min,
596 int_bld->zero, *level0_out, "");
597
598 *level1_out = LLVMBuildSelect(builder, clamp_min,
599 int_bld->zero, *level1_out, "");
600
601 *lod_fpart_inout = LLVMBuildSelect(builder, clamp_min,
602 float_bld->zero, *lod_fpart_inout, "");
603
604 /* lod_ipart >= last_level */
605 clamp_max = LLVMBuildICmp(builder, LLVMIntSGE,
606 lod_ipart, last_level,
607 "clamp_lod_to_last");
608
609 *level0_out = LLVMBuildSelect(builder, clamp_max,
610 last_level, *level0_out, "");
611
612 *level1_out = LLVMBuildSelect(builder, clamp_max,
613 last_level, *level1_out, "");
614
615 *lod_fpart_inout = LLVMBuildSelect(builder, clamp_max,
616 float_bld->zero, *lod_fpart_inout, "");
617
618 lp_build_name(*level0_out, "sampler%u_miplevel0", unit);
619 lp_build_name(*level1_out, "sampler%u_miplevel1", unit);
620 lp_build_name(*lod_fpart_inout, "sampler%u_mipweight", unit);
621 }
622
623
624 /**
625 * Return pointer to a single mipmap level.
626 * \param data_array array of pointers to mipmap levels
627 * \param level integer mipmap level
628 */
629 LLVMValueRef
630 lp_build_get_mipmap_level(struct lp_build_sample_context *bld,
631 LLVMValueRef level)
632 {
633 LLVMValueRef indexes[2], data_ptr;
634 indexes[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
635 indexes[1] = level;
636 data_ptr = LLVMBuildGEP(bld->builder, bld->data_array, indexes, 2, "");
637 data_ptr = LLVMBuildLoad(bld->builder, data_ptr, "");
638 return data_ptr;
639 }
640
641
642 LLVMValueRef
643 lp_build_get_const_mipmap_level(struct lp_build_sample_context *bld,
644 int level)
645 {
646 LLVMValueRef lvl = LLVMConstInt(LLVMInt32Type(), level, 0);
647 return lp_build_get_mipmap_level(bld, lvl);
648 }
649
650
651 /**
652 * Codegen equivalent for u_minify().
653 * Return max(1, base_size >> level);
654 */
655 static LLVMValueRef
656 lp_build_minify(struct lp_build_context *bld,
657 LLVMValueRef base_size,
658 LLVMValueRef level)
659 {
660 assert(lp_check_value(bld->type, base_size));
661 assert(lp_check_value(bld->type, level));
662
663 if (level == bld->zero) {
664 /* if we're using mipmap level zero, no minification is needed */
665 return base_size;
666 }
667 else {
668 LLVMValueRef size =
669 LLVMBuildLShr(bld->builder, base_size, level, "minify");
670 assert(bld->type.sign);
671 size = lp_build_max(bld, size, bld->one);
672 return size;
673 }
674 }
675
676
677 /**
678 * Dereference stride_array[mipmap_level] array to get a stride.
679 * Return stride as a vector.
680 */
681 static LLVMValueRef
682 lp_build_get_level_stride_vec(struct lp_build_sample_context *bld,
683 LLVMValueRef stride_array, LLVMValueRef level)
684 {
685 LLVMValueRef indexes[2], stride;
686 indexes[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
687 indexes[1] = level;
688 stride = LLVMBuildGEP(bld->builder, stride_array, indexes, 2, "");
689 stride = LLVMBuildLoad(bld->builder, stride, "");
690 stride = lp_build_broadcast_scalar(&bld->int_coord_bld, stride);
691 return stride;
692 }
693
694
695 /**
696 * When sampling a mipmap, we need to compute the width, height, depth
697 * of the source levels from the level indexes. This helper function
698 * does that.
699 */
700 void
701 lp_build_mipmap_level_sizes(struct lp_build_sample_context *bld,
702 LLVMValueRef ilevel,
703 LLVMValueRef *out_size,
704 LLVMValueRef *row_stride_vec,
705 LLVMValueRef *img_stride_vec)
706 {
707 const unsigned dims = bld->dims;
708 LLVMValueRef ilevel_vec;
709
710 ilevel_vec = lp_build_broadcast_scalar(&bld->int_size_bld, ilevel);
711
712 /*
713 * Compute width, height, depth at mipmap level 'ilevel'
714 */
715 *out_size = lp_build_minify(&bld->int_size_bld, bld->int_size, ilevel_vec);
716
717 if (dims >= 2) {
718 *row_stride_vec = lp_build_get_level_stride_vec(bld,
719 bld->row_stride_array,
720 ilevel);
721 if (dims == 3 || bld->static_state->target == PIPE_TEXTURE_CUBE) {
722 *img_stride_vec = lp_build_get_level_stride_vec(bld,
723 bld->img_stride_array,
724 ilevel);
725 }
726 }
727 }
728
729
730 /**
731 * Extract and broadcast texture size.
732 *
733 * @param size_type type of the texture size vector (either
734 * bld->int_size_type or bld->float_size_type)
735 * @param coord_type type of the texture size vector (either
736 * bld->int_coord_type or bld->coord_type)
737 * @param int_size vector with the integer texture size (width, height,
738 * depth)
739 */
740 void
741 lp_build_extract_image_sizes(struct lp_build_sample_context *bld,
742 struct lp_type size_type,
743 struct lp_type coord_type,
744 LLVMValueRef size,
745 LLVMValueRef *out_width,
746 LLVMValueRef *out_height,
747 LLVMValueRef *out_depth)
748 {
749 const unsigned dims = bld->dims;
750 LLVMTypeRef i32t = LLVMInt32Type();
751
752 *out_width = lp_build_extract_broadcast(bld->builder,
753 size_type,
754 coord_type,
755 size,
756 LLVMConstInt(i32t, 0, 0));
757 if (dims >= 2) {
758 *out_height = lp_build_extract_broadcast(bld->builder,
759 size_type,
760 coord_type,
761 size,
762 LLVMConstInt(i32t, 1, 0));
763 if (dims == 3) {
764 *out_depth = lp_build_extract_broadcast(bld->builder,
765 size_type,
766 coord_type,
767 size,
768 LLVMConstInt(i32t, 2, 0));
769 }
770 }
771 }
772
773
774 /**
775 * Unnormalize coords.
776 *
777 * @param int_size vector with the integer texture size (width, height, depth)
778 */
779 void
780 lp_build_unnormalized_coords(struct lp_build_sample_context *bld,
781 LLVMValueRef flt_size,
782 LLVMValueRef *s,
783 LLVMValueRef *t,
784 LLVMValueRef *r)
785 {
786 const unsigned dims = bld->dims;
787 LLVMValueRef width;
788 LLVMValueRef height;
789 LLVMValueRef depth;
790
791 lp_build_extract_image_sizes(bld,
792 bld->float_size_type,
793 bld->coord_type,
794 flt_size,
795 &width,
796 &height,
797 &depth);
798
799 /* s = s * width, t = t * height */
800 *s = lp_build_mul(&bld->coord_bld, *s, width);
801 if (dims >= 2) {
802 *t = lp_build_mul(&bld->coord_bld, *t, height);
803 if (dims >= 3) {
804 *r = lp_build_mul(&bld->coord_bld, *r, depth);
805 }
806 }
807 }
808
809
810 /** Helper used by lp_build_cube_lookup() */
811 static LLVMValueRef
812 lp_build_cube_ima(struct lp_build_context *coord_bld, LLVMValueRef coord)
813 {
814 /* ima = -0.5 / abs(coord); */
815 LLVMValueRef negHalf = lp_build_const_vec(coord_bld->type, -0.5);
816 LLVMValueRef absCoord = lp_build_abs(coord_bld, coord);
817 LLVMValueRef ima = lp_build_div(coord_bld, negHalf, absCoord);
818 return ima;
819 }
820
821
822 /**
823 * Helper used by lp_build_cube_lookup()
824 * \param sign scalar +1 or -1
825 * \param coord float vector
826 * \param ima float vector
827 */
828 static LLVMValueRef
829 lp_build_cube_coord(struct lp_build_context *coord_bld,
830 LLVMValueRef sign, int negate_coord,
831 LLVMValueRef coord, LLVMValueRef ima)
832 {
833 /* return negate(coord) * ima * sign + 0.5; */
834 LLVMValueRef half = lp_build_const_vec(coord_bld->type, 0.5);
835 LLVMValueRef res;
836
837 assert(negate_coord == +1 || negate_coord == -1);
838
839 if (negate_coord == -1) {
840 coord = lp_build_negate(coord_bld, coord);
841 }
842
843 res = lp_build_mul(coord_bld, coord, ima);
844 if (sign) {
845 sign = lp_build_broadcast_scalar(coord_bld, sign);
846 res = lp_build_mul(coord_bld, res, sign);
847 }
848 res = lp_build_add(coord_bld, res, half);
849
850 return res;
851 }
852
853
854 /** Helper used by lp_build_cube_lookup()
855 * Return (major_coord >= 0) ? pos_face : neg_face;
856 */
857 static LLVMValueRef
858 lp_build_cube_face(struct lp_build_sample_context *bld,
859 LLVMValueRef major_coord,
860 unsigned pos_face, unsigned neg_face)
861 {
862 LLVMValueRef cmp = LLVMBuildFCmp(bld->builder, LLVMRealUGE,
863 major_coord,
864 bld->float_bld.zero, "");
865 LLVMValueRef pos = LLVMConstInt(LLVMInt32Type(), pos_face, 0);
866 LLVMValueRef neg = LLVMConstInt(LLVMInt32Type(), neg_face, 0);
867 LLVMValueRef res = LLVMBuildSelect(bld->builder, cmp, pos, neg, "");
868 return res;
869 }
870
871
872
873 /**
874 * Generate code to do cube face selection and compute per-face texcoords.
875 */
876 void
877 lp_build_cube_lookup(struct lp_build_sample_context *bld,
878 LLVMValueRef s,
879 LLVMValueRef t,
880 LLVMValueRef r,
881 LLVMValueRef *face,
882 LLVMValueRef *face_s,
883 LLVMValueRef *face_t)
884 {
885 struct lp_build_context *float_bld = &bld->float_bld;
886 struct lp_build_context *coord_bld = &bld->coord_bld;
887 LLVMValueRef rx, ry, rz;
888 LLVMValueRef arx, ary, arz;
889 LLVMValueRef c25 = LLVMConstReal(LLVMFloatType(), 0.25);
890 LLVMValueRef arx_ge_ary, arx_ge_arz;
891 LLVMValueRef ary_ge_arx, ary_ge_arz;
892 LLVMValueRef arx_ge_ary_arz, ary_ge_arx_arz;
893 LLVMValueRef rx_pos, ry_pos, rz_pos;
894
895 assert(bld->coord_bld.type.length == 4);
896
897 /*
898 * Use the average of the four pixel's texcoords to choose the face.
899 */
900 rx = lp_build_mul(float_bld, c25,
901 lp_build_sum_vector(&bld->coord_bld, s));
902 ry = lp_build_mul(float_bld, c25,
903 lp_build_sum_vector(&bld->coord_bld, t));
904 rz = lp_build_mul(float_bld, c25,
905 lp_build_sum_vector(&bld->coord_bld, r));
906
907 arx = lp_build_abs(float_bld, rx);
908 ary = lp_build_abs(float_bld, ry);
909 arz = lp_build_abs(float_bld, rz);
910
911 /*
912 * Compare sign/magnitude of rx,ry,rz to determine face
913 */
914 arx_ge_ary = LLVMBuildFCmp(bld->builder, LLVMRealUGE, arx, ary, "");
915 arx_ge_arz = LLVMBuildFCmp(bld->builder, LLVMRealUGE, arx, arz, "");
916 ary_ge_arx = LLVMBuildFCmp(bld->builder, LLVMRealUGE, ary, arx, "");
917 ary_ge_arz = LLVMBuildFCmp(bld->builder, LLVMRealUGE, ary, arz, "");
918
919 arx_ge_ary_arz = LLVMBuildAnd(bld->builder, arx_ge_ary, arx_ge_arz, "");
920 ary_ge_arx_arz = LLVMBuildAnd(bld->builder, ary_ge_arx, ary_ge_arz, "");
921
922 rx_pos = LLVMBuildFCmp(bld->builder, LLVMRealUGE, rx, float_bld->zero, "");
923 ry_pos = LLVMBuildFCmp(bld->builder, LLVMRealUGE, ry, float_bld->zero, "");
924 rz_pos = LLVMBuildFCmp(bld->builder, LLVMRealUGE, rz, float_bld->zero, "");
925
926 {
927 struct lp_build_if_state if_ctx;
928 LLVMValueRef face_s_var;
929 LLVMValueRef face_t_var;
930 LLVMValueRef face_var;
931
932 face_s_var = lp_build_alloca(bld->builder, bld->coord_bld.vec_type, "face_s_var");
933 face_t_var = lp_build_alloca(bld->builder, bld->coord_bld.vec_type, "face_t_var");
934 face_var = lp_build_alloca(bld->builder, bld->int_bld.vec_type, "face_var");
935
936 lp_build_if(&if_ctx, bld->builder, arx_ge_ary_arz);
937 {
938 /* +/- X face */
939 LLVMValueRef sign = lp_build_sgn(float_bld, rx);
940 LLVMValueRef ima = lp_build_cube_ima(coord_bld, s);
941 *face_s = lp_build_cube_coord(coord_bld, sign, +1, r, ima);
942 *face_t = lp_build_cube_coord(coord_bld, NULL, +1, t, ima);
943 *face = lp_build_cube_face(bld, rx,
944 PIPE_TEX_FACE_POS_X,
945 PIPE_TEX_FACE_NEG_X);
946 LLVMBuildStore(bld->builder, *face_s, face_s_var);
947 LLVMBuildStore(bld->builder, *face_t, face_t_var);
948 LLVMBuildStore(bld->builder, *face, face_var);
949 }
950 lp_build_else(&if_ctx);
951 {
952 struct lp_build_if_state if_ctx2;
953
954 ary_ge_arx_arz = LLVMBuildAnd(bld->builder, ary_ge_arx, ary_ge_arz, "");
955
956 lp_build_if(&if_ctx2, bld->builder, ary_ge_arx_arz);
957 {
958 /* +/- Y face */
959 LLVMValueRef sign = lp_build_sgn(float_bld, ry);
960 LLVMValueRef ima = lp_build_cube_ima(coord_bld, t);
961 *face_s = lp_build_cube_coord(coord_bld, NULL, -1, s, ima);
962 *face_t = lp_build_cube_coord(coord_bld, sign, -1, r, ima);
963 *face = lp_build_cube_face(bld, ry,
964 PIPE_TEX_FACE_POS_Y,
965 PIPE_TEX_FACE_NEG_Y);
966 LLVMBuildStore(bld->builder, *face_s, face_s_var);
967 LLVMBuildStore(bld->builder, *face_t, face_t_var);
968 LLVMBuildStore(bld->builder, *face, face_var);
969 }
970 lp_build_else(&if_ctx2);
971 {
972 /* +/- Z face */
973 LLVMValueRef sign = lp_build_sgn(float_bld, rz);
974 LLVMValueRef ima = lp_build_cube_ima(coord_bld, r);
975 *face_s = lp_build_cube_coord(coord_bld, sign, -1, s, ima);
976 *face_t = lp_build_cube_coord(coord_bld, NULL, +1, t, ima);
977 *face = lp_build_cube_face(bld, rz,
978 PIPE_TEX_FACE_POS_Z,
979 PIPE_TEX_FACE_NEG_Z);
980 LLVMBuildStore(bld->builder, *face_s, face_s_var);
981 LLVMBuildStore(bld->builder, *face_t, face_t_var);
982 LLVMBuildStore(bld->builder, *face, face_var);
983 }
984 lp_build_endif(&if_ctx2);
985 }
986
987 lp_build_endif(&if_ctx);
988
989 *face_s = LLVMBuildLoad(bld->builder, face_s_var, "face_s");
990 *face_t = LLVMBuildLoad(bld->builder, face_t_var, "face_t");
991 *face = LLVMBuildLoad(bld->builder, face_var, "face");
992 }
993 }
994
995
996 /**
997 * Compute the partial offset of a pixel block along an arbitrary axis.
998 *
999 * @param coord coordinate in pixels
1000 * @param stride number of bytes between rows of successive pixel blocks
1001 * @param block_length number of pixels in a pixels block along the coordinate
1002 * axis
1003 * @param out_offset resulting relative offset of the pixel block in bytes
1004 * @param out_subcoord resulting sub-block pixel coordinate
1005 */
1006 void
1007 lp_build_sample_partial_offset(struct lp_build_context *bld,
1008 unsigned block_length,
1009 LLVMValueRef coord,
1010 LLVMValueRef stride,
1011 LLVMValueRef *out_offset,
1012 LLVMValueRef *out_subcoord)
1013 {
1014 LLVMValueRef offset;
1015 LLVMValueRef subcoord;
1016
1017 if (block_length == 1) {
1018 subcoord = bld->zero;
1019 }
1020 else {
1021 /*
1022 * Pixel blocks have power of two dimensions. LLVM should convert the
1023 * rem/div to bit arithmetic.
1024 * TODO: Verify this.
1025 * It does indeed BUT it does transform it to scalar (and back) when doing so
1026 * (using roughly extract, shift/and, mov, unpack) (llvm 2.7).
1027 * The generated code looks seriously unfunny and is quite expensive.
1028 */
1029 #if 0
1030 LLVMValueRef block_width = lp_build_const_int_vec(bld->type, block_length);
1031 subcoord = LLVMBuildURem(bld->builder, coord, block_width, "");
1032 coord = LLVMBuildUDiv(bld->builder, coord, block_width, "");
1033 #else
1034 unsigned logbase2 = util_unsigned_logbase2(block_length);
1035 LLVMValueRef block_shift = lp_build_const_int_vec(bld->type, logbase2);
1036 LLVMValueRef block_mask = lp_build_const_int_vec(bld->type, block_length - 1);
1037 subcoord = LLVMBuildAnd(bld->builder, coord, block_mask, "");
1038 coord = LLVMBuildLShr(bld->builder, coord, block_shift, "");
1039 #endif
1040 }
1041
1042 offset = lp_build_mul(bld, coord, stride);
1043
1044 assert(out_offset);
1045 assert(out_subcoord);
1046
1047 *out_offset = offset;
1048 *out_subcoord = subcoord;
1049 }
1050
1051
1052 /**
1053 * Compute the offset of a pixel block.
1054 *
1055 * x, y, z, y_stride, z_stride are vectors, and they refer to pixels.
1056 *
1057 * Returns the relative offset and i,j sub-block coordinates
1058 */
1059 void
1060 lp_build_sample_offset(struct lp_build_context *bld,
1061 const struct util_format_description *format_desc,
1062 LLVMValueRef x,
1063 LLVMValueRef y,
1064 LLVMValueRef z,
1065 LLVMValueRef y_stride,
1066 LLVMValueRef z_stride,
1067 LLVMValueRef *out_offset,
1068 LLVMValueRef *out_i,
1069 LLVMValueRef *out_j)
1070 {
1071 LLVMValueRef x_stride;
1072 LLVMValueRef offset;
1073
1074 x_stride = lp_build_const_vec(bld->type, format_desc->block.bits/8);
1075
1076 lp_build_sample_partial_offset(bld,
1077 format_desc->block.width,
1078 x, x_stride,
1079 &offset, out_i);
1080
1081 if (y && y_stride) {
1082 LLVMValueRef y_offset;
1083 lp_build_sample_partial_offset(bld,
1084 format_desc->block.height,
1085 y, y_stride,
1086 &y_offset, out_j);
1087 offset = lp_build_add(bld, offset, y_offset);
1088 }
1089 else {
1090 *out_j = bld->zero;
1091 }
1092
1093 if (z && z_stride) {
1094 LLVMValueRef z_offset;
1095 LLVMValueRef k;
1096 lp_build_sample_partial_offset(bld,
1097 1, /* pixel blocks are always 2D */
1098 z, z_stride,
1099 &z_offset, &k);
1100 offset = lp_build_add(bld, offset, z_offset);
1101 }
1102
1103 *out_offset = offset;
1104 }