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