Merge branch 'gallium-msaa'
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_sample.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Texture sampling -- common code.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
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"
45
46
47 /**
48 * Initialize lp_sampler_static_state object with the gallium sampler
49 * and texture state.
50 * The former is considered to be static and the later dynamic.
51 */
52 void
53 lp_sampler_static_state(struct lp_sampler_static_state *state,
54 const struct pipe_sampler_view *view,
55 const struct pipe_sampler_state *sampler)
56 {
57 const struct pipe_resource *texture = view->texture;
58
59 memset(state, 0, sizeof *state);
60
61 if(!texture)
62 return;
63
64 if(!sampler)
65 return;
66
67 /*
68 * We don't copy sampler state over unless it is actually enabled, to avoid
69 * spurious recompiles, as the sampler static state is part of the shader
70 * key.
71 *
72 * Ideally the state tracker or cso_cache module would make all state
73 * canonical, but until that happens it's better to be safe than sorry here.
74 *
75 * XXX: Actually there's much more than can be done here, especially
76 * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
77 */
78
79 state->format = view->format;
80 state->swizzle_r = view->swizzle_r;
81 state->swizzle_g = view->swizzle_g;
82 state->swizzle_b = view->swizzle_b;
83 state->swizzle_a = view->swizzle_a;
84
85 state->target = texture->target;
86 state->pot_width = util_is_pot(texture->width0);
87 state->pot_height = util_is_pot(texture->height0);
88 state->pot_depth = util_is_pot(texture->depth0);
89
90 state->wrap_s = sampler->wrap_s;
91 state->wrap_t = sampler->wrap_t;
92 state->wrap_r = sampler->wrap_r;
93 state->min_img_filter = sampler->min_img_filter;
94 state->mag_img_filter = sampler->mag_img_filter;
95 if (texture->last_level) {
96 state->min_mip_filter = sampler->min_mip_filter;
97 } else {
98 state->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
99 }
100
101 state->compare_mode = sampler->compare_mode;
102 if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) {
103 state->compare_func = sampler->compare_func;
104 }
105
106 state->normalized_coords = sampler->normalized_coords;
107 state->lod_bias = sampler->lod_bias;
108 state->min_lod = sampler->min_lod;
109 state->max_lod = sampler->max_lod;
110 state->border_color[0] = sampler->border_color[0];
111 state->border_color[1] = sampler->border_color[1];
112 state->border_color[2] = sampler->border_color[2];
113 state->border_color[3] = sampler->border_color[3];
114
115 /*
116 * FIXME: Handle the remainder of pipe_sampler_view.
117 */
118 }
119
120
121 /**
122 * Gather elements from scatter positions in memory into a single vector.
123 * Use for fetching texels from a texture.
124 * For SSE, typical values are length=4, src_width=32, dst_width=32.
125 *
126 * @param length length of the offsets
127 * @param src_width src element width in bits
128 * @param dst_width result element width in bits (src will be expanded to fit)
129 * @param base_ptr base pointer, should be a i8 pointer type.
130 * @param offsets vector with offsets
131 */
132 LLVMValueRef
133 lp_build_gather(LLVMBuilderRef builder,
134 unsigned length,
135 unsigned src_width,
136 unsigned dst_width,
137 LLVMValueRef base_ptr,
138 LLVMValueRef offsets)
139 {
140 LLVMTypeRef src_type = LLVMIntType(src_width);
141 LLVMTypeRef src_ptr_type = LLVMPointerType(src_type, 0);
142 LLVMTypeRef dst_elem_type = LLVMIntType(dst_width);
143 LLVMTypeRef dst_vec_type = LLVMVectorType(dst_elem_type, length);
144 LLVMValueRef res;
145 unsigned i;
146
147 res = LLVMGetUndef(dst_vec_type);
148 for(i = 0; i < length; ++i) {
149 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
150 LLVMValueRef elem_offset;
151 LLVMValueRef elem_ptr;
152 LLVMValueRef elem;
153
154 elem_offset = LLVMBuildExtractElement(builder, offsets, index, "");
155 elem_ptr = LLVMBuildGEP(builder, base_ptr, &elem_offset, 1, "");
156 elem_ptr = LLVMBuildBitCast(builder, elem_ptr, src_ptr_type, "");
157 elem = LLVMBuildLoad(builder, elem_ptr, "");
158
159 assert(src_width <= dst_width);
160 if(src_width > dst_width)
161 elem = LLVMBuildTrunc(builder, elem, dst_elem_type, "");
162 if(src_width < dst_width)
163 elem = LLVMBuildZExt(builder, elem, dst_elem_type, "");
164
165 res = LLVMBuildInsertElement(builder, res, elem, index, "");
166 }
167
168 return res;
169 }
170
171
172 /**
173 * Compute the offset of a pixel block.
174 *
175 * x, y, z, y_stride, z_stride are vectors, and they refer to pixel blocks, as
176 * per format description, and not individual pixels.
177 */
178 LLVMValueRef
179 lp_build_sample_offset(struct lp_build_context *bld,
180 const struct util_format_description *format_desc,
181 LLVMValueRef x,
182 LLVMValueRef y,
183 LLVMValueRef z,
184 LLVMValueRef y_stride,
185 LLVMValueRef z_stride)
186 {
187 LLVMValueRef x_stride;
188 LLVMValueRef offset;
189
190 x_stride = lp_build_const_vec(bld->type, format_desc->block.bits/8);
191 offset = lp_build_mul(bld, x, x_stride);
192
193 if (y && y_stride) {
194 LLVMValueRef y_offset = lp_build_mul(bld, y, y_stride);
195 offset = lp_build_add(bld, offset, y_offset);
196 }
197
198 if (z && z_stride) {
199 LLVMValueRef z_offset = lp_build_mul(bld, z, z_stride);
200 offset = lp_build_add(bld, offset, z_offset);
201 }
202
203 return offset;
204 }