gallivm: fix min/mag switchover point for nearest/none mip filter
[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 * Helper struct holding all derivatives needed for sampling
56 */
57 struct lp_derivatives
58 {
59 LLVMValueRef ddx[3];
60 LLVMValueRef ddy[3];
61 };
62
63
64 enum lp_sampler_lod_property {
65 LP_SAMPLER_LOD_SCALAR,
66 LP_SAMPLER_LOD_PER_ELEMENT,
67 LP_SAMPLER_LOD_PER_QUAD
68 };
69
70
71 /**
72 * Texture static state.
73 *
74 * These are the bits of state from pipe_resource/pipe_sampler_view that
75 * are embedded in the generated code.
76 */
77 struct lp_static_texture_state
78 {
79 /* pipe_sampler_view's state */
80 enum pipe_format format;
81 unsigned swizzle_r:3; /**< PIPE_SWIZZLE_* */
82 unsigned swizzle_g:3;
83 unsigned swizzle_b:3;
84 unsigned swizzle_a:3;
85
86 /* pipe_texture's state */
87 unsigned target:4; /**< PIPE_TEXTURE_* */
88 unsigned pot_width:1; /**< is the width a power of two? */
89 unsigned pot_height:1;
90 unsigned pot_depth:1;
91 unsigned level_zero_only:1;
92 };
93
94
95 /**
96 * Sampler static state.
97 *
98 * These are the bits of state from pipe_sampler_state that
99 * are embedded in the generated code.
100 */
101 struct lp_static_sampler_state
102 {
103 /* pipe_sampler_state's state */
104 unsigned wrap_s:3;
105 unsigned wrap_t:3;
106 unsigned wrap_r:3;
107 unsigned min_img_filter:2;
108 unsigned min_mip_filter:2;
109 unsigned mag_img_filter:2;
110 unsigned compare_mode:1;
111 unsigned compare_func:3;
112 unsigned normalized_coords:1;
113 unsigned min_max_lod_equal:1; /**< min_lod == max_lod ? */
114 unsigned lod_bias_non_zero:1;
115 unsigned apply_min_lod:1; /**< min_lod > 0 ? */
116 unsigned apply_max_lod:1; /**< max_lod < last_level ? */
117
118 /* Hacks */
119 unsigned force_nearest_s:1;
120 unsigned force_nearest_t:1;
121 };
122
123
124 /**
125 * Sampler dynamic state.
126 *
127 * These are the bits of state from pipe_resource/pipe_sampler_view
128 * as well as from sampler state that are computed at runtime.
129 *
130 * There are obtained through callbacks, as we don't want to tie the texture
131 * sampling code generation logic to any particular texture layout or pipe
132 * driver.
133 */
134 struct lp_sampler_dynamic_state
135 {
136 /* First callbacks for sampler view state */
137
138 /** Obtain the base texture width (or number of elements) (returns int32) */
139 LLVMValueRef
140 (*width)( const struct lp_sampler_dynamic_state *state,
141 struct gallivm_state *gallivm,
142 unsigned texture_unit);
143
144 /** Obtain the base texture height (returns int32) */
145 LLVMValueRef
146 (*height)( const struct lp_sampler_dynamic_state *state,
147 struct gallivm_state *gallivm,
148 unsigned texture_unit);
149
150 /** Obtain the base texture depth (or array size) (returns int32) */
151 LLVMValueRef
152 (*depth)( const struct lp_sampler_dynamic_state *state,
153 struct gallivm_state *gallivm,
154 unsigned texture_unit);
155
156 /** Obtain the first mipmap level (base level) (returns int32) */
157 LLVMValueRef
158 (*first_level)( const struct lp_sampler_dynamic_state *state,
159 struct gallivm_state *gallivm,
160 unsigned texture_unit);
161
162 /** Obtain the number of mipmap levels minus one (returns int32) */
163 LLVMValueRef
164 (*last_level)( const struct lp_sampler_dynamic_state *state,
165 struct gallivm_state *gallivm,
166 unsigned texture_unit);
167
168 /** Obtain stride in bytes between image rows/blocks (returns int32) */
169 LLVMValueRef
170 (*row_stride)( const struct lp_sampler_dynamic_state *state,
171 struct gallivm_state *gallivm,
172 unsigned texture_unit);
173
174 /** Obtain stride in bytes between image slices (returns int32) */
175 LLVMValueRef
176 (*img_stride)( const struct lp_sampler_dynamic_state *state,
177 struct gallivm_state *gallivm,
178 unsigned texture_unit);
179
180 /** Obtain pointer to base of texture */
181 LLVMValueRef
182 (*base_ptr)( const struct lp_sampler_dynamic_state *state,
183 struct gallivm_state *gallivm,
184 unsigned texture_unit);
185
186 /** Obtain pointer to array of mipmap offsets */
187 LLVMValueRef
188 (*mip_offsets)( const struct lp_sampler_dynamic_state *state,
189 struct gallivm_state *gallivm,
190 unsigned texture_unit);
191
192 /* These are callbacks for sampler state */
193
194 /** Obtain texture min lod (returns float) */
195 LLVMValueRef
196 (*min_lod)(const struct lp_sampler_dynamic_state *state,
197 struct gallivm_state *gallivm, unsigned sampler_unit);
198
199 /** Obtain texture max lod (returns float) */
200 LLVMValueRef
201 (*max_lod)(const struct lp_sampler_dynamic_state *state,
202 struct gallivm_state *gallivm, unsigned sampler_unit);
203
204 /** Obtain texture lod bias (returns float) */
205 LLVMValueRef
206 (*lod_bias)(const struct lp_sampler_dynamic_state *state,
207 struct gallivm_state *gallivm, unsigned sampler_unit);
208
209 /** Obtain texture border color (returns ptr to float[4]) */
210 LLVMValueRef
211 (*border_color)(const struct lp_sampler_dynamic_state *state,
212 struct gallivm_state *gallivm, unsigned sampler_unit);
213 };
214
215
216 /**
217 * Keep all information for sampling code generation in a single place.
218 */
219 struct lp_build_sample_context
220 {
221 struct gallivm_state *gallivm;
222
223 const struct lp_static_texture_state *static_texture_state;
224 const struct lp_static_sampler_state *static_sampler_state;
225
226 struct lp_sampler_dynamic_state *dynamic_state;
227
228 const struct util_format_description *format_desc;
229
230 /* See texture_dims() */
231 unsigned dims;
232
233 /** SIMD vector width */
234 unsigned vector_width;
235
236 /** number of lod values (valid are 1, length/4, length) */
237 unsigned num_lods;
238
239 /** regular scalar float type */
240 struct lp_type float_type;
241 struct lp_build_context float_bld;
242
243 /** float vector type */
244 struct lp_build_context float_vec_bld;
245
246 /** regular scalar int type */
247 struct lp_type int_type;
248 struct lp_build_context int_bld;
249
250 /** Incoming coordinates type and build context */
251 struct lp_type coord_type;
252 struct lp_build_context coord_bld;
253
254 /** Signed integer coordinates */
255 struct lp_type int_coord_type;
256 struct lp_build_context int_coord_bld;
257
258 /** Unsigned integer texture size */
259 struct lp_type int_size_in_type;
260 struct lp_build_context int_size_in_bld;
261
262 /** Float incoming texture size */
263 struct lp_type float_size_in_type;
264 struct lp_build_context float_size_in_bld;
265
266 /** Unsigned integer texture size (might be per quad) */
267 struct lp_type int_size_type;
268 struct lp_build_context int_size_bld;
269
270 /** Float texture size (might be per quad) */
271 struct lp_type float_size_type;
272 struct lp_build_context float_size_bld;
273
274 /** Output texels type and build context */
275 struct lp_type texel_type;
276 struct lp_build_context texel_bld;
277
278 /** Float level type */
279 struct lp_type levelf_type;
280 struct lp_build_context levelf_bld;
281
282 /** Int level type */
283 struct lp_type leveli_type;
284 struct lp_build_context leveli_bld;
285
286 /* Common dynamic state values */
287 LLVMValueRef row_stride_array;
288 LLVMValueRef img_stride_array;
289 LLVMValueRef base_ptr;
290 LLVMValueRef mip_offsets;
291
292 /** Integer vector with texture width, height, depth */
293 LLVMValueRef int_size;
294
295 LLVMValueRef border_color_clamped;
296 };
297
298
299
300 /**
301 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
302 * this time. Return whether the given mode is supported by that function.
303 */
304 static INLINE boolean
305 lp_is_simple_wrap_mode(unsigned mode)
306 {
307 switch (mode) {
308 case PIPE_TEX_WRAP_REPEAT:
309 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
310 return TRUE;
311 default:
312 return FALSE;
313 }
314 }
315
316
317 static INLINE void
318 apply_sampler_swizzle(struct lp_build_sample_context *bld,
319 LLVMValueRef *texel)
320 {
321 unsigned char swizzles[4];
322
323 swizzles[0] = bld->static_texture_state->swizzle_r;
324 swizzles[1] = bld->static_texture_state->swizzle_g;
325 swizzles[2] = bld->static_texture_state->swizzle_b;
326 swizzles[3] = bld->static_texture_state->swizzle_a;
327
328 lp_build_swizzle_soa_inplace(&bld->texel_bld, texel, swizzles);
329 }
330
331 /*
332 * not really dimension as such, this indicates the amount of
333 * "normal" texture coords subject to minification, wrapping etc.
334 */
335 static INLINE unsigned
336 texture_dims(enum pipe_texture_target tex)
337 {
338 switch (tex) {
339 case PIPE_TEXTURE_1D:
340 case PIPE_TEXTURE_1D_ARRAY:
341 case PIPE_BUFFER:
342 return 1;
343 case PIPE_TEXTURE_2D:
344 case PIPE_TEXTURE_2D_ARRAY:
345 case PIPE_TEXTURE_RECT:
346 case PIPE_TEXTURE_CUBE:
347 return 2;
348 case PIPE_TEXTURE_CUBE_ARRAY:
349 assert(0);
350 return 2;
351 case PIPE_TEXTURE_3D:
352 return 3;
353 default:
354 assert(0 && "bad texture target in texture_dims()");
355 return 2;
356 }
357 }
358
359
360 boolean
361 lp_sampler_wrap_mode_uses_border_color(unsigned mode,
362 unsigned min_img_filter,
363 unsigned mag_img_filter);
364
365 /**
366 * Derive the sampler static state.
367 */
368 void
369 lp_sampler_static_sampler_state(struct lp_static_sampler_state *state,
370 const struct pipe_sampler_state *sampler);
371
372
373 void
374 lp_sampler_static_texture_state(struct lp_static_texture_state *state,
375 const struct pipe_sampler_view *view);
376
377
378 void
379 lp_build_lod_selector(struct lp_build_sample_context *bld,
380 unsigned texture_index,
381 unsigned sampler_index,
382 LLVMValueRef s,
383 LLVMValueRef t,
384 LLVMValueRef r,
385 LLVMValueRef cube_rho,
386 const struct lp_derivatives *derivs,
387 LLVMValueRef lod_bias, /* optional */
388 LLVMValueRef explicit_lod, /* optional */
389 unsigned mip_filter,
390 LLVMValueRef *out_lod_ipart,
391 LLVMValueRef *out_lod_fpart,
392 LLVMValueRef *out_lod_positive);
393
394 void
395 lp_build_nearest_mip_level(struct lp_build_sample_context *bld,
396 unsigned texture_unit,
397 LLVMValueRef lod,
398 LLVMValueRef *level_out,
399 LLVMValueRef *out_of_bounds);
400
401 void
402 lp_build_linear_mip_levels(struct lp_build_sample_context *bld,
403 unsigned texture_unit,
404 LLVMValueRef lod_ipart,
405 LLVMValueRef *lod_fpart_inout,
406 LLVMValueRef *level0_out,
407 LLVMValueRef *level1_out);
408
409 LLVMValueRef
410 lp_build_get_mipmap_level(struct lp_build_sample_context *bld,
411 LLVMValueRef level);
412
413
414 LLVMValueRef
415 lp_build_get_mip_offsets(struct lp_build_sample_context *bld,
416 LLVMValueRef level);
417
418
419 void
420 lp_build_mipmap_level_sizes(struct lp_build_sample_context *bld,
421 LLVMValueRef ilevel,
422 LLVMValueRef *out_size_vec,
423 LLVMValueRef *row_stride_vec,
424 LLVMValueRef *img_stride_vec);
425
426
427 void
428 lp_build_extract_image_sizes(struct lp_build_sample_context *bld,
429 struct lp_build_context *size_bld,
430 struct lp_type coord_type,
431 LLVMValueRef size,
432 LLVMValueRef *out_width,
433 LLVMValueRef *out_height,
434 LLVMValueRef *out_depth);
435
436
437 void
438 lp_build_unnormalized_coords(struct lp_build_sample_context *bld,
439 LLVMValueRef flt_size,
440 LLVMValueRef *s,
441 LLVMValueRef *t,
442 LLVMValueRef *r);
443
444
445 void
446 lp_build_cube_lookup(struct lp_build_sample_context *bld,
447 LLVMValueRef *coords,
448 const struct lp_derivatives *derivs, /* optional */
449 LLVMValueRef *rho,
450 boolean need_derivs);
451
452
453 void
454 lp_build_sample_partial_offset(struct lp_build_context *bld,
455 unsigned block_length,
456 LLVMValueRef coord,
457 LLVMValueRef stride,
458 LLVMValueRef *out_offset,
459 LLVMValueRef *out_i);
460
461
462 void
463 lp_build_sample_offset(struct lp_build_context *bld,
464 const struct util_format_description *format_desc,
465 LLVMValueRef x,
466 LLVMValueRef y,
467 LLVMValueRef z,
468 LLVMValueRef y_stride,
469 LLVMValueRef z_stride,
470 LLVMValueRef *out_offset,
471 LLVMValueRef *out_i,
472 LLVMValueRef *out_j);
473
474
475 void
476 lp_build_sample_soa(struct gallivm_state *gallivm,
477 const struct lp_static_texture_state *static_texture_state,
478 const struct lp_static_sampler_state *static_sampler_state,
479 struct lp_sampler_dynamic_state *dynamic_texture_state,
480 struct lp_type fp_type,
481 boolean is_fetch,
482 unsigned texture_index,
483 unsigned sampler_index,
484 const LLVMValueRef *coords,
485 const LLVMValueRef *offsets,
486 const struct lp_derivatives *derivs,
487 LLVMValueRef lod_bias,
488 LLVMValueRef explicit_lod,
489 enum lp_sampler_lod_property lod_property,
490 LLVMValueRef texel_out[4]);
491
492
493 void
494 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context *bld,
495 LLVMValueRef coord_f,
496 LLVMValueRef length_i,
497 LLVMValueRef length_f,
498 LLVMValueRef *coord0_i,
499 LLVMValueRef *weight_f);
500
501
502 void
503 lp_build_size_query_soa(struct gallivm_state *gallivm,
504 const struct lp_static_texture_state *static_state,
505 struct lp_sampler_dynamic_state *dynamic_state,
506 struct lp_type int_type,
507 unsigned texture_unit,
508 unsigned target,
509 boolean is_sviewinfo,
510 enum lp_sampler_lod_property lod_property,
511 LLVMValueRef explicit_lod,
512 LLVMValueRef *sizes_out);
513
514 void
515 lp_build_sample_nop(struct gallivm_state *gallivm,
516 struct lp_type type,
517 const LLVMValueRef *coords,
518 LLVMValueRef texel_out[4]);
519
520
521 LLVMValueRef
522 lp_build_minify(struct lp_build_context *bld,
523 LLVMValueRef base_size,
524 LLVMValueRef level);
525
526
527 #endif /* LP_BLD_SAMPLE_H */