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