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