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