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
;
143 LLVMValueRef image_index_offset
;
146 LLVMAtomicRMWBinOp op
;
147 LLVMValueRef exec_mask
;
148 LLVMValueRef context_ptr
;
149 LLVMValueRef thread_data_ptr
;
150 const LLVMValueRef
*coords
;
151 LLVMValueRef ms_index
;
152 LLVMValueRef indata
[4];
153 LLVMValueRef indata2
[4];
154 LLVMValueRef
*outdata
;
157 * Texture static state.
159 * These are the bits of state from pipe_resource/pipe_sampler_view that
160 * are embedded in the generated code.
162 struct lp_static_texture_state
164 /* pipe_sampler_view's state */
165 enum pipe_format format
;
166 unsigned swizzle_r
:3; /**< PIPE_SWIZZLE_* */
167 unsigned swizzle_g
:3;
168 unsigned swizzle_b
:3;
169 unsigned swizzle_a
:3;
171 /* pipe_texture's state */
172 unsigned target
:4; /**< PIPE_TEXTURE_* */
173 unsigned pot_width
:1; /**< is the width a power of two? */
174 unsigned pot_height
:1;
175 unsigned pot_depth
:1;
176 unsigned level_zero_only
:1;
181 * Sampler static state.
183 * These are the bits of state from pipe_sampler_state that
184 * are embedded in the generated code.
186 struct lp_static_sampler_state
188 /* pipe_sampler_state's state */
192 unsigned min_img_filter
:2;
193 unsigned min_mip_filter
:2;
194 unsigned mag_img_filter
:2;
195 unsigned compare_mode
:1;
196 unsigned compare_func
:3;
197 unsigned normalized_coords
:1;
198 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
199 unsigned lod_bias_non_zero
:1;
200 unsigned max_lod_pos
:1;
201 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
202 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
203 unsigned seamless_cube_map
:1;
206 unsigned force_nearest_s
:1;
207 unsigned force_nearest_t
:1;
212 * Sampler dynamic state.
214 * These are the bits of state from pipe_resource/pipe_sampler_view
215 * as well as from sampler state that are computed at runtime.
217 * There are obtained through callbacks, as we don't want to tie the texture
218 * sampling code generation logic to any particular texture layout or pipe
221 struct lp_sampler_dynamic_state
223 /* First callbacks for sampler view state */
225 /** Obtain the base texture width (or number of elements) (returns int32) */
227 (*width
)(const struct lp_sampler_dynamic_state
*state
,
228 struct gallivm_state
*gallivm
,
229 LLVMValueRef context_ptr
,
230 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
232 /** Obtain the base texture height (returns int32) */
234 (*height
)(const struct lp_sampler_dynamic_state
*state
,
235 struct gallivm_state
*gallivm
,
236 LLVMValueRef context_ptr
,
237 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
239 /** Obtain the base texture depth (or array size) (returns int32) */
241 (*depth
)(const struct lp_sampler_dynamic_state
*state
,
242 struct gallivm_state
*gallivm
,
243 LLVMValueRef context_ptr
,
244 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
246 /** Obtain the first mipmap level (base level) (returns int32) */
248 (*first_level
)(const struct lp_sampler_dynamic_state
*state
,
249 struct gallivm_state
*gallivm
,
250 LLVMValueRef context_ptr
,
251 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
253 /** Obtain the number of mipmap levels minus one (returns int32) */
255 (*last_level
)(const struct lp_sampler_dynamic_state
*state
,
256 struct gallivm_state
*gallivm
,
257 LLVMValueRef context_ptr
,
258 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
260 /** Obtain stride in bytes between image rows/blocks (returns int32) */
262 (*row_stride
)(const struct lp_sampler_dynamic_state
*state
,
263 struct gallivm_state
*gallivm
,
264 LLVMValueRef context_ptr
,
265 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
267 /** Obtain stride in bytes between image slices (returns int32) */
269 (*img_stride
)(const struct lp_sampler_dynamic_state
*state
,
270 struct gallivm_state
*gallivm
,
271 LLVMValueRef context_ptr
,
272 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
274 /** Obtain pointer to base of texture */
276 (*base_ptr
)(const struct lp_sampler_dynamic_state
*state
,
277 struct gallivm_state
*gallivm
,
278 LLVMValueRef context_ptr
,
279 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
281 /** Obtain pointer to array of mipmap offsets */
283 (*mip_offsets
)(const struct lp_sampler_dynamic_state
*state
,
284 struct gallivm_state
*gallivm
,
285 LLVMValueRef context_ptr
,
286 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
288 /** Obtain number of samples (returns int32) */
290 (*num_samples
)(const struct lp_sampler_dynamic_state
*state
,
291 struct gallivm_state
*gallivm
,
292 LLVMValueRef context_ptr
,
293 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
295 /** Obtain multisample stride (returns int32) */
297 (*sample_stride
)(const struct lp_sampler_dynamic_state
*state
,
298 struct gallivm_state
*gallivm
,
299 LLVMValueRef context_ptr
,
300 unsigned texture_unit
, LLVMValueRef texture_unit_offset
);
302 /* These are callbacks for sampler state */
304 /** Obtain texture min lod (returns float) */
306 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
307 struct gallivm_state
*gallivm
,
308 LLVMValueRef context_ptr
,
309 unsigned sampler_unit
);
311 /** Obtain texture max lod (returns float) */
313 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
314 struct gallivm_state
*gallivm
,
315 LLVMValueRef context_ptr
,
316 unsigned sampler_unit
);
318 /** Obtain texture lod bias (returns float) */
320 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
321 struct gallivm_state
*gallivm
,
322 LLVMValueRef context_ptr
,
323 unsigned sampler_unit
);
325 /** Obtain texture border color (returns ptr to float[4]) */
327 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
328 struct gallivm_state
*gallivm
,
329 LLVMValueRef context_ptr
,
330 unsigned sampler_unit
);
333 * Obtain texture cache (returns ptr to lp_build_format_cache).
335 * It's optional: no caching will be done if it's NULL.
338 (*cache_ptr
)(const struct lp_sampler_dynamic_state
*state
,
339 struct gallivm_state
*gallivm
,
340 LLVMValueRef thread_data_ptr
,
346 * Keep all information for sampling code generation in a single place.
348 struct lp_build_sample_context
350 struct gallivm_state
*gallivm
;
352 const struct lp_static_texture_state
*static_texture_state
;
353 const struct lp_static_sampler_state
*static_sampler_state
;
355 struct lp_sampler_dynamic_state
*dynamic_state
;
357 const struct util_format_description
*format_desc
;
359 /* See texture_dims() */
362 /** SIMD vector width */
363 unsigned vector_width
;
365 /** number of mipmaps (valid are 1, length/4, length) */
368 /** number of lod values (valid are 1, length/4, length) */
371 unsigned gather_comp
;
373 boolean no_brilinear
;
374 boolean no_rho_approx
;
377 /** regular scalar float type */
378 struct lp_type float_type
;
379 struct lp_build_context float_bld
;
381 /** float vector type */
382 struct lp_build_context float_vec_bld
;
384 /** regular scalar int type */
385 struct lp_type int_type
;
386 struct lp_build_context int_bld
;
388 /** Incoming coordinates type and build context */
389 struct lp_type coord_type
;
390 struct lp_build_context coord_bld
;
392 /** Signed integer coordinates */
393 struct lp_type int_coord_type
;
394 struct lp_build_context int_coord_bld
;
396 /** Unsigned integer texture size */
397 struct lp_type int_size_in_type
;
398 struct lp_build_context int_size_in_bld
;
400 /** Float incoming texture size */
401 struct lp_type float_size_in_type
;
402 struct lp_build_context float_size_in_bld
;
404 /** Unsigned integer texture size (might be per quad) */
405 struct lp_type int_size_type
;
406 struct lp_build_context int_size_bld
;
408 /** Float texture size (might be per quad) */
409 struct lp_type float_size_type
;
410 struct lp_build_context float_size_bld
;
412 /** Output texels type and build context */
413 struct lp_type texel_type
;
414 struct lp_build_context texel_bld
;
416 /** Float level type */
417 struct lp_type levelf_type
;
418 struct lp_build_context levelf_bld
;
420 /** Int level type */
421 struct lp_type leveli_type
;
422 struct lp_build_context leveli_bld
;
424 /** Float lod type */
425 struct lp_type lodf_type
;
426 struct lp_build_context lodf_bld
;
429 struct lp_type lodi_type
;
430 struct lp_build_context lodi_bld
;
432 /* Common dynamic state values */
433 LLVMValueRef row_stride_array
;
434 LLVMValueRef img_stride_array
;
435 LLVMValueRef base_ptr
;
436 LLVMValueRef mip_offsets
;
438 LLVMValueRef sample_stride
;
440 /** Integer vector with texture width, height, depth */
441 LLVMValueRef int_size
;
443 LLVMValueRef border_color_clamped
;
445 LLVMValueRef context_ptr
;
449 * Indirect texture access context
451 * This is used to store info across building
452 * and indirect texture switch statement.
454 struct lp_build_sample_array_switch
{
455 struct gallivm_state
*gallivm
;
456 struct lp_sampler_params params
;
457 unsigned base
, range
;
458 LLVMValueRef switch_ref
;
459 LLVMBasicBlockRef merge_ref
;
463 struct lp_build_img_op_array_switch
{
464 struct gallivm_state
*gallivm
;
465 struct lp_img_params params
;
466 unsigned base
, range
;
467 LLVMValueRef switch_ref
;
468 LLVMBasicBlockRef merge_ref
;
473 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
474 * this time. Return whether the given mode is supported by that function.
476 static inline boolean
477 lp_is_simple_wrap_mode(unsigned mode
)
480 case PIPE_TEX_WRAP_REPEAT
:
481 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
490 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
493 unsigned char swizzles
[4];
495 swizzles
[0] = bld
->static_texture_state
->swizzle_r
;
496 swizzles
[1] = bld
->static_texture_state
->swizzle_g
;
497 swizzles
[2] = bld
->static_texture_state
->swizzle_b
;
498 swizzles
[3] = bld
->static_texture_state
->swizzle_a
;
500 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
504 * not really dimension as such, this indicates the amount of
505 * "normal" texture coords subject to minification, wrapping etc.
507 static inline unsigned
508 texture_dims(enum pipe_texture_target tex
)
511 case PIPE_TEXTURE_1D
:
512 case PIPE_TEXTURE_1D_ARRAY
:
515 case PIPE_TEXTURE_2D
:
516 case PIPE_TEXTURE_2D_ARRAY
:
517 case PIPE_TEXTURE_RECT
:
518 case PIPE_TEXTURE_CUBE
:
519 case PIPE_TEXTURE_CUBE_ARRAY
:
521 case PIPE_TEXTURE_3D
:
524 assert(0 && "bad texture target in texture_dims()");
529 static inline boolean
530 has_layer_coord(enum pipe_texture_target tex
)
533 case PIPE_TEXTURE_1D_ARRAY
:
534 case PIPE_TEXTURE_2D_ARRAY
:
535 /* cube is not layered but 3rd coord (after cube mapping) behaves the same */
536 case PIPE_TEXTURE_CUBE
:
537 case PIPE_TEXTURE_CUBE_ARRAY
:
546 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
547 unsigned min_img_filter
,
548 unsigned mag_img_filter
);
551 * Derive the sampler static state.
554 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
555 const struct pipe_sampler_state
*sampler
);
559 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
560 const struct pipe_sampler_view
*view
);
563 lp_sampler_static_texture_state_image(struct lp_static_texture_state
*state
,
564 const struct pipe_image_view
*view
);
567 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
569 unsigned texture_index
,
570 unsigned sampler_index
,
574 LLVMValueRef cube_rho
,
575 const struct lp_derivatives
*derivs
,
576 LLVMValueRef lod_bias
, /* optional */
577 LLVMValueRef explicit_lod
, /* optional */
579 LLVMValueRef
*out_lod
,
580 LLVMValueRef
*out_lod_ipart
,
581 LLVMValueRef
*out_lod_fpart
,
582 LLVMValueRef
*out_lod_positive
);
585 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
586 unsigned texture_unit
,
588 LLVMValueRef
*level_out
,
589 LLVMValueRef
*out_of_bounds
);
592 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
593 unsigned texture_unit
,
594 LLVMValueRef lod_ipart
,
595 LLVMValueRef
*lod_fpart_inout
,
596 LLVMValueRef
*level0_out
,
597 LLVMValueRef
*level1_out
);
600 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
605 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
610 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
612 LLVMValueRef
*out_size_vec
,
613 LLVMValueRef
*row_stride_vec
,
614 LLVMValueRef
*img_stride_vec
);
618 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
619 struct lp_build_context
*size_bld
,
620 struct lp_type coord_type
,
622 LLVMValueRef
*out_width
,
623 LLVMValueRef
*out_height
,
624 LLVMValueRef
*out_depth
);
628 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
629 LLVMValueRef flt_size
,
636 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
637 LLVMValueRef
*coords
,
638 const struct lp_derivatives
*derivs_in
, /* optional */
640 struct lp_derivatives
*derivs_out
, /* optional */
641 boolean need_derivs
);
645 lp_build_cube_new_coords(struct lp_build_context
*ivec_bld
,
651 LLVMValueRef max_coord
,
652 LLVMValueRef new_faces
[4],
653 LLVMValueRef new_xcoords
[4][2],
654 LLVMValueRef new_ycoords
[4][2]);
658 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
659 unsigned block_length
,
662 LLVMValueRef
*out_offset
,
663 LLVMValueRef
*out_i
);
667 lp_build_sample_offset(struct lp_build_context
*bld
,
668 const struct util_format_description
*format_desc
,
672 LLVMValueRef y_stride
,
673 LLVMValueRef z_stride
,
674 LLVMValueRef
*out_offset
,
676 LLVMValueRef
*out_j
);
680 lp_build_sample_soa(const struct lp_static_texture_state
*static_texture_state
,
681 const struct lp_static_sampler_state
*static_sampler_state
,
682 struct lp_sampler_dynamic_state
*dynamic_texture_state
,
683 struct gallivm_state
*gallivm
,
684 const struct lp_sampler_params
*params
);
688 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
689 LLVMValueRef coord_f
,
690 LLVMValueRef length_i
,
691 LLVMValueRef length_f
,
692 LLVMValueRef
*coord0_i
,
693 LLVMValueRef
*weight_f
);
697 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
698 const struct lp_static_texture_state
*static_state
,
699 struct lp_sampler_dynamic_state
*dynamic_state
,
700 const struct lp_sampler_size_query_params
*params
);
703 lp_build_sample_nop(struct gallivm_state
*gallivm
,
705 const LLVMValueRef
*coords
,
706 LLVMValueRef texel_out
[4]);
710 lp_build_minify(struct lp_build_context
*bld
,
711 LLVMValueRef base_size
,
716 lp_build_img_op_soa(const struct lp_static_texture_state
*static_texture_state
,
717 struct lp_sampler_dynamic_state
*dynamic_state
,
718 struct gallivm_state
*gallivm
,
719 const struct lp_img_params
*params
,
720 LLVMValueRef outdata
[4]);
723 lp_build_sample_array_init_soa(struct lp_build_sample_array_switch
*switch_info
,
724 struct gallivm_state
*gallivm
,
725 const struct lp_sampler_params
*params
,
727 unsigned base
, unsigned range
);
730 lp_build_sample_array_case_soa(struct lp_build_sample_array_switch
*switch_info
,
732 const struct lp_static_texture_state
*static_texture_state
,
733 const struct lp_static_sampler_state
*static_sampler_state
,
734 struct lp_sampler_dynamic_state
*dynamic_texture_state
);
736 void lp_build_sample_array_fini_soa(struct lp_build_sample_array_switch
*switch_info
);
739 lp_build_image_op_switch_soa(struct lp_build_img_op_array_switch
*switch_info
,
740 struct gallivm_state
*gallivm
,
741 const struct lp_img_params
*params
,
743 unsigned base
, unsigned range
);
746 lp_build_image_op_array_case(struct lp_build_img_op_array_switch
*switch_info
,
748 const struct lp_static_texture_state
*static_texture_state
,
749 struct lp_sampler_dynamic_state
*dynamic_state
);
751 void lp_build_image_op_array_fini_soa(struct lp_build_img_op_array_switch
*switch_info
);
756 #endif /* LP_BLD_SAMPLE_H */