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