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