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_debug.h"
40 #include "lp_bld_const.h"
41 #include "lp_bld_arit.h"
42 #include "lp_bld_type.h"
43 #include "lp_bld_format.h"
44 #include "lp_bld_sample.h"
48 * Initialize lp_sampler_static_state object with the gallium sampler
50 * The former is considered to be static and the later dynamic.
53 lp_sampler_static_state(struct lp_sampler_static_state
*state
,
54 const struct pipe_texture
*texture
,
55 const struct pipe_sampler_state
*sampler
)
57 memset(state
, 0, sizeof *state
);
65 state
->format
= texture
->format
;
66 state
->target
= texture
->target
;
67 state
->pot_width
= util_is_pot(texture
->width0
);
68 state
->pot_height
= util_is_pot(texture
->height0
);
69 state
->pot_depth
= util_is_pot(texture
->depth0
);
71 state
->wrap_s
= sampler
->wrap_s
;
72 state
->wrap_t
= sampler
->wrap_t
;
73 state
->wrap_r
= sampler
->wrap_r
;
74 state
->min_img_filter
= sampler
->min_img_filter
;
75 state
->min_mip_filter
= sampler
->min_mip_filter
;
76 state
->mag_img_filter
= sampler
->mag_img_filter
;
77 state
->compare_mode
= sampler
->compare_mode
;
78 state
->compare_func
= sampler
->compare_func
;
79 state
->normalized_coords
= sampler
->normalized_coords
;
80 state
->lod_bias
= sampler
->lod_bias
;
81 state
->min_lod
= sampler
->min_lod
;
82 state
->max_lod
= sampler
->max_lod
;
83 state
->border_color
[0] = sampler
->border_color
[0];
84 state
->border_color
[1] = sampler
->border_color
[1];
85 state
->border_color
[2] = sampler
->border_color
[2];
86 state
->border_color
[3] = sampler
->border_color
[3];
91 * Gather elements from scatter positions in memory into a single vector.
93 * @param src_width src element width
94 * @param dst_width result element width (source will be expanded to fit)
95 * @param length length of the offsets,
96 * @param base_ptr base pointer, should be a i8 pointer type.
97 * @param offsets vector with offsets
100 lp_build_gather(LLVMBuilderRef builder
,
104 LLVMValueRef base_ptr
,
105 LLVMValueRef offsets
)
107 LLVMTypeRef src_type
= LLVMIntType(src_width
);
108 LLVMTypeRef src_ptr_type
= LLVMPointerType(src_type
, 0);
109 LLVMTypeRef dst_elem_type
= LLVMIntType(dst_width
);
110 LLVMTypeRef dst_vec_type
= LLVMVectorType(dst_elem_type
, length
);
114 res
= LLVMGetUndef(dst_vec_type
);
115 for(i
= 0; i
< length
; ++i
) {
116 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
117 LLVMValueRef elem_offset
;
118 LLVMValueRef elem_ptr
;
121 elem_offset
= LLVMBuildExtractElement(builder
, offsets
, index
, "");
122 elem_ptr
= LLVMBuildGEP(builder
, base_ptr
, &elem_offset
, 1, "");
123 elem_ptr
= LLVMBuildBitCast(builder
, elem_ptr
, src_ptr_type
, "");
124 elem
= LLVMBuildLoad(builder
, elem_ptr
, "");
126 assert(src_width
<= dst_width
);
127 if(src_width
> dst_width
)
128 elem
= LLVMBuildTrunc(builder
, elem
, dst_elem_type
, "");
129 if(src_width
< dst_width
)
130 elem
= LLVMBuildZExt(builder
, elem
, dst_elem_type
, "");
132 res
= LLVMBuildInsertElement(builder
, res
, elem
, index
, "");
140 * Compute the offset of a pixel.
142 * x, y, y_stride are vectors
145 lp_build_sample_offset(struct lp_build_context
*bld
,
146 const struct util_format_description
*format_desc
,
149 LLVMValueRef y_stride
,
150 LLVMValueRef data_ptr
)
152 LLVMValueRef x_stride
;
155 x_stride
= lp_build_const_scalar(bld
->type
, format_desc
->block
.bits
/8);
157 if(format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
158 LLVMValueRef x_lo
, x_hi
;
159 LLVMValueRef y_lo
, y_hi
;
160 LLVMValueRef x_stride_lo
, x_stride_hi
;
161 LLVMValueRef y_stride_lo
, y_stride_hi
;
162 LLVMValueRef x_offset_lo
, x_offset_hi
;
163 LLVMValueRef y_offset_lo
, y_offset_hi
;
164 LLVMValueRef offset_lo
, offset_hi
;
166 x_lo
= LLVMBuildAnd(bld
->builder
, x
, bld
->one
, "");
167 y_lo
= LLVMBuildAnd(bld
->builder
, y
, bld
->one
, "");
169 x_hi
= LLVMBuildLShr(bld
->builder
, x
, bld
->one
, "");
170 y_hi
= LLVMBuildLShr(bld
->builder
, y
, bld
->one
, "");
172 x_stride_lo
= x_stride
;
173 y_stride_lo
= lp_build_const_scalar(bld
->type
, 2*format_desc
->block
.bits
/8);
175 x_stride_hi
= lp_build_const_scalar(bld
->type
, 4*format_desc
->block
.bits
/8);
176 y_stride_hi
= LLVMBuildShl(bld
->builder
, y_stride
, bld
->one
, "");
178 x_offset_lo
= lp_build_mul(bld
, x_lo
, x_stride_lo
);
179 y_offset_lo
= lp_build_mul(bld
, y_lo
, y_stride_lo
);
180 offset_lo
= lp_build_add(bld
, x_offset_lo
, y_offset_lo
);
182 x_offset_hi
= lp_build_mul(bld
, x_hi
, x_stride_hi
);
183 y_offset_hi
= lp_build_mul(bld
, y_hi
, y_stride_hi
);
184 offset_hi
= lp_build_add(bld
, x_offset_hi
, y_offset_hi
);
186 offset
= lp_build_add(bld
, offset_hi
, offset_lo
);
189 LLVMValueRef x_offset
;
190 LLVMValueRef y_offset
;
192 x_offset
= lp_build_mul(bld
, x
, x_stride
);
193 y_offset
= lp_build_mul(bld
, y
, y_stride
);
195 offset
= lp_build_add(bld
, x_offset
, y_offset
);