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
56 * Does the given texture wrap mode allow sampling the texture border color?
57 * XXX maybe move this into gallium util code.
60 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
61 unsigned min_img_filter
,
62 unsigned mag_img_filter
)
65 case PIPE_TEX_WRAP_REPEAT
:
66 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
67 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
68 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
70 case PIPE_TEX_WRAP_CLAMP
:
71 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
72 if (min_img_filter
== PIPE_TEX_FILTER_NEAREST
&&
73 mag_img_filter
== PIPE_TEX_FILTER_NEAREST
) {
78 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
79 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
82 assert(0 && "unexpected wrap mode");
89 * Initialize lp_sampler_static_state object with the gallium sampler
91 * The former is considered to be static and the later dynamic.
94 lp_sampler_static_state(struct lp_sampler_static_state
*state
,
95 const struct pipe_sampler_view
*view
,
96 const struct pipe_sampler_state
*sampler
)
98 const struct pipe_resource
*texture
= view
->texture
;
100 memset(state
, 0, sizeof *state
);
109 * We don't copy sampler state over unless it is actually enabled, to avoid
110 * spurious recompiles, as the sampler static state is part of the shader
113 * Ideally the state tracker or cso_cache module would make all state
114 * canonical, but until that happens it's better to be safe than sorry here.
116 * XXX: Actually there's much more than can be done here, especially
117 * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
120 state
->format
= view
->format
;
121 state
->swizzle_r
= view
->swizzle_r
;
122 state
->swizzle_g
= view
->swizzle_g
;
123 state
->swizzle_b
= view
->swizzle_b
;
124 state
->swizzle_a
= view
->swizzle_a
;
126 state
->target
= texture
->target
;
127 state
->pot_width
= util_is_power_of_two(texture
->width0
);
128 state
->pot_height
= util_is_power_of_two(texture
->height0
);
129 state
->pot_depth
= util_is_power_of_two(texture
->depth0
);
131 state
->wrap_s
= sampler
->wrap_s
;
132 state
->wrap_t
= sampler
->wrap_t
;
133 state
->wrap_r
= sampler
->wrap_r
;
134 state
->min_img_filter
= sampler
->min_img_filter
;
135 state
->mag_img_filter
= sampler
->mag_img_filter
;
137 if (view
->last_level
&& sampler
->max_lod
> 0.0f
) {
138 state
->min_mip_filter
= sampler
->min_mip_filter
;
140 state
->min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
143 if (state
->min_mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
144 if (sampler
->lod_bias
!= 0.0f
) {
145 state
->lod_bias_non_zero
= 1;
148 /* If min_lod == max_lod we can greatly simplify mipmap selection.
149 * This is a case that occurs during automatic mipmap generation.
151 if (sampler
->min_lod
== sampler
->max_lod
) {
152 state
->min_max_lod_equal
= 1;
154 if (sampler
->min_lod
> 0.0f
) {
155 state
->apply_min_lod
= 1;
158 if (sampler
->max_lod
< (float)view
->last_level
) {
159 state
->apply_max_lod
= 1;
164 state
->compare_mode
= sampler
->compare_mode
;
165 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
) {
166 state
->compare_func
= sampler
->compare_func
;
169 state
->normalized_coords
= sampler
->normalized_coords
;
172 * FIXME: Handle the remainder of pipe_sampler_view.
178 * Generate code to compute coordinate gradient (rho).
179 * \param ddx partial derivatives of (s, t, r, q) with respect to X
180 * \param ddy partial derivatives of (s, t, r, q) with respect to Y
182 * XXX: The resulting rho is scalar, so we ignore all but the first element of
183 * derivatives that are passed by the shader.
186 lp_build_rho(struct lp_build_sample_context
*bld
,
187 const LLVMValueRef ddx
[4],
188 const LLVMValueRef ddy
[4])
190 struct lp_build_context
*float_size_bld
= &bld
->float_size_bld
;
191 struct lp_build_context
*float_bld
= &bld
->float_bld
;
192 const unsigned dims
= bld
->dims
;
193 LLVMTypeRef i32t
= LLVMInt32Type();
194 LLVMValueRef index0
= LLVMConstInt(i32t
, 0, 0);
195 LLVMValueRef index1
= LLVMConstInt(i32t
, 1, 0);
196 LLVMValueRef index2
= LLVMConstInt(i32t
, 2, 0);
197 LLVMValueRef dsdx
, dsdy
, dtdx
, dtdy
, drdx
, drdy
;
198 LLVMValueRef rho_x
, rho_y
;
199 LLVMValueRef rho_vec
;
200 LLVMValueRef float_size
;
211 rho_x
= float_size_bld
->undef
;
212 rho_y
= float_size_bld
->undef
;
214 rho_x
= LLVMBuildInsertElement(bld
->builder
, rho_x
, dsdx
, index0
, "");
215 rho_y
= LLVMBuildInsertElement(bld
->builder
, rho_y
, dsdy
, index0
, "");
220 rho_x
= LLVMBuildInsertElement(bld
->builder
, rho_x
, dtdx
, index1
, "");
221 rho_y
= LLVMBuildInsertElement(bld
->builder
, rho_y
, dtdy
, index1
, "");
227 rho_x
= LLVMBuildInsertElement(bld
->builder
, rho_x
, drdx
, index2
, "");
228 rho_y
= LLVMBuildInsertElement(bld
->builder
, rho_y
, drdy
, index2
, "");
232 rho_x
= lp_build_abs(float_size_bld
, rho_x
);
233 rho_y
= lp_build_abs(float_size_bld
, rho_y
);
235 rho_vec
= lp_build_max(float_size_bld
, rho_x
, rho_y
);
237 float_size
= lp_build_int_to_float(float_size_bld
, bld
->int_size
);
239 rho_vec
= lp_build_mul(float_size_bld
, rho_vec
, float_size
);
246 LLVMValueRef rho_s
, rho_t
, rho_r
;
248 rho_s
= LLVMBuildExtractElement(bld
->builder
, rho_vec
, index0
, "");
249 rho_t
= LLVMBuildExtractElement(bld
->builder
, rho_vec
, index1
, "");
251 rho
= lp_build_max(float_bld
, rho_s
, rho_t
);
254 rho_r
= LLVMBuildExtractElement(bld
->builder
, rho_vec
, index0
, "");
255 rho
= lp_build_max(float_bld
, rho
, rho_r
);
265 * Bri-linear lod computation
267 * Use a piece-wise linear approximation of log2 such that:
268 * - round to nearest, for values in the neighborhood of -1, 0, 1, 2, etc.
269 * - linear approximation for values in the neighborhood of 0.5, 1.5., etc,
270 * with the steepness specified in 'factor'
271 * - exact result for 0.5, 1.5, etc.
287 * This is a technique also commonly used in hardware:
288 * - http://ixbtlabs.com/articles2/gffx/nv40-rx800-3.html
290 * TODO: For correctness, this should only be applied when texture is known to
291 * have regular mipmaps, i.e., mipmaps derived from the base level.
293 * TODO: This could be done in fixed point, where applicable.
296 lp_build_brilinear_lod(struct lp_build_context
*bld
,
299 LLVMValueRef
*out_lod_ipart
,
300 LLVMValueRef
*out_lod_fpart
)
302 LLVMValueRef lod_fpart
;
303 double pre_offset
= (factor
- 0.5)/factor
- 0.5;
304 double post_offset
= 1 - factor
;
307 lp_build_printf(bld
->builder
, "lod = %f\n", lod
);
310 lod
= lp_build_add(bld
, lod
,
311 lp_build_const_vec(bld
->type
, pre_offset
));
313 lp_build_ifloor_fract(bld
, lod
, out_lod_ipart
, &lod_fpart
);
315 lod_fpart
= lp_build_mul(bld
, lod_fpart
,
316 lp_build_const_vec(bld
->type
, factor
));
318 lod_fpart
= lp_build_add(bld
, lod_fpart
,
319 lp_build_const_vec(bld
->type
, post_offset
));
322 * It's not necessary to clamp lod_fpart since:
323 * - the above expression will never produce numbers greater than one.
324 * - the mip filtering branch is only taken if lod_fpart is positive
327 *out_lod_fpart
= lod_fpart
;
330 lp_build_printf(bld
->builder
, "lod_ipart = %i\n", *out_lod_ipart
);
331 lp_build_printf(bld
->builder
, "lod_fpart = %f\n\n", *out_lod_fpart
);
337 * Combined log2 and brilinear lod computation.
339 * It's in all identical to calling lp_build_fast_log2() and
340 * lp_build_brilinear_lod() above, but by combining we can compute the interger
341 * and fractional part independently.
344 lp_build_brilinear_rho(struct lp_build_context
*bld
,
347 LLVMValueRef
*out_lod_ipart
,
348 LLVMValueRef
*out_lod_fpart
)
350 LLVMValueRef lod_ipart
;
351 LLVMValueRef lod_fpart
;
353 const double pre_factor
= (2*factor
- 0.5)/(M_SQRT2
*factor
);
354 const double post_offset
= 1 - 2*factor
;
356 assert(bld
->type
.floating
);
358 assert(lp_check_value(bld
->type
, rho
));
361 * The pre factor will make the intersections with the exact powers of two
362 * happen precisely where we want then to be, which means that the integer
363 * part will not need any post adjustments.
365 rho
= lp_build_mul(bld
, rho
,
366 lp_build_const_vec(bld
->type
, pre_factor
));
368 /* ipart = ifloor(log2(rho)) */
369 lod_ipart
= lp_build_extract_exponent(bld
, rho
, 0);
371 /* fpart = rho / 2**ipart */
372 lod_fpart
= lp_build_extract_mantissa(bld
, rho
);
374 lod_fpart
= lp_build_mul(bld
, lod_fpart
,
375 lp_build_const_vec(bld
->type
, factor
));
377 lod_fpart
= lp_build_add(bld
, lod_fpart
,
378 lp_build_const_vec(bld
->type
, post_offset
));
381 * Like lp_build_brilinear_lod, it's not necessary to clamp lod_fpart since:
382 * - the above expression will never produce numbers greater than one.
383 * - the mip filtering branch is only taken if lod_fpart is positive
386 *out_lod_ipart
= lod_ipart
;
387 *out_lod_fpart
= lod_fpart
;
392 * Generate code to compute texture level of detail (lambda).
393 * \param ddx partial derivatives of (s, t, r, q) with respect to X
394 * \param ddy partial derivatives of (s, t, r, q) with respect to Y
395 * \param lod_bias optional float vector with the shader lod bias
396 * \param explicit_lod optional float vector with the explicit lod
397 * \param width scalar int texture width
398 * \param height scalar int texture height
399 * \param depth scalar int texture depth
401 * XXX: The resulting lod is scalar, so ignore all but the first element of
402 * derivatives, lod_bias, etc that are passed by the shader.
405 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
407 const LLVMValueRef ddx
[4],
408 const LLVMValueRef ddy
[4],
409 LLVMValueRef lod_bias
, /* optional */
410 LLVMValueRef explicit_lod
, /* optional */
412 LLVMValueRef
*out_lod_ipart
,
413 LLVMValueRef
*out_lod_fpart
)
416 struct lp_build_context
*float_bld
= &bld
->float_bld
;
419 *out_lod_ipart
= bld
->int_bld
.zero
;
420 *out_lod_fpart
= bld
->float_bld
.zero
;
422 if (bld
->static_state
->min_max_lod_equal
) {
423 /* User is forcing sampling from a particular mipmap level.
424 * This is hit during mipmap generation.
426 LLVMValueRef min_lod
=
427 bld
->dynamic_state
->min_lod(bld
->dynamic_state
, bld
->builder
, unit
);
432 LLVMValueRef sampler_lod_bias
=
433 bld
->dynamic_state
->lod_bias(bld
->dynamic_state
, bld
->builder
, unit
);
434 LLVMValueRef index0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
437 lod
= LLVMBuildExtractElement(bld
->builder
, explicit_lod
,
443 rho
= lp_build_rho(bld
, ddx
, ddy
);
446 * Compute lod = log2(rho)
450 !bld
->static_state
->lod_bias_non_zero
&&
451 !bld
->static_state
->apply_max_lod
&&
452 !bld
->static_state
->apply_min_lod
) {
454 * Special case when there are no post-log2 adjustments, which
455 * saves instructions but keeping the integer and fractional lod
456 * computations separate from the start.
459 if (mip_filter
== PIPE_TEX_MIPFILTER_NONE
||
460 mip_filter
== PIPE_TEX_MIPFILTER_NEAREST
) {
461 *out_lod_ipart
= lp_build_ilog2(float_bld
, rho
);
462 *out_lod_fpart
= bld
->float_bld
.zero
;
465 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
&&
466 !(gallivm_debug
& GALLIVM_DEBUG_NO_BRILINEAR
)) {
467 lp_build_brilinear_rho(float_bld
, rho
, BRILINEAR_FACTOR
,
468 out_lod_ipart
, out_lod_fpart
);
474 lod
= lp_build_log2(float_bld
, rho
);
477 lod
= lp_build_fast_log2(float_bld
, rho
);
480 /* add shader lod bias */
482 lod_bias
= LLVMBuildExtractElement(bld
->builder
, lod_bias
,
484 lod
= LLVMBuildFAdd(bld
->builder
, lod
, lod_bias
, "shader_lod_bias");
488 /* add sampler lod bias */
489 if (bld
->static_state
->lod_bias_non_zero
)
490 lod
= LLVMBuildFAdd(bld
->builder
, lod
, sampler_lod_bias
, "sampler_lod_bias");
494 if (bld
->static_state
->apply_max_lod
) {
495 LLVMValueRef max_lod
=
496 bld
->dynamic_state
->max_lod(bld
->dynamic_state
, bld
->builder
, unit
);
498 lod
= lp_build_min(float_bld
, lod
, max_lod
);
500 if (bld
->static_state
->apply_min_lod
) {
501 LLVMValueRef min_lod
=
502 bld
->dynamic_state
->min_lod(bld
->dynamic_state
, bld
->builder
, unit
);
504 lod
= lp_build_max(float_bld
, lod
, min_lod
);
508 if (mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
) {
509 if (!(gallivm_debug
& GALLIVM_DEBUG_NO_BRILINEAR
)) {
510 lp_build_brilinear_lod(float_bld
, lod
, BRILINEAR_FACTOR
,
511 out_lod_ipart
, out_lod_fpart
);
514 lp_build_ifloor_fract(float_bld
, lod
, out_lod_ipart
, out_lod_fpart
);
517 lp_build_name(*out_lod_fpart
, "lod_fpart");
520 *out_lod_ipart
= lp_build_iround(float_bld
, lod
);
523 lp_build_name(*out_lod_ipart
, "lod_ipart");
530 * For PIPE_TEX_MIPFILTER_NEAREST, convert float LOD to integer
531 * mipmap level index.
532 * Note: this is all scalar code.
533 * \param lod scalar float texture level of detail
534 * \param level_out returns integer
537 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
539 LLVMValueRef lod_ipart
,
540 LLVMValueRef
*level_out
)
542 struct lp_build_context
*int_bld
= &bld
->int_bld
;
543 LLVMValueRef last_level
, level
;
545 LLVMValueRef zero
= LLVMConstInt(LLVMInt32Type(), 0, 0);
547 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
550 /* convert float lod to integer */
553 /* clamp level to legal range of levels */
554 *level_out
= lp_build_clamp(int_bld
, level
, zero
, last_level
);
559 * For PIPE_TEX_MIPFILTER_LINEAR, convert float LOD to integer to
560 * two (adjacent) mipmap level indexes. Later, we'll sample from those
561 * two mipmap levels and interpolate between them.
564 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
566 LLVMValueRef lod_ipart
,
567 LLVMValueRef
*lod_fpart_inout
,
568 LLVMValueRef
*level0_out
,
569 LLVMValueRef
*level1_out
)
571 LLVMBuilderRef builder
= bld
->builder
;
572 struct lp_build_context
*int_bld
= &bld
->int_bld
;
573 struct lp_build_context
*float_bld
= &bld
->float_bld
;
574 LLVMValueRef last_level
;
575 LLVMValueRef clamp_min
;
576 LLVMValueRef clamp_max
;
578 *level0_out
= lod_ipart
;
579 *level1_out
= lp_build_add(int_bld
, lod_ipart
, int_bld
->one
);
581 last_level
= bld
->dynamic_state
->last_level(bld
->dynamic_state
,
585 * Clamp both lod_ipart and lod_ipart + 1 to [0, last_level], with the
586 * minimum number of comparisons, and zeroing lod_fpart in the extreme
587 * ends in the process.
591 clamp_min
= LLVMBuildICmp(builder
, LLVMIntSLT
,
592 lod_ipart
, int_bld
->zero
,
593 "clamp_lod_to_zero");
595 *level0_out
= LLVMBuildSelect(builder
, clamp_min
,
596 int_bld
->zero
, *level0_out
, "");
598 *level1_out
= LLVMBuildSelect(builder
, clamp_min
,
599 int_bld
->zero
, *level1_out
, "");
601 *lod_fpart_inout
= LLVMBuildSelect(builder
, clamp_min
,
602 float_bld
->zero
, *lod_fpart_inout
, "");
604 /* lod_ipart >= last_level */
605 clamp_max
= LLVMBuildICmp(builder
, LLVMIntSGE
,
606 lod_ipart
, last_level
,
607 "clamp_lod_to_last");
609 *level0_out
= LLVMBuildSelect(builder
, clamp_max
,
610 last_level
, *level0_out
, "");
612 *level1_out
= LLVMBuildSelect(builder
, clamp_max
,
613 last_level
, *level1_out
, "");
615 *lod_fpart_inout
= LLVMBuildSelect(builder
, clamp_max
,
616 float_bld
->zero
, *lod_fpart_inout
, "");
618 lp_build_name(*level0_out
, "sampler%u_miplevel0", unit
);
619 lp_build_name(*level1_out
, "sampler%u_miplevel1", unit
);
620 lp_build_name(*lod_fpart_inout
, "sampler%u_mipweight", unit
);
625 * Return pointer to a single mipmap level.
626 * \param data_array array of pointers to mipmap levels
627 * \param level integer mipmap level
630 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
633 LLVMValueRef indexes
[2], data_ptr
;
634 indexes
[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
636 data_ptr
= LLVMBuildGEP(bld
->builder
, bld
->data_array
, indexes
, 2, "");
637 data_ptr
= LLVMBuildLoad(bld
->builder
, data_ptr
, "");
643 lp_build_get_const_mipmap_level(struct lp_build_sample_context
*bld
,
646 LLVMValueRef lvl
= LLVMConstInt(LLVMInt32Type(), level
, 0);
647 return lp_build_get_mipmap_level(bld
, lvl
);
652 * Codegen equivalent for u_minify().
653 * Return max(1, base_size >> level);
656 lp_build_minify(struct lp_build_context
*bld
,
657 LLVMValueRef base_size
,
660 assert(lp_check_value(bld
->type
, base_size
));
661 assert(lp_check_value(bld
->type
, level
));
663 if (level
== bld
->zero
) {
664 /* if we're using mipmap level zero, no minification is needed */
669 LLVMBuildLShr(bld
->builder
, base_size
, level
, "minify");
670 assert(bld
->type
.sign
);
671 size
= lp_build_max(bld
, size
, bld
->one
);
678 * Dereference stride_array[mipmap_level] array to get a stride.
679 * Return stride as a vector.
682 lp_build_get_level_stride_vec(struct lp_build_sample_context
*bld
,
683 LLVMValueRef stride_array
, LLVMValueRef level
)
685 LLVMValueRef indexes
[2], stride
;
686 indexes
[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
688 stride
= LLVMBuildGEP(bld
->builder
, stride_array
, indexes
, 2, "");
689 stride
= LLVMBuildLoad(bld
->builder
, stride
, "");
690 stride
= lp_build_broadcast_scalar(&bld
->int_coord_bld
, stride
);
696 * When sampling a mipmap, we need to compute the width, height, depth
697 * of the source levels from the level indexes. This helper function
701 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
703 LLVMValueRef
*out_size
,
704 LLVMValueRef
*row_stride_vec
,
705 LLVMValueRef
*img_stride_vec
)
707 const unsigned dims
= bld
->dims
;
708 LLVMValueRef ilevel_vec
;
710 ilevel_vec
= lp_build_broadcast_scalar(&bld
->int_size_bld
, ilevel
);
713 * Compute width, height, depth at mipmap level 'ilevel'
715 *out_size
= lp_build_minify(&bld
->int_size_bld
, bld
->int_size
, ilevel_vec
);
718 *row_stride_vec
= lp_build_get_level_stride_vec(bld
,
719 bld
->row_stride_array
,
721 if (dims
== 3 || bld
->static_state
->target
== PIPE_TEXTURE_CUBE
) {
722 *img_stride_vec
= lp_build_get_level_stride_vec(bld
,
723 bld
->img_stride_array
,
731 * Extract and broadcast texture size.
733 * @param size_type type of the texture size vector (either
734 * bld->int_size_type or bld->float_size_type)
735 * @param coord_type type of the texture size vector (either
736 * bld->int_coord_type or bld->coord_type)
737 * @param int_size vector with the integer texture size (width, height,
741 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
742 struct lp_type size_type
,
743 struct lp_type coord_type
,
745 LLVMValueRef
*out_width
,
746 LLVMValueRef
*out_height
,
747 LLVMValueRef
*out_depth
)
749 const unsigned dims
= bld
->dims
;
750 LLVMTypeRef i32t
= LLVMInt32Type();
752 *out_width
= lp_build_extract_broadcast(bld
->builder
,
756 LLVMConstInt(i32t
, 0, 0));
758 *out_height
= lp_build_extract_broadcast(bld
->builder
,
762 LLVMConstInt(i32t
, 1, 0));
764 *out_depth
= lp_build_extract_broadcast(bld
->builder
,
768 LLVMConstInt(i32t
, 2, 0));
775 * Unnormalize coords.
777 * @param int_size vector with the integer texture size (width, height, depth)
780 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
781 LLVMValueRef flt_size
,
786 const unsigned dims
= bld
->dims
;
791 lp_build_extract_image_sizes(bld
,
792 bld
->float_size_type
,
799 /* s = s * width, t = t * height */
800 *s
= lp_build_mul(&bld
->coord_bld
, *s
, width
);
802 *t
= lp_build_mul(&bld
->coord_bld
, *t
, height
);
804 *r
= lp_build_mul(&bld
->coord_bld
, *r
, depth
);
810 /** Helper used by lp_build_cube_lookup() */
812 lp_build_cube_ima(struct lp_build_context
*coord_bld
, LLVMValueRef coord
)
814 /* ima = -0.5 / abs(coord); */
815 LLVMValueRef negHalf
= lp_build_const_vec(coord_bld
->type
, -0.5);
816 LLVMValueRef absCoord
= lp_build_abs(coord_bld
, coord
);
817 LLVMValueRef ima
= lp_build_div(coord_bld
, negHalf
, absCoord
);
823 * Helper used by lp_build_cube_lookup()
824 * \param sign scalar +1 or -1
825 * \param coord float vector
826 * \param ima float vector
829 lp_build_cube_coord(struct lp_build_context
*coord_bld
,
830 LLVMValueRef sign
, int negate_coord
,
831 LLVMValueRef coord
, LLVMValueRef ima
)
833 /* return negate(coord) * ima * sign + 0.5; */
834 LLVMValueRef half
= lp_build_const_vec(coord_bld
->type
, 0.5);
837 assert(negate_coord
== +1 || negate_coord
== -1);
839 if (negate_coord
== -1) {
840 coord
= lp_build_negate(coord_bld
, coord
);
843 res
= lp_build_mul(coord_bld
, coord
, ima
);
845 sign
= lp_build_broadcast_scalar(coord_bld
, sign
);
846 res
= lp_build_mul(coord_bld
, res
, sign
);
848 res
= lp_build_add(coord_bld
, res
, half
);
854 /** Helper used by lp_build_cube_lookup()
855 * Return (major_coord >= 0) ? pos_face : neg_face;
858 lp_build_cube_face(struct lp_build_sample_context
*bld
,
859 LLVMValueRef major_coord
,
860 unsigned pos_face
, unsigned neg_face
)
862 LLVMValueRef cmp
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
,
864 bld
->float_bld
.zero
, "");
865 LLVMValueRef pos
= LLVMConstInt(LLVMInt32Type(), pos_face
, 0);
866 LLVMValueRef neg
= LLVMConstInt(LLVMInt32Type(), neg_face
, 0);
867 LLVMValueRef res
= LLVMBuildSelect(bld
->builder
, cmp
, pos
, neg
, "");
874 * Generate code to do cube face selection and compute per-face texcoords.
877 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
882 LLVMValueRef
*face_s
,
883 LLVMValueRef
*face_t
)
885 struct lp_build_context
*float_bld
= &bld
->float_bld
;
886 struct lp_build_context
*coord_bld
= &bld
->coord_bld
;
887 LLVMValueRef rx
, ry
, rz
;
888 LLVMValueRef arx
, ary
, arz
;
889 LLVMValueRef c25
= LLVMConstReal(LLVMFloatType(), 0.25);
890 LLVMValueRef arx_ge_ary
, arx_ge_arz
;
891 LLVMValueRef ary_ge_arx
, ary_ge_arz
;
892 LLVMValueRef arx_ge_ary_arz
, ary_ge_arx_arz
;
893 LLVMValueRef rx_pos
, ry_pos
, rz_pos
;
895 assert(bld
->coord_bld
.type
.length
== 4);
898 * Use the average of the four pixel's texcoords to choose the face.
900 rx
= lp_build_mul(float_bld
, c25
,
901 lp_build_sum_vector(&bld
->coord_bld
, s
));
902 ry
= lp_build_mul(float_bld
, c25
,
903 lp_build_sum_vector(&bld
->coord_bld
, t
));
904 rz
= lp_build_mul(float_bld
, c25
,
905 lp_build_sum_vector(&bld
->coord_bld
, r
));
907 arx
= lp_build_abs(float_bld
, rx
);
908 ary
= lp_build_abs(float_bld
, ry
);
909 arz
= lp_build_abs(float_bld
, rz
);
912 * Compare sign/magnitude of rx,ry,rz to determine face
914 arx_ge_ary
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, arx
, ary
, "");
915 arx_ge_arz
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, arx
, arz
, "");
916 ary_ge_arx
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ary
, arx
, "");
917 ary_ge_arz
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ary
, arz
, "");
919 arx_ge_ary_arz
= LLVMBuildAnd(bld
->builder
, arx_ge_ary
, arx_ge_arz
, "");
920 ary_ge_arx_arz
= LLVMBuildAnd(bld
->builder
, ary_ge_arx
, ary_ge_arz
, "");
922 rx_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, rx
, float_bld
->zero
, "");
923 ry_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, ry
, float_bld
->zero
, "");
924 rz_pos
= LLVMBuildFCmp(bld
->builder
, LLVMRealUGE
, rz
, float_bld
->zero
, "");
927 struct lp_build_if_state if_ctx
;
928 LLVMValueRef face_s_var
;
929 LLVMValueRef face_t_var
;
930 LLVMValueRef face_var
;
932 face_s_var
= lp_build_alloca(bld
->builder
, bld
->coord_bld
.vec_type
, "face_s_var");
933 face_t_var
= lp_build_alloca(bld
->builder
, bld
->coord_bld
.vec_type
, "face_t_var");
934 face_var
= lp_build_alloca(bld
->builder
, bld
->int_bld
.vec_type
, "face_var");
936 lp_build_if(&if_ctx
, bld
->builder
, arx_ge_ary_arz
);
939 LLVMValueRef sign
= lp_build_sgn(float_bld
, rx
);
940 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, s
);
941 *face_s
= lp_build_cube_coord(coord_bld
, sign
, +1, r
, ima
);
942 *face_t
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
943 *face
= lp_build_cube_face(bld
, rx
,
945 PIPE_TEX_FACE_NEG_X
);
946 LLVMBuildStore(bld
->builder
, *face_s
, face_s_var
);
947 LLVMBuildStore(bld
->builder
, *face_t
, face_t_var
);
948 LLVMBuildStore(bld
->builder
, *face
, face_var
);
950 lp_build_else(&if_ctx
);
952 struct lp_build_if_state if_ctx2
;
954 ary_ge_arx_arz
= LLVMBuildAnd(bld
->builder
, ary_ge_arx
, ary_ge_arz
, "");
956 lp_build_if(&if_ctx2
, bld
->builder
, ary_ge_arx_arz
);
959 LLVMValueRef sign
= lp_build_sgn(float_bld
, ry
);
960 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, t
);
961 *face_s
= lp_build_cube_coord(coord_bld
, NULL
, -1, s
, ima
);
962 *face_t
= lp_build_cube_coord(coord_bld
, sign
, -1, r
, ima
);
963 *face
= lp_build_cube_face(bld
, ry
,
965 PIPE_TEX_FACE_NEG_Y
);
966 LLVMBuildStore(bld
->builder
, *face_s
, face_s_var
);
967 LLVMBuildStore(bld
->builder
, *face_t
, face_t_var
);
968 LLVMBuildStore(bld
->builder
, *face
, face_var
);
970 lp_build_else(&if_ctx2
);
973 LLVMValueRef sign
= lp_build_sgn(float_bld
, rz
);
974 LLVMValueRef ima
= lp_build_cube_ima(coord_bld
, r
);
975 *face_s
= lp_build_cube_coord(coord_bld
, sign
, -1, s
, ima
);
976 *face_t
= lp_build_cube_coord(coord_bld
, NULL
, +1, t
, ima
);
977 *face
= lp_build_cube_face(bld
, rz
,
979 PIPE_TEX_FACE_NEG_Z
);
980 LLVMBuildStore(bld
->builder
, *face_s
, face_s_var
);
981 LLVMBuildStore(bld
->builder
, *face_t
, face_t_var
);
982 LLVMBuildStore(bld
->builder
, *face
, face_var
);
984 lp_build_endif(&if_ctx2
);
987 lp_build_endif(&if_ctx
);
989 *face_s
= LLVMBuildLoad(bld
->builder
, face_s_var
, "face_s");
990 *face_t
= LLVMBuildLoad(bld
->builder
, face_t_var
, "face_t");
991 *face
= LLVMBuildLoad(bld
->builder
, face_var
, "face");
997 * Compute the partial offset of a pixel block along an arbitrary axis.
999 * @param coord coordinate in pixels
1000 * @param stride number of bytes between rows of successive pixel blocks
1001 * @param block_length number of pixels in a pixels block along the coordinate
1003 * @param out_offset resulting relative offset of the pixel block in bytes
1004 * @param out_subcoord resulting sub-block pixel coordinate
1007 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
1008 unsigned block_length
,
1010 LLVMValueRef stride
,
1011 LLVMValueRef
*out_offset
,
1012 LLVMValueRef
*out_subcoord
)
1014 LLVMValueRef offset
;
1015 LLVMValueRef subcoord
;
1017 if (block_length
== 1) {
1018 subcoord
= bld
->zero
;
1022 * Pixel blocks have power of two dimensions. LLVM should convert the
1023 * rem/div to bit arithmetic.
1024 * TODO: Verify this.
1025 * It does indeed BUT it does transform it to scalar (and back) when doing so
1026 * (using roughly extract, shift/and, mov, unpack) (llvm 2.7).
1027 * The generated code looks seriously unfunny and is quite expensive.
1030 LLVMValueRef block_width
= lp_build_const_int_vec(bld
->type
, block_length
);
1031 subcoord
= LLVMBuildURem(bld
->builder
, coord
, block_width
, "");
1032 coord
= LLVMBuildUDiv(bld
->builder
, coord
, block_width
, "");
1034 unsigned logbase2
= util_unsigned_logbase2(block_length
);
1035 LLVMValueRef block_shift
= lp_build_const_int_vec(bld
->type
, logbase2
);
1036 LLVMValueRef block_mask
= lp_build_const_int_vec(bld
->type
, block_length
- 1);
1037 subcoord
= LLVMBuildAnd(bld
->builder
, coord
, block_mask
, "");
1038 coord
= LLVMBuildLShr(bld
->builder
, coord
, block_shift
, "");
1042 offset
= lp_build_mul(bld
, coord
, stride
);
1045 assert(out_subcoord
);
1047 *out_offset
= offset
;
1048 *out_subcoord
= subcoord
;
1053 * Compute the offset of a pixel block.
1055 * x, y, z, y_stride, z_stride are vectors, and they refer to pixels.
1057 * Returns the relative offset and i,j sub-block coordinates
1060 lp_build_sample_offset(struct lp_build_context
*bld
,
1061 const struct util_format_description
*format_desc
,
1065 LLVMValueRef y_stride
,
1066 LLVMValueRef z_stride
,
1067 LLVMValueRef
*out_offset
,
1068 LLVMValueRef
*out_i
,
1069 LLVMValueRef
*out_j
)
1071 LLVMValueRef x_stride
;
1072 LLVMValueRef offset
;
1074 x_stride
= lp_build_const_vec(bld
->type
, format_desc
->block
.bits
/8);
1076 lp_build_sample_partial_offset(bld
,
1077 format_desc
->block
.width
,
1081 if (y
&& y_stride
) {
1082 LLVMValueRef y_offset
;
1083 lp_build_sample_partial_offset(bld
,
1084 format_desc
->block
.height
,
1087 offset
= lp_build_add(bld
, offset
, y_offset
);
1093 if (z
&& z_stride
) {
1094 LLVMValueRef z_offset
;
1096 lp_build_sample_partial_offset(bld
,
1097 1, /* pixel blocks are always 2D */
1100 offset
= lp_build_add(bld
, offset
, z_offset
);
1103 *out_offset
= offset
;