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