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