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 "lp_bld_arit.h"
40 #include "lp_bld_const.h"
41 #include "lp_bld_debug.h"
42 #include "lp_bld_printf.h"
43 #include "lp_bld_flow.h"
44 #include "lp_bld_sample.h"
45 #include "lp_bld_swizzle.h"
46 #include "lp_bld_type.h"
47 #include "lp_bld_logic.h"
48 #include "lp_bld_pack.h"
52 * Bri-linear factor. Should be greater than one.
54 #define BRILINEAR_FACTOR 2
57 * Does the given texture wrap mode allow sampling the texture border color?
58 * XXX maybe move this into gallium util code.
61 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
62 unsigned min_img_filter
,
63 unsigned mag_img_filter
)
66 case PIPE_TEX_WRAP_REPEAT
:
67 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
68 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
69 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
71 case PIPE_TEX_WRAP_CLAMP
:
72 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
73 if (min_img_filter
== PIPE_TEX_FILTER_NEAREST
&&
74 mag_img_filter
== PIPE_TEX_FILTER_NEAREST
) {
79 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
80 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
83 assert(0 && "unexpected wrap mode");
90 * Initialize lp_sampler_static_texture_state object with the gallium
91 * texture/sampler_view state (this contains the parts which are
95 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
96 const struct pipe_sampler_view
*view
)
98 const struct pipe_resource
*texture
;
100 memset(state
, 0, sizeof *state
);
102 if (!view
|| !view
->texture
)
105 texture
= view
->texture
;
107 state
->format
= view
->format
;
108 state
->swizzle_r
= view
->swizzle_r
;
109 state
->swizzle_g
= view
->swizzle_g
;
110 state
->swizzle_b
= view
->swizzle_b
;
111 state
->swizzle_a
= view
->swizzle_a
;
113 state
->target
= texture
->target
;
114 state
->pot_width
= util_is_power_of_two(texture
->width0
);
115 state
->pot_height
= util_is_power_of_two(texture
->height0
);
116 state
->pot_depth
= util_is_power_of_two(texture
->depth0
);
117 state
->level_zero_only
= !view
->u
.tex
.last_level
;
120 * FIXME: Handle the remainder of pipe_sampler_view.
126 * Initialize lp_sampler_static_sampler_state object with the gallium sampler
127 * state (this contains the parts which are considered static).
130 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
131 const struct pipe_sampler_state
*sampler
)
133 memset(state
, 0, sizeof *state
);
139 * We don't copy sampler state over unless it is actually enabled, to avoid
140 * spurious recompiles, as the sampler static state is part of the shader
143 * Ideally the state tracker or cso_cache module would make all state
144 * canonical, but until that happens it's better to be safe than sorry here.
146 * XXX: Actually there's much more than can be done here, especially
147 * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
150 state
->wrap_s
= sampler
->wrap_s
;
151 state
->wrap_t
= sampler
->wrap_t
;
152 state
->wrap_r
= sampler
->wrap_r
;
153 state
->min_img_filter
= sampler
->min_img_filter
;
154 state
->mag_img_filter
= sampler
->mag_img_filter
;
156 if (sampler
->max_lod
> 0.0f
) {
157 state
->min_mip_filter
= sampler
->min_mip_filter
;
159 state
->min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
162 if (state
->min_mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
163 if (sampler
->lod_bias
!= 0.0f
) {
164 state
->lod_bias_non_zero
= 1;
167 /* If min_lod == max_lod we can greatly simplify mipmap selection.
168 * This is a case that occurs during automatic mipmap generation.
170 if (sampler
->min_lod
== sampler
->max_lod
) {
171 state
->min_max_lod_equal
= 1;
173 if (sampler
->min_lod
> 0.0f
) {
174 state
->apply_min_lod
= 1;
178 * XXX this won't do anything with the mesa state tracker which always
179 * sets max_lod to not more than actually present mip maps...
181 if (sampler
->max_lod
< (PIPE_MAX_TEXTURE_LEVELS
- 1)) {
182 state
->apply_max_lod
= 1;
187 state
->compare_mode
= sampler
->compare_mode
;
188 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
189 state
->compare_func
= sampler
->compare_func
;
192 state
->normalized_coords
= sampler
->normalized_coords
;
197 * Generate code to compute coordinate gradient (rho).
198 * \param derivs partial derivatives of (s, t, r, q) with respect to X and Y
200 * The resulting rho is scalar per quad.
203 lp_build_rho(struct lp_build_sample_context
*bld
,
204 unsigned texture_unit
,
205 const struct lp_derivatives
*derivs
)
207 struct gallivm_state
*gallivm
= bld
->gallivm
;
208 struct lp_build_context
*int_size_bld
= &bld
->int_size_in_bld
;
209 struct lp_build_context
*float_size_bld
= &bld
->float_size_in_bld
;
210 struct lp_build_context
*float_bld
= &bld
->float_bld
;
211 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
212 struct lp_build_context
*perquadf_bld
= &bld
->perquadf_bld
;
213 const LLVMValueRef
*ddx_ddy
= derivs
->ddx_ddy
;
214 const unsigned dims
= bld
->dims
;
215 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
216 LLVMTypeRef i32t
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
217 LLVMValueRef index0
= LLVMConstInt(i32t
, 0, 0);
218 LLVMValueRef index1
= LLVMConstInt(i32t
, 1, 0);
219 LLVMValueRef index2
= LLVMConstInt(i32t
, 2, 0);
220 LLVMValueRef rho_vec
;
221 LLVMValueRef int_size
, float_size
;
223 LLVMValueRef first_level
, first_level_vec
;
224 LLVMValueRef abs_ddx_ddy
[2];
225 unsigned length
= coord_bld
->type
.length
;
226 unsigned num_quads
= length
/ 4;
228 LLVMValueRef i32undef
= LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
229 LLVMValueRef rho_xvec
, rho_yvec
;
231 abs_ddx_ddy
[0] = lp_build_abs(coord_bld
, ddx_ddy
[0]);
233 abs_ddx_ddy
[1] = lp_build_abs(coord_bld
, ddx_ddy
[1]);
236 abs_ddx_ddy
[1] = NULL
;
240 static const unsigned char swizzle1
[] = {
241 0, LP_BLD_SWIZZLE_DONTCARE
,
242 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
244 static const unsigned char swizzle2
[] = {
245 1, LP_BLD_SWIZZLE_DONTCARE
,
246 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
248 rho_xvec
= lp_build_swizzle_aos(coord_bld
, abs_ddx_ddy
[0], swizzle1
);
249 rho_yvec
= lp_build_swizzle_aos(coord_bld
, abs_ddx_ddy
[0], swizzle2
);
251 else if (dims
== 2) {
252 static const unsigned char swizzle1
[] = {
254 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
256 static const unsigned char swizzle2
[] = {
258 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
260 rho_xvec
= lp_build_swizzle_aos(coord_bld
, abs_ddx_ddy
[0], swizzle1
);
261 rho_yvec
= lp_build_swizzle_aos(coord_bld
, abs_ddx_ddy
[0], swizzle2
);
264 LLVMValueRef shuffles1
[LP_MAX_VECTOR_LENGTH
];
265 LLVMValueRef shuffles2
[LP_MAX_VECTOR_LENGTH
];
267 for (i
= 0; i
< num_quads
; i
++) {
268 shuffles1
[4*i
+ 0] = lp_build_const_int32(gallivm
, 4*i
);
269 shuffles1
[4*i
+ 1] = lp_build_const_int32(gallivm
, 4*i
+ 2);
270 shuffles1
[4*i
+ 2] = lp_build_const_int32(gallivm
, length
+ 4*i
);
271 shuffles1
[4*i
+ 3] = i32undef
;
272 shuffles2
[4*i
+ 0] = lp_build_const_int32(gallivm
, 4*i
+ 1);
273 shuffles2
[4*i
+ 1] = lp_build_const_int32(gallivm
, 4*i
+ 3);
274 shuffles2
[4*i
+ 2] = lp_build_const_int32(gallivm
, length
+ 4*i
+ 1);
275 shuffles2
[4*i
+ 3] = i32undef
;
277 rho_xvec
= LLVMBuildShuffleVector(builder
, abs_ddx_ddy
[0], abs_ddx_ddy
[1],
278 LLVMConstVector(shuffles1
, length
), "");
279 rho_yvec
= LLVMBuildShuffleVector(builder
, abs_ddx_ddy
[0], abs_ddx_ddy
[1],
280 LLVMConstVector(shuffles2
, length
), "");
283 rho_vec
= lp_build_max(coord_bld
, rho_xvec
, rho_yvec
);
285 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
286 bld
->gallivm
, texture_unit
);
287 first_level_vec
= lp_build_broadcast_scalar(int_size_bld
, first_level
);
288 int_size
= lp_build_minify(int_size_bld
, bld
->int_size
, first_level_vec
);
289 float_size
= lp_build_int_to_float(float_size_bld
, int_size
);
291 if (bld
->coord_type
.length
> 4) {
292 /* expand size to each quad */
294 /* could use some broadcast_vector helper for this? */
295 int num_quads
= bld
->coord_type
.length
/ 4;
296 LLVMValueRef src
[LP_MAX_VECTOR_LENGTH
/4];
297 for (i
= 0; i
< num_quads
; i
++) {
300 float_size
= lp_build_concat(bld
->gallivm
, src
, float_size_bld
->type
, num_quads
);
303 float_size
= lp_build_broadcast_scalar(coord_bld
, float_size
);
305 rho_vec
= lp_build_mul(coord_bld
, rho_vec
, float_size
);
312 static const unsigned char swizzle1
[] = {
313 0, LP_BLD_SWIZZLE_DONTCARE
,
314 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
316 static const unsigned char swizzle2
[] = {
317 1, LP_BLD_SWIZZLE_DONTCARE
,
318 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
320 LLVMValueRef rho_s
, rho_t
, rho_r
;
322 rho_s
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle1
);
323 rho_t
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle2
);
325 rho
= lp_build_max(coord_bld
, rho_s
, rho_t
);
328 static const unsigned char swizzle3
[] = {
329 2, LP_BLD_SWIZZLE_DONTCARE
,
330 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
332 rho_r
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle3
);
333 rho
= lp_build_max(coord_bld
, rho
, rho_r
);
337 rho
= lp_build_pack_aos_scalars(bld
->gallivm
, coord_bld
->type
,
338 perquadf_bld
->type
, rho
, 0);
342 rho_vec
= LLVMBuildExtractElement(builder
, rho_vec
, index0
, "");
344 rho_vec
= lp_build_mul(float_size_bld
, rho_vec
, float_size
);
351 LLVMValueRef rho_s
, rho_t
, rho_r
;
353 rho_s
= LLVMBuildExtractElement(builder
, rho_vec
, index0
, "");
354 rho_t
= LLVMBuildExtractElement(builder
, rho_vec
, index1
, "");
356 rho
= lp_build_max(float_bld
, rho_s
, rho_t
);
359 rho_r
= LLVMBuildExtractElement(builder
, rho_vec
, index2
, "");
360 rho
= lp_build_max(float_bld
, rho
, rho_r
);
371 * Bri-linear lod computation
373 * Use a piece-wise linear approximation of log2 such that:
374 * - round to nearest, for values in the neighborhood of -1, 0, 1, 2, etc.
375 * - linear approximation for values in the neighborhood of 0.5, 1.5., etc,
376 * with the steepness specified in 'factor'
377 * - exact result for 0.5, 1.5, etc.
393 * This is a technique also commonly used in hardware:
394 * - http://ixbtlabs.com/articles2/gffx/nv40-rx800-3.html
396 * TODO: For correctness, this should only be applied when texture is known to
397 * have regular mipmaps, i.e., mipmaps derived from the base level.
399 * TODO: This could be done in fixed point, where applicable.
402 lp_build_brilinear_lod(struct lp_build_context
*bld
,
405 LLVMValueRef
*out_lod_ipart
,
406 LLVMValueRef
*out_lod_fpart
)
408 LLVMValueRef lod_fpart
;
409 double pre_offset
= (factor
- 0.5)/factor
- 0.5;
410 double post_offset
= 1 - factor
;
413 lp_build_printf(bld
->gallivm
, "lod = %f\n", lod
);
416 lod
= lp_build_add(bld
, lod
,
417 lp_build_const_vec(bld
->gallivm
, bld
->type
, pre_offset
));
419 lp_build_ifloor_fract(bld
, lod
, out_lod_ipart
, &lod_fpart
);
421 lod_fpart
= lp_build_mul(bld
, lod_fpart
,
422 lp_build_const_vec(bld
->gallivm
, bld
->type
, factor
));
424 lod_fpart
= lp_build_add(bld
, lod_fpart
,
425 lp_build_const_vec(bld
->gallivm
, bld
->type
, post_offset
));
428 * It's not necessary to clamp lod_fpart since:
429 * - the above expression will never produce numbers greater than one.
430 * - the mip filtering branch is only taken if lod_fpart is positive
433 *out_lod_fpart
= lod_fpart
;
436 lp_build_printf(bld
->gallivm
, "lod_ipart = %i\n", *out_lod_ipart
);
437 lp_build_printf(bld
->gallivm
, "lod_fpart = %f\n\n", *out_lod_fpart
);
443 * Combined log2 and brilinear lod computation.
445 * It's in all identical to calling lp_build_fast_log2() and
446 * lp_build_brilinear_lod() above, but by combining we can compute the integer
447 * and fractional part independently.
450 lp_build_brilinear_rho(struct lp_build_context
*bld
,
453 LLVMValueRef
*out_lod_ipart
,
454 LLVMValueRef
*out_lod_fpart
)
456 LLVMValueRef lod_ipart
;
457 LLVMValueRef lod_fpart
;
459 const double pre_factor
= (2*factor
- 0.5)/(M_SQRT2
*factor
);
460 const double post_offset
= 1 - 2*factor
;
462 assert(bld
->type
.floating
);
464 assert(lp_check_value(bld
->type
, rho
));
467 * The pre factor will make the intersections with the exact powers of two
468 * happen precisely where we want then to be, which means that the integer
469 * part will not need any post adjustments.
471 rho
= lp_build_mul(bld
, rho
,
472 lp_build_const_vec(bld
->gallivm
, bld
->type
, pre_factor
));
474 /* ipart = ifloor(log2(rho)) */
475 lod_ipart
= lp_build_extract_exponent(bld
, rho
, 0);
477 /* fpart = rho / 2**ipart */
478 lod_fpart
= lp_build_extract_mantissa(bld
, rho
);
480 lod_fpart
= lp_build_mul(bld
, lod_fpart
,
481 lp_build_const_vec(bld
->gallivm
, bld
->type
, factor
));
483 lod_fpart
= lp_build_add(bld
, lod_fpart
,
484 lp_build_const_vec(bld
->gallivm
, bld
->type
, post_offset
));
487 * Like lp_build_brilinear_lod, it's not necessary to clamp lod_fpart since:
488 * - the above expression will never produce numbers greater than one.
489 * - the mip filtering branch is only taken if lod_fpart is positive
492 *out_lod_ipart
= lod_ipart
;
493 *out_lod_fpart
= lod_fpart
;
498 * Generate code to compute texture level of detail (lambda).
499 * \param derivs partial derivatives of (s, t, r, q) with respect to X and Y
500 * \param lod_bias optional float vector with the shader lod bias
501 * \param explicit_lod optional float vector with the explicit lod
502 * \param width scalar int texture width
503 * \param height scalar int texture height
504 * \param depth scalar int texture depth
506 * The resulting lod is scalar per quad, so only the first value per quad
507 * passed in from lod_bias, explicit_lod is used.
510 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
511 unsigned texture_unit
,
512 unsigned sampler_unit
,
513 const struct lp_derivatives
*derivs
,
514 LLVMValueRef lod_bias
, /* optional */
515 LLVMValueRef explicit_lod
, /* optional */
517 LLVMValueRef
*out_lod_ipart
,
518 LLVMValueRef
*out_lod_fpart
)
521 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
522 struct lp_build_context
*perquadf_bld
= &bld
->perquadf_bld
;
525 *out_lod_ipart
= bld
->perquadi_bld
.zero
;
526 *out_lod_fpart
= perquadf_bld
->zero
;
528 if (bld
->static_sampler_state
->min_max_lod_equal
) {
529 /* User is forcing sampling from a particular mipmap level.
530 * This is hit during mipmap generation.
532 LLVMValueRef min_lod
=
533 bld
->dynamic_state
->min_lod(bld
->dynamic_state
,
534 bld
->gallivm
, sampler_unit
);
536 lod
= lp_build_broadcast_scalar(perquadf_bld
, min_lod
);
540 lod
= lp_build_pack_aos_scalars(bld
->gallivm
, bld
->coord_bld
.type
,
541 perquadf_bld
->type
, explicit_lod
, 0);
546 rho
= lp_build_rho(bld
, texture_unit
, derivs
);
549 * Compute lod = log2(rho)
553 !bld
->static_sampler_state
->lod_bias_non_zero
&&
554 !bld
->static_sampler_state
->apply_max_lod
&&
555 !bld
->static_sampler_state
->apply_min_lod
) {
557 * Special case when there are no post-log2 adjustments, which
558 * saves instructions but keeping the integer and fractional lod
559 * computations separate from the start.
562 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
||
563 mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
564 *out_lod_ipart
= lp_build_ilog2(perquadf_bld
, rho
);
565 *out_lod_fpart
= perquadf_bld
->zero
;
568 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
&&
569 !(gallivm_debug
& GALLIVM_DEBUG_NO_BRILINEAR
)) {
570 lp_build_brilinear_rho(perquadf_bld
, rho
, BRILINEAR_FACTOR
,
571 out_lod_ipart
, out_lod_fpart
);
577 lod
= lp_build_log2(perquadf_bld
, rho
);
580 lod
= lp_build_fast_log2(perquadf_bld
, rho
);
583 /* add shader lod bias */
585 lod_bias
= lp_build_pack_aos_scalars(bld
->gallivm
, bld
->coord_bld
.type
,
586 perquadf_bld
->type
, lod_bias
, 0);
587 lod
= LLVMBuildFAdd(builder
, lod
, lod_bias
, "shader_lod_bias");
591 /* add sampler lod bias */
592 if (bld
->static_sampler_state
->lod_bias_non_zero
) {
593 LLVMValueRef sampler_lod_bias
=
594 bld
->dynamic_state
->lod_bias(bld
->dynamic_state
,
595 bld
->gallivm
, sampler_unit
);
596 sampler_lod_bias
= lp_build_broadcast_scalar(perquadf_bld
,
598 lod
= LLVMBuildFAdd(builder
, lod
, sampler_lod_bias
, "sampler_lod_bias");
602 if (bld
->static_sampler_state
->apply_max_lod
) {
603 LLVMValueRef max_lod
=
604 bld
->dynamic_state
->max_lod(bld
->dynamic_state
,
605 bld
->gallivm
, sampler_unit
);
606 max_lod
= lp_build_broadcast_scalar(perquadf_bld
, max_lod
);
608 lod
= lp_build_min(perquadf_bld
, lod
, max_lod
);
610 if (bld
->static_sampler_state
->apply_min_lod
) {
611 LLVMValueRef min_lod
=
612 bld
->dynamic_state
->min_lod(bld
->dynamic_state
,
613 bld
->gallivm
, sampler_unit
);
614 min_lod
= lp_build_broadcast_scalar(perquadf_bld
, min_lod
);
616 lod
= lp_build_max(perquadf_bld
, lod
, min_lod
);
620 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
621 if (!(gallivm_debug
& GALLIVM_DEBUG_NO_BRILINEAR
)) {
622 lp_build_brilinear_lod(perquadf_bld
, lod
, BRILINEAR_FACTOR
,
623 out_lod_ipart
, out_lod_fpart
);
626 lp_build_ifloor_fract(perquadf_bld
, lod
, out_lod_ipart
, out_lod_fpart
);
629 lp_build_name(*out_lod_fpart
, "lod_fpart");
632 *out_lod_ipart
= lp_build_iround(perquadf_bld
, lod
);
635 lp_build_name(*out_lod_ipart
, "lod_ipart");
642 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
643 * mipmap level index.
644 * Note: this is all scalar per quad code.
645 * \param lod_ipart int texture level of detail
646 * \param level_out returns integer
649 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
650 unsigned texture_unit
,
651 LLVMValueRef lod_ipart
,
652 LLVMValueRef
*level_out
)
654 struct lp_build_context
*perquadi_bld
= &bld
->perquadi_bld
;
655 LLVMValueRef first_level
, last_level
, level
;
657 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
658 bld
->gallivm
, texture_unit
);
659 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
660 bld
->gallivm
, texture_unit
);
661 first_level
= lp_build_broadcast_scalar(perquadi_bld
, first_level
);
662 last_level
= lp_build_broadcast_scalar(perquadi_bld
, last_level
);
664 level
= lp_build_add(perquadi_bld
, lod_ipart
, first_level
);
666 /* clamp level to legal range of levels */
667 *level_out
= lp_build_clamp(perquadi_bld
, level
, first_level
, last_level
);
672 * For PIPE_TEX_MIPFILTER_LINEAR, convert per-quad int LOD(s) to two (per-quad)
673 * (adjacent) mipmap level indexes, and fix up float lod part accordingly.
674 * Later, we'll sample from those two mipmap levels and interpolate between them.
677 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
678 unsigned texture_unit
,
679 LLVMValueRef lod_ipart
,
680 LLVMValueRef
*lod_fpart_inout
,
681 LLVMValueRef
*level0_out
,
682 LLVMValueRef
*level1_out
)
684 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
685 struct lp_build_context
*perquadi_bld
= &bld
->perquadi_bld
;
686 struct lp_build_context
*perquadf_bld
= &bld
->perquadf_bld
;
687 LLVMValueRef first_level
, last_level
;
688 LLVMValueRef clamp_min
;
689 LLVMValueRef clamp_max
;
691 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
692 bld
->gallivm
, texture_unit
);
693 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
694 bld
->gallivm
, texture_unit
);
695 first_level
= lp_build_broadcast_scalar(perquadi_bld
, first_level
);
696 last_level
= lp_build_broadcast_scalar(perquadi_bld
, last_level
);
698 *level0_out
= lp_build_add(perquadi_bld
, lod_ipart
, first_level
);
699 *level1_out
= lp_build_add(perquadi_bld
, *level0_out
, perquadi_bld
->one
);
702 * Clamp both *level0_out and *level1_out to [first_level, last_level], with
703 * the minimum number of comparisons, and zeroing lod_fpart in the extreme
704 * ends in the process.
708 * This code (vector select in particular) only works with llvm 3.1
709 * (if there's more than one quad, with x86 backend). Might consider
710 * converting to our lp_bld_logic helpers.
712 #if HAVE_LLVM < 0x0301
713 assert(perquadi_bld
->type
.length
== 1);
716 /* *level0_out < first_level */
717 clamp_min
= LLVMBuildICmp(builder
, LLVMIntSLT
,
718 *level0_out
, first_level
,
719 "clamp_lod_to_first");
721 *level0_out
= LLVMBuildSelect(builder
, clamp_min
,
722 first_level
, *level0_out
, "");
724 *level1_out
= LLVMBuildSelect(builder
, clamp_min
,
725 first_level
, *level1_out
, "");
727 *lod_fpart_inout
= LLVMBuildSelect(builder
, clamp_min
,
728 perquadf_bld
->zero
, *lod_fpart_inout
, "");
730 /* *level0_out >= last_level */
731 clamp_max
= LLVMBuildICmp(builder
, LLVMIntSGE
,
732 *level0_out
, last_level
,
733 "clamp_lod_to_last");
735 *level0_out
= LLVMBuildSelect(builder
, clamp_max
,
736 last_level
, *level0_out
, "");
738 *level1_out
= LLVMBuildSelect(builder
, clamp_max
,
739 last_level
, *level1_out
, "");
741 *lod_fpart_inout
= LLVMBuildSelect(builder
, clamp_max
,
742 perquadf_bld
->zero
, *lod_fpart_inout
, "");
744 lp_build_name(*level0_out
, "texture%u_miplevel0", texture_unit
);
745 lp_build_name(*level1_out
, "texture%u_miplevel1", texture_unit
);
746 lp_build_name(*lod_fpart_inout
, "texture%u_mipweight", texture_unit
);
751 * Return pointer to a single mipmap level.
752 * \param level integer mipmap level
755 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
758 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
759 LLVMValueRef indexes
[2], data_ptr
, mip_offset
;
761 indexes
[0] = lp_build_const_int32(bld
->gallivm
, 0);
763 mip_offset
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
764 mip_offset
= LLVMBuildLoad(builder
, mip_offset
, "");
765 data_ptr
= LLVMBuildGEP(builder
, bld
->base_ptr
, &mip_offset
, 1, "");
770 * Return (per-pixel) offsets to mip levels.
771 * \param level integer mipmap level
774 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
777 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
778 LLVMValueRef indexes
[2], offsets
, offset1
;
780 indexes
[0] = lp_build_const_int32(bld
->gallivm
, 0);
781 if (bld
->num_lods
== 1) {
783 offset1
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
784 offset1
= LLVMBuildLoad(builder
, offset1
, "");
785 offsets
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, offset1
);
787 else if (bld
->num_lods
== bld
->coord_bld
.type
.length
/ 4) {
790 offsets
= bld
->int_coord_bld
.undef
;
791 for (i
= 0; i
< bld
->num_lods
; i
++) {
792 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
793 LLVMValueRef indexo
= lp_build_const_int32(bld
->gallivm
, 4 * i
);
794 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
795 offset1
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
796 offset1
= LLVMBuildLoad(builder
, offset1
, "");
797 offsets
= LLVMBuildInsertElement(builder
, offsets
, offset1
, indexo
, "");
799 offsets
= lp_build_swizzle_scalar_aos(&bld
->int_coord_bld
, offsets
, 0, 4);
804 assert (bld
->num_lods
== bld
->coord_bld
.type
.length
);
806 offsets
= bld
->int_coord_bld
.undef
;
807 for (i
= 0; i
< bld
->num_lods
; i
++) {
808 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
809 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
810 offset1
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
811 offset1
= LLVMBuildLoad(builder
, offset1
, "");
812 offsets
= LLVMBuildInsertElement(builder
, offsets
, offset1
, indexi
, "");
820 * Codegen equivalent for u_minify().
821 * Return max(1, base_size >> level);
824 lp_build_minify(struct lp_build_context
*bld
,
825 LLVMValueRef base_size
,
828 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
829 assert(lp_check_value(bld
->type
, base_size
));
830 assert(lp_check_value(bld
->type
, level
));
832 if (level
== bld
->zero
) {
833 /* if we're using mipmap level zero, no minification is needed */
838 LLVMBuildLShr(builder
, base_size
, level
, "minify");
839 assert(bld
->type
.sign
);
840 size
= lp_build_max(bld
, size
, bld
->one
);
847 * Dereference stride_array[mipmap_level] array to get a stride.
848 * Return stride as a vector.
851 lp_build_get_level_stride_vec(struct lp_build_sample_context
*bld
,
852 LLVMValueRef stride_array
, LLVMValueRef level
)
854 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
855 LLVMValueRef indexes
[2], stride
, stride1
;
856 indexes
[0] = lp_build_const_int32(bld
->gallivm
, 0);
857 if (bld
->num_lods
== 1) {
859 stride1
= LLVMBuildGEP(builder
, stride_array
, indexes
, 2, "");
860 stride1
= LLVMBuildLoad(builder
, stride1
, "");
861 stride
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, stride1
);
863 else if (bld
->num_lods
== bld
->coord_bld
.type
.length
/ 4) {
864 LLVMValueRef stride1
;
867 stride
= bld
->int_coord_bld
.undef
;
868 for (i
= 0; i
< bld
->num_lods
; i
++) {
869 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
870 LLVMValueRef indexo
= lp_build_const_int32(bld
->gallivm
, i
);
871 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
872 stride1
= LLVMBuildGEP(builder
, stride_array
, indexes
, 2, "");
873 stride1
= LLVMBuildLoad(builder
, stride1
, "");
874 stride
= LLVMBuildInsertElement(builder
, stride
, stride1
, indexo
, "");
876 stride
= lp_build_swizzle_scalar_aos(&bld
->int_coord_bld
, stride
, 0, 4);
879 LLVMValueRef stride1
;
882 assert (bld
->num_lods
== bld
->coord_bld
.type
.length
);
884 stride
= bld
->int_coord_bld
.undef
;
885 for (i
= 0; i
< bld
->coord_bld
.type
.length
; i
++) {
886 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
887 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
888 stride1
= LLVMBuildGEP(builder
, stride_array
, indexes
, 2, "");
889 stride1
= LLVMBuildLoad(builder
, stride1
, "");
890 stride
= LLVMBuildInsertElement(builder
, stride
, stride1
, indexi
, "");
898 * When sampling a mipmap, we need to compute the width, height, depth
899 * of the source levels from the level indexes. This helper function
903 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
905 LLVMValueRef
*out_size
,
906 LLVMValueRef
*row_stride_vec
,
907 LLVMValueRef
*img_stride_vec
)
909 const unsigned dims
= bld
->dims
;
910 LLVMValueRef ilevel_vec
;
913 * Compute width, height, depth at mipmap level 'ilevel'
915 if (bld
->num_lods
== 1) {
916 ilevel_vec
= lp_build_broadcast_scalar(&bld
->int_size_bld
, ilevel
);
917 *out_size
= lp_build_minify(&bld
->int_size_bld
, bld
->int_size
, ilevel_vec
);
920 LLVMValueRef int_size_vec
;
921 LLVMValueRef tmp
[LP_MAX_VECTOR_LENGTH
];
922 unsigned num_quads
= bld
->coord_bld
.type
.length
/ 4;
925 if (bld
->num_lods
== num_quads
) {
927 * XXX: this should be #ifndef SANE_INSTRUCTION_SET.
928 * intel "forgot" the variable shift count instruction until avx2.
929 * A harmless 8x32 shift gets translated into 32 instructions
930 * (16 extracts, 8 scalar shifts, 8 inserts), llvm is apparently
931 * unable to recognize if there are really just 2 different shift
932 * count values. So do the shift 4-wide before expansion.
934 struct lp_build_context bld4
;
935 struct lp_type type4
;
937 type4
= bld
->int_coord_bld
.type
;
940 lp_build_context_init(&bld4
, bld
->gallivm
, type4
);
942 if (bld
->dims
== 1) {
943 assert(bld
->int_size_in_bld
.type
.length
== 1);
944 int_size_vec
= lp_build_broadcast_scalar(&bld4
,
948 assert(bld
->int_size_in_bld
.type
.length
== 4);
949 int_size_vec
= bld
->int_size
;
952 for (i
= 0; i
< num_quads
; i
++) {
953 LLVMValueRef ileveli
;
954 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
956 ileveli
= lp_build_extract_broadcast(bld
->gallivm
,
957 bld
->perquadi_bld
.type
,
961 tmp
[i
] = lp_build_minify(&bld4
, int_size_vec
, ileveli
);
964 * out_size is [w0, h0, d0, _, w1, h1, d1, _, ...] vector for dims > 1,
965 * [w0, w0, w0, w0, w1, w1, w1, w1, ...] otherwise.
967 *out_size
= lp_build_concat(bld
->gallivm
,
973 /* FIXME: this is terrible and results in _huge_ vector
974 * (for the dims > 1 case).
975 * Should refactor this (together with extract_image_sizes) and do
976 * something more useful. Could for instance if we have width,height
977 * with 4-wide vector pack all elements into a 8xi16 vector
978 * (on which we can still do useful math) instead of using a 16xi32
980 * FIXME: some callers can't handle this yet.
981 * For dims == 1 this will create [w0, w1, w2, w3, ...] vector.
982 * For dims > 1 this will create [w0, h0, d0, _, w1, h1, d1, _, ...] vector.
984 assert(bld
->num_lods
== bld
->coord_bld
.type
.length
);
985 if (bld
->dims
== 1) {
986 assert(bld
->int_size_bld
.type
.length
== 1);
987 int_size_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
,
989 /* vector shift with variable shift count alert... */
990 *out_size
= lp_build_minify(&bld
->int_coord_bld
, int_size_vec
, ilevel
);
993 LLVMValueRef ilevel1
;
994 for (i
= 0; i
< bld
->num_lods
; i
++) {
995 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
996 ilevel1
= lp_build_extract_broadcast(bld
->gallivm
, bld
->int_coord_type
,
997 bld
->int_size_in_bld
.type
, ilevel
, indexi
);
998 tmp
[i
] = bld
->int_size
;
999 tmp
[i
] = lp_build_minify(&bld
->int_size_in_bld
, tmp
[i
], ilevel1
);
1001 int_size_vec
= lp_build_concat(bld
->gallivm
,
1003 bld
->int_size_in_bld
.type
,
1010 *row_stride_vec
= lp_build_get_level_stride_vec(bld
,
1011 bld
->row_stride_array
,
1015 bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE
||
1016 bld
->static_texture_state
->target
== PIPE_TEXTURE_1D_ARRAY
||
1017 bld
->static_texture_state
->target
== PIPE_TEXTURE_2D_ARRAY
) {
1018 *img_stride_vec
= lp_build_get_level_stride_vec(bld
,
1019 bld
->img_stride_array
,
1026 * Extract and broadcast texture size.
1028 * @param size_type type of the texture size vector (either
1029 * bld->int_size_type or bld->float_size_type)
1030 * @param coord_type type of the texture size vector (either
1031 * bld->int_coord_type or bld->coord_type)
1032 * @param size vector with the texture size (width, height, depth)
1035 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
1036 struct lp_build_context
*size_bld
,
1037 struct lp_type coord_type
,
1039 LLVMValueRef
*out_width
,
1040 LLVMValueRef
*out_height
,
1041 LLVMValueRef
*out_depth
)
1043 const unsigned dims
= bld
->dims
;
1044 LLVMTypeRef i32t
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
1045 struct lp_type size_type
= size_bld
->type
;
1047 if (bld
->num_lods
== 1) {
1048 *out_width
= lp_build_extract_broadcast(bld
->gallivm
,
1052 LLVMConstInt(i32t
, 0, 0));
1054 *out_height
= lp_build_extract_broadcast(bld
->gallivm
,
1058 LLVMConstInt(i32t
, 1, 0));
1060 *out_depth
= lp_build_extract_broadcast(bld
->gallivm
,
1064 LLVMConstInt(i32t
, 2, 0));
1069 unsigned num_quads
= bld
->coord_bld
.type
.length
/ 4;
1074 else if (bld
->num_lods
== num_quads
) {
1075 *out_width
= lp_build_swizzle_scalar_aos(size_bld
, size
, 0, 4);
1077 *out_height
= lp_build_swizzle_scalar_aos(size_bld
, size
, 1, 4);
1079 *out_depth
= lp_build_swizzle_scalar_aos(size_bld
, size
, 2, 4);
1084 assert(bld
->num_lods
== bld
->coord_type
.length
);
1085 *out_width
= lp_build_pack_aos_scalars(bld
->gallivm
, size_type
,
1086 coord_type
, size
, 0);
1088 *out_width
= lp_build_pack_aos_scalars(bld
->gallivm
, size_type
,
1089 coord_type
, size
, 1);
1091 *out_width
= lp_build_pack_aos_scalars(bld
->gallivm
, size_type
,
1092 coord_type
, size
, 2);
1101 * Unnormalize coords.
1103 * @param flt_size vector with the integer texture size (width, height, depth)
1106 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
1107 LLVMValueRef flt_size
,
1112 const unsigned dims
= bld
->dims
;
1114 LLVMValueRef height
;
1117 lp_build_extract_image_sizes(bld
,
1118 &bld
->float_size_bld
,
1125 /* s = s * width, t = t * height */
1126 *s
= lp_build_mul(&bld
->coord_bld
, *s
, width
);
1128 *t
= lp_build_mul(&bld
->coord_bld
, *t
, height
);
1130 *r
= lp_build_mul(&bld
->coord_bld
, *r
, depth
);
1136 /** Helper used by lp_build_cube_lookup() */
1138 lp_build_cube_imapos(struct lp_build_context
*coord_bld
, LLVMValueRef coord
)
1140 /* ima = +0.5 / abs(coord); */
1141 LLVMValueRef posHalf
= lp_build_const_vec(coord_bld
->gallivm
, coord_bld
->type
, 0.5);
1142 LLVMValueRef absCoord
= lp_build_abs(coord_bld
, coord
);
1143 LLVMValueRef ima
= lp_build_div(coord_bld
, posHalf
, absCoord
);
1147 /** Helper used by lp_build_cube_lookup() */
1149 lp_build_cube_imaneg(struct lp_build_context
*coord_bld
, LLVMValueRef coord
)
1151 /* ima = -0.5 / abs(coord); */
1152 LLVMValueRef negHalf
= lp_build_const_vec(coord_bld
->gallivm
, coord_bld
->type
, -0.5);
1153 LLVMValueRef absCoord
= lp_build_abs(coord_bld
, coord
);
1154 LLVMValueRef ima
= lp_build_div(coord_bld
, negHalf
, absCoord
);
1159 * Helper used by lp_build_cube_lookup()
1160 * FIXME: the sign here can also be 0.
1161 * Arithmetically this could definitely make a difference. Either
1162 * fix the comment or use other (simpler) sign function, not sure
1163 * which one it should be.
1164 * \param sign scalar +1 or -1
1165 * \param coord float vector
1166 * \param ima float vector
1169 lp_build_cube_coord(struct lp_build_context
*coord_bld
,
1170 LLVMValueRef sign
, int negate_coord
,
1171 LLVMValueRef coord
, LLVMValueRef ima
)
1173 /* return negate(coord) * ima * sign + 0.5; */
1174 LLVMValueRef half
= lp_build_const_vec(coord_bld
->gallivm
, coord_bld
->type
, 0.5);
1177 assert(negate_coord
== +1 || negate_coord
== -1);
1179 if (negate_coord
== -1) {
1180 coord
= lp_build_negate(coord_bld
, coord
);
1183 res
= lp_build_mul(coord_bld
, coord
, ima
);
1185 sign
= lp_build_broadcast_scalar(coord_bld
, sign
);
1186 res
= lp_build_mul(coord_bld
, res
, sign
);
1188 res
= lp_build_add(coord_bld
, res
, half
);
1194 /** Helper used by lp_build_cube_lookup()
1195 * Return (major_coord >= 0) ? pos_face : neg_face;
1198 lp_build_cube_face(struct lp_build_sample_context
*bld
,
1199 LLVMValueRef major_coord
,
1200 unsigned pos_face
, unsigned neg_face
)
1202 struct gallivm_state
*gallivm
= bld
->gallivm
;
1203 LLVMBuilderRef builder
= gallivm
->builder
;
1204 LLVMValueRef cmp
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
1206 bld
->float_bld
.zero
, "");
1207 LLVMValueRef pos
= lp_build_const_int32(gallivm
, pos_face
);
1208 LLVMValueRef neg
= lp_build_const_int32(gallivm
, neg_face
);
1209 LLVMValueRef res
= LLVMBuildSelect(builder
, cmp
, pos
, neg
, "");
1216 * Generate code to do cube face selection and compute per-face texcoords.
1219 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
1224 LLVMValueRef
*face_s
,
1225 LLVMValueRef
*face_t
)
1227 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
1228 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1229 struct gallivm_state
*gallivm
= bld
->gallivm
;
1230 LLVMValueRef rx
, ry
, rz
;
1231 LLVMValueRef tmp
[4], rxyz
, arxyz
;
1234 * Use the average of the four pixel's texcoords to choose the face.
1235 * Slight simplification just calculate the sum, skip scaling.
1240 rxyz
= lp_build_hadd_partial4(&bld
->coord_bld
, tmp
, 3);
1241 arxyz
= lp_build_abs(&bld
->coord_bld
, rxyz
);
1243 if (coord_bld
->type
.length
> 4) {
1244 struct lp_build_context
*cint_bld
= &bld
->int_coord_bld
;
1245 struct lp_type intctype
= cint_bld
->type
;
1246 LLVMValueRef signrxs
, signrys
, signrzs
, signrxyz
, sign
;
1247 LLVMValueRef arxs
, arys
, arzs
;
1248 LLVMValueRef arx_ge_ary
, maxarxsarys
, arz_ge_arx_ary
;
1249 LLVMValueRef snewx
, tnewx
, snewy
, tnewy
, snewz
, tnewz
;
1250 LLVMValueRef ryneg
, rzneg
;
1251 LLVMValueRef ma
, ima
;
1252 LLVMValueRef posHalf
= lp_build_const_vec(gallivm
, coord_bld
->type
, 0.5);
1253 LLVMValueRef signmask
= lp_build_const_int_vec(gallivm
, intctype
,
1254 1 << (intctype
.width
- 1));
1255 LLVMValueRef signshift
= lp_build_const_int_vec(gallivm
, intctype
,
1257 LLVMValueRef facex
= lp_build_const_int_vec(gallivm
, intctype
, PIPE_TEX_FACE_POS_X
);
1258 LLVMValueRef facey
= lp_build_const_int_vec(gallivm
, intctype
, PIPE_TEX_FACE_POS_Y
);
1259 LLVMValueRef facez
= lp_build_const_int_vec(gallivm
, intctype
, PIPE_TEX_FACE_POS_Z
);
1261 assert(PIPE_TEX_FACE_NEG_X
== PIPE_TEX_FACE_POS_X
+ 1);
1262 assert(PIPE_TEX_FACE_NEG_Y
== PIPE_TEX_FACE_POS_Y
+ 1);
1263 assert(PIPE_TEX_FACE_NEG_Z
== PIPE_TEX_FACE_POS_Z
+ 1);
1265 rx
= LLVMBuildBitCast(builder
, s
, lp_build_vec_type(gallivm
, intctype
), "");
1266 ry
= LLVMBuildBitCast(builder
, t
, lp_build_vec_type(gallivm
, intctype
), "");
1267 rz
= LLVMBuildBitCast(builder
, r
, lp_build_vec_type(gallivm
, intctype
), "");
1268 ryneg
= LLVMBuildXor(builder
, ry
, signmask
, "");
1269 rzneg
= LLVMBuildXor(builder
, rz
, signmask
, "");
1271 /* the sign bit comes from the averaged vector (per quad),
1272 * as does the decision which face to use */
1273 signrxyz
= LLVMBuildBitCast(builder
, rxyz
, lp_build_vec_type(gallivm
, intctype
), "");
1274 signrxyz
= LLVMBuildAnd(builder
, signrxyz
, signmask
, "");
1276 arxs
= lp_build_swizzle_scalar_aos(coord_bld
, arxyz
, 0, 4);
1277 arys
= lp_build_swizzle_scalar_aos(coord_bld
, arxyz
, 1, 4);
1278 arzs
= lp_build_swizzle_scalar_aos(coord_bld
, arxyz
, 2, 4);
1281 * select x if x >= y else select y
1282 * select previous result if y >= max(x,y) else select z
1284 arx_ge_ary
= lp_build_cmp(coord_bld
, PIPE_FUNC_GEQUAL
, arxs
, arys
);
1285 maxarxsarys
= lp_build_max(coord_bld
, arxs
, arys
);
1286 arz_ge_arx_ary
= lp_build_cmp(coord_bld
, PIPE_FUNC_GEQUAL
, maxarxsarys
, arzs
);
1289 * compute all possible new s/t coords
1290 * snewx = signrx * -rz;
1293 * tnewy = signry * rz;
1294 * snewz = signrz * rx;
1297 signrxs
= lp_build_swizzle_scalar_aos(cint_bld
, signrxyz
, 0, 4);
1298 snewx
= LLVMBuildXor(builder
, signrxs
, rzneg
, "");
1301 signrys
= lp_build_swizzle_scalar_aos(cint_bld
, signrxyz
, 1, 4);
1303 tnewy
= LLVMBuildXor(builder
, signrys
, rz
, "");
1305 signrzs
= lp_build_swizzle_scalar_aos(cint_bld
, signrxyz
, 2, 4);
1306 snewz
= LLVMBuildXor(builder
, signrzs
, rx
, "");
1309 /* XXX on x86 unclear if we should cast the values back to float
1310 * or not - on some cpus (nehalem) pblendvb has twice the throughput
1311 * of blendvps though on others there just might be domain
1312 * transition penalties when using it (this depends on what llvm
1313 * will chose for the bit ops above so there appears no "right way",
1314 * but given the boatload of selects let's just use the int type).
1316 * Unfortunately we also need the sign bit of the summed coords.
1318 *face_s
= lp_build_select(cint_bld
, arx_ge_ary
, snewx
, snewy
);
1319 *face_t
= lp_build_select(cint_bld
, arx_ge_ary
, tnewx
, tnewy
);
1320 ma
= lp_build_select(coord_bld
, arx_ge_ary
, s
, t
);
1321 *face
= lp_build_select(cint_bld
, arx_ge_ary
, facex
, facey
);
1322 sign
= lp_build_select(cint_bld
, arx_ge_ary
, signrxs
, signrys
);
1324 *face_s
= lp_build_select(cint_bld
, arz_ge_arx_ary
, *face_s
, snewz
);
1325 *face_t
= lp_build_select(cint_bld
, arz_ge_arx_ary
, *face_t
, tnewz
);
1326 ma
= lp_build_select(coord_bld
, arz_ge_arx_ary
, ma
, r
);
1327 *face
= lp_build_select(cint_bld
, arz_ge_arx_ary
, *face
, facez
);
1328 sign
= lp_build_select(cint_bld
, arz_ge_arx_ary
, sign
, signrzs
);
1330 *face_s
= LLVMBuildBitCast(builder
, *face_s
,
1331 lp_build_vec_type(gallivm
, coord_bld
->type
), "");
1332 *face_t
= LLVMBuildBitCast(builder
, *face_t
,
1333 lp_build_vec_type(gallivm
, coord_bld
->type
), "");
1335 /* add +1 for neg face */
1336 /* XXX with AVX probably want to use another select here -
1337 * as long as we ensure vblendvps gets used we can actually
1338 * skip the comparison and just use sign as a "mask" directly.
1340 sign
= LLVMBuildLShr(builder
, sign
, signshift
, "");
1341 *face
= LLVMBuildOr(builder
, *face
, sign
, "face");
1343 ima
= lp_build_cube_imapos(coord_bld
, ma
);
1345 *face_s
= lp_build_mul(coord_bld
, *face_s
, ima
);
1346 *face_s
= lp_build_add(coord_bld
, *face_s
, posHalf
);
1347 *face_t
= lp_build_mul(coord_bld
, *face_t
, ima
);
1348 *face_t
= lp_build_add(coord_bld
, *face_t
, posHalf
);
1352 struct lp_build_if_state if_ctx
;
1353 LLVMValueRef face_s_var
;
1354 LLVMValueRef face_t_var
;
1355 LLVMValueRef face_var
;
1356 LLVMValueRef arx_ge_ary_arz
, ary_ge_arx_arz
;
1357 LLVMValueRef shuffles
[4];
1358 LLVMValueRef arxy_ge_aryx
, arxy_ge_arzz
, arxy_ge_arxy_arzz
;
1359 LLVMValueRef arxyxy
, aryxzz
, arxyxy_ge_aryxzz
;
1360 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1362 assert(bld
->coord_bld
.type
.length
== 4);
1364 shuffles
[0] = lp_build_const_int32(gallivm
, 0);
1365 shuffles
[1] = lp_build_const_int32(gallivm
, 1);
1366 shuffles
[2] = lp_build_const_int32(gallivm
, 0);
1367 shuffles
[3] = lp_build_const_int32(gallivm
, 1);
1368 arxyxy
= LLVMBuildShuffleVector(builder
, arxyz
, arxyz
, LLVMConstVector(shuffles
, 4), "");
1369 shuffles
[0] = lp_build_const_int32(gallivm
, 1);
1370 shuffles
[1] = lp_build_const_int32(gallivm
, 0);
1371 shuffles
[2] = lp_build_const_int32(gallivm
, 2);
1372 shuffles
[3] = lp_build_const_int32(gallivm
, 2);
1373 aryxzz
= LLVMBuildShuffleVector(builder
, arxyz
, arxyz
, LLVMConstVector(shuffles
, 4), "");
1374 arxyxy_ge_aryxzz
= lp_build_cmp(&bld
->coord_bld
, PIPE_FUNC_GEQUAL
, arxyxy
, aryxzz
);
1376 shuffles
[0] = lp_build_const_int32(gallivm
, 0);
1377 shuffles
[1] = lp_build_const_int32(gallivm
, 1);
1378 arxy_ge_aryx
= LLVMBuildShuffleVector(builder
, arxyxy_ge_aryxzz
, arxyxy_ge_aryxzz
,
1379 LLVMConstVector(shuffles
, 2), "");
1380 shuffles
[0] = lp_build_const_int32(gallivm
, 2);
1381 shuffles
[1] = lp_build_const_int32(gallivm
, 3);
1382 arxy_ge_arzz
= LLVMBuildShuffleVector(builder
, arxyxy_ge_aryxzz
, arxyxy_ge_aryxzz
,
1383 LLVMConstVector(shuffles
, 2), "");
1384 arxy_ge_arxy_arzz
= LLVMBuildAnd(builder
, arxy_ge_aryx
, arxy_ge_arzz
, "");
1386 arx_ge_ary_arz
= LLVMBuildExtractElement(builder
, arxy_ge_arxy_arzz
,
1387 lp_build_const_int32(gallivm
, 0), "");
1388 arx_ge_ary_arz
= LLVMBuildICmp(builder
, LLVMIntNE
, arx_ge_ary_arz
,
1389 lp_build_const_int32(gallivm
, 0), "");
1390 ary_ge_arx_arz
= LLVMBuildExtractElement(builder
, arxy_ge_arxy_arzz
,
1391 lp_build_const_int32(gallivm
, 1), "");
1392 ary_ge_arx_arz
= LLVMBuildICmp(builder
, LLVMIntNE
, ary_ge_arx_arz
,
1393 lp_build_const_int32(gallivm
, 0), "");
1394 face_s_var
= lp_build_alloca(gallivm
, bld
->coord_bld
.vec_type
, "face_s_var");
1395 face_t_var
= lp_build_alloca(gallivm
, bld
->coord_bld
.vec_type
, "face_t_var");
1396 face_var
= lp_build_alloca(gallivm
, bld
->int_bld
.vec_type
, "face_var");
1398 lp_build_if(&if_ctx
, gallivm
, arx_ge_ary_arz
);
1401 LLVMValueRef sign
, ima
;
1402 rx
= LLVMBuildExtractElement(builder
, rxyz
,
1403 lp_build_const_int32(gallivm
, 0), "");
1405 sign
= lp_build_sgn(float_bld
, rx
);
1406 ima
= lp_build_cube_imaneg(coord_bld
, s
);
1407 *face_s
= lp_build_cube_coord(coord_bld
, sign
, +1, r
, ima
);
1408 *face_t
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1409 *face
= lp_build_cube_face(bld
, rx
,
1410 PIPE_TEX_FACE_POS_X
,
1411 PIPE_TEX_FACE_NEG_X
);
1412 LLVMBuildStore(builder
, *face_s
, face_s_var
);
1413 LLVMBuildStore(builder
, *face_t
, face_t_var
);
1414 LLVMBuildStore(builder
, *face
, face_var
);
1416 lp_build_else(&if_ctx
);
1418 struct lp_build_if_state if_ctx2
;
1420 lp_build_if(&if_ctx2
, gallivm
, ary_ge_arx_arz
);
1422 LLVMValueRef sign
, ima
;
1424 ry
= LLVMBuildExtractElement(builder
, rxyz
,
1425 lp_build_const_int32(gallivm
, 1), "");
1426 sign
= lp_build_sgn(float_bld
, ry
);
1427 ima
= lp_build_cube_imaneg(coord_bld
, t
);
1428 *face_s
= lp_build_cube_coord(coord_bld
, NULL
, -1, s
, ima
);
1429 *face_t
= lp_build_cube_coord(coord_bld
, sign
, -1, r
, ima
);
1430 *face
= lp_build_cube_face(bld
, ry
,
1431 PIPE_TEX_FACE_POS_Y
,
1432 PIPE_TEX_FACE_NEG_Y
);
1433 LLVMBuildStore(builder
, *face_s
, face_s_var
);
1434 LLVMBuildStore(builder
, *face_t
, face_t_var
);
1435 LLVMBuildStore(builder
, *face
, face_var
);
1437 lp_build_else(&if_ctx2
);
1440 LLVMValueRef sign
, ima
;
1441 rz
= LLVMBuildExtractElement(builder
, rxyz
,
1442 lp_build_const_int32(gallivm
, 2), "");
1443 sign
= lp_build_sgn(float_bld
, rz
);
1444 ima
= lp_build_cube_imaneg(coord_bld
, r
);
1445 *face_s
= lp_build_cube_coord(coord_bld
, sign
, -1, s
, ima
);
1446 *face_t
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1447 *face
= lp_build_cube_face(bld
, rz
,
1448 PIPE_TEX_FACE_POS_Z
,
1449 PIPE_TEX_FACE_NEG_Z
);
1450 LLVMBuildStore(builder
, *face_s
, face_s_var
);
1451 LLVMBuildStore(builder
, *face_t
, face_t_var
);
1452 LLVMBuildStore(builder
, *face
, face_var
);
1454 lp_build_endif(&if_ctx2
);
1457 lp_build_endif(&if_ctx
);
1459 *face_s
= LLVMBuildLoad(builder
, face_s_var
, "face_s");
1460 *face_t
= LLVMBuildLoad(builder
, face_t_var
, "face_t");
1461 *face
= LLVMBuildLoad(builder
, face_var
, "face");
1462 *face
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, *face
);
1468 * Compute the partial offset of a pixel block along an arbitrary axis.
1470 * @param coord coordinate in pixels
1471 * @param stride number of bytes between rows of successive pixel blocks
1472 * @param block_length number of pixels in a pixels block along the coordinate
1474 * @param out_offset resulting relative offset of the pixel block in bytes
1475 * @param out_subcoord resulting sub-block pixel coordinate
1478 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
1479 unsigned block_length
,
1481 LLVMValueRef stride
,
1482 LLVMValueRef
*out_offset
,
1483 LLVMValueRef
*out_subcoord
)
1485 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1486 LLVMValueRef offset
;
1487 LLVMValueRef subcoord
;
1489 if (block_length
== 1) {
1490 subcoord
= bld
->zero
;
1494 * Pixel blocks have power of two dimensions. LLVM should convert the
1495 * rem/div to bit arithmetic.
1496 * TODO: Verify this.
1497 * It does indeed BUT it does transform it to scalar (and back) when doing so
1498 * (using roughly extract, shift/and, mov, unpack) (llvm 2.7).
1499 * The generated code looks seriously unfunny and is quite expensive.
1502 LLVMValueRef block_width
= lp_build_const_int_vec(bld
->type
, block_length
);
1503 subcoord
= LLVMBuildURem(builder
, coord
, block_width
, "");
1504 coord
= LLVMBuildUDiv(builder
, coord
, block_width
, "");
1506 unsigned logbase2
= util_logbase2(block_length
);
1507 LLVMValueRef block_shift
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, logbase2
);
1508 LLVMValueRef block_mask
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, block_length
- 1);
1509 subcoord
= LLVMBuildAnd(builder
, coord
, block_mask
, "");
1510 coord
= LLVMBuildLShr(builder
, coord
, block_shift
, "");
1514 offset
= lp_build_mul(bld
, coord
, stride
);
1517 assert(out_subcoord
);
1519 *out_offset
= offset
;
1520 *out_subcoord
= subcoord
;
1525 * Compute the offset of a pixel block.
1527 * x, y, z, y_stride, z_stride are vectors, and they refer to pixels.
1529 * Returns the relative offset and i,j sub-block coordinates
1532 lp_build_sample_offset(struct lp_build_context
*bld
,
1533 const struct util_format_description
*format_desc
,
1537 LLVMValueRef y_stride
,
1538 LLVMValueRef z_stride
,
1539 LLVMValueRef
*out_offset
,
1540 LLVMValueRef
*out_i
,
1541 LLVMValueRef
*out_j
)
1543 LLVMValueRef x_stride
;
1544 LLVMValueRef offset
;
1546 x_stride
= lp_build_const_vec(bld
->gallivm
, bld
->type
,
1547 format_desc
->block
.bits
/8);
1549 lp_build_sample_partial_offset(bld
,
1550 format_desc
->block
.width
,
1554 if (y
&& y_stride
) {
1555 LLVMValueRef y_offset
;
1556 lp_build_sample_partial_offset(bld
,
1557 format_desc
->block
.height
,
1560 offset
= lp_build_add(bld
, offset
, y_offset
);
1566 if (z
&& z_stride
) {
1567 LLVMValueRef z_offset
;
1569 lp_build_sample_partial_offset(bld
,
1570 1, /* pixel blocks are always 2D */
1573 offset
= lp_build_add(bld
, offset
, z_offset
);
1576 *out_offset
= offset
;