1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Texture sampling -- common code.
32 * @author Jose Fonseca <jfonseca@vmware.com>
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"
55 * Bri-linear factor. Should be greater than one.
57 #define BRILINEAR_FACTOR 2
60 * Does the given texture wrap mode allow sampling the texture border color?
61 * XXX maybe move this into gallium util code.
64 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
65 unsigned min_img_filter
,
66 unsigned mag_img_filter
)
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
:
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
) {
82 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
83 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
86 assert(0 && "unexpected wrap mode");
93 * Initialize lp_sampler_static_texture_state object with the gallium
94 * texture/sampler_view state (this contains the parts which are
98 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
99 const struct pipe_sampler_view
*view
)
101 const struct pipe_resource
*texture
;
103 memset(state
, 0, sizeof *state
);
105 if (!view
|| !view
->texture
)
108 texture
= view
->texture
;
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
;
116 state
->target
= view
->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
;
123 * the layer / element / level parameters are all either dynamic
124 * state or handled transparently wrt execution.
130 * Initialize lp_sampler_static_sampler_state object with the gallium sampler
131 * state (this contains the parts which are considered static).
134 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
135 const struct pipe_sampler_state
*sampler
)
137 memset(state
, 0, sizeof *state
);
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
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.
150 * XXX: Actually there's much more than can be done here, especially
151 * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
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
;
161 if (sampler
->max_lod
> 0.0f
) {
162 state
->min_mip_filter
= sampler
->min_mip_filter
;
164 state
->min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
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;
173 /* If min_lod == max_lod we can greatly simplify mipmap selection.
174 * This is a case that occurs during automatic mipmap generation.
176 if (sampler
->min_lod
== sampler
->max_lod
) {
177 state
->min_max_lod_equal
= 1;
179 if (sampler
->min_lod
> 0.0f
) {
180 state
->apply_min_lod
= 1;
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...
187 if (sampler
->max_lod
< (PIPE_MAX_TEXTURE_LEVELS
- 1)) {
188 state
->apply_max_lod
= 1;
193 state
->compare_mode
= sampler
->compare_mode
;
194 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
195 state
->compare_func
= sampler
->compare_func
;
198 state
->normalized_coords
= sampler
->normalized_coords
;
203 * Generate code to compute coordinate gradient (rho).
204 * \param derivs partial derivatives of (s, t, r, q) with respect to X and Y
206 * The resulting rho has bld->levelf format (per quad or per element).
209 lp_build_rho(struct lp_build_sample_context
*bld
,
210 unsigned texture_unit
,
214 LLVMValueRef cube_rho
,
215 const struct lp_derivatives
*derivs
)
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
;
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);
239 LLVMValueRef i32undef
= LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
240 LLVMValueRef rho_xvec
, rho_yvec
;
242 /* Note that all simplified calculations will only work for isotropic filtering */
245 * rho calcs are always per quad except for explicit derivs (excluding
246 * the messy cube maps for now) when requested.
249 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
, bld
->gallivm
,
250 bld
->context_ptr
, 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
);
256 LLVMValueRef cubesize
;
257 LLVMValueRef index0
= lp_build_const_int32(gallivm
, 0);
260 * Cube map code did already everything except size mul and per-quad extraction.
261 * Luckily cube maps are always quadratic!
264 rho
= lp_build_pack_aos_scalars(bld
->gallivm
, coord_bld
->type
,
265 rho_bld
->type
, cube_rho
, 0);
268 rho
= lp_build_swizzle_scalar_aos(coord_bld
, cube_rho
, 0, 4);
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
);
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
);
283 floatdim
= lp_build_extract_broadcast(gallivm
, bld
->float_size_in_type
,
284 coord_bld
->type
, float_size
, indexi
);
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.
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
]);
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
]);
305 rho_xvec
= lp_build_add(coord_bld
, ddx
[0], ddx
[1]);
306 rho_yvec
= lp_build_add(coord_bld
, ddy
[0], ddy
[1]);
308 rho_xvec
= lp_build_add(coord_bld
, rho_xvec
, ddx
[2]);
309 rho_yvec
= lp_build_add(coord_bld
, rho_yvec
, ddy
[2]);
311 rho
= lp_build_max(coord_bld
, rho_xvec
, rho_yvec
);
312 /* skipping sqrt hence returning rho squared */
317 rho
= lp_build_max(coord_bld
, rho
, ddmax
[1]);
319 rho
= lp_build_max(coord_bld
, rho
, ddmax
[2]);
325 * rho_vec contains per-pixel rho, convert to scalar per quad.
327 rho
= lp_build_pack_aos_scalars(bld
->gallivm
, coord_bld
->type
,
328 rho_bld
->type
, rho
, 0);
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.
337 static const unsigned char swizzle0
[] = { /* no-op swizzle */
338 0, LP_BLD_SWIZZLE_DONTCARE
,
339 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
341 static const unsigned char swizzle1
[] = {
342 1, LP_BLD_SWIZZLE_DONTCARE
,
343 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
345 static const unsigned char swizzle2
[] = {
346 2, LP_BLD_SWIZZLE_DONTCARE
,
347 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
351 ddx_ddy
[0] = lp_build_packed_ddx_ddy_onecoord(coord_bld
, s
);
353 else if (dims
>= 2) {
354 ddx_ddy
[0] = lp_build_packed_ddx_ddy_twocoord(coord_bld
, s
, t
);
356 ddx_ddy
[1] = lp_build_packed_ddx_ddy_onecoord(coord_bld
, r
);
361 static const unsigned char swizzle01
[] = { /* no-op swizzle */
363 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
365 static const unsigned char swizzle23
[] = {
367 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
369 LLVMValueRef ddx_ddys
, ddx_ddyt
, floatdim
, shuffles
[LP_MAX_VECTOR_LENGTH
/ 4];
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
;
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
);
384 static const unsigned char swizzle02
[] = {
386 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
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]);
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
);
401 rho
= lp_build_pack_aos_scalars(bld
->gallivm
, coord_bld
->type
,
402 rho_bld
->type
, rho
, 0);
405 rho
= lp_build_swizzle_scalar_aos(coord_bld
, rho
, 0, 4);
407 /* skipping sqrt hence returning rho squared */
410 ddx_ddy
[0] = lp_build_abs(coord_bld
, ddx_ddy
[0]);
412 ddx_ddy
[1] = lp_build_abs(coord_bld
, ddx_ddy
[1]);
415 ddx_ddy
[1] = NULL
; /* silence compiler warning */
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
);
422 else if (dims
== 2) {
423 static const unsigned char swizzle02
[] = {
425 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
427 static const unsigned char swizzle13
[] = {
429 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
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
);
435 LLVMValueRef shuffles1
[LP_MAX_VECTOR_LENGTH
];
436 LLVMValueRef shuffles2
[LP_MAX_VECTOR_LENGTH
];
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
;
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
), "");
454 rho_vec
= lp_build_max(coord_bld
, rho_xvec
, rho_yvec
);
456 if (bld
->coord_type
.length
> 4) {
457 /* expand size to each quad */
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
++) {
464 float_size
= lp_build_concat(bld
->gallivm
, src
, float_size_bld
->type
, num_quads
);
467 float_size
= lp_build_broadcast_scalar(coord_bld
, float_size
);
469 rho_vec
= lp_build_mul(coord_bld
, rho_vec
, float_size
);
476 LLVMValueRef rho_s
, rho_t
, rho_r
;
478 rho_s
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle0
);
479 rho_t
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle1
);
481 rho
= lp_build_max(coord_bld
, rho_s
, rho_t
);
484 rho_r
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle2
);
485 rho
= lp_build_max(coord_bld
, rho
, rho_r
);
490 rho
= lp_build_pack_aos_scalars(bld
->gallivm
, coord_bld
->type
,
491 rho_bld
->type
, rho
, 0);
494 rho
= lp_build_swizzle_scalar_aos(coord_bld
, rho
, 0, 4);
499 rho_vec
= LLVMBuildExtractElement(builder
, rho_vec
, index0
, "");
501 rho_vec
= lp_build_mul(float_size_bld
, rho_vec
, float_size
);
508 LLVMValueRef rho_s
, rho_t
, rho_r
;
510 rho_s
= LLVMBuildExtractElement(builder
, rho_vec
, index0
, "");
511 rho_t
= LLVMBuildExtractElement(builder
, rho_vec
, index1
, "");
513 rho
= lp_build_max(float_bld
, rho_s
, rho_t
);
516 rho_r
= LLVMBuildExtractElement(builder
, rho_vec
, index2
, "");
517 rho
= lp_build_max(float_bld
, rho
, rho_r
);
522 rho
= lp_build_broadcast_scalar(rho_bld
, rho
);
533 * Bri-linear lod computation
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.
555 * This is a technique also commonly used in hardware:
556 * - http://ixbtlabs.com/articles2/gffx/nv40-rx800-3.html
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.
561 * TODO: This could be done in fixed point, where applicable.
564 lp_build_brilinear_lod(struct lp_build_context
*bld
,
567 LLVMValueRef
*out_lod_ipart
,
568 LLVMValueRef
*out_lod_fpart
)
570 LLVMValueRef lod_fpart
;
571 double pre_offset
= (factor
- 0.5)/factor
- 0.5;
572 double post_offset
= 1 - factor
;
575 lp_build_printf(bld
->gallivm
, "lod = %f\n", lod
);
578 lod
= lp_build_add(bld
, lod
,
579 lp_build_const_vec(bld
->gallivm
, bld
->type
, pre_offset
));
581 lp_build_ifloor_fract(bld
, lod
, out_lod_ipart
, &lod_fpart
);
583 lod_fpart
= lp_build_mul(bld
, lod_fpart
,
584 lp_build_const_vec(bld
->gallivm
, bld
->type
, factor
));
586 lod_fpart
= lp_build_add(bld
, lod_fpart
,
587 lp_build_const_vec(bld
->gallivm
, bld
->type
, post_offset
));
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
595 *out_lod_fpart
= lod_fpart
;
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
);
605 * Combined log2 and brilinear lod computation.
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.
612 lp_build_brilinear_rho(struct lp_build_context
*bld
,
615 LLVMValueRef
*out_lod_ipart
,
616 LLVMValueRef
*out_lod_fpart
)
618 LLVMValueRef lod_ipart
;
619 LLVMValueRef lod_fpart
;
621 const double pre_factor
= (2*factor
- 0.5)/(M_SQRT2
*factor
);
622 const double post_offset
= 1 - 2*factor
;
624 assert(bld
->type
.floating
);
626 assert(lp_check_value(bld
->type
, rho
));
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.
633 rho
= lp_build_mul(bld
, rho
,
634 lp_build_const_vec(bld
->gallivm
, bld
->type
, pre_factor
));
636 /* ipart = ifloor(log2(rho)) */
637 lod_ipart
= lp_build_extract_exponent(bld
, rho
, 0);
639 /* fpart = rho / 2**ipart */
640 lod_fpart
= lp_build_extract_mantissa(bld
, rho
);
642 lod_fpart
= lp_build_mul(bld
, lod_fpart
,
643 lp_build_const_vec(bld
->gallivm
, bld
->type
, factor
));
645 lod_fpart
= lp_build_add(bld
, lod_fpart
,
646 lp_build_const_vec(bld
->gallivm
, bld
->type
, post_offset
));
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
654 *out_lod_ipart
= lod_ipart
;
655 *out_lod_fpart
= lod_fpart
;
660 * Fast implementation of iround(log2(sqrt(x))), based on
661 * log2(x^n) == n*log2(x).
663 * Gives accurate results all the time.
664 * (Could be trivially extended to handle other power-of-two roots.)
667 lp_build_ilog2_sqrt(struct lp_build_context
*bld
,
670 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
672 struct lp_type i_type
= lp_int_type(bld
->type
);
673 LLVMValueRef one
= lp_build_const_int_vec(bld
->gallivm
, i_type
, 1);
675 assert(bld
->type
.floating
);
677 assert(lp_check_value(bld
->type
, x
));
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
, "");
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)
697 * The resulting lod can be scalar per quad or be per element.
700 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
701 unsigned texture_unit
,
702 unsigned sampler_unit
,
706 LLVMValueRef cube_rho
,
707 const struct lp_derivatives
*derivs
,
708 LLVMValueRef lod_bias
, /* optional */
709 LLVMValueRef explicit_lod
, /* optional */
711 LLVMValueRef
*out_lod_ipart
,
712 LLVMValueRef
*out_lod_fpart
,
713 LLVMValueRef
*out_lod_positive
)
716 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
717 struct lp_sampler_dynamic_state
*dynamic_state
= bld
->dynamic_state
;
718 struct lp_build_context
*lodf_bld
= &bld
->lodf_bld
;
721 *out_lod_ipart
= bld
->lodi_bld
.zero
;
722 *out_lod_positive
= bld
->lodi_bld
.zero
;
723 *out_lod_fpart
= lodf_bld
->zero
;
726 * For determining min/mag, we follow GL 4.1 spec, 3.9.12 Texture Magnification:
727 * "Implementations may either unconditionally assume c = 0 for the minification
728 * vs. magnification switch-over point, or may choose to make c depend on the
729 * combination of minification and magnification modes as follows: if the
730 * magnification filter is given by LINEAR and the minification filter is given
731 * by NEAREST_MIPMAP_NEAREST or NEAREST_MIPMAP_LINEAR, then c = 0.5. This is
732 * done to ensure that a minified texture does not appear "sharper" than a
733 * magnified texture. Otherwise c = 0."
734 * And 3.9.11 Texture Minification:
735 * "If lod is less than or equal to the constant c (see section 3.9.12) the
736 * texture is said to be magnified; if it is greater, the texture is minified."
737 * So, using 0 as switchover point always, and using magnification for lod == 0.
738 * Note that the always c = 0 behavior is new (first appearing in GL 3.1 spec),
739 * old GL versions required 0.5 for the modes listed above.
740 * I have no clue about the (undocumented) wishes of d3d9/d3d10 here!
743 if (bld
->static_sampler_state
->min_max_lod_equal
) {
744 /* User is forcing sampling from a particular mipmap level.
745 * This is hit during mipmap generation.
747 LLVMValueRef min_lod
=
748 dynamic_state
->min_lod(dynamic_state
, bld
->gallivm
,
749 bld
->context_ptr
, sampler_unit
);
751 lod
= lp_build_broadcast_scalar(lodf_bld
, min_lod
);
755 if (bld
->num_lods
!= bld
->coord_type
.length
)
756 lod
= lp_build_pack_aos_scalars(bld
->gallivm
, bld
->coord_bld
.type
,
757 lodf_bld
->type
, explicit_lod
, 0);
763 boolean rho_squared
= ((gallivm_debug
& GALLIVM_DEBUG_NO_RHO_APPROX
) &&
764 (bld
->dims
> 1)) || cube_rho
;
766 rho
= lp_build_rho(bld
, texture_unit
, s
, t
, r
, cube_rho
, derivs
);
769 * Compute lod = log2(rho)
773 !bld
->static_sampler_state
->lod_bias_non_zero
&&
774 !bld
->static_sampler_state
->apply_max_lod
&&
775 !bld
->static_sampler_state
->apply_min_lod
) {
777 * Special case when there are no post-log2 adjustments, which
778 * saves instructions but keeping the integer and fractional lod
779 * computations separate from the start.
782 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
||
783 mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
785 * Don't actually need both values all the time, lod_ipart is
786 * needed for nearest mipfilter, lod_positive if min != mag.
789 *out_lod_ipart
= lp_build_ilog2_sqrt(lodf_bld
, rho
);
792 *out_lod_ipart
= lp_build_ilog2(lodf_bld
, rho
);
794 *out_lod_positive
= lp_build_cmp(lodf_bld
, PIPE_FUNC_GREATER
,
798 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
&&
799 !(gallivm_debug
& GALLIVM_DEBUG_NO_BRILINEAR
) &&
802 * This can't work if rho is squared. Not sure if it could be
803 * fixed while keeping it worthwile, could also do sqrt here
804 * but brilinear and no_rho_opt seems like a combination not
805 * making much sense anyway so just use ordinary path below.
807 lp_build_brilinear_rho(lodf_bld
, rho
, BRILINEAR_FACTOR
,
808 out_lod_ipart
, out_lod_fpart
);
809 *out_lod_positive
= lp_build_cmp(lodf_bld
, PIPE_FUNC_GREATER
,
816 lod
= lp_build_log2(lodf_bld
, rho
);
819 lod
= lp_build_fast_log2(lodf_bld
, rho
);
822 /* log2(x^2) == 0.5*log2(x) */
823 lod
= lp_build_mul(lodf_bld
, lod
,
824 lp_build_const_vec(bld
->gallivm
, lodf_bld
->type
, 0.5F
));
827 /* add shader lod bias */
829 if (bld
->num_lods
!= bld
->coord_type
.length
)
830 lod_bias
= lp_build_pack_aos_scalars(bld
->gallivm
, bld
->coord_bld
.type
,
831 lodf_bld
->type
, lod_bias
, 0);
832 lod
= LLVMBuildFAdd(builder
, lod
, lod_bias
, "shader_lod_bias");
836 /* add sampler lod bias */
837 if (bld
->static_sampler_state
->lod_bias_non_zero
) {
838 LLVMValueRef sampler_lod_bias
=
839 dynamic_state
->lod_bias(dynamic_state
, bld
->gallivm
,
840 bld
->context_ptr
, sampler_unit
);
841 sampler_lod_bias
= lp_build_broadcast_scalar(lodf_bld
,
843 lod
= LLVMBuildFAdd(builder
, lod
, sampler_lod_bias
, "sampler_lod_bias");
847 if (bld
->static_sampler_state
->apply_max_lod
) {
848 LLVMValueRef max_lod
=
849 dynamic_state
->max_lod(dynamic_state
, bld
->gallivm
,
850 bld
->context_ptr
, sampler_unit
);
851 max_lod
= lp_build_broadcast_scalar(lodf_bld
, max_lod
);
853 lod
= lp_build_min(lodf_bld
, lod
, max_lod
);
855 if (bld
->static_sampler_state
->apply_min_lod
) {
856 LLVMValueRef min_lod
=
857 dynamic_state
->min_lod(dynamic_state
, bld
->gallivm
,
858 bld
->context_ptr
, sampler_unit
);
859 min_lod
= lp_build_broadcast_scalar(lodf_bld
, min_lod
);
861 lod
= lp_build_max(lodf_bld
, lod
, min_lod
);
865 *out_lod_positive
= lp_build_cmp(lodf_bld
, PIPE_FUNC_GREATER
,
866 lod
, lodf_bld
->zero
);
868 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
869 if (!(gallivm_debug
& GALLIVM_DEBUG_NO_BRILINEAR
)) {
870 lp_build_brilinear_lod(lodf_bld
, lod
, BRILINEAR_FACTOR
,
871 out_lod_ipart
, out_lod_fpart
);
874 lp_build_ifloor_fract(lodf_bld
, lod
, out_lod_ipart
, out_lod_fpart
);
877 lp_build_name(*out_lod_fpart
, "lod_fpart");
880 *out_lod_ipart
= lp_build_iround(lodf_bld
, lod
);
883 lp_build_name(*out_lod_ipart
, "lod_ipart");
890 * For PIPE_TEX_MIPFILTER_NEAREST, convert int part of lod
891 * to actual mip level.
892 * Note: this is all scalar per quad code.
893 * \param lod_ipart int texture level of detail
894 * \param level_out returns integer
895 * \param out_of_bounds returns per coord out_of_bounds mask if provided
898 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
899 unsigned texture_unit
,
900 LLVMValueRef lod_ipart
,
901 LLVMValueRef
*level_out
,
902 LLVMValueRef
*out_of_bounds
)
904 struct lp_build_context
*leveli_bld
= &bld
->leveli_bld
;
905 struct lp_sampler_dynamic_state
*dynamic_state
= bld
->dynamic_state
;
906 LLVMValueRef first_level
, last_level
, level
;
908 first_level
= dynamic_state
->first_level(dynamic_state
, bld
->gallivm
,
909 bld
->context_ptr
, texture_unit
);
910 last_level
= dynamic_state
->last_level(dynamic_state
, bld
->gallivm
,
911 bld
->context_ptr
, texture_unit
);
912 first_level
= lp_build_broadcast_scalar(leveli_bld
, first_level
);
913 last_level
= lp_build_broadcast_scalar(leveli_bld
, last_level
);
915 level
= lp_build_add(leveli_bld
, lod_ipart
, first_level
);
918 LLVMValueRef out
, out1
;
919 out
= lp_build_cmp(leveli_bld
, PIPE_FUNC_LESS
, level
, first_level
);
920 out1
= lp_build_cmp(leveli_bld
, PIPE_FUNC_GREATER
, level
, last_level
);
921 out
= lp_build_or(leveli_bld
, out
, out1
);
922 if (bld
->num_mips
== bld
->coord_bld
.type
.length
) {
923 *out_of_bounds
= out
;
925 else if (bld
->num_mips
== 1) {
926 *out_of_bounds
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, out
);
929 assert(bld
->num_mips
== bld
->coord_bld
.type
.length
/ 4);
930 *out_of_bounds
= lp_build_unpack_broadcast_aos_scalars(bld
->gallivm
,
932 bld
->int_coord_bld
.type
,
935 level
= lp_build_andnot(&bld
->int_coord_bld
, level
, *out_of_bounds
);
939 /* clamp level to legal range of levels */
940 *level_out
= lp_build_clamp(leveli_bld
, level
, first_level
, last_level
);
947 * For PIPE_TEX_MIPFILTER_LINEAR, convert per-quad (or per element) int LOD(s)
948 * to two (per-quad) (adjacent) mipmap level indexes, and fix up float lod
950 * Later, we'll sample from those two mipmap levels and interpolate between them.
953 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
954 unsigned texture_unit
,
955 LLVMValueRef lod_ipart
,
956 LLVMValueRef
*lod_fpart_inout
,
957 LLVMValueRef
*level0_out
,
958 LLVMValueRef
*level1_out
)
960 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
961 struct lp_sampler_dynamic_state
*dynamic_state
= bld
->dynamic_state
;
962 struct lp_build_context
*leveli_bld
= &bld
->leveli_bld
;
963 struct lp_build_context
*levelf_bld
= &bld
->levelf_bld
;
964 LLVMValueRef first_level
, last_level
;
965 LLVMValueRef clamp_min
;
966 LLVMValueRef clamp_max
;
968 assert(bld
->num_lods
== bld
->num_mips
);
970 first_level
= dynamic_state
->first_level(dynamic_state
, bld
->gallivm
,
971 bld
->context_ptr
, texture_unit
);
972 last_level
= dynamic_state
->last_level(dynamic_state
, bld
->gallivm
,
973 bld
->context_ptr
, texture_unit
);
974 first_level
= lp_build_broadcast_scalar(leveli_bld
, first_level
);
975 last_level
= lp_build_broadcast_scalar(leveli_bld
, last_level
);
977 *level0_out
= lp_build_add(leveli_bld
, lod_ipart
, first_level
);
978 *level1_out
= lp_build_add(leveli_bld
, *level0_out
, leveli_bld
->one
);
981 * Clamp both *level0_out and *level1_out to [first_level, last_level], with
982 * the minimum number of comparisons, and zeroing lod_fpart in the extreme
983 * ends in the process.
986 /* *level0_out < first_level */
987 clamp_min
= LLVMBuildICmp(builder
, LLVMIntSLT
,
988 *level0_out
, first_level
,
989 "clamp_lod_to_first");
991 *level0_out
= LLVMBuildSelect(builder
, clamp_min
,
992 first_level
, *level0_out
, "");
994 *level1_out
= LLVMBuildSelect(builder
, clamp_min
,
995 first_level
, *level1_out
, "");
997 *lod_fpart_inout
= LLVMBuildSelect(builder
, clamp_min
,
998 levelf_bld
->zero
, *lod_fpart_inout
, "");
1000 /* *level0_out >= last_level */
1001 clamp_max
= LLVMBuildICmp(builder
, LLVMIntSGE
,
1002 *level0_out
, last_level
,
1003 "clamp_lod_to_last");
1005 *level0_out
= LLVMBuildSelect(builder
, clamp_max
,
1006 last_level
, *level0_out
, "");
1008 *level1_out
= LLVMBuildSelect(builder
, clamp_max
,
1009 last_level
, *level1_out
, "");
1011 *lod_fpart_inout
= LLVMBuildSelect(builder
, clamp_max
,
1012 levelf_bld
->zero
, *lod_fpart_inout
, "");
1014 lp_build_name(*level0_out
, "texture%u_miplevel0", texture_unit
);
1015 lp_build_name(*level1_out
, "texture%u_miplevel1", texture_unit
);
1016 lp_build_name(*lod_fpart_inout
, "texture%u_mipweight", texture_unit
);
1021 * Return pointer to a single mipmap level.
1022 * \param level integer mipmap level
1025 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
1028 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1029 LLVMValueRef indexes
[2], data_ptr
, mip_offset
;
1031 indexes
[0] = lp_build_const_int32(bld
->gallivm
, 0);
1033 mip_offset
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
1034 mip_offset
= LLVMBuildLoad(builder
, mip_offset
, "");
1035 data_ptr
= LLVMBuildGEP(builder
, bld
->base_ptr
, &mip_offset
, 1, "");
1040 * Return (per-pixel) offsets to mip levels.
1041 * \param level integer mipmap level
1044 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
1047 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1048 LLVMValueRef indexes
[2], offsets
, offset1
;
1050 indexes
[0] = lp_build_const_int32(bld
->gallivm
, 0);
1051 if (bld
->num_mips
== 1) {
1053 offset1
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
1054 offset1
= LLVMBuildLoad(builder
, offset1
, "");
1055 offsets
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, offset1
);
1057 else if (bld
->num_mips
== bld
->coord_bld
.type
.length
/ 4) {
1060 offsets
= bld
->int_coord_bld
.undef
;
1061 for (i
= 0; i
< bld
->num_mips
; i
++) {
1062 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1063 LLVMValueRef indexo
= lp_build_const_int32(bld
->gallivm
, 4 * i
);
1064 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
1065 offset1
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
1066 offset1
= LLVMBuildLoad(builder
, offset1
, "");
1067 offsets
= LLVMBuildInsertElement(builder
, offsets
, offset1
, indexo
, "");
1069 offsets
= lp_build_swizzle_scalar_aos(&bld
->int_coord_bld
, offsets
, 0, 4);
1074 assert (bld
->num_mips
== bld
->coord_bld
.type
.length
);
1076 offsets
= bld
->int_coord_bld
.undef
;
1077 for (i
= 0; i
< bld
->num_mips
; i
++) {
1078 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1079 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
1080 offset1
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
1081 offset1
= LLVMBuildLoad(builder
, offset1
, "");
1082 offsets
= LLVMBuildInsertElement(builder
, offsets
, offset1
, indexi
, "");
1090 * Codegen equivalent for u_minify().
1091 * @param lod_scalar if lod is a (broadcasted) scalar
1092 * Return max(1, base_size >> level);
1095 lp_build_minify(struct lp_build_context
*bld
,
1096 LLVMValueRef base_size
,
1100 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1101 assert(lp_check_value(bld
->type
, base_size
));
1102 assert(lp_check_value(bld
->type
, level
));
1104 if (level
== bld
->zero
) {
1105 /* if we're using mipmap level zero, no minification is needed */
1110 assert(bld
->type
.sign
);
1112 (util_cpu_caps
.has_avx2
|| !util_cpu_caps
.has_sse
)) {
1113 size
= LLVMBuildLShr(builder
, base_size
, level
, "minify");
1114 size
= lp_build_max(bld
, size
, bld
->one
);
1118 * emulate shift with float mul, since intel "forgot" shifts with
1119 * per-element shift count until avx2, which results in terrible
1120 * scalar extraction (both count and value), scalar shift,
1121 * vector reinsertion. Should not be an issue on any non-x86 cpu
1122 * with a vector instruction set.
1123 * On cpus with AMD's XOP this should also be unnecessary but I'm
1124 * not sure if llvm would emit this with current flags.
1126 LLVMValueRef const127
, const23
, lf
;
1127 struct lp_type ftype
;
1128 struct lp_build_context fbld
;
1129 ftype
= lp_type_float_vec(32, bld
->type
.length
* bld
->type
.width
);
1130 lp_build_context_init(&fbld
, bld
->gallivm
, ftype
);
1131 const127
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, 127);
1132 const23
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, 23);
1134 /* calculate 2^(-level) float */
1135 lf
= lp_build_sub(bld
, const127
, level
);
1136 lf
= lp_build_shl(bld
, lf
, const23
);
1137 lf
= LLVMBuildBitCast(builder
, lf
, fbld
.vec_type
, "");
1139 /* finish shift operation by doing float mul */
1140 base_size
= lp_build_int_to_float(&fbld
, base_size
);
1141 size
= lp_build_mul(&fbld
, base_size
, lf
);
1143 * do the max also with floats because
1144 * a) non-emulated int max requires sse41
1145 * (this is actually a lie as we could cast to 16bit values
1146 * as 16bit is sufficient and 16bit int max is sse2)
1147 * b) with avx we can do int max 4-wide but float max 8-wide
1149 size
= lp_build_max(&fbld
, size
, fbld
.one
);
1150 size
= lp_build_itrunc(&fbld
, size
);
1158 * Dereference stride_array[mipmap_level] array to get a stride.
1159 * Return stride as a vector.
1162 lp_build_get_level_stride_vec(struct lp_build_sample_context
*bld
,
1163 LLVMValueRef stride_array
, LLVMValueRef level
)
1165 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1166 LLVMValueRef indexes
[2], stride
, stride1
;
1167 indexes
[0] = lp_build_const_int32(bld
->gallivm
, 0);
1168 if (bld
->num_mips
== 1) {
1170 stride1
= LLVMBuildGEP(builder
, stride_array
, indexes
, 2, "");
1171 stride1
= LLVMBuildLoad(builder
, stride1
, "");
1172 stride
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, stride1
);
1174 else if (bld
->num_mips
== bld
->coord_bld
.type
.length
/ 4) {
1175 LLVMValueRef stride1
;
1178 stride
= bld
->int_coord_bld
.undef
;
1179 for (i
= 0; i
< bld
->num_mips
; i
++) {
1180 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1181 LLVMValueRef indexo
= lp_build_const_int32(bld
->gallivm
, 4 * i
);
1182 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
1183 stride1
= LLVMBuildGEP(builder
, stride_array
, indexes
, 2, "");
1184 stride1
= LLVMBuildLoad(builder
, stride1
, "");
1185 stride
= LLVMBuildInsertElement(builder
, stride
, stride1
, indexo
, "");
1187 stride
= lp_build_swizzle_scalar_aos(&bld
->int_coord_bld
, stride
, 0, 4);
1190 LLVMValueRef stride1
;
1193 assert (bld
->num_mips
== bld
->coord_bld
.type
.length
);
1195 stride
= bld
->int_coord_bld
.undef
;
1196 for (i
= 0; i
< bld
->coord_bld
.type
.length
; i
++) {
1197 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1198 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
1199 stride1
= LLVMBuildGEP(builder
, stride_array
, indexes
, 2, "");
1200 stride1
= LLVMBuildLoad(builder
, stride1
, "");
1201 stride
= LLVMBuildInsertElement(builder
, stride
, stride1
, indexi
, "");
1209 * When sampling a mipmap, we need to compute the width, height, depth
1210 * of the source levels from the level indexes. This helper function
1214 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
1215 LLVMValueRef ilevel
,
1216 LLVMValueRef
*out_size
,
1217 LLVMValueRef
*row_stride_vec
,
1218 LLVMValueRef
*img_stride_vec
)
1220 const unsigned dims
= bld
->dims
;
1221 LLVMValueRef ilevel_vec
;
1224 * Compute width, height, depth at mipmap level 'ilevel'
1226 if (bld
->num_mips
== 1) {
1227 ilevel_vec
= lp_build_broadcast_scalar(&bld
->int_size_bld
, ilevel
);
1228 *out_size
= lp_build_minify(&bld
->int_size_bld
, bld
->int_size
, ilevel_vec
, TRUE
);
1231 LLVMValueRef int_size_vec
;
1232 LLVMValueRef tmp
[LP_MAX_VECTOR_LENGTH
];
1233 unsigned num_quads
= bld
->coord_bld
.type
.length
/ 4;
1236 if (bld
->num_mips
== num_quads
) {
1238 * XXX: this should be #ifndef SANE_INSTRUCTION_SET.
1239 * intel "forgot" the variable shift count instruction until avx2.
1240 * A harmless 8x32 shift gets translated into 32 instructions
1241 * (16 extracts, 8 scalar shifts, 8 inserts), llvm is apparently
1242 * unable to recognize if there are really just 2 different shift
1243 * count values. So do the shift 4-wide before expansion.
1245 struct lp_build_context bld4
;
1246 struct lp_type type4
;
1248 type4
= bld
->int_coord_bld
.type
;
1251 lp_build_context_init(&bld4
, bld
->gallivm
, type4
);
1253 if (bld
->dims
== 1) {
1254 assert(bld
->int_size_in_bld
.type
.length
== 1);
1255 int_size_vec
= lp_build_broadcast_scalar(&bld4
,
1259 assert(bld
->int_size_in_bld
.type
.length
== 4);
1260 int_size_vec
= bld
->int_size
;
1263 for (i
= 0; i
< num_quads
; i
++) {
1264 LLVMValueRef ileveli
;
1265 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1267 ileveli
= lp_build_extract_broadcast(bld
->gallivm
,
1268 bld
->leveli_bld
.type
,
1272 tmp
[i
] = lp_build_minify(&bld4
, int_size_vec
, ileveli
, TRUE
);
1275 * out_size is [w0, h0, d0, _, w1, h1, d1, _, ...] vector for dims > 1,
1276 * [w0, w0, w0, w0, w1, w1, w1, w1, ...] otherwise.
1278 *out_size
= lp_build_concat(bld
->gallivm
,
1284 /* FIXME: this is terrible and results in _huge_ vector
1285 * (for the dims > 1 case).
1286 * Should refactor this (together with extract_image_sizes) and do
1287 * something more useful. Could for instance if we have width,height
1288 * with 4-wide vector pack all elements into a 8xi16 vector
1289 * (on which we can still do useful math) instead of using a 16xi32
1291 * For dims == 1 this will create [w0, w1, w2, w3, ...] vector.
1292 * For dims > 1 this will create [w0, h0, d0, _, w1, h1, d1, _, ...] vector.
1294 assert(bld
->num_mips
== bld
->coord_bld
.type
.length
);
1295 if (bld
->dims
== 1) {
1296 assert(bld
->int_size_in_bld
.type
.length
== 1);
1297 int_size_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
,
1299 *out_size
= lp_build_minify(&bld
->int_coord_bld
, int_size_vec
, ilevel
, FALSE
);
1302 LLVMValueRef ilevel1
;
1303 for (i
= 0; i
< bld
->num_mips
; i
++) {
1304 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1305 ilevel1
= lp_build_extract_broadcast(bld
->gallivm
, bld
->int_coord_type
,
1306 bld
->int_size_in_bld
.type
, ilevel
, indexi
);
1307 tmp
[i
] = bld
->int_size
;
1308 tmp
[i
] = lp_build_minify(&bld
->int_size_in_bld
, tmp
[i
], ilevel1
, TRUE
);
1310 *out_size
= lp_build_concat(bld
->gallivm
, tmp
,
1311 bld
->int_size_in_bld
.type
,
1318 *row_stride_vec
= lp_build_get_level_stride_vec(bld
,
1319 bld
->row_stride_array
,
1322 if (dims
== 3 || has_layer_coord(bld
->static_texture_state
->target
)) {
1323 *img_stride_vec
= lp_build_get_level_stride_vec(bld
,
1324 bld
->img_stride_array
,
1331 * Extract and broadcast texture size.
1333 * @param size_type type of the texture size vector (either
1334 * bld->int_size_type or bld->float_size_type)
1335 * @param coord_type type of the texture size vector (either
1336 * bld->int_coord_type or bld->coord_type)
1337 * @param size vector with the texture size (width, height, depth)
1340 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
1341 struct lp_build_context
*size_bld
,
1342 struct lp_type coord_type
,
1344 LLVMValueRef
*out_width
,
1345 LLVMValueRef
*out_height
,
1346 LLVMValueRef
*out_depth
)
1348 const unsigned dims
= bld
->dims
;
1349 LLVMTypeRef i32t
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
1350 struct lp_type size_type
= size_bld
->type
;
1352 if (bld
->num_mips
== 1) {
1353 *out_width
= lp_build_extract_broadcast(bld
->gallivm
,
1357 LLVMConstInt(i32t
, 0, 0));
1359 *out_height
= lp_build_extract_broadcast(bld
->gallivm
,
1363 LLVMConstInt(i32t
, 1, 0));
1365 *out_depth
= lp_build_extract_broadcast(bld
->gallivm
,
1369 LLVMConstInt(i32t
, 2, 0));
1374 unsigned num_quads
= bld
->coord_bld
.type
.length
/ 4;
1379 else if (bld
->num_mips
== num_quads
) {
1380 *out_width
= lp_build_swizzle_scalar_aos(size_bld
, size
, 0, 4);
1382 *out_height
= lp_build_swizzle_scalar_aos(size_bld
, size
, 1, 4);
1384 *out_depth
= lp_build_swizzle_scalar_aos(size_bld
, size
, 2, 4);
1389 assert(bld
->num_mips
== bld
->coord_type
.length
);
1390 *out_width
= lp_build_pack_aos_scalars(bld
->gallivm
, size_type
,
1391 coord_type
, size
, 0);
1393 *out_height
= lp_build_pack_aos_scalars(bld
->gallivm
, size_type
,
1394 coord_type
, size
, 1);
1396 *out_depth
= lp_build_pack_aos_scalars(bld
->gallivm
, size_type
,
1397 coord_type
, size
, 2);
1406 * Unnormalize coords.
1408 * @param flt_size vector with the integer texture size (width, height, depth)
1411 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
1412 LLVMValueRef flt_size
,
1417 const unsigned dims
= bld
->dims
;
1419 LLVMValueRef height
;
1422 lp_build_extract_image_sizes(bld
,
1423 &bld
->float_size_bld
,
1430 /* s = s * width, t = t * height */
1431 *s
= lp_build_mul(&bld
->coord_bld
, *s
, width
);
1433 *t
= lp_build_mul(&bld
->coord_bld
, *t
, height
);
1435 *r
= lp_build_mul(&bld
->coord_bld
, *r
, depth
);
1441 * Generate new coords and faces for cubemap texels falling off the face.
1443 * @param face face (center) of the pixel
1444 * @param x0 lower x coord
1445 * @param x1 higher x coord (must be x0 + 1)
1446 * @param y0 lower y coord
1447 * @param y1 higher y coord (must be x0 + 1)
1448 * @param max_coord texture cube (level) size - 1
1449 * @param next_faces new face values when falling off
1450 * @param next_xcoords new x coord values when falling off
1451 * @param next_ycoords new y coord values when falling off
1453 * The arrays hold the new values when under/overflow of
1454 * lower x, higher x, lower y, higher y coord would occur (in this order).
1455 * next_xcoords/next_ycoords have two entries each (for both new lower and
1459 lp_build_cube_new_coords(struct lp_build_context
*ivec_bld
,
1465 LLVMValueRef max_coord
,
1466 LLVMValueRef next_faces
[4],
1467 LLVMValueRef next_xcoords
[4][2],
1468 LLVMValueRef next_ycoords
[4][2])
1471 * Lookup tables aren't nice for simd code hence try some logic here.
1472 * (Note that while it would not be necessary to do per-sample (4) lookups
1473 * when using a LUT as it's impossible that texels fall off of positive
1474 * and negative edges simultaneously, it would however be necessary to
1475 * do 2 lookups for corner handling as in this case texels both fall off
1479 * Next faces (for face 012345):
1484 * Hence nfx+ (and nfy+) == nfx- (nfy-) xor 1
1485 * nfx-: face > 1 ? (face == 5 ? 0 : 1) : (4 + face & 1)
1486 * nfy+: face & ~4 > 1 ? face + 2 : 3;
1487 * This could also use pshufb instead, but would need (manually coded)
1488 * ssse3 intrinsic (llvm won't do non-constant shuffles).
1490 struct gallivm_state
*gallivm
= ivec_bld
->gallivm
;
1491 LLVMValueRef sel
, sel_f2345
, sel_f23
, sel_f2
, tmpsel
, tmp
;
1492 LLVMValueRef faceand1
, sel_fand1
, maxmx0
, maxmx1
, maxmy0
, maxmy1
;
1493 LLVMValueRef c2
= lp_build_const_int_vec(gallivm
, ivec_bld
->type
, 2);
1494 LLVMValueRef c3
= lp_build_const_int_vec(gallivm
, ivec_bld
->type
, 3);
1495 LLVMValueRef c4
= lp_build_const_int_vec(gallivm
, ivec_bld
->type
, 4);
1496 LLVMValueRef c5
= lp_build_const_int_vec(gallivm
, ivec_bld
->type
, 5);
1498 sel
= lp_build_cmp(ivec_bld
, PIPE_FUNC_EQUAL
, face
, c5
);
1499 tmpsel
= lp_build_select(ivec_bld
, sel
, ivec_bld
->zero
, ivec_bld
->one
);
1500 sel_f2345
= lp_build_cmp(ivec_bld
, PIPE_FUNC_GREATER
, face
, ivec_bld
->one
);
1501 faceand1
= lp_build_and(ivec_bld
, face
, ivec_bld
->one
);
1502 tmp
= lp_build_add(ivec_bld
, faceand1
, c4
);
1503 next_faces
[0] = lp_build_select(ivec_bld
, sel_f2345
, tmpsel
, tmp
);
1504 next_faces
[1] = lp_build_xor(ivec_bld
, next_faces
[0], ivec_bld
->one
);
1506 tmp
= lp_build_andnot(ivec_bld
, face
, c4
);
1507 sel_f23
= lp_build_cmp(ivec_bld
, PIPE_FUNC_GREATER
, tmp
, ivec_bld
->one
);
1508 tmp
= lp_build_add(ivec_bld
, face
, c2
);
1509 next_faces
[3] = lp_build_select(ivec_bld
, sel_f23
, tmp
, c3
);
1510 next_faces
[2] = lp_build_xor(ivec_bld
, next_faces
[3], ivec_bld
->one
);
1513 * new xcoords (for face 012345):
1514 * x < 0.0 : max max t max-t max max
1515 * x >= 1.0 : 0 0 max-t t 0 0
1516 * y < 0.0 : max 0 max-s s s max-s
1517 * y >= 1.0 : max 0 s max-s s max-s
1519 * ncx[1] = face & ~4 > 1 ? (face == 2 ? max-t : t) : 0
1520 * ncx[0] = max - ncx[1]
1521 * ncx[3] = face > 1 ? (face & 1 ? max-s : s) : (face & 1) ? 0 : max
1522 * ncx[2] = face & ~4 > 1 ? max - ncx[3] : ncx[3]
1524 sel_f2
= lp_build_cmp(ivec_bld
, PIPE_FUNC_EQUAL
, face
, c2
);
1525 maxmy0
= lp_build_sub(ivec_bld
, max_coord
, y0
);
1526 tmp
= lp_build_select(ivec_bld
, sel_f2
, maxmy0
, y0
);
1527 next_xcoords
[1][0] = lp_build_select(ivec_bld
, sel_f23
, tmp
, ivec_bld
->zero
);
1528 next_xcoords
[0][0] = lp_build_sub(ivec_bld
, max_coord
, next_xcoords
[1][0]);
1529 maxmy1
= lp_build_sub(ivec_bld
, max_coord
, y1
);
1530 tmp
= lp_build_select(ivec_bld
, sel_f2
, maxmy1
, y1
);
1531 next_xcoords
[1][1] = lp_build_select(ivec_bld
, sel_f23
, tmp
, ivec_bld
->zero
);
1532 next_xcoords
[0][1] = lp_build_sub(ivec_bld
, max_coord
, next_xcoords
[1][1]);
1534 sel_fand1
= lp_build_cmp(ivec_bld
, PIPE_FUNC_EQUAL
, faceand1
, ivec_bld
->one
);
1536 tmpsel
= lp_build_select(ivec_bld
, sel_fand1
, ivec_bld
->zero
, max_coord
);
1537 maxmx0
= lp_build_sub(ivec_bld
, max_coord
, x0
);
1538 tmp
= lp_build_select(ivec_bld
, sel_fand1
, maxmx0
, x0
);
1539 next_xcoords
[3][0] = lp_build_select(ivec_bld
, sel_f2345
, tmp
, tmpsel
);
1540 tmp
= lp_build_sub(ivec_bld
, max_coord
, next_xcoords
[3][0]);
1541 next_xcoords
[2][0] = lp_build_select(ivec_bld
, sel_f23
, tmp
, next_xcoords
[3][0]);
1542 maxmx1
= lp_build_sub(ivec_bld
, max_coord
, x1
);
1543 tmp
= lp_build_select(ivec_bld
, sel_fand1
, maxmx1
, x1
);
1544 next_xcoords
[3][1] = lp_build_select(ivec_bld
, sel_f2345
, tmp
, tmpsel
);
1545 tmp
= lp_build_sub(ivec_bld
, max_coord
, next_xcoords
[3][1]);
1546 next_xcoords
[2][1] = lp_build_select(ivec_bld
, sel_f23
, tmp
, next_xcoords
[3][1]);
1549 * new ycoords (for face 012345):
1550 * x < 0.0 : t t 0 max t t
1551 * x >= 1.0 : t t 0 max t t
1552 * y < 0.0 : max-s s 0 max max 0
1553 * y >= 1.0 : s max-s 0 max 0 max
1555 * ncy[0] = face & ~4 > 1 ? (face == 2 ? 0 : max) : t
1557 * ncy[3] = face > 1 ? (face & 1 ? max : 0) : (face & 1) ? max-s : max
1558 * ncx[2] = face & ~4 > 1 ? max - ncx[3] : ncx[3]
1560 tmp
= lp_build_select(ivec_bld
, sel_f2
, ivec_bld
->zero
, max_coord
);
1561 next_ycoords
[0][0] = lp_build_select(ivec_bld
, sel_f23
, tmp
, y0
);
1562 next_ycoords
[1][0] = next_ycoords
[0][0];
1563 next_ycoords
[0][1] = lp_build_select(ivec_bld
, sel_f23
, tmp
, y1
);
1564 next_ycoords
[1][1] = next_ycoords
[0][1];
1566 tmpsel
= lp_build_select(ivec_bld
, sel_fand1
, maxmx0
, x0
);
1567 tmp
= lp_build_select(ivec_bld
, sel_fand1
, max_coord
, ivec_bld
->zero
);
1568 next_ycoords
[3][0] = lp_build_select(ivec_bld
, sel_f2345
, tmp
, tmpsel
);
1569 tmp
= lp_build_sub(ivec_bld
, max_coord
, next_ycoords
[3][0]);
1570 next_ycoords
[2][0] = lp_build_select(ivec_bld
, sel_f23
, next_ycoords
[3][0], tmp
);
1571 tmpsel
= lp_build_select(ivec_bld
, sel_fand1
, maxmx1
, x1
);
1572 tmp
= lp_build_select(ivec_bld
, sel_fand1
, max_coord
, ivec_bld
->zero
);
1573 next_ycoords
[3][1] = lp_build_select(ivec_bld
, sel_f2345
, tmp
, tmpsel
);
1574 tmp
= lp_build_sub(ivec_bld
, max_coord
, next_ycoords
[3][1]);
1575 next_ycoords
[2][1] = lp_build_select(ivec_bld
, sel_f23
, next_ycoords
[3][1], tmp
);
1579 /** Helper used by lp_build_cube_lookup() */
1581 lp_build_cube_imapos(struct lp_build_context
*coord_bld
, LLVMValueRef coord
)
1583 /* ima = +0.5 / abs(coord); */
1584 LLVMValueRef posHalf
= lp_build_const_vec(coord_bld
->gallivm
, coord_bld
->type
, 0.5);
1585 LLVMValueRef absCoord
= lp_build_abs(coord_bld
, coord
);
1586 LLVMValueRef ima
= lp_build_div(coord_bld
, posHalf
, absCoord
);
1591 /** Helper for doing 3-wise selection.
1592 * Returns sel1 ? val2 : (sel0 ? val0 : val1).
1595 lp_build_select3(struct lp_build_context
*sel_bld
,
1603 tmp
= lp_build_select(sel_bld
, sel0
, val0
, val1
);
1604 return lp_build_select(sel_bld
, sel1
, val2
, tmp
);
1609 * Generate code to do cube face selection and compute per-face texcoords.
1612 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
1613 LLVMValueRef
*coords
,
1614 const struct lp_derivatives
*derivs_in
, /* optional */
1616 struct lp_derivatives
*derivs_out
, /* optional */
1617 boolean need_derivs
)
1619 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
1620 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1621 struct gallivm_state
*gallivm
= bld
->gallivm
;
1622 LLVMValueRef si
, ti
, ri
;
1625 * Do per-pixel face selection. We cannot however (as we used to do)
1626 * simply calculate the derivs afterwards (which is very bogus for
1627 * explicit derivs btw) because the values would be "random" when
1628 * not all pixels lie on the same face. So what we do here is just
1629 * calculate the derivatives after scaling the coords by the absolute
1630 * value of the inverse major axis, and essentially do rho calculation
1631 * steps as if it were a 3d texture. This is perfect if all pixels hit
1632 * the same face, but not so great at edges, I believe the max error
1633 * should be sqrt(2) with no_rho_approx or 2 otherwise (essentially measuring
1634 * the 3d distance between 2 points on the cube instead of measuring up/down
1635 * the edge). Still this is possibly a win over just selecting the same face
1636 * for all pixels. Unfortunately, something like that doesn't work for
1637 * explicit derivatives.
1639 struct lp_build_context
*cint_bld
= &bld
->int_coord_bld
;
1640 struct lp_type intctype
= cint_bld
->type
;
1641 LLVMTypeRef coord_vec_type
= coord_bld
->vec_type
;
1642 LLVMTypeRef cint_vec_type
= cint_bld
->vec_type
;
1643 LLVMValueRef as
, at
, ar
, face
, face_s
, face_t
;
1644 LLVMValueRef as_ge_at
, maxasat
, ar_ge_as_at
;
1645 LLVMValueRef snewx
, tnewx
, snewy
, tnewy
, snewz
, tnewz
;
1646 LLVMValueRef tnegi
, rnegi
;
1647 LLVMValueRef ma
, mai
, signma
, signmabit
, imahalfpos
;
1648 LLVMValueRef posHalf
= lp_build_const_vec(gallivm
, coord_bld
->type
, 0.5);
1649 LLVMValueRef signmask
= lp_build_const_int_vec(gallivm
, intctype
,
1650 1LL << (intctype
.width
- 1));
1651 LLVMValueRef signshift
= lp_build_const_int_vec(gallivm
, intctype
,
1653 LLVMValueRef facex
= lp_build_const_int_vec(gallivm
, intctype
, PIPE_TEX_FACE_POS_X
);
1654 LLVMValueRef facey
= lp_build_const_int_vec(gallivm
, intctype
, PIPE_TEX_FACE_POS_Y
);
1655 LLVMValueRef facez
= lp_build_const_int_vec(gallivm
, intctype
, PIPE_TEX_FACE_POS_Z
);
1656 LLVMValueRef s
= coords
[0];
1657 LLVMValueRef t
= coords
[1];
1658 LLVMValueRef r
= coords
[2];
1660 assert(PIPE_TEX_FACE_NEG_X
== PIPE_TEX_FACE_POS_X
+ 1);
1661 assert(PIPE_TEX_FACE_NEG_Y
== PIPE_TEX_FACE_POS_Y
+ 1);
1662 assert(PIPE_TEX_FACE_NEG_Z
== PIPE_TEX_FACE_POS_Z
+ 1);
1665 * get absolute value (for x/y/z face selection) and sign bit
1666 * (for mirroring minor coords and pos/neg face selection)
1667 * of the original coords.
1669 as
= lp_build_abs(&bld
->coord_bld
, s
);
1670 at
= lp_build_abs(&bld
->coord_bld
, t
);
1671 ar
= lp_build_abs(&bld
->coord_bld
, r
);
1674 * major face determination: select x if x > y else select y
1675 * select z if z >= max(x,y) else select previous result
1676 * if some axis are the same we chose z over y, y over x - the
1677 * dx10 spec seems to ask for it while OpenGL doesn't care (if we
1678 * wouldn't care could save a select or two if using different
1679 * compares and doing at_g_as_ar last since tnewx and tnewz are the
1682 as_ge_at
= lp_build_cmp(coord_bld
, PIPE_FUNC_GREATER
, as
, at
);
1683 maxasat
= lp_build_max(coord_bld
, as
, at
);
1684 ar_ge_as_at
= lp_build_cmp(coord_bld
, PIPE_FUNC_GEQUAL
, ar
, maxasat
);
1686 if (need_derivs
&& (derivs_in
||
1687 ((gallivm_debug
& GALLIVM_DEBUG_NO_QUAD_LOD
) &&
1688 (gallivm_debug
& GALLIVM_DEBUG_NO_RHO_APPROX
)))) {
1690 * XXX: This is really really complex.
1691 * It is a bit overkill to use this for implicit derivatives as well,
1692 * no way this is worth the cost in practice, but seems to be the
1693 * only way for getting accurate and per-pixel lod values.
1695 LLVMValueRef ima
, imahalf
, tmp
, ddx
[3], ddy
[3];
1696 LLVMValueRef madx
, mady
, madxdivma
, madydivma
;
1697 LLVMValueRef sdxi
, tdxi
, rdxi
, sdyi
, tdyi
, rdyi
;
1698 LLVMValueRef tdxnegi
, rdxnegi
, tdynegi
, rdynegi
;
1699 LLVMValueRef sdxnewx
, sdxnewy
, sdxnewz
, tdxnewx
, tdxnewy
, tdxnewz
;
1700 LLVMValueRef sdynewx
, sdynewy
, sdynewz
, tdynewx
, tdynewy
, tdynewz
;
1701 LLVMValueRef face_sdx
, face_tdx
, face_sdy
, face_tdy
;
1703 * s = 1/2 * ( sc / ma + 1)
1704 * t = 1/2 * ( tc / ma + 1)
1706 * s' = 1/2 * (sc' * ma - sc * ma') / ma^2
1707 * t' = 1/2 * (tc' * ma - tc * ma') / ma^2
1709 * dx.s = 0.5 * (dx.sc - sc * dx.ma / ma) / ma
1710 * dx.t = 0.5 * (dx.tc - tc * dx.ma / ma) / ma
1711 * dy.s = 0.5 * (dy.sc - sc * dy.ma / ma) / ma
1712 * dy.t = 0.5 * (dy.tc - tc * dy.ma / ma) / ma
1715 /* select ma, calculate ima */
1716 ma
= lp_build_select3(coord_bld
, as_ge_at
, ar_ge_as_at
, s
, t
, r
);
1717 mai
= LLVMBuildBitCast(builder
, ma
, cint_vec_type
, "");
1718 signmabit
= LLVMBuildAnd(builder
, mai
, signmask
, "");
1719 ima
= lp_build_div(coord_bld
, coord_bld
->one
, ma
);
1720 imahalf
= lp_build_mul(coord_bld
, posHalf
, ima
);
1721 imahalfpos
= lp_build_abs(coord_bld
, imahalf
);
1724 ddx
[0] = lp_build_ddx(coord_bld
, s
);
1725 ddx
[1] = lp_build_ddx(coord_bld
, t
);
1726 ddx
[2] = lp_build_ddx(coord_bld
, r
);
1727 ddy
[0] = lp_build_ddy(coord_bld
, s
);
1728 ddy
[1] = lp_build_ddy(coord_bld
, t
);
1729 ddy
[2] = lp_build_ddy(coord_bld
, r
);
1732 ddx
[0] = derivs_in
->ddx
[0];
1733 ddx
[1] = derivs_in
->ddx
[1];
1734 ddx
[2] = derivs_in
->ddx
[2];
1735 ddy
[0] = derivs_in
->ddy
[0];
1736 ddy
[1] = derivs_in
->ddy
[1];
1737 ddy
[2] = derivs_in
->ddy
[2];
1740 /* select major derivatives */
1741 madx
= lp_build_select3(coord_bld
, as_ge_at
, ar_ge_as_at
, ddx
[0], ddx
[1], ddx
[2]);
1742 mady
= lp_build_select3(coord_bld
, as_ge_at
, ar_ge_as_at
, ddy
[0], ddy
[1], ddy
[2]);
1744 si
= LLVMBuildBitCast(builder
, s
, cint_vec_type
, "");
1745 ti
= LLVMBuildBitCast(builder
, t
, cint_vec_type
, "");
1746 ri
= LLVMBuildBitCast(builder
, r
, cint_vec_type
, "");
1748 sdxi
= LLVMBuildBitCast(builder
, ddx
[0], cint_vec_type
, "");
1749 tdxi
= LLVMBuildBitCast(builder
, ddx
[1], cint_vec_type
, "");
1750 rdxi
= LLVMBuildBitCast(builder
, ddx
[2], cint_vec_type
, "");
1752 sdyi
= LLVMBuildBitCast(builder
, ddy
[0], cint_vec_type
, "");
1753 tdyi
= LLVMBuildBitCast(builder
, ddy
[1], cint_vec_type
, "");
1754 rdyi
= LLVMBuildBitCast(builder
, ddy
[2], cint_vec_type
, "");
1757 * compute all possible new s/t coords, which does the mirroring,
1758 * and do the same for derivs minor axes.
1759 * snewx = signma * -r;
1762 * tnewy = signma * r;
1763 * snewz = signma * s;
1766 tnegi
= LLVMBuildXor(builder
, ti
, signmask
, "");
1767 rnegi
= LLVMBuildXor(builder
, ri
, signmask
, "");
1768 tdxnegi
= LLVMBuildXor(builder
, tdxi
, signmask
, "");
1769 rdxnegi
= LLVMBuildXor(builder
, rdxi
, signmask
, "");
1770 tdynegi
= LLVMBuildXor(builder
, tdyi
, signmask
, "");
1771 rdynegi
= LLVMBuildXor(builder
, rdyi
, signmask
, "");
1773 snewx
= LLVMBuildXor(builder
, signmabit
, rnegi
, "");
1775 sdxnewx
= LLVMBuildXor(builder
, signmabit
, rdxnegi
, "");
1777 sdynewx
= LLVMBuildXor(builder
, signmabit
, rdynegi
, "");
1781 tnewy
= LLVMBuildXor(builder
, signmabit
, ri
, "");
1783 tdxnewy
= LLVMBuildXor(builder
, signmabit
, rdxi
, "");
1785 tdynewy
= LLVMBuildXor(builder
, signmabit
, rdyi
, "");
1787 snewz
= LLVMBuildXor(builder
, signmabit
, si
, "");
1789 sdxnewz
= LLVMBuildXor(builder
, signmabit
, sdxi
, "");
1791 sdynewz
= LLVMBuildXor(builder
, signmabit
, sdyi
, "");
1794 /* select the mirrored values */
1795 face
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, facex
, facey
, facez
);
1796 face_s
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, snewx
, snewy
, snewz
);
1797 face_t
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, tnewx
, tnewy
, tnewz
);
1798 face_sdx
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, sdxnewx
, sdxnewy
, sdxnewz
);
1799 face_tdx
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, tdxnewx
, tdxnewy
, tdxnewz
);
1800 face_sdy
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, sdynewx
, sdynewy
, sdynewz
);
1801 face_tdy
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, tdynewx
, tdynewy
, tdynewz
);
1803 face_s
= LLVMBuildBitCast(builder
, face_s
, coord_vec_type
, "");
1804 face_t
= LLVMBuildBitCast(builder
, face_t
, coord_vec_type
, "");
1805 face_sdx
= LLVMBuildBitCast(builder
, face_sdx
, coord_vec_type
, "");
1806 face_tdx
= LLVMBuildBitCast(builder
, face_tdx
, coord_vec_type
, "");
1807 face_sdy
= LLVMBuildBitCast(builder
, face_sdy
, coord_vec_type
, "");
1808 face_tdy
= LLVMBuildBitCast(builder
, face_tdy
, coord_vec_type
, "");
1810 /* deriv math, dx.s = 0.5 * (dx.sc - sc * dx.ma / ma) / ma */
1811 madxdivma
= lp_build_mul(coord_bld
, madx
, ima
);
1812 tmp
= lp_build_mul(coord_bld
, madxdivma
, face_s
);
1813 tmp
= lp_build_sub(coord_bld
, face_sdx
, tmp
);
1814 derivs_out
->ddx
[0] = lp_build_mul(coord_bld
, tmp
, imahalf
);
1816 /* dx.t = 0.5 * (dx.tc - tc * dx.ma / ma) / ma */
1817 tmp
= lp_build_mul(coord_bld
, madxdivma
, face_t
);
1818 tmp
= lp_build_sub(coord_bld
, face_tdx
, tmp
);
1819 derivs_out
->ddx
[1] = lp_build_mul(coord_bld
, tmp
, imahalf
);
1821 /* dy.s = 0.5 * (dy.sc - sc * dy.ma / ma) / ma */
1822 madydivma
= lp_build_mul(coord_bld
, mady
, ima
);
1823 tmp
= lp_build_mul(coord_bld
, madydivma
, face_s
);
1824 tmp
= lp_build_sub(coord_bld
, face_sdy
, tmp
);
1825 derivs_out
->ddy
[0] = lp_build_mul(coord_bld
, tmp
, imahalf
);
1827 /* dy.t = 0.5 * (dy.tc - tc * dy.ma / ma) / ma */
1828 tmp
= lp_build_mul(coord_bld
, madydivma
, face_t
);
1829 tmp
= lp_build_sub(coord_bld
, face_tdy
, tmp
);
1830 derivs_out
->ddy
[1] = lp_build_mul(coord_bld
, tmp
, imahalf
);
1832 signma
= LLVMBuildLShr(builder
, mai
, signshift
, "");
1833 coords
[2] = LLVMBuildOr(builder
, face
, signma
, "face");
1835 /* project coords */
1836 face_s
= lp_build_mul(coord_bld
, face_s
, imahalfpos
);
1837 face_t
= lp_build_mul(coord_bld
, face_t
, imahalfpos
);
1839 coords
[0] = lp_build_add(coord_bld
, face_s
, posHalf
);
1840 coords
[1] = lp_build_add(coord_bld
, face_t
, posHalf
);
1845 else if (need_derivs
) {
1846 LLVMValueRef ddx_ddy
[2], tmp
[3], rho_vec
;
1847 static const unsigned char swizzle0
[] = { /* no-op swizzle */
1848 0, LP_BLD_SWIZZLE_DONTCARE
,
1849 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1851 static const unsigned char swizzle1
[] = {
1852 1, LP_BLD_SWIZZLE_DONTCARE
,
1853 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1855 static const unsigned char swizzle01
[] = { /* no-op swizzle */
1857 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1859 static const unsigned char swizzle23
[] = {
1861 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1863 static const unsigned char swizzle02
[] = {
1865 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1869 * scale the s/t/r coords pre-select/mirror so we can calculate
1870 * "reasonable" derivs.
1872 ma
= lp_build_select3(coord_bld
, as_ge_at
, ar_ge_as_at
, s
, t
, r
);
1873 imahalfpos
= lp_build_cube_imapos(coord_bld
, ma
);
1874 s
= lp_build_mul(coord_bld
, s
, imahalfpos
);
1875 t
= lp_build_mul(coord_bld
, t
, imahalfpos
);
1876 r
= lp_build_mul(coord_bld
, r
, imahalfpos
);
1879 * This isn't quite the same as the "ordinary" (3d deriv) path since we
1880 * know the texture is square which simplifies things (we can omit the
1881 * size mul which happens very early completely here and do it at the
1883 * Also always do calculations according to GALLIVM_DEBUG_NO_RHO_APPROX
1884 * since the error can get quite big otherwise at edges.
1885 * (With no_rho_approx max error is sqrt(2) at edges, same as it is
1886 * without no_rho_approx for 2d textures, otherwise it would be factor 2.)
1888 ddx_ddy
[0] = lp_build_packed_ddx_ddy_twocoord(coord_bld
, s
, t
);
1889 ddx_ddy
[1] = lp_build_packed_ddx_ddy_onecoord(coord_bld
, r
);
1891 ddx_ddy
[0] = lp_build_mul(coord_bld
, ddx_ddy
[0], ddx_ddy
[0]);
1892 ddx_ddy
[1] = lp_build_mul(coord_bld
, ddx_ddy
[1], ddx_ddy
[1]);
1894 tmp
[0] = lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle01
);
1895 tmp
[1] = lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle23
);
1896 tmp
[2] = lp_build_swizzle_aos(coord_bld
, ddx_ddy
[1], swizzle02
);
1898 rho_vec
= lp_build_add(coord_bld
, tmp
[0], tmp
[1]);
1899 rho_vec
= lp_build_add(coord_bld
, rho_vec
, tmp
[2]);
1901 tmp
[0] = lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle0
);
1902 tmp
[1] = lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle1
);
1903 *rho
= lp_build_max(coord_bld
, tmp
[0], tmp
[1]);
1907 ma
= lp_build_select3(coord_bld
, as_ge_at
, ar_ge_as_at
, s
, t
, r
);
1909 mai
= LLVMBuildBitCast(builder
, ma
, cint_vec_type
, "");
1910 signmabit
= LLVMBuildAnd(builder
, mai
, signmask
, "");
1912 si
= LLVMBuildBitCast(builder
, s
, cint_vec_type
, "");
1913 ti
= LLVMBuildBitCast(builder
, t
, cint_vec_type
, "");
1914 ri
= LLVMBuildBitCast(builder
, r
, cint_vec_type
, "");
1917 * compute all possible new s/t coords, which does the mirroring
1918 * snewx = signma * -r;
1921 * tnewy = signma * r;
1922 * snewz = signma * s;
1925 tnegi
= LLVMBuildXor(builder
, ti
, signmask
, "");
1926 rnegi
= LLVMBuildXor(builder
, ri
, signmask
, "");
1928 snewx
= LLVMBuildXor(builder
, signmabit
, rnegi
, "");
1932 tnewy
= LLVMBuildXor(builder
, signmabit
, ri
, "");
1934 snewz
= LLVMBuildXor(builder
, signmabit
, si
, "");
1937 /* select the mirrored values */
1938 face_s
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, snewx
, snewy
, snewz
);
1939 face_t
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, tnewx
, tnewy
, tnewz
);
1940 face
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, facex
, facey
, facez
);
1942 face_s
= LLVMBuildBitCast(builder
, face_s
, coord_vec_type
, "");
1943 face_t
= LLVMBuildBitCast(builder
, face_t
, coord_vec_type
, "");
1945 /* add +1 for neg face */
1946 /* XXX with AVX probably want to use another select here -
1947 * as long as we ensure vblendvps gets used we can actually
1948 * skip the comparison and just use sign as a "mask" directly.
1950 signma
= LLVMBuildLShr(builder
, mai
, signshift
, "");
1951 coords
[2] = LLVMBuildOr(builder
, face
, signma
, "face");
1953 /* project coords */
1955 imahalfpos
= lp_build_cube_imapos(coord_bld
, ma
);
1956 face_s
= lp_build_mul(coord_bld
, face_s
, imahalfpos
);
1957 face_t
= lp_build_mul(coord_bld
, face_t
, imahalfpos
);
1960 coords
[0] = lp_build_add(coord_bld
, face_s
, posHalf
);
1961 coords
[1] = lp_build_add(coord_bld
, face_t
, posHalf
);
1966 * Compute the partial offset of a pixel block along an arbitrary axis.
1968 * @param coord coordinate in pixels
1969 * @param stride number of bytes between rows of successive pixel blocks
1970 * @param block_length number of pixels in a pixels block along the coordinate
1972 * @param out_offset resulting relative offset of the pixel block in bytes
1973 * @param out_subcoord resulting sub-block pixel coordinate
1976 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
1977 unsigned block_length
,
1979 LLVMValueRef stride
,
1980 LLVMValueRef
*out_offset
,
1981 LLVMValueRef
*out_subcoord
)
1983 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1984 LLVMValueRef offset
;
1985 LLVMValueRef subcoord
;
1987 if (block_length
== 1) {
1988 subcoord
= bld
->zero
;
1992 * Pixel blocks have power of two dimensions. LLVM should convert the
1993 * rem/div to bit arithmetic.
1994 * TODO: Verify this.
1995 * It does indeed BUT it does transform it to scalar (and back) when doing so
1996 * (using roughly extract, shift/and, mov, unpack) (llvm 2.7).
1997 * The generated code looks seriously unfunny and is quite expensive.
2000 LLVMValueRef block_width
= lp_build_const_int_vec(bld
->type
, block_length
);
2001 subcoord
= LLVMBuildURem(builder
, coord
, block_width
, "");
2002 coord
= LLVMBuildUDiv(builder
, coord
, block_width
, "");
2004 unsigned logbase2
= util_logbase2(block_length
);
2005 LLVMValueRef block_shift
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, logbase2
);
2006 LLVMValueRef block_mask
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, block_length
- 1);
2007 subcoord
= LLVMBuildAnd(builder
, coord
, block_mask
, "");
2008 coord
= LLVMBuildLShr(builder
, coord
, block_shift
, "");
2012 offset
= lp_build_mul(bld
, coord
, stride
);
2015 assert(out_subcoord
);
2017 *out_offset
= offset
;
2018 *out_subcoord
= subcoord
;
2023 * Compute the offset of a pixel block.
2025 * x, y, z, y_stride, z_stride are vectors, and they refer to pixels.
2027 * Returns the relative offset and i,j sub-block coordinates
2030 lp_build_sample_offset(struct lp_build_context
*bld
,
2031 const struct util_format_description
*format_desc
,
2035 LLVMValueRef y_stride
,
2036 LLVMValueRef z_stride
,
2037 LLVMValueRef
*out_offset
,
2038 LLVMValueRef
*out_i
,
2039 LLVMValueRef
*out_j
)
2041 LLVMValueRef x_stride
;
2042 LLVMValueRef offset
;
2044 x_stride
= lp_build_const_vec(bld
->gallivm
, bld
->type
,
2045 format_desc
->block
.bits
/8);
2047 lp_build_sample_partial_offset(bld
,
2048 format_desc
->block
.width
,
2052 if (y
&& y_stride
) {
2053 LLVMValueRef y_offset
;
2054 lp_build_sample_partial_offset(bld
,
2055 format_desc
->block
.height
,
2058 offset
= lp_build_add(bld
, offset
, y_offset
);
2064 if (z
&& z_stride
) {
2065 LLVMValueRef z_offset
;
2067 lp_build_sample_partial_offset(bld
,
2068 1, /* pixel blocks are always 2D */
2071 offset
= lp_build_add(bld
, offset
, z_offset
);
2074 *out_offset
= offset
;