Squashed commit of the following:
[profile/ivi/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_resource *texture,
55                         const struct pipe_sampler_state *sampler)
56 {
57    memset(state, 0, sizeof *state);
58
59    if(!texture)
60       return;
61
62    if(!sampler)
63       return;
64
65    /*
66     * We don't copy sampler state over unless it is actually enabled, to avoid
67     * spurious recompiles, as the sampler static state is part of the shader
68     * key.
69     *
70     * Ideally the state tracker or cso_cache module would make all state
71     * canonical, but until that happens it's better to be safe than sorry here.
72     *
73     * XXX: Actually there's much more than can be done here, especially
74     * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
75     */
76
77    state->format            = texture->format;
78    state->target            = texture->target;
79    state->pot_width         = util_is_pot(texture->width0);
80    state->pot_height        = util_is_pot(texture->height0);
81    state->pot_depth         = util_is_pot(texture->depth0);
82
83    state->wrap_s            = sampler->wrap_s;
84    state->wrap_t            = sampler->wrap_t;
85    state->wrap_r            = sampler->wrap_r;
86    state->min_img_filter    = sampler->min_img_filter;
87    state->mag_img_filter    = sampler->mag_img_filter;
88    if (texture->last_level) {
89       state->min_mip_filter = sampler->min_mip_filter;
90    } else {
91       state->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
92    }
93
94    state->compare_mode      = sampler->compare_mode;
95    if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) {
96       state->compare_func   = sampler->compare_func;
97    }
98
99    state->normalized_coords = sampler->normalized_coords;
100    state->lod_bias          = sampler->lod_bias;
101    state->min_lod           = sampler->min_lod;
102    state->max_lod           = sampler->max_lod;
103    state->border_color[0]   = sampler->border_color[0];
104    state->border_color[1]   = sampler->border_color[1];
105    state->border_color[2]   = sampler->border_color[2];
106    state->border_color[3]   = sampler->border_color[3];
107 }
108
109
110 /**
111  * Gather elements from scatter positions in memory into a single vector.
112  *
113  * @param src_width src element width
114  * @param dst_width result element width (source will be expanded to fit)
115  * @param length length of the offsets,
116  * @param base_ptr base pointer, should be a i8 pointer type.
117  * @param offsets vector with offsets
118  */
119 LLVMValueRef
120 lp_build_gather(LLVMBuilderRef builder,
121                 unsigned length,
122                 unsigned src_width,
123                 unsigned dst_width,
124                 LLVMValueRef base_ptr,
125                 LLVMValueRef offsets)
126 {
127    LLVMTypeRef src_type = LLVMIntType(src_width);
128    LLVMTypeRef src_ptr_type = LLVMPointerType(src_type, 0);
129    LLVMTypeRef dst_elem_type = LLVMIntType(dst_width);
130    LLVMTypeRef dst_vec_type = LLVMVectorType(dst_elem_type, length);
131    LLVMValueRef res;
132    unsigned i;
133
134    res = LLVMGetUndef(dst_vec_type);
135    for(i = 0; i < length; ++i) {
136       LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
137       LLVMValueRef elem_offset;
138       LLVMValueRef elem_ptr;
139       LLVMValueRef elem;
140
141       elem_offset = LLVMBuildExtractElement(builder, offsets, index, "");
142       elem_ptr = LLVMBuildGEP(builder, base_ptr, &elem_offset, 1, "");
143       elem_ptr = LLVMBuildBitCast(builder, elem_ptr, src_ptr_type, "");
144       elem = LLVMBuildLoad(builder, elem_ptr, "");
145
146       assert(src_width <= dst_width);
147       if(src_width > dst_width)
148          elem = LLVMBuildTrunc(builder, elem, dst_elem_type, "");
149       if(src_width < dst_width)
150          elem = LLVMBuildZExt(builder, elem, dst_elem_type, "");
151
152       res = LLVMBuildInsertElement(builder, res, elem, index, "");
153    }
154
155    return res;
156 }
157
158
159 /**
160  * Compute the offset of a pixel block.
161  *
162  * x, y, z, y_stride, z_stride are vectors, and they refer to pixel blocks, as
163  * per format description, and not individual pixels.
164  */
165 LLVMValueRef
166 lp_build_sample_offset(struct lp_build_context *bld,
167                        const struct util_format_description *format_desc,
168                        LLVMValueRef x,
169                        LLVMValueRef y,
170                        LLVMValueRef z,
171                        LLVMValueRef y_stride,
172                        LLVMValueRef z_stride)
173 {
174    LLVMValueRef x_stride;
175    LLVMValueRef offset;
176
177    x_stride = lp_build_const_vec(bld->type, format_desc->block.bits/8);
178
179    if(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
180       LLVMValueRef x_lo, x_hi;
181       LLVMValueRef y_lo, y_hi;
182       LLVMValueRef x_stride_lo, x_stride_hi;
183       LLVMValueRef y_stride_lo, y_stride_hi;
184       LLVMValueRef x_offset_lo, x_offset_hi;
185       LLVMValueRef y_offset_lo, y_offset_hi;
186       LLVMValueRef offset_lo, offset_hi;
187
188       /* XXX 1D & 3D addressing not done yet */
189       assert(!z);
190       assert(!z_stride);
191
192       x_lo = LLVMBuildAnd(bld->builder, x, bld->one, "");
193       y_lo = LLVMBuildAnd(bld->builder, y, bld->one, "");
194
195       x_hi = LLVMBuildLShr(bld->builder, x, bld->one, "");
196       y_hi = LLVMBuildLShr(bld->builder, y, bld->one, "");
197
198       x_stride_lo = x_stride;
199       y_stride_lo = lp_build_const_vec(bld->type, 2*format_desc->block.bits/8);
200
201       x_stride_hi = lp_build_const_vec(bld->type, 4*format_desc->block.bits/8);
202       y_stride_hi = LLVMBuildShl(bld->builder, y_stride, bld->one, "");
203
204       x_offset_lo = lp_build_mul(bld, x_lo, x_stride_lo);
205       y_offset_lo = lp_build_mul(bld, y_lo, y_stride_lo);
206       offset_lo = lp_build_add(bld, x_offset_lo, y_offset_lo);
207
208       x_offset_hi = lp_build_mul(bld, x_hi, x_stride_hi);
209       y_offset_hi = lp_build_mul(bld, y_hi, y_stride_hi);
210       offset_hi = lp_build_add(bld, x_offset_hi, y_offset_hi);
211
212       offset = lp_build_add(bld, offset_hi, offset_lo);
213    }
214    else {
215       offset = lp_build_mul(bld, x, x_stride);
216
217       if (y && y_stride) {
218          LLVMValueRef y_offset = lp_build_mul(bld, y, y_stride);
219          offset = lp_build_add(bld, offset, y_offset);
220       }
221
222       if (z && z_stride) {
223          LLVMValueRef z_offset = lp_build_mul(bld, z, z_stride);
224          offset = lp_build_add(bld, offset, z_offset);
225       }
226    }
227
228    return offset;
229 }