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"
49 #include "lp_bld_quad.h"
50 #include "lp_bld_bitarit.h"
54 * Bri-linear factor. Should be greater than one.
56 #define BRILINEAR_FACTOR 2
59 * Does the given texture wrap mode allow sampling the texture border color?
60 * XXX maybe move this into gallium util code.
63 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
64 unsigned min_img_filter
,
65 unsigned mag_img_filter
)
68 case PIPE_TEX_WRAP_REPEAT
:
69 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
70 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
71 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
73 case PIPE_TEX_WRAP_CLAMP
:
74 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
75 if (min_img_filter
== PIPE_TEX_FILTER_NEAREST
&&
76 mag_img_filter
== PIPE_TEX_FILTER_NEAREST
) {
81 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
82 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
85 assert(0 && "unexpected wrap mode");
92 * Initialize lp_sampler_static_texture_state object with the gallium
93 * texture/sampler_view state (this contains the parts which are
97 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
98 const struct pipe_sampler_view
*view
)
100 const struct pipe_resource
*texture
;
102 memset(state
, 0, sizeof *state
);
104 if (!view
|| !view
->texture
)
107 texture
= view
->texture
;
109 state
->format
= view
->format
;
110 state
->swizzle_r
= view
->swizzle_r
;
111 state
->swizzle_g
= view
->swizzle_g
;
112 state
->swizzle_b
= view
->swizzle_b
;
113 state
->swizzle_a
= view
->swizzle_a
;
115 state
->target
= texture
->target
;
116 state
->pot_width
= util_is_power_of_two(texture
->width0
);
117 state
->pot_height
= util_is_power_of_two(texture
->height0
);
118 state
->pot_depth
= util_is_power_of_two(texture
->depth0
);
119 state
->level_zero_only
= !view
->u
.tex
.last_level
;
122 * the layer / element / level parameters are all either dynamic
123 * state or handled transparently wrt execution.
129 * Initialize lp_sampler_static_sampler_state object with the gallium sampler
130 * state (this contains the parts which are considered static).
133 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
134 const struct pipe_sampler_state
*sampler
)
136 memset(state
, 0, sizeof *state
);
142 * We don't copy sampler state over unless it is actually enabled, to avoid
143 * spurious recompiles, as the sampler static state is part of the shader
146 * Ideally the state tracker or cso_cache module would make all state
147 * canonical, but until that happens it's better to be safe than sorry here.
149 * XXX: Actually there's much more than can be done here, especially
150 * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
153 state
->wrap_s
= sampler
->wrap_s
;
154 state
->wrap_t
= sampler
->wrap_t
;
155 state
->wrap_r
= sampler
->wrap_r
;
156 state
->min_img_filter
= sampler
->min_img_filter
;
157 state
->mag_img_filter
= sampler
->mag_img_filter
;
158 state
->seamless_cube_map
= sampler
->seamless_cube_map
;
160 if (sampler
->max_lod
> 0.0f
) {
161 state
->min_mip_filter
= sampler
->min_mip_filter
;
163 state
->min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
166 if (state
->min_mip_filter
!= PIPE_TEX_MIPFILTER_NONE
||
167 state
->min_img_filter
!= state
->mag_img_filter
) {
168 if (sampler
->lod_bias
!= 0.0f
) {
169 state
->lod_bias_non_zero
= 1;
172 /* If min_lod == max_lod we can greatly simplify mipmap selection.
173 * This is a case that occurs during automatic mipmap generation.
175 if (sampler
->min_lod
== sampler
->max_lod
) {
176 state
->min_max_lod_equal
= 1;
178 if (sampler
->min_lod
> 0.0f
) {
179 state
->apply_min_lod
= 1;
183 * XXX this won't do anything with the mesa state tracker which always
184 * sets max_lod to not more than actually present mip maps...
186 if (sampler
->max_lod
< (PIPE_MAX_TEXTURE_LEVELS
- 1)) {
187 state
->apply_max_lod
= 1;
192 state
->compare_mode
= sampler
->compare_mode
;
193 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
194 state
->compare_func
= sampler
->compare_func
;
197 state
->normalized_coords
= sampler
->normalized_coords
;
202 * Generate code to compute coordinate gradient (rho).
203 * \param derivs partial derivatives of (s, t, r, q) with respect to X and Y
205 * The resulting rho has bld->levelf format (per quad or per element).
208 lp_build_rho(struct lp_build_sample_context
*bld
,
209 unsigned texture_unit
,
213 LLVMValueRef cube_rho
,
214 const struct lp_derivatives
*derivs
)
216 struct gallivm_state
*gallivm
= bld
->gallivm
;
217 struct lp_build_context
*int_size_bld
= &bld
->int_size_in_bld
;
218 struct lp_build_context
*float_size_bld
= &bld
->float_size_in_bld
;
219 struct lp_build_context
*float_bld
= &bld
->float_bld
;
220 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
221 struct lp_build_context
*rho_bld
= &bld
->lodf_bld
;
222 const unsigned dims
= bld
->dims
;
223 LLVMValueRef ddx_ddy
[2];
224 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
225 LLVMTypeRef i32t
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
226 LLVMValueRef index0
= LLVMConstInt(i32t
, 0, 0);
227 LLVMValueRef index1
= LLVMConstInt(i32t
, 1, 0);
228 LLVMValueRef index2
= LLVMConstInt(i32t
, 2, 0);
229 LLVMValueRef rho_vec
;
230 LLVMValueRef int_size
, float_size
;
232 LLVMValueRef first_level
, first_level_vec
;
233 unsigned length
= coord_bld
->type
.length
;
234 unsigned num_quads
= length
/ 4;
235 boolean rho_per_quad
= rho_bld
->type
.length
!= length
;
236 boolean no_rho_opt
= (gallivm_debug
& GALLIVM_DEBUG_NO_RHO_APPROX
) && (dims
> 1);
238 LLVMValueRef i32undef
= LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
239 LLVMValueRef rho_xvec
, rho_yvec
;
241 /* Note that all simplified calculations will only work for isotropic filtering */
244 * rho calcs are always per quad except for explicit derivs (excluding
245 * the messy cube maps for now) when requested.
248 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
249 bld
->gallivm
, texture_unit
);
250 first_level_vec
= lp_build_broadcast_scalar(int_size_bld
, first_level
);
251 int_size
= lp_build_minify(int_size_bld
, bld
->int_size
, first_level_vec
);
252 float_size
= lp_build_int_to_float(float_size_bld
, int_size
);
255 LLVMValueRef cubesize
;
256 LLVMValueRef index0
= lp_build_const_int32(gallivm
, 0);
259 * Cube map code did already everything except size mul and per-quad extraction.
260 * Luckily cube maps are always quadratic!
263 rho
= lp_build_pack_aos_scalars(bld
->gallivm
, coord_bld
->type
,
264 rho_bld
->type
, cube_rho
, 0);
267 rho
= lp_build_swizzle_scalar_aos(coord_bld
, cube_rho
, 0, 4);
269 /* Could optimize this for single quad just skip the broadcast */
270 cubesize
= lp_build_extract_broadcast(gallivm
, bld
->float_size_in_type
,
271 rho_bld
->type
, float_size
, index0
);
273 /* skipping sqrt hence returning rho squared */
274 cubesize
= lp_build_mul(rho_bld
, cubesize
, cubesize
);
276 rho
= lp_build_mul(rho_bld
, cubesize
, rho
);
278 else if (derivs
&& !(bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE
)) {
279 LLVMValueRef ddmax
[3], ddx
[3], ddy
[3];
280 for (i
= 0; i
< dims
; i
++) {
281 LLVMValueRef floatdim
;
282 LLVMValueRef indexi
= lp_build_const_int32(gallivm
, i
);
284 floatdim
= lp_build_extract_broadcast(gallivm
, bld
->float_size_in_type
,
285 coord_bld
->type
, float_size
, indexi
);
288 * note that for rho_per_quad case could reduce math (at some shuffle
289 * cost), but for now use same code to per-pixel lod case.
292 ddx
[i
] = lp_build_mul(coord_bld
, floatdim
, derivs
->ddx
[i
]);
293 ddy
[i
] = lp_build_mul(coord_bld
, floatdim
, derivs
->ddy
[i
]);
294 ddx
[i
] = lp_build_mul(coord_bld
, ddx
[i
], ddx
[i
]);
295 ddy
[i
] = lp_build_mul(coord_bld
, ddy
[i
], ddy
[i
]);
298 LLVMValueRef tmpx
, tmpy
;
299 tmpx
= lp_build_abs(coord_bld
, derivs
->ddx
[i
]);
300 tmpy
= lp_build_abs(coord_bld
, derivs
->ddy
[i
]);
301 ddmax
[i
] = lp_build_max(coord_bld
, tmpx
, tmpy
);
302 ddmax
[i
] = lp_build_mul(coord_bld
, floatdim
, ddmax
[i
]);
306 rho_xvec
= lp_build_add(coord_bld
, ddx
[0], ddx
[1]);
307 rho_yvec
= lp_build_add(coord_bld
, ddy
[0], ddy
[1]);
309 rho_xvec
= lp_build_add(coord_bld
, rho_xvec
, ddx
[2]);
310 rho_yvec
= lp_build_add(coord_bld
, rho_yvec
, ddy
[2]);
312 rho
= lp_build_max(coord_bld
, rho_xvec
, rho_yvec
);
313 /* skipping sqrt hence returning rho squared */
318 rho
= lp_build_max(coord_bld
, rho
, ddmax
[1]);
320 rho
= lp_build_max(coord_bld
, rho
, ddmax
[2]);
326 * rho_vec contains per-pixel rho, convert to scalar per quad.
328 rho
= lp_build_pack_aos_scalars(bld
->gallivm
, coord_bld
->type
,
329 rho_bld
->type
, rho
, 0);
334 * This looks all a bit complex, but it's not that bad
335 * (the shuffle code makes it look worse than it is).
336 * Still, might not be ideal for all cases.
338 static const unsigned char swizzle0
[] = { /* no-op swizzle */
339 0, LP_BLD_SWIZZLE_DONTCARE
,
340 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
342 static const unsigned char swizzle1
[] = {
343 1, LP_BLD_SWIZZLE_DONTCARE
,
344 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
346 static const unsigned char swizzle2
[] = {
347 2, LP_BLD_SWIZZLE_DONTCARE
,
348 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
352 ddx_ddy
[0] = lp_build_packed_ddx_ddy_onecoord(coord_bld
, s
);
354 else if (dims
>= 2) {
355 ddx_ddy
[0] = lp_build_packed_ddx_ddy_twocoord(coord_bld
, s
, t
);
357 ddx_ddy
[1] = lp_build_packed_ddx_ddy_onecoord(coord_bld
, r
);
362 static const unsigned char swizzle01
[] = { /* no-op swizzle */
364 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
366 static const unsigned char swizzle23
[] = {
368 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
370 LLVMValueRef ddx_ddys
, ddx_ddyt
, floatdim
, shuffles
[LP_MAX_VECTOR_LENGTH
/ 4];
372 for (i
= 0; i
< num_quads
; i
++) {
373 shuffles
[i
*4+0] = shuffles
[i
*4+1] = index0
;
374 shuffles
[i
*4+2] = shuffles
[i
*4+3] = index1
;
376 floatdim
= LLVMBuildShuffleVector(builder
, float_size
, float_size
,
377 LLVMConstVector(shuffles
, length
), "");
378 ddx_ddy
[0] = lp_build_mul(coord_bld
, ddx_ddy
[0], floatdim
);
379 ddx_ddy
[0] = lp_build_mul(coord_bld
, ddx_ddy
[0], ddx_ddy
[0]);
380 ddx_ddys
= lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle01
);
381 ddx_ddyt
= lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle23
);
382 rho_vec
= lp_build_add(coord_bld
, ddx_ddys
, ddx_ddyt
);
385 static const unsigned char swizzle02
[] = {
387 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
389 floatdim
= lp_build_extract_broadcast(gallivm
, bld
->float_size_in_type
,
390 coord_bld
->type
, float_size
, index2
);
391 ddx_ddy
[1] = lp_build_mul(coord_bld
, ddx_ddy
[1], floatdim
);
392 ddx_ddy
[1] = lp_build_mul(coord_bld
, ddx_ddy
[1], ddx_ddy
[1]);
393 ddx_ddy
[1] = lp_build_swizzle_aos(coord_bld
, ddx_ddy
[1], swizzle02
);
394 rho_vec
= lp_build_add(coord_bld
, rho_vec
, ddx_ddy
[1]);
397 rho_xvec
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle0
);
398 rho_yvec
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle1
);
399 rho
= lp_build_max(coord_bld
, rho_xvec
, rho_yvec
);
402 rho
= lp_build_pack_aos_scalars(bld
->gallivm
, coord_bld
->type
,
403 rho_bld
->type
, rho
, 0);
406 rho
= lp_build_swizzle_scalar_aos(coord_bld
, rho
, 0, 4);
408 /* skipping sqrt hence returning rho squared */
411 ddx_ddy
[0] = lp_build_abs(coord_bld
, ddx_ddy
[0]);
413 ddx_ddy
[1] = lp_build_abs(coord_bld
, ddx_ddy
[1]);
417 rho_xvec
= lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle0
);
418 rho_yvec
= lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle2
);
420 else if (dims
== 2) {
421 static const unsigned char swizzle02
[] = {
423 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
425 static const unsigned char swizzle13
[] = {
427 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
429 rho_xvec
= lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle02
);
430 rho_yvec
= lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle13
);
433 LLVMValueRef shuffles1
[LP_MAX_VECTOR_LENGTH
];
434 LLVMValueRef shuffles2
[LP_MAX_VECTOR_LENGTH
];
436 for (i
= 0; i
< num_quads
; i
++) {
437 shuffles1
[4*i
+ 0] = lp_build_const_int32(gallivm
, 4*i
);
438 shuffles1
[4*i
+ 1] = lp_build_const_int32(gallivm
, 4*i
+ 2);
439 shuffles1
[4*i
+ 2] = lp_build_const_int32(gallivm
, length
+ 4*i
);
440 shuffles1
[4*i
+ 3] = i32undef
;
441 shuffles2
[4*i
+ 0] = lp_build_const_int32(gallivm
, 4*i
+ 1);
442 shuffles2
[4*i
+ 1] = lp_build_const_int32(gallivm
, 4*i
+ 3);
443 shuffles2
[4*i
+ 2] = lp_build_const_int32(gallivm
, length
+ 4*i
+ 2);
444 shuffles2
[4*i
+ 3] = i32undef
;
446 rho_xvec
= LLVMBuildShuffleVector(builder
, ddx_ddy
[0], ddx_ddy
[1],
447 LLVMConstVector(shuffles1
, length
), "");
448 rho_yvec
= LLVMBuildShuffleVector(builder
, ddx_ddy
[0], ddx_ddy
[1],
449 LLVMConstVector(shuffles2
, length
), "");
452 rho_vec
= lp_build_max(coord_bld
, rho_xvec
, rho_yvec
);
454 if (bld
->coord_type
.length
> 4) {
455 /* expand size to each quad */
457 /* could use some broadcast_vector helper for this? */
458 LLVMValueRef src
[LP_MAX_VECTOR_LENGTH
/4];
459 for (i
= 0; i
< num_quads
; i
++) {
462 float_size
= lp_build_concat(bld
->gallivm
, src
, float_size_bld
->type
, num_quads
);
465 float_size
= lp_build_broadcast_scalar(coord_bld
, float_size
);
467 rho_vec
= lp_build_mul(coord_bld
, rho_vec
, float_size
);
474 LLVMValueRef rho_s
, rho_t
, rho_r
;
476 rho_s
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle0
);
477 rho_t
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle1
);
479 rho
= lp_build_max(coord_bld
, rho_s
, rho_t
);
482 rho_r
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle2
);
483 rho
= lp_build_max(coord_bld
, rho
, rho_r
);
488 rho
= lp_build_pack_aos_scalars(bld
->gallivm
, coord_bld
->type
,
489 rho_bld
->type
, rho
, 0);
492 rho
= lp_build_swizzle_scalar_aos(coord_bld
, rho
, 0, 4);
497 rho_vec
= LLVMBuildExtractElement(builder
, rho_vec
, index0
, "");
499 rho_vec
= lp_build_mul(float_size_bld
, rho_vec
, float_size
);
506 LLVMValueRef rho_s
, rho_t
, rho_r
;
508 rho_s
= LLVMBuildExtractElement(builder
, rho_vec
, index0
, "");
509 rho_t
= LLVMBuildExtractElement(builder
, rho_vec
, index1
, "");
511 rho
= lp_build_max(float_bld
, rho_s
, rho_t
);
514 rho_r
= LLVMBuildExtractElement(builder
, rho_vec
, index2
, "");
515 rho
= lp_build_max(float_bld
, rho
, rho_r
);
520 rho
= lp_build_broadcast_scalar(rho_bld
, rho
);
531 * Bri-linear lod computation
533 * Use a piece-wise linear approximation of log2 such that:
534 * - round to nearest, for values in the neighborhood of -1, 0, 1, 2, etc.
535 * - linear approximation for values in the neighborhood of 0.5, 1.5., etc,
536 * with the steepness specified in 'factor'
537 * - exact result for 0.5, 1.5, etc.
553 * This is a technique also commonly used in hardware:
554 * - http://ixbtlabs.com/articles2/gffx/nv40-rx800-3.html
556 * TODO: For correctness, this should only be applied when texture is known to
557 * have regular mipmaps, i.e., mipmaps derived from the base level.
559 * TODO: This could be done in fixed point, where applicable.
562 lp_build_brilinear_lod(struct lp_build_context
*bld
,
565 LLVMValueRef
*out_lod_ipart
,
566 LLVMValueRef
*out_lod_fpart
)
568 LLVMValueRef lod_fpart
;
569 double pre_offset
= (factor
- 0.5)/factor
- 0.5;
570 double post_offset
= 1 - factor
;
573 lp_build_printf(bld
->gallivm
, "lod = %f\n", lod
);
576 lod
= lp_build_add(bld
, lod
,
577 lp_build_const_vec(bld
->gallivm
, bld
->type
, pre_offset
));
579 lp_build_ifloor_fract(bld
, lod
, out_lod_ipart
, &lod_fpart
);
581 lod_fpart
= lp_build_mul(bld
, lod_fpart
,
582 lp_build_const_vec(bld
->gallivm
, bld
->type
, factor
));
584 lod_fpart
= lp_build_add(bld
, lod_fpart
,
585 lp_build_const_vec(bld
->gallivm
, bld
->type
, post_offset
));
588 * It's not necessary to clamp lod_fpart since:
589 * - the above expression will never produce numbers greater than one.
590 * - the mip filtering branch is only taken if lod_fpart is positive
593 *out_lod_fpart
= lod_fpart
;
596 lp_build_printf(bld
->gallivm
, "lod_ipart = %i\n", *out_lod_ipart
);
597 lp_build_printf(bld
->gallivm
, "lod_fpart = %f\n\n", *out_lod_fpart
);
603 * Combined log2 and brilinear lod computation.
605 * It's in all identical to calling lp_build_fast_log2() and
606 * lp_build_brilinear_lod() above, but by combining we can compute the integer
607 * and fractional part independently.
610 lp_build_brilinear_rho(struct lp_build_context
*bld
,
613 LLVMValueRef
*out_lod_ipart
,
614 LLVMValueRef
*out_lod_fpart
)
616 LLVMValueRef lod_ipart
;
617 LLVMValueRef lod_fpart
;
619 const double pre_factor
= (2*factor
- 0.5)/(M_SQRT2
*factor
);
620 const double post_offset
= 1 - 2*factor
;
622 assert(bld
->type
.floating
);
624 assert(lp_check_value(bld
->type
, rho
));
627 * The pre factor will make the intersections with the exact powers of two
628 * happen precisely where we want them to be, which means that the integer
629 * part will not need any post adjustments.
631 rho
= lp_build_mul(bld
, rho
,
632 lp_build_const_vec(bld
->gallivm
, bld
->type
, pre_factor
));
634 /* ipart = ifloor(log2(rho)) */
635 lod_ipart
= lp_build_extract_exponent(bld
, rho
, 0);
637 /* fpart = rho / 2**ipart */
638 lod_fpart
= lp_build_extract_mantissa(bld
, rho
);
640 lod_fpart
= lp_build_mul(bld
, lod_fpart
,
641 lp_build_const_vec(bld
->gallivm
, bld
->type
, factor
));
643 lod_fpart
= lp_build_add(bld
, lod_fpart
,
644 lp_build_const_vec(bld
->gallivm
, bld
->type
, post_offset
));
647 * Like lp_build_brilinear_lod, it's not necessary to clamp lod_fpart since:
648 * - the above expression will never produce numbers greater than one.
649 * - the mip filtering branch is only taken if lod_fpart is positive
652 *out_lod_ipart
= lod_ipart
;
653 *out_lod_fpart
= lod_fpart
;
658 * Fast implementation of iround(log2(sqrt(x))), based on
659 * log2(x^n) == n*log2(x).
661 * Gives accurate results all the time.
662 * (Could be trivially extended to handle other power-of-two roots.)
665 lp_build_ilog2_sqrt(struct lp_build_context
*bld
,
668 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
670 struct lp_type i_type
= lp_int_type(bld
->type
);
671 LLVMValueRef one
= lp_build_const_int_vec(bld
->gallivm
, i_type
, 1);
673 assert(bld
->type
.floating
);
675 assert(lp_check_value(bld
->type
, x
));
677 /* ipart = log2(x) + 0.5 = 0.5*(log2(x^2) + 1.0) */
678 ipart
= lp_build_extract_exponent(bld
, x
, 1);
679 ipart
= LLVMBuildAShr(builder
, ipart
, one
, "");
686 * Generate code to compute texture level of detail (lambda).
687 * \param derivs partial derivatives of (s, t, r, q) with respect to X and Y
688 * \param lod_bias optional float vector with the shader lod bias
689 * \param explicit_lod optional float vector with the explicit lod
690 * \param cube_rho rho calculated by cube coord mapping (optional)
691 * \param out_lod_ipart integer part of lod
692 * \param out_lod_fpart float part of lod (never larger than 1 but may be negative)
693 * \param out_lod_positive (mask) if lod is positive (i.e. texture is minified)
695 * The resulting lod can be scalar per quad or be per element.
698 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
699 unsigned texture_unit
,
700 unsigned sampler_unit
,
704 LLVMValueRef cube_rho
,
705 const struct lp_derivatives
*derivs
,
706 LLVMValueRef lod_bias
, /* optional */
707 LLVMValueRef explicit_lod
, /* optional */
709 LLVMValueRef
*out_lod_ipart
,
710 LLVMValueRef
*out_lod_fpart
,
711 LLVMValueRef
*out_lod_positive
)
714 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
715 struct lp_build_context
*lodf_bld
= &bld
->lodf_bld
;
718 *out_lod_ipart
= bld
->lodi_bld
.zero
;
719 *out_lod_positive
= bld
->lodi_bld
.zero
;
720 *out_lod_fpart
= lodf_bld
->zero
;
723 * For determining min/mag, we follow GL 4.1 spec, 3.9.12 Texture Magnification:
724 * "Implementations may either unconditionally assume c = 0 for the minification
725 * vs. magnification switch-over point, or may choose to make c depend on the
726 * combination of minification and magnification modes as follows: if the
727 * magnification filter is given by LINEAR and the minification filter is given
728 * by NEAREST_MIPMAP_NEAREST or NEAREST_MIPMAP_LINEAR, then c = 0.5. This is
729 * done to ensure that a minified texture does not appear "sharper" than a
730 * magnified texture. Otherwise c = 0."
731 * And 3.9.11 Texture Minification:
732 * "If lod is less than or equal to the constant c (see section 3.9.12) the
733 * texture is said to be magnified; if it is greater, the texture is minified."
734 * So, using 0 as switchover point always, and using magnification for lod == 0.
735 * Note that the always c = 0 behavior is new (first appearing in GL 3.1 spec),
736 * old GL versions required 0.5 for the modes listed above.
737 * I have no clue about the (undocumented) wishes of d3d9/d3d10 here!
740 if (bld
->static_sampler_state
->min_max_lod_equal
) {
741 /* User is forcing sampling from a particular mipmap level.
742 * This is hit during mipmap generation.
744 LLVMValueRef min_lod
=
745 bld
->dynamic_state
->min_lod(bld
->dynamic_state
,
746 bld
->gallivm
, sampler_unit
);
748 lod
= lp_build_broadcast_scalar(lodf_bld
, min_lod
);
752 if (bld
->num_lods
!= bld
->coord_type
.length
)
753 lod
= lp_build_pack_aos_scalars(bld
->gallivm
, bld
->coord_bld
.type
,
754 lodf_bld
->type
, explicit_lod
, 0);
760 boolean rho_squared
= (gallivm_debug
& GALLIVM_DEBUG_NO_RHO_APPROX
) &&
763 rho
= lp_build_rho(bld
, texture_unit
, s
, t
, r
, cube_rho
, derivs
);
766 * Compute lod = log2(rho)
770 !bld
->static_sampler_state
->lod_bias_non_zero
&&
771 !bld
->static_sampler_state
->apply_max_lod
&&
772 !bld
->static_sampler_state
->apply_min_lod
) {
774 * Special case when there are no post-log2 adjustments, which
775 * saves instructions but keeping the integer and fractional lod
776 * computations separate from the start.
779 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
||
780 mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
782 * Don't actually need both values all the time, lod_ipart is
783 * needed for nearest mipfilter, lod_positive if min != mag.
786 *out_lod_ipart
= lp_build_ilog2_sqrt(lodf_bld
, rho
);
789 *out_lod_ipart
= lp_build_ilog2(lodf_bld
, rho
);
791 *out_lod_positive
= lp_build_cmp(lodf_bld
, PIPE_FUNC_GREATER
,
795 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
&&
796 !(gallivm_debug
& GALLIVM_DEBUG_NO_BRILINEAR
) &&
799 * This can't work if rho is squared. Not sure if it could be
800 * fixed while keeping it worthwile, could also do sqrt here
801 * but brilinear and no_rho_opt seems like a combination not
802 * making much sense anyway so just use ordinary path below.
804 lp_build_brilinear_rho(lodf_bld
, rho
, BRILINEAR_FACTOR
,
805 out_lod_ipart
, out_lod_fpart
);
806 *out_lod_positive
= lp_build_cmp(lodf_bld
, PIPE_FUNC_GREATER
,
813 lod
= lp_build_log2(lodf_bld
, rho
);
816 lod
= lp_build_fast_log2(lodf_bld
, rho
);
819 /* log2(x^2) == 0.5*log2(x) */
820 lod
= lp_build_mul(lodf_bld
, lod
,
821 lp_build_const_vec(bld
->gallivm
, lodf_bld
->type
, 0.5F
));
824 /* add shader lod bias */
826 if (bld
->num_lods
!= bld
->coord_type
.length
)
827 lod_bias
= lp_build_pack_aos_scalars(bld
->gallivm
, bld
->coord_bld
.type
,
828 lodf_bld
->type
, lod_bias
, 0);
829 lod
= LLVMBuildFAdd(builder
, lod
, lod_bias
, "shader_lod_bias");
833 /* add sampler lod bias */
834 if (bld
->static_sampler_state
->lod_bias_non_zero
) {
835 LLVMValueRef sampler_lod_bias
=
836 bld
->dynamic_state
->lod_bias(bld
->dynamic_state
,
837 bld
->gallivm
, sampler_unit
);
838 sampler_lod_bias
= lp_build_broadcast_scalar(lodf_bld
,
840 lod
= LLVMBuildFAdd(builder
, lod
, sampler_lod_bias
, "sampler_lod_bias");
844 if (bld
->static_sampler_state
->apply_max_lod
) {
845 LLVMValueRef max_lod
=
846 bld
->dynamic_state
->max_lod(bld
->dynamic_state
,
847 bld
->gallivm
, sampler_unit
);
848 max_lod
= lp_build_broadcast_scalar(lodf_bld
, max_lod
);
850 lod
= lp_build_min(lodf_bld
, lod
, max_lod
);
852 if (bld
->static_sampler_state
->apply_min_lod
) {
853 LLVMValueRef min_lod
=
854 bld
->dynamic_state
->min_lod(bld
->dynamic_state
,
855 bld
->gallivm
, sampler_unit
);
856 min_lod
= lp_build_broadcast_scalar(lodf_bld
, min_lod
);
858 lod
= lp_build_max(lodf_bld
, lod
, min_lod
);
862 *out_lod_positive
= lp_build_cmp(lodf_bld
, PIPE_FUNC_GREATER
,
863 lod
, lodf_bld
->zero
);
865 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
866 if (!(gallivm_debug
& GALLIVM_DEBUG_NO_BRILINEAR
)) {
867 lp_build_brilinear_lod(lodf_bld
, lod
, BRILINEAR_FACTOR
,
868 out_lod_ipart
, out_lod_fpart
);
871 lp_build_ifloor_fract(lodf_bld
, lod
, out_lod_ipart
, out_lod_fpart
);
874 lp_build_name(*out_lod_fpart
, "lod_fpart");
877 *out_lod_ipart
= lp_build_iround(lodf_bld
, lod
);
880 lp_build_name(*out_lod_ipart
, "lod_ipart");
887 * For PIPE_TEX_MIPFILTER_NEAREST, convert int part of lod
888 * to actual mip level.
889 * Note: this is all scalar per quad code.
890 * \param lod_ipart int texture level of detail
891 * \param level_out returns integer
892 * \param out_of_bounds returns per coord out_of_bounds mask if provided
895 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
896 unsigned texture_unit
,
897 LLVMValueRef lod_ipart
,
898 LLVMValueRef
*level_out
,
899 LLVMValueRef
*out_of_bounds
)
901 struct lp_build_context
*leveli_bld
= &bld
->leveli_bld
;
902 LLVMValueRef first_level
, last_level
, level
;
904 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
905 bld
->gallivm
, texture_unit
);
906 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
907 bld
->gallivm
, texture_unit
);
908 first_level
= lp_build_broadcast_scalar(leveli_bld
, first_level
);
909 last_level
= lp_build_broadcast_scalar(leveli_bld
, last_level
);
911 level
= lp_build_add(leveli_bld
, lod_ipart
, first_level
);
914 LLVMValueRef out
, out1
;
915 out
= lp_build_cmp(leveli_bld
, PIPE_FUNC_LESS
, level
, first_level
);
916 out1
= lp_build_cmp(leveli_bld
, PIPE_FUNC_GREATER
, level
, last_level
);
917 out
= lp_build_or(leveli_bld
, out
, out1
);
918 if (bld
->num_mips
== bld
->coord_bld
.type
.length
) {
919 *out_of_bounds
= out
;
921 else if (bld
->num_mips
== 1) {
922 *out_of_bounds
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, out
);
925 assert(bld
->num_mips
== bld
->coord_bld
.type
.length
/ 4);
926 *out_of_bounds
= lp_build_unpack_broadcast_aos_scalars(bld
->gallivm
,
928 bld
->int_coord_bld
.type
,
934 /* clamp level to legal range of levels */
935 *level_out
= lp_build_clamp(leveli_bld
, level
, first_level
, last_level
);
942 * For PIPE_TEX_MIPFILTER_LINEAR, convert per-quad (or per element) int LOD(s)
943 * to two (per-quad) (adjacent) mipmap level indexes, and fix up float lod
945 * Later, we'll sample from those two mipmap levels and interpolate between them.
948 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
949 unsigned texture_unit
,
950 LLVMValueRef lod_ipart
,
951 LLVMValueRef
*lod_fpart_inout
,
952 LLVMValueRef
*level0_out
,
953 LLVMValueRef
*level1_out
)
955 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
956 struct lp_build_context
*leveli_bld
= &bld
->leveli_bld
;
957 struct lp_build_context
*levelf_bld
= &bld
->levelf_bld
;
958 LLVMValueRef first_level
, last_level
;
959 LLVMValueRef clamp_min
;
960 LLVMValueRef clamp_max
;
962 assert(bld
->num_lods
== bld
->num_mips
);
964 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
965 bld
->gallivm
, texture_unit
);
966 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
967 bld
->gallivm
, texture_unit
);
968 first_level
= lp_build_broadcast_scalar(leveli_bld
, first_level
);
969 last_level
= lp_build_broadcast_scalar(leveli_bld
, last_level
);
971 *level0_out
= lp_build_add(leveli_bld
, lod_ipart
, first_level
);
972 *level1_out
= lp_build_add(leveli_bld
, *level0_out
, leveli_bld
->one
);
975 * Clamp both *level0_out and *level1_out to [first_level, last_level], with
976 * the minimum number of comparisons, and zeroing lod_fpart in the extreme
977 * ends in the process.
981 * This code (vector select in particular) only works with llvm 3.1
982 * (if there's more than one quad, with x86 backend). Might consider
983 * converting to our lp_bld_logic helpers.
985 #if HAVE_LLVM < 0x0301
986 assert(leveli_bld
->type
.length
== 1);
989 /* *level0_out < first_level */
990 clamp_min
= LLVMBuildICmp(builder
, LLVMIntSLT
,
991 *level0_out
, first_level
,
992 "clamp_lod_to_first");
994 *level0_out
= LLVMBuildSelect(builder
, clamp_min
,
995 first_level
, *level0_out
, "");
997 *level1_out
= LLVMBuildSelect(builder
, clamp_min
,
998 first_level
, *level1_out
, "");
1000 *lod_fpart_inout
= LLVMBuildSelect(builder
, clamp_min
,
1001 levelf_bld
->zero
, *lod_fpart_inout
, "");
1003 /* *level0_out >= last_level */
1004 clamp_max
= LLVMBuildICmp(builder
, LLVMIntSGE
,
1005 *level0_out
, last_level
,
1006 "clamp_lod_to_last");
1008 *level0_out
= LLVMBuildSelect(builder
, clamp_max
,
1009 last_level
, *level0_out
, "");
1011 *level1_out
= LLVMBuildSelect(builder
, clamp_max
,
1012 last_level
, *level1_out
, "");
1014 *lod_fpart_inout
= LLVMBuildSelect(builder
, clamp_max
,
1015 levelf_bld
->zero
, *lod_fpart_inout
, "");
1017 lp_build_name(*level0_out
, "texture%u_miplevel0", texture_unit
);
1018 lp_build_name(*level1_out
, "texture%u_miplevel1", texture_unit
);
1019 lp_build_name(*lod_fpart_inout
, "texture%u_mipweight", texture_unit
);
1024 * Return pointer to a single mipmap level.
1025 * \param level integer mipmap level
1028 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
1031 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1032 LLVMValueRef indexes
[2], data_ptr
, mip_offset
;
1034 indexes
[0] = lp_build_const_int32(bld
->gallivm
, 0);
1036 mip_offset
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
1037 mip_offset
= LLVMBuildLoad(builder
, mip_offset
, "");
1038 data_ptr
= LLVMBuildGEP(builder
, bld
->base_ptr
, &mip_offset
, 1, "");
1043 * Return (per-pixel) offsets to mip levels.
1044 * \param level integer mipmap level
1047 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
1050 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1051 LLVMValueRef indexes
[2], offsets
, offset1
;
1053 indexes
[0] = lp_build_const_int32(bld
->gallivm
, 0);
1054 if (bld
->num_mips
== 1) {
1056 offset1
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
1057 offset1
= LLVMBuildLoad(builder
, offset1
, "");
1058 offsets
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, offset1
);
1060 else if (bld
->num_mips
== bld
->coord_bld
.type
.length
/ 4) {
1063 offsets
= bld
->int_coord_bld
.undef
;
1064 for (i
= 0; i
< bld
->num_mips
; i
++) {
1065 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1066 LLVMValueRef indexo
= lp_build_const_int32(bld
->gallivm
, 4 * i
);
1067 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
1068 offset1
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
1069 offset1
= LLVMBuildLoad(builder
, offset1
, "");
1070 offsets
= LLVMBuildInsertElement(builder
, offsets
, offset1
, indexo
, "");
1072 offsets
= lp_build_swizzle_scalar_aos(&bld
->int_coord_bld
, offsets
, 0, 4);
1077 assert (bld
->num_mips
== bld
->coord_bld
.type
.length
);
1079 offsets
= bld
->int_coord_bld
.undef
;
1080 for (i
= 0; i
< bld
->num_mips
; i
++) {
1081 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1082 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
1083 offset1
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
1084 offset1
= LLVMBuildLoad(builder
, offset1
, "");
1085 offsets
= LLVMBuildInsertElement(builder
, offsets
, offset1
, indexi
, "");
1093 * Codegen equivalent for u_minify().
1094 * Return max(1, base_size >> level);
1097 lp_build_minify(struct lp_build_context
*bld
,
1098 LLVMValueRef base_size
,
1101 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1102 assert(lp_check_value(bld
->type
, base_size
));
1103 assert(lp_check_value(bld
->type
, level
));
1105 if (level
== bld
->zero
) {
1106 /* if we're using mipmap level zero, no minification is needed */
1111 LLVMBuildLShr(builder
, base_size
, level
, "minify");
1112 assert(bld
->type
.sign
);
1113 size
= lp_build_max(bld
, size
, bld
->one
);
1120 * Dereference stride_array[mipmap_level] array to get a stride.
1121 * Return stride as a vector.
1124 lp_build_get_level_stride_vec(struct lp_build_sample_context
*bld
,
1125 LLVMValueRef stride_array
, LLVMValueRef level
)
1127 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1128 LLVMValueRef indexes
[2], stride
, stride1
;
1129 indexes
[0] = lp_build_const_int32(bld
->gallivm
, 0);
1130 if (bld
->num_mips
== 1) {
1132 stride1
= LLVMBuildGEP(builder
, stride_array
, indexes
, 2, "");
1133 stride1
= LLVMBuildLoad(builder
, stride1
, "");
1134 stride
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, stride1
);
1136 else if (bld
->num_mips
== bld
->coord_bld
.type
.length
/ 4) {
1137 LLVMValueRef stride1
;
1140 stride
= bld
->int_coord_bld
.undef
;
1141 for (i
= 0; i
< bld
->num_mips
; i
++) {
1142 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1143 LLVMValueRef indexo
= lp_build_const_int32(bld
->gallivm
, 4 * i
);
1144 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
1145 stride1
= LLVMBuildGEP(builder
, stride_array
, indexes
, 2, "");
1146 stride1
= LLVMBuildLoad(builder
, stride1
, "");
1147 stride
= LLVMBuildInsertElement(builder
, stride
, stride1
, indexo
, "");
1149 stride
= lp_build_swizzle_scalar_aos(&bld
->int_coord_bld
, stride
, 0, 4);
1152 LLVMValueRef stride1
;
1155 assert (bld
->num_mips
== bld
->coord_bld
.type
.length
);
1157 stride
= bld
->int_coord_bld
.undef
;
1158 for (i
= 0; i
< bld
->coord_bld
.type
.length
; i
++) {
1159 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1160 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
1161 stride1
= LLVMBuildGEP(builder
, stride_array
, indexes
, 2, "");
1162 stride1
= LLVMBuildLoad(builder
, stride1
, "");
1163 stride
= LLVMBuildInsertElement(builder
, stride
, stride1
, indexi
, "");
1171 * When sampling a mipmap, we need to compute the width, height, depth
1172 * of the source levels from the level indexes. This helper function
1176 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
1177 LLVMValueRef ilevel
,
1178 LLVMValueRef
*out_size
,
1179 LLVMValueRef
*row_stride_vec
,
1180 LLVMValueRef
*img_stride_vec
)
1182 const unsigned dims
= bld
->dims
;
1183 LLVMValueRef ilevel_vec
;
1186 * Compute width, height, depth at mipmap level 'ilevel'
1188 if (bld
->num_mips
== 1) {
1189 ilevel_vec
= lp_build_broadcast_scalar(&bld
->int_size_bld
, ilevel
);
1190 *out_size
= lp_build_minify(&bld
->int_size_bld
, bld
->int_size
, ilevel_vec
);
1193 LLVMValueRef int_size_vec
;
1194 LLVMValueRef tmp
[LP_MAX_VECTOR_LENGTH
];
1195 unsigned num_quads
= bld
->coord_bld
.type
.length
/ 4;
1198 if (bld
->num_mips
== num_quads
) {
1200 * XXX: this should be #ifndef SANE_INSTRUCTION_SET.
1201 * intel "forgot" the variable shift count instruction until avx2.
1202 * A harmless 8x32 shift gets translated into 32 instructions
1203 * (16 extracts, 8 scalar shifts, 8 inserts), llvm is apparently
1204 * unable to recognize if there are really just 2 different shift
1205 * count values. So do the shift 4-wide before expansion.
1207 struct lp_build_context bld4
;
1208 struct lp_type type4
;
1210 type4
= bld
->int_coord_bld
.type
;
1213 lp_build_context_init(&bld4
, bld
->gallivm
, type4
);
1215 if (bld
->dims
== 1) {
1216 assert(bld
->int_size_in_bld
.type
.length
== 1);
1217 int_size_vec
= lp_build_broadcast_scalar(&bld4
,
1221 assert(bld
->int_size_in_bld
.type
.length
== 4);
1222 int_size_vec
= bld
->int_size
;
1225 for (i
= 0; i
< num_quads
; i
++) {
1226 LLVMValueRef ileveli
;
1227 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1229 ileveli
= lp_build_extract_broadcast(bld
->gallivm
,
1230 bld
->leveli_bld
.type
,
1234 tmp
[i
] = lp_build_minify(&bld4
, int_size_vec
, ileveli
);
1237 * out_size is [w0, h0, d0, _, w1, h1, d1, _, ...] vector for dims > 1,
1238 * [w0, w0, w0, w0, w1, w1, w1, w1, ...] otherwise.
1240 *out_size
= lp_build_concat(bld
->gallivm
,
1246 /* FIXME: this is terrible and results in _huge_ vector
1247 * (for the dims > 1 case).
1248 * Should refactor this (together with extract_image_sizes) and do
1249 * something more useful. Could for instance if we have width,height
1250 * with 4-wide vector pack all elements into a 8xi16 vector
1251 * (on which we can still do useful math) instead of using a 16xi32
1253 * FIXME: some callers can't handle this yet.
1254 * For dims == 1 this will create [w0, w1, w2, w3, ...] vector.
1255 * For dims > 1 this will create [w0, h0, d0, _, w1, h1, d1, _, ...] vector.
1257 assert(bld
->num_mips
== bld
->coord_bld
.type
.length
);
1258 if (bld
->dims
== 1) {
1259 assert(bld
->int_size_in_bld
.type
.length
== 1);
1260 int_size_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
,
1262 /* vector shift with variable shift count alert... */
1263 *out_size
= lp_build_minify(&bld
->int_coord_bld
, int_size_vec
, ilevel
);
1266 LLVMValueRef ilevel1
;
1267 for (i
= 0; i
< bld
->num_mips
; i
++) {
1268 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1269 ilevel1
= lp_build_extract_broadcast(bld
->gallivm
, bld
->int_coord_type
,
1270 bld
->int_size_in_bld
.type
, ilevel
, indexi
);
1271 tmp
[i
] = bld
->int_size
;
1272 tmp
[i
] = lp_build_minify(&bld
->int_size_in_bld
, tmp
[i
], ilevel1
);
1274 *out_size
= lp_build_concat(bld
->gallivm
, tmp
,
1275 bld
->int_size_in_bld
.type
,
1282 *row_stride_vec
= lp_build_get_level_stride_vec(bld
,
1283 bld
->row_stride_array
,
1287 bld
->static_texture_state
->target
== PIPE_TEXTURE_CUBE
||
1288 bld
->static_texture_state
->target
== PIPE_TEXTURE_1D_ARRAY
||
1289 bld
->static_texture_state
->target
== PIPE_TEXTURE_2D_ARRAY
) {
1290 *img_stride_vec
= lp_build_get_level_stride_vec(bld
,
1291 bld
->img_stride_array
,
1298 * Extract and broadcast texture size.
1300 * @param size_type type of the texture size vector (either
1301 * bld->int_size_type or bld->float_size_type)
1302 * @param coord_type type of the texture size vector (either
1303 * bld->int_coord_type or bld->coord_type)
1304 * @param size vector with the texture size (width, height, depth)
1307 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
1308 struct lp_build_context
*size_bld
,
1309 struct lp_type coord_type
,
1311 LLVMValueRef
*out_width
,
1312 LLVMValueRef
*out_height
,
1313 LLVMValueRef
*out_depth
)
1315 const unsigned dims
= bld
->dims
;
1316 LLVMTypeRef i32t
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
1317 struct lp_type size_type
= size_bld
->type
;
1319 if (bld
->num_mips
== 1) {
1320 *out_width
= lp_build_extract_broadcast(bld
->gallivm
,
1324 LLVMConstInt(i32t
, 0, 0));
1326 *out_height
= lp_build_extract_broadcast(bld
->gallivm
,
1330 LLVMConstInt(i32t
, 1, 0));
1332 *out_depth
= lp_build_extract_broadcast(bld
->gallivm
,
1336 LLVMConstInt(i32t
, 2, 0));
1341 unsigned num_quads
= bld
->coord_bld
.type
.length
/ 4;
1346 else if (bld
->num_mips
== num_quads
) {
1347 *out_width
= lp_build_swizzle_scalar_aos(size_bld
, size
, 0, 4);
1349 *out_height
= lp_build_swizzle_scalar_aos(size_bld
, size
, 1, 4);
1351 *out_depth
= lp_build_swizzle_scalar_aos(size_bld
, size
, 2, 4);
1356 assert(bld
->num_mips
== bld
->coord_type
.length
);
1357 *out_width
= lp_build_pack_aos_scalars(bld
->gallivm
, size_type
,
1358 coord_type
, size
, 0);
1360 *out_height
= lp_build_pack_aos_scalars(bld
->gallivm
, size_type
,
1361 coord_type
, size
, 1);
1363 *out_depth
= lp_build_pack_aos_scalars(bld
->gallivm
, size_type
,
1364 coord_type
, size
, 2);
1373 * Unnormalize coords.
1375 * @param flt_size vector with the integer texture size (width, height, depth)
1378 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
1379 LLVMValueRef flt_size
,
1384 const unsigned dims
= bld
->dims
;
1386 LLVMValueRef height
;
1389 lp_build_extract_image_sizes(bld
,
1390 &bld
->float_size_bld
,
1397 /* s = s * width, t = t * height */
1398 *s
= lp_build_mul(&bld
->coord_bld
, *s
, width
);
1400 *t
= lp_build_mul(&bld
->coord_bld
, *t
, height
);
1402 *r
= lp_build_mul(&bld
->coord_bld
, *r
, depth
);
1408 /** Helper used by lp_build_cube_lookup() */
1410 lp_build_cube_imapos(struct lp_build_context
*coord_bld
, LLVMValueRef coord
)
1412 /* ima = +0.5 / abs(coord); */
1413 LLVMValueRef posHalf
= lp_build_const_vec(coord_bld
->gallivm
, coord_bld
->type
, 0.5);
1414 LLVMValueRef absCoord
= lp_build_abs(coord_bld
, coord
);
1415 LLVMValueRef ima
= lp_build_div(coord_bld
, posHalf
, absCoord
);
1419 /** Helper used by lp_build_cube_lookup() */
1421 lp_build_cube_imaneg(struct lp_build_context
*coord_bld
, LLVMValueRef coord
)
1423 /* ima = -0.5 / abs(coord); */
1424 LLVMValueRef negHalf
= lp_build_const_vec(coord_bld
->gallivm
, coord_bld
->type
, -0.5);
1425 LLVMValueRef absCoord
= lp_build_abs(coord_bld
, coord
);
1426 LLVMValueRef ima
= lp_build_div(coord_bld
, negHalf
, absCoord
);
1431 * Helper used by lp_build_cube_lookup()
1432 * FIXME: the sign here can also be 0.
1433 * Arithmetically this could definitely make a difference. Either
1434 * fix the comment or use other (simpler) sign function, not sure
1435 * which one it should be.
1436 * \param sign scalar +1 or -1
1437 * \param coord float vector
1438 * \param ima float vector
1441 lp_build_cube_coord(struct lp_build_context
*coord_bld
,
1442 LLVMValueRef sign
, int negate_coord
,
1443 LLVMValueRef coord
, LLVMValueRef ima
)
1445 /* return negate(coord) * ima * sign + 0.5; */
1446 LLVMValueRef half
= lp_build_const_vec(coord_bld
->gallivm
, coord_bld
->type
, 0.5);
1449 assert(negate_coord
== +1 || negate_coord
== -1);
1451 if (negate_coord
== -1) {
1452 coord
= lp_build_negate(coord_bld
, coord
);
1455 res
= lp_build_mul(coord_bld
, coord
, ima
);
1457 sign
= lp_build_broadcast_scalar(coord_bld
, sign
);
1458 res
= lp_build_mul(coord_bld
, res
, sign
);
1460 res
= lp_build_add(coord_bld
, res
, half
);
1466 /** Helper used by lp_build_cube_lookup()
1467 * Return (major_coord >= 0) ? pos_face : neg_face;
1470 lp_build_cube_face(struct lp_build_sample_context
*bld
,
1471 LLVMValueRef major_coord
,
1472 unsigned pos_face
, unsigned neg_face
)
1474 struct gallivm_state
*gallivm
= bld
->gallivm
;
1475 LLVMBuilderRef builder
= gallivm
->builder
;
1476 LLVMValueRef cmp
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
1478 bld
->float_bld
.zero
, "");
1479 LLVMValueRef pos
= lp_build_const_int32(gallivm
, pos_face
);
1480 LLVMValueRef neg
= lp_build_const_int32(gallivm
, neg_face
);
1481 LLVMValueRef res
= LLVMBuildSelect(builder
, cmp
, pos
, neg
, "");
1488 * Generate code to do cube face selection and compute per-face texcoords.
1491 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
1492 LLVMValueRef
*coords
,
1493 const struct lp_derivatives
*derivs
, /* optional */
1495 boolean need_derivs
)
1497 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
1498 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1499 struct gallivm_state
*gallivm
= bld
->gallivm
;
1500 LLVMValueRef si
, ti
, ri
;
1502 if (1 || coord_bld
->type
.length
> 4) {
1504 * Do per-pixel face selection. We cannot however (as we used to do)
1505 * simply calculate the derivs afterwards (which is very bogus for
1506 * explicit derivs btw) because the values would be "random" when
1507 * not all pixels lie on the same face. So what we do here is just
1508 * calculate the derivatives after scaling the coords by the absolute
1509 * value of the inverse major axis, and essentially do rho calculation
1510 * steps as if it were a 3d texture. This is perfect if all pixels hit
1511 * the same face, but not so great at edges, I believe the max error
1512 * should be sqrt(2) with no_rho_approx or 2 otherwise (essentially measuring
1513 * the 3d distance between 2 points on the cube instead of measuring up/down
1514 * the edge). Still this is possibly a win over just selecting the same face
1515 * for all pixels. Unfortunately, something like that doesn't work for
1516 * explicit derivatives.
1517 * TODO: handle explicit derivatives by transforming them alongside coords
1520 struct lp_build_context
*cint_bld
= &bld
->int_coord_bld
;
1521 struct lp_type intctype
= cint_bld
->type
;
1522 LLVMValueRef signs
, signt
, signr
, signma
;
1523 LLVMValueRef as
, at
, ar
, face
, face_s
, face_t
;
1524 LLVMValueRef as_ge_at
, maxasat
, ar_ge_as_at
;
1525 LLVMValueRef snewx
, tnewx
, snewy
, tnewy
, snewz
, tnewz
;
1526 LLVMValueRef tnegi
, rnegi
;
1527 LLVMValueRef ma
, mai
, ima
;
1528 LLVMValueRef posHalf
= lp_build_const_vec(gallivm
, coord_bld
->type
, 0.5);
1529 LLVMValueRef signmask
= lp_build_const_int_vec(gallivm
, intctype
,
1530 1 << (intctype
.width
- 1));
1531 LLVMValueRef signshift
= lp_build_const_int_vec(gallivm
, intctype
,
1533 LLVMValueRef facex
= lp_build_const_int_vec(gallivm
, intctype
, PIPE_TEX_FACE_POS_X
);
1534 LLVMValueRef facey
= lp_build_const_int_vec(gallivm
, intctype
, PIPE_TEX_FACE_POS_Y
);
1535 LLVMValueRef facez
= lp_build_const_int_vec(gallivm
, intctype
, PIPE_TEX_FACE_POS_Z
);
1536 LLVMValueRef s
= coords
[0];
1537 LLVMValueRef t
= coords
[1];
1538 LLVMValueRef r
= coords
[2];
1540 assert(PIPE_TEX_FACE_NEG_X
== PIPE_TEX_FACE_POS_X
+ 1);
1541 assert(PIPE_TEX_FACE_NEG_Y
== PIPE_TEX_FACE_POS_Y
+ 1);
1542 assert(PIPE_TEX_FACE_NEG_Z
== PIPE_TEX_FACE_POS_Z
+ 1);
1545 * get absolute value (for x/y/z face selection) and sign bit
1546 * (for mirroring minor coords and pos/neg face selection)
1547 * of the original coords.
1549 as
= lp_build_abs(&bld
->coord_bld
, s
);
1550 at
= lp_build_abs(&bld
->coord_bld
, t
);
1551 ar
= lp_build_abs(&bld
->coord_bld
, r
);
1554 * major face determination: select x if x > y else select y
1555 * select z if z >= max(x,y) else select previous result
1556 * if some axis are the same we chose z over y, y over x - the
1557 * dx10 spec seems to ask for it while OpenGL doesn't care (if we
1558 * wouldn't care could save a select or two if using different
1559 * compares and doing at_g_as_ar last since tnewx and tnewz are the
1562 as_ge_at
= lp_build_cmp(coord_bld
, PIPE_FUNC_GREATER
, as
, at
);
1563 maxasat
= lp_build_max(coord_bld
, as
, at
);
1564 ar_ge_as_at
= lp_build_cmp(coord_bld
, PIPE_FUNC_GEQUAL
, ar
, maxasat
);
1567 LLVMValueRef ddx_ddy
[2], tmp
[3], rho_vec
;
1568 static const unsigned char swizzle0
[] = { /* no-op swizzle */
1569 0, LP_BLD_SWIZZLE_DONTCARE
,
1570 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1572 static const unsigned char swizzle1
[] = {
1573 1, LP_BLD_SWIZZLE_DONTCARE
,
1574 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1576 static const unsigned char swizzle01
[] = { /* no-op swizzle */
1578 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1580 static const unsigned char swizzle23
[] = {
1582 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1584 static const unsigned char swizzle02
[] = {
1586 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1590 * scale the s/t/r coords pre-select/mirror so we can calculate
1591 * "reasonable" derivs.
1593 ma
= lp_build_select(coord_bld
, as_ge_at
, s
, t
);
1594 ma
= lp_build_select(coord_bld
, ar_ge_as_at
, r
, ma
);
1595 ima
= lp_build_cube_imapos(coord_bld
, ma
);
1596 s
= lp_build_mul(coord_bld
, s
, ima
);
1597 t
= lp_build_mul(coord_bld
, t
, ima
);
1598 r
= lp_build_mul(coord_bld
, r
, ima
);
1601 * This isn't quite the same as the "ordinary" (3d deriv) path since we
1602 * know the texture is square which simplifies things (we can omit the
1603 * size mul which happens very early completely here and do it at the
1606 ddx_ddy
[0] = lp_build_packed_ddx_ddy_twocoord(coord_bld
, s
, t
);
1607 ddx_ddy
[1] = lp_build_packed_ddx_ddy_onecoord(coord_bld
, r
);
1609 if (gallivm_debug
& GALLIVM_DEBUG_NO_RHO_APPROX
) {
1610 ddx_ddy
[0] = lp_build_mul(coord_bld
, ddx_ddy
[0], ddx_ddy
[0]);
1611 ddx_ddy
[1] = lp_build_mul(coord_bld
, ddx_ddy
[1], ddx_ddy
[1]);
1614 ddx_ddy
[0] = lp_build_abs(coord_bld
, ddx_ddy
[0]);
1615 ddx_ddy
[1] = lp_build_abs(coord_bld
, ddx_ddy
[1]);
1618 tmp
[0] = lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle01
);
1619 tmp
[1] = lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle23
);
1620 tmp
[2] = lp_build_swizzle_aos(coord_bld
, ddx_ddy
[1], swizzle02
);
1622 if (gallivm_debug
& GALLIVM_DEBUG_NO_RHO_APPROX
) {
1623 rho_vec
= lp_build_add(coord_bld
, tmp
[0], tmp
[1]);
1624 rho_vec
= lp_build_add(coord_bld
, rho_vec
, tmp
[2]);
1627 rho_vec
= lp_build_max(coord_bld
, tmp
[0], tmp
[1]);
1628 rho_vec
= lp_build_max(coord_bld
, rho_vec
, tmp
[2]);
1631 tmp
[0] = lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle0
);
1632 tmp
[1] = lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle1
);
1633 *rho
= lp_build_max(coord_bld
, tmp
[0], tmp
[1]);
1636 si
= LLVMBuildBitCast(builder
, s
, lp_build_vec_type(gallivm
, intctype
), "");
1637 ti
= LLVMBuildBitCast(builder
, t
, lp_build_vec_type(gallivm
, intctype
), "");
1638 ri
= LLVMBuildBitCast(builder
, r
, lp_build_vec_type(gallivm
, intctype
), "");
1639 signs
= LLVMBuildAnd(builder
, si
, signmask
, "");
1640 signt
= LLVMBuildAnd(builder
, ti
, signmask
, "");
1641 signr
= LLVMBuildAnd(builder
, ri
, signmask
, "");
1644 * compute all possible new s/t coords
1645 * snewx = signs * -r;
1648 * tnewy = signt * r;
1649 * snewz = signr * s;
1652 tnegi
= LLVMBuildXor(builder
, ti
, signmask
, "");
1653 rnegi
= LLVMBuildXor(builder
, ri
, signmask
, "");
1655 snewx
= LLVMBuildXor(builder
, signs
, rnegi
, "");
1659 tnewy
= LLVMBuildXor(builder
, signt
, ri
, "");
1661 snewz
= LLVMBuildXor(builder
, signr
, si
, "");
1664 /* XXX on x86 unclear if we should cast the values back to float
1665 * or not - on some cpus (nehalem) pblendvb has twice the throughput
1666 * of blendvps though on others there just might be domain
1667 * transition penalties when using it (this depends on what llvm
1668 * will chose for the bit ops above so there appears no "right way",
1669 * but given the boatload of selects let's just use the int type).
1674 ma
= lp_build_select(coord_bld
, as_ge_at
, s
, t
);
1676 face_s
= lp_build_select(cint_bld
, as_ge_at
, snewx
, snewy
);
1677 face_t
= lp_build_select(cint_bld
, as_ge_at
, tnewx
, tnewy
);
1678 face
= lp_build_select(cint_bld
, as_ge_at
, facex
, facey
);
1681 ma
= lp_build_select(coord_bld
, ar_ge_as_at
, r
, ma
);
1683 face_s
= lp_build_select(cint_bld
, ar_ge_as_at
, snewz
, face_s
);
1684 face_t
= lp_build_select(cint_bld
, ar_ge_as_at
, tnewz
, face_t
);
1685 face
= lp_build_select(cint_bld
, ar_ge_as_at
, facez
, face
);
1687 face_s
= LLVMBuildBitCast(builder
, face_s
,
1688 lp_build_vec_type(gallivm
, coord_bld
->type
), "");
1689 face_t
= LLVMBuildBitCast(builder
, face_t
,
1690 lp_build_vec_type(gallivm
, coord_bld
->type
), "");
1692 /* add +1 for neg face */
1693 /* XXX with AVX probably want to use another select here -
1694 * as long as we ensure vblendvps gets used we can actually
1695 * skip the comparison and just use sign as a "mask" directly.
1697 mai
= LLVMBuildBitCast(builder
, ma
, lp_build_vec_type(gallivm
, intctype
), "");
1698 signma
= LLVMBuildLShr(builder
, mai
, signshift
, "");
1699 coords
[2] = LLVMBuildOr(builder
, face
, signma
, "face");
1701 /* project coords */
1703 ima
= lp_build_cube_imapos(coord_bld
, ma
);
1704 face_s
= lp_build_mul(coord_bld
, face_s
, ima
);
1705 face_t
= lp_build_mul(coord_bld
, face_t
, ima
);
1708 coords
[0] = lp_build_add(coord_bld
, face_s
, posHalf
);
1709 coords
[1] = lp_build_add(coord_bld
, face_t
, posHalf
);
1713 struct lp_build_if_state if_ctx
;
1714 LLVMValueRef face_s_var
;
1715 LLVMValueRef face_t_var
;
1716 LLVMValueRef face_var
;
1717 LLVMValueRef arx_ge_ary_arz
, ary_ge_arx_arz
;
1718 LLVMValueRef shuffles
[4];
1719 LLVMValueRef arxy_ge_aryx
, arxy_ge_arzz
, arxy_ge_arxy_arzz
;
1720 LLVMValueRef arxyxy
, aryxzz
, arxyxy_ge_aryxzz
;
1721 LLVMValueRef tmp
[4], rxyz
, arxyz
;
1722 struct lp_build_context
*float_bld
= &bld
->float_bld
;
1723 LLVMValueRef s
, t
, r
, face
, face_s
, face_t
;
1725 assert(bld
->coord_bld
.type
.length
== 4);
1727 tmp
[0] = s
= coords
[0];
1728 tmp
[1] = t
= coords
[1];
1729 tmp
[2] = r
= coords
[2];
1730 rxyz
= lp_build_hadd_partial4(&bld
->coord_bld
, tmp
, 3);
1731 arxyz
= lp_build_abs(&bld
->coord_bld
, rxyz
);
1733 shuffles
[0] = lp_build_const_int32(gallivm
, 0);
1734 shuffles
[1] = lp_build_const_int32(gallivm
, 1);
1735 shuffles
[2] = lp_build_const_int32(gallivm
, 0);
1736 shuffles
[3] = lp_build_const_int32(gallivm
, 1);
1737 arxyxy
= LLVMBuildShuffleVector(builder
, arxyz
, arxyz
, LLVMConstVector(shuffles
, 4), "");
1738 shuffles
[0] = lp_build_const_int32(gallivm
, 1);
1739 shuffles
[1] = lp_build_const_int32(gallivm
, 0);
1740 shuffles
[2] = lp_build_const_int32(gallivm
, 2);
1741 shuffles
[3] = lp_build_const_int32(gallivm
, 2);
1742 aryxzz
= LLVMBuildShuffleVector(builder
, arxyz
, arxyz
, LLVMConstVector(shuffles
, 4), "");
1743 arxyxy_ge_aryxzz
= lp_build_cmp(&bld
->coord_bld
, PIPE_FUNC_GEQUAL
, arxyxy
, aryxzz
);
1745 shuffles
[0] = lp_build_const_int32(gallivm
, 0);
1746 shuffles
[1] = lp_build_const_int32(gallivm
, 1);
1747 arxy_ge_aryx
= LLVMBuildShuffleVector(builder
, arxyxy_ge_aryxzz
, arxyxy_ge_aryxzz
,
1748 LLVMConstVector(shuffles
, 2), "");
1749 shuffles
[0] = lp_build_const_int32(gallivm
, 2);
1750 shuffles
[1] = lp_build_const_int32(gallivm
, 3);
1751 arxy_ge_arzz
= LLVMBuildShuffleVector(builder
, arxyxy_ge_aryxzz
, arxyxy_ge_aryxzz
,
1752 LLVMConstVector(shuffles
, 2), "");
1753 arxy_ge_arxy_arzz
= LLVMBuildAnd(builder
, arxy_ge_aryx
, arxy_ge_arzz
, "");
1755 arx_ge_ary_arz
= LLVMBuildExtractElement(builder
, arxy_ge_arxy_arzz
,
1756 lp_build_const_int32(gallivm
, 0), "");
1757 arx_ge_ary_arz
= LLVMBuildICmp(builder
, LLVMIntNE
, arx_ge_ary_arz
,
1758 lp_build_const_int32(gallivm
, 0), "");
1759 ary_ge_arx_arz
= LLVMBuildExtractElement(builder
, arxy_ge_arxy_arzz
,
1760 lp_build_const_int32(gallivm
, 1), "");
1761 ary_ge_arx_arz
= LLVMBuildICmp(builder
, LLVMIntNE
, ary_ge_arx_arz
,
1762 lp_build_const_int32(gallivm
, 0), "");
1763 face_s_var
= lp_build_alloca(gallivm
, bld
->coord_bld
.vec_type
, "face_s_var");
1764 face_t_var
= lp_build_alloca(gallivm
, bld
->coord_bld
.vec_type
, "face_t_var");
1765 face_var
= lp_build_alloca(gallivm
, bld
->int_bld
.vec_type
, "face_var");
1767 lp_build_if(&if_ctx
, gallivm
, arx_ge_ary_arz
);
1770 LLVMValueRef sign
, ima
;
1771 si
= LLVMBuildExtractElement(builder
, rxyz
,
1772 lp_build_const_int32(gallivm
, 0), "");
1774 sign
= lp_build_sgn(float_bld
, si
);
1775 ima
= lp_build_cube_imaneg(coord_bld
, s
);
1776 face_s
= lp_build_cube_coord(coord_bld
, sign
, +1, r
, ima
);
1777 face_t
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1778 face
= lp_build_cube_face(bld
, si
,
1779 PIPE_TEX_FACE_POS_X
,
1780 PIPE_TEX_FACE_NEG_X
);
1781 LLVMBuildStore(builder
, face_s
, face_s_var
);
1782 LLVMBuildStore(builder
, face_t
, face_t_var
);
1783 LLVMBuildStore(builder
, face
, face_var
);
1785 lp_build_else(&if_ctx
);
1787 struct lp_build_if_state if_ctx2
;
1789 lp_build_if(&if_ctx2
, gallivm
, ary_ge_arx_arz
);
1791 LLVMValueRef sign
, ima
;
1793 ti
= LLVMBuildExtractElement(builder
, rxyz
,
1794 lp_build_const_int32(gallivm
, 1), "");
1795 sign
= lp_build_sgn(float_bld
, ti
);
1796 ima
= lp_build_cube_imaneg(coord_bld
, t
);
1797 face_s
= lp_build_cube_coord(coord_bld
, NULL
, -1, s
, ima
);
1798 face_t
= lp_build_cube_coord(coord_bld
, sign
, -1, r
, ima
);
1799 face
= lp_build_cube_face(bld
, ti
,
1800 PIPE_TEX_FACE_POS_Y
,
1801 PIPE_TEX_FACE_NEG_Y
);
1802 LLVMBuildStore(builder
, face_s
, face_s_var
);
1803 LLVMBuildStore(builder
, face_t
, face_t_var
);
1804 LLVMBuildStore(builder
, face
, face_var
);
1806 lp_build_else(&if_ctx2
);
1809 LLVMValueRef sign
, ima
;
1810 ri
= LLVMBuildExtractElement(builder
, rxyz
,
1811 lp_build_const_int32(gallivm
, 2), "");
1812 sign
= lp_build_sgn(float_bld
, ri
);
1813 ima
= lp_build_cube_imaneg(coord_bld
, r
);
1814 face_s
= lp_build_cube_coord(coord_bld
, sign
, -1, s
, ima
);
1815 face_t
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1816 face
= lp_build_cube_face(bld
, ri
,
1817 PIPE_TEX_FACE_POS_Z
,
1818 PIPE_TEX_FACE_NEG_Z
);
1819 LLVMBuildStore(builder
, face_s
, face_s_var
);
1820 LLVMBuildStore(builder
, face_t
, face_t_var
);
1821 LLVMBuildStore(builder
, face
, face_var
);
1823 lp_build_endif(&if_ctx2
);
1826 lp_build_endif(&if_ctx
);
1828 coords
[0] = LLVMBuildLoad(builder
, face_s_var
, "face_s");
1829 coords
[1] = LLVMBuildLoad(builder
, face_t_var
, "face_t");
1830 face
= LLVMBuildLoad(builder
, face_var
, "face");
1831 coords
[2] = lp_build_broadcast_scalar(&bld
->int_coord_bld
, face
);
1837 * Compute the partial offset of a pixel block along an arbitrary axis.
1839 * @param coord coordinate in pixels
1840 * @param stride number of bytes between rows of successive pixel blocks
1841 * @param block_length number of pixels in a pixels block along the coordinate
1843 * @param out_offset resulting relative offset of the pixel block in bytes
1844 * @param out_subcoord resulting sub-block pixel coordinate
1847 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
1848 unsigned block_length
,
1850 LLVMValueRef stride
,
1851 LLVMValueRef
*out_offset
,
1852 LLVMValueRef
*out_subcoord
)
1854 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1855 LLVMValueRef offset
;
1856 LLVMValueRef subcoord
;
1858 if (block_length
== 1) {
1859 subcoord
= bld
->zero
;
1863 * Pixel blocks have power of two dimensions. LLVM should convert the
1864 * rem/div to bit arithmetic.
1865 * TODO: Verify this.
1866 * It does indeed BUT it does transform it to scalar (and back) when doing so
1867 * (using roughly extract, shift/and, mov, unpack) (llvm 2.7).
1868 * The generated code looks seriously unfunny and is quite expensive.
1871 LLVMValueRef block_width
= lp_build_const_int_vec(bld
->type
, block_length
);
1872 subcoord
= LLVMBuildURem(builder
, coord
, block_width
, "");
1873 coord
= LLVMBuildUDiv(builder
, coord
, block_width
, "");
1875 unsigned logbase2
= util_logbase2(block_length
);
1876 LLVMValueRef block_shift
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, logbase2
);
1877 LLVMValueRef block_mask
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, block_length
- 1);
1878 subcoord
= LLVMBuildAnd(builder
, coord
, block_mask
, "");
1879 coord
= LLVMBuildLShr(builder
, coord
, block_shift
, "");
1883 offset
= lp_build_mul(bld
, coord
, stride
);
1886 assert(out_subcoord
);
1888 *out_offset
= offset
;
1889 *out_subcoord
= subcoord
;
1894 * Compute the offset of a pixel block.
1896 * x, y, z, y_stride, z_stride are vectors, and they refer to pixels.
1898 * Returns the relative offset and i,j sub-block coordinates
1901 lp_build_sample_offset(struct lp_build_context
*bld
,
1902 const struct util_format_description
*format_desc
,
1906 LLVMValueRef y_stride
,
1907 LLVMValueRef z_stride
,
1908 LLVMValueRef
*out_offset
,
1909 LLVMValueRef
*out_i
,
1910 LLVMValueRef
*out_j
)
1912 LLVMValueRef x_stride
;
1913 LLVMValueRef offset
;
1915 x_stride
= lp_build_const_vec(bld
->gallivm
, bld
->type
,
1916 format_desc
->block
.bits
/8);
1918 lp_build_sample_partial_offset(bld
,
1919 format_desc
->block
.width
,
1923 if (y
&& y_stride
) {
1924 LLVMValueRef y_offset
;
1925 lp_build_sample_partial_offset(bld
,
1926 format_desc
->block
.height
,
1929 offset
= lp_build_add(bld
, offset
, y_offset
);
1935 if (z
&& z_stride
) {
1936 LLVMValueRef z_offset
;
1938 lp_build_sample_partial_offset(bld
,
1939 1, /* pixel blocks are always 2D */
1942 offset
= lp_build_add(bld
, offset
, z_offset
);
1945 *out_offset
= offset
;