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