1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
32 * @author Jose Fonseca <jfonseca@vmware.com>
35 #ifndef LP_BLD_SAMPLE_H
36 #define LP_BLD_SAMPLE_H
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"
50 struct pipe_sampler_view
;
51 struct pipe_sampler_state
;
52 struct pipe_image_view
;
53 struct util_format_description
;
55 struct lp_build_context
;
59 * Helper struct holding all derivatives needed for sampling
68 enum lp_sampler_lod_property
{
69 LP_SAMPLER_LOD_SCALAR
,
70 LP_SAMPLER_LOD_PER_ELEMENT
,
71 LP_SAMPLER_LOD_PER_QUAD
75 enum lp_sampler_lod_control
{
76 LP_SAMPLER_LOD_IMPLICIT
,
78 LP_SAMPLER_LOD_EXPLICIT
,
79 LP_SAMPLER_LOD_DERIVATIVES
,
83 enum lp_sampler_op_type
{
84 LP_SAMPLER_OP_TEXTURE
,
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)
103 struct lp_sampler_params
106 unsigned texture_index
;
107 unsigned sampler_index
;
108 LLVMValueRef texture_index_offset
;
110 LLVMValueRef context_ptr
;
111 LLVMValueRef thread_data_ptr
;
112 const LLVMValueRef
*coords
;
113 const LLVMValueRef
*offsets
;
114 LLVMValueRef ms_index
;
116 const struct lp_derivatives
*derivs
;
120 struct lp_sampler_size_query_params
122 struct lp_type int_type
;
123 unsigned texture_unit
;
124 LLVMValueRef texture_unit_offset
;
126 LLVMValueRef context_ptr
;
127 boolean is_sviewinfo
;
129 enum lp_sampler_lod_property lod_property
;
130 LLVMValueRef explicit_lod
;
131 LLVMValueRef
*sizes_out
;
134 #define LP_IMG_LOAD 0
135 #define LP_IMG_STORE 1
136 #define LP_IMG_ATOMIC 2
137 #define LP_IMG_ATOMIC_CAS 3
142 unsigned image_index
;
145 LLVMAtomicRMWBinOp op
;
146 LLVMValueRef exec_mask
;
147 LLVMValueRef context_ptr
;
148 LLVMValueRef thread_data_ptr
;
149 const LLVMValueRef
*coords
;
150 LLVMValueRef ms_index
;
151 LLVMValueRef indata
[4];
152 LLVMValueRef indata2
[4];
153 LLVMValueRef
*outdata
;
156 * Texture static state.
158 * These are the bits of state from pipe_resource/pipe_sampler_view that
159 * are embedded in the generated code.
161 struct lp_static_texture_state
163 /* pipe_sampler_view's state */
164 enum pipe_format format
;
165 unsigned swizzle_r
:3; /**< PIPE_SWIZZLE_* */
166 unsigned swizzle_g
:3;
167 unsigned swizzle_b
:3;
168 unsigned swizzle_a
:3;
170 /* pipe_texture's state */
171 unsigned target
:4; /**< PIPE_TEXTURE_* */
172 unsigned pot_width
:1; /**< is the width a power of two? */
173 unsigned pot_height
:1;
174 unsigned pot_depth
:1;
175 unsigned level_zero_only
:1;
180 * Sampler static state.
182 * These are the bits of state from pipe_sampler_state that
183 * are embedded in the generated code.
185 struct lp_static_sampler_state
187 /* pipe_sampler_state's state */
191 unsigned min_img_filter
:2;
192 unsigned min_mip_filter
:2;
193 unsigned mag_img_filter
:2;
194 unsigned compare_mode
:1;
195 unsigned compare_func
:3;
196 unsigned normalized_coords
:1;
197 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
198 unsigned lod_bias_non_zero
:1;
199 unsigned max_lod_pos
:1;
200 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
201 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
202 unsigned seamless_cube_map
:1;
205 unsigned force_nearest_s
:1;
206 unsigned force_nearest_t
:1;
211 * Sampler dynamic state.
213 * These are the bits of state from pipe_resource/pipe_sampler_view
214 * as well as from sampler state that are computed at runtime.
216 * There are obtained through callbacks, as we don't want to tie the texture
217 * sampling code generation logic to any particular texture layout or pipe
220 struct lp_sampler_dynamic_state
222 /* First callbacks for sampler view state */
224 /** Obtain the base texture width (or number of elements) (returns int32) */
226 (*width
)(const struct lp_sampler_dynamic_state
*state
,
227 struct gallivm_state
*gallivm
,
228 LLVMValueRef context_ptr
,
229 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
231 /** Obtain the base texture height (returns int32) */
233 (*height
)(const struct lp_sampler_dynamic_state
*state
,
234 struct gallivm_state
*gallivm
,
235 LLVMValueRef context_ptr
,
236 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
238 /** Obtain the base texture depth (or array size) (returns int32) */
240 (*depth
)(const struct lp_sampler_dynamic_state
*state
,
241 struct gallivm_state
*gallivm
,
242 LLVMValueRef context_ptr
,
243 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
245 /** Obtain the first mipmap level (base level) (returns int32) */
247 (*first_level
)(const struct lp_sampler_dynamic_state
*state
,
248 struct gallivm_state
*gallivm
,
249 LLVMValueRef context_ptr
,
250 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
252 /** Obtain the number of mipmap levels minus one (returns int32) */
254 (*last_level
)(const struct lp_sampler_dynamic_state
*state
,
255 struct gallivm_state
*gallivm
,
256 LLVMValueRef context_ptr
,
257 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
259 /** Obtain stride in bytes between image rows/blocks (returns int32) */
261 (*row_stride
)(const struct lp_sampler_dynamic_state
*state
,
262 struct gallivm_state
*gallivm
,
263 LLVMValueRef context_ptr
,
264 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
266 /** Obtain stride in bytes between image slices (returns int32) */
268 (*img_stride
)(const struct lp_sampler_dynamic_state
*state
,
269 struct gallivm_state
*gallivm
,
270 LLVMValueRef context_ptr
,
271 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
273 /** Obtain pointer to base of texture */
275 (*base_ptr
)(const struct lp_sampler_dynamic_state
*state
,
276 struct gallivm_state
*gallivm
,
277 LLVMValueRef context_ptr
,
278 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
280 /** Obtain pointer to array of mipmap offsets */
282 (*mip_offsets
)(const struct lp_sampler_dynamic_state
*state
,
283 struct gallivm_state
*gallivm
,
284 LLVMValueRef context_ptr
,
285 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
287 /** Obtain number of samples (returns int32) */
289 (*num_samples
)(const struct lp_sampler_dynamic_state
*state
,
290 struct gallivm_state
*gallivm
,
291 LLVMValueRef context_ptr
,
292 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
294 /** Obtain multisample stride (returns int32) */
296 (*sample_stride
)(const struct lp_sampler_dynamic_state
*state
,
297 struct gallivm_state
*gallivm
,
298 LLVMValueRef context_ptr
,
299 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
301 /* These are callbacks for sampler state */
303 /** Obtain texture min lod (returns float) */
305 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
306 struct gallivm_state
*gallivm
,
307 LLVMValueRef context_ptr
,
308 unsigned sampler_unit
);
310 /** Obtain texture max lod (returns float) */
312 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
313 struct gallivm_state
*gallivm
,
314 LLVMValueRef context_ptr
,
315 unsigned sampler_unit
);
317 /** Obtain texture lod bias (returns float) */
319 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
320 struct gallivm_state
*gallivm
,
321 LLVMValueRef context_ptr
,
322 unsigned sampler_unit
);
324 /** Obtain texture border color (returns ptr to float[4]) */
326 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
327 struct gallivm_state
*gallivm
,
328 LLVMValueRef context_ptr
,
329 unsigned sampler_unit
);
332 * Obtain texture cache (returns ptr to lp_build_format_cache).
334 * It's optional: no caching will be done if it's NULL.
337 (*cache_ptr
)(const struct lp_sampler_dynamic_state
*state
,
338 struct gallivm_state
*gallivm
,
339 LLVMValueRef thread_data_ptr
,
345 * Keep all information for sampling code generation in a single place.
347 struct lp_build_sample_context
349 struct gallivm_state
*gallivm
;
351 const struct lp_static_texture_state
*static_texture_state
;
352 const struct lp_static_sampler_state
*static_sampler_state
;
354 struct lp_sampler_dynamic_state
*dynamic_state
;
356 const struct util_format_description
*format_desc
;
358 /* See texture_dims() */
361 /** SIMD vector width */
362 unsigned vector_width
;
364 /** number of mipmaps (valid are 1, length/4, length) */
367 /** number of lod values (valid are 1, length/4, length) */
370 unsigned gather_comp
;
372 boolean no_brilinear
;
373 boolean no_rho_approx
;
376 /** regular scalar float type */
377 struct lp_type float_type
;
378 struct lp_build_context float_bld
;
380 /** float vector type */
381 struct lp_build_context float_vec_bld
;
383 /** regular scalar int type */
384 struct lp_type int_type
;
385 struct lp_build_context int_bld
;
387 /** Incoming coordinates type and build context */
388 struct lp_type coord_type
;
389 struct lp_build_context coord_bld
;
391 /** Signed integer coordinates */
392 struct lp_type int_coord_type
;
393 struct lp_build_context int_coord_bld
;
395 /** Unsigned integer texture size */
396 struct lp_type int_size_in_type
;
397 struct lp_build_context int_size_in_bld
;
399 /** Float incoming texture size */
400 struct lp_type float_size_in_type
;
401 struct lp_build_context float_size_in_bld
;
403 /** Unsigned integer texture size (might be per quad) */
404 struct lp_type int_size_type
;
405 struct lp_build_context int_size_bld
;
407 /** Float texture size (might be per quad) */
408 struct lp_type float_size_type
;
409 struct lp_build_context float_size_bld
;
411 /** Output texels type and build context */
412 struct lp_type texel_type
;
413 struct lp_build_context texel_bld
;
415 /** Float level type */
416 struct lp_type levelf_type
;
417 struct lp_build_context levelf_bld
;
419 /** Int level type */
420 struct lp_type leveli_type
;
421 struct lp_build_context leveli_bld
;
423 /** Float lod type */
424 struct lp_type lodf_type
;
425 struct lp_build_context lodf_bld
;
428 struct lp_type lodi_type
;
429 struct lp_build_context lodi_bld
;
431 /* Common dynamic state values */
432 LLVMValueRef row_stride_array
;
433 LLVMValueRef img_stride_array
;
434 LLVMValueRef base_ptr
;
435 LLVMValueRef mip_offsets
;
437 LLVMValueRef sample_stride
;
439 /** Integer vector with texture width, height, depth */
440 LLVMValueRef int_size
;
442 LLVMValueRef border_color_clamped
;
444 LLVMValueRef context_ptr
;
448 * Indirect texture access context
450 * This is used to store info across building
451 * and indirect texture switch statement.
453 struct lp_build_sample_array_switch
{
454 struct gallivm_state
*gallivm
;
455 struct lp_sampler_params params
;
456 unsigned base
, range
;
457 LLVMValueRef switch_ref
;
458 LLVMBasicBlockRef merge_ref
;
464 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
465 * this time. Return whether the given mode is supported by that function.
467 static inline boolean
468 lp_is_simple_wrap_mode(unsigned mode
)
471 case PIPE_TEX_WRAP_REPEAT
:
472 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
481 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
484 unsigned char swizzles
[4];
486 swizzles
[0] = bld
->static_texture_state
->swizzle_r
;
487 swizzles
[1] = bld
->static_texture_state
->swizzle_g
;
488 swizzles
[2] = bld
->static_texture_state
->swizzle_b
;
489 swizzles
[3] = bld
->static_texture_state
->swizzle_a
;
491 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
495 * not really dimension as such, this indicates the amount of
496 * "normal" texture coords subject to minification, wrapping etc.
498 static inline unsigned
499 texture_dims(enum pipe_texture_target tex
)
502 case PIPE_TEXTURE_1D
:
503 case PIPE_TEXTURE_1D_ARRAY
:
506 case PIPE_TEXTURE_2D
:
507 case PIPE_TEXTURE_2D_ARRAY
:
508 case PIPE_TEXTURE_RECT
:
509 case PIPE_TEXTURE_CUBE
:
510 case PIPE_TEXTURE_CUBE_ARRAY
:
512 case PIPE_TEXTURE_3D
:
515 assert(0 && "bad texture target in texture_dims()");
520 static inline boolean
521 has_layer_coord(enum pipe_texture_target tex
)
524 case PIPE_TEXTURE_1D_ARRAY
:
525 case PIPE_TEXTURE_2D_ARRAY
:
526 /* cube is not layered but 3rd coord (after cube mapping) behaves the same */
527 case PIPE_TEXTURE_CUBE
:
528 case PIPE_TEXTURE_CUBE_ARRAY
:
537 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
538 unsigned min_img_filter
,
539 unsigned mag_img_filter
);
542 * Derive the sampler static state.
545 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
546 const struct pipe_sampler_state
*sampler
);
550 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
551 const struct pipe_sampler_view
*view
);
554 lp_sampler_static_texture_state_image(struct lp_static_texture_state
*state
,
555 const struct pipe_image_view
*view
);
558 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
560 unsigned texture_index
,
561 unsigned sampler_index
,
565 LLVMValueRef cube_rho
,
566 const struct lp_derivatives
*derivs
,
567 LLVMValueRef lod_bias
, /* optional */
568 LLVMValueRef explicit_lod
, /* optional */
570 LLVMValueRef
*out_lod
,
571 LLVMValueRef
*out_lod_ipart
,
572 LLVMValueRef
*out_lod_fpart
,
573 LLVMValueRef
*out_lod_positive
);
576 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
577 unsigned texture_unit
,
579 LLVMValueRef
*level_out
,
580 LLVMValueRef
*out_of_bounds
);
583 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
584 unsigned texture_unit
,
585 LLVMValueRef lod_ipart
,
586 LLVMValueRef
*lod_fpart_inout
,
587 LLVMValueRef
*level0_out
,
588 LLVMValueRef
*level1_out
);
591 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
596 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
601 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
603 LLVMValueRef
*out_size_vec
,
604 LLVMValueRef
*row_stride_vec
,
605 LLVMValueRef
*img_stride_vec
);
609 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
610 struct lp_build_context
*size_bld
,
611 struct lp_type coord_type
,
613 LLVMValueRef
*out_width
,
614 LLVMValueRef
*out_height
,
615 LLVMValueRef
*out_depth
);
619 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
620 LLVMValueRef flt_size
,
627 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
628 LLVMValueRef
*coords
,
629 const struct lp_derivatives
*derivs_in
, /* optional */
631 struct lp_derivatives
*derivs_out
, /* optional */
632 boolean need_derivs
);
636 lp_build_cube_new_coords(struct lp_build_context
*ivec_bld
,
642 LLVMValueRef max_coord
,
643 LLVMValueRef new_faces
[4],
644 LLVMValueRef new_xcoords
[4][2],
645 LLVMValueRef new_ycoords
[4][2]);
649 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
650 unsigned block_length
,
653 LLVMValueRef
*out_offset
,
654 LLVMValueRef
*out_i
);
658 lp_build_sample_offset(struct lp_build_context
*bld
,
659 const struct util_format_description
*format_desc
,
663 LLVMValueRef y_stride
,
664 LLVMValueRef z_stride
,
665 LLVMValueRef
*out_offset
,
667 LLVMValueRef
*out_j
);
671 lp_build_sample_soa(const struct lp_static_texture_state
*static_texture_state
,
672 const struct lp_static_sampler_state
*static_sampler_state
,
673 struct lp_sampler_dynamic_state
*dynamic_texture_state
,
674 struct gallivm_state
*gallivm
,
675 const struct lp_sampler_params
*params
);
679 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
680 LLVMValueRef coord_f
,
681 LLVMValueRef length_i
,
682 LLVMValueRef length_f
,
683 LLVMValueRef
*coord0_i
,
684 LLVMValueRef
*weight_f
);
688 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
689 const struct lp_static_texture_state
*static_state
,
690 struct lp_sampler_dynamic_state
*dynamic_state
,
691 const struct lp_sampler_size_query_params
*params
);
694 lp_build_sample_nop(struct gallivm_state
*gallivm
,
696 const LLVMValueRef
*coords
,
697 LLVMValueRef texel_out
[4]);
701 lp_build_minify(struct lp_build_context
*bld
,
702 LLVMValueRef base_size
,
707 lp_build_img_op_soa(const struct lp_static_texture_state
*static_texture_state
,
708 struct lp_sampler_dynamic_state
*dynamic_state
,
709 struct gallivm_state
*gallivm
,
710 const struct lp_img_params
*params
);
713 lp_build_sample_array_init_soa(struct lp_build_sample_array_switch
*switch_info
,
714 struct gallivm_state
*gallivm
,
715 const struct lp_sampler_params
*params
,
717 unsigned base
, unsigned range
);
720 lp_build_sample_array_case_soa(struct lp_build_sample_array_switch
*switch_info
,
722 const struct lp_static_texture_state
*static_texture_state
,
723 const struct lp_static_sampler_state
*static_sampler_state
,
724 struct lp_sampler_dynamic_state
*dynamic_texture_state
);
726 void lp_build_sample_array_fini_soa(struct lp_build_sample_array_switch
*switch_info
);
731 #endif /* LP_BLD_SAMPLE_H */