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