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