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"
50 * Bri-linear factor. Should be greater than one.
52 #define BRILINEAR_FACTOR 2
55 lp_build_minify(struct lp_build_context
*bld
,
56 LLVMValueRef base_size
,
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_state object with the gallium sampler
95 * The former is considered to be static and the later dynamic.
98 lp_sampler_static_state(struct lp_sampler_static_state
*state
,
99 const struct pipe_sampler_view
*view
,
100 const struct pipe_sampler_state
*sampler
)
102 const struct pipe_resource
*texture
= view
->texture
;
104 memset(state
, 0, sizeof *state
);
113 * We don't copy sampler state over unless it is actually enabled, to avoid
114 * spurious recompiles, as the sampler static state is part of the shader
117 * Ideally the state tracker or cso_cache module would make all state
118 * canonical, but until that happens it's better to be safe than sorry here.
120 * XXX: Actually there's much more than can be done here, especially
121 * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
124 state
->format
= view
->format
;
125 state
->swizzle_r
= view
->swizzle_r
;
126 state
->swizzle_g
= view
->swizzle_g
;
127 state
->swizzle_b
= view
->swizzle_b
;
128 state
->swizzle_a
= view
->swizzle_a
;
130 state
->target
= texture
->target
;
131 state
->pot_width
= util_is_power_of_two(texture
->width0
);
132 state
->pot_height
= util_is_power_of_two(texture
->height0
);
133 state
->pot_depth
= util_is_power_of_two(texture
->depth0
);
135 state
->wrap_s
= sampler
->wrap_s
;
136 state
->wrap_t
= sampler
->wrap_t
;
137 state
->wrap_r
= sampler
->wrap_r
;
138 state
->min_img_filter
= sampler
->min_img_filter
;
139 state
->mag_img_filter
= sampler
->mag_img_filter
;
141 if (view
->u
.tex
.last_level
&& sampler
->max_lod
> 0.0f
) {
142 state
->min_mip_filter
= sampler
->min_mip_filter
;
144 state
->min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
147 if (state
->min_mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
148 if (sampler
->lod_bias
!= 0.0f
) {
149 state
->lod_bias_non_zero
= 1;
152 /* If min_lod == max_lod we can greatly simplify mipmap selection.
153 * This is a case that occurs during automatic mipmap generation.
155 if (sampler
->min_lod
== sampler
->max_lod
) {
156 state
->min_max_lod_equal
= 1;
158 if (sampler
->min_lod
> 0.0f
) {
159 state
->apply_min_lod
= 1;
162 if (sampler
->max_lod
< (float)view
->u
.tex
.last_level
) {
163 state
->apply_max_lod
= 1;
168 state
->compare_mode
= sampler
->compare_mode
;
169 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
170 state
->compare_func
= sampler
->compare_func
;
173 state
->normalized_coords
= sampler
->normalized_coords
;
176 * FIXME: Handle the remainder of pipe_sampler_view.
182 * Generate code to compute coordinate gradient (rho).
183 * \param ddx partial derivatives of (s, t, r, q) with respect to X
184 * \param ddy partial derivatives of (s, t, r, q) with respect to Y
186 * XXX: The resulting rho is scalar, so we ignore all but the first element of
187 * derivatives that are passed by the shader.
190 lp_build_rho(struct lp_build_sample_context
*bld
,
192 const LLVMValueRef ddx
[4],
193 const LLVMValueRef ddy
[4])
195 struct lp_build_context
*int_size_bld
= &bld
->int_size_bld
;
196 struct lp_build_context
*float_size_bld
= &bld
->float_size_bld
;
197 struct lp_build_context
*float_bld
= &bld
->float_bld
;
198 const unsigned dims
= bld
->dims
;
199 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
200 LLVMTypeRef i32t
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
201 LLVMValueRef index0
= LLVMConstInt(i32t
, 0, 0);
202 LLVMValueRef index1
= LLVMConstInt(i32t
, 1, 0);
203 LLVMValueRef index2
= LLVMConstInt(i32t
, 2, 0);
204 LLVMValueRef dsdx
, dsdy
, dtdx
, dtdy
, drdx
, drdy
;
205 LLVMValueRef rho_x
, rho_y
;
206 LLVMValueRef rho_vec
;
207 LLVMValueRef int_size
, float_size
;
209 LLVMValueRef first_level
, first_level_vec
;
219 rho_x
= float_size_bld
->undef
;
220 rho_y
= float_size_bld
->undef
;
222 rho_x
= LLVMBuildInsertElement(builder
, rho_x
, dsdx
, index0
, "");
223 rho_y
= LLVMBuildInsertElement(builder
, rho_y
, dsdy
, index0
, "");
228 rho_x
= LLVMBuildInsertElement(builder
, rho_x
, dtdx
, index1
, "");
229 rho_y
= LLVMBuildInsertElement(builder
, rho_y
, dtdy
, index1
, "");
235 rho_x
= LLVMBuildInsertElement(builder
, rho_x
, drdx
, index2
, "");
236 rho_y
= LLVMBuildInsertElement(builder
, rho_y
, drdy
, index2
, "");
240 rho_x
= lp_build_abs(float_size_bld
, rho_x
);
241 rho_y
= lp_build_abs(float_size_bld
, rho_y
);
243 rho_vec
= lp_build_max(float_size_bld
, rho_x
, rho_y
);
245 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
247 first_level_vec
= lp_build_broadcast_scalar(&bld
->int_size_bld
, first_level
);
248 int_size
= lp_build_minify(int_size_bld
, bld
->int_size
, first_level_vec
);
249 float_size
= lp_build_int_to_float(float_size_bld
, int_size
);
251 rho_vec
= lp_build_mul(float_size_bld
, rho_vec
, float_size
);
258 LLVMValueRef rho_s
, rho_t
, rho_r
;
260 rho_s
= LLVMBuildExtractElement(builder
, rho_vec
, index0
, "");
261 rho_t
= LLVMBuildExtractElement(builder
, rho_vec
, index1
, "");
263 rho
= lp_build_max(float_bld
, rho_s
, rho_t
);
266 rho_r
= LLVMBuildExtractElement(builder
, rho_vec
, index0
, "");
267 rho
= lp_build_max(float_bld
, rho
, rho_r
);
277 * Bri-linear lod computation
279 * Use a piece-wise linear approximation of log2 such that:
280 * - round to nearest, for values in the neighborhood of -1, 0, 1, 2, etc.
281 * - linear approximation for values in the neighborhood of 0.5, 1.5., etc,
282 * with the steepness specified in 'factor'
283 * - exact result for 0.5, 1.5, etc.
299 * This is a technique also commonly used in hardware:
300 * - http://ixbtlabs.com/articles2/gffx/nv40-rx800-3.html
302 * TODO: For correctness, this should only be applied when texture is known to
303 * have regular mipmaps, i.e., mipmaps derived from the base level.
305 * TODO: This could be done in fixed point, where applicable.
308 lp_build_brilinear_lod(struct lp_build_context
*bld
,
311 LLVMValueRef
*out_lod_ipart
,
312 LLVMValueRef
*out_lod_fpart
)
314 LLVMValueRef lod_fpart
;
315 double pre_offset
= (factor
- 0.5)/factor
- 0.5;
316 double post_offset
= 1 - factor
;
319 lp_build_printf(bld
->gallivm
, "lod = %f\n", lod
);
322 lod
= lp_build_add(bld
, lod
,
323 lp_build_const_vec(bld
->gallivm
, bld
->type
, pre_offset
));
325 lp_build_ifloor_fract(bld
, lod
, out_lod_ipart
, &lod_fpart
);
327 lod_fpart
= lp_build_mul(bld
, lod_fpart
,
328 lp_build_const_vec(bld
->gallivm
, bld
->type
, factor
));
330 lod_fpart
= lp_build_add(bld
, lod_fpart
,
331 lp_build_const_vec(bld
->gallivm
, bld
->type
, post_offset
));
334 * It's not necessary to clamp lod_fpart since:
335 * - the above expression will never produce numbers greater than one.
336 * - the mip filtering branch is only taken if lod_fpart is positive
339 *out_lod_fpart
= lod_fpart
;
342 lp_build_printf(bld
->gallivm
, "lod_ipart = %i\n", *out_lod_ipart
);
343 lp_build_printf(bld
->gallivm
, "lod_fpart = %f\n\n", *out_lod_fpart
);
349 * Combined log2 and brilinear lod computation.
351 * It's in all identical to calling lp_build_fast_log2() and
352 * lp_build_brilinear_lod() above, but by combining we can compute the interger
353 * and fractional part independently.
356 lp_build_brilinear_rho(struct lp_build_context
*bld
,
359 LLVMValueRef
*out_lod_ipart
,
360 LLVMValueRef
*out_lod_fpart
)
362 LLVMValueRef lod_ipart
;
363 LLVMValueRef lod_fpart
;
365 const double pre_factor
= (2*factor
- 0.5)/(M_SQRT2
*factor
);
366 const double post_offset
= 1 - 2*factor
;
368 assert(bld
->type
.floating
);
370 assert(lp_check_value(bld
->type
, rho
));
373 * The pre factor will make the intersections with the exact powers of two
374 * happen precisely where we want then to be, which means that the integer
375 * part will not need any post adjustments.
377 rho
= lp_build_mul(bld
, rho
,
378 lp_build_const_vec(bld
->gallivm
, bld
->type
, pre_factor
));
380 /* ipart = ifloor(log2(rho)) */
381 lod_ipart
= lp_build_extract_exponent(bld
, rho
, 0);
383 /* fpart = rho / 2**ipart */
384 lod_fpart
= lp_build_extract_mantissa(bld
, rho
);
386 lod_fpart
= lp_build_mul(bld
, lod_fpart
,
387 lp_build_const_vec(bld
->gallivm
, bld
->type
, factor
));
389 lod_fpart
= lp_build_add(bld
, lod_fpart
,
390 lp_build_const_vec(bld
->gallivm
, bld
->type
, post_offset
));
393 * Like lp_build_brilinear_lod, it's not necessary to clamp lod_fpart since:
394 * - the above expression will never produce numbers greater than one.
395 * - the mip filtering branch is only taken if lod_fpart is positive
398 *out_lod_ipart
= lod_ipart
;
399 *out_lod_fpart
= lod_fpart
;
404 * Generate code to compute texture level of detail (lambda).
405 * \param ddx partial derivatives of (s, t, r, q) with respect to X
406 * \param ddy partial derivatives of (s, t, r, q) with respect to Y
407 * \param lod_bias optional float vector with the shader lod bias
408 * \param explicit_lod optional float vector with the explicit lod
409 * \param width scalar int texture width
410 * \param height scalar int texture height
411 * \param depth scalar int texture depth
413 * XXX: The resulting lod is scalar, so ignore all but the first element of
414 * derivatives, lod_bias, etc that are passed by the shader.
417 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
419 const LLVMValueRef ddx
[4],
420 const LLVMValueRef ddy
[4],
421 LLVMValueRef lod_bias
, /* optional */
422 LLVMValueRef explicit_lod
, /* optional */
424 LLVMValueRef
*out_lod_ipart
,
425 LLVMValueRef
*out_lod_fpart
)
428 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
429 struct lp_build_context
*float_bld
= &bld
->float_bld
;
432 *out_lod_ipart
= bld
->int_bld
.zero
;
433 *out_lod_fpart
= bld
->float_bld
.zero
;
435 if (bld
->static_state
->min_max_lod_equal
) {
436 /* User is forcing sampling from a particular mipmap level.
437 * This is hit during mipmap generation.
439 LLVMValueRef min_lod
=
440 bld
->dynamic_state
->min_lod(bld
->dynamic_state
, bld
->gallivm
, unit
);
445 LLVMValueRef sampler_lod_bias
=
446 bld
->dynamic_state
->lod_bias(bld
->dynamic_state
, bld
->gallivm
, unit
);
447 LLVMValueRef index0
= lp_build_const_int32(bld
->gallivm
, 0);
450 lod
= LLVMBuildExtractElement(builder
, explicit_lod
,
456 rho
= lp_build_rho(bld
, unit
, ddx
, ddy
);
459 * Compute lod = log2(rho)
463 !bld
->static_state
->lod_bias_non_zero
&&
464 !bld
->static_state
->apply_max_lod
&&
465 !bld
->static_state
->apply_min_lod
) {
467 * Special case when there are no post-log2 adjustments, which
468 * saves instructions but keeping the integer and fractional lod
469 * computations separate from the start.
472 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
||
473 mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
474 *out_lod_ipart
= lp_build_ilog2(float_bld
, rho
);
475 *out_lod_fpart
= bld
->float_bld
.zero
;
478 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
&&
479 !(gallivm_debug
& GALLIVM_DEBUG_NO_BRILINEAR
)) {
480 lp_build_brilinear_rho(float_bld
, rho
, BRILINEAR_FACTOR
,
481 out_lod_ipart
, out_lod_fpart
);
487 lod
= lp_build_log2(float_bld
, rho
);
490 lod
= lp_build_fast_log2(float_bld
, rho
);
493 /* add shader lod bias */
495 lod_bias
= LLVMBuildExtractElement(builder
, lod_bias
,
497 lod
= LLVMBuildFAdd(builder
, lod
, lod_bias
, "shader_lod_bias");
501 /* add sampler lod bias */
502 if (bld
->static_state
->lod_bias_non_zero
)
503 lod
= LLVMBuildFAdd(builder
, lod
, sampler_lod_bias
, "sampler_lod_bias");
507 if (bld
->static_state
->apply_max_lod
) {
508 LLVMValueRef max_lod
=
509 bld
->dynamic_state
->max_lod(bld
->dynamic_state
, bld
->gallivm
, unit
);
511 lod
= lp_build_min(float_bld
, lod
, max_lod
);
513 if (bld
->static_state
->apply_min_lod
) {
514 LLVMValueRef min_lod
=
515 bld
->dynamic_state
->min_lod(bld
->dynamic_state
, bld
->gallivm
, unit
);
517 lod
= lp_build_max(float_bld
, lod
, min_lod
);
521 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
522 if (!(gallivm_debug
& GALLIVM_DEBUG_NO_BRILINEAR
)) {
523 lp_build_brilinear_lod(float_bld
, lod
, BRILINEAR_FACTOR
,
524 out_lod_ipart
, out_lod_fpart
);
527 lp_build_ifloor_fract(float_bld
, lod
, out_lod_ipart
, out_lod_fpart
);
530 lp_build_name(*out_lod_fpart
, "lod_fpart");
533 *out_lod_ipart
= lp_build_iround(float_bld
, lod
);
536 lp_build_name(*out_lod_ipart
, "lod_ipart");
543 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
544 * mipmap level index.
545 * Note: this is all scalar code.
546 * \param lod scalar float texture level of detail
547 * \param level_out returns integer
550 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
552 LLVMValueRef lod_ipart
,
553 LLVMValueRef
*level_out
)
555 struct lp_build_context
*int_bld
= &bld
->int_bld
;
556 LLVMValueRef first_level
, last_level
, level
;
558 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
560 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
563 /* convert float lod to integer */
564 level
= lp_build_add(int_bld
, lod_ipart
, first_level
);
566 /* clamp level to legal range of levels */
567 *level_out
= lp_build_clamp(int_bld
, level
, first_level
, last_level
);
572 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
573 * two (adjacent) mipmap level indexes. Later, we'll sample from those
574 * two mipmap levels and interpolate between them.
577 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
579 LLVMValueRef lod_ipart
,
580 LLVMValueRef
*lod_fpart_inout
,
581 LLVMValueRef
*level0_out
,
582 LLVMValueRef
*level1_out
)
584 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
585 struct lp_build_context
*int_bld
= &bld
->int_bld
;
586 struct lp_build_context
*float_bld
= &bld
->float_bld
;
587 LLVMValueRef first_level
, last_level
;
588 LLVMValueRef clamp_min
;
589 LLVMValueRef clamp_max
;
591 first_level
= bld
->dynamic_state
->first_level(bld
->dynamic_state
,
594 *level0_out
= lp_build_add(int_bld
, lod_ipart
, first_level
);
595 *level1_out
= lp_build_add(int_bld
, *level0_out
, int_bld
->one
);
597 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
601 * Clamp both *level0_out and *level1_out to [first_level, last_level], with
602 * the minimum number of comparisons, and zeroing lod_fpart in the extreme
603 * ends in the process.
606 /* *level0_out < first_level */
607 clamp_min
= LLVMBuildICmp(builder
, LLVMIntSLT
,
608 *level0_out
, first_level
,
609 "clamp_lod_to_first");
611 *level0_out
= LLVMBuildSelect(builder
, clamp_min
,
612 first_level
, *level0_out
, "");
614 *level1_out
= LLVMBuildSelect(builder
, clamp_min
,
615 first_level
, *level1_out
, "");
617 *lod_fpart_inout
= LLVMBuildSelect(builder
, clamp_min
,
618 float_bld
->zero
, *lod_fpart_inout
, "");
620 /* *level0_out >= last_level */
621 clamp_max
= LLVMBuildICmp(builder
, LLVMIntSGE
,
622 *level0_out
, last_level
,
623 "clamp_lod_to_last");
625 *level0_out
= LLVMBuildSelect(builder
, clamp_max
,
626 last_level
, *level0_out
, "");
628 *level1_out
= LLVMBuildSelect(builder
, clamp_max
,
629 last_level
, *level1_out
, "");
631 *lod_fpart_inout
= LLVMBuildSelect(builder
, clamp_max
,
632 float_bld
->zero
, *lod_fpart_inout
, "");
634 lp_build_name(*level0_out
, "sampler%u_miplevel0", unit
);
635 lp_build_name(*level1_out
, "sampler%u_miplevel1", unit
);
636 lp_build_name(*lod_fpart_inout
, "sampler%u_mipweight", unit
);
641 * Return pointer to a single mipmap level.
642 * \param data_array array of pointers to mipmap levels
643 * \param level integer mipmap level
646 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
649 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
650 LLVMValueRef indexes
[2], data_ptr
;
652 indexes
[0] = lp_build_const_int32(bld
->gallivm
, 0);
654 data_ptr
= LLVMBuildGEP(builder
, bld
->data_array
, indexes
, 2, "");
655 data_ptr
= LLVMBuildLoad(builder
, data_ptr
, "");
661 lp_build_get_const_mipmap_level(struct lp_build_sample_context
*bld
,
664 LLVMValueRef lvl
= lp_build_const_int32(bld
->gallivm
, level
);
665 return lp_build_get_mipmap_level(bld
, lvl
);
670 * Codegen equivalent for u_minify().
671 * Return max(1, base_size >> level);
674 lp_build_minify(struct lp_build_context
*bld
,
675 LLVMValueRef base_size
,
678 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
679 assert(lp_check_value(bld
->type
, base_size
));
680 assert(lp_check_value(bld
->type
, level
));
682 if (level
== bld
->zero
) {
683 /* if we're using mipmap level zero, no minification is needed */
688 LLVMBuildLShr(builder
, base_size
, level
, "minify");
689 assert(bld
->type
.sign
);
690 size
= lp_build_max(bld
, size
, bld
->one
);
697 * Dereference stride_array[mipmap_level] array to get a stride.
698 * Return stride as a vector.
701 lp_build_get_level_stride_vec(struct lp_build_sample_context
*bld
,
702 LLVMValueRef stride_array
, LLVMValueRef level
)
704 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
705 LLVMValueRef indexes
[2], stride
;
706 indexes
[0] = lp_build_const_int32(bld
->gallivm
, 0);
708 stride
= LLVMBuildGEP(builder
, stride_array
, indexes
, 2, "");
709 stride
= LLVMBuildLoad(builder
, stride
, "");
710 stride
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, stride
);
716 * When sampling a mipmap, we need to compute the width, height, depth
717 * of the source levels from the level indexes. This helper function
721 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
723 LLVMValueRef
*out_size
,
724 LLVMValueRef
*row_stride_vec
,
725 LLVMValueRef
*img_stride_vec
)
727 const unsigned dims
= bld
->dims
;
728 LLVMValueRef ilevel_vec
;
730 ilevel_vec
= lp_build_broadcast_scalar(&bld
->int_size_bld
, ilevel
);
733 * Compute width, height, depth at mipmap level 'ilevel'
735 *out_size
= lp_build_minify(&bld
->int_size_bld
, bld
->int_size
, ilevel_vec
);
738 *row_stride_vec
= lp_build_get_level_stride_vec(bld
,
739 bld
->row_stride_array
,
741 if (dims
== 3 || bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
742 *img_stride_vec
= lp_build_get_level_stride_vec(bld
,
743 bld
->img_stride_array
,
751 * Extract and broadcast texture size.
753 * @param size_type type of the texture size vector (either
754 * bld->int_size_type or bld->float_size_type)
755 * @param coord_type type of the texture size vector (either
756 * bld->int_coord_type or bld->coord_type)
757 * @param int_size vector with the integer texture size (width, height,
761 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
762 struct lp_type size_type
,
763 struct lp_type coord_type
,
765 LLVMValueRef
*out_width
,
766 LLVMValueRef
*out_height
,
767 LLVMValueRef
*out_depth
)
769 const unsigned dims
= bld
->dims
;
770 LLVMTypeRef i32t
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
772 *out_width
= lp_build_extract_broadcast(bld
->gallivm
,
776 LLVMConstInt(i32t
, 0, 0));
778 *out_height
= lp_build_extract_broadcast(bld
->gallivm
,
782 LLVMConstInt(i32t
, 1, 0));
784 *out_depth
= lp_build_extract_broadcast(bld
->gallivm
,
788 LLVMConstInt(i32t
, 2, 0));
795 * Unnormalize coords.
797 * @param int_size vector with the integer texture size (width, height, depth)
800 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
801 LLVMValueRef flt_size
,
806 const unsigned dims
= bld
->dims
;
811 lp_build_extract_image_sizes(bld
,
812 bld
->float_size_type
,
819 /* s = s * width, t = t * height */
820 *s
= lp_build_mul(&bld
->coord_bld
, *s
, width
);
822 *t
= lp_build_mul(&bld
->coord_bld
, *t
, height
);
824 *r
= lp_build_mul(&bld
->coord_bld
, *r
, depth
);
830 /** Helper used by lp_build_cube_lookup() */
832 lp_build_cube_ima(struct lp_build_context
*coord_bld
, LLVMValueRef coord
)
834 /* ima = -0.5 / abs(coord); */
835 LLVMValueRef negHalf
= lp_build_const_vec(coord_bld
->gallivm
, coord_bld
->type
, -0.5);
836 LLVMValueRef absCoord
= lp_build_abs(coord_bld
, coord
);
837 LLVMValueRef ima
= lp_build_div(coord_bld
, negHalf
, absCoord
);
843 * Helper used by lp_build_cube_lookup()
844 * \param sign scalar +1 or -1
845 * \param coord float vector
846 * \param ima float vector
849 lp_build_cube_coord(struct lp_build_context
*coord_bld
,
850 LLVMValueRef sign
, int negate_coord
,
851 LLVMValueRef coord
, LLVMValueRef ima
)
853 /* return negate(coord) * ima * sign + 0.5; */
854 LLVMValueRef half
= lp_build_const_vec(coord_bld
->gallivm
, coord_bld
->type
, 0.5);
857 assert(negate_coord
== +1 || negate_coord
== -1);
859 if (negate_coord
== -1) {
860 coord
= lp_build_negate(coord_bld
, coord
);
863 res
= lp_build_mul(coord_bld
, coord
, ima
);
865 sign
= lp_build_broadcast_scalar(coord_bld
, sign
);
866 res
= lp_build_mul(coord_bld
, res
, sign
);
868 res
= lp_build_add(coord_bld
, res
, half
);
874 /** Helper used by lp_build_cube_lookup()
875 * Return (major_coord >= 0) ? pos_face : neg_face;
878 lp_build_cube_face(struct lp_build_sample_context
*bld
,
879 LLVMValueRef major_coord
,
880 unsigned pos_face
, unsigned neg_face
)
882 struct gallivm_state
*gallivm
= bld
->gallivm
;
883 LLVMBuilderRef builder
= gallivm
->builder
;
884 LLVMValueRef cmp
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
886 bld
->float_bld
.zero
, "");
887 LLVMValueRef pos
= lp_build_const_int32(gallivm
, pos_face
);
888 LLVMValueRef neg
= lp_build_const_int32(gallivm
, neg_face
);
889 LLVMValueRef res
= LLVMBuildSelect(builder
, cmp
, pos
, neg
, "");
896 * Generate code to do cube face selection and compute per-face texcoords.
899 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
904 LLVMValueRef
*face_s
,
905 LLVMValueRef
*face_t
)
907 struct lp_build_context
*float_bld
= &bld
->float_bld
;
908 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
909 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
910 LLVMValueRef rx
, ry
, rz
;
911 LLVMValueRef arx
, ary
, arz
;
912 LLVMValueRef c25
= lp_build_const_float(bld
->gallivm
, 0.25);
913 LLVMValueRef arx_ge_ary
, arx_ge_arz
;
914 LLVMValueRef ary_ge_arx
, ary_ge_arz
;
915 LLVMValueRef arx_ge_ary_arz
, ary_ge_arx_arz
;
916 LLVMValueRef rx_pos
, ry_pos
, rz_pos
;
918 assert(bld
->coord_bld
.type
.length
== 4);
921 * Use the average of the four pixel's texcoords to choose the face.
923 rx
= lp_build_mul(float_bld
, c25
,
924 lp_build_sum_vector(&bld
->coord_bld
, s
));
925 ry
= lp_build_mul(float_bld
, c25
,
926 lp_build_sum_vector(&bld
->coord_bld
, t
));
927 rz
= lp_build_mul(float_bld
, c25
,
928 lp_build_sum_vector(&bld
->coord_bld
, r
));
930 arx
= lp_build_abs(float_bld
, rx
);
931 ary
= lp_build_abs(float_bld
, ry
);
932 arz
= lp_build_abs(float_bld
, rz
);
935 * Compare sign/magnitude of rx,ry,rz to determine face
937 arx_ge_ary
= LLVMBuildFCmp(builder
, LLVMRealUGE
, arx
, ary
, "");
938 arx_ge_arz
= LLVMBuildFCmp(builder
, LLVMRealUGE
, arx
, arz
, "");
939 ary_ge_arx
= LLVMBuildFCmp(builder
, LLVMRealUGE
, ary
, arx
, "");
940 ary_ge_arz
= LLVMBuildFCmp(builder
, LLVMRealUGE
, ary
, arz
, "");
942 arx_ge_ary_arz
= LLVMBuildAnd(builder
, arx_ge_ary
, arx_ge_arz
, "");
943 ary_ge_arx_arz
= LLVMBuildAnd(builder
, ary_ge_arx
, ary_ge_arz
, "");
945 rx_pos
= LLVMBuildFCmp(builder
, LLVMRealUGE
, rx
, float_bld
->zero
, "");
946 ry_pos
= LLVMBuildFCmp(builder
, LLVMRealUGE
, ry
, float_bld
->zero
, "");
947 rz_pos
= LLVMBuildFCmp(builder
, LLVMRealUGE
, rz
, float_bld
->zero
, "");
950 struct lp_build_if_state if_ctx
;
951 LLVMValueRef face_s_var
;
952 LLVMValueRef face_t_var
;
953 LLVMValueRef face_var
;
955 face_s_var
= lp_build_alloca(bld
->gallivm
, bld
->coord_bld
.vec_type
, "face_s_var");
956 face_t_var
= lp_build_alloca(bld
->gallivm
, bld
->coord_bld
.vec_type
, "face_t_var");
957 face_var
= lp_build_alloca(bld
->gallivm
, bld
->int_bld
.vec_type
, "face_var");
959 lp_build_if(&if_ctx
, bld
->gallivm
, arx_ge_ary_arz
);
962 LLVMValueRef sign
= lp_build_sgn(float_bld
, rx
);
963 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, s
);
964 *face_s
= lp_build_cube_coord(coord_bld
, sign
, +1, r
, ima
);
965 *face_t
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
966 *face
= lp_build_cube_face(bld
, rx
,
968 PIPE_TEX_FACE_NEG_X
);
969 LLVMBuildStore(builder
, *face_s
, face_s_var
);
970 LLVMBuildStore(builder
, *face_t
, face_t_var
);
971 LLVMBuildStore(builder
, *face
, face_var
);
973 lp_build_else(&if_ctx
);
975 struct lp_build_if_state if_ctx2
;
977 ary_ge_arx_arz
= LLVMBuildAnd(builder
, ary_ge_arx
, ary_ge_arz
, "");
979 lp_build_if(&if_ctx2
, bld
->gallivm
, ary_ge_arx_arz
);
982 LLVMValueRef sign
= lp_build_sgn(float_bld
, ry
);
983 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, t
);
984 *face_s
= lp_build_cube_coord(coord_bld
, NULL
, -1, s
, ima
);
985 *face_t
= lp_build_cube_coord(coord_bld
, sign
, -1, r
, ima
);
986 *face
= lp_build_cube_face(bld
, ry
,
988 PIPE_TEX_FACE_NEG_Y
);
989 LLVMBuildStore(builder
, *face_s
, face_s_var
);
990 LLVMBuildStore(builder
, *face_t
, face_t_var
);
991 LLVMBuildStore(builder
, *face
, face_var
);
993 lp_build_else(&if_ctx2
);
996 LLVMValueRef sign
= lp_build_sgn(float_bld
, rz
);
997 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, r
);
998 *face_s
= lp_build_cube_coord(coord_bld
, sign
, -1, s
, ima
);
999 *face_t
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
1000 *face
= lp_build_cube_face(bld
, rz
,
1001 PIPE_TEX_FACE_POS_Z
,
1002 PIPE_TEX_FACE_NEG_Z
);
1003 LLVMBuildStore(builder
, *face_s
, face_s_var
);
1004 LLVMBuildStore(builder
, *face_t
, face_t_var
);
1005 LLVMBuildStore(builder
, *face
, face_var
);
1007 lp_build_endif(&if_ctx2
);
1010 lp_build_endif(&if_ctx
);
1012 *face_s
= LLVMBuildLoad(builder
, face_s_var
, "face_s");
1013 *face_t
= LLVMBuildLoad(builder
, face_t_var
, "face_t");
1014 *face
= LLVMBuildLoad(builder
, face_var
, "face");
1020 * Compute the partial offset of a pixel block along an arbitrary axis.
1022 * @param coord coordinate in pixels
1023 * @param stride number of bytes between rows of successive pixel blocks
1024 * @param block_length number of pixels in a pixels block along the coordinate
1026 * @param out_offset resulting relative offset of the pixel block in bytes
1027 * @param out_subcoord resulting sub-block pixel coordinate
1030 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
1031 unsigned block_length
,
1033 LLVMValueRef stride
,
1034 LLVMValueRef
*out_offset
,
1035 LLVMValueRef
*out_subcoord
)
1037 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1038 LLVMValueRef offset
;
1039 LLVMValueRef subcoord
;
1041 if (block_length
== 1) {
1042 subcoord
= bld
->zero
;
1046 * Pixel blocks have power of two dimensions. LLVM should convert the
1047 * rem/div to bit arithmetic.
1048 * TODO: Verify this.
1049 * It does indeed BUT it does transform it to scalar (and back) when doing so
1050 * (using roughly extract, shift/and, mov, unpack) (llvm 2.7).
1051 * The generated code looks seriously unfunny and is quite expensive.
1054 LLVMValueRef block_width
= lp_build_const_int_vec(bld
->type
, block_length
);
1055 subcoord
= LLVMBuildURem(builder
, coord
, block_width
, "");
1056 coord
= LLVMBuildUDiv(builder
, coord
, block_width
, "");
1058 unsigned logbase2
= util_logbase2(block_length
);
1059 LLVMValueRef block_shift
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, logbase2
);
1060 LLVMValueRef block_mask
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
, block_length
- 1);
1061 subcoord
= LLVMBuildAnd(builder
, coord
, block_mask
, "");
1062 coord
= LLVMBuildLShr(builder
, coord
, block_shift
, "");
1066 offset
= lp_build_mul(bld
, coord
, stride
);
1069 assert(out_subcoord
);
1071 *out_offset
= offset
;
1072 *out_subcoord
= subcoord
;
1077 * Compute the offset of a pixel block.
1079 * x, y, z, y_stride, z_stride are vectors, and they refer to pixels.
1081 * Returns the relative offset and i,j sub-block coordinates
1084 lp_build_sample_offset(struct lp_build_context
*bld
,
1085 const struct util_format_description
*format_desc
,
1089 LLVMValueRef y_stride
,
1090 LLVMValueRef z_stride
,
1091 LLVMValueRef
*out_offset
,
1092 LLVMValueRef
*out_i
,
1093 LLVMValueRef
*out_j
)
1095 LLVMValueRef x_stride
;
1096 LLVMValueRef offset
;
1098 x_stride
= lp_build_const_vec(bld
->gallivm
, bld
->type
,
1099 format_desc
->block
.bits
/8);
1101 lp_build_sample_partial_offset(bld
,
1102 format_desc
->block
.width
,
1106 if (y
&& y_stride
) {
1107 LLVMValueRef y_offset
;
1108 lp_build_sample_partial_offset(bld
,
1109 format_desc
->block
.height
,
1112 offset
= lp_build_add(bld
, offset
, y_offset
);
1118 if (z
&& z_stride
) {
1119 LLVMValueRef z_offset
;
1121 lp_build_sample_partial_offset(bld
,
1122 1, /* pixel blocks are always 2D */
1125 offset
= lp_build_add(bld
, offset
, z_offset
);
1128 *out_offset
= offset
;