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