Merge remote branch 'origin/master' into nv50-compiler
[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_sample.h"
44
45
46 /**
47 * Initialize lp_sampler_static_state object with the gallium sampler
48 * and texture state.
49 * The former is considered to be static and the later dynamic.
50 */
51 void
52 lp_sampler_static_state(struct lp_sampler_static_state *state,
53 const struct pipe_sampler_view *view,
54 const struct pipe_sampler_state *sampler)
55 {
56 const struct pipe_resource *texture = view->texture;
57
58 memset(state, 0, sizeof *state);
59
60 if(!texture)
61 return;
62
63 if(!sampler)
64 return;
65
66 /*
67 * We don't copy sampler state over unless it is actually enabled, to avoid
68 * spurious recompiles, as the sampler static state is part of the shader
69 * key.
70 *
71 * Ideally the state tracker or cso_cache module would make all state
72 * canonical, but until that happens it's better to be safe than sorry here.
73 *
74 * XXX: Actually there's much more than can be done here, especially
75 * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
76 */
77
78 state->format = view->format;
79 state->swizzle_r = view->swizzle_r;
80 state->swizzle_g = view->swizzle_g;
81 state->swizzle_b = view->swizzle_b;
82 state->swizzle_a = view->swizzle_a;
83
84 state->target = texture->target;
85 state->pot_width = util_is_power_of_two(texture->width0);
86 state->pot_height = util_is_power_of_two(texture->height0);
87 state->pot_depth = util_is_power_of_two(texture->depth0);
88
89 state->wrap_s = sampler->wrap_s;
90 state->wrap_t = sampler->wrap_t;
91 state->wrap_r = sampler->wrap_r;
92 state->min_img_filter = sampler->min_img_filter;
93 state->mag_img_filter = sampler->mag_img_filter;
94 if (view->last_level) {
95 state->min_mip_filter = sampler->min_mip_filter;
96 } else {
97 state->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
98 }
99
100 state->compare_mode = sampler->compare_mode;
101 if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) {
102 state->compare_func = sampler->compare_func;
103 }
104
105 state->normalized_coords = sampler->normalized_coords;
106 state->lod_bias = sampler->lod_bias;
107 if (!view->last_level &&
108 sampler->min_img_filter == sampler->mag_img_filter) {
109 state->min_lod = 0.0f;
110 state->max_lod = 0.0f;
111 } else {
112 state->min_lod = MAX2(sampler->min_lod, 0.0f);
113 state->max_lod = sampler->max_lod;
114 }
115 state->border_color[0] = sampler->border_color[0];
116 state->border_color[1] = sampler->border_color[1];
117 state->border_color[2] = sampler->border_color[2];
118 state->border_color[3] = sampler->border_color[3];
119
120 /*
121 * FIXME: Handle the remainder of pipe_sampler_view.
122 */
123 }
124
125
126 /**
127 * Compute the partial offset of a pixel block along an arbitrary axis.
128 *
129 * @param coord coordinate in pixels
130 * @param stride number of bytes between rows of successive pixel blocks
131 * @param block_length number of pixels in a pixels block along the coordinate
132 * axis
133 * @param out_offset resulting relative offset of the pixel block in bytes
134 * @param out_subcoord resulting sub-block pixel coordinate
135 */
136 void
137 lp_build_sample_partial_offset(struct lp_build_context *bld,
138 unsigned block_length,
139 LLVMValueRef coord,
140 LLVMValueRef stride,
141 LLVMValueRef *out_offset,
142 LLVMValueRef *out_subcoord)
143 {
144 LLVMValueRef offset;
145 LLVMValueRef subcoord;
146
147 if (block_length == 1) {
148 subcoord = bld->zero;
149 }
150 else {
151 /*
152 * Pixel blocks have power of two dimensions. LLVM should convert the
153 * rem/div to bit arithmetic.
154 * TODO: Verify this.
155 */
156
157 LLVMValueRef block_width = lp_build_const_int_vec(bld->type, block_length);
158 subcoord = LLVMBuildURem(bld->builder, coord, block_width, "");
159 coord = LLVMBuildUDiv(bld->builder, coord, block_width, "");
160 }
161
162 offset = lp_build_mul(bld, coord, stride);
163
164 assert(out_offset);
165 assert(out_subcoord);
166
167 *out_offset = offset;
168 *out_subcoord = subcoord;
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 pixels.
176 *
177 * Returns the relative offset and i,j sub-block coordinates
178 */
179 void
180 lp_build_sample_offset(struct lp_build_context *bld,
181 const struct util_format_description *format_desc,
182 LLVMValueRef x,
183 LLVMValueRef y,
184 LLVMValueRef z,
185 LLVMValueRef y_stride,
186 LLVMValueRef z_stride,
187 LLVMValueRef *out_offset,
188 LLVMValueRef *out_i,
189 LLVMValueRef *out_j)
190 {
191 LLVMValueRef x_stride;
192 LLVMValueRef offset;
193
194 x_stride = lp_build_const_vec(bld->type, format_desc->block.bits/8);
195
196 lp_build_sample_partial_offset(bld,
197 format_desc->block.width,
198 x, x_stride,
199 &offset, out_i);
200
201 if (y && y_stride) {
202 LLVMValueRef y_offset;
203 lp_build_sample_partial_offset(bld,
204 format_desc->block.height,
205 y, y_stride,
206 &y_offset, out_j);
207 offset = lp_build_add(bld, offset, y_offset);
208 }
209 else {
210 *out_j = bld->zero;
211 }
212
213 if (z && z_stride) {
214 LLVMValueRef z_offset;
215 LLVMValueRef k;
216 lp_build_sample_partial_offset(bld,
217 1, /* pixel blocks are always 2D */
218 z, z_stride,
219 &z_offset, &k);
220 offset = lp_build_add(bld, offset, z_offset);
221 }
222
223 *out_offset = offset;
224 }