gallivm,draw,llvmpipe: Support wider native registers.
[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 #include "lp_bld_logic.h"
48 #include "lp_bld_pack.h"
49
50
51 /*
52 * Bri-linear factor. Should be greater than one.
53 */
54 #define BRILINEAR_FACTOR 2
55
56 /**
57 * Does the given texture wrap mode allow sampling the texture border color?
58 * XXX maybe move this into gallium util code.
59 */
60 boolean
61 lp_sampler_wrap_mode_uses_border_color(unsigned mode,
62 unsigned min_img_filter,
63 unsigned mag_img_filter)
64 {
65 switch (mode) {
66 case PIPE_TEX_WRAP_REPEAT:
67 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
68 case PIPE_TEX_WRAP_MIRROR_REPEAT:
69 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
70 return FALSE;
71 case PIPE_TEX_WRAP_CLAMP:
72 case PIPE_TEX_WRAP_MIRROR_CLAMP:
73 if (min_img_filter == PIPE_TEX_FILTER_NEAREST &&
74 mag_img_filter == PIPE_TEX_FILTER_NEAREST) {
75 return FALSE;
76 } else {
77 return TRUE;
78 }
79 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
80 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
81 return TRUE;
82 default:
83 assert(0 && "unexpected wrap mode");
84 return FALSE;
85 }
86 }
87
88
89 /**
90 * Initialize lp_sampler_static_state object with the gallium sampler
91 * and texture state.
92 * The former is considered to be static and the later dynamic.
93 */
94 void
95 lp_sampler_static_state(struct lp_sampler_static_state *state,
96 const struct pipe_sampler_view *view,
97 const struct pipe_sampler_state *sampler)
98 {
99 const struct pipe_resource *texture = view->texture;
100
101 memset(state, 0, sizeof *state);
102
103 if(!texture)
104 return;
105
106 if(!sampler)
107 return;
108
109 /*
110 * We don't copy sampler state over unless it is actually enabled, to avoid
111 * spurious recompiles, as the sampler static state is part of the shader
112 * key.
113 *
114 * Ideally the state tracker or cso_cache module would make all state
115 * canonical, but until that happens it's better to be safe than sorry here.
116 *
117 * XXX: Actually there's much more than can be done here, especially
118 * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
119 */
120
121 state->format = view->format;
122 state->swizzle_r = view->swizzle_r;
123 state->swizzle_g = view->swizzle_g;
124 state->swizzle_b = view->swizzle_b;
125 state->swizzle_a = view->swizzle_a;
126
127 state->target = texture->target;
128 state->pot_width = util_is_power_of_two(texture->width0);
129 state->pot_height = util_is_power_of_two(texture->height0);
130 state->pot_depth = util_is_power_of_two(texture->depth0);
131
132 state->wrap_s = sampler->wrap_s;
133 state->wrap_t = sampler->wrap_t;
134 state->wrap_r = sampler->wrap_r;
135 state->min_img_filter = sampler->min_img_filter;
136 state->mag_img_filter = sampler->mag_img_filter;
137
138 if (view->u.tex.last_level && sampler->max_lod > 0.0f) {
139 state->min_mip_filter = sampler->min_mip_filter;
140 } else {
141 state->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
142 }
143
144 if (state->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
145 if (sampler->lod_bias != 0.0f) {
146 state->lod_bias_non_zero = 1;
147 }
148
149 /* If min_lod == max_lod we can greatly simplify mipmap selection.
150 * This is a case that occurs during automatic mipmap generation.
151 */
152 if (sampler->min_lod == sampler->max_lod) {
153 state->min_max_lod_equal = 1;
154 } else {
155 if (sampler->min_lod > 0.0f) {
156 state->apply_min_lod = 1;
157 }
158
159 if (sampler->max_lod < (float)view->u.tex.last_level) {
160 state->apply_max_lod = 1;
161 }
162 }
163 }
164
165 state->compare_mode = sampler->compare_mode;
166 if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) {
167 state->compare_func = sampler->compare_func;
168 }
169
170 state->normalized_coords = sampler->normalized_coords;
171
172 /*
173 * FIXME: Handle the remainder of pipe_sampler_view.
174 */
175 }
176
177
178 /**
179 * Generate code to compute coordinate gradient (rho).
180 * \param derivs partial derivatives of (s, t, r, q) with respect to X and Y
181 *
182 * The resulting rho is scalar per quad.
183 */
184 static LLVMValueRef
185 lp_build_rho(struct lp_build_sample_context *bld,
186 unsigned unit,
187 const struct lp_derivatives *derivs)
188 {
189 struct gallivm_state *gallivm = bld->gallivm;
190 struct lp_build_context *int_size_bld = &bld->int_size_bld;
191 struct lp_build_context *float_size_bld = &bld->float_size_bld;
192 struct lp_build_context *float_bld = &bld->float_bld;
193 struct lp_build_context *coord_bld = &bld->coord_bld;
194 struct lp_build_context *perquadf_bld = &bld->perquadf_bld;
195 const LLVMValueRef *ddx_ddy = derivs->ddx_ddy;
196 const unsigned dims = bld->dims;
197 LLVMBuilderRef builder = bld->gallivm->builder;
198 LLVMTypeRef i32t = LLVMInt32TypeInContext(bld->gallivm->context);
199 LLVMValueRef index0 = LLVMConstInt(i32t, 0, 0);
200 LLVMValueRef index1 = LLVMConstInt(i32t, 1, 0);
201 LLVMValueRef index2 = LLVMConstInt(i32t, 2, 0);
202 LLVMValueRef rho_vec;
203 LLVMValueRef int_size, float_size;
204 LLVMValueRef rho;
205 LLVMValueRef first_level, first_level_vec;
206 LLVMValueRef abs_ddx_ddy[2];
207 unsigned length = coord_bld->type.length;
208 unsigned num_quads = length / 4;
209 unsigned i;
210 LLVMValueRef i32undef = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
211 LLVMValueRef rho_xvec, rho_yvec;
212
213 abs_ddx_ddy[0] = lp_build_abs(coord_bld, ddx_ddy[0]);
214 if (dims > 2) {
215 abs_ddx_ddy[1] = lp_build_abs(coord_bld, ddx_ddy[1]);
216 }
217
218 if (dims == 1) {
219 static const unsigned char swizzle1[] = {
220 0, LP_BLD_SWIZZLE_DONTCARE,
221 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
222 };
223 static const unsigned char swizzle2[] = {
224 1, LP_BLD_SWIZZLE_DONTCARE,
225 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
226 };
227 rho_xvec = lp_build_swizzle_aos(coord_bld, abs_ddx_ddy[0], swizzle1);
228 rho_yvec = lp_build_swizzle_aos(coord_bld, abs_ddx_ddy[0], swizzle2);
229 }
230 else if (dims == 2) {
231 static const unsigned char swizzle1[] = {
232 0, 2,
233 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
234 };
235 static const unsigned char swizzle2[] = {
236 1, 3,
237 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
238 };
239 rho_xvec = lp_build_swizzle_aos(coord_bld, abs_ddx_ddy[0], swizzle1);
240 rho_yvec = lp_build_swizzle_aos(coord_bld, abs_ddx_ddy[0], swizzle2);
241 }
242 else {
243 LLVMValueRef shuffles1[LP_MAX_VECTOR_LENGTH];
244 LLVMValueRef shuffles2[LP_MAX_VECTOR_LENGTH];
245 assert(dims == 3);
246 for (i = 0; i < num_quads; i++) {
247 shuffles1[4*i + 0] = lp_build_const_int32(gallivm, 4*i);
248 shuffles1[4*i + 1] = lp_build_const_int32(gallivm, 4*i + 2);
249 shuffles1[4*i + 2] = lp_build_const_int32(gallivm, length + 4*i);
250 shuffles1[4*i + 3] = i32undef;
251 shuffles2[4*i + 0] = lp_build_const_int32(gallivm, 4*i + 1);
252 shuffles2[4*i + 1] = lp_build_const_int32(gallivm, 4*i + 3);
253 shuffles2[4*i + 2] = lp_build_const_int32(gallivm, length + 4*i + 1);
254 shuffles2[4*i + 3] = i32undef;
255 }
256 rho_xvec = LLVMBuildShuffleVector(builder, abs_ddx_ddy[0], abs_ddx_ddy[1],
257 LLVMConstVector(shuffles1, length), "");
258 rho_yvec = LLVMBuildShuffleVector(builder, abs_ddx_ddy[0], abs_ddx_ddy[1],
259 LLVMConstVector(shuffles2, length), "");
260 }
261
262 rho_vec = lp_build_max(coord_bld, rho_xvec, rho_yvec);
263
264 first_level = bld->dynamic_state->first_level(bld->dynamic_state,
265 bld->gallivm, unit);
266 first_level_vec = lp_build_broadcast_scalar(&bld->int_size_bld, first_level);
267 int_size = lp_build_minify(int_size_bld, bld->int_size, first_level_vec);
268 float_size = lp_build_int_to_float(float_size_bld, int_size);
269
270 if (bld->coord_type.length > 4) {
271 /* expand size to each quad */
272 if (dims > 1) {
273 /* could use some broadcast_vector helper for this? */
274 int num_quads = bld->coord_type.length / 4;
275 LLVMValueRef src[LP_MAX_VECTOR_LENGTH/4];
276 for (i = 0; i < num_quads; i++) {
277 src[i] = float_size;
278 }
279 float_size = lp_build_concat(bld->gallivm, src, float_size_bld->type, num_quads);
280 }
281 else {
282 float_size = lp_build_broadcast_scalar(coord_bld, float_size);
283 }
284 rho_vec = lp_build_mul(coord_bld, rho_vec, float_size);
285
286 if (dims <= 1) {
287 rho = rho_vec;
288 }
289 else {
290 if (dims >= 2) {
291 static const unsigned char swizzle1[] = {
292 0, LP_BLD_SWIZZLE_DONTCARE,
293 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
294 };
295 static const unsigned char swizzle2[] = {
296 1, LP_BLD_SWIZZLE_DONTCARE,
297 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
298 };
299 LLVMValueRef rho_s, rho_t, rho_r;
300
301 rho_s = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle1);
302 rho_t = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle2);
303
304 rho = lp_build_max(coord_bld, rho_s, rho_t);
305
306 if (dims >= 3) {
307 static const unsigned char swizzle3[] = {
308 2, LP_BLD_SWIZZLE_DONTCARE,
309 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
310 };
311 rho_r = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle3);
312 rho = lp_build_max(coord_bld, rho, rho_r);
313 }
314 }
315 }
316 rho = lp_build_pack_aos_scalars(bld->gallivm, coord_bld->type,
317 perquadf_bld->type, rho);
318 }
319 else {
320 if (dims <= 1) {
321 rho_vec = LLVMBuildExtractElement(builder, rho_vec, index0, "");
322 }
323 rho_vec = lp_build_mul(float_size_bld, rho_vec, float_size);
324
325 if (dims <= 1) {
326 rho = rho_vec;
327 }
328 else {
329 if (dims >= 2) {
330 LLVMValueRef rho_s, rho_t, rho_r;
331
332 rho_s = LLVMBuildExtractElement(builder, rho_vec, index0, "");
333 rho_t = LLVMBuildExtractElement(builder, rho_vec, index1, "");
334
335 rho = lp_build_max(float_bld, rho_s, rho_t);
336
337 if (dims >= 3) {
338 rho_r = LLVMBuildExtractElement(builder, rho_vec, index2, "");
339 rho = lp_build_max(float_bld, rho, rho_r);
340 }
341 }
342 }
343 }
344
345 return rho;
346 }
347
348
349 /*
350 * Bri-linear lod computation
351 *
352 * Use a piece-wise linear approximation of log2 such that:
353 * - round to nearest, for values in the neighborhood of -1, 0, 1, 2, etc.
354 * - linear approximation for values in the neighborhood of 0.5, 1.5., etc,
355 * with the steepness specified in 'factor'
356 * - exact result for 0.5, 1.5, etc.
357 *
358 *
359 * 1.0 - /----*
360 * /
361 * /
362 * /
363 * 0.5 - *
364 * /
365 * /
366 * /
367 * 0.0 - *----/
368 *
369 * | |
370 * 2^0 2^1
371 *
372 * This is a technique also commonly used in hardware:
373 * - http://ixbtlabs.com/articles2/gffx/nv40-rx800-3.html
374 *
375 * TODO: For correctness, this should only be applied when texture is known to
376 * have regular mipmaps, i.e., mipmaps derived from the base level.
377 *
378 * TODO: This could be done in fixed point, where applicable.
379 */
380 static void
381 lp_build_brilinear_lod(struct lp_build_context *bld,
382 LLVMValueRef lod,
383 double factor,
384 LLVMValueRef *out_lod_ipart,
385 LLVMValueRef *out_lod_fpart)
386 {
387 LLVMValueRef lod_fpart;
388 double pre_offset = (factor - 0.5)/factor - 0.5;
389 double post_offset = 1 - factor;
390
391 if (0) {
392 lp_build_printf(bld->gallivm, "lod = %f\n", lod);
393 }
394
395 lod = lp_build_add(bld, lod,
396 lp_build_const_vec(bld->gallivm, bld->type, pre_offset));
397
398 lp_build_ifloor_fract(bld, lod, out_lod_ipart, &lod_fpart);
399
400 lod_fpart = lp_build_mul(bld, lod_fpart,
401 lp_build_const_vec(bld->gallivm, bld->type, factor));
402
403 lod_fpart = lp_build_add(bld, lod_fpart,
404 lp_build_const_vec(bld->gallivm, bld->type, post_offset));
405
406 /*
407 * It's not necessary to clamp lod_fpart since:
408 * - the above expression will never produce numbers greater than one.
409 * - the mip filtering branch is only taken if lod_fpart is positive
410 */
411
412 *out_lod_fpart = lod_fpart;
413
414 if (0) {
415 lp_build_printf(bld->gallivm, "lod_ipart = %i\n", *out_lod_ipart);
416 lp_build_printf(bld->gallivm, "lod_fpart = %f\n\n", *out_lod_fpart);
417 }
418 }
419
420
421 /*
422 * Combined log2 and brilinear lod computation.
423 *
424 * It's in all identical to calling lp_build_fast_log2() and
425 * lp_build_brilinear_lod() above, but by combining we can compute the integer
426 * and fractional part independently.
427 */
428 static void
429 lp_build_brilinear_rho(struct lp_build_context *bld,
430 LLVMValueRef rho,
431 double factor,
432 LLVMValueRef *out_lod_ipart,
433 LLVMValueRef *out_lod_fpart)
434 {
435 LLVMValueRef lod_ipart;
436 LLVMValueRef lod_fpart;
437
438 const double pre_factor = (2*factor - 0.5)/(M_SQRT2*factor);
439 const double post_offset = 1 - 2*factor;
440
441 assert(bld->type.floating);
442
443 assert(lp_check_value(bld->type, rho));
444
445 /*
446 * The pre factor will make the intersections with the exact powers of two
447 * happen precisely where we want then to be, which means that the integer
448 * part will not need any post adjustments.
449 */
450 rho = lp_build_mul(bld, rho,
451 lp_build_const_vec(bld->gallivm, bld->type, pre_factor));
452
453 /* ipart = ifloor(log2(rho)) */
454 lod_ipart = lp_build_extract_exponent(bld, rho, 0);
455
456 /* fpart = rho / 2**ipart */
457 lod_fpart = lp_build_extract_mantissa(bld, rho);
458
459 lod_fpart = lp_build_mul(bld, lod_fpart,
460 lp_build_const_vec(bld->gallivm, bld->type, factor));
461
462 lod_fpart = lp_build_add(bld, lod_fpart,
463 lp_build_const_vec(bld->gallivm, bld->type, post_offset));
464
465 /*
466 * Like lp_build_brilinear_lod, it's not necessary to clamp lod_fpart since:
467 * - the above expression will never produce numbers greater than one.
468 * - the mip filtering branch is only taken if lod_fpart is positive
469 */
470
471 *out_lod_ipart = lod_ipart;
472 *out_lod_fpart = lod_fpart;
473 }
474
475
476 /**
477 * Generate code to compute texture level of detail (lambda).
478 * \param derivs partial derivatives of (s, t, r, q) with respect to X and Y
479 * \param lod_bias optional float vector with the shader lod bias
480 * \param explicit_lod optional float vector with the explicit lod
481 * \param width scalar int texture width
482 * \param height scalar int texture height
483 * \param depth scalar int texture depth
484 *
485 * The resulting lod is scalar per quad, so only the first value per quad
486 * passed in from lod_bias, explicit_lod is used.
487 */
488 void
489 lp_build_lod_selector(struct lp_build_sample_context *bld,
490 unsigned unit,
491 const struct lp_derivatives *derivs,
492 LLVMValueRef lod_bias, /* optional */
493 LLVMValueRef explicit_lod, /* optional */
494 unsigned mip_filter,
495 LLVMValueRef *out_lod_ipart,
496 LLVMValueRef *out_lod_fpart)
497
498 {
499 LLVMBuilderRef builder = bld->gallivm->builder;
500 struct lp_build_context *perquadf_bld = &bld->perquadf_bld;
501 LLVMValueRef lod;
502
503 *out_lod_ipart = bld->perquadi_bld.zero;
504 *out_lod_fpart = perquadf_bld->zero;
505
506 if (bld->static_state->min_max_lod_equal) {
507 /* User is forcing sampling from a particular mipmap level.
508 * This is hit during mipmap generation.
509 */
510 LLVMValueRef min_lod =
511 bld->dynamic_state->min_lod(bld->dynamic_state, bld->gallivm, unit);
512
513 lod = lp_build_broadcast_scalar(perquadf_bld, min_lod);
514 }
515 else {
516 if (explicit_lod) {
517 lod = lp_build_pack_aos_scalars(bld->gallivm, bld->coord_bld.type,
518 perquadf_bld->type, explicit_lod);
519 }
520 else {
521 LLVMValueRef rho;
522
523 rho = lp_build_rho(bld, unit, derivs);
524
525 /*
526 * Compute lod = log2(rho)
527 */
528
529 if (!lod_bias &&
530 !bld->static_state->lod_bias_non_zero &&
531 !bld->static_state->apply_max_lod &&
532 !bld->static_state->apply_min_lod) {
533 /*
534 * Special case when there are no post-log2 adjustments, which
535 * saves instructions but keeping the integer and fractional lod
536 * computations separate from the start.
537 */
538
539 if (mip_filter == PIPE_TEX_MIPFILTER_NONE ||
540 mip_filter == PIPE_TEX_MIPFILTER_NEAREST) {
541 *out_lod_ipart = lp_build_ilog2(perquadf_bld, rho);
542 *out_lod_fpart = perquadf_bld->zero;
543 return;
544 }
545 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR &&
546 !(gallivm_debug & GALLIVM_DEBUG_NO_BRILINEAR)) {
547 lp_build_brilinear_rho(perquadf_bld, rho, BRILINEAR_FACTOR,
548 out_lod_ipart, out_lod_fpart);
549 return;
550 }
551 }
552
553 if (0) {
554 lod = lp_build_log2(perquadf_bld, rho);
555 }
556 else {
557 lod = lp_build_fast_log2(perquadf_bld, rho);
558 }
559
560 /* add shader lod bias */
561 if (lod_bias) {
562 lod_bias = lp_build_pack_aos_scalars(bld->gallivm, bld->coord_bld.type,
563 perquadf_bld->type, lod_bias);
564 lod = LLVMBuildFAdd(builder, lod, lod_bias, "shader_lod_bias");
565 }
566 }
567
568 /* add sampler lod bias */
569 if (bld->static_state->lod_bias_non_zero) {
570 LLVMValueRef sampler_lod_bias =
571 bld->dynamic_state->lod_bias(bld->dynamic_state, bld->gallivm, unit);
572 sampler_lod_bias = lp_build_broadcast_scalar(perquadf_bld,
573 sampler_lod_bias);
574 lod = LLVMBuildFAdd(builder, lod, sampler_lod_bias, "sampler_lod_bias");
575 }
576
577 /* clamp lod */
578 if (bld->static_state->apply_max_lod) {
579 LLVMValueRef max_lod =
580 bld->dynamic_state->max_lod(bld->dynamic_state, bld->gallivm, unit);
581 max_lod = lp_build_broadcast_scalar(perquadf_bld, max_lod);
582
583 lod = lp_build_min(perquadf_bld, lod, max_lod);
584 }
585 if (bld->static_state->apply_min_lod) {
586 LLVMValueRef min_lod =
587 bld->dynamic_state->min_lod(bld->dynamic_state, bld->gallivm, unit);
588 min_lod = lp_build_broadcast_scalar(perquadf_bld, min_lod);
589
590 lod = lp_build_max(perquadf_bld, lod, min_lod);
591 }
592 }
593
594 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
595 if (!(gallivm_debug & GALLIVM_DEBUG_NO_BRILINEAR)) {
596 lp_build_brilinear_lod(perquadf_bld, lod, BRILINEAR_FACTOR,
597 out_lod_ipart, out_lod_fpart);
598 }
599 else {
600 lp_build_ifloor_fract(perquadf_bld, lod, out_lod_ipart, out_lod_fpart);
601 }
602
603 lp_build_name(*out_lod_fpart, "lod_fpart");
604 }
605 else {
606 *out_lod_ipart = lp_build_iround(perquadf_bld, lod);
607 }
608
609 lp_build_name(*out_lod_ipart, "lod_ipart");
610
611 return;
612 }
613
614
615 /**
616 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
617 * mipmap level index.
618 * Note: this is all scalar per quad code.
619 * \param lod_ipart int texture level of detail
620 * \param level_out returns integer
621 */
622 void
623 lp_build_nearest_mip_level(struct lp_build_sample_context *bld,
624 unsigned unit,
625 LLVMValueRef lod_ipart,
626 LLVMValueRef *level_out)
627 {
628 struct lp_build_context *perquadi_bld = &bld->perquadi_bld;
629 LLVMValueRef first_level, last_level, level;
630
631 first_level = bld->dynamic_state->first_level(bld->dynamic_state,
632 bld->gallivm, unit);
633 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
634 bld->gallivm, unit);
635 first_level = lp_build_broadcast_scalar(perquadi_bld, first_level);
636 last_level = lp_build_broadcast_scalar(perquadi_bld, last_level);
637
638 level = lp_build_add(perquadi_bld, lod_ipart, first_level);
639
640 /* clamp level to legal range of levels */
641 *level_out = lp_build_clamp(perquadi_bld, level, first_level, last_level);
642 }
643
644
645 /**
646 * For PIPE_TEX_MIPFILTER_LINEAR, convert per-quad int LOD(s) to two (per-quad)
647 * (adjacent) mipmap level indexes, and fix up float lod part accordingly.
648 * Later, we'll sample from those two mipmap levels and interpolate between them.
649 */
650 void
651 lp_build_linear_mip_levels(struct lp_build_sample_context *bld,
652 unsigned unit,
653 LLVMValueRef lod_ipart,
654 LLVMValueRef *lod_fpart_inout,
655 LLVMValueRef *level0_out,
656 LLVMValueRef *level1_out)
657 {
658 LLVMBuilderRef builder = bld->gallivm->builder;
659 struct lp_build_context *perquadi_bld = &bld->perquadi_bld;
660 struct lp_build_context *perquadf_bld = &bld->perquadf_bld;
661 LLVMValueRef first_level, last_level;
662 LLVMValueRef clamp_min;
663 LLVMValueRef clamp_max;
664
665 first_level = bld->dynamic_state->first_level(bld->dynamic_state,
666 bld->gallivm, unit);
667 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
668 bld->gallivm, unit);
669 first_level = lp_build_broadcast_scalar(perquadi_bld, first_level);
670 last_level = lp_build_broadcast_scalar(perquadi_bld, last_level);
671
672 *level0_out = lp_build_add(perquadi_bld, lod_ipart, first_level);
673 *level1_out = lp_build_add(perquadi_bld, *level0_out, perquadi_bld->one);
674
675 /*
676 * Clamp both *level0_out and *level1_out to [first_level, last_level], with
677 * the minimum number of comparisons, and zeroing lod_fpart in the extreme
678 * ends in the process.
679 */
680
681 /*
682 * This code (vector select in particular) only works with llvm 3.1
683 * (if there's more than one quad, with x86 backend). Might consider
684 * converting to our lp_bld_logic helpers.
685 */
686 #if HAVE_LLVM < 0x0301
687 assert(perquadi_bld->type.length == 1);
688 #endif
689
690 /* *level0_out < first_level */
691 clamp_min = LLVMBuildICmp(builder, LLVMIntSLT,
692 *level0_out, first_level,
693 "clamp_lod_to_first");
694
695 *level0_out = LLVMBuildSelect(builder, clamp_min,
696 first_level, *level0_out, "");
697
698 *level1_out = LLVMBuildSelect(builder, clamp_min,
699 first_level, *level1_out, "");
700
701 *lod_fpart_inout = LLVMBuildSelect(builder, clamp_min,
702 perquadf_bld->zero, *lod_fpart_inout, "");
703
704 /* *level0_out >= last_level */
705 clamp_max = LLVMBuildICmp(builder, LLVMIntSGE,
706 *level0_out, last_level,
707 "clamp_lod_to_last");
708
709 *level0_out = LLVMBuildSelect(builder, clamp_max,
710 last_level, *level0_out, "");
711
712 *level1_out = LLVMBuildSelect(builder, clamp_max,
713 last_level, *level1_out, "");
714
715 *lod_fpart_inout = LLVMBuildSelect(builder, clamp_max,
716 perquadf_bld->zero, *lod_fpart_inout, "");
717
718 lp_build_name(*level0_out, "sampler%u_miplevel0", unit);
719 lp_build_name(*level1_out, "sampler%u_miplevel1", unit);
720 lp_build_name(*lod_fpart_inout, "sampler%u_mipweight", unit);
721 }
722
723
724 /**
725 * Return pointer to a single mipmap level.
726 * \param data_array array of pointers to mipmap levels
727 * \param level integer mipmap level
728 */
729 LLVMValueRef
730 lp_build_get_mipmap_level(struct lp_build_sample_context *bld,
731 LLVMValueRef level)
732 {
733 LLVMBuilderRef builder = bld->gallivm->builder;
734 LLVMValueRef indexes[2], data_ptr;
735
736 indexes[0] = lp_build_const_int32(bld->gallivm, 0);
737 indexes[1] = level;
738 data_ptr = LLVMBuildGEP(builder, bld->data_array, indexes, 2, "");
739 data_ptr = LLVMBuildLoad(builder, data_ptr, "");
740 return data_ptr;
741 }
742
743
744 /**
745 * Codegen equivalent for u_minify().
746 * Return max(1, base_size >> level);
747 */
748 LLVMValueRef
749 lp_build_minify(struct lp_build_context *bld,
750 LLVMValueRef base_size,
751 LLVMValueRef level)
752 {
753 LLVMBuilderRef builder = bld->gallivm->builder;
754 assert(lp_check_value(bld->type, base_size));
755 assert(lp_check_value(bld->type, level));
756
757 if (level == bld->zero) {
758 /* if we're using mipmap level zero, no minification is needed */
759 return base_size;
760 }
761 else {
762 LLVMValueRef size =
763 LLVMBuildLShr(builder, base_size, level, "minify");
764 assert(bld->type.sign);
765 size = lp_build_max(bld, size, bld->one);
766 return size;
767 }
768 }
769
770
771 /**
772 * Dereference stride_array[mipmap_level] array to get a stride.
773 * Return stride as a vector.
774 */
775 static LLVMValueRef
776 lp_build_get_level_stride_vec(struct lp_build_sample_context *bld,
777 LLVMValueRef stride_array, LLVMValueRef level)
778 {
779 LLVMBuilderRef builder = bld->gallivm->builder;
780 LLVMValueRef indexes[2], stride;
781 indexes[0] = lp_build_const_int32(bld->gallivm, 0);
782 indexes[1] = level;
783 stride = LLVMBuildGEP(builder, stride_array, indexes, 2, "");
784 stride = LLVMBuildLoad(builder, stride, "");
785 stride = lp_build_broadcast_scalar(&bld->int_coord_bld, stride);
786 return stride;
787 }
788
789
790 /**
791 * When sampling a mipmap, we need to compute the width, height, depth
792 * of the source levels from the level indexes. This helper function
793 * does that.
794 */
795 void
796 lp_build_mipmap_level_sizes(struct lp_build_sample_context *bld,
797 LLVMValueRef ilevel,
798 LLVMValueRef *out_size,
799 LLVMValueRef *row_stride_vec,
800 LLVMValueRef *img_stride_vec)
801 {
802 const unsigned dims = bld->dims;
803 LLVMValueRef ilevel_vec;
804
805 ilevel_vec = lp_build_broadcast_scalar(&bld->int_size_bld, ilevel);
806
807 /*
808 * Compute width, height, depth at mipmap level 'ilevel'
809 */
810 *out_size = lp_build_minify(&bld->int_size_bld, bld->int_size, ilevel_vec);
811
812 if (dims >= 2) {
813 *row_stride_vec = lp_build_get_level_stride_vec(bld,
814 bld->row_stride_array,
815 ilevel);
816 if (dims == 3 || bld->static_state->target == PIPE_TEXTURE_CUBE) {
817 *img_stride_vec = lp_build_get_level_stride_vec(bld,
818 bld->img_stride_array,
819 ilevel);
820 }
821 }
822 }
823
824
825 /**
826 * Extract and broadcast texture size.
827 *
828 * @param size_type type of the texture size vector (either
829 * bld->int_size_type or bld->float_size_type)
830 * @param coord_type type of the texture size vector (either
831 * bld->int_coord_type or bld->coord_type)
832 * @param size vector with the texture size (width, height, depth)
833 */
834 void
835 lp_build_extract_image_sizes(struct lp_build_sample_context *bld,
836 struct lp_type size_type,
837 struct lp_type coord_type,
838 LLVMValueRef size,
839 LLVMValueRef *out_width,
840 LLVMValueRef *out_height,
841 LLVMValueRef *out_depth)
842 {
843 const unsigned dims = bld->dims;
844 LLVMTypeRef i32t = LLVMInt32TypeInContext(bld->gallivm->context);
845
846 *out_width = lp_build_extract_broadcast(bld->gallivm,
847 size_type,
848 coord_type,
849 size,
850 LLVMConstInt(i32t, 0, 0));
851 if (dims >= 2) {
852 *out_height = lp_build_extract_broadcast(bld->gallivm,
853 size_type,
854 coord_type,
855 size,
856 LLVMConstInt(i32t, 1, 0));
857 if (dims == 3) {
858 *out_depth = lp_build_extract_broadcast(bld->gallivm,
859 size_type,
860 coord_type,
861 size,
862 LLVMConstInt(i32t, 2, 0));
863 }
864 }
865 }
866
867
868 /**
869 * Unnormalize coords.
870 *
871 * @param flt_size vector with the integer texture size (width, height, depth)
872 */
873 void
874 lp_build_unnormalized_coords(struct lp_build_sample_context *bld,
875 LLVMValueRef flt_size,
876 LLVMValueRef *s,
877 LLVMValueRef *t,
878 LLVMValueRef *r)
879 {
880 const unsigned dims = bld->dims;
881 LLVMValueRef width;
882 LLVMValueRef height;
883 LLVMValueRef depth;
884
885 lp_build_extract_image_sizes(bld,
886 bld->float_size_type,
887 bld->coord_type,
888 flt_size,
889 &width,
890 &height,
891 &depth);
892
893 /* s = s * width, t = t * height */
894 *s = lp_build_mul(&bld->coord_bld, *s, width);
895 if (dims >= 2) {
896 *t = lp_build_mul(&bld->coord_bld, *t, height);
897 if (dims >= 3) {
898 *r = lp_build_mul(&bld->coord_bld, *r, depth);
899 }
900 }
901 }
902
903
904 /** Helper used by lp_build_cube_lookup() */
905 static LLVMValueRef
906 lp_build_cube_imapos(struct lp_build_context *coord_bld, LLVMValueRef coord)
907 {
908 /* ima = +0.5 / abs(coord); */
909 LLVMValueRef posHalf = lp_build_const_vec(coord_bld->gallivm, coord_bld->type, 0.5);
910 LLVMValueRef absCoord = lp_build_abs(coord_bld, coord);
911 LLVMValueRef ima = lp_build_div(coord_bld, posHalf, absCoord);
912 return ima;
913 }
914
915 /** Helper used by lp_build_cube_lookup() */
916 static LLVMValueRef
917 lp_build_cube_imaneg(struct lp_build_context *coord_bld, LLVMValueRef coord)
918 {
919 /* ima = -0.5 / abs(coord); */
920 LLVMValueRef negHalf = lp_build_const_vec(coord_bld->gallivm, coord_bld->type, -0.5);
921 LLVMValueRef absCoord = lp_build_abs(coord_bld, coord);
922 LLVMValueRef ima = lp_build_div(coord_bld, negHalf, absCoord);
923 return ima;
924 }
925
926 /**
927 * Helper used by lp_build_cube_lookup()
928 * FIXME: the sign here can also be 0.
929 * Arithmetically this could definitely make a difference. Either
930 * fix the comment or use other (simpler) sign function, not sure
931 * which one it should be.
932 * \param sign scalar +1 or -1
933 * \param coord float vector
934 * \param ima float vector
935 */
936 static LLVMValueRef
937 lp_build_cube_coord(struct lp_build_context *coord_bld,
938 LLVMValueRef sign, int negate_coord,
939 LLVMValueRef coord, LLVMValueRef ima)
940 {
941 /* return negate(coord) * ima * sign + 0.5; */
942 LLVMValueRef half = lp_build_const_vec(coord_bld->gallivm, coord_bld->type, 0.5);
943 LLVMValueRef res;
944
945 assert(negate_coord == +1 || negate_coord == -1);
946
947 if (negate_coord == -1) {
948 coord = lp_build_negate(coord_bld, coord);
949 }
950
951 res = lp_build_mul(coord_bld, coord, ima);
952 if (sign) {
953 sign = lp_build_broadcast_scalar(coord_bld, sign);
954 res = lp_build_mul(coord_bld, res, sign);
955 }
956 res = lp_build_add(coord_bld, res, half);
957
958 return res;
959 }
960
961
962 /** Helper used by lp_build_cube_lookup()
963 * Return (major_coord >= 0) ? pos_face : neg_face;
964 */
965 static LLVMValueRef
966 lp_build_cube_face(struct lp_build_sample_context *bld,
967 LLVMValueRef major_coord,
968 unsigned pos_face, unsigned neg_face)
969 {
970 struct gallivm_state *gallivm = bld->gallivm;
971 LLVMBuilderRef builder = gallivm->builder;
972 LLVMValueRef cmp = LLVMBuildFCmp(builder, LLVMRealUGE,
973 major_coord,
974 bld->float_bld.zero, "");
975 LLVMValueRef pos = lp_build_const_int32(gallivm, pos_face);
976 LLVMValueRef neg = lp_build_const_int32(gallivm, neg_face);
977 LLVMValueRef res = LLVMBuildSelect(builder, cmp, pos, neg, "");
978 return res;
979 }
980
981
982
983 /**
984 * Generate code to do cube face selection and compute per-face texcoords.
985 */
986 void
987 lp_build_cube_lookup(struct lp_build_sample_context *bld,
988 LLVMValueRef s,
989 LLVMValueRef t,
990 LLVMValueRef r,
991 LLVMValueRef *face,
992 LLVMValueRef *face_s,
993 LLVMValueRef *face_t)
994 {
995 struct lp_build_context *coord_bld = &bld->coord_bld;
996 LLVMBuilderRef builder = bld->gallivm->builder;
997 struct gallivm_state *gallivm = bld->gallivm;
998 LLVMValueRef rx, ry, rz;
999 LLVMValueRef tmp[4], rxyz, arxyz;
1000
1001 /*
1002 * Use the average of the four pixel's texcoords to choose the face.
1003 * Slight simplification just calculate the sum, skip scaling.
1004 */
1005 tmp[0] = s;
1006 tmp[1] = t;
1007 tmp[2] = r;
1008 rxyz = lp_build_hadd_partial4(&bld->coord_bld, tmp, 3);
1009 arxyz = lp_build_abs(&bld->coord_bld, rxyz);
1010
1011 if (coord_bld->type.length > 4) {
1012 struct lp_build_context *cint_bld = &bld->int_coord_bld;
1013 struct lp_type intctype = cint_bld->type;
1014 LLVMValueRef signrxs, signrys, signrzs, signrxyz, sign;
1015 LLVMValueRef arxs, arys, arzs;
1016 LLVMValueRef arx_ge_ary, maxarxsarys, arz_ge_arx_ary;
1017 LLVMValueRef snewx, tnewx, snewy, tnewy, snewz, tnewz;
1018 LLVMValueRef ryneg, rzneg;
1019 LLVMValueRef ma, ima;
1020 LLVMValueRef posHalf = lp_build_const_vec(gallivm, coord_bld->type, 0.5);
1021 LLVMValueRef signmask = lp_build_const_int_vec(gallivm, intctype,
1022 1 << (intctype.width - 1));
1023 LLVMValueRef signshift = lp_build_const_int_vec(gallivm, intctype,
1024 intctype.width -1);
1025 LLVMValueRef facex = lp_build_const_int_vec(gallivm, intctype, PIPE_TEX_FACE_POS_X);
1026 LLVMValueRef facey = lp_build_const_int_vec(gallivm, intctype, PIPE_TEX_FACE_POS_Y);
1027 LLVMValueRef facez = lp_build_const_int_vec(gallivm, intctype, PIPE_TEX_FACE_POS_Z);
1028
1029 assert(PIPE_TEX_FACE_NEG_X == PIPE_TEX_FACE_POS_X + 1);
1030 assert(PIPE_TEX_FACE_NEG_Y == PIPE_TEX_FACE_POS_Y + 1);
1031 assert(PIPE_TEX_FACE_NEG_Z == PIPE_TEX_FACE_POS_Z + 1);
1032
1033 rx = LLVMBuildBitCast(builder, s, lp_build_vec_type(gallivm, intctype), "");
1034 ry = LLVMBuildBitCast(builder, t, lp_build_vec_type(gallivm, intctype), "");
1035 rz = LLVMBuildBitCast(builder, r, lp_build_vec_type(gallivm, intctype), "");
1036 ryneg = LLVMBuildXor(builder, ry, signmask, "");
1037 rzneg = LLVMBuildXor(builder, rz, signmask, "");
1038
1039 /* the sign bit comes from the averaged vector (per quad),
1040 * as does the decision which face to use */
1041 signrxyz = LLVMBuildBitCast(builder, rxyz, lp_build_vec_type(gallivm, intctype), "");
1042 signrxyz = LLVMBuildAnd(builder, signrxyz, signmask, "");
1043
1044 arxs = lp_build_swizzle_scalar_aos(coord_bld, arxyz, 0);
1045 arys = lp_build_swizzle_scalar_aos(coord_bld, arxyz, 1);
1046 arzs = lp_build_swizzle_scalar_aos(coord_bld, arxyz, 2);
1047
1048 /*
1049 * select x if x >= y else select y
1050 * select previous result if y >= max(x,y) else select z
1051 */
1052 arx_ge_ary = lp_build_cmp(coord_bld, PIPE_FUNC_GEQUAL, arxs, arys);
1053 maxarxsarys = lp_build_max(coord_bld, arxs, arys);
1054 arz_ge_arx_ary = lp_build_cmp(coord_bld, PIPE_FUNC_GEQUAL, maxarxsarys, arzs);
1055
1056 /*
1057 * compute all possible new s/t coords
1058 * snewx = signrx * -rz;
1059 * tnewx = -ry;
1060 * snewy = rx;
1061 * tnewy = signry * rz;
1062 * snewz = signrz * rx;
1063 * tnewz = -ry;
1064 */
1065 signrxs = lp_build_swizzle_scalar_aos(cint_bld, signrxyz, 0);
1066 snewx = LLVMBuildXor(builder, signrxs, rzneg, "");
1067 tnewx = ryneg;
1068
1069 signrys = lp_build_swizzle_scalar_aos(cint_bld, signrxyz, 1);
1070 snewy = rx;
1071 tnewy = LLVMBuildXor(builder, signrys, rz, "");
1072
1073 signrzs = lp_build_swizzle_scalar_aos(cint_bld, signrxyz, 2);
1074 snewz = LLVMBuildXor(builder, signrzs, rx, "");
1075 tnewz = ryneg;
1076
1077 /* XXX on x86 unclear if we should cast the values back to float
1078 * or not - on some cpus (nehalem) pblendvb has twice the throughput
1079 * of blendvps though on others there just might be domain
1080 * transition penalties when using it (this depends on what llvm
1081 * will chose for the bit ops above so there appears no "right way",
1082 * but given the boatload of selects let's just use the int type).
1083 *
1084 * Unfortunately we also need the sign bit of the summed coords.
1085 */
1086 *face_s = lp_build_select(cint_bld, arx_ge_ary, snewx, snewy);
1087 *face_t = lp_build_select(cint_bld, arx_ge_ary, tnewx, tnewy);
1088 ma = lp_build_select(coord_bld, arx_ge_ary, s, t);
1089 *face = lp_build_select(cint_bld, arx_ge_ary, facex, facey);
1090 sign = lp_build_select(cint_bld, arx_ge_ary, signrxs, signrys);
1091
1092 *face_s = lp_build_select(cint_bld, arz_ge_arx_ary, *face_s, snewz);
1093 *face_t = lp_build_select(cint_bld, arz_ge_arx_ary, *face_t, tnewz);
1094 ma = lp_build_select(coord_bld, arz_ge_arx_ary, ma, r);
1095 *face = lp_build_select(cint_bld, arz_ge_arx_ary, *face, facez);
1096 sign = lp_build_select(cint_bld, arz_ge_arx_ary, sign, signrzs);
1097
1098 *face_s = LLVMBuildBitCast(builder, *face_s,
1099 lp_build_vec_type(gallivm, coord_bld->type), "");
1100 *face_t = LLVMBuildBitCast(builder, *face_t,
1101 lp_build_vec_type(gallivm, coord_bld->type), "");
1102
1103 /* add +1 for neg face */
1104 /* XXX with AVX probably want to use another select here -
1105 * as long as we ensure vblendvps gets used we can actually
1106 * skip the comparison and just use sign as a "mask" directly.
1107 */
1108 sign = LLVMBuildLShr(builder, sign, signshift, "");
1109 *face = LLVMBuildOr(builder, *face, sign, "face");
1110
1111 ima = lp_build_cube_imapos(coord_bld, ma);
1112
1113 *face_s = lp_build_mul(coord_bld, *face_s, ima);
1114 *face_s = lp_build_add(coord_bld, *face_s, posHalf);
1115 *face_t = lp_build_mul(coord_bld, *face_t, ima);
1116 *face_t = lp_build_add(coord_bld, *face_t, posHalf);
1117 }
1118
1119 else {
1120 struct lp_build_if_state if_ctx;
1121 LLVMValueRef face_s_var;
1122 LLVMValueRef face_t_var;
1123 LLVMValueRef face_var;
1124 LLVMValueRef arx_ge_ary_arz, ary_ge_arx_arz;
1125 LLVMValueRef shuffles[4];
1126 LLVMValueRef arxy_ge_aryx, arxy_ge_arzz, arxy_ge_arxy_arzz;
1127 LLVMValueRef arxyxy, aryxzz, arxyxy_ge_aryxzz;
1128 struct lp_build_context *float_bld = &bld->float_bld;
1129
1130 assert(bld->coord_bld.type.length == 4);
1131
1132 shuffles[0] = lp_build_const_int32(gallivm, 0);
1133 shuffles[1] = lp_build_const_int32(gallivm, 1);
1134 shuffles[2] = lp_build_const_int32(gallivm, 0);
1135 shuffles[3] = lp_build_const_int32(gallivm, 1);
1136 arxyxy = LLVMBuildShuffleVector(builder, arxyz, arxyz, LLVMConstVector(shuffles, 4), "");
1137 shuffles[0] = lp_build_const_int32(gallivm, 1);
1138 shuffles[1] = lp_build_const_int32(gallivm, 0);
1139 shuffles[2] = lp_build_const_int32(gallivm, 2);
1140 shuffles[3] = lp_build_const_int32(gallivm, 2);
1141 aryxzz = LLVMBuildShuffleVector(builder, arxyz, arxyz, LLVMConstVector(shuffles, 4), "");
1142 arxyxy_ge_aryxzz = lp_build_cmp(&bld->coord_bld, PIPE_FUNC_GEQUAL, arxyxy, aryxzz);
1143
1144 shuffles[0] = lp_build_const_int32(gallivm, 0);
1145 shuffles[1] = lp_build_const_int32(gallivm, 1);
1146 arxy_ge_aryx = LLVMBuildShuffleVector(builder, arxyxy_ge_aryxzz, arxyxy_ge_aryxzz,
1147 LLVMConstVector(shuffles, 2), "");
1148 shuffles[0] = lp_build_const_int32(gallivm, 2);
1149 shuffles[1] = lp_build_const_int32(gallivm, 3);
1150 arxy_ge_arzz = LLVMBuildShuffleVector(builder, arxyxy_ge_aryxzz, arxyxy_ge_aryxzz,
1151 LLVMConstVector(shuffles, 2), "");
1152 arxy_ge_arxy_arzz = LLVMBuildAnd(builder, arxy_ge_aryx, arxy_ge_arzz, "");
1153
1154 arx_ge_ary_arz = LLVMBuildExtractElement(builder, arxy_ge_arxy_arzz,
1155 lp_build_const_int32(gallivm, 0), "");
1156 arx_ge_ary_arz = LLVMBuildICmp(builder, LLVMIntNE, arx_ge_ary_arz,
1157 lp_build_const_int32(gallivm, 0), "");
1158 ary_ge_arx_arz = LLVMBuildExtractElement(builder, arxy_ge_arxy_arzz,
1159 lp_build_const_int32(gallivm, 1), "");
1160 ary_ge_arx_arz = LLVMBuildICmp(builder, LLVMIntNE, ary_ge_arx_arz,
1161 lp_build_const_int32(gallivm, 0), "");
1162 face_s_var = lp_build_alloca(gallivm, bld->coord_bld.vec_type, "face_s_var");
1163 face_t_var = lp_build_alloca(gallivm, bld->coord_bld.vec_type, "face_t_var");
1164 face_var = lp_build_alloca(gallivm, bld->int_bld.vec_type, "face_var");
1165
1166 lp_build_if(&if_ctx, gallivm, arx_ge_ary_arz);
1167 {
1168 /* +/- X face */
1169 LLVMValueRef sign, ima;
1170 rx = LLVMBuildExtractElement(builder, rxyz,
1171 lp_build_const_int32(gallivm, 0), "");
1172 /* +/- X face */
1173 sign = lp_build_sgn(float_bld, rx);
1174 ima = lp_build_cube_imaneg(coord_bld, s);
1175 *face_s = lp_build_cube_coord(coord_bld, sign, +1, r, ima);
1176 *face_t = lp_build_cube_coord(coord_bld, NULL, +1, t, ima);
1177 *face = lp_build_cube_face(bld, rx,
1178 PIPE_TEX_FACE_POS_X,
1179 PIPE_TEX_FACE_NEG_X);
1180 LLVMBuildStore(builder, *face_s, face_s_var);
1181 LLVMBuildStore(builder, *face_t, face_t_var);
1182 LLVMBuildStore(builder, *face, face_var);
1183 }
1184 lp_build_else(&if_ctx);
1185 {
1186 struct lp_build_if_state if_ctx2;
1187
1188 lp_build_if(&if_ctx2, gallivm, ary_ge_arx_arz);
1189 {
1190 LLVMValueRef sign, ima;
1191 /* +/- Y face */
1192 ry = LLVMBuildExtractElement(builder, rxyz,
1193 lp_build_const_int32(gallivm, 1), "");
1194 sign = lp_build_sgn(float_bld, ry);
1195 ima = lp_build_cube_imaneg(coord_bld, t);
1196 *face_s = lp_build_cube_coord(coord_bld, NULL, -1, s, ima);
1197 *face_t = lp_build_cube_coord(coord_bld, sign, -1, r, ima);
1198 *face = lp_build_cube_face(bld, ry,
1199 PIPE_TEX_FACE_POS_Y,
1200 PIPE_TEX_FACE_NEG_Y);
1201 LLVMBuildStore(builder, *face_s, face_s_var);
1202 LLVMBuildStore(builder, *face_t, face_t_var);
1203 LLVMBuildStore(builder, *face, face_var);
1204 }
1205 lp_build_else(&if_ctx2);
1206 {
1207 /* +/- Z face */
1208 LLVMValueRef sign, ima;
1209 rz = LLVMBuildExtractElement(builder, rxyz,
1210 lp_build_const_int32(gallivm, 2), "");
1211 sign = lp_build_sgn(float_bld, rz);
1212 ima = lp_build_cube_imaneg(coord_bld, r);
1213 *face_s = lp_build_cube_coord(coord_bld, sign, -1, s, ima);
1214 *face_t = lp_build_cube_coord(coord_bld, NULL, +1, t, ima);
1215 *face = lp_build_cube_face(bld, rz,
1216 PIPE_TEX_FACE_POS_Z,
1217 PIPE_TEX_FACE_NEG_Z);
1218 LLVMBuildStore(builder, *face_s, face_s_var);
1219 LLVMBuildStore(builder, *face_t, face_t_var);
1220 LLVMBuildStore(builder, *face, face_var);
1221 }
1222 lp_build_endif(&if_ctx2);
1223 }
1224
1225 lp_build_endif(&if_ctx);
1226
1227 *face_s = LLVMBuildLoad(builder, face_s_var, "face_s");
1228 *face_t = LLVMBuildLoad(builder, face_t_var, "face_t");
1229 *face = LLVMBuildLoad(builder, face_var, "face");
1230 *face = lp_build_broadcast_scalar(&bld->int_coord_bld, *face);
1231 }
1232 }
1233
1234
1235 /**
1236 * Compute the partial offset of a pixel block along an arbitrary axis.
1237 *
1238 * @param coord coordinate in pixels
1239 * @param stride number of bytes between rows of successive pixel blocks
1240 * @param block_length number of pixels in a pixels block along the coordinate
1241 * axis
1242 * @param out_offset resulting relative offset of the pixel block in bytes
1243 * @param out_subcoord resulting sub-block pixel coordinate
1244 */
1245 void
1246 lp_build_sample_partial_offset(struct lp_build_context *bld,
1247 unsigned block_length,
1248 LLVMValueRef coord,
1249 LLVMValueRef stride,
1250 LLVMValueRef *out_offset,
1251 LLVMValueRef *out_subcoord)
1252 {
1253 LLVMBuilderRef builder = bld->gallivm->builder;
1254 LLVMValueRef offset;
1255 LLVMValueRef subcoord;
1256
1257 if (block_length == 1) {
1258 subcoord = bld->zero;
1259 }
1260 else {
1261 /*
1262 * Pixel blocks have power of two dimensions. LLVM should convert the
1263 * rem/div to bit arithmetic.
1264 * TODO: Verify this.
1265 * It does indeed BUT it does transform it to scalar (and back) when doing so
1266 * (using roughly extract, shift/and, mov, unpack) (llvm 2.7).
1267 * The generated code looks seriously unfunny and is quite expensive.
1268 */
1269 #if 0
1270 LLVMValueRef block_width = lp_build_const_int_vec(bld->type, block_length);
1271 subcoord = LLVMBuildURem(builder, coord, block_width, "");
1272 coord = LLVMBuildUDiv(builder, coord, block_width, "");
1273 #else
1274 unsigned logbase2 = util_logbase2(block_length);
1275 LLVMValueRef block_shift = lp_build_const_int_vec(bld->gallivm, bld->type, logbase2);
1276 LLVMValueRef block_mask = lp_build_const_int_vec(bld->gallivm, bld->type, block_length - 1);
1277 subcoord = LLVMBuildAnd(builder, coord, block_mask, "");
1278 coord = LLVMBuildLShr(builder, coord, block_shift, "");
1279 #endif
1280 }
1281
1282 offset = lp_build_mul(bld, coord, stride);
1283
1284 assert(out_offset);
1285 assert(out_subcoord);
1286
1287 *out_offset = offset;
1288 *out_subcoord = subcoord;
1289 }
1290
1291
1292 /**
1293 * Compute the offset of a pixel block.
1294 *
1295 * x, y, z, y_stride, z_stride are vectors, and they refer to pixels.
1296 *
1297 * Returns the relative offset and i,j sub-block coordinates
1298 */
1299 void
1300 lp_build_sample_offset(struct lp_build_context *bld,
1301 const struct util_format_description *format_desc,
1302 LLVMValueRef x,
1303 LLVMValueRef y,
1304 LLVMValueRef z,
1305 LLVMValueRef y_stride,
1306 LLVMValueRef z_stride,
1307 LLVMValueRef *out_offset,
1308 LLVMValueRef *out_i,
1309 LLVMValueRef *out_j)
1310 {
1311 LLVMValueRef x_stride;
1312 LLVMValueRef offset;
1313
1314 x_stride = lp_build_const_vec(bld->gallivm, bld->type,
1315 format_desc->block.bits/8);
1316
1317 lp_build_sample_partial_offset(bld,
1318 format_desc->block.width,
1319 x, x_stride,
1320 &offset, out_i);
1321
1322 if (y && y_stride) {
1323 LLVMValueRef y_offset;
1324 lp_build_sample_partial_offset(bld,
1325 format_desc->block.height,
1326 y, y_stride,
1327 &y_offset, out_j);
1328 offset = lp_build_add(bld, offset, y_offset);
1329 }
1330 else {
1331 *out_j = bld->zero;
1332 }
1333
1334 if (z && z_stride) {
1335 LLVMValueRef z_offset;
1336 LLVMValueRef k;
1337 lp_build_sample_partial_offset(bld,
1338 1, /* pixel blocks are always 2D */
1339 z, z_stride,
1340 &z_offset, &k);
1341 offset = lp_build_add(bld, offset, z_offset);
1342 }
1343
1344 *out_offset = offset;
1345 }