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