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