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/format/u_format.h"
38 #include "util/u_math.h"
39 #include "util/u_cpu_detect.h"
40 #include "lp_bld_arit.h"
41 #include "lp_bld_const.h"
42 #include "lp_bld_debug.h"
43 #include "lp_bld_printf.h"
44 #include "lp_bld_flow.h"
45 #include "lp_bld_sample.h"
46 #include "lp_bld_swizzle.h"
47 #include "lp_bld_type.h"
48 #include "lp_bld_logic.h"
49 #include "lp_bld_pack.h"
50 #include "lp_bld_quad.h"
51 #include "lp_bld_bitarit.h"
55 * Bri-linear factor. Should be greater than one.
57 #define BRILINEAR_FACTOR 2
60 * Does the given texture wrap mode allow sampling the texture border color?
61 * XXX maybe move this into gallium util code.
64 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
65 unsigned min_img_filter
,
66 unsigned mag_img_filter
)
69 case PIPE_TEX_WRAP_REPEAT
:
70 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
71 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
72 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
74 case PIPE_TEX_WRAP_CLAMP
:
75 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
76 if (min_img_filter
== PIPE_TEX_FILTER_NEAREST
&&
77 mag_img_filter
== PIPE_TEX_FILTER_NEAREST
) {
82 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
83 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
86 assert(0 && "unexpected wrap mode");
93 * Initialize lp_sampler_static_texture_state object with the gallium
94 * texture/sampler_view state (this contains the parts which are
98 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
99 const struct pipe_sampler_view
*view
)
101 const struct pipe_resource
*texture
;
103 memset(state
, 0, sizeof *state
);
105 if (!view
|| !view
->texture
)
108 texture
= view
->texture
;
110 state
->format
= view
->format
;
111 state
->swizzle_r
= view
->swizzle_r
;
112 state
->swizzle_g
= view
->swizzle_g
;
113 state
->swizzle_b
= view
->swizzle_b
;
114 state
->swizzle_a
= view
->swizzle_a
;
116 state
->target
= view
->target
;
117 state
->pot_width
= util_is_power_of_two_or_zero(texture
->width0
);
118 state
->pot_height
= util_is_power_of_two_or_zero(texture
->height0
);
119 state
->pot_depth
= util_is_power_of_two_or_zero(texture
->depth0
);
120 state
->level_zero_only
= !view
->u
.tex
.last_level
;
123 * the layer / element / level parameters are all either dynamic
124 * state or handled transparently wrt execution.
129 * Initialize lp_sampler_static_texture_state object with the gallium
130 * texture/sampler_view state (this contains the parts which are
131 * considered static).
134 lp_sampler_static_texture_state_image(struct lp_static_texture_state
*state
,
135 const struct pipe_image_view
*view
)
137 const struct pipe_resource
*resource
;
139 memset(state
, 0, sizeof *state
);
141 if (!view
|| !view
->resource
)
144 resource
= view
->resource
;
146 state
->format
= view
->format
;
147 state
->swizzle_r
= PIPE_SWIZZLE_X
;
148 state
->swizzle_g
= PIPE_SWIZZLE_Y
;
149 state
->swizzle_b
= PIPE_SWIZZLE_Z
;
150 state
->swizzle_a
= PIPE_SWIZZLE_W
;
152 state
->target
= view
->resource
->target
;
153 state
->pot_width
= util_is_power_of_two_or_zero(resource
->width0
);
154 state
->pot_height
= util_is_power_of_two_or_zero(resource
->height0
);
155 state
->pot_depth
= util_is_power_of_two_or_zero(resource
->depth0
);
156 state
->level_zero_only
= 0;
159 * the layer / element / level parameters are all either dynamic
160 * state or handled transparently wrt execution.
165 * Initialize lp_sampler_static_sampler_state object with the gallium sampler
166 * state (this contains the parts which are considered static).
169 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
170 const struct pipe_sampler_state
*sampler
)
172 memset(state
, 0, sizeof *state
);
178 * We don't copy sampler state over unless it is actually enabled, to avoid
179 * spurious recompiles, as the sampler static state is part of the shader
182 * Ideally gallium frontends or cso_cache module would make all state
183 * canonical, but until that happens it's better to be safe than sorry here.
185 * XXX: Actually there's much more than can be done here, especially
186 * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
189 state
->wrap_s
= sampler
->wrap_s
;
190 state
->wrap_t
= sampler
->wrap_t
;
191 state
->wrap_r
= sampler
->wrap_r
;
192 state
->min_img_filter
= sampler
->min_img_filter
;
193 state
->mag_img_filter
= sampler
->mag_img_filter
;
194 state
->min_mip_filter
= sampler
->min_mip_filter
;
195 state
->seamless_cube_map
= sampler
->seamless_cube_map
;
197 if (sampler
->max_lod
> 0.0f
) {
198 state
->max_lod_pos
= 1;
201 if (sampler
->lod_bias
!= 0.0f
) {
202 state
->lod_bias_non_zero
= 1;
205 if (state
->min_mip_filter
!= PIPE_TEX_MIPFILTER_NONE
||
206 state
->min_img_filter
!= state
->mag_img_filter
) {
208 /* If min_lod == max_lod we can greatly simplify mipmap selection.
209 * This is a case that occurs during automatic mipmap generation.
211 if (sampler
->min_lod
== sampler
->max_lod
) {
212 state
->min_max_lod_equal
= 1;
214 if (sampler
->min_lod
> 0.0f
) {
215 state
->apply_min_lod
= 1;
219 * XXX this won't do anything with the mesa state tracker which always
220 * sets max_lod to not more than actually present mip maps...
222 if (sampler
->max_lod
< (PIPE_MAX_TEXTURE_LEVELS
- 1)) {
223 state
->apply_max_lod
= 1;
228 state
->compare_mode
= sampler
->compare_mode
;
229 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
230 state
->compare_func
= sampler
->compare_func
;
233 state
->normalized_coords
= sampler
->normalized_coords
;
238 * Generate code to compute coordinate gradient (rho).
239 * \param derivs partial derivatives of (s, t, r, q) with respect to X and Y
241 * The resulting rho has bld->levelf format (per quad or per element).
244 lp_build_rho(struct lp_build_sample_context
*bld
,
245 unsigned texture_unit
,
249 LLVMValueRef cube_rho
,
250 const struct lp_derivatives
*derivs
)
252 struct gallivm_state
*gallivm
= bld
->gallivm
;
253 struct lp_build_context
*int_size_bld
= &bld
->int_size_in_bld
;
254 struct lp_build_context
*float_size_bld
= &bld
->float_size_in_bld
;
255 struct lp_build_context
*float_bld
= &bld
->float_bld
;
256 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
257 struct lp_build_context
*rho_bld
= &bld
->lodf_bld
;
258 const unsigned dims
= bld
->dims
;
259 LLVMValueRef ddx_ddy
[2] = {NULL
};
260 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
261 LLVMTypeRef i32t
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
262 LLVMValueRef index0
= LLVMConstInt(i32t
, 0, 0);
263 LLVMValueRef index1
= LLVMConstInt(i32t
, 1, 0);
264 LLVMValueRef index2
= LLVMConstInt(i32t
, 2, 0);
265 LLVMValueRef rho_vec
;
266 LLVMValueRef int_size
, float_size
;
268 LLVMValueRef first_level
, first_level_vec
;
269 unsigned length
= coord_bld
->type
.length
;
270 unsigned num_quads
= length
/ 4;
271 boolean rho_per_quad
= rho_bld
->type
.length
!= length
;
272 boolean no_rho_opt
= bld
->no_rho_approx
&& (dims
> 1);
274 LLVMValueRef i32undef
= LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
275 LLVMValueRef rho_xvec
, rho_yvec
;
277 /* Note that all simplified calculations will only work for isotropic filtering */
280 * rho calcs are always per quad except for explicit derivs (excluding
281 * the messy cube maps for now) when requested.
284 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
, bld
->gallivm
,
285 bld
->context_ptr
, texture_unit
, NULL
);
286 first_level_vec
= lp_build_broadcast_scalar(int_size_bld
, first_level
);
287 int_size
= lp_build_minify(int_size_bld
, bld
->int_size
, first_level_vec
, TRUE
);
288 float_size
= lp_build_int_to_float(float_size_bld
, int_size
);
291 LLVMValueRef cubesize
;
292 LLVMValueRef index0
= lp_build_const_int32(gallivm
, 0);
295 * Cube map code did already everything except size mul and per-quad extraction.
296 * Luckily cube maps are always quadratic!
299 rho
= lp_build_pack_aos_scalars(bld
->gallivm
, coord_bld
->type
,
300 rho_bld
->type
, cube_rho
, 0);
303 rho
= lp_build_swizzle_scalar_aos(coord_bld
, cube_rho
, 0, 4);
305 /* Could optimize this for single quad just skip the broadcast */
306 cubesize
= lp_build_extract_broadcast(gallivm
, bld
->float_size_in_type
,
307 rho_bld
->type
, float_size
, index0
);
308 /* skipping sqrt hence returning rho squared */
309 cubesize
= lp_build_mul(rho_bld
, cubesize
, cubesize
);
310 rho
= lp_build_mul(rho_bld
, cubesize
, rho
);
313 LLVMValueRef ddmax
[3] = { NULL
}, ddx
[3] = { NULL
}, ddy
[3] = { NULL
};
314 for (i
= 0; i
< dims
; i
++) {
315 LLVMValueRef floatdim
;
316 LLVMValueRef indexi
= lp_build_const_int32(gallivm
, i
);
318 floatdim
= lp_build_extract_broadcast(gallivm
, bld
->float_size_in_type
,
319 coord_bld
->type
, float_size
, indexi
);
322 * note that for rho_per_quad case could reduce math (at some shuffle
323 * cost), but for now use same code to per-pixel lod case.
326 ddx
[i
] = lp_build_mul(coord_bld
, floatdim
, derivs
->ddx
[i
]);
327 ddy
[i
] = lp_build_mul(coord_bld
, floatdim
, derivs
->ddy
[i
]);
328 ddx
[i
] = lp_build_mul(coord_bld
, ddx
[i
], ddx
[i
]);
329 ddy
[i
] = lp_build_mul(coord_bld
, ddy
[i
], ddy
[i
]);
332 LLVMValueRef tmpx
, tmpy
;
333 tmpx
= lp_build_abs(coord_bld
, derivs
->ddx
[i
]);
334 tmpy
= lp_build_abs(coord_bld
, derivs
->ddy
[i
]);
335 ddmax
[i
] = lp_build_max(coord_bld
, tmpx
, tmpy
);
336 ddmax
[i
] = lp_build_mul(coord_bld
, floatdim
, ddmax
[i
]);
340 rho_xvec
= lp_build_add(coord_bld
, ddx
[0], ddx
[1]);
341 rho_yvec
= lp_build_add(coord_bld
, ddy
[0], ddy
[1]);
343 rho_xvec
= lp_build_add(coord_bld
, rho_xvec
, ddx
[2]);
344 rho_yvec
= lp_build_add(coord_bld
, rho_yvec
, ddy
[2]);
346 rho
= lp_build_max(coord_bld
, rho_xvec
, rho_yvec
);
347 /* skipping sqrt hence returning rho squared */
352 rho
= lp_build_max(coord_bld
, rho
, ddmax
[1]);
354 rho
= lp_build_max(coord_bld
, rho
, ddmax
[2]);
360 * rho_vec contains per-pixel rho, convert to scalar per quad.
362 rho
= lp_build_pack_aos_scalars(bld
->gallivm
, coord_bld
->type
,
363 rho_bld
->type
, rho
, 0);
368 * This looks all a bit complex, but it's not that bad
369 * (the shuffle code makes it look worse than it is).
370 * Still, might not be ideal for all cases.
372 static const unsigned char swizzle0
[] = { /* no-op swizzle */
373 0, LP_BLD_SWIZZLE_DONTCARE
,
374 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
376 static const unsigned char swizzle1
[] = {
377 1, LP_BLD_SWIZZLE_DONTCARE
,
378 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
380 static const unsigned char swizzle2
[] = {
381 2, LP_BLD_SWIZZLE_DONTCARE
,
382 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
386 ddx_ddy
[0] = lp_build_packed_ddx_ddy_onecoord(coord_bld
, s
);
388 else if (dims
>= 2) {
389 ddx_ddy
[0] = lp_build_packed_ddx_ddy_twocoord(coord_bld
, s
, t
);
391 ddx_ddy
[1] = lp_build_packed_ddx_ddy_onecoord(coord_bld
, r
);
396 static const unsigned char swizzle01
[] = { /* no-op swizzle */
398 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
400 static const unsigned char swizzle23
[] = {
402 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
404 LLVMValueRef ddx_ddys
, ddx_ddyt
, floatdim
, shuffles
[LP_MAX_VECTOR_LENGTH
/ 4];
406 for (i
= 0; i
< num_quads
; i
++) {
407 shuffles
[i
*4+0] = shuffles
[i
*4+1] = index0
;
408 shuffles
[i
*4+2] = shuffles
[i
*4+3] = index1
;
410 floatdim
= LLVMBuildShuffleVector(builder
, float_size
, float_size
,
411 LLVMConstVector(shuffles
, length
), "");
412 ddx_ddy
[0] = lp_build_mul(coord_bld
, ddx_ddy
[0], floatdim
);
413 ddx_ddy
[0] = lp_build_mul(coord_bld
, ddx_ddy
[0], ddx_ddy
[0]);
414 ddx_ddys
= lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle01
);
415 ddx_ddyt
= lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle23
);
416 rho_vec
= lp_build_add(coord_bld
, ddx_ddys
, ddx_ddyt
);
419 static const unsigned char swizzle02
[] = {
421 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
423 floatdim
= lp_build_extract_broadcast(gallivm
, bld
->float_size_in_type
,
424 coord_bld
->type
, float_size
, index2
);
425 ddx_ddy
[1] = lp_build_mul(coord_bld
, ddx_ddy
[1], floatdim
);
426 ddx_ddy
[1] = lp_build_mul(coord_bld
, ddx_ddy
[1], ddx_ddy
[1]);
427 ddx_ddy
[1] = lp_build_swizzle_aos(coord_bld
, ddx_ddy
[1], swizzle02
);
428 rho_vec
= lp_build_add(coord_bld
, rho_vec
, ddx_ddy
[1]);
431 rho_xvec
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle0
);
432 rho_yvec
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle1
);
433 rho
= lp_build_max(coord_bld
, rho_xvec
, rho_yvec
);
436 rho
= lp_build_pack_aos_scalars(bld
->gallivm
, coord_bld
->type
,
437 rho_bld
->type
, rho
, 0);
440 rho
= lp_build_swizzle_scalar_aos(coord_bld
, rho
, 0, 4);
442 /* skipping sqrt hence returning rho squared */
445 ddx_ddy
[0] = lp_build_abs(coord_bld
, ddx_ddy
[0]);
447 ddx_ddy
[1] = lp_build_abs(coord_bld
, ddx_ddy
[1]);
450 ddx_ddy
[1] = NULL
; /* silence compiler warning */
454 rho_xvec
= lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle0
);
455 rho_yvec
= lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle2
);
457 else if (dims
== 2) {
458 static const unsigned char swizzle02
[] = {
460 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
462 static const unsigned char swizzle13
[] = {
464 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
466 rho_xvec
= lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle02
);
467 rho_yvec
= lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle13
);
470 LLVMValueRef shuffles1
[LP_MAX_VECTOR_LENGTH
];
471 LLVMValueRef shuffles2
[LP_MAX_VECTOR_LENGTH
];
473 for (i
= 0; i
< num_quads
; i
++) {
474 shuffles1
[4*i
+ 0] = lp_build_const_int32(gallivm
, 4*i
);
475 shuffles1
[4*i
+ 1] = lp_build_const_int32(gallivm
, 4*i
+ 2);
476 shuffles1
[4*i
+ 2] = lp_build_const_int32(gallivm
, length
+ 4*i
);
477 shuffles1
[4*i
+ 3] = i32undef
;
478 shuffles2
[4*i
+ 0] = lp_build_const_int32(gallivm
, 4*i
+ 1);
479 shuffles2
[4*i
+ 1] = lp_build_const_int32(gallivm
, 4*i
+ 3);
480 shuffles2
[4*i
+ 2] = lp_build_const_int32(gallivm
, length
+ 4*i
+ 2);
481 shuffles2
[4*i
+ 3] = i32undef
;
483 rho_xvec
= LLVMBuildShuffleVector(builder
, ddx_ddy
[0], ddx_ddy
[1],
484 LLVMConstVector(shuffles1
, length
), "");
485 rho_yvec
= LLVMBuildShuffleVector(builder
, ddx_ddy
[0], ddx_ddy
[1],
486 LLVMConstVector(shuffles2
, length
), "");
489 rho_vec
= lp_build_max(coord_bld
, rho_xvec
, rho_yvec
);
491 if (bld
->coord_type
.length
> 4) {
492 /* expand size to each quad */
494 /* could use some broadcast_vector helper for this? */
495 LLVMValueRef src
[LP_MAX_VECTOR_LENGTH
/4];
496 for (i
= 0; i
< num_quads
; i
++) {
499 float_size
= lp_build_concat(bld
->gallivm
, src
, float_size_bld
->type
, num_quads
);
502 float_size
= lp_build_broadcast_scalar(coord_bld
, float_size
);
504 rho_vec
= lp_build_mul(coord_bld
, rho_vec
, float_size
);
511 LLVMValueRef rho_s
, rho_t
, rho_r
;
513 rho_s
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle0
);
514 rho_t
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle1
);
516 rho
= lp_build_max(coord_bld
, rho_s
, rho_t
);
519 rho_r
= lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle2
);
520 rho
= lp_build_max(coord_bld
, rho
, rho_r
);
525 rho
= lp_build_pack_aos_scalars(bld
->gallivm
, coord_bld
->type
,
526 rho_bld
->type
, rho
, 0);
529 rho
= lp_build_swizzle_scalar_aos(coord_bld
, rho
, 0, 4);
534 rho_vec
= LLVMBuildExtractElement(builder
, rho_vec
, index0
, "");
536 rho_vec
= lp_build_mul(float_size_bld
, rho_vec
, float_size
);
543 LLVMValueRef rho_s
, rho_t
, rho_r
;
545 rho_s
= LLVMBuildExtractElement(builder
, rho_vec
, index0
, "");
546 rho_t
= LLVMBuildExtractElement(builder
, rho_vec
, index1
, "");
548 rho
= lp_build_max(float_bld
, rho_s
, rho_t
);
551 rho_r
= LLVMBuildExtractElement(builder
, rho_vec
, index2
, "");
552 rho
= lp_build_max(float_bld
, rho
, rho_r
);
557 rho
= lp_build_broadcast_scalar(rho_bld
, rho
);
568 * Bri-linear lod computation
570 * Use a piece-wise linear approximation of log2 such that:
571 * - round to nearest, for values in the neighborhood of -1, 0, 1, 2, etc.
572 * - linear approximation for values in the neighborhood of 0.5, 1.5., etc,
573 * with the steepness specified in 'factor'
574 * - exact result for 0.5, 1.5, etc.
590 * This is a technique also commonly used in hardware:
591 * - http://ixbtlabs.com/articles2/gffx/nv40-rx800-3.html
593 * TODO: For correctness, this should only be applied when texture is known to
594 * have regular mipmaps, i.e., mipmaps derived from the base level.
596 * TODO: This could be done in fixed point, where applicable.
599 lp_build_brilinear_lod(struct lp_build_context
*bld
,
602 LLVMValueRef
*out_lod_ipart
,
603 LLVMValueRef
*out_lod_fpart
)
605 LLVMValueRef lod_fpart
;
606 double pre_offset
= (factor
- 0.5)/factor
- 0.5;
607 double post_offset
= 1 - factor
;
610 lp_build_printf(bld
->gallivm
, "lod = %f\n", lod
);
613 lod
= lp_build_add(bld
, lod
,
614 lp_build_const_vec(bld
->gallivm
, bld
->type
, pre_offset
));
616 lp_build_ifloor_fract(bld
, lod
, out_lod_ipart
, &lod_fpart
);
618 lod_fpart
= lp_build_mad(bld
, lod_fpart
,
619 lp_build_const_vec(bld
->gallivm
, bld
->type
, factor
),
620 lp_build_const_vec(bld
->gallivm
, bld
->type
, post_offset
));
623 * It's not necessary to clamp lod_fpart since:
624 * - the above expression will never produce numbers greater than one.
625 * - the mip filtering branch is only taken if lod_fpart is positive
628 *out_lod_fpart
= lod_fpart
;
631 lp_build_printf(bld
->gallivm
, "lod_ipart = %i\n", *out_lod_ipart
);
632 lp_build_printf(bld
->gallivm
, "lod_fpart = %f\n\n", *out_lod_fpart
);
638 * Combined log2 and brilinear lod computation.
640 * It's in all identical to calling lp_build_fast_log2() and
641 * lp_build_brilinear_lod() above, but by combining we can compute the integer
642 * and fractional part independently.
645 lp_build_brilinear_rho(struct lp_build_context
*bld
,
648 LLVMValueRef
*out_lod_ipart
,
649 LLVMValueRef
*out_lod_fpart
)
651 LLVMValueRef lod_ipart
;
652 LLVMValueRef lod_fpart
;
654 const double pre_factor
= (2*factor
- 0.5)/(M_SQRT2
*factor
);
655 const double post_offset
= 1 - 2*factor
;
657 assert(bld
->type
.floating
);
659 assert(lp_check_value(bld
->type
, rho
));
662 * The pre factor will make the intersections with the exact powers of two
663 * happen precisely where we want them to be, which means that the integer
664 * part will not need any post adjustments.
666 rho
= lp_build_mul(bld
, rho
,
667 lp_build_const_vec(bld
->gallivm
, bld
->type
, pre_factor
));
669 /* ipart = ifloor(log2(rho)) */
670 lod_ipart
= lp_build_extract_exponent(bld
, rho
, 0);
672 /* fpart = rho / 2**ipart */
673 lod_fpart
= lp_build_extract_mantissa(bld
, rho
);
675 lod_fpart
= lp_build_mad(bld
, lod_fpart
,
676 lp_build_const_vec(bld
->gallivm
, bld
->type
, factor
),
677 lp_build_const_vec(bld
->gallivm
, bld
->type
, post_offset
));
680 * Like lp_build_brilinear_lod, it's not necessary to clamp lod_fpart since:
681 * - the above expression will never produce numbers greater than one.
682 * - the mip filtering branch is only taken if lod_fpart is positive
685 *out_lod_ipart
= lod_ipart
;
686 *out_lod_fpart
= lod_fpart
;
691 * Fast implementation of iround(log2(sqrt(x))), based on
692 * log2(x^n) == n*log2(x).
694 * Gives accurate results all the time.
695 * (Could be trivially extended to handle other power-of-two roots.)
698 lp_build_ilog2_sqrt(struct lp_build_context
*bld
,
701 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
703 struct lp_type i_type
= lp_int_type(bld
->type
);
704 LLVMValueRef one
= lp_build_const_int_vec(bld
->gallivm
, i_type
, 1);
706 assert(bld
->type
.floating
);
708 assert(lp_check_value(bld
->type
, x
));
710 /* ipart = log2(x) + 0.5 = 0.5*(log2(x^2) + 1.0) */
711 ipart
= lp_build_extract_exponent(bld
, x
, 1);
712 ipart
= LLVMBuildAShr(builder
, ipart
, one
, "");
719 * Generate code to compute texture level of detail (lambda).
720 * \param derivs partial derivatives of (s, t, r, q) with respect to X and Y
721 * \param lod_bias optional float vector with the shader lod bias
722 * \param explicit_lod optional float vector with the explicit lod
723 * \param cube_rho rho calculated by cube coord mapping (optional)
724 * \param out_lod_ipart integer part of lod
725 * \param out_lod_fpart float part of lod (never larger than 1 but may be negative)
726 * \param out_lod_positive (mask) if lod is positive (i.e. texture is minified)
728 * The resulting lod can be scalar per quad or be per element.
731 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
733 unsigned texture_unit
,
734 unsigned sampler_unit
,
738 LLVMValueRef cube_rho
,
739 const struct lp_derivatives
*derivs
,
740 LLVMValueRef lod_bias
, /* optional */
741 LLVMValueRef explicit_lod
, /* optional */
743 LLVMValueRef
*out_lod
,
744 LLVMValueRef
*out_lod_ipart
,
745 LLVMValueRef
*out_lod_fpart
,
746 LLVMValueRef
*out_lod_positive
)
749 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
750 struct lp_sampler_dynamic_state
*dynamic_state
= bld
->dynamic_state
;
751 struct lp_build_context
*lodf_bld
= &bld
->lodf_bld
;
754 *out_lod_ipart
= bld
->lodi_bld
.zero
;
755 *out_lod_positive
= bld
->lodi_bld
.zero
;
756 *out_lod_fpart
= lodf_bld
->zero
;
759 * For determining min/mag, we follow GL 4.1 spec, 3.9.12 Texture Magnification:
760 * "Implementations may either unconditionally assume c = 0 for the minification
761 * vs. magnification switch-over point, or may choose to make c depend on the
762 * combination of minification and magnification modes as follows: if the
763 * magnification filter is given by LINEAR and the minification filter is given
764 * by NEAREST_MIPMAP_NEAREST or NEAREST_MIPMAP_LINEAR, then c = 0.5. This is
765 * done to ensure that a minified texture does not appear "sharper" than a
766 * magnified texture. Otherwise c = 0."
767 * And 3.9.11 Texture Minification:
768 * "If lod is less than or equal to the constant c (see section 3.9.12) the
769 * texture is said to be magnified; if it is greater, the texture is minified."
770 * So, using 0 as switchover point always, and using magnification for lod == 0.
771 * Note that the always c = 0 behavior is new (first appearing in GL 3.1 spec),
772 * old GL versions required 0.5 for the modes listed above.
773 * I have no clue about the (undocumented) wishes of d3d9/d3d10 here!
776 if (bld
->static_sampler_state
->min_max_lod_equal
&& !is_lodq
) {
777 /* User is forcing sampling from a particular mipmap level.
778 * This is hit during mipmap generation.
780 LLVMValueRef min_lod
=
781 dynamic_state
->min_lod(dynamic_state
, bld
->gallivm
,
782 bld
->context_ptr
, sampler_unit
);
784 lod
= lp_build_broadcast_scalar(lodf_bld
, min_lod
);
788 if (bld
->num_lods
!= bld
->coord_type
.length
)
789 lod
= lp_build_pack_aos_scalars(bld
->gallivm
, bld
->coord_bld
.type
,
790 lodf_bld
->type
, explicit_lod
, 0);
796 boolean rho_squared
= (bld
->no_rho_approx
&&
797 (bld
->dims
> 1)) || cube_rho
;
799 rho
= lp_build_rho(bld
, texture_unit
, s
, t
, r
, cube_rho
, derivs
);
802 * Compute lod = log2(rho)
805 if (!lod_bias
&& !is_lodq
&&
806 !bld
->static_sampler_state
->lod_bias_non_zero
&&
807 !bld
->static_sampler_state
->apply_max_lod
&&
808 !bld
->static_sampler_state
->apply_min_lod
) {
810 * Special case when there are no post-log2 adjustments, which
811 * saves instructions but keeping the integer and fractional lod
812 * computations separate from the start.
815 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
||
816 mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
818 * Don't actually need both values all the time, lod_ipart is
819 * needed for nearest mipfilter, lod_positive if min != mag.
822 *out_lod_ipart
= lp_build_ilog2_sqrt(lodf_bld
, rho
);
825 *out_lod_ipart
= lp_build_ilog2(lodf_bld
, rho
);
827 *out_lod_positive
= lp_build_cmp(lodf_bld
, PIPE_FUNC_GREATER
,
831 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
&&
832 !bld
->no_brilinear
&& !rho_squared
) {
834 * This can't work if rho is squared. Not sure if it could be
835 * fixed while keeping it worthwile, could also do sqrt here
836 * but brilinear and no_rho_opt seems like a combination not
837 * making much sense anyway so just use ordinary path below.
839 lp_build_brilinear_rho(lodf_bld
, rho
, BRILINEAR_FACTOR
,
840 out_lod_ipart
, out_lod_fpart
);
841 *out_lod_positive
= lp_build_cmp(lodf_bld
, PIPE_FUNC_GREATER
,
848 lod
= lp_build_log2(lodf_bld
, rho
);
851 lod
= lp_build_fast_log2(lodf_bld
, rho
);
854 /* log2(x^2) == 0.5*log2(x) */
855 lod
= lp_build_mul(lodf_bld
, lod
,
856 lp_build_const_vec(bld
->gallivm
, lodf_bld
->type
, 0.5F
));
859 /* add shader lod bias */
861 if (bld
->num_lods
!= bld
->coord_type
.length
)
862 lod_bias
= lp_build_pack_aos_scalars(bld
->gallivm
, bld
->coord_bld
.type
,
863 lodf_bld
->type
, lod_bias
, 0);
864 lod
= LLVMBuildFAdd(builder
, lod
, lod_bias
, "shader_lod_bias");
868 /* add sampler lod bias */
869 if (bld
->static_sampler_state
->lod_bias_non_zero
) {
870 LLVMValueRef sampler_lod_bias
=
871 dynamic_state
->lod_bias(dynamic_state
, bld
->gallivm
,
872 bld
->context_ptr
, sampler_unit
);
873 sampler_lod_bias
= lp_build_broadcast_scalar(lodf_bld
,
875 lod
= LLVMBuildFAdd(builder
, lod
, sampler_lod_bias
, "sampler_lod_bias");
883 if (bld
->static_sampler_state
->apply_max_lod
) {
884 LLVMValueRef max_lod
=
885 dynamic_state
->max_lod(dynamic_state
, bld
->gallivm
,
886 bld
->context_ptr
, sampler_unit
);
887 max_lod
= lp_build_broadcast_scalar(lodf_bld
, max_lod
);
889 lod
= lp_build_min(lodf_bld
, lod
, max_lod
);
891 if (bld
->static_sampler_state
->apply_min_lod
) {
892 LLVMValueRef min_lod
=
893 dynamic_state
->min_lod(dynamic_state
, bld
->gallivm
,
894 bld
->context_ptr
, sampler_unit
);
895 min_lod
= lp_build_broadcast_scalar(lodf_bld
, min_lod
);
897 lod
= lp_build_max(lodf_bld
, lod
, min_lod
);
901 *out_lod_fpart
= lod
;
906 *out_lod_positive
= lp_build_cmp(lodf_bld
, PIPE_FUNC_GREATER
,
907 lod
, lodf_bld
->zero
);
909 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
910 if (!bld
->no_brilinear
) {
911 lp_build_brilinear_lod(lodf_bld
, lod
, BRILINEAR_FACTOR
,
912 out_lod_ipart
, out_lod_fpart
);
915 lp_build_ifloor_fract(lodf_bld
, lod
, out_lod_ipart
, out_lod_fpart
);
918 lp_build_name(*out_lod_fpart
, "lod_fpart");
921 *out_lod_ipart
= lp_build_iround(lodf_bld
, lod
);
924 lp_build_name(*out_lod_ipart
, "lod_ipart");
931 * For PIPE_TEX_MIPFILTER_NEAREST, convert int part of lod
932 * to actual mip level.
933 * Note: this is all scalar per quad code.
934 * \param lod_ipart int texture level of detail
935 * \param level_out returns integer
936 * \param out_of_bounds returns per coord out_of_bounds mask if provided
939 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
940 unsigned texture_unit
,
941 LLVMValueRef lod_ipart
,
942 LLVMValueRef
*level_out
,
943 LLVMValueRef
*out_of_bounds
)
945 struct lp_build_context
*leveli_bld
= &bld
->leveli_bld
;
946 struct lp_sampler_dynamic_state
*dynamic_state
= bld
->dynamic_state
;
947 LLVMValueRef first_level
, last_level
, level
;
949 first_level
= dynamic_state
->first_level(dynamic_state
, bld
->gallivm
,
950 bld
->context_ptr
, texture_unit
, NULL
);
951 last_level
= dynamic_state
->last_level(dynamic_state
, bld
->gallivm
,
952 bld
->context_ptr
, texture_unit
, NULL
);
953 first_level
= lp_build_broadcast_scalar(leveli_bld
, first_level
);
954 last_level
= lp_build_broadcast_scalar(leveli_bld
, last_level
);
956 level
= lp_build_add(leveli_bld
, lod_ipart
, first_level
);
959 LLVMValueRef out
, out1
;
960 out
= lp_build_cmp(leveli_bld
, PIPE_FUNC_LESS
, level
, first_level
);
961 out1
= lp_build_cmp(leveli_bld
, PIPE_FUNC_GREATER
, level
, last_level
);
962 out
= lp_build_or(leveli_bld
, out
, out1
);
963 if (bld
->num_mips
== bld
->coord_bld
.type
.length
) {
964 *out_of_bounds
= out
;
966 else if (bld
->num_mips
== 1) {
967 *out_of_bounds
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, out
);
970 assert(bld
->num_mips
== bld
->coord_bld
.type
.length
/ 4);
971 *out_of_bounds
= lp_build_unpack_broadcast_aos_scalars(bld
->gallivm
,
973 bld
->int_coord_bld
.type
,
976 level
= lp_build_andnot(&bld
->int_coord_bld
, level
, *out_of_bounds
);
980 /* clamp level to legal range of levels */
981 *level_out
= lp_build_clamp(leveli_bld
, level
, first_level
, last_level
);
988 * For PIPE_TEX_MIPFILTER_LINEAR, convert per-quad (or per element) int LOD(s)
989 * to two (per-quad) (adjacent) mipmap level indexes, and fix up float lod
991 * Later, we'll sample from those two mipmap levels and interpolate between them.
994 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
995 unsigned texture_unit
,
996 LLVMValueRef lod_ipart
,
997 LLVMValueRef
*lod_fpart_inout
,
998 LLVMValueRef
*level0_out
,
999 LLVMValueRef
*level1_out
)
1001 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1002 struct lp_sampler_dynamic_state
*dynamic_state
= bld
->dynamic_state
;
1003 struct lp_build_context
*leveli_bld
= &bld
->leveli_bld
;
1004 struct lp_build_context
*levelf_bld
= &bld
->levelf_bld
;
1005 LLVMValueRef first_level
, last_level
;
1006 LLVMValueRef clamp_min
;
1007 LLVMValueRef clamp_max
;
1009 assert(bld
->num_lods
== bld
->num_mips
);
1011 first_level
= dynamic_state
->first_level(dynamic_state
, bld
->gallivm
,
1012 bld
->context_ptr
, texture_unit
, NULL
);
1013 last_level
= dynamic_state
->last_level(dynamic_state
, bld
->gallivm
,
1014 bld
->context_ptr
, texture_unit
, NULL
);
1015 first_level
= lp_build_broadcast_scalar(leveli_bld
, first_level
);
1016 last_level
= lp_build_broadcast_scalar(leveli_bld
, last_level
);
1018 *level0_out
= lp_build_add(leveli_bld
, lod_ipart
, first_level
);
1019 *level1_out
= lp_build_add(leveli_bld
, *level0_out
, leveli_bld
->one
);
1022 * Clamp both *level0_out and *level1_out to [first_level, last_level], with
1023 * the minimum number of comparisons, and zeroing lod_fpart in the extreme
1024 * ends in the process.
1027 /* *level0_out < first_level */
1028 clamp_min
= LLVMBuildICmp(builder
, LLVMIntSLT
,
1029 *level0_out
, first_level
,
1030 "clamp_lod_to_first");
1032 *level0_out
= LLVMBuildSelect(builder
, clamp_min
,
1033 first_level
, *level0_out
, "");
1035 *level1_out
= LLVMBuildSelect(builder
, clamp_min
,
1036 first_level
, *level1_out
, "");
1038 *lod_fpart_inout
= LLVMBuildSelect(builder
, clamp_min
,
1039 levelf_bld
->zero
, *lod_fpart_inout
, "");
1041 /* *level0_out >= last_level */
1042 clamp_max
= LLVMBuildICmp(builder
, LLVMIntSGE
,
1043 *level0_out
, last_level
,
1044 "clamp_lod_to_last");
1046 *level0_out
= LLVMBuildSelect(builder
, clamp_max
,
1047 last_level
, *level0_out
, "");
1049 *level1_out
= LLVMBuildSelect(builder
, clamp_max
,
1050 last_level
, *level1_out
, "");
1052 *lod_fpart_inout
= LLVMBuildSelect(builder
, clamp_max
,
1053 levelf_bld
->zero
, *lod_fpart_inout
, "");
1055 lp_build_name(*level0_out
, "texture%u_miplevel0", texture_unit
);
1056 lp_build_name(*level1_out
, "texture%u_miplevel1", texture_unit
);
1057 lp_build_name(*lod_fpart_inout
, "texture%u_mipweight", texture_unit
);
1062 * Return pointer to a single mipmap level.
1063 * \param level integer mipmap level
1066 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
1069 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1070 LLVMValueRef indexes
[2], data_ptr
, mip_offset
;
1072 indexes
[0] = lp_build_const_int32(bld
->gallivm
, 0);
1074 mip_offset
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
1075 mip_offset
= LLVMBuildLoad(builder
, mip_offset
, "");
1076 data_ptr
= LLVMBuildGEP(builder
, bld
->base_ptr
, &mip_offset
, 1, "");
1081 * Return (per-pixel) offsets to mip levels.
1082 * \param level integer mipmap level
1085 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
1088 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1089 LLVMValueRef indexes
[2], offsets
, offset1
;
1091 indexes
[0] = lp_build_const_int32(bld
->gallivm
, 0);
1092 if (bld
->num_mips
== 1) {
1094 offset1
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
1095 offset1
= LLVMBuildLoad(builder
, offset1
, "");
1096 offsets
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, offset1
);
1098 else if (bld
->num_mips
== bld
->coord_bld
.type
.length
/ 4) {
1101 offsets
= bld
->int_coord_bld
.undef
;
1102 for (i
= 0; i
< bld
->num_mips
; i
++) {
1103 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1104 LLVMValueRef indexo
= lp_build_const_int32(bld
->gallivm
, 4 * i
);
1105 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
1106 offset1
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
1107 offset1
= LLVMBuildLoad(builder
, offset1
, "");
1108 offsets
= LLVMBuildInsertElement(builder
, offsets
, offset1
, indexo
, "");
1110 offsets
= lp_build_swizzle_scalar_aos(&bld
->int_coord_bld
, offsets
, 0, 4);
1115 assert (bld
->num_mips
== bld
->coord_bld
.type
.length
);
1117 offsets
= bld
->int_coord_bld
.undef
;
1118 for (i
= 0; i
< bld
->num_mips
; i
++) {
1119 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1120 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
1121 offset1
= LLVMBuildGEP(builder
, bld
->mip_offsets
, indexes
, 2, "");
1122 offset1
= LLVMBuildLoad(builder
, offset1
, "");
1123 offsets
= LLVMBuildInsertElement(builder
, offsets
, offset1
, indexi
, "");
1131 * Codegen equivalent for u_minify().
1132 * @param lod_scalar if lod is a (broadcasted) scalar
1133 * Return max(1, base_size >> level);
1136 lp_build_minify(struct lp_build_context
*bld
,
1137 LLVMValueRef base_size
,
1141 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1142 assert(lp_check_value(bld
->type
, base_size
));
1143 assert(lp_check_value(bld
->type
, level
));
1145 if (level
== bld
->zero
) {
1146 /* if we're using mipmap level zero, no minification is needed */
1151 assert(bld
->type
.sign
);
1153 (util_cpu_caps
.has_avx2
|| !util_cpu_caps
.has_sse
)) {
1154 size
= LLVMBuildLShr(builder
, base_size
, level
, "minify");
1155 size
= lp_build_max(bld
, size
, bld
->one
);
1159 * emulate shift with float mul, since intel "forgot" shifts with
1160 * per-element shift count until avx2, which results in terrible
1161 * scalar extraction (both count and value), scalar shift,
1162 * vector reinsertion. Should not be an issue on any non-x86 cpu
1163 * with a vector instruction set.
1164 * On cpus with AMD's XOP this should also be unnecessary but I'm
1165 * not sure if llvm would emit this with current flags.
1167 LLVMValueRef const127
, const23
, lf
;
1168 struct lp_type ftype
;
1169 struct lp_build_context fbld
;
1170 ftype
= lp_type_float_vec(32, bld
->type
.length
* bld
->type
.width
);
1171 lp_build_context_init(&fbld
, bld
->gallivm
, ftype
);
1172 const127
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, 127);
1173 const23
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, 23);
1175 /* calculate 2^(-level) float */
1176 lf
= lp_build_sub(bld
, const127
, level
);
1177 lf
= lp_build_shl(bld
, lf
, const23
);
1178 lf
= LLVMBuildBitCast(builder
, lf
, fbld
.vec_type
, "");
1180 /* finish shift operation by doing float mul */
1181 base_size
= lp_build_int_to_float(&fbld
, base_size
);
1182 size
= lp_build_mul(&fbld
, base_size
, lf
);
1184 * do the max also with floats because
1185 * a) non-emulated int max requires sse41
1186 * (this is actually a lie as we could cast to 16bit values
1187 * as 16bit is sufficient and 16bit int max is sse2)
1188 * b) with avx we can do int max 4-wide but float max 8-wide
1190 size
= lp_build_max(&fbld
, size
, fbld
.one
);
1191 size
= lp_build_itrunc(&fbld
, size
);
1199 * Dereference stride_array[mipmap_level] array to get a stride.
1200 * Return stride as a vector.
1203 lp_build_get_level_stride_vec(struct lp_build_sample_context
*bld
,
1204 LLVMValueRef stride_array
, LLVMValueRef level
)
1206 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1207 LLVMValueRef indexes
[2], stride
, stride1
;
1208 indexes
[0] = lp_build_const_int32(bld
->gallivm
, 0);
1209 if (bld
->num_mips
== 1) {
1211 stride1
= LLVMBuildGEP(builder
, stride_array
, indexes
, 2, "");
1212 stride1
= LLVMBuildLoad(builder
, stride1
, "");
1213 stride
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, stride1
);
1215 else if (bld
->num_mips
== bld
->coord_bld
.type
.length
/ 4) {
1216 LLVMValueRef stride1
;
1219 stride
= bld
->int_coord_bld
.undef
;
1220 for (i
= 0; i
< bld
->num_mips
; i
++) {
1221 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1222 LLVMValueRef indexo
= lp_build_const_int32(bld
->gallivm
, 4 * i
);
1223 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
1224 stride1
= LLVMBuildGEP(builder
, stride_array
, indexes
, 2, "");
1225 stride1
= LLVMBuildLoad(builder
, stride1
, "");
1226 stride
= LLVMBuildInsertElement(builder
, stride
, stride1
, indexo
, "");
1228 stride
= lp_build_swizzle_scalar_aos(&bld
->int_coord_bld
, stride
, 0, 4);
1231 LLVMValueRef stride1
;
1234 assert (bld
->num_mips
== bld
->coord_bld
.type
.length
);
1236 stride
= bld
->int_coord_bld
.undef
;
1237 for (i
= 0; i
< bld
->coord_bld
.type
.length
; i
++) {
1238 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1239 indexes
[1] = LLVMBuildExtractElement(builder
, level
, indexi
, "");
1240 stride1
= LLVMBuildGEP(builder
, stride_array
, indexes
, 2, "");
1241 stride1
= LLVMBuildLoad(builder
, stride1
, "");
1242 stride
= LLVMBuildInsertElement(builder
, stride
, stride1
, indexi
, "");
1250 * When sampling a mipmap, we need to compute the width, height, depth
1251 * of the source levels from the level indexes. This helper function
1255 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
1256 LLVMValueRef ilevel
,
1257 LLVMValueRef
*out_size
,
1258 LLVMValueRef
*row_stride_vec
,
1259 LLVMValueRef
*img_stride_vec
)
1261 const unsigned dims
= bld
->dims
;
1262 LLVMValueRef ilevel_vec
;
1265 * Compute width, height, depth at mipmap level 'ilevel'
1267 if (bld
->num_mips
== 1) {
1268 ilevel_vec
= lp_build_broadcast_scalar(&bld
->int_size_bld
, ilevel
);
1269 *out_size
= lp_build_minify(&bld
->int_size_bld
, bld
->int_size
, ilevel_vec
, TRUE
);
1272 LLVMValueRef int_size_vec
;
1273 LLVMValueRef tmp
[LP_MAX_VECTOR_LENGTH
];
1274 unsigned num_quads
= bld
->coord_bld
.type
.length
/ 4;
1277 if (bld
->num_mips
== num_quads
) {
1279 * XXX: this should be #ifndef SANE_INSTRUCTION_SET.
1280 * intel "forgot" the variable shift count instruction until avx2.
1281 * A harmless 8x32 shift gets translated into 32 instructions
1282 * (16 extracts, 8 scalar shifts, 8 inserts), llvm is apparently
1283 * unable to recognize if there are really just 2 different shift
1284 * count values. So do the shift 4-wide before expansion.
1286 struct lp_build_context bld4
;
1287 struct lp_type type4
;
1289 type4
= bld
->int_coord_bld
.type
;
1292 lp_build_context_init(&bld4
, bld
->gallivm
, type4
);
1294 if (bld
->dims
== 1) {
1295 assert(bld
->int_size_in_bld
.type
.length
== 1);
1296 int_size_vec
= lp_build_broadcast_scalar(&bld4
,
1300 assert(bld
->int_size_in_bld
.type
.length
== 4);
1301 int_size_vec
= bld
->int_size
;
1304 for (i
= 0; i
< num_quads
; i
++) {
1305 LLVMValueRef ileveli
;
1306 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1308 ileveli
= lp_build_extract_broadcast(bld
->gallivm
,
1309 bld
->leveli_bld
.type
,
1313 tmp
[i
] = lp_build_minify(&bld4
, int_size_vec
, ileveli
, TRUE
);
1316 * out_size is [w0, h0, d0, _, w1, h1, d1, _, ...] vector for dims > 1,
1317 * [w0, w0, w0, w0, w1, w1, w1, w1, ...] otherwise.
1319 *out_size
= lp_build_concat(bld
->gallivm
,
1325 /* FIXME: this is terrible and results in _huge_ vector
1326 * (for the dims > 1 case).
1327 * Should refactor this (together with extract_image_sizes) and do
1328 * something more useful. Could for instance if we have width,height
1329 * with 4-wide vector pack all elements into a 8xi16 vector
1330 * (on which we can still do useful math) instead of using a 16xi32
1332 * For dims == 1 this will create [w0, w1, w2, w3, ...] vector.
1333 * For dims > 1 this will create [w0, h0, d0, _, w1, h1, d1, _, ...] vector.
1335 assert(bld
->num_mips
== bld
->coord_bld
.type
.length
);
1336 if (bld
->dims
== 1) {
1337 assert(bld
->int_size_in_bld
.type
.length
== 1);
1338 int_size_vec
= lp_build_broadcast_scalar(&bld
->int_coord_bld
,
1340 *out_size
= lp_build_minify(&bld
->int_coord_bld
, int_size_vec
, ilevel
, FALSE
);
1343 LLVMValueRef ilevel1
;
1344 for (i
= 0; i
< bld
->num_mips
; i
++) {
1345 LLVMValueRef indexi
= lp_build_const_int32(bld
->gallivm
, i
);
1346 ilevel1
= lp_build_extract_broadcast(bld
->gallivm
, bld
->int_coord_type
,
1347 bld
->int_size_in_bld
.type
, ilevel
, indexi
);
1348 tmp
[i
] = bld
->int_size
;
1349 tmp
[i
] = lp_build_minify(&bld
->int_size_in_bld
, tmp
[i
], ilevel1
, TRUE
);
1351 *out_size
= lp_build_concat(bld
->gallivm
, tmp
,
1352 bld
->int_size_in_bld
.type
,
1359 *row_stride_vec
= lp_build_get_level_stride_vec(bld
,
1360 bld
->row_stride_array
,
1363 if (dims
== 3 || has_layer_coord(bld
->static_texture_state
->target
)) {
1364 *img_stride_vec
= lp_build_get_level_stride_vec(bld
,
1365 bld
->img_stride_array
,
1372 * Extract and broadcast texture size.
1374 * @param size_type type of the texture size vector (either
1375 * bld->int_size_type or bld->float_size_type)
1376 * @param coord_type type of the texture size vector (either
1377 * bld->int_coord_type or bld->coord_type)
1378 * @param size vector with the texture size (width, height, depth)
1381 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
1382 struct lp_build_context
*size_bld
,
1383 struct lp_type coord_type
,
1385 LLVMValueRef
*out_width
,
1386 LLVMValueRef
*out_height
,
1387 LLVMValueRef
*out_depth
)
1389 const unsigned dims
= bld
->dims
;
1390 LLVMTypeRef i32t
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
1391 struct lp_type size_type
= size_bld
->type
;
1393 if (bld
->num_mips
== 1) {
1394 *out_width
= lp_build_extract_broadcast(bld
->gallivm
,
1398 LLVMConstInt(i32t
, 0, 0));
1400 *out_height
= lp_build_extract_broadcast(bld
->gallivm
,
1404 LLVMConstInt(i32t
, 1, 0));
1406 *out_depth
= lp_build_extract_broadcast(bld
->gallivm
,
1410 LLVMConstInt(i32t
, 2, 0));
1415 unsigned num_quads
= bld
->coord_bld
.type
.length
/ 4;
1420 else if (bld
->num_mips
== num_quads
) {
1421 *out_width
= lp_build_swizzle_scalar_aos(size_bld
, size
, 0, 4);
1423 *out_height
= lp_build_swizzle_scalar_aos(size_bld
, size
, 1, 4);
1425 *out_depth
= lp_build_swizzle_scalar_aos(size_bld
, size
, 2, 4);
1430 assert(bld
->num_mips
== bld
->coord_type
.length
);
1431 *out_width
= lp_build_pack_aos_scalars(bld
->gallivm
, size_type
,
1432 coord_type
, size
, 0);
1434 *out_height
= lp_build_pack_aos_scalars(bld
->gallivm
, size_type
,
1435 coord_type
, size
, 1);
1437 *out_depth
= lp_build_pack_aos_scalars(bld
->gallivm
, size_type
,
1438 coord_type
, size
, 2);
1447 * Unnormalize coords.
1449 * @param flt_size vector with the integer texture size (width, height, depth)
1452 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
1453 LLVMValueRef flt_size
,
1458 const unsigned dims
= bld
->dims
;
1460 LLVMValueRef height
= NULL
;
1461 LLVMValueRef depth
= NULL
;
1463 lp_build_extract_image_sizes(bld
,
1464 &bld
->float_size_bld
,
1471 /* s = s * width, t = t * height */
1472 *s
= lp_build_mul(&bld
->coord_bld
, *s
, width
);
1474 *t
= lp_build_mul(&bld
->coord_bld
, *t
, height
);
1476 *r
= lp_build_mul(&bld
->coord_bld
, *r
, depth
);
1482 * Generate new coords and faces for cubemap texels falling off the face.
1484 * @param face face (center) of the pixel
1485 * @param x0 lower x coord
1486 * @param x1 higher x coord (must be x0 + 1)
1487 * @param y0 lower y coord
1488 * @param y1 higher y coord (must be x0 + 1)
1489 * @param max_coord texture cube (level) size - 1
1490 * @param next_faces new face values when falling off
1491 * @param next_xcoords new x coord values when falling off
1492 * @param next_ycoords new y coord values when falling off
1494 * The arrays hold the new values when under/overflow of
1495 * lower x, higher x, lower y, higher y coord would occur (in this order).
1496 * next_xcoords/next_ycoords have two entries each (for both new lower and
1500 lp_build_cube_new_coords(struct lp_build_context
*ivec_bld
,
1506 LLVMValueRef max_coord
,
1507 LLVMValueRef next_faces
[4],
1508 LLVMValueRef next_xcoords
[4][2],
1509 LLVMValueRef next_ycoords
[4][2])
1512 * Lookup tables aren't nice for simd code hence try some logic here.
1513 * (Note that while it would not be necessary to do per-sample (4) lookups
1514 * when using a LUT as it's impossible that texels fall off of positive
1515 * and negative edges simultaneously, it would however be necessary to
1516 * do 2 lookups for corner handling as in this case texels both fall off
1520 * Next faces (for face 012345):
1525 * Hence nfx+ (and nfy+) == nfx- (nfy-) xor 1
1526 * nfx-: face > 1 ? (face == 5 ? 0 : 1) : (4 + face & 1)
1527 * nfy+: face & ~4 > 1 ? face + 2 : 3;
1528 * This could also use pshufb instead, but would need (manually coded)
1529 * ssse3 intrinsic (llvm won't do non-constant shuffles).
1531 struct gallivm_state
*gallivm
= ivec_bld
->gallivm
;
1532 LLVMValueRef sel
, sel_f2345
, sel_f23
, sel_f2
, tmpsel
, tmp
;
1533 LLVMValueRef faceand1
, sel_fand1
, maxmx0
, maxmx1
, maxmy0
, maxmy1
;
1534 LLVMValueRef c2
= lp_build_const_int_vec(gallivm
, ivec_bld
->type
, 2);
1535 LLVMValueRef c3
= lp_build_const_int_vec(gallivm
, ivec_bld
->type
, 3);
1536 LLVMValueRef c4
= lp_build_const_int_vec(gallivm
, ivec_bld
->type
, 4);
1537 LLVMValueRef c5
= lp_build_const_int_vec(gallivm
, ivec_bld
->type
, 5);
1539 sel
= lp_build_cmp(ivec_bld
, PIPE_FUNC_EQUAL
, face
, c5
);
1540 tmpsel
= lp_build_select(ivec_bld
, sel
, ivec_bld
->zero
, ivec_bld
->one
);
1541 sel_f2345
= lp_build_cmp(ivec_bld
, PIPE_FUNC_GREATER
, face
, ivec_bld
->one
);
1542 faceand1
= lp_build_and(ivec_bld
, face
, ivec_bld
->one
);
1543 tmp
= lp_build_add(ivec_bld
, faceand1
, c4
);
1544 next_faces
[0] = lp_build_select(ivec_bld
, sel_f2345
, tmpsel
, tmp
);
1545 next_faces
[1] = lp_build_xor(ivec_bld
, next_faces
[0], ivec_bld
->one
);
1547 tmp
= lp_build_andnot(ivec_bld
, face
, c4
);
1548 sel_f23
= lp_build_cmp(ivec_bld
, PIPE_FUNC_GREATER
, tmp
, ivec_bld
->one
);
1549 tmp
= lp_build_add(ivec_bld
, face
, c2
);
1550 next_faces
[3] = lp_build_select(ivec_bld
, sel_f23
, tmp
, c3
);
1551 next_faces
[2] = lp_build_xor(ivec_bld
, next_faces
[3], ivec_bld
->one
);
1554 * new xcoords (for face 012345):
1555 * x < 0.0 : max max t max-t max max
1556 * x >= 1.0 : 0 0 max-t t 0 0
1557 * y < 0.0 : max 0 max-s s s max-s
1558 * y >= 1.0 : max 0 s max-s s max-s
1560 * ncx[1] = face & ~4 > 1 ? (face == 2 ? max-t : t) : 0
1561 * ncx[0] = max - ncx[1]
1562 * ncx[3] = face > 1 ? (face & 1 ? max-s : s) : (face & 1) ? 0 : max
1563 * ncx[2] = face & ~4 > 1 ? max - ncx[3] : ncx[3]
1565 sel_f2
= lp_build_cmp(ivec_bld
, PIPE_FUNC_EQUAL
, face
, c2
);
1566 maxmy0
= lp_build_sub(ivec_bld
, max_coord
, y0
);
1567 tmp
= lp_build_select(ivec_bld
, sel_f2
, maxmy0
, y0
);
1568 next_xcoords
[1][0] = lp_build_select(ivec_bld
, sel_f23
, tmp
, ivec_bld
->zero
);
1569 next_xcoords
[0][0] = lp_build_sub(ivec_bld
, max_coord
, next_xcoords
[1][0]);
1570 maxmy1
= lp_build_sub(ivec_bld
, max_coord
, y1
);
1571 tmp
= lp_build_select(ivec_bld
, sel_f2
, maxmy1
, y1
);
1572 next_xcoords
[1][1] = lp_build_select(ivec_bld
, sel_f23
, tmp
, ivec_bld
->zero
);
1573 next_xcoords
[0][1] = lp_build_sub(ivec_bld
, max_coord
, next_xcoords
[1][1]);
1575 sel_fand1
= lp_build_cmp(ivec_bld
, PIPE_FUNC_EQUAL
, faceand1
, ivec_bld
->one
);
1577 tmpsel
= lp_build_select(ivec_bld
, sel_fand1
, ivec_bld
->zero
, max_coord
);
1578 maxmx0
= lp_build_sub(ivec_bld
, max_coord
, x0
);
1579 tmp
= lp_build_select(ivec_bld
, sel_fand1
, maxmx0
, x0
);
1580 next_xcoords
[3][0] = lp_build_select(ivec_bld
, sel_f2345
, tmp
, tmpsel
);
1581 tmp
= lp_build_sub(ivec_bld
, max_coord
, next_xcoords
[3][0]);
1582 next_xcoords
[2][0] = lp_build_select(ivec_bld
, sel_f23
, tmp
, next_xcoords
[3][0]);
1583 maxmx1
= lp_build_sub(ivec_bld
, max_coord
, x1
);
1584 tmp
= lp_build_select(ivec_bld
, sel_fand1
, maxmx1
, x1
);
1585 next_xcoords
[3][1] = lp_build_select(ivec_bld
, sel_f2345
, tmp
, tmpsel
);
1586 tmp
= lp_build_sub(ivec_bld
, max_coord
, next_xcoords
[3][1]);
1587 next_xcoords
[2][1] = lp_build_select(ivec_bld
, sel_f23
, tmp
, next_xcoords
[3][1]);
1590 * new ycoords (for face 012345):
1591 * x < 0.0 : t t 0 max t t
1592 * x >= 1.0 : t t 0 max t t
1593 * y < 0.0 : max-s s 0 max max 0
1594 * y >= 1.0 : s max-s 0 max 0 max
1596 * ncy[0] = face & ~4 > 1 ? (face == 2 ? 0 : max) : t
1598 * ncy[3] = face > 1 ? (face & 1 ? max : 0) : (face & 1) ? max-s : max
1599 * ncx[2] = face & ~4 > 1 ? max - ncx[3] : ncx[3]
1601 tmp
= lp_build_select(ivec_bld
, sel_f2
, ivec_bld
->zero
, max_coord
);
1602 next_ycoords
[0][0] = lp_build_select(ivec_bld
, sel_f23
, tmp
, y0
);
1603 next_ycoords
[1][0] = next_ycoords
[0][0];
1604 next_ycoords
[0][1] = lp_build_select(ivec_bld
, sel_f23
, tmp
, y1
);
1605 next_ycoords
[1][1] = next_ycoords
[0][1];
1607 tmpsel
= lp_build_select(ivec_bld
, sel_fand1
, maxmx0
, x0
);
1608 tmp
= lp_build_select(ivec_bld
, sel_fand1
, max_coord
, ivec_bld
->zero
);
1609 next_ycoords
[3][0] = lp_build_select(ivec_bld
, sel_f2345
, tmp
, tmpsel
);
1610 tmp
= lp_build_sub(ivec_bld
, max_coord
, next_ycoords
[3][0]);
1611 next_ycoords
[2][0] = lp_build_select(ivec_bld
, sel_f23
, next_ycoords
[3][0], tmp
);
1612 tmpsel
= lp_build_select(ivec_bld
, sel_fand1
, maxmx1
, x1
);
1613 tmp
= lp_build_select(ivec_bld
, sel_fand1
, max_coord
, ivec_bld
->zero
);
1614 next_ycoords
[3][1] = lp_build_select(ivec_bld
, sel_f2345
, tmp
, tmpsel
);
1615 tmp
= lp_build_sub(ivec_bld
, max_coord
, next_ycoords
[3][1]);
1616 next_ycoords
[2][1] = lp_build_select(ivec_bld
, sel_f23
, next_ycoords
[3][1], tmp
);
1620 /** Helper used by lp_build_cube_lookup() */
1622 lp_build_cube_imapos(struct lp_build_context
*coord_bld
, LLVMValueRef coord
)
1624 /* ima = +0.5 / abs(coord); */
1625 LLVMValueRef posHalf
= lp_build_const_vec(coord_bld
->gallivm
, coord_bld
->type
, 0.5);
1626 LLVMValueRef absCoord
= lp_build_abs(coord_bld
, coord
);
1627 LLVMValueRef ima
= lp_build_div(coord_bld
, posHalf
, absCoord
);
1632 /** Helper for doing 3-wise selection.
1633 * Returns sel1 ? val2 : (sel0 ? val0 : val1).
1636 lp_build_select3(struct lp_build_context
*sel_bld
,
1644 tmp
= lp_build_select(sel_bld
, sel0
, val0
, val1
);
1645 return lp_build_select(sel_bld
, sel1
, val2
, tmp
);
1650 * Generate code to do cube face selection and compute per-face texcoords.
1653 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
1654 LLVMValueRef
*coords
,
1655 const struct lp_derivatives
*derivs_in
, /* optional */
1657 struct lp_derivatives
*derivs_out
, /* optional */
1658 boolean need_derivs
)
1660 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
1661 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1662 struct gallivm_state
*gallivm
= bld
->gallivm
;
1663 LLVMValueRef si
, ti
, ri
;
1666 * Do per-pixel face selection. We cannot however (as we used to do)
1667 * simply calculate the derivs afterwards (which is very bogus for
1668 * explicit derivs btw) because the values would be "random" when
1669 * not all pixels lie on the same face. So what we do here is just
1670 * calculate the derivatives after scaling the coords by the absolute
1671 * value of the inverse major axis, and essentially do rho calculation
1672 * steps as if it were a 3d texture. This is perfect if all pixels hit
1673 * the same face, but not so great at edges, I believe the max error
1674 * should be sqrt(2) with no_rho_approx or 2 otherwise (essentially measuring
1675 * the 3d distance between 2 points on the cube instead of measuring up/down
1676 * the edge). Still this is possibly a win over just selecting the same face
1677 * for all pixels. Unfortunately, something like that doesn't work for
1678 * explicit derivatives.
1680 struct lp_build_context
*cint_bld
= &bld
->int_coord_bld
;
1681 struct lp_type intctype
= cint_bld
->type
;
1682 LLVMTypeRef coord_vec_type
= coord_bld
->vec_type
;
1683 LLVMTypeRef cint_vec_type
= cint_bld
->vec_type
;
1684 LLVMValueRef as
, at
, ar
, face
, face_s
, face_t
;
1685 LLVMValueRef as_ge_at
, maxasat
, ar_ge_as_at
;
1686 LLVMValueRef snewx
, tnewx
, snewy
, tnewy
, snewz
, tnewz
;
1687 LLVMValueRef tnegi
, rnegi
;
1688 LLVMValueRef ma
, mai
, signma
, signmabit
, imahalfpos
;
1689 LLVMValueRef posHalf
= lp_build_const_vec(gallivm
, coord_bld
->type
, 0.5);
1690 LLVMValueRef signmask
= lp_build_const_int_vec(gallivm
, intctype
,
1691 1LL << (intctype
.width
- 1));
1692 LLVMValueRef signshift
= lp_build_const_int_vec(gallivm
, intctype
,
1694 LLVMValueRef facex
= lp_build_const_int_vec(gallivm
, intctype
, PIPE_TEX_FACE_POS_X
);
1695 LLVMValueRef facey
= lp_build_const_int_vec(gallivm
, intctype
, PIPE_TEX_FACE_POS_Y
);
1696 LLVMValueRef facez
= lp_build_const_int_vec(gallivm
, intctype
, PIPE_TEX_FACE_POS_Z
);
1697 LLVMValueRef s
= coords
[0];
1698 LLVMValueRef t
= coords
[1];
1699 LLVMValueRef r
= coords
[2];
1701 assert(PIPE_TEX_FACE_NEG_X
== PIPE_TEX_FACE_POS_X
+ 1);
1702 assert(PIPE_TEX_FACE_NEG_Y
== PIPE_TEX_FACE_POS_Y
+ 1);
1703 assert(PIPE_TEX_FACE_NEG_Z
== PIPE_TEX_FACE_POS_Z
+ 1);
1706 * get absolute value (for x/y/z face selection) and sign bit
1707 * (for mirroring minor coords and pos/neg face selection)
1708 * of the original coords.
1710 as
= lp_build_abs(&bld
->coord_bld
, s
);
1711 at
= lp_build_abs(&bld
->coord_bld
, t
);
1712 ar
= lp_build_abs(&bld
->coord_bld
, r
);
1715 * major face determination: select x if x > y else select y
1716 * select z if z >= max(x,y) else select previous result
1717 * if some axis are the same we chose z over y, y over x - the
1718 * dx10 spec seems to ask for it while OpenGL doesn't care (if we
1719 * wouldn't care could save a select or two if using different
1720 * compares and doing at_g_as_ar last since tnewx and tnewz are the
1723 as_ge_at
= lp_build_cmp(coord_bld
, PIPE_FUNC_GREATER
, as
, at
);
1724 maxasat
= lp_build_max(coord_bld
, as
, at
);
1725 ar_ge_as_at
= lp_build_cmp(coord_bld
, PIPE_FUNC_GEQUAL
, ar
, maxasat
);
1727 if (need_derivs
&& (derivs_in
|| (bld
->no_quad_lod
&& bld
->no_rho_approx
))) {
1729 * XXX: This is really really complex.
1730 * It is a bit overkill to use this for implicit derivatives as well,
1731 * no way this is worth the cost in practice, but seems to be the
1732 * only way for getting accurate and per-pixel lod values.
1734 LLVMValueRef ima
, imahalf
, tmp
, ddx
[3], ddy
[3];
1735 LLVMValueRef madx
, mady
, madxdivma
, madydivma
;
1736 LLVMValueRef sdxi
, tdxi
, rdxi
, sdyi
, tdyi
, rdyi
;
1737 LLVMValueRef tdxnegi
, rdxnegi
, tdynegi
, rdynegi
;
1738 LLVMValueRef sdxnewx
, sdxnewy
, sdxnewz
, tdxnewx
, tdxnewy
, tdxnewz
;
1739 LLVMValueRef sdynewx
, sdynewy
, sdynewz
, tdynewx
, tdynewy
, tdynewz
;
1740 LLVMValueRef face_sdx
, face_tdx
, face_sdy
, face_tdy
;
1742 * s = 1/2 * ( sc / ma + 1)
1743 * t = 1/2 * ( tc / ma + 1)
1745 * s' = 1/2 * (sc' * ma - sc * ma') / ma^2
1746 * t' = 1/2 * (tc' * ma - tc * ma') / ma^2
1748 * dx.s = 0.5 * (dx.sc - sc * dx.ma / ma) / ma
1749 * dx.t = 0.5 * (dx.tc - tc * dx.ma / ma) / ma
1750 * dy.s = 0.5 * (dy.sc - sc * dy.ma / ma) / ma
1751 * dy.t = 0.5 * (dy.tc - tc * dy.ma / ma) / ma
1754 /* select ma, calculate ima */
1755 ma
= lp_build_select3(coord_bld
, as_ge_at
, ar_ge_as_at
, s
, t
, r
);
1756 mai
= LLVMBuildBitCast(builder
, ma
, cint_vec_type
, "");
1757 signmabit
= LLVMBuildAnd(builder
, mai
, signmask
, "");
1758 ima
= lp_build_div(coord_bld
, coord_bld
->one
, ma
);
1759 imahalf
= lp_build_mul(coord_bld
, posHalf
, ima
);
1760 imahalfpos
= lp_build_abs(coord_bld
, imahalf
);
1763 ddx
[0] = lp_build_ddx(coord_bld
, s
);
1764 ddx
[1] = lp_build_ddx(coord_bld
, t
);
1765 ddx
[2] = lp_build_ddx(coord_bld
, r
);
1766 ddy
[0] = lp_build_ddy(coord_bld
, s
);
1767 ddy
[1] = lp_build_ddy(coord_bld
, t
);
1768 ddy
[2] = lp_build_ddy(coord_bld
, r
);
1771 ddx
[0] = derivs_in
->ddx
[0];
1772 ddx
[1] = derivs_in
->ddx
[1];
1773 ddx
[2] = derivs_in
->ddx
[2];
1774 ddy
[0] = derivs_in
->ddy
[0];
1775 ddy
[1] = derivs_in
->ddy
[1];
1776 ddy
[2] = derivs_in
->ddy
[2];
1779 /* select major derivatives */
1780 madx
= lp_build_select3(coord_bld
, as_ge_at
, ar_ge_as_at
, ddx
[0], ddx
[1], ddx
[2]);
1781 mady
= lp_build_select3(coord_bld
, as_ge_at
, ar_ge_as_at
, ddy
[0], ddy
[1], ddy
[2]);
1783 si
= LLVMBuildBitCast(builder
, s
, cint_vec_type
, "");
1784 ti
= LLVMBuildBitCast(builder
, t
, cint_vec_type
, "");
1785 ri
= LLVMBuildBitCast(builder
, r
, cint_vec_type
, "");
1787 sdxi
= LLVMBuildBitCast(builder
, ddx
[0], cint_vec_type
, "");
1788 tdxi
= LLVMBuildBitCast(builder
, ddx
[1], cint_vec_type
, "");
1789 rdxi
= LLVMBuildBitCast(builder
, ddx
[2], cint_vec_type
, "");
1791 sdyi
= LLVMBuildBitCast(builder
, ddy
[0], cint_vec_type
, "");
1792 tdyi
= LLVMBuildBitCast(builder
, ddy
[1], cint_vec_type
, "");
1793 rdyi
= LLVMBuildBitCast(builder
, ddy
[2], cint_vec_type
, "");
1796 * compute all possible new s/t coords, which does the mirroring,
1797 * and do the same for derivs minor axes.
1798 * snewx = signma * -r;
1801 * tnewy = signma * r;
1802 * snewz = signma * s;
1805 tnegi
= LLVMBuildXor(builder
, ti
, signmask
, "");
1806 rnegi
= LLVMBuildXor(builder
, ri
, signmask
, "");
1807 tdxnegi
= LLVMBuildXor(builder
, tdxi
, signmask
, "");
1808 rdxnegi
= LLVMBuildXor(builder
, rdxi
, signmask
, "");
1809 tdynegi
= LLVMBuildXor(builder
, tdyi
, signmask
, "");
1810 rdynegi
= LLVMBuildXor(builder
, rdyi
, signmask
, "");
1812 snewx
= LLVMBuildXor(builder
, signmabit
, rnegi
, "");
1814 sdxnewx
= LLVMBuildXor(builder
, signmabit
, rdxnegi
, "");
1816 sdynewx
= LLVMBuildXor(builder
, signmabit
, rdynegi
, "");
1820 tnewy
= LLVMBuildXor(builder
, signmabit
, ri
, "");
1822 tdxnewy
= LLVMBuildXor(builder
, signmabit
, rdxi
, "");
1824 tdynewy
= LLVMBuildXor(builder
, signmabit
, rdyi
, "");
1826 snewz
= LLVMBuildXor(builder
, signmabit
, si
, "");
1828 sdxnewz
= LLVMBuildXor(builder
, signmabit
, sdxi
, "");
1830 sdynewz
= LLVMBuildXor(builder
, signmabit
, sdyi
, "");
1833 /* select the mirrored values */
1834 face
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, facex
, facey
, facez
);
1835 face_s
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, snewx
, snewy
, snewz
);
1836 face_t
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, tnewx
, tnewy
, tnewz
);
1837 face_sdx
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, sdxnewx
, sdxnewy
, sdxnewz
);
1838 face_tdx
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, tdxnewx
, tdxnewy
, tdxnewz
);
1839 face_sdy
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, sdynewx
, sdynewy
, sdynewz
);
1840 face_tdy
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, tdynewx
, tdynewy
, tdynewz
);
1842 face_s
= LLVMBuildBitCast(builder
, face_s
, coord_vec_type
, "");
1843 face_t
= LLVMBuildBitCast(builder
, face_t
, coord_vec_type
, "");
1844 face_sdx
= LLVMBuildBitCast(builder
, face_sdx
, coord_vec_type
, "");
1845 face_tdx
= LLVMBuildBitCast(builder
, face_tdx
, coord_vec_type
, "");
1846 face_sdy
= LLVMBuildBitCast(builder
, face_sdy
, coord_vec_type
, "");
1847 face_tdy
= LLVMBuildBitCast(builder
, face_tdy
, coord_vec_type
, "");
1849 /* deriv math, dx.s = 0.5 * (dx.sc - sc * dx.ma / ma) / ma */
1850 madxdivma
= lp_build_mul(coord_bld
, madx
, ima
);
1851 tmp
= lp_build_mul(coord_bld
, madxdivma
, face_s
);
1852 tmp
= lp_build_sub(coord_bld
, face_sdx
, tmp
);
1853 derivs_out
->ddx
[0] = lp_build_mul(coord_bld
, tmp
, imahalf
);
1855 /* dx.t = 0.5 * (dx.tc - tc * dx.ma / ma) / ma */
1856 tmp
= lp_build_mul(coord_bld
, madxdivma
, face_t
);
1857 tmp
= lp_build_sub(coord_bld
, face_tdx
, tmp
);
1858 derivs_out
->ddx
[1] = lp_build_mul(coord_bld
, tmp
, imahalf
);
1860 /* dy.s = 0.5 * (dy.sc - sc * dy.ma / ma) / ma */
1861 madydivma
= lp_build_mul(coord_bld
, mady
, ima
);
1862 tmp
= lp_build_mul(coord_bld
, madydivma
, face_s
);
1863 tmp
= lp_build_sub(coord_bld
, face_sdy
, tmp
);
1864 derivs_out
->ddy
[0] = lp_build_mul(coord_bld
, tmp
, imahalf
);
1866 /* dy.t = 0.5 * (dy.tc - tc * dy.ma / ma) / ma */
1867 tmp
= lp_build_mul(coord_bld
, madydivma
, face_t
);
1868 tmp
= lp_build_sub(coord_bld
, face_tdy
, tmp
);
1869 derivs_out
->ddy
[1] = lp_build_mul(coord_bld
, tmp
, imahalf
);
1871 signma
= LLVMBuildLShr(builder
, mai
, signshift
, "");
1872 coords
[2] = LLVMBuildOr(builder
, face
, signma
, "face");
1874 /* project coords */
1875 face_s
= lp_build_mul(coord_bld
, face_s
, imahalfpos
);
1876 face_t
= lp_build_mul(coord_bld
, face_t
, imahalfpos
);
1878 coords
[0] = lp_build_add(coord_bld
, face_s
, posHalf
);
1879 coords
[1] = lp_build_add(coord_bld
, face_t
, posHalf
);
1884 else if (need_derivs
) {
1885 LLVMValueRef ddx_ddy
[2], tmp
[3], rho_vec
;
1886 static const unsigned char swizzle0
[] = { /* no-op swizzle */
1887 0, LP_BLD_SWIZZLE_DONTCARE
,
1888 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1890 static const unsigned char swizzle1
[] = {
1891 1, LP_BLD_SWIZZLE_DONTCARE
,
1892 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1894 static const unsigned char swizzle01
[] = { /* no-op swizzle */
1896 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1898 static const unsigned char swizzle23
[] = {
1900 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1902 static const unsigned char swizzle02
[] = {
1904 LP_BLD_SWIZZLE_DONTCARE
, LP_BLD_SWIZZLE_DONTCARE
1908 * scale the s/t/r coords pre-select/mirror so we can calculate
1909 * "reasonable" derivs.
1911 ma
= lp_build_select3(coord_bld
, as_ge_at
, ar_ge_as_at
, s
, t
, r
);
1912 imahalfpos
= lp_build_cube_imapos(coord_bld
, ma
);
1913 s
= lp_build_mul(coord_bld
, s
, imahalfpos
);
1914 t
= lp_build_mul(coord_bld
, t
, imahalfpos
);
1915 r
= lp_build_mul(coord_bld
, r
, imahalfpos
);
1918 * This isn't quite the same as the "ordinary" (3d deriv) path since we
1919 * know the texture is square which simplifies things (we can omit the
1920 * size mul which happens very early completely here and do it at the
1922 * Also always do calculations according to GALLIVM_DEBUG_NO_RHO_APPROX
1923 * since the error can get quite big otherwise at edges.
1924 * (With no_rho_approx max error is sqrt(2) at edges, same as it is
1925 * without no_rho_approx for 2d textures, otherwise it would be factor 2.)
1927 ddx_ddy
[0] = lp_build_packed_ddx_ddy_twocoord(coord_bld
, s
, t
);
1928 ddx_ddy
[1] = lp_build_packed_ddx_ddy_onecoord(coord_bld
, r
);
1930 ddx_ddy
[0] = lp_build_mul(coord_bld
, ddx_ddy
[0], ddx_ddy
[0]);
1931 ddx_ddy
[1] = lp_build_mul(coord_bld
, ddx_ddy
[1], ddx_ddy
[1]);
1933 tmp
[0] = lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle01
);
1934 tmp
[1] = lp_build_swizzle_aos(coord_bld
, ddx_ddy
[0], swizzle23
);
1935 tmp
[2] = lp_build_swizzle_aos(coord_bld
, ddx_ddy
[1], swizzle02
);
1937 rho_vec
= lp_build_add(coord_bld
, tmp
[0], tmp
[1]);
1938 rho_vec
= lp_build_add(coord_bld
, rho_vec
, tmp
[2]);
1940 tmp
[0] = lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle0
);
1941 tmp
[1] = lp_build_swizzle_aos(coord_bld
, rho_vec
, swizzle1
);
1942 *rho
= lp_build_max(coord_bld
, tmp
[0], tmp
[1]);
1946 ma
= lp_build_select3(coord_bld
, as_ge_at
, ar_ge_as_at
, s
, t
, r
);
1948 mai
= LLVMBuildBitCast(builder
, ma
, cint_vec_type
, "");
1949 signmabit
= LLVMBuildAnd(builder
, mai
, signmask
, "");
1951 si
= LLVMBuildBitCast(builder
, s
, cint_vec_type
, "");
1952 ti
= LLVMBuildBitCast(builder
, t
, cint_vec_type
, "");
1953 ri
= LLVMBuildBitCast(builder
, r
, cint_vec_type
, "");
1956 * compute all possible new s/t coords, which does the mirroring
1957 * snewx = signma * -r;
1960 * tnewy = signma * r;
1961 * snewz = signma * s;
1964 tnegi
= LLVMBuildXor(builder
, ti
, signmask
, "");
1965 rnegi
= LLVMBuildXor(builder
, ri
, signmask
, "");
1967 snewx
= LLVMBuildXor(builder
, signmabit
, rnegi
, "");
1971 tnewy
= LLVMBuildXor(builder
, signmabit
, ri
, "");
1973 snewz
= LLVMBuildXor(builder
, signmabit
, si
, "");
1976 /* select the mirrored values */
1977 face_s
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, snewx
, snewy
, snewz
);
1978 face_t
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, tnewx
, tnewy
, tnewz
);
1979 face
= lp_build_select3(cint_bld
, as_ge_at
, ar_ge_as_at
, facex
, facey
, facez
);
1981 face_s
= LLVMBuildBitCast(builder
, face_s
, coord_vec_type
, "");
1982 face_t
= LLVMBuildBitCast(builder
, face_t
, coord_vec_type
, "");
1984 /* add +1 for neg face */
1985 /* XXX with AVX probably want to use another select here -
1986 * as long as we ensure vblendvps gets used we can actually
1987 * skip the comparison and just use sign as a "mask" directly.
1989 signma
= LLVMBuildLShr(builder
, mai
, signshift
, "");
1990 coords
[2] = LLVMBuildOr(builder
, face
, signma
, "face");
1992 /* project coords */
1994 imahalfpos
= lp_build_cube_imapos(coord_bld
, ma
);
1995 face_s
= lp_build_mul(coord_bld
, face_s
, imahalfpos
);
1996 face_t
= lp_build_mul(coord_bld
, face_t
, imahalfpos
);
1999 coords
[0] = lp_build_add(coord_bld
, face_s
, posHalf
);
2000 coords
[1] = lp_build_add(coord_bld
, face_t
, posHalf
);
2005 * Compute the partial offset of a pixel block along an arbitrary axis.
2007 * @param coord coordinate in pixels
2008 * @param stride number of bytes between rows of successive pixel blocks
2009 * @param block_length number of pixels in a pixels block along the coordinate
2011 * @param out_offset resulting relative offset of the pixel block in bytes
2012 * @param out_subcoord resulting sub-block pixel coordinate
2015 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
2016 unsigned block_length
,
2018 LLVMValueRef stride
,
2019 LLVMValueRef
*out_offset
,
2020 LLVMValueRef
*out_subcoord
)
2022 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
2023 LLVMValueRef offset
;
2024 LLVMValueRef subcoord
;
2026 if (block_length
== 1) {
2027 subcoord
= bld
->zero
;
2031 * Pixel blocks have power of two dimensions. LLVM should convert the
2032 * rem/div to bit arithmetic.
2033 * TODO: Verify this.
2034 * It does indeed BUT it does transform it to scalar (and back) when doing so
2035 * (using roughly extract, shift/and, mov, unpack) (llvm 2.7).
2036 * The generated code looks seriously unfunny and is quite expensive.
2039 LLVMValueRef block_width
= lp_build_const_int_vec(bld
->type
, block_length
);
2040 subcoord
= LLVMBuildURem(builder
, coord
, block_width
, "");
2041 coord
= LLVMBuildUDiv(builder
, coord
, block_width
, "");
2043 unsigned logbase2
= util_logbase2(block_length
);
2044 LLVMValueRef block_shift
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, logbase2
);
2045 LLVMValueRef block_mask
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, block_length
- 1);
2046 subcoord
= LLVMBuildAnd(builder
, coord
, block_mask
, "");
2047 coord
= LLVMBuildLShr(builder
, coord
, block_shift
, "");
2051 offset
= lp_build_mul(bld
, coord
, stride
);
2054 assert(out_subcoord
);
2056 *out_offset
= offset
;
2057 *out_subcoord
= subcoord
;
2062 * Compute the offset of a pixel block.
2064 * x, y, z, y_stride, z_stride are vectors, and they refer to pixels.
2066 * Returns the relative offset and i,j sub-block coordinates
2069 lp_build_sample_offset(struct lp_build_context
*bld
,
2070 const struct util_format_description
*format_desc
,
2074 LLVMValueRef y_stride
,
2075 LLVMValueRef z_stride
,
2076 LLVMValueRef
*out_offset
,
2077 LLVMValueRef
*out_i
,
2078 LLVMValueRef
*out_j
)
2080 LLVMValueRef x_stride
;
2081 LLVMValueRef offset
;
2083 x_stride
= lp_build_const_vec(bld
->gallivm
, bld
->type
,
2084 format_desc
->block
.bits
/8);
2086 lp_build_sample_partial_offset(bld
,
2087 format_desc
->block
.width
,
2091 if (y
&& y_stride
) {
2092 LLVMValueRef y_offset
;
2093 lp_build_sample_partial_offset(bld
,
2094 format_desc
->block
.height
,
2097 offset
= lp_build_add(bld
, offset
, y_offset
);
2103 if (z
&& z_stride
) {
2104 LLVMValueRef z_offset
;
2106 lp_build_sample_partial_offset(bld
,
2107 1, /* pixel blocks are always 2D */
2110 offset
= lp_build_add(bld
, offset
, z_offset
);
2113 *out_offset
= offset
;