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