gallivm: Only apply min/max_lod when necessary.
[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_flow.h"
43 #include "lp_bld_sample.h"
44 #include "lp_bld_swizzle.h"
45 #include "lp_bld_type.h"
46
47
48 /**
49 * Does the given texture wrap mode allow sampling the texture border color?
50 * XXX maybe move this into gallium util code.
51 */
52 boolean
53 lp_sampler_wrap_mode_uses_border_color(unsigned mode,
54 unsigned min_img_filter,
55 unsigned mag_img_filter)
56 {
57 switch (mode) {
58 case PIPE_TEX_WRAP_REPEAT:
59 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
60 case PIPE_TEX_WRAP_MIRROR_REPEAT:
61 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
62 return FALSE;
63 case PIPE_TEX_WRAP_CLAMP:
64 case PIPE_TEX_WRAP_MIRROR_CLAMP:
65 if (min_img_filter == PIPE_TEX_FILTER_NEAREST &&
66 mag_img_filter == PIPE_TEX_FILTER_NEAREST) {
67 return FALSE;
68 } else {
69 return TRUE;
70 }
71 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
72 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
73 return TRUE;
74 default:
75 assert(0 && "unexpected wrap mode");
76 return FALSE;
77 }
78 }
79
80
81 /**
82 * Initialize lp_sampler_static_state object with the gallium sampler
83 * and texture state.
84 * The former is considered to be static and the later dynamic.
85 */
86 void
87 lp_sampler_static_state(struct lp_sampler_static_state *state,
88 const struct pipe_sampler_view *view,
89 const struct pipe_sampler_state *sampler)
90 {
91 const struct pipe_resource *texture = view->texture;
92
93 memset(state, 0, sizeof *state);
94
95 if(!texture)
96 return;
97
98 if(!sampler)
99 return;
100
101 /*
102 * We don't copy sampler state over unless it is actually enabled, to avoid
103 * spurious recompiles, as the sampler static state is part of the shader
104 * key.
105 *
106 * Ideally the state tracker or cso_cache module would make all state
107 * canonical, but until that happens it's better to be safe than sorry here.
108 *
109 * XXX: Actually there's much more than can be done here, especially
110 * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
111 */
112
113 state->format = view->format;
114 state->swizzle_r = view->swizzle_r;
115 state->swizzle_g = view->swizzle_g;
116 state->swizzle_b = view->swizzle_b;
117 state->swizzle_a = view->swizzle_a;
118
119 state->target = texture->target;
120 state->pot_width = util_is_power_of_two(texture->width0);
121 state->pot_height = util_is_power_of_two(texture->height0);
122 state->pot_depth = util_is_power_of_two(texture->depth0);
123
124 state->wrap_s = sampler->wrap_s;
125 state->wrap_t = sampler->wrap_t;
126 state->wrap_r = sampler->wrap_r;
127 state->min_img_filter = sampler->min_img_filter;
128 state->mag_img_filter = sampler->mag_img_filter;
129
130 if (view->last_level && sampler->max_lod > 0.0f) {
131 state->min_mip_filter = sampler->min_mip_filter;
132 } else {
133 state->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
134 }
135
136 if (state->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
137 if (sampler->lod_bias != 0.0f) {
138 state->lod_bias_non_zero = 1;
139 }
140
141 /* If min_lod == max_lod we can greatly simplify mipmap selection.
142 * This is a case that occurs during automatic mipmap generation.
143 */
144 if (sampler->min_lod == sampler->max_lod) {
145 state->min_max_lod_equal = 1;
146 } else {
147 if (sampler->min_lod > 0.0f) {
148 state->apply_min_lod = 1;
149 }
150
151 if (sampler->max_lod < (float)view->last_level) {
152 state->apply_max_lod = 1;
153 }
154 }
155 }
156
157 state->compare_mode = sampler->compare_mode;
158 if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) {
159 state->compare_func = sampler->compare_func;
160 }
161
162 state->normalized_coords = sampler->normalized_coords;
163
164 /*
165 * FIXME: Handle the remainder of pipe_sampler_view.
166 */
167 }
168
169
170 /**
171 * Generate code to compute texture level of detail (lambda).
172 * \param ddx partial derivatives of (s, t, r, q) with respect to X
173 * \param ddy partial derivatives of (s, t, r, q) with respect to Y
174 * \param lod_bias optional float vector with the shader lod bias
175 * \param explicit_lod optional float vector with the explicit lod
176 * \param width scalar int texture width
177 * \param height scalar int texture height
178 * \param depth scalar int texture depth
179 *
180 * XXX: The resulting lod is scalar, so ignore all but the first element of
181 * derivatives, lod_bias, etc that are passed by the shader.
182 */
183 LLVMValueRef
184 lp_build_lod_selector(struct lp_build_sample_context *bld,
185 unsigned unit,
186 const LLVMValueRef ddx[4],
187 const LLVMValueRef ddy[4],
188 LLVMValueRef lod_bias, /* optional */
189 LLVMValueRef explicit_lod, /* optional */
190 LLVMValueRef width,
191 LLVMValueRef height,
192 LLVMValueRef depth)
193
194 {
195 if (bld->static_state->min_max_lod_equal) {
196 /* User is forcing sampling from a particular mipmap level.
197 * This is hit during mipmap generation.
198 */
199 LLVMValueRef min_lod =
200 bld->dynamic_state->min_lod(bld->dynamic_state, bld->builder, unit);
201
202 return min_lod;
203 }
204 else {
205 struct lp_build_context *float_bld = &bld->float_bld;
206 LLVMValueRef sampler_lod_bias =
207 bld->dynamic_state->lod_bias(bld->dynamic_state, bld->builder, unit);
208 LLVMValueRef index0 = LLVMConstInt(LLVMInt32Type(), 0, 0);
209 LLVMValueRef lod;
210
211 if (explicit_lod) {
212 lod = LLVMBuildExtractElement(bld->builder, explicit_lod,
213 index0, "");
214 }
215 else {
216 const int dims = texture_dims(bld->static_state->target);
217 LLVMValueRef dsdx, dsdy;
218 LLVMValueRef dtdx = NULL, dtdy = NULL, drdx = NULL, drdy = NULL;
219 LLVMValueRef rho;
220
221 dsdx = LLVMBuildExtractElement(bld->builder, ddx[0], index0, "dsdx");
222 dsdx = lp_build_abs(float_bld, dsdx);
223 dsdy = LLVMBuildExtractElement(bld->builder, ddy[0], index0, "dsdy");
224 dsdy = lp_build_abs(float_bld, dsdy);
225 if (dims > 1) {
226 dtdx = LLVMBuildExtractElement(bld->builder, ddx[1], index0, "dtdx");
227 dtdx = lp_build_abs(float_bld, dtdx);
228 dtdy = LLVMBuildExtractElement(bld->builder, ddy[1], index0, "dtdy");
229 dtdy = lp_build_abs(float_bld, dtdy);
230 if (dims > 2) {
231 drdx = LLVMBuildExtractElement(bld->builder, ddx[2], index0, "drdx");
232 drdx = lp_build_abs(float_bld, drdx);
233 drdy = LLVMBuildExtractElement(bld->builder, ddy[2], index0, "drdy");
234 drdy = lp_build_abs(float_bld, drdy);
235 }
236 }
237
238 /* Compute rho = max of all partial derivatives scaled by texture size.
239 * XXX this could be vectorized somewhat
240 */
241 rho = LLVMBuildFMul(bld->builder,
242 lp_build_max(float_bld, dsdx, dsdy),
243 lp_build_int_to_float(float_bld, width), "");
244 if (dims > 1) {
245 LLVMValueRef max;
246 max = LLVMBuildFMul(bld->builder,
247 lp_build_max(float_bld, dtdx, dtdy),
248 lp_build_int_to_float(float_bld, height), "");
249 rho = lp_build_max(float_bld, rho, max);
250 if (dims > 2) {
251 max = LLVMBuildFMul(bld->builder,
252 lp_build_max(float_bld, drdx, drdy),
253 lp_build_int_to_float(float_bld, depth), "");
254 rho = lp_build_max(float_bld, rho, max);
255 }
256 }
257
258 /* compute lod = log2(rho) */
259 #if 0
260 lod = lp_build_log2(float_bld, rho);
261 #else
262 lod = lp_build_fast_log2(float_bld, rho);
263 #endif
264
265 /* add shader lod bias */
266 if (lod_bias) {
267 lod_bias = LLVMBuildExtractElement(bld->builder, lod_bias,
268 index0, "");
269 lod = LLVMBuildFAdd(bld->builder, lod, lod_bias, "shader_lod_bias");
270 }
271 }
272
273 /* add sampler lod bias */
274 if (bld->static_state->lod_bias_non_zero)
275 lod = LLVMBuildFAdd(bld->builder, lod, sampler_lod_bias, "sampler_lod_bias");
276
277
278 /* clamp lod */
279 if (bld->static_state->apply_max_lod) {
280 LLVMValueRef max_lod =
281 bld->dynamic_state->max_lod(bld->dynamic_state, bld->builder, unit);
282
283 lod = lp_build_min(float_bld, lod, max_lod);
284 }
285 if (bld->static_state->apply_min_lod) {
286 LLVMValueRef min_lod =
287 bld->dynamic_state->min_lod(bld->dynamic_state, bld->builder, unit);
288
289 lod = lp_build_max(float_bld, lod, min_lod);
290 }
291
292 return lod;
293 }
294 }
295
296
297 /**
298 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
299 * mipmap level index.
300 * Note: this is all scalar code.
301 * \param lod scalar float texture level of detail
302 * \param level_out returns integer
303 */
304 void
305 lp_build_nearest_mip_level(struct lp_build_sample_context *bld,
306 unsigned unit,
307 LLVMValueRef lod,
308 LLVMValueRef *level_out)
309 {
310 struct lp_build_context *float_bld = &bld->float_bld;
311 struct lp_build_context *int_bld = &bld->int_bld;
312 LLVMValueRef last_level, level;
313
314 LLVMValueRef zero = LLVMConstInt(LLVMInt32Type(), 0, 0);
315
316 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
317 bld->builder, unit);
318
319 /* convert float lod to integer */
320 level = lp_build_iround(float_bld, lod);
321
322 /* clamp level to legal range of levels */
323 *level_out = lp_build_clamp(int_bld, level, zero, last_level);
324 }
325
326
327 /**
328 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
329 * two (adjacent) mipmap level indexes. Later, we'll sample from those
330 * two mipmap levels and interpolate between them.
331 */
332 void
333 lp_build_linear_mip_levels(struct lp_build_sample_context *bld,
334 unsigned unit,
335 LLVMValueRef lod,
336 LLVMValueRef *level0_out,
337 LLVMValueRef *level1_out,
338 LLVMValueRef *weight_out)
339 {
340 struct lp_build_context *float_bld = &bld->float_bld;
341 struct lp_build_context *int_bld = &bld->int_bld;
342 LLVMValueRef last_level, level;
343
344 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
345 bld->builder, unit);
346
347 /* convert float lod to integer */
348 lp_build_ifloor_fract(float_bld, lod, &level, weight_out);
349
350 /* compute level 0 and clamp to legal range of levels */
351 *level0_out = lp_build_clamp(int_bld, level,
352 int_bld->zero,
353 last_level);
354 /* compute level 1 and clamp to legal range of levels */
355 level = lp_build_add(int_bld, level, int_bld->one);
356 *level1_out = lp_build_clamp(int_bld, level,
357 int_bld->zero,
358 last_level);
359 }
360
361
362 /**
363 * Return pointer to a single mipmap level.
364 * \param data_array array of pointers to mipmap levels
365 * \param level integer mipmap level
366 */
367 LLVMValueRef
368 lp_build_get_mipmap_level(struct lp_build_sample_context *bld,
369 LLVMValueRef data_array, LLVMValueRef level)
370 {
371 LLVMValueRef indexes[2], data_ptr;
372 indexes[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
373 indexes[1] = level;
374 data_ptr = LLVMBuildGEP(bld->builder, data_array, indexes, 2, "");
375 data_ptr = LLVMBuildLoad(bld->builder, data_ptr, "");
376 return data_ptr;
377 }
378
379
380 LLVMValueRef
381 lp_build_get_const_mipmap_level(struct lp_build_sample_context *bld,
382 LLVMValueRef data_array, int level)
383 {
384 LLVMValueRef lvl = LLVMConstInt(LLVMInt32Type(), level, 0);
385 return lp_build_get_mipmap_level(bld, data_array, lvl);
386 }
387
388
389 /**
390 * Codegen equivalent for u_minify().
391 * Return max(1, base_size >> level);
392 */
393 static LLVMValueRef
394 lp_build_minify(struct lp_build_sample_context *bld,
395 LLVMValueRef base_size,
396 LLVMValueRef level)
397 {
398 if (level == bld->int_coord_bld.zero) {
399 /* if we're using mipmap level zero, no minification is needed */
400 return base_size;
401 }
402 else {
403 LLVMValueRef size =
404 LLVMBuildLShr(bld->builder, base_size, level, "minify");
405 size = lp_build_max(&bld->int_coord_bld, size, bld->int_coord_bld.one);
406 return size;
407 }
408 }
409
410
411 /**
412 * Dereference stride_array[mipmap_level] array to get a stride.
413 * Return stride as a vector.
414 */
415 static LLVMValueRef
416 lp_build_get_level_stride_vec(struct lp_build_sample_context *bld,
417 LLVMValueRef stride_array, LLVMValueRef level)
418 {
419 LLVMValueRef indexes[2], stride;
420 indexes[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
421 indexes[1] = level;
422 stride = LLVMBuildGEP(bld->builder, stride_array, indexes, 2, "");
423 stride = LLVMBuildLoad(bld->builder, stride, "");
424 stride = lp_build_broadcast_scalar(&bld->int_coord_bld, stride);
425 return stride;
426 }
427
428
429 /**
430 * When sampling a mipmap, we need to compute the width, height, depth
431 * of the source levels from the level indexes. This helper function
432 * does that.
433 */
434 void
435 lp_build_mipmap_level_sizes(struct lp_build_sample_context *bld,
436 unsigned dims,
437 LLVMValueRef width_vec,
438 LLVMValueRef height_vec,
439 LLVMValueRef depth_vec,
440 LLVMValueRef ilevel0,
441 LLVMValueRef ilevel1,
442 LLVMValueRef row_stride_array,
443 LLVMValueRef img_stride_array,
444 LLVMValueRef *width0_vec,
445 LLVMValueRef *width1_vec,
446 LLVMValueRef *height0_vec,
447 LLVMValueRef *height1_vec,
448 LLVMValueRef *depth0_vec,
449 LLVMValueRef *depth1_vec,
450 LLVMValueRef *row_stride0_vec,
451 LLVMValueRef *row_stride1_vec,
452 LLVMValueRef *img_stride0_vec,
453 LLVMValueRef *img_stride1_vec)
454 {
455 const unsigned mip_filter = bld->static_state->min_mip_filter;
456 LLVMValueRef ilevel0_vec, ilevel1_vec;
457
458 ilevel0_vec = lp_build_broadcast_scalar(&bld->int_coord_bld, ilevel0);
459 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
460 ilevel1_vec = lp_build_broadcast_scalar(&bld->int_coord_bld, ilevel1);
461
462 /*
463 * Compute width, height, depth at mipmap level 'ilevel0'
464 */
465 *width0_vec = lp_build_minify(bld, width_vec, ilevel0_vec);
466 if (dims >= 2) {
467 *height0_vec = lp_build_minify(bld, height_vec, ilevel0_vec);
468 *row_stride0_vec = lp_build_get_level_stride_vec(bld,
469 row_stride_array,
470 ilevel0);
471 if (dims == 3 || bld->static_state->target == PIPE_TEXTURE_CUBE) {
472 *img_stride0_vec = lp_build_get_level_stride_vec(bld,
473 img_stride_array,
474 ilevel0);
475 if (dims == 3) {
476 *depth0_vec = lp_build_minify(bld, depth_vec, ilevel0_vec);
477 }
478 }
479 }
480 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
481 /* compute width, height, depth for second mipmap level at 'ilevel1' */
482 *width1_vec = lp_build_minify(bld, width_vec, ilevel1_vec);
483 if (dims >= 2) {
484 *height1_vec = lp_build_minify(bld, height_vec, ilevel1_vec);
485 *row_stride1_vec = lp_build_get_level_stride_vec(bld,
486 row_stride_array,
487 ilevel1);
488 if (dims == 3 || bld->static_state->target == PIPE_TEXTURE_CUBE) {
489 *img_stride1_vec = lp_build_get_level_stride_vec(bld,
490 img_stride_array,
491 ilevel1);
492 if (dims == 3) {
493 *depth1_vec = lp_build_minify(bld, depth_vec, ilevel1_vec);
494 }
495 }
496 }
497 }
498 }
499
500
501
502 /** Helper used by lp_build_cube_lookup() */
503 static LLVMValueRef
504 lp_build_cube_ima(struct lp_build_context *coord_bld, LLVMValueRef coord)
505 {
506 /* ima = -0.5 / abs(coord); */
507 LLVMValueRef negHalf = lp_build_const_vec(coord_bld->type, -0.5);
508 LLVMValueRef absCoord = lp_build_abs(coord_bld, coord);
509 LLVMValueRef ima = lp_build_div(coord_bld, negHalf, absCoord);
510 return ima;
511 }
512
513
514 /**
515 * Helper used by lp_build_cube_lookup()
516 * \param sign scalar +1 or -1
517 * \param coord float vector
518 * \param ima float vector
519 */
520 static LLVMValueRef
521 lp_build_cube_coord(struct lp_build_context *coord_bld,
522 LLVMValueRef sign, int negate_coord,
523 LLVMValueRef coord, LLVMValueRef ima)
524 {
525 /* return negate(coord) * ima * sign + 0.5; */
526 LLVMValueRef half = lp_build_const_vec(coord_bld->type, 0.5);
527 LLVMValueRef res;
528
529 assert(negate_coord == +1 || negate_coord == -1);
530
531 if (negate_coord == -1) {
532 coord = lp_build_negate(coord_bld, coord);
533 }
534
535 res = lp_build_mul(coord_bld, coord, ima);
536 if (sign) {
537 sign = lp_build_broadcast_scalar(coord_bld, sign);
538 res = lp_build_mul(coord_bld, res, sign);
539 }
540 res = lp_build_add(coord_bld, res, half);
541
542 return res;
543 }
544
545
546 /** Helper used by lp_build_cube_lookup()
547 * Return (major_coord >= 0) ? pos_face : neg_face;
548 */
549 static LLVMValueRef
550 lp_build_cube_face(struct lp_build_sample_context *bld,
551 LLVMValueRef major_coord,
552 unsigned pos_face, unsigned neg_face)
553 {
554 LLVMValueRef cmp = LLVMBuildFCmp(bld->builder, LLVMRealUGE,
555 major_coord,
556 bld->float_bld.zero, "");
557 LLVMValueRef pos = LLVMConstInt(LLVMInt32Type(), pos_face, 0);
558 LLVMValueRef neg = LLVMConstInt(LLVMInt32Type(), neg_face, 0);
559 LLVMValueRef res = LLVMBuildSelect(bld->builder, cmp, pos, neg, "");
560 return res;
561 }
562
563
564
565 /**
566 * Generate code to do cube face selection and compute per-face texcoords.
567 */
568 void
569 lp_build_cube_lookup(struct lp_build_sample_context *bld,
570 LLVMValueRef s,
571 LLVMValueRef t,
572 LLVMValueRef r,
573 LLVMValueRef *face,
574 LLVMValueRef *face_s,
575 LLVMValueRef *face_t)
576 {
577 struct lp_build_context *float_bld = &bld->float_bld;
578 struct lp_build_context *coord_bld = &bld->coord_bld;
579 LLVMValueRef rx, ry, rz;
580 LLVMValueRef arx, ary, arz;
581 LLVMValueRef c25 = LLVMConstReal(LLVMFloatType(), 0.25);
582 LLVMValueRef arx_ge_ary, arx_ge_arz;
583 LLVMValueRef ary_ge_arx, ary_ge_arz;
584 LLVMValueRef arx_ge_ary_arz, ary_ge_arx_arz;
585 LLVMValueRef rx_pos, ry_pos, rz_pos;
586
587 assert(bld->coord_bld.type.length == 4);
588
589 /*
590 * Use the average of the four pixel's texcoords to choose the face.
591 */
592 rx = lp_build_mul(float_bld, c25,
593 lp_build_sum_vector(&bld->coord_bld, s));
594 ry = lp_build_mul(float_bld, c25,
595 lp_build_sum_vector(&bld->coord_bld, t));
596 rz = lp_build_mul(float_bld, c25,
597 lp_build_sum_vector(&bld->coord_bld, r));
598
599 arx = lp_build_abs(float_bld, rx);
600 ary = lp_build_abs(float_bld, ry);
601 arz = lp_build_abs(float_bld, rz);
602
603 /*
604 * Compare sign/magnitude of rx,ry,rz to determine face
605 */
606 arx_ge_ary = LLVMBuildFCmp(bld->builder, LLVMRealUGE, arx, ary, "");
607 arx_ge_arz = LLVMBuildFCmp(bld->builder, LLVMRealUGE, arx, arz, "");
608 ary_ge_arx = LLVMBuildFCmp(bld->builder, LLVMRealUGE, ary, arx, "");
609 ary_ge_arz = LLVMBuildFCmp(bld->builder, LLVMRealUGE, ary, arz, "");
610
611 arx_ge_ary_arz = LLVMBuildAnd(bld->builder, arx_ge_ary, arx_ge_arz, "");
612 ary_ge_arx_arz = LLVMBuildAnd(bld->builder, ary_ge_arx, ary_ge_arz, "");
613
614 rx_pos = LLVMBuildFCmp(bld->builder, LLVMRealUGE, rx, float_bld->zero, "");
615 ry_pos = LLVMBuildFCmp(bld->builder, LLVMRealUGE, ry, float_bld->zero, "");
616 rz_pos = LLVMBuildFCmp(bld->builder, LLVMRealUGE, rz, float_bld->zero, "");
617
618 {
619 struct lp_build_flow_context *flow_ctx;
620 struct lp_build_if_state if_ctx;
621
622 flow_ctx = lp_build_flow_create(bld->builder);
623 lp_build_flow_scope_begin(flow_ctx);
624
625 *face_s = bld->coord_bld.undef;
626 *face_t = bld->coord_bld.undef;
627 *face = bld->int_bld.undef;
628
629 lp_build_name(*face_s, "face_s");
630 lp_build_name(*face_t, "face_t");
631 lp_build_name(*face, "face");
632
633 lp_build_flow_scope_declare(flow_ctx, face_s);
634 lp_build_flow_scope_declare(flow_ctx, face_t);
635 lp_build_flow_scope_declare(flow_ctx, face);
636
637 lp_build_if(&if_ctx, flow_ctx, bld->builder, arx_ge_ary_arz);
638 {
639 /* +/- X face */
640 LLVMValueRef sign = lp_build_sgn(float_bld, rx);
641 LLVMValueRef ima = lp_build_cube_ima(coord_bld, s);
642 *face_s = lp_build_cube_coord(coord_bld, sign, +1, r, ima);
643 *face_t = lp_build_cube_coord(coord_bld, NULL, +1, t, ima);
644 *face = lp_build_cube_face(bld, rx,
645 PIPE_TEX_FACE_POS_X,
646 PIPE_TEX_FACE_NEG_X);
647 }
648 lp_build_else(&if_ctx);
649 {
650 struct lp_build_flow_context *flow_ctx2;
651 struct lp_build_if_state if_ctx2;
652
653 LLVMValueRef face_s2 = bld->coord_bld.undef;
654 LLVMValueRef face_t2 = bld->coord_bld.undef;
655 LLVMValueRef face2 = bld->int_bld.undef;
656
657 flow_ctx2 = lp_build_flow_create(bld->builder);
658 lp_build_flow_scope_begin(flow_ctx2);
659 lp_build_flow_scope_declare(flow_ctx2, &face_s2);
660 lp_build_flow_scope_declare(flow_ctx2, &face_t2);
661 lp_build_flow_scope_declare(flow_ctx2, &face2);
662
663 ary_ge_arx_arz = LLVMBuildAnd(bld->builder, ary_ge_arx, ary_ge_arz, "");
664
665 lp_build_if(&if_ctx2, flow_ctx2, bld->builder, ary_ge_arx_arz);
666 {
667 /* +/- Y face */
668 LLVMValueRef sign = lp_build_sgn(float_bld, ry);
669 LLVMValueRef ima = lp_build_cube_ima(coord_bld, t);
670 face_s2 = lp_build_cube_coord(coord_bld, NULL, -1, s, ima);
671 face_t2 = lp_build_cube_coord(coord_bld, sign, -1, r, ima);
672 face2 = lp_build_cube_face(bld, ry,
673 PIPE_TEX_FACE_POS_Y,
674 PIPE_TEX_FACE_NEG_Y);
675 }
676 lp_build_else(&if_ctx2);
677 {
678 /* +/- Z face */
679 LLVMValueRef sign = lp_build_sgn(float_bld, rz);
680 LLVMValueRef ima = lp_build_cube_ima(coord_bld, r);
681 face_s2 = lp_build_cube_coord(coord_bld, sign, -1, s, ima);
682 face_t2 = lp_build_cube_coord(coord_bld, NULL, +1, t, ima);
683 face2 = lp_build_cube_face(bld, rz,
684 PIPE_TEX_FACE_POS_Z,
685 PIPE_TEX_FACE_NEG_Z);
686 }
687 lp_build_endif(&if_ctx2);
688 lp_build_flow_scope_end(flow_ctx2);
689 lp_build_flow_destroy(flow_ctx2);
690 *face_s = face_s2;
691 *face_t = face_t2;
692 *face = face2;
693 }
694
695 lp_build_endif(&if_ctx);
696 lp_build_flow_scope_end(flow_ctx);
697 lp_build_flow_destroy(flow_ctx);
698 }
699 }
700
701
702 /**
703 * Compute the partial offset of a pixel block along an arbitrary axis.
704 *
705 * @param coord coordinate in pixels
706 * @param stride number of bytes between rows of successive pixel blocks
707 * @param block_length number of pixels in a pixels block along the coordinate
708 * axis
709 * @param out_offset resulting relative offset of the pixel block in bytes
710 * @param out_subcoord resulting sub-block pixel coordinate
711 */
712 void
713 lp_build_sample_partial_offset(struct lp_build_context *bld,
714 unsigned block_length,
715 LLVMValueRef coord,
716 LLVMValueRef stride,
717 LLVMValueRef *out_offset,
718 LLVMValueRef *out_subcoord)
719 {
720 LLVMValueRef offset;
721 LLVMValueRef subcoord;
722
723 if (block_length == 1) {
724 subcoord = bld->zero;
725 }
726 else {
727 /*
728 * Pixel blocks have power of two dimensions. LLVM should convert the
729 * rem/div to bit arithmetic.
730 * TODO: Verify this.
731 * It does indeed BUT it does transform it to scalar (and back) when doing so
732 * (using roughly extract, shift/and, mov, unpack) (llvm 2.7).
733 * The generated code looks seriously unfunny and is quite expensive.
734 */
735 #if 0
736 LLVMValueRef block_width = lp_build_const_int_vec(bld->type, block_length);
737 subcoord = LLVMBuildURem(bld->builder, coord, block_width, "");
738 coord = LLVMBuildUDiv(bld->builder, coord, block_width, "");
739 #else
740 unsigned logbase2 = util_unsigned_logbase2(block_length);
741 LLVMValueRef block_shift = lp_build_const_int_vec(bld->type, logbase2);
742 LLVMValueRef block_mask = lp_build_const_int_vec(bld->type, block_length - 1);
743 subcoord = LLVMBuildAnd(bld->builder, coord, block_mask, "");
744 coord = LLVMBuildLShr(bld->builder, coord, block_shift, "");
745 #endif
746 }
747
748 offset = lp_build_mul(bld, coord, stride);
749
750 assert(out_offset);
751 assert(out_subcoord);
752
753 *out_offset = offset;
754 *out_subcoord = subcoord;
755 }
756
757
758 /**
759 * Compute the offset of a pixel block.
760 *
761 * x, y, z, y_stride, z_stride are vectors, and they refer to pixels.
762 *
763 * Returns the relative offset and i,j sub-block coordinates
764 */
765 void
766 lp_build_sample_offset(struct lp_build_context *bld,
767 const struct util_format_description *format_desc,
768 LLVMValueRef x,
769 LLVMValueRef y,
770 LLVMValueRef z,
771 LLVMValueRef y_stride,
772 LLVMValueRef z_stride,
773 LLVMValueRef *out_offset,
774 LLVMValueRef *out_i,
775 LLVMValueRef *out_j)
776 {
777 LLVMValueRef x_stride;
778 LLVMValueRef offset;
779
780 x_stride = lp_build_const_vec(bld->type, format_desc->block.bits/8);
781
782 lp_build_sample_partial_offset(bld,
783 format_desc->block.width,
784 x, x_stride,
785 &offset, out_i);
786
787 if (y && y_stride) {
788 LLVMValueRef y_offset;
789 lp_build_sample_partial_offset(bld,
790 format_desc->block.height,
791 y, y_stride,
792 &y_offset, out_j);
793 offset = lp_build_add(bld, offset, y_offset);
794 }
795 else {
796 *out_j = bld->zero;
797 }
798
799 if (z && z_stride) {
800 LLVMValueRef z_offset;
801 LLVMValueRef k;
802 lp_build_sample_partial_offset(bld,
803 1, /* pixel blocks are always 2D */
804 z, z_stride,
805 &z_offset, &k);
806 offset = lp_build_add(bld, offset, z_offset);
807 }
808
809 *out_offset = offset;
810 }