gallivm: optimize lp_build_minify for sse
[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 "util/u_cpu_detect.h"
40 #include "lp_bld_arit.h"
41 #include "lp_bld_const.h"
42 #include "lp_bld_debug.h"
43 #include "lp_bld_printf.h"
44 #include "lp_bld_flow.h"
45 #include "lp_bld_sample.h"
46 #include "lp_bld_swizzle.h"
47 #include "lp_bld_type.h"
48 #include "lp_bld_logic.h"
49 #include "lp_bld_pack.h"
50 #include "lp_bld_quad.h"
51 #include "lp_bld_bitarit.h"
52
53
54 /*
55 * Bri-linear factor. Should be greater than one.
56 */
57 #define BRILINEAR_FACTOR 2
58
59 /**
60 * Does the given texture wrap mode allow sampling the texture border color?
61 * XXX maybe move this into gallium util code.
62 */
63 boolean
64 lp_sampler_wrap_mode_uses_border_color(unsigned mode,
65 unsigned min_img_filter,
66 unsigned mag_img_filter)
67 {
68 switch (mode) {
69 case PIPE_TEX_WRAP_REPEAT:
70 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
71 case PIPE_TEX_WRAP_MIRROR_REPEAT:
72 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
73 return FALSE;
74 case PIPE_TEX_WRAP_CLAMP:
75 case PIPE_TEX_WRAP_MIRROR_CLAMP:
76 if (min_img_filter == PIPE_TEX_FILTER_NEAREST &&
77 mag_img_filter == PIPE_TEX_FILTER_NEAREST) {
78 return FALSE;
79 } else {
80 return TRUE;
81 }
82 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
83 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
84 return TRUE;
85 default:
86 assert(0 && "unexpected wrap mode");
87 return FALSE;
88 }
89 }
90
91
92 /**
93 * Initialize lp_sampler_static_texture_state object with the gallium
94 * texture/sampler_view state (this contains the parts which are
95 * considered static).
96 */
97 void
98 lp_sampler_static_texture_state(struct lp_static_texture_state *state,
99 const struct pipe_sampler_view *view)
100 {
101 const struct pipe_resource *texture;
102
103 memset(state, 0, sizeof *state);
104
105 if (!view || !view->texture)
106 return;
107
108 texture = view->texture;
109
110 state->format = view->format;
111 state->swizzle_r = view->swizzle_r;
112 state->swizzle_g = view->swizzle_g;
113 state->swizzle_b = view->swizzle_b;
114 state->swizzle_a = view->swizzle_a;
115
116 state->target = texture->target;
117 state->pot_width = util_is_power_of_two(texture->width0);
118 state->pot_height = util_is_power_of_two(texture->height0);
119 state->pot_depth = util_is_power_of_two(texture->depth0);
120 state->level_zero_only = !view->u.tex.last_level;
121
122 /*
123 * the layer / element / level parameters are all either dynamic
124 * state or handled transparently wrt execution.
125 */
126 }
127
128
129 /**
130 * Initialize lp_sampler_static_sampler_state object with the gallium sampler
131 * state (this contains the parts which are considered static).
132 */
133 void
134 lp_sampler_static_sampler_state(struct lp_static_sampler_state *state,
135 const struct pipe_sampler_state *sampler)
136 {
137 memset(state, 0, sizeof *state);
138
139 if (!sampler)
140 return;
141
142 /*
143 * We don't copy sampler state over unless it is actually enabled, to avoid
144 * spurious recompiles, as the sampler static state is part of the shader
145 * key.
146 *
147 * Ideally the state tracker or cso_cache module would make all state
148 * canonical, but until that happens it's better to be safe than sorry here.
149 *
150 * XXX: Actually there's much more than can be done here, especially
151 * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
152 */
153
154 state->wrap_s = sampler->wrap_s;
155 state->wrap_t = sampler->wrap_t;
156 state->wrap_r = sampler->wrap_r;
157 state->min_img_filter = sampler->min_img_filter;
158 state->mag_img_filter = sampler->mag_img_filter;
159 state->seamless_cube_map = sampler->seamless_cube_map;
160
161 if (sampler->max_lod > 0.0f) {
162 state->min_mip_filter = sampler->min_mip_filter;
163 } else {
164 state->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
165 }
166
167 if (state->min_mip_filter != PIPE_TEX_MIPFILTER_NONE ||
168 state->min_img_filter != state->mag_img_filter) {
169 if (sampler->lod_bias != 0.0f) {
170 state->lod_bias_non_zero = 1;
171 }
172
173 /* If min_lod == max_lod we can greatly simplify mipmap selection.
174 * This is a case that occurs during automatic mipmap generation.
175 */
176 if (sampler->min_lod == sampler->max_lod) {
177 state->min_max_lod_equal = 1;
178 } else {
179 if (sampler->min_lod > 0.0f) {
180 state->apply_min_lod = 1;
181 }
182
183 /*
184 * XXX this won't do anything with the mesa state tracker which always
185 * sets max_lod to not more than actually present mip maps...
186 */
187 if (sampler->max_lod < (PIPE_MAX_TEXTURE_LEVELS - 1)) {
188 state->apply_max_lod = 1;
189 }
190 }
191 }
192
193 state->compare_mode = sampler->compare_mode;
194 if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) {
195 state->compare_func = sampler->compare_func;
196 }
197
198 state->normalized_coords = sampler->normalized_coords;
199 }
200
201
202 /**
203 * Generate code to compute coordinate gradient (rho).
204 * \param derivs partial derivatives of (s, t, r, q) with respect to X and Y
205 *
206 * The resulting rho has bld->levelf format (per quad or per element).
207 */
208 static LLVMValueRef
209 lp_build_rho(struct lp_build_sample_context *bld,
210 unsigned texture_unit,
211 LLVMValueRef s,
212 LLVMValueRef t,
213 LLVMValueRef r,
214 LLVMValueRef cube_rho,
215 const struct lp_derivatives *derivs)
216 {
217 struct gallivm_state *gallivm = bld->gallivm;
218 struct lp_build_context *int_size_bld = &bld->int_size_in_bld;
219 struct lp_build_context *float_size_bld = &bld->float_size_in_bld;
220 struct lp_build_context *float_bld = &bld->float_bld;
221 struct lp_build_context *coord_bld = &bld->coord_bld;
222 struct lp_build_context *rho_bld = &bld->lodf_bld;
223 const unsigned dims = bld->dims;
224 LLVMValueRef ddx_ddy[2];
225 LLVMBuilderRef builder = bld->gallivm->builder;
226 LLVMTypeRef i32t = LLVMInt32TypeInContext(bld->gallivm->context);
227 LLVMValueRef index0 = LLVMConstInt(i32t, 0, 0);
228 LLVMValueRef index1 = LLVMConstInt(i32t, 1, 0);
229 LLVMValueRef index2 = LLVMConstInt(i32t, 2, 0);
230 LLVMValueRef rho_vec;
231 LLVMValueRef int_size, float_size;
232 LLVMValueRef rho;
233 LLVMValueRef first_level, first_level_vec;
234 unsigned length = coord_bld->type.length;
235 unsigned num_quads = length / 4;
236 boolean rho_per_quad = rho_bld->type.length != length;
237 boolean no_rho_opt = (gallivm_debug & GALLIVM_DEBUG_NO_RHO_APPROX) && (dims > 1);
238 unsigned i;
239 LLVMValueRef i32undef = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
240 LLVMValueRef rho_xvec, rho_yvec;
241
242 /* Note that all simplified calculations will only work for isotropic filtering */
243
244 /*
245 * rho calcs are always per quad except for explicit derivs (excluding
246 * the messy cube maps for now) when requested.
247 */
248
249 first_level = bld->dynamic_state->first_level(bld->dynamic_state,
250 bld->gallivm, texture_unit);
251 first_level_vec = lp_build_broadcast_scalar(int_size_bld, first_level);
252 int_size = lp_build_minify(int_size_bld, bld->int_size, first_level_vec, TRUE);
253 float_size = lp_build_int_to_float(float_size_bld, int_size);
254
255 if (cube_rho) {
256 LLVMValueRef cubesize;
257 LLVMValueRef index0 = lp_build_const_int32(gallivm, 0);
258
259 /*
260 * Cube map code did already everything except size mul and per-quad extraction.
261 * Luckily cube maps are always quadratic!
262 */
263 if (rho_per_quad) {
264 rho = lp_build_pack_aos_scalars(bld->gallivm, coord_bld->type,
265 rho_bld->type, cube_rho, 0);
266 }
267 else {
268 rho = lp_build_swizzle_scalar_aos(coord_bld, cube_rho, 0, 4);
269 }
270 /* Could optimize this for single quad just skip the broadcast */
271 cubesize = lp_build_extract_broadcast(gallivm, bld->float_size_in_type,
272 rho_bld->type, float_size, index0);
273 /* skipping sqrt hence returning rho squared */
274 cubesize = lp_build_mul(rho_bld, cubesize, cubesize);
275 rho = lp_build_mul(rho_bld, cubesize, rho);
276 }
277 else if (derivs) {
278 LLVMValueRef ddmax[3], ddx[3], ddy[3];
279 for (i = 0; i < dims; i++) {
280 LLVMValueRef floatdim;
281 LLVMValueRef indexi = lp_build_const_int32(gallivm, i);
282
283 floatdim = lp_build_extract_broadcast(gallivm, bld->float_size_in_type,
284 coord_bld->type, float_size, indexi);
285
286 /*
287 * note that for rho_per_quad case could reduce math (at some shuffle
288 * cost), but for now use same code to per-pixel lod case.
289 */
290 if (no_rho_opt) {
291 ddx[i] = lp_build_mul(coord_bld, floatdim, derivs->ddx[i]);
292 ddy[i] = lp_build_mul(coord_bld, floatdim, derivs->ddy[i]);
293 ddx[i] = lp_build_mul(coord_bld, ddx[i], ddx[i]);
294 ddy[i] = lp_build_mul(coord_bld, ddy[i], ddy[i]);
295 }
296 else {
297 LLVMValueRef tmpx, tmpy;
298 tmpx = lp_build_abs(coord_bld, derivs->ddx[i]);
299 tmpy = lp_build_abs(coord_bld, derivs->ddy[i]);
300 ddmax[i] = lp_build_max(coord_bld, tmpx, tmpy);
301 ddmax[i] = lp_build_mul(coord_bld, floatdim, ddmax[i]);
302 }
303 }
304 if (no_rho_opt) {
305 rho_xvec = lp_build_add(coord_bld, ddx[0], ddx[1]);
306 rho_yvec = lp_build_add(coord_bld, ddy[0], ddy[1]);
307 if (dims > 2) {
308 rho_xvec = lp_build_add(coord_bld, rho_xvec, ddx[2]);
309 rho_yvec = lp_build_add(coord_bld, rho_yvec, ddy[2]);
310 }
311 rho = lp_build_max(coord_bld, rho_xvec, rho_yvec);
312 /* skipping sqrt hence returning rho squared */
313 }
314 else {
315 rho = ddmax[0];
316 if (dims > 1) {
317 rho = lp_build_max(coord_bld, rho, ddmax[1]);
318 if (dims > 2) {
319 rho = lp_build_max(coord_bld, rho, ddmax[2]);
320 }
321 }
322 }
323 if (rho_per_quad) {
324 /*
325 * rho_vec contains per-pixel rho, convert to scalar per quad.
326 */
327 rho = lp_build_pack_aos_scalars(bld->gallivm, coord_bld->type,
328 rho_bld->type, rho, 0);
329 }
330 }
331 else {
332 /*
333 * This looks all a bit complex, but it's not that bad
334 * (the shuffle code makes it look worse than it is).
335 * Still, might not be ideal for all cases.
336 */
337 static const unsigned char swizzle0[] = { /* no-op swizzle */
338 0, LP_BLD_SWIZZLE_DONTCARE,
339 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
340 };
341 static const unsigned char swizzle1[] = {
342 1, LP_BLD_SWIZZLE_DONTCARE,
343 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
344 };
345 static const unsigned char swizzle2[] = {
346 2, LP_BLD_SWIZZLE_DONTCARE,
347 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
348 };
349
350 if (dims < 2) {
351 ddx_ddy[0] = lp_build_packed_ddx_ddy_onecoord(coord_bld, s);
352 }
353 else if (dims >= 2) {
354 ddx_ddy[0] = lp_build_packed_ddx_ddy_twocoord(coord_bld, s, t);
355 if (dims > 2) {
356 ddx_ddy[1] = lp_build_packed_ddx_ddy_onecoord(coord_bld, r);
357 }
358 }
359
360 if (no_rho_opt) {
361 static const unsigned char swizzle01[] = { /* no-op swizzle */
362 0, 1,
363 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
364 };
365 static const unsigned char swizzle23[] = {
366 2, 3,
367 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
368 };
369 LLVMValueRef ddx_ddys, ddx_ddyt, floatdim, shuffles[LP_MAX_VECTOR_LENGTH / 4];
370
371 for (i = 0; i < num_quads; i++) {
372 shuffles[i*4+0] = shuffles[i*4+1] = index0;
373 shuffles[i*4+2] = shuffles[i*4+3] = index1;
374 }
375 floatdim = LLVMBuildShuffleVector(builder, float_size, float_size,
376 LLVMConstVector(shuffles, length), "");
377 ddx_ddy[0] = lp_build_mul(coord_bld, ddx_ddy[0], floatdim);
378 ddx_ddy[0] = lp_build_mul(coord_bld, ddx_ddy[0], ddx_ddy[0]);
379 ddx_ddys = lp_build_swizzle_aos(coord_bld, ddx_ddy[0], swizzle01);
380 ddx_ddyt = lp_build_swizzle_aos(coord_bld, ddx_ddy[0], swizzle23);
381 rho_vec = lp_build_add(coord_bld, ddx_ddys, ddx_ddyt);
382
383 if (dims > 2) {
384 static const unsigned char swizzle02[] = {
385 0, 2,
386 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
387 };
388 floatdim = lp_build_extract_broadcast(gallivm, bld->float_size_in_type,
389 coord_bld->type, float_size, index2);
390 ddx_ddy[1] = lp_build_mul(coord_bld, ddx_ddy[1], floatdim);
391 ddx_ddy[1] = lp_build_mul(coord_bld, ddx_ddy[1], ddx_ddy[1]);
392 ddx_ddy[1] = lp_build_swizzle_aos(coord_bld, ddx_ddy[1], swizzle02);
393 rho_vec = lp_build_add(coord_bld, rho_vec, ddx_ddy[1]);
394 }
395
396 rho_xvec = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle0);
397 rho_yvec = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle1);
398 rho = lp_build_max(coord_bld, rho_xvec, rho_yvec);
399
400 if (rho_per_quad) {
401 rho = lp_build_pack_aos_scalars(bld->gallivm, coord_bld->type,
402 rho_bld->type, rho, 0);
403 }
404 else {
405 rho = lp_build_swizzle_scalar_aos(coord_bld, rho, 0, 4);
406 }
407 /* skipping sqrt hence returning rho squared */
408 }
409 else {
410 ddx_ddy[0] = lp_build_abs(coord_bld, ddx_ddy[0]);
411 if (dims > 2) {
412 ddx_ddy[1] = lp_build_abs(coord_bld, ddx_ddy[1]);
413 }
414
415 if (dims < 2) {
416 rho_xvec = lp_build_swizzle_aos(coord_bld, ddx_ddy[0], swizzle0);
417 rho_yvec = lp_build_swizzle_aos(coord_bld, ddx_ddy[0], swizzle2);
418 }
419 else if (dims == 2) {
420 static const unsigned char swizzle02[] = {
421 0, 2,
422 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
423 };
424 static const unsigned char swizzle13[] = {
425 1, 3,
426 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
427 };
428 rho_xvec = lp_build_swizzle_aos(coord_bld, ddx_ddy[0], swizzle02);
429 rho_yvec = lp_build_swizzle_aos(coord_bld, ddx_ddy[0], swizzle13);
430 }
431 else {
432 LLVMValueRef shuffles1[LP_MAX_VECTOR_LENGTH];
433 LLVMValueRef shuffles2[LP_MAX_VECTOR_LENGTH];
434 assert(dims == 3);
435 for (i = 0; i < num_quads; i++) {
436 shuffles1[4*i + 0] = lp_build_const_int32(gallivm, 4*i);
437 shuffles1[4*i + 1] = lp_build_const_int32(gallivm, 4*i + 2);
438 shuffles1[4*i + 2] = lp_build_const_int32(gallivm, length + 4*i);
439 shuffles1[4*i + 3] = i32undef;
440 shuffles2[4*i + 0] = lp_build_const_int32(gallivm, 4*i + 1);
441 shuffles2[4*i + 1] = lp_build_const_int32(gallivm, 4*i + 3);
442 shuffles2[4*i + 2] = lp_build_const_int32(gallivm, length + 4*i + 2);
443 shuffles2[4*i + 3] = i32undef;
444 }
445 rho_xvec = LLVMBuildShuffleVector(builder, ddx_ddy[0], ddx_ddy[1],
446 LLVMConstVector(shuffles1, length), "");
447 rho_yvec = LLVMBuildShuffleVector(builder, ddx_ddy[0], ddx_ddy[1],
448 LLVMConstVector(shuffles2, length), "");
449 }
450
451 rho_vec = lp_build_max(coord_bld, rho_xvec, rho_yvec);
452
453 if (bld->coord_type.length > 4) {
454 /* expand size to each quad */
455 if (dims > 1) {
456 /* could use some broadcast_vector helper for this? */
457 LLVMValueRef src[LP_MAX_VECTOR_LENGTH/4];
458 for (i = 0; i < num_quads; i++) {
459 src[i] = float_size;
460 }
461 float_size = lp_build_concat(bld->gallivm, src, float_size_bld->type, num_quads);
462 }
463 else {
464 float_size = lp_build_broadcast_scalar(coord_bld, float_size);
465 }
466 rho_vec = lp_build_mul(coord_bld, rho_vec, float_size);
467
468 if (dims <= 1) {
469 rho = rho_vec;
470 }
471 else {
472 if (dims >= 2) {
473 LLVMValueRef rho_s, rho_t, rho_r;
474
475 rho_s = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle0);
476 rho_t = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle1);
477
478 rho = lp_build_max(coord_bld, rho_s, rho_t);
479
480 if (dims >= 3) {
481 rho_r = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle2);
482 rho = lp_build_max(coord_bld, rho, rho_r);
483 }
484 }
485 }
486 if (rho_per_quad) {
487 rho = lp_build_pack_aos_scalars(bld->gallivm, coord_bld->type,
488 rho_bld->type, rho, 0);
489 }
490 else {
491 rho = lp_build_swizzle_scalar_aos(coord_bld, rho, 0, 4);
492 }
493 }
494 else {
495 if (dims <= 1) {
496 rho_vec = LLVMBuildExtractElement(builder, rho_vec, index0, "");
497 }
498 rho_vec = lp_build_mul(float_size_bld, rho_vec, float_size);
499
500 if (dims <= 1) {
501 rho = rho_vec;
502 }
503 else {
504 if (dims >= 2) {
505 LLVMValueRef rho_s, rho_t, rho_r;
506
507 rho_s = LLVMBuildExtractElement(builder, rho_vec, index0, "");
508 rho_t = LLVMBuildExtractElement(builder, rho_vec, index1, "");
509
510 rho = lp_build_max(float_bld, rho_s, rho_t);
511
512 if (dims >= 3) {
513 rho_r = LLVMBuildExtractElement(builder, rho_vec, index2, "");
514 rho = lp_build_max(float_bld, rho, rho_r);
515 }
516 }
517 }
518 if (!rho_per_quad) {
519 rho = lp_build_broadcast_scalar(rho_bld, rho);
520 }
521 }
522 }
523 }
524
525 return rho;
526 }
527
528
529 /*
530 * Bri-linear lod computation
531 *
532 * Use a piece-wise linear approximation of log2 such that:
533 * - round to nearest, for values in the neighborhood of -1, 0, 1, 2, etc.
534 * - linear approximation for values in the neighborhood of 0.5, 1.5., etc,
535 * with the steepness specified in 'factor'
536 * - exact result for 0.5, 1.5, etc.
537 *
538 *
539 * 1.0 - /----*
540 * /
541 * /
542 * /
543 * 0.5 - *
544 * /
545 * /
546 * /
547 * 0.0 - *----/
548 *
549 * | |
550 * 2^0 2^1
551 *
552 * This is a technique also commonly used in hardware:
553 * - http://ixbtlabs.com/articles2/gffx/nv40-rx800-3.html
554 *
555 * TODO: For correctness, this should only be applied when texture is known to
556 * have regular mipmaps, i.e., mipmaps derived from the base level.
557 *
558 * TODO: This could be done in fixed point, where applicable.
559 */
560 static void
561 lp_build_brilinear_lod(struct lp_build_context *bld,
562 LLVMValueRef lod,
563 double factor,
564 LLVMValueRef *out_lod_ipart,
565 LLVMValueRef *out_lod_fpart)
566 {
567 LLVMValueRef lod_fpart;
568 double pre_offset = (factor - 0.5)/factor - 0.5;
569 double post_offset = 1 - factor;
570
571 if (0) {
572 lp_build_printf(bld->gallivm, "lod = %f\n", lod);
573 }
574
575 lod = lp_build_add(bld, lod,
576 lp_build_const_vec(bld->gallivm, bld->type, pre_offset));
577
578 lp_build_ifloor_fract(bld, lod, out_lod_ipart, &lod_fpart);
579
580 lod_fpart = lp_build_mul(bld, lod_fpart,
581 lp_build_const_vec(bld->gallivm, bld->type, factor));
582
583 lod_fpart = lp_build_add(bld, lod_fpart,
584 lp_build_const_vec(bld->gallivm, bld->type, post_offset));
585
586 /*
587 * It's not necessary to clamp lod_fpart since:
588 * - the above expression will never produce numbers greater than one.
589 * - the mip filtering branch is only taken if lod_fpart is positive
590 */
591
592 *out_lod_fpart = lod_fpart;
593
594 if (0) {
595 lp_build_printf(bld->gallivm, "lod_ipart = %i\n", *out_lod_ipart);
596 lp_build_printf(bld->gallivm, "lod_fpart = %f\n\n", *out_lod_fpart);
597 }
598 }
599
600
601 /*
602 * Combined log2 and brilinear lod computation.
603 *
604 * It's in all identical to calling lp_build_fast_log2() and
605 * lp_build_brilinear_lod() above, but by combining we can compute the integer
606 * and fractional part independently.
607 */
608 static void
609 lp_build_brilinear_rho(struct lp_build_context *bld,
610 LLVMValueRef rho,
611 double factor,
612 LLVMValueRef *out_lod_ipart,
613 LLVMValueRef *out_lod_fpart)
614 {
615 LLVMValueRef lod_ipart;
616 LLVMValueRef lod_fpart;
617
618 const double pre_factor = (2*factor - 0.5)/(M_SQRT2*factor);
619 const double post_offset = 1 - 2*factor;
620
621 assert(bld->type.floating);
622
623 assert(lp_check_value(bld->type, rho));
624
625 /*
626 * The pre factor will make the intersections with the exact powers of two
627 * happen precisely where we want them to be, which means that the integer
628 * part will not need any post adjustments.
629 */
630 rho = lp_build_mul(bld, rho,
631 lp_build_const_vec(bld->gallivm, bld->type, pre_factor));
632
633 /* ipart = ifloor(log2(rho)) */
634 lod_ipart = lp_build_extract_exponent(bld, rho, 0);
635
636 /* fpart = rho / 2**ipart */
637 lod_fpart = lp_build_extract_mantissa(bld, rho);
638
639 lod_fpart = lp_build_mul(bld, lod_fpart,
640 lp_build_const_vec(bld->gallivm, bld->type, factor));
641
642 lod_fpart = lp_build_add(bld, lod_fpart,
643 lp_build_const_vec(bld->gallivm, bld->type, post_offset));
644
645 /*
646 * Like lp_build_brilinear_lod, it's not necessary to clamp lod_fpart since:
647 * - the above expression will never produce numbers greater than one.
648 * - the mip filtering branch is only taken if lod_fpart is positive
649 */
650
651 *out_lod_ipart = lod_ipart;
652 *out_lod_fpart = lod_fpart;
653 }
654
655
656 /**
657 * Fast implementation of iround(log2(sqrt(x))), based on
658 * log2(x^n) == n*log2(x).
659 *
660 * Gives accurate results all the time.
661 * (Could be trivially extended to handle other power-of-two roots.)
662 */
663 static LLVMValueRef
664 lp_build_ilog2_sqrt(struct lp_build_context *bld,
665 LLVMValueRef x)
666 {
667 LLVMBuilderRef builder = bld->gallivm->builder;
668 LLVMValueRef ipart;
669 struct lp_type i_type = lp_int_type(bld->type);
670 LLVMValueRef one = lp_build_const_int_vec(bld->gallivm, i_type, 1);
671
672 assert(bld->type.floating);
673
674 assert(lp_check_value(bld->type, x));
675
676 /* ipart = log2(x) + 0.5 = 0.5*(log2(x^2) + 1.0) */
677 ipart = lp_build_extract_exponent(bld, x, 1);
678 ipart = LLVMBuildAShr(builder, ipart, one, "");
679
680 return ipart;
681 }
682
683
684 /**
685 * Generate code to compute texture level of detail (lambda).
686 * \param derivs partial derivatives of (s, t, r, q) with respect to X and Y
687 * \param lod_bias optional float vector with the shader lod bias
688 * \param explicit_lod optional float vector with the explicit lod
689 * \param cube_rho rho calculated by cube coord mapping (optional)
690 * \param out_lod_ipart integer part of lod
691 * \param out_lod_fpart float part of lod (never larger than 1 but may be negative)
692 * \param out_lod_positive (mask) if lod is positive (i.e. texture is minified)
693 *
694 * The resulting lod can be scalar per quad or be per element.
695 */
696 void
697 lp_build_lod_selector(struct lp_build_sample_context *bld,
698 unsigned texture_unit,
699 unsigned sampler_unit,
700 LLVMValueRef s,
701 LLVMValueRef t,
702 LLVMValueRef r,
703 LLVMValueRef cube_rho,
704 const struct lp_derivatives *derivs,
705 LLVMValueRef lod_bias, /* optional */
706 LLVMValueRef explicit_lod, /* optional */
707 unsigned mip_filter,
708 LLVMValueRef *out_lod_ipart,
709 LLVMValueRef *out_lod_fpart,
710 LLVMValueRef *out_lod_positive)
711
712 {
713 LLVMBuilderRef builder = bld->gallivm->builder;
714 struct lp_build_context *lodf_bld = &bld->lodf_bld;
715 LLVMValueRef lod;
716
717 *out_lod_ipart = bld->lodi_bld.zero;
718 *out_lod_positive = bld->lodi_bld.zero;
719 *out_lod_fpart = lodf_bld->zero;
720
721 /*
722 * For determining min/mag, we follow GL 4.1 spec, 3.9.12 Texture Magnification:
723 * "Implementations may either unconditionally assume c = 0 for the minification
724 * vs. magnification switch-over point, or may choose to make c depend on the
725 * combination of minification and magnification modes as follows: if the
726 * magnification filter is given by LINEAR and the minification filter is given
727 * by NEAREST_MIPMAP_NEAREST or NEAREST_MIPMAP_LINEAR, then c = 0.5. This is
728 * done to ensure that a minified texture does not appear "sharper" than a
729 * magnified texture. Otherwise c = 0."
730 * And 3.9.11 Texture Minification:
731 * "If lod is less than or equal to the constant c (see section 3.9.12) the
732 * texture is said to be magnified; if it is greater, the texture is minified."
733 * So, using 0 as switchover point always, and using magnification for lod == 0.
734 * Note that the always c = 0 behavior is new (first appearing in GL 3.1 spec),
735 * old GL versions required 0.5 for the modes listed above.
736 * I have no clue about the (undocumented) wishes of d3d9/d3d10 here!
737 */
738
739 if (bld->static_sampler_state->min_max_lod_equal) {
740 /* User is forcing sampling from a particular mipmap level.
741 * This is hit during mipmap generation.
742 */
743 LLVMValueRef min_lod =
744 bld->dynamic_state->min_lod(bld->dynamic_state,
745 bld->gallivm, sampler_unit);
746
747 lod = lp_build_broadcast_scalar(lodf_bld, min_lod);
748 }
749 else {
750 if (explicit_lod) {
751 if (bld->num_lods != bld->coord_type.length)
752 lod = lp_build_pack_aos_scalars(bld->gallivm, bld->coord_bld.type,
753 lodf_bld->type, explicit_lod, 0);
754 else
755 lod = explicit_lod;
756 }
757 else {
758 LLVMValueRef rho;
759 boolean rho_squared = ((gallivm_debug & GALLIVM_DEBUG_NO_RHO_APPROX) &&
760 (bld->dims > 1)) || cube_rho;
761
762 rho = lp_build_rho(bld, texture_unit, s, t, r, cube_rho, derivs);
763
764 /*
765 * Compute lod = log2(rho)
766 */
767
768 if (!lod_bias &&
769 !bld->static_sampler_state->lod_bias_non_zero &&
770 !bld->static_sampler_state->apply_max_lod &&
771 !bld->static_sampler_state->apply_min_lod) {
772 /*
773 * Special case when there are no post-log2 adjustments, which
774 * saves instructions but keeping the integer and fractional lod
775 * computations separate from the start.
776 */
777
778 if (mip_filter == PIPE_TEX_MIPFILTER_NONE ||
779 mip_filter == PIPE_TEX_MIPFILTER_NEAREST) {
780 /*
781 * Don't actually need both values all the time, lod_ipart is
782 * needed for nearest mipfilter, lod_positive if min != mag.
783 */
784 if (rho_squared) {
785 *out_lod_ipart = lp_build_ilog2_sqrt(lodf_bld, rho);
786 }
787 else {
788 *out_lod_ipart = lp_build_ilog2(lodf_bld, rho);
789 }
790 *out_lod_positive = lp_build_cmp(lodf_bld, PIPE_FUNC_GREATER,
791 rho, lodf_bld->one);
792 return;
793 }
794 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR &&
795 !(gallivm_debug & GALLIVM_DEBUG_NO_BRILINEAR) &&
796 !rho_squared) {
797 /*
798 * This can't work if rho is squared. Not sure if it could be
799 * fixed while keeping it worthwile, could also do sqrt here
800 * but brilinear and no_rho_opt seems like a combination not
801 * making much sense anyway so just use ordinary path below.
802 */
803 lp_build_brilinear_rho(lodf_bld, rho, BRILINEAR_FACTOR,
804 out_lod_ipart, out_lod_fpart);
805 *out_lod_positive = lp_build_cmp(lodf_bld, PIPE_FUNC_GREATER,
806 rho, lodf_bld->one);
807 return;
808 }
809 }
810
811 if (0) {
812 lod = lp_build_log2(lodf_bld, rho);
813 }
814 else {
815 lod = lp_build_fast_log2(lodf_bld, rho);
816 }
817 if (rho_squared) {
818 /* log2(x^2) == 0.5*log2(x) */
819 lod = lp_build_mul(lodf_bld, lod,
820 lp_build_const_vec(bld->gallivm, lodf_bld->type, 0.5F));
821 }
822
823 /* add shader lod bias */
824 if (lod_bias) {
825 if (bld->num_lods != bld->coord_type.length)
826 lod_bias = lp_build_pack_aos_scalars(bld->gallivm, bld->coord_bld.type,
827 lodf_bld->type, lod_bias, 0);
828 lod = LLVMBuildFAdd(builder, lod, lod_bias, "shader_lod_bias");
829 }
830 }
831
832 /* add sampler lod bias */
833 if (bld->static_sampler_state->lod_bias_non_zero) {
834 LLVMValueRef sampler_lod_bias =
835 bld->dynamic_state->lod_bias(bld->dynamic_state,
836 bld->gallivm, sampler_unit);
837 sampler_lod_bias = lp_build_broadcast_scalar(lodf_bld,
838 sampler_lod_bias);
839 lod = LLVMBuildFAdd(builder, lod, sampler_lod_bias, "sampler_lod_bias");
840 }
841
842 /* clamp lod */
843 if (bld->static_sampler_state->apply_max_lod) {
844 LLVMValueRef max_lod =
845 bld->dynamic_state->max_lod(bld->dynamic_state,
846 bld->gallivm, sampler_unit);
847 max_lod = lp_build_broadcast_scalar(lodf_bld, max_lod);
848
849 lod = lp_build_min(lodf_bld, lod, max_lod);
850 }
851 if (bld->static_sampler_state->apply_min_lod) {
852 LLVMValueRef min_lod =
853 bld->dynamic_state->min_lod(bld->dynamic_state,
854 bld->gallivm, sampler_unit);
855 min_lod = lp_build_broadcast_scalar(lodf_bld, min_lod);
856
857 lod = lp_build_max(lodf_bld, lod, min_lod);
858 }
859 }
860
861 *out_lod_positive = lp_build_cmp(lodf_bld, PIPE_FUNC_GREATER,
862 lod, lodf_bld->zero);
863
864 if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
865 if (!(gallivm_debug & GALLIVM_DEBUG_NO_BRILINEAR)) {
866 lp_build_brilinear_lod(lodf_bld, lod, BRILINEAR_FACTOR,
867 out_lod_ipart, out_lod_fpart);
868 }
869 else {
870 lp_build_ifloor_fract(lodf_bld, lod, out_lod_ipart, out_lod_fpart);
871 }
872
873 lp_build_name(*out_lod_fpart, "lod_fpart");
874 }
875 else {
876 *out_lod_ipart = lp_build_iround(lodf_bld, lod);
877 }
878
879 lp_build_name(*out_lod_ipart, "lod_ipart");
880
881 return;
882 }
883
884
885 /**
886 * For PIPE_TEX_MIPFILTER_NEAREST, convert int part of lod
887 * to actual mip level.
888 * Note: this is all scalar per quad code.
889 * \param lod_ipart int texture level of detail
890 * \param level_out returns integer
891 * \param out_of_bounds returns per coord out_of_bounds mask if provided
892 */
893 void
894 lp_build_nearest_mip_level(struct lp_build_sample_context *bld,
895 unsigned texture_unit,
896 LLVMValueRef lod_ipart,
897 LLVMValueRef *level_out,
898 LLVMValueRef *out_of_bounds)
899 {
900 struct lp_build_context *leveli_bld = &bld->leveli_bld;
901 LLVMValueRef first_level, last_level, level;
902
903 first_level = bld->dynamic_state->first_level(bld->dynamic_state,
904 bld->gallivm, texture_unit);
905 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
906 bld->gallivm, texture_unit);
907 first_level = lp_build_broadcast_scalar(leveli_bld, first_level);
908 last_level = lp_build_broadcast_scalar(leveli_bld, last_level);
909
910 level = lp_build_add(leveli_bld, lod_ipart, first_level);
911
912 if (out_of_bounds) {
913 LLVMValueRef out, out1;
914 out = lp_build_cmp(leveli_bld, PIPE_FUNC_LESS, level, first_level);
915 out1 = lp_build_cmp(leveli_bld, PIPE_FUNC_GREATER, level, last_level);
916 out = lp_build_or(leveli_bld, out, out1);
917 if (bld->num_mips == bld->coord_bld.type.length) {
918 *out_of_bounds = out;
919 }
920 else if (bld->num_mips == 1) {
921 *out_of_bounds = lp_build_broadcast_scalar(&bld->int_coord_bld, out);
922 }
923 else {
924 assert(bld->num_mips == bld->coord_bld.type.length / 4);
925 *out_of_bounds = lp_build_unpack_broadcast_aos_scalars(bld->gallivm,
926 leveli_bld->type,
927 bld->int_coord_bld.type,
928 out);
929 }
930 *level_out = level;
931 }
932 else {
933 /* clamp level to legal range of levels */
934 *level_out = lp_build_clamp(leveli_bld, level, first_level, last_level);
935
936 }
937 }
938
939
940 /**
941 * For PIPE_TEX_MIPFILTER_LINEAR, convert per-quad (or per element) int LOD(s)
942 * to two (per-quad) (adjacent) mipmap level indexes, and fix up float lod
943 * part accordingly.
944 * Later, we'll sample from those two mipmap levels and interpolate between them.
945 */
946 void
947 lp_build_linear_mip_levels(struct lp_build_sample_context *bld,
948 unsigned texture_unit,
949 LLVMValueRef lod_ipart,
950 LLVMValueRef *lod_fpart_inout,
951 LLVMValueRef *level0_out,
952 LLVMValueRef *level1_out)
953 {
954 LLVMBuilderRef builder = bld->gallivm->builder;
955 struct lp_build_context *leveli_bld = &bld->leveli_bld;
956 struct lp_build_context *levelf_bld = &bld->levelf_bld;
957 LLVMValueRef first_level, last_level;
958 LLVMValueRef clamp_min;
959 LLVMValueRef clamp_max;
960
961 assert(bld->num_lods == bld->num_mips);
962
963 first_level = bld->dynamic_state->first_level(bld->dynamic_state,
964 bld->gallivm, texture_unit);
965 last_level = bld->dynamic_state->last_level(bld->dynamic_state,
966 bld->gallivm, texture_unit);
967 first_level = lp_build_broadcast_scalar(leveli_bld, first_level);
968 last_level = lp_build_broadcast_scalar(leveli_bld, last_level);
969
970 *level0_out = lp_build_add(leveli_bld, lod_ipart, first_level);
971 *level1_out = lp_build_add(leveli_bld, *level0_out, leveli_bld->one);
972
973 /*
974 * Clamp both *level0_out and *level1_out to [first_level, last_level], with
975 * the minimum number of comparisons, and zeroing lod_fpart in the extreme
976 * ends in the process.
977 */
978
979 /*
980 * This code (vector select in particular) only works with llvm 3.1
981 * (if there's more than one quad, with x86 backend). Might consider
982 * converting to our lp_bld_logic helpers.
983 */
984 #if HAVE_LLVM < 0x0301
985 assert(leveli_bld->type.length == 1);
986 #endif
987
988 /* *level0_out < first_level */
989 clamp_min = LLVMBuildICmp(builder, LLVMIntSLT,
990 *level0_out, first_level,
991 "clamp_lod_to_first");
992
993 *level0_out = LLVMBuildSelect(builder, clamp_min,
994 first_level, *level0_out, "");
995
996 *level1_out = LLVMBuildSelect(builder, clamp_min,
997 first_level, *level1_out, "");
998
999 *lod_fpart_inout = LLVMBuildSelect(builder, clamp_min,
1000 levelf_bld->zero, *lod_fpart_inout, "");
1001
1002 /* *level0_out >= last_level */
1003 clamp_max = LLVMBuildICmp(builder, LLVMIntSGE,
1004 *level0_out, last_level,
1005 "clamp_lod_to_last");
1006
1007 *level0_out = LLVMBuildSelect(builder, clamp_max,
1008 last_level, *level0_out, "");
1009
1010 *level1_out = LLVMBuildSelect(builder, clamp_max,
1011 last_level, *level1_out, "");
1012
1013 *lod_fpart_inout = LLVMBuildSelect(builder, clamp_max,
1014 levelf_bld->zero, *lod_fpart_inout, "");
1015
1016 lp_build_name(*level0_out, "texture%u_miplevel0", texture_unit);
1017 lp_build_name(*level1_out, "texture%u_miplevel1", texture_unit);
1018 lp_build_name(*lod_fpart_inout, "texture%u_mipweight", texture_unit);
1019 }
1020
1021
1022 /**
1023 * Return pointer to a single mipmap level.
1024 * \param level integer mipmap level
1025 */
1026 LLVMValueRef
1027 lp_build_get_mipmap_level(struct lp_build_sample_context *bld,
1028 LLVMValueRef level)
1029 {
1030 LLVMBuilderRef builder = bld->gallivm->builder;
1031 LLVMValueRef indexes[2], data_ptr, mip_offset;
1032
1033 indexes[0] = lp_build_const_int32(bld->gallivm, 0);
1034 indexes[1] = level;
1035 mip_offset = LLVMBuildGEP(builder, bld->mip_offsets, indexes, 2, "");
1036 mip_offset = LLVMBuildLoad(builder, mip_offset, "");
1037 data_ptr = LLVMBuildGEP(builder, bld->base_ptr, &mip_offset, 1, "");
1038 return data_ptr;
1039 }
1040
1041 /**
1042 * Return (per-pixel) offsets to mip levels.
1043 * \param level integer mipmap level
1044 */
1045 LLVMValueRef
1046 lp_build_get_mip_offsets(struct lp_build_sample_context *bld,
1047 LLVMValueRef level)
1048 {
1049 LLVMBuilderRef builder = bld->gallivm->builder;
1050 LLVMValueRef indexes[2], offsets, offset1;
1051
1052 indexes[0] = lp_build_const_int32(bld->gallivm, 0);
1053 if (bld->num_mips == 1) {
1054 indexes[1] = level;
1055 offset1 = LLVMBuildGEP(builder, bld->mip_offsets, indexes, 2, "");
1056 offset1 = LLVMBuildLoad(builder, offset1, "");
1057 offsets = lp_build_broadcast_scalar(&bld->int_coord_bld, offset1);
1058 }
1059 else if (bld->num_mips == bld->coord_bld.type.length / 4) {
1060 unsigned i;
1061
1062 offsets = bld->int_coord_bld.undef;
1063 for (i = 0; i < bld->num_mips; i++) {
1064 LLVMValueRef indexi = lp_build_const_int32(bld->gallivm, i);
1065 LLVMValueRef indexo = lp_build_const_int32(bld->gallivm, 4 * i);
1066 indexes[1] = LLVMBuildExtractElement(builder, level, indexi, "");
1067 offset1 = LLVMBuildGEP(builder, bld->mip_offsets, indexes, 2, "");
1068 offset1 = LLVMBuildLoad(builder, offset1, "");
1069 offsets = LLVMBuildInsertElement(builder, offsets, offset1, indexo, "");
1070 }
1071 offsets = lp_build_swizzle_scalar_aos(&bld->int_coord_bld, offsets, 0, 4);
1072 }
1073 else {
1074 unsigned i;
1075
1076 assert (bld->num_mips == bld->coord_bld.type.length);
1077
1078 offsets = bld->int_coord_bld.undef;
1079 for (i = 0; i < bld->num_mips; i++) {
1080 LLVMValueRef indexi = lp_build_const_int32(bld->gallivm, i);
1081 indexes[1] = LLVMBuildExtractElement(builder, level, indexi, "");
1082 offset1 = LLVMBuildGEP(builder, bld->mip_offsets, indexes, 2, "");
1083 offset1 = LLVMBuildLoad(builder, offset1, "");
1084 offsets = LLVMBuildInsertElement(builder, offsets, offset1, indexi, "");
1085 }
1086 }
1087 return offsets;
1088 }
1089
1090
1091 /**
1092 * Codegen equivalent for u_minify().
1093 * @param lod_scalar if lod is a (broadcasted) scalar
1094 * Return max(1, base_size >> level);
1095 */
1096 LLVMValueRef
1097 lp_build_minify(struct lp_build_context *bld,
1098 LLVMValueRef base_size,
1099 LLVMValueRef level,
1100 boolean lod_scalar)
1101 {
1102 LLVMBuilderRef builder = bld->gallivm->builder;
1103 assert(lp_check_value(bld->type, base_size));
1104 assert(lp_check_value(bld->type, level));
1105
1106 if (level == bld->zero) {
1107 /* if we're using mipmap level zero, no minification is needed */
1108 return base_size;
1109 }
1110 else {
1111 LLVMValueRef size;
1112 assert(bld->type.sign);
1113 if (lod_scalar ||
1114 (util_cpu_caps.has_avx2 || !util_cpu_caps.has_sse)) {
1115 size = LLVMBuildLShr(builder, base_size, level, "minify");
1116 size = lp_build_max(bld, size, bld->one);
1117 }
1118 else {
1119 /*
1120 * emulate shift with float mul, since intel "forgot" shifts with
1121 * per-element shift count until avx2, which results in terrible
1122 * scalar extraction (both count and value), scalar shift,
1123 * vector reinsertion. Should not be an issue on any non-x86 cpu
1124 * with a vector instruction set.
1125 * On cpus with AMD's XOP this should also be unnecessary but I'm
1126 * not sure if llvm would emit this with current flags.
1127 */
1128 LLVMValueRef const127, const23, lf;
1129 struct lp_type ftype;
1130 struct lp_build_context fbld;
1131 ftype = lp_type_float_vec(32, bld->type.length * bld->type.width);
1132 lp_build_context_init(&fbld, bld->gallivm, ftype);
1133 const127 = lp_build_const_int_vec(bld->gallivm, bld->type, 127);
1134 const23 = lp_build_const_int_vec(bld->gallivm, bld->type, 23);
1135
1136 /* calculate 2^(-level) float */
1137 lf = lp_build_sub(bld, const127, level);
1138 lf = lp_build_shl(bld, lf, const23);
1139 lf = LLVMBuildBitCast(builder, lf, fbld.vec_type, "");
1140
1141 /* finish shift operation by doing float mul */
1142 base_size = lp_build_int_to_float(&fbld, base_size);
1143 size = lp_build_mul(&fbld, base_size, lf);
1144 /*
1145 * do the max also with floats because
1146 * a) non-emulated int max requires sse41
1147 * (this is actually a lie as we could cast to 16bit values
1148 * as 16bit is sufficient and 16bit int max is sse2)
1149 * b) with avx we can do int max 4-wide but float max 8-wide
1150 */
1151 size = lp_build_max(&fbld, size, fbld.one);
1152 size = lp_build_itrunc(&fbld, size);
1153 }
1154 return size;
1155 }
1156 }
1157
1158
1159 /**
1160 * Dereference stride_array[mipmap_level] array to get a stride.
1161 * Return stride as a vector.
1162 */
1163 static LLVMValueRef
1164 lp_build_get_level_stride_vec(struct lp_build_sample_context *bld,
1165 LLVMValueRef stride_array, LLVMValueRef level)
1166 {
1167 LLVMBuilderRef builder = bld->gallivm->builder;
1168 LLVMValueRef indexes[2], stride, stride1;
1169 indexes[0] = lp_build_const_int32(bld->gallivm, 0);
1170 if (bld->num_mips == 1) {
1171 indexes[1] = level;
1172 stride1 = LLVMBuildGEP(builder, stride_array, indexes, 2, "");
1173 stride1 = LLVMBuildLoad(builder, stride1, "");
1174 stride = lp_build_broadcast_scalar(&bld->int_coord_bld, stride1);
1175 }
1176 else if (bld->num_mips == bld->coord_bld.type.length / 4) {
1177 LLVMValueRef stride1;
1178 unsigned i;
1179
1180 stride = bld->int_coord_bld.undef;
1181 for (i = 0; i < bld->num_mips; i++) {
1182 LLVMValueRef indexi = lp_build_const_int32(bld->gallivm, i);
1183 LLVMValueRef indexo = lp_build_const_int32(bld->gallivm, 4 * i);
1184 indexes[1] = LLVMBuildExtractElement(builder, level, indexi, "");
1185 stride1 = LLVMBuildGEP(builder, stride_array, indexes, 2, "");
1186 stride1 = LLVMBuildLoad(builder, stride1, "");
1187 stride = LLVMBuildInsertElement(builder, stride, stride1, indexo, "");
1188 }
1189 stride = lp_build_swizzle_scalar_aos(&bld->int_coord_bld, stride, 0, 4);
1190 }
1191 else {
1192 LLVMValueRef stride1;
1193 unsigned i;
1194
1195 assert (bld->num_mips == bld->coord_bld.type.length);
1196
1197 stride = bld->int_coord_bld.undef;
1198 for (i = 0; i < bld->coord_bld.type.length; i++) {
1199 LLVMValueRef indexi = lp_build_const_int32(bld->gallivm, i);
1200 indexes[1] = LLVMBuildExtractElement(builder, level, indexi, "");
1201 stride1 = LLVMBuildGEP(builder, stride_array, indexes, 2, "");
1202 stride1 = LLVMBuildLoad(builder, stride1, "");
1203 stride = LLVMBuildInsertElement(builder, stride, stride1, indexi, "");
1204 }
1205 }
1206 return stride;
1207 }
1208
1209
1210 /**
1211 * When sampling a mipmap, we need to compute the width, height, depth
1212 * of the source levels from the level indexes. This helper function
1213 * does that.
1214 */
1215 void
1216 lp_build_mipmap_level_sizes(struct lp_build_sample_context *bld,
1217 LLVMValueRef ilevel,
1218 LLVMValueRef *out_size,
1219 LLVMValueRef *row_stride_vec,
1220 LLVMValueRef *img_stride_vec)
1221 {
1222 const unsigned dims = bld->dims;
1223 LLVMValueRef ilevel_vec;
1224
1225 /*
1226 * Compute width, height, depth at mipmap level 'ilevel'
1227 */
1228 if (bld->num_mips == 1) {
1229 ilevel_vec = lp_build_broadcast_scalar(&bld->int_size_bld, ilevel);
1230 *out_size = lp_build_minify(&bld->int_size_bld, bld->int_size, ilevel_vec, TRUE);
1231 }
1232 else {
1233 LLVMValueRef int_size_vec;
1234 LLVMValueRef tmp[LP_MAX_VECTOR_LENGTH];
1235 unsigned num_quads = bld->coord_bld.type.length / 4;
1236 unsigned i;
1237
1238 if (bld->num_mips == num_quads) {
1239 /*
1240 * XXX: this should be #ifndef SANE_INSTRUCTION_SET.
1241 * intel "forgot" the variable shift count instruction until avx2.
1242 * A harmless 8x32 shift gets translated into 32 instructions
1243 * (16 extracts, 8 scalar shifts, 8 inserts), llvm is apparently
1244 * unable to recognize if there are really just 2 different shift
1245 * count values. So do the shift 4-wide before expansion.
1246 */
1247 struct lp_build_context bld4;
1248 struct lp_type type4;
1249
1250 type4 = bld->int_coord_bld.type;
1251 type4.length = 4;
1252
1253 lp_build_context_init(&bld4, bld->gallivm, type4);
1254
1255 if (bld->dims == 1) {
1256 assert(bld->int_size_in_bld.type.length == 1);
1257 int_size_vec = lp_build_broadcast_scalar(&bld4,
1258 bld->int_size);
1259 }
1260 else {
1261 assert(bld->int_size_in_bld.type.length == 4);
1262 int_size_vec = bld->int_size;
1263 }
1264
1265 for (i = 0; i < num_quads; i++) {
1266 LLVMValueRef ileveli;
1267 LLVMValueRef indexi = lp_build_const_int32(bld->gallivm, i);
1268
1269 ileveli = lp_build_extract_broadcast(bld->gallivm,
1270 bld->leveli_bld.type,
1271 bld4.type,
1272 ilevel,
1273 indexi);
1274 tmp[i] = lp_build_minify(&bld4, int_size_vec, ileveli, TRUE);
1275 }
1276 /*
1277 * out_size is [w0, h0, d0, _, w1, h1, d1, _, ...] vector for dims > 1,
1278 * [w0, w0, w0, w0, w1, w1, w1, w1, ...] otherwise.
1279 */
1280 *out_size = lp_build_concat(bld->gallivm,
1281 tmp,
1282 bld4.type,
1283 num_quads);
1284 }
1285 else {
1286 /* FIXME: this is terrible and results in _huge_ vector
1287 * (for the dims > 1 case).
1288 * Should refactor this (together with extract_image_sizes) and do
1289 * something more useful. Could for instance if we have width,height
1290 * with 4-wide vector pack all elements into a 8xi16 vector
1291 * (on which we can still do useful math) instead of using a 16xi32
1292 * vector.
1293 * For dims == 1 this will create [w0, w1, w2, w3, ...] vector.
1294 * For dims > 1 this will create [w0, h0, d0, _, w1, h1, d1, _, ...] vector.
1295 */
1296 assert(bld->num_mips == bld->coord_bld.type.length);
1297 if (bld->dims == 1) {
1298 assert(bld->int_size_in_bld.type.length == 1);
1299 int_size_vec = lp_build_broadcast_scalar(&bld->int_coord_bld,
1300 bld->int_size);
1301 *out_size = lp_build_minify(&bld->int_coord_bld, int_size_vec, ilevel, FALSE);
1302 }
1303 else {
1304 LLVMValueRef ilevel1;
1305 for (i = 0; i < bld->num_mips; i++) {
1306 LLVMValueRef indexi = lp_build_const_int32(bld->gallivm, i);
1307 ilevel1 = lp_build_extract_broadcast(bld->gallivm, bld->int_coord_type,
1308 bld->int_size_in_bld.type, ilevel, indexi);
1309 tmp[i] = bld->int_size;
1310 tmp[i] = lp_build_minify(&bld->int_size_in_bld, tmp[i], ilevel1, TRUE);
1311 }
1312 *out_size = lp_build_concat(bld->gallivm, tmp,
1313 bld->int_size_in_bld.type,
1314 bld->num_mips);
1315 }
1316 }
1317 }
1318
1319 if (dims >= 2) {
1320 *row_stride_vec = lp_build_get_level_stride_vec(bld,
1321 bld->row_stride_array,
1322 ilevel);
1323 }
1324 if (dims == 3 ||
1325 bld->static_texture_state->target == PIPE_TEXTURE_CUBE ||
1326 bld->static_texture_state->target == PIPE_TEXTURE_1D_ARRAY ||
1327 bld->static_texture_state->target == PIPE_TEXTURE_2D_ARRAY) {
1328 *img_stride_vec = lp_build_get_level_stride_vec(bld,
1329 bld->img_stride_array,
1330 ilevel);
1331 }
1332 }
1333
1334
1335 /**
1336 * Extract and broadcast texture size.
1337 *
1338 * @param size_type type of the texture size vector (either
1339 * bld->int_size_type or bld->float_size_type)
1340 * @param coord_type type of the texture size vector (either
1341 * bld->int_coord_type or bld->coord_type)
1342 * @param size vector with the texture size (width, height, depth)
1343 */
1344 void
1345 lp_build_extract_image_sizes(struct lp_build_sample_context *bld,
1346 struct lp_build_context *size_bld,
1347 struct lp_type coord_type,
1348 LLVMValueRef size,
1349 LLVMValueRef *out_width,
1350 LLVMValueRef *out_height,
1351 LLVMValueRef *out_depth)
1352 {
1353 const unsigned dims = bld->dims;
1354 LLVMTypeRef i32t = LLVMInt32TypeInContext(bld->gallivm->context);
1355 struct lp_type size_type = size_bld->type;
1356
1357 if (bld->num_mips == 1) {
1358 *out_width = lp_build_extract_broadcast(bld->gallivm,
1359 size_type,
1360 coord_type,
1361 size,
1362 LLVMConstInt(i32t, 0, 0));
1363 if (dims >= 2) {
1364 *out_height = lp_build_extract_broadcast(bld->gallivm,
1365 size_type,
1366 coord_type,
1367 size,
1368 LLVMConstInt(i32t, 1, 0));
1369 if (dims == 3) {
1370 *out_depth = lp_build_extract_broadcast(bld->gallivm,
1371 size_type,
1372 coord_type,
1373 size,
1374 LLVMConstInt(i32t, 2, 0));
1375 }
1376 }
1377 }
1378 else {
1379 unsigned num_quads = bld->coord_bld.type.length / 4;
1380
1381 if (dims == 1) {
1382 *out_width = size;
1383 }
1384 else if (bld->num_mips == num_quads) {
1385 *out_width = lp_build_swizzle_scalar_aos(size_bld, size, 0, 4);
1386 if (dims >= 2) {
1387 *out_height = lp_build_swizzle_scalar_aos(size_bld, size, 1, 4);
1388 if (dims == 3) {
1389 *out_depth = lp_build_swizzle_scalar_aos(size_bld, size, 2, 4);
1390 }
1391 }
1392 }
1393 else {
1394 assert(bld->num_mips == bld->coord_type.length);
1395 *out_width = lp_build_pack_aos_scalars(bld->gallivm, size_type,
1396 coord_type, size, 0);
1397 if (dims >= 2) {
1398 *out_height = lp_build_pack_aos_scalars(bld->gallivm, size_type,
1399 coord_type, size, 1);
1400 if (dims == 3) {
1401 *out_depth = lp_build_pack_aos_scalars(bld->gallivm, size_type,
1402 coord_type, size, 2);
1403 }
1404 }
1405 }
1406 }
1407 }
1408
1409
1410 /**
1411 * Unnormalize coords.
1412 *
1413 * @param flt_size vector with the integer texture size (width, height, depth)
1414 */
1415 void
1416 lp_build_unnormalized_coords(struct lp_build_sample_context *bld,
1417 LLVMValueRef flt_size,
1418 LLVMValueRef *s,
1419 LLVMValueRef *t,
1420 LLVMValueRef *r)
1421 {
1422 const unsigned dims = bld->dims;
1423 LLVMValueRef width;
1424 LLVMValueRef height;
1425 LLVMValueRef depth;
1426
1427 lp_build_extract_image_sizes(bld,
1428 &bld->float_size_bld,
1429 bld->coord_type,
1430 flt_size,
1431 &width,
1432 &height,
1433 &depth);
1434
1435 /* s = s * width, t = t * height */
1436 *s = lp_build_mul(&bld->coord_bld, *s, width);
1437 if (dims >= 2) {
1438 *t = lp_build_mul(&bld->coord_bld, *t, height);
1439 if (dims >= 3) {
1440 *r = lp_build_mul(&bld->coord_bld, *r, depth);
1441 }
1442 }
1443 }
1444
1445 /**
1446 * Generate new coords and faces for cubemap texels falling off the face.
1447 *
1448 * @param face face (center) of the pixel
1449 * @param x0 lower x coord
1450 * @param x1 higher x coord (must be x0 + 1)
1451 * @param y0 lower y coord
1452 * @param y1 higher y coord (must be x0 + 1)
1453 * @param max_coord texture cube (level) size - 1
1454 * @param next_faces new face values when falling off
1455 * @param next_xcoords new x coord values when falling off
1456 * @param next_ycoords new y coord values when falling off
1457 *
1458 * The arrays hold the new values when under/overflow of
1459 * lower x, higher x, lower y, higher y coord would occur (in this order).
1460 * next_xcoords/next_ycoords have two entries each (for both new lower and
1461 * higher coord).
1462 */
1463 void
1464 lp_build_cube_new_coords(struct lp_build_context *ivec_bld,
1465 LLVMValueRef face,
1466 LLVMValueRef x0,
1467 LLVMValueRef x1,
1468 LLVMValueRef y0,
1469 LLVMValueRef y1,
1470 LLVMValueRef max_coord,
1471 LLVMValueRef next_faces[4],
1472 LLVMValueRef next_xcoords[4][2],
1473 LLVMValueRef next_ycoords[4][2])
1474 {
1475 /*
1476 * Lookup tables aren't nice for simd code hence try some logic here.
1477 * (Note that while it would not be necessary to do per-sample (4) lookups
1478 * when using a LUT as it's impossible that texels fall off of positive
1479 * and negative edges simultaneously, it would however be necessary to
1480 * do 2 lookups for corner handling as in this case texels both fall off
1481 * of x and y axes.)
1482 */
1483 /*
1484 * Next faces (for face 012345):
1485 * x < 0.0 : 451110
1486 * x >= 1.0 : 540001
1487 * y < 0.0 : 225422
1488 * y >= 1.0 : 334533
1489 * Hence nfx+ (and nfy+) == nfx- (nfy-) xor 1
1490 * nfx-: face > 1 ? (face == 5 ? 0 : 1) : (4 + face & 1)
1491 * nfy+: face & ~4 > 1 ? face + 2 : 3;
1492 * This could also use pshufb instead, but would need (manually coded)
1493 * ssse3 intrinsic (llvm won't do non-constant shuffles).
1494 */
1495 struct gallivm_state *gallivm = ivec_bld->gallivm;
1496 LLVMValueRef sel, sel_f2345, sel_f23, sel_f2, tmpsel, tmp;
1497 LLVMValueRef faceand1, sel_fand1, maxmx0, maxmx1, maxmy0, maxmy1;
1498 LLVMValueRef c2 = lp_build_const_int_vec(gallivm, ivec_bld->type, 2);
1499 LLVMValueRef c3 = lp_build_const_int_vec(gallivm, ivec_bld->type, 3);
1500 LLVMValueRef c4 = lp_build_const_int_vec(gallivm, ivec_bld->type, 4);
1501 LLVMValueRef c5 = lp_build_const_int_vec(gallivm, ivec_bld->type, 5);
1502
1503 sel = lp_build_cmp(ivec_bld, PIPE_FUNC_EQUAL, face, c5);
1504 tmpsel = lp_build_select(ivec_bld, sel, ivec_bld->zero, ivec_bld->one);
1505 sel_f2345 = lp_build_cmp(ivec_bld, PIPE_FUNC_GREATER, face, ivec_bld->one);
1506 faceand1 = lp_build_and(ivec_bld, face, ivec_bld->one);
1507 tmp = lp_build_add(ivec_bld, faceand1, c4);
1508 next_faces[0] = lp_build_select(ivec_bld, sel_f2345, tmpsel, tmp);
1509 next_faces[1] = lp_build_xor(ivec_bld, next_faces[0], ivec_bld->one);
1510
1511 tmp = lp_build_andnot(ivec_bld, face, c4);
1512 sel_f23 = lp_build_cmp(ivec_bld, PIPE_FUNC_GREATER, tmp, ivec_bld->one);
1513 tmp = lp_build_add(ivec_bld, face, c2);
1514 next_faces[3] = lp_build_select(ivec_bld, sel_f23, tmp, c3);
1515 next_faces[2] = lp_build_xor(ivec_bld, next_faces[3], ivec_bld->one);
1516
1517 /*
1518 * new xcoords (for face 012345):
1519 * x < 0.0 : max max t max-t max max
1520 * x >= 1.0 : 0 0 max-t t 0 0
1521 * y < 0.0 : max 0 max-s s s max-s
1522 * y >= 1.0 : max 0 s max-s s max-s
1523 *
1524 * ncx[1] = face & ~4 > 1 ? (face == 2 ? max-t : t) : 0
1525 * ncx[0] = max - ncx[1]
1526 * ncx[3] = face > 1 ? (face & 1 ? max-s : s) : (face & 1) ? 0 : max
1527 * ncx[2] = face & ~4 > 1 ? max - ncx[3] : ncx[3]
1528 */
1529 sel_f2 = lp_build_cmp(ivec_bld, PIPE_FUNC_EQUAL, face, c2);
1530 maxmy0 = lp_build_sub(ivec_bld, max_coord, y0);
1531 tmp = lp_build_select(ivec_bld, sel_f2, maxmy0, y0);
1532 next_xcoords[1][0] = lp_build_select(ivec_bld, sel_f23, tmp, ivec_bld->zero);
1533 next_xcoords[0][0] = lp_build_sub(ivec_bld, max_coord, next_xcoords[1][0]);
1534 maxmy1 = lp_build_sub(ivec_bld, max_coord, y1);
1535 tmp = lp_build_select(ivec_bld, sel_f2, maxmy1, y1);
1536 next_xcoords[1][1] = lp_build_select(ivec_bld, sel_f23, tmp, ivec_bld->zero);
1537 next_xcoords[0][1] = lp_build_sub(ivec_bld, max_coord, next_xcoords[1][1]);
1538
1539 sel_fand1 = lp_build_cmp(ivec_bld, PIPE_FUNC_EQUAL, faceand1, ivec_bld->one);
1540
1541 tmpsel = lp_build_select(ivec_bld, sel_fand1, ivec_bld->zero, max_coord);
1542 maxmx0 = lp_build_sub(ivec_bld, max_coord, x0);
1543 tmp = lp_build_select(ivec_bld, sel_fand1, maxmx0, x0);
1544 next_xcoords[3][0] = lp_build_select(ivec_bld, sel_f2345, tmp, tmpsel);
1545 tmp = lp_build_sub(ivec_bld, max_coord, next_xcoords[3][0]);
1546 next_xcoords[2][0] = lp_build_select(ivec_bld, sel_f23, tmp, next_xcoords[3][0]);
1547 maxmx1 = lp_build_sub(ivec_bld, max_coord, x1);
1548 tmp = lp_build_select(ivec_bld, sel_fand1, maxmx1, x1);
1549 next_xcoords[3][1] = lp_build_select(ivec_bld, sel_f2345, tmp, tmpsel);
1550 tmp = lp_build_sub(ivec_bld, max_coord, next_xcoords[3][1]);
1551 next_xcoords[2][1] = lp_build_select(ivec_bld, sel_f23, tmp, next_xcoords[3][1]);
1552
1553 /*
1554 * new ycoords (for face 012345):
1555 * x < 0.0 : t t 0 max t t
1556 * x >= 1.0 : t t 0 max t t
1557 * y < 0.0 : max-s s 0 max max 0
1558 * y >= 1.0 : s max-s 0 max 0 max
1559 *
1560 * ncy[0] = face & ~4 > 1 ? (face == 2 ? 0 : max) : t
1561 * ncy[1] = ncy[0]
1562 * ncy[3] = face > 1 ? (face & 1 ? max : 0) : (face & 1) ? max-s : max
1563 * ncx[2] = face & ~4 > 1 ? max - ncx[3] : ncx[3]
1564 */
1565 tmp = lp_build_select(ivec_bld, sel_f2, ivec_bld->zero, max_coord);
1566 next_ycoords[0][0] = lp_build_select(ivec_bld, sel_f23, tmp, y0);
1567 next_ycoords[1][0] = next_ycoords[0][0];
1568 next_ycoords[0][1] = lp_build_select(ivec_bld, sel_f23, tmp, y1);
1569 next_ycoords[1][1] = next_ycoords[0][1];
1570
1571 tmpsel = lp_build_select(ivec_bld, sel_fand1, maxmx0, x0);
1572 tmp = lp_build_select(ivec_bld, sel_fand1, max_coord, ivec_bld->zero);
1573 next_ycoords[3][0] = lp_build_select(ivec_bld, sel_f2345, tmp, tmpsel);
1574 tmp = lp_build_sub(ivec_bld, max_coord, next_ycoords[3][0]);
1575 next_ycoords[2][0] = lp_build_select(ivec_bld, sel_f23, next_ycoords[3][0], tmp);
1576 tmpsel = lp_build_select(ivec_bld, sel_fand1, maxmx1, x1);
1577 tmp = lp_build_select(ivec_bld, sel_fand1, max_coord, ivec_bld->zero);
1578 next_ycoords[3][1] = lp_build_select(ivec_bld, sel_f2345, tmp, tmpsel);
1579 tmp = lp_build_sub(ivec_bld, max_coord, next_ycoords[3][1]);
1580 next_ycoords[2][1] = lp_build_select(ivec_bld, sel_f23, next_ycoords[3][1], tmp);
1581 }
1582
1583
1584 /** Helper used by lp_build_cube_lookup() */
1585 static LLVMValueRef
1586 lp_build_cube_imapos(struct lp_build_context *coord_bld, LLVMValueRef coord)
1587 {
1588 /* ima = +0.5 / abs(coord); */
1589 LLVMValueRef posHalf = lp_build_const_vec(coord_bld->gallivm, coord_bld->type, 0.5);
1590 LLVMValueRef absCoord = lp_build_abs(coord_bld, coord);
1591 LLVMValueRef ima = lp_build_div(coord_bld, posHalf, absCoord);
1592 return ima;
1593 }
1594
1595
1596 /** Helper for doing 3-wise selection.
1597 * Returns sel1 ? val2 : (sel0 ? val0 : val1).
1598 */
1599 static LLVMValueRef
1600 lp_build_select3(struct lp_build_context *sel_bld,
1601 LLVMValueRef sel0,
1602 LLVMValueRef sel1,
1603 LLVMValueRef val0,
1604 LLVMValueRef val1,
1605 LLVMValueRef val2)
1606 {
1607 LLVMValueRef tmp;
1608 tmp = lp_build_select(sel_bld, sel0, val0, val1);
1609 return lp_build_select(sel_bld, sel1, val2, tmp);
1610 }
1611
1612
1613 /**
1614 * Generate code to do cube face selection and compute per-face texcoords.
1615 */
1616 void
1617 lp_build_cube_lookup(struct lp_build_sample_context *bld,
1618 LLVMValueRef *coords,
1619 const struct lp_derivatives *derivs_in, /* optional */
1620 LLVMValueRef *rho,
1621 struct lp_derivatives *derivs_out, /* optional */
1622 boolean need_derivs)
1623 {
1624 struct lp_build_context *coord_bld = &bld->coord_bld;
1625 LLVMBuilderRef builder = bld->gallivm->builder;
1626 struct gallivm_state *gallivm = bld->gallivm;
1627 LLVMValueRef si, ti, ri;
1628
1629 /*
1630 * Do per-pixel face selection. We cannot however (as we used to do)
1631 * simply calculate the derivs afterwards (which is very bogus for
1632 * explicit derivs btw) because the values would be "random" when
1633 * not all pixels lie on the same face. So what we do here is just
1634 * calculate the derivatives after scaling the coords by the absolute
1635 * value of the inverse major axis, and essentially do rho calculation
1636 * steps as if it were a 3d texture. This is perfect if all pixels hit
1637 * the same face, but not so great at edges, I believe the max error
1638 * should be sqrt(2) with no_rho_approx or 2 otherwise (essentially measuring
1639 * the 3d distance between 2 points on the cube instead of measuring up/down
1640 * the edge). Still this is possibly a win over just selecting the same face
1641 * for all pixels. Unfortunately, something like that doesn't work for
1642 * explicit derivatives.
1643 */
1644 struct lp_build_context *cint_bld = &bld->int_coord_bld;
1645 struct lp_type intctype = cint_bld->type;
1646 LLVMTypeRef coord_vec_type = coord_bld->vec_type;
1647 LLVMTypeRef cint_vec_type = cint_bld->vec_type;
1648 LLVMValueRef as, at, ar, face, face_s, face_t;
1649 LLVMValueRef as_ge_at, maxasat, ar_ge_as_at;
1650 LLVMValueRef snewx, tnewx, snewy, tnewy, snewz, tnewz;
1651 LLVMValueRef tnegi, rnegi;
1652 LLVMValueRef ma, mai, signma, signmabit, imahalfpos;
1653 LLVMValueRef posHalf = lp_build_const_vec(gallivm, coord_bld->type, 0.5);
1654 LLVMValueRef signmask = lp_build_const_int_vec(gallivm, intctype,
1655 1 << (intctype.width - 1));
1656 LLVMValueRef signshift = lp_build_const_int_vec(gallivm, intctype,
1657 intctype.width -1);
1658 LLVMValueRef facex = lp_build_const_int_vec(gallivm, intctype, PIPE_TEX_FACE_POS_X);
1659 LLVMValueRef facey = lp_build_const_int_vec(gallivm, intctype, PIPE_TEX_FACE_POS_Y);
1660 LLVMValueRef facez = lp_build_const_int_vec(gallivm, intctype, PIPE_TEX_FACE_POS_Z);
1661 LLVMValueRef s = coords[0];
1662 LLVMValueRef t = coords[1];
1663 LLVMValueRef r = coords[2];
1664
1665 assert(PIPE_TEX_FACE_NEG_X == PIPE_TEX_FACE_POS_X + 1);
1666 assert(PIPE_TEX_FACE_NEG_Y == PIPE_TEX_FACE_POS_Y + 1);
1667 assert(PIPE_TEX_FACE_NEG_Z == PIPE_TEX_FACE_POS_Z + 1);
1668
1669 /*
1670 * get absolute value (for x/y/z face selection) and sign bit
1671 * (for mirroring minor coords and pos/neg face selection)
1672 * of the original coords.
1673 */
1674 as = lp_build_abs(&bld->coord_bld, s);
1675 at = lp_build_abs(&bld->coord_bld, t);
1676 ar = lp_build_abs(&bld->coord_bld, r);
1677
1678 /*
1679 * major face determination: select x if x > y else select y
1680 * select z if z >= max(x,y) else select previous result
1681 * if some axis are the same we chose z over y, y over x - the
1682 * dx10 spec seems to ask for it while OpenGL doesn't care (if we
1683 * wouldn't care could save a select or two if using different
1684 * compares and doing at_g_as_ar last since tnewx and tnewz are the
1685 * same).
1686 */
1687 as_ge_at = lp_build_cmp(coord_bld, PIPE_FUNC_GREATER, as, at);
1688 maxasat = lp_build_max(coord_bld, as, at);
1689 ar_ge_as_at = lp_build_cmp(coord_bld, PIPE_FUNC_GEQUAL, ar, maxasat);
1690
1691 if (need_derivs && (derivs_in ||
1692 ((gallivm_debug & GALLIVM_DEBUG_NO_QUAD_LOD) &&
1693 (gallivm_debug & GALLIVM_DEBUG_NO_RHO_APPROX)))) {
1694 /*
1695 * XXX: This is really really complex.
1696 * It is a bit overkill to use this for implicit derivatives as well,
1697 * no way this is worth the cost in practice, but seems to be the
1698 * only way for getting accurate and per-pixel lod values.
1699 */
1700 LLVMValueRef ima, imahalf, tmp, ddx[3], ddy[3];
1701 LLVMValueRef madx, mady, madxdivma, madydivma;
1702 LLVMValueRef sdxi, tdxi, rdxi, sdyi, tdyi, rdyi;
1703 LLVMValueRef tdxnegi, rdxnegi, tdynegi, rdynegi;
1704 LLVMValueRef sdxnewx, sdxnewy, sdxnewz, tdxnewx, tdxnewy, tdxnewz;
1705 LLVMValueRef sdynewx, sdynewy, sdynewz, tdynewx, tdynewy, tdynewz;
1706 LLVMValueRef face_sdx, face_tdx, face_sdy, face_tdy;
1707 /*
1708 * s = 1/2 * ( sc / ma + 1)
1709 * t = 1/2 * ( tc / ma + 1)
1710 *
1711 * s' = 1/2 * (sc' * ma - sc * ma') / ma^2
1712 * t' = 1/2 * (tc' * ma - tc * ma') / ma^2
1713 *
1714 * dx.s = 0.5 * (dx.sc - sc * dx.ma / ma) / ma
1715 * dx.t = 0.5 * (dx.tc - tc * dx.ma / ma) / ma
1716 * dy.s = 0.5 * (dy.sc - sc * dy.ma / ma) / ma
1717 * dy.t = 0.5 * (dy.tc - tc * dy.ma / ma) / ma
1718 */
1719
1720 /* select ma, calculate ima */
1721 ma = lp_build_select3(coord_bld, as_ge_at, ar_ge_as_at, s, t, r);
1722 mai = LLVMBuildBitCast(builder, ma, cint_vec_type, "");
1723 signmabit = LLVMBuildAnd(builder, mai, signmask, "");
1724 ima = lp_build_div(coord_bld, coord_bld->one, ma);
1725 imahalf = lp_build_mul(coord_bld, posHalf, ima);
1726 imahalfpos = lp_build_abs(coord_bld, imahalf);
1727
1728 if (!derivs_in) {
1729 ddx[0] = lp_build_ddx(coord_bld, s);
1730 ddx[1] = lp_build_ddx(coord_bld, t);
1731 ddx[2] = lp_build_ddx(coord_bld, r);
1732 ddy[0] = lp_build_ddy(coord_bld, s);
1733 ddy[1] = lp_build_ddy(coord_bld, t);
1734 ddy[2] = lp_build_ddy(coord_bld, r);
1735 }
1736 else {
1737 ddx[0] = derivs_in->ddx[0];
1738 ddx[1] = derivs_in->ddx[1];
1739 ddx[2] = derivs_in->ddx[2];
1740 ddy[0] = derivs_in->ddy[0];
1741 ddy[1] = derivs_in->ddy[1];
1742 ddy[2] = derivs_in->ddy[2];
1743 }
1744
1745 /* select major derivatives */
1746 madx = lp_build_select3(coord_bld, as_ge_at, ar_ge_as_at, ddx[0], ddx[1], ddx[2]);
1747 mady = lp_build_select3(coord_bld, as_ge_at, ar_ge_as_at, ddy[0], ddy[1], ddy[2]);
1748
1749 si = LLVMBuildBitCast(builder, s, cint_vec_type, "");
1750 ti = LLVMBuildBitCast(builder, t, cint_vec_type, "");
1751 ri = LLVMBuildBitCast(builder, r, cint_vec_type, "");
1752
1753 sdxi = LLVMBuildBitCast(builder, ddx[0], cint_vec_type, "");
1754 tdxi = LLVMBuildBitCast(builder, ddx[1], cint_vec_type, "");
1755 rdxi = LLVMBuildBitCast(builder, ddx[2], cint_vec_type, "");
1756
1757 sdyi = LLVMBuildBitCast(builder, ddy[0], cint_vec_type, "");
1758 tdyi = LLVMBuildBitCast(builder, ddy[1], cint_vec_type, "");
1759 rdyi = LLVMBuildBitCast(builder, ddy[2], cint_vec_type, "");
1760
1761 /*
1762 * compute all possible new s/t coords, which does the mirroring,
1763 * and do the same for derivs minor axes.
1764 * snewx = signma * -r;
1765 * tnewx = -t;
1766 * snewy = s;
1767 * tnewy = signma * r;
1768 * snewz = signma * s;
1769 * tnewz = -t;
1770 */
1771 tnegi = LLVMBuildXor(builder, ti, signmask, "");
1772 rnegi = LLVMBuildXor(builder, ri, signmask, "");
1773 tdxnegi = LLVMBuildXor(builder, tdxi, signmask, "");
1774 rdxnegi = LLVMBuildXor(builder, rdxi, signmask, "");
1775 tdynegi = LLVMBuildXor(builder, tdyi, signmask, "");
1776 rdynegi = LLVMBuildXor(builder, rdyi, signmask, "");
1777
1778 snewx = LLVMBuildXor(builder, signmabit, rnegi, "");
1779 tnewx = tnegi;
1780 sdxnewx = LLVMBuildXor(builder, signmabit, rdxnegi, "");
1781 tdxnewx = tdxnegi;
1782 sdynewx = LLVMBuildXor(builder, signmabit, rdynegi, "");
1783 tdynewx = tdynegi;
1784
1785 snewy = si;
1786 tnewy = LLVMBuildXor(builder, signmabit, ri, "");
1787 sdxnewy = sdxi;
1788 tdxnewy = LLVMBuildXor(builder, signmabit, rdxi, "");
1789 sdynewy = sdyi;
1790 tdynewy = LLVMBuildXor(builder, signmabit, rdyi, "");
1791
1792 snewz = LLVMBuildXor(builder, signmabit, si, "");
1793 tnewz = tnegi;
1794 sdxnewz = LLVMBuildXor(builder, signmabit, sdxi, "");
1795 tdxnewz = tdxnegi;
1796 sdynewz = LLVMBuildXor(builder, signmabit, sdyi, "");
1797 tdynewz = tdynegi;
1798
1799 /* select the mirrored values */
1800 face = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, facex, facey, facez);
1801 face_s = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, snewx, snewy, snewz);
1802 face_t = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, tnewx, tnewy, tnewz);
1803 face_sdx = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, sdxnewx, sdxnewy, sdxnewz);
1804 face_tdx = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, tdxnewx, tdxnewy, tdxnewz);
1805 face_sdy = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, sdynewx, sdynewy, sdynewz);
1806 face_tdy = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, tdynewx, tdynewy, tdynewz);
1807
1808 face_s = LLVMBuildBitCast(builder, face_s, coord_vec_type, "");
1809 face_t = LLVMBuildBitCast(builder, face_t, coord_vec_type, "");
1810 face_sdx = LLVMBuildBitCast(builder, face_sdx, coord_vec_type, "");
1811 face_tdx = LLVMBuildBitCast(builder, face_tdx, coord_vec_type, "");
1812 face_sdy = LLVMBuildBitCast(builder, face_sdy, coord_vec_type, "");
1813 face_tdy = LLVMBuildBitCast(builder, face_tdy, coord_vec_type, "");
1814
1815 /* deriv math, dx.s = 0.5 * (dx.sc - sc * dx.ma / ma) / ma */
1816 madxdivma = lp_build_mul(coord_bld, madx, ima);
1817 tmp = lp_build_mul(coord_bld, madxdivma, face_s);
1818 tmp = lp_build_sub(coord_bld, face_sdx, tmp);
1819 derivs_out->ddx[0] = lp_build_mul(coord_bld, tmp, imahalf);
1820
1821 /* dx.t = 0.5 * (dx.tc - tc * dx.ma / ma) / ma */
1822 tmp = lp_build_mul(coord_bld, madxdivma, face_t);
1823 tmp = lp_build_sub(coord_bld, face_tdx, tmp);
1824 derivs_out->ddx[1] = lp_build_mul(coord_bld, tmp, imahalf);
1825
1826 /* dy.s = 0.5 * (dy.sc - sc * dy.ma / ma) / ma */
1827 madydivma = lp_build_mul(coord_bld, mady, ima);
1828 tmp = lp_build_mul(coord_bld, madydivma, face_s);
1829 tmp = lp_build_sub(coord_bld, face_sdy, tmp);
1830 derivs_out->ddy[0] = lp_build_mul(coord_bld, tmp, imahalf);
1831
1832 /* dy.t = 0.5 * (dy.tc - tc * dy.ma / ma) / ma */
1833 tmp = lp_build_mul(coord_bld, madydivma, face_t);
1834 tmp = lp_build_sub(coord_bld, face_tdy, tmp);
1835 derivs_out->ddy[1] = lp_build_mul(coord_bld, tmp, imahalf);
1836
1837 signma = LLVMBuildLShr(builder, mai, signshift, "");
1838 coords[2] = LLVMBuildOr(builder, face, signma, "face");
1839
1840 /* project coords */
1841 face_s = lp_build_mul(coord_bld, face_s, imahalfpos);
1842 face_t = lp_build_mul(coord_bld, face_t, imahalfpos);
1843
1844 coords[0] = lp_build_add(coord_bld, face_s, posHalf);
1845 coords[1] = lp_build_add(coord_bld, face_t, posHalf);
1846
1847 return;
1848 }
1849
1850 else if (need_derivs) {
1851 LLVMValueRef ddx_ddy[2], tmp[3], rho_vec;
1852 static const unsigned char swizzle0[] = { /* no-op swizzle */
1853 0, LP_BLD_SWIZZLE_DONTCARE,
1854 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
1855 };
1856 static const unsigned char swizzle1[] = {
1857 1, LP_BLD_SWIZZLE_DONTCARE,
1858 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
1859 };
1860 static const unsigned char swizzle01[] = { /* no-op swizzle */
1861 0, 1,
1862 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
1863 };
1864 static const unsigned char swizzle23[] = {
1865 2, 3,
1866 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
1867 };
1868 static const unsigned char swizzle02[] = {
1869 0, 2,
1870 LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
1871 };
1872
1873 /*
1874 * scale the s/t/r coords pre-select/mirror so we can calculate
1875 * "reasonable" derivs.
1876 */
1877 ma = lp_build_select3(coord_bld, as_ge_at, ar_ge_as_at, s, t, r);
1878 imahalfpos = lp_build_cube_imapos(coord_bld, ma);
1879 s = lp_build_mul(coord_bld, s, imahalfpos);
1880 t = lp_build_mul(coord_bld, t, imahalfpos);
1881 r = lp_build_mul(coord_bld, r, imahalfpos);
1882
1883 /*
1884 * This isn't quite the same as the "ordinary" (3d deriv) path since we
1885 * know the texture is square which simplifies things (we can omit the
1886 * size mul which happens very early completely here and do it at the
1887 * very end).
1888 * Also always do calculations according to GALLIVM_DEBUG_NO_RHO_APPROX
1889 * since the error can get quite big otherwise at edges.
1890 * (With no_rho_approx max error is sqrt(2) at edges, same as it is
1891 * without no_rho_approx for 2d textures, otherwise it would be factor 2.)
1892 */
1893 ddx_ddy[0] = lp_build_packed_ddx_ddy_twocoord(coord_bld, s, t);
1894 ddx_ddy[1] = lp_build_packed_ddx_ddy_onecoord(coord_bld, r);
1895
1896 ddx_ddy[0] = lp_build_mul(coord_bld, ddx_ddy[0], ddx_ddy[0]);
1897 ddx_ddy[1] = lp_build_mul(coord_bld, ddx_ddy[1], ddx_ddy[1]);
1898
1899 tmp[0] = lp_build_swizzle_aos(coord_bld, ddx_ddy[0], swizzle01);
1900 tmp[1] = lp_build_swizzle_aos(coord_bld, ddx_ddy[0], swizzle23);
1901 tmp[2] = lp_build_swizzle_aos(coord_bld, ddx_ddy[1], swizzle02);
1902
1903 rho_vec = lp_build_add(coord_bld, tmp[0], tmp[1]);
1904 rho_vec = lp_build_add(coord_bld, rho_vec, tmp[2]);
1905
1906 tmp[0] = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle0);
1907 tmp[1] = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle1);
1908 *rho = lp_build_max(coord_bld, tmp[0], tmp[1]);
1909 }
1910
1911 if (!need_derivs) {
1912 ma = lp_build_select3(coord_bld, as_ge_at, ar_ge_as_at, s, t, r);
1913 }
1914 mai = LLVMBuildBitCast(builder, ma, cint_vec_type, "");
1915 signmabit = LLVMBuildAnd(builder, mai, signmask, "");
1916
1917 si = LLVMBuildBitCast(builder, s, cint_vec_type, "");
1918 ti = LLVMBuildBitCast(builder, t, cint_vec_type, "");
1919 ri = LLVMBuildBitCast(builder, r, cint_vec_type, "");
1920
1921 /*
1922 * compute all possible new s/t coords, which does the mirroring
1923 * snewx = signma * -r;
1924 * tnewx = -t;
1925 * snewy = s;
1926 * tnewy = signma * r;
1927 * snewz = signma * s;
1928 * tnewz = -t;
1929 */
1930 tnegi = LLVMBuildXor(builder, ti, signmask, "");
1931 rnegi = LLVMBuildXor(builder, ri, signmask, "");
1932
1933 snewx = LLVMBuildXor(builder, signmabit, rnegi, "");
1934 tnewx = tnegi;
1935
1936 snewy = si;
1937 tnewy = LLVMBuildXor(builder, signmabit, ri, "");
1938
1939 snewz = LLVMBuildXor(builder, signmabit, si, "");
1940 tnewz = tnegi;
1941
1942 /* select the mirrored values */
1943 face_s = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, snewx, snewy, snewz);
1944 face_t = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, tnewx, tnewy, tnewz);
1945 face = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, facex, facey, facez);
1946
1947 face_s = LLVMBuildBitCast(builder, face_s, coord_vec_type, "");
1948 face_t = LLVMBuildBitCast(builder, face_t, coord_vec_type, "");
1949
1950 /* add +1 for neg face */
1951 /* XXX with AVX probably want to use another select here -
1952 * as long as we ensure vblendvps gets used we can actually
1953 * skip the comparison and just use sign as a "mask" directly.
1954 */
1955 signma = LLVMBuildLShr(builder, mai, signshift, "");
1956 coords[2] = LLVMBuildOr(builder, face, signma, "face");
1957
1958 /* project coords */
1959 if (!need_derivs) {
1960 imahalfpos = lp_build_cube_imapos(coord_bld, ma);
1961 face_s = lp_build_mul(coord_bld, face_s, imahalfpos);
1962 face_t = lp_build_mul(coord_bld, face_t, imahalfpos);
1963 }
1964
1965 coords[0] = lp_build_add(coord_bld, face_s, posHalf);
1966 coords[1] = lp_build_add(coord_bld, face_t, posHalf);
1967 }
1968
1969
1970 /**
1971 * Compute the partial offset of a pixel block along an arbitrary axis.
1972 *
1973 * @param coord coordinate in pixels
1974 * @param stride number of bytes between rows of successive pixel blocks
1975 * @param block_length number of pixels in a pixels block along the coordinate
1976 * axis
1977 * @param out_offset resulting relative offset of the pixel block in bytes
1978 * @param out_subcoord resulting sub-block pixel coordinate
1979 */
1980 void
1981 lp_build_sample_partial_offset(struct lp_build_context *bld,
1982 unsigned block_length,
1983 LLVMValueRef coord,
1984 LLVMValueRef stride,
1985 LLVMValueRef *out_offset,
1986 LLVMValueRef *out_subcoord)
1987 {
1988 LLVMBuilderRef builder = bld->gallivm->builder;
1989 LLVMValueRef offset;
1990 LLVMValueRef subcoord;
1991
1992 if (block_length == 1) {
1993 subcoord = bld->zero;
1994 }
1995 else {
1996 /*
1997 * Pixel blocks have power of two dimensions. LLVM should convert the
1998 * rem/div to bit arithmetic.
1999 * TODO: Verify this.
2000 * It does indeed BUT it does transform it to scalar (and back) when doing so
2001 * (using roughly extract, shift/and, mov, unpack) (llvm 2.7).
2002 * The generated code looks seriously unfunny and is quite expensive.
2003 */
2004 #if 0
2005 LLVMValueRef block_width = lp_build_const_int_vec(bld->type, block_length);
2006 subcoord = LLVMBuildURem(builder, coord, block_width, "");
2007 coord = LLVMBuildUDiv(builder, coord, block_width, "");
2008 #else
2009 unsigned logbase2 = util_logbase2(block_length);
2010 LLVMValueRef block_shift = lp_build_const_int_vec(bld->gallivm, bld->type, logbase2);
2011 LLVMValueRef block_mask = lp_build_const_int_vec(bld->gallivm, bld->type, block_length - 1);
2012 subcoord = LLVMBuildAnd(builder, coord, block_mask, "");
2013 coord = LLVMBuildLShr(builder, coord, block_shift, "");
2014 #endif
2015 }
2016
2017 offset = lp_build_mul(bld, coord, stride);
2018
2019 assert(out_offset);
2020 assert(out_subcoord);
2021
2022 *out_offset = offset;
2023 *out_subcoord = subcoord;
2024 }
2025
2026
2027 /**
2028 * Compute the offset of a pixel block.
2029 *
2030 * x, y, z, y_stride, z_stride are vectors, and they refer to pixels.
2031 *
2032 * Returns the relative offset and i,j sub-block coordinates
2033 */
2034 void
2035 lp_build_sample_offset(struct lp_build_context *bld,
2036 const struct util_format_description *format_desc,
2037 LLVMValueRef x,
2038 LLVMValueRef y,
2039 LLVMValueRef z,
2040 LLVMValueRef y_stride,
2041 LLVMValueRef z_stride,
2042 LLVMValueRef *out_offset,
2043 LLVMValueRef *out_i,
2044 LLVMValueRef *out_j)
2045 {
2046 LLVMValueRef x_stride;
2047 LLVMValueRef offset;
2048
2049 x_stride = lp_build_const_vec(bld->gallivm, bld->type,
2050 format_desc->block.bits/8);
2051
2052 lp_build_sample_partial_offset(bld,
2053 format_desc->block.width,
2054 x, x_stride,
2055 &offset, out_i);
2056
2057 if (y && y_stride) {
2058 LLVMValueRef y_offset;
2059 lp_build_sample_partial_offset(bld,
2060 format_desc->block.height,
2061 y, y_stride,
2062 &y_offset, out_j);
2063 offset = lp_build_add(bld, offset, y_offset);
2064 }
2065 else {
2066 *out_j = bld->zero;
2067 }
2068
2069 if (z && z_stride) {
2070 LLVMValueRef z_offset;
2071 LLVMValueRef k;
2072 lp_build_sample_partial_offset(bld,
2073 1, /* pixel blocks are always 2D */
2074 z, z_stride,
2075 &z_offset, &k);
2076 offset = lp_build_add(bld, offset, z_offset);
2077 }
2078
2079 *out_offset = offset;
2080 }