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