llvmpipe: make min/max lod and lod bias dynamic state
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_sample.h
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.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35 #ifndef LP_BLD_SAMPLE_H
36 #define LP_BLD_SAMPLE_H
37
38
39 #include "pipe/p_format.h"
40 #include "util/u_debug.h"
41 #include "gallivm/lp_bld.h"
42 #include "gallivm/lp_bld_type.h"
43 #include "gallivm/lp_bld_swizzle.h"
44
45
46 struct pipe_resource;
47 struct pipe_sampler_view;
48 struct pipe_sampler_state;
49 struct util_format_description;
50 struct lp_type;
51 struct lp_build_context;
52
53
54 /**
55 * Sampler static state.
56 *
57 * These are the bits of state from pipe_resource and pipe_sampler_state that
58 * are embedded in the generated code.
59 */
60 struct lp_sampler_static_state
61 {
62 /* pipe_sampler_view's state */
63 enum pipe_format format;
64 unsigned swizzle_r:3; /**< PIPE_SWIZZLE_* */
65 unsigned swizzle_g:3;
66 unsigned swizzle_b:3;
67 unsigned swizzle_a:3;
68
69 /* pipe_texture's state */
70 unsigned target:3; /**< PIPE_TEXTURE_* */
71 unsigned pot_width:1; /**< is the width a power of two? */
72 unsigned pot_height:1;
73 unsigned pot_depth:1;
74
75 /* pipe_sampler_state's state */
76 unsigned wrap_s:3;
77 unsigned wrap_t:3;
78 unsigned wrap_r:3;
79 unsigned min_img_filter:2;
80 unsigned min_mip_filter:2;
81 unsigned mag_img_filter:2;
82 unsigned compare_mode:1;
83 unsigned compare_func:3;
84 unsigned normalized_coords:1;
85 float border_color[4];
86 unsigned min_max_lod_equal:1; /**< min_lod == max_lod ? */
87 float min_max_lod; /**< only valid when min_max_lod_equal=1 */
88
89 /* Aero hacks */
90 unsigned force_nearest_s:1;
91 unsigned force_nearest_t:1;
92 };
93
94
95 /**
96 * Sampler dynamic state.
97 *
98 * These are the bits of state from pipe_resource and pipe_sampler_state that
99 * are computed in runtime.
100 *
101 * There are obtained through callbacks, as we don't want to tie the texture
102 * sampling code generation logic to any particular texture layout or pipe
103 * driver.
104 */
105 struct lp_sampler_dynamic_state
106 {
107
108 /** Obtain the base texture width. */
109 LLVMValueRef
110 (*width)( const struct lp_sampler_dynamic_state *state,
111 LLVMBuilderRef builder,
112 unsigned unit);
113
114 /** Obtain the base texture height. */
115 LLVMValueRef
116 (*height)( const struct lp_sampler_dynamic_state *state,
117 LLVMBuilderRef builder,
118 unsigned unit);
119
120 /** Obtain the base texture depth. */
121 LLVMValueRef
122 (*depth)( const struct lp_sampler_dynamic_state *state,
123 LLVMBuilderRef builder,
124 unsigned unit);
125
126 /** Obtain the number of mipmap levels (minus one). */
127 LLVMValueRef
128 (*last_level)( const struct lp_sampler_dynamic_state *state,
129 LLVMBuilderRef builder,
130 unsigned unit);
131
132 LLVMValueRef
133 (*row_stride)( const struct lp_sampler_dynamic_state *state,
134 LLVMBuilderRef builder,
135 unsigned unit);
136
137 LLVMValueRef
138 (*img_stride)( const struct lp_sampler_dynamic_state *state,
139 LLVMBuilderRef builder,
140 unsigned unit);
141
142 LLVMValueRef
143 (*data_ptr)( const struct lp_sampler_dynamic_state *state,
144 LLVMBuilderRef builder,
145 unsigned unit);
146
147 /** Obtain texture min lod */
148 LLVMValueRef
149 (*min_lod)(const struct lp_sampler_dynamic_state *state,
150 LLVMBuilderRef builder, unsigned unit);
151
152 /** Obtain texture max lod */
153 LLVMValueRef
154 (*max_lod)(const struct lp_sampler_dynamic_state *state,
155 LLVMBuilderRef builder, unsigned unit);
156
157 /** Obtain texture lod bias */
158 LLVMValueRef
159 (*lod_bias)(const struct lp_sampler_dynamic_state *state,
160 LLVMBuilderRef builder, unsigned unit);
161 };
162
163
164 /**
165 * Keep all information for sampling code generation in a single place.
166 */
167 struct lp_build_sample_context
168 {
169 LLVMBuilderRef builder;
170
171 const struct lp_sampler_static_state *static_state;
172
173 struct lp_sampler_dynamic_state *dynamic_state;
174
175 const struct util_format_description *format_desc;
176
177 /** regular scalar float type */
178 struct lp_type float_type;
179 struct lp_build_context float_bld;
180
181 /** regular scalar float type */
182 struct lp_type int_type;
183 struct lp_build_context int_bld;
184
185 /** Incoming coordinates type and build context */
186 struct lp_type coord_type;
187 struct lp_build_context coord_bld;
188
189 /** Unsigned integer coordinates */
190 struct lp_type uint_coord_type;
191 struct lp_build_context uint_coord_bld;
192
193 /** Signed integer coordinates */
194 struct lp_type int_coord_type;
195 struct lp_build_context int_coord_bld;
196
197 /** Output texels type and build context */
198 struct lp_type texel_type;
199 struct lp_build_context texel_bld;
200 };
201
202
203
204 /**
205 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
206 * this time. Return whether the given mode is supported by that function.
207 */
208 static INLINE boolean
209 lp_is_simple_wrap_mode(unsigned mode)
210 {
211 switch (mode) {
212 case PIPE_TEX_WRAP_REPEAT:
213 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
214 return TRUE;
215 default:
216 return FALSE;
217 }
218 }
219
220
221 static INLINE void
222 apply_sampler_swizzle(struct lp_build_sample_context *bld,
223 LLVMValueRef *texel)
224 {
225 unsigned char swizzles[4];
226
227 swizzles[0] = bld->static_state->swizzle_r;
228 swizzles[1] = bld->static_state->swizzle_g;
229 swizzles[2] = bld->static_state->swizzle_b;
230 swizzles[3] = bld->static_state->swizzle_a;
231
232 lp_build_swizzle_soa_inplace(&bld->texel_bld, texel, swizzles);
233 }
234
235
236 static INLINE int
237 texture_dims(enum pipe_texture_target tex)
238 {
239 switch (tex) {
240 case PIPE_TEXTURE_1D:
241 return 1;
242 case PIPE_TEXTURE_2D:
243 case PIPE_TEXTURE_RECT:
244 case PIPE_TEXTURE_CUBE:
245 return 2;
246 case PIPE_TEXTURE_3D:
247 return 3;
248 default:
249 assert(0 && "bad texture target in texture_dims()");
250 return 2;
251 }
252 }
253
254
255 /**
256 * Derive the sampler static state.
257 */
258 void
259 lp_sampler_static_state(struct lp_sampler_static_state *state,
260 const struct pipe_sampler_view *view,
261 const struct pipe_sampler_state *sampler);
262
263
264 LLVMValueRef
265 lp_build_lod_selector(struct lp_build_sample_context *bld,
266 unsigned unit,
267 const LLVMValueRef ddx[4],
268 const LLVMValueRef ddy[4],
269 LLVMValueRef lod_bias, /* optional */
270 LLVMValueRef explicit_lod, /* optional */
271 LLVMValueRef width,
272 LLVMValueRef height,
273 LLVMValueRef depth);
274
275 void
276 lp_build_nearest_mip_level(struct lp_build_sample_context *bld,
277 unsigned unit,
278 LLVMValueRef lod,
279 LLVMValueRef *level_out);
280
281 void
282 lp_build_linear_mip_levels(struct lp_build_sample_context *bld,
283 unsigned unit,
284 LLVMValueRef lod,
285 LLVMValueRef *level0_out,
286 LLVMValueRef *level1_out,
287 LLVMValueRef *weight_out);
288
289 LLVMValueRef
290 lp_build_get_mipmap_level(struct lp_build_sample_context *bld,
291 LLVMValueRef data_array, LLVMValueRef level);
292
293 LLVMValueRef
294 lp_build_get_const_mipmap_level(struct lp_build_sample_context *bld,
295 LLVMValueRef data_array, int level);
296
297
298 void
299 lp_build_mipmap_level_sizes(struct lp_build_sample_context *bld,
300 unsigned dims,
301 LLVMValueRef width_vec,
302 LLVMValueRef height_vec,
303 LLVMValueRef depth_vec,
304 LLVMValueRef ilevel0,
305 LLVMValueRef ilevel1,
306 LLVMValueRef row_stride_array,
307 LLVMValueRef img_stride_array,
308 LLVMValueRef *width0_vec,
309 LLVMValueRef *width1_vec,
310 LLVMValueRef *height0_vec,
311 LLVMValueRef *height1_vec,
312 LLVMValueRef *depth0_vec,
313 LLVMValueRef *depth1_vec,
314 LLVMValueRef *row_stride0_vec,
315 LLVMValueRef *row_stride1_vec,
316 LLVMValueRef *img_stride0_vec,
317 LLVMValueRef *img_stride1_vec);
318
319
320 void
321 lp_build_cube_lookup(struct lp_build_sample_context *bld,
322 LLVMValueRef s,
323 LLVMValueRef t,
324 LLVMValueRef r,
325 LLVMValueRef *face,
326 LLVMValueRef *face_s,
327 LLVMValueRef *face_t);
328
329
330 void
331 lp_build_sample_partial_offset(struct lp_build_context *bld,
332 unsigned block_length,
333 LLVMValueRef coord,
334 LLVMValueRef stride,
335 LLVMValueRef *out_offset,
336 LLVMValueRef *out_i);
337
338
339 void
340 lp_build_sample_offset(struct lp_build_context *bld,
341 const struct util_format_description *format_desc,
342 LLVMValueRef x,
343 LLVMValueRef y,
344 LLVMValueRef z,
345 LLVMValueRef y_stride,
346 LLVMValueRef z_stride,
347 LLVMValueRef *out_offset,
348 LLVMValueRef *out_i,
349 LLVMValueRef *out_j);
350
351
352 void
353 lp_build_sample_soa(LLVMBuilderRef builder,
354 const struct lp_sampler_static_state *static_state,
355 struct lp_sampler_dynamic_state *dynamic_state,
356 struct lp_type fp_type,
357 unsigned unit,
358 unsigned num_coords,
359 const LLVMValueRef *coords,
360 const LLVMValueRef *ddx,
361 const LLVMValueRef *ddy,
362 LLVMValueRef lod_bias,
363 LLVMValueRef explicit_lod,
364 LLVMValueRef texel_out[4]);
365
366 void
367 lp_build_sample_nop(struct lp_type type,
368 LLVMValueRef texel_out[4]);
369
370
371 #endif /* LP_BLD_SAMPLE_H */