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
;
109 LLVMValueRef context_ptr
;
110 LLVMValueRef thread_data_ptr
;
111 const LLVMValueRef
*coords
;
112 const LLVMValueRef
*offsets
;
113 LLVMValueRef ms_index
;
115 const struct lp_derivatives
*derivs
;
119 struct lp_sampler_size_query_params
121 struct lp_type int_type
;
122 unsigned texture_unit
;
124 LLVMValueRef context_ptr
;
125 boolean is_sviewinfo
;
127 enum lp_sampler_lod_property lod_property
;
128 LLVMValueRef explicit_lod
;
129 LLVMValueRef
*sizes_out
;
132 #define LP_IMG_LOAD 0
133 #define LP_IMG_STORE 1
134 #define LP_IMG_ATOMIC 2
135 #define LP_IMG_ATOMIC_CAS 3
140 unsigned image_index
;
143 LLVMAtomicRMWBinOp op
;
144 LLVMValueRef exec_mask
;
145 LLVMValueRef context_ptr
;
146 LLVMValueRef thread_data_ptr
;
147 const LLVMValueRef
*coords
;
148 LLVMValueRef ms_index
;
149 LLVMValueRef indata
[4];
150 LLVMValueRef indata2
[4];
151 LLVMValueRef
*outdata
;
154 * Texture static state.
156 * These are the bits of state from pipe_resource/pipe_sampler_view that
157 * are embedded in the generated code.
159 struct lp_static_texture_state
161 /* pipe_sampler_view's state */
162 enum pipe_format format
;
163 unsigned swizzle_r
:3; /**< PIPE_SWIZZLE_* */
164 unsigned swizzle_g
:3;
165 unsigned swizzle_b
:3;
166 unsigned swizzle_a
:3;
168 /* pipe_texture's state */
169 unsigned target
:4; /**< PIPE_TEXTURE_* */
170 unsigned pot_width
:1; /**< is the width a power of two? */
171 unsigned pot_height
:1;
172 unsigned pot_depth
:1;
173 unsigned level_zero_only
:1;
178 * Sampler static state.
180 * These are the bits of state from pipe_sampler_state that
181 * are embedded in the generated code.
183 struct lp_static_sampler_state
185 /* pipe_sampler_state's state */
189 unsigned min_img_filter
:2;
190 unsigned min_mip_filter
:2;
191 unsigned mag_img_filter
:2;
192 unsigned compare_mode
:1;
193 unsigned compare_func
:3;
194 unsigned normalized_coords
:1;
195 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
196 unsigned lod_bias_non_zero
:1;
197 unsigned max_lod_pos
:1;
198 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
199 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
200 unsigned seamless_cube_map
:1;
203 unsigned force_nearest_s
:1;
204 unsigned force_nearest_t
:1;
209 * Sampler dynamic state.
211 * These are the bits of state from pipe_resource/pipe_sampler_view
212 * as well as from sampler state that are computed at runtime.
214 * There are obtained through callbacks, as we don't want to tie the texture
215 * sampling code generation logic to any particular texture layout or pipe
218 struct lp_sampler_dynamic_state
220 /* First callbacks for sampler view state */
222 /** Obtain the base texture width (or number of elements) (returns int32) */
224 (*width
)(const struct lp_sampler_dynamic_state
*state
,
225 struct gallivm_state
*gallivm
,
226 LLVMValueRef context_ptr
,
227 unsigned texture_unit
);
229 /** Obtain the base texture height (returns int32) */
231 (*height
)(const struct lp_sampler_dynamic_state
*state
,
232 struct gallivm_state
*gallivm
,
233 LLVMValueRef context_ptr
,
234 unsigned texture_unit
);
236 /** Obtain the base texture depth (or array size) (returns int32) */
238 (*depth
)(const struct lp_sampler_dynamic_state
*state
,
239 struct gallivm_state
*gallivm
,
240 LLVMValueRef context_ptr
,
241 unsigned texture_unit
);
243 /** Obtain the first mipmap level (base level) (returns int32) */
245 (*first_level
)(const struct lp_sampler_dynamic_state
*state
,
246 struct gallivm_state
*gallivm
,
247 LLVMValueRef context_ptr
,
248 unsigned texture_unit
);
250 /** Obtain the number of mipmap levels minus one (returns int32) */
252 (*last_level
)(const struct lp_sampler_dynamic_state
*state
,
253 struct gallivm_state
*gallivm
,
254 LLVMValueRef context_ptr
,
255 unsigned texture_unit
);
257 /** Obtain stride in bytes between image rows/blocks (returns int32) */
259 (*row_stride
)(const struct lp_sampler_dynamic_state
*state
,
260 struct gallivm_state
*gallivm
,
261 LLVMValueRef context_ptr
,
262 unsigned texture_unit
);
264 /** Obtain stride in bytes between image slices (returns int32) */
266 (*img_stride
)(const struct lp_sampler_dynamic_state
*state
,
267 struct gallivm_state
*gallivm
,
268 LLVMValueRef context_ptr
,
269 unsigned texture_unit
);
271 /** Obtain pointer to base of texture */
273 (*base_ptr
)(const struct lp_sampler_dynamic_state
*state
,
274 struct gallivm_state
*gallivm
,
275 LLVMValueRef context_ptr
,
276 unsigned texture_unit
);
278 /** Obtain pointer to array of mipmap offsets */
280 (*mip_offsets
)(const struct lp_sampler_dynamic_state
*state
,
281 struct gallivm_state
*gallivm
,
282 LLVMValueRef context_ptr
,
283 unsigned texture_unit
);
285 /** Obtain number of samples (returns int32) */
287 (*num_samples
)(const struct lp_sampler_dynamic_state
*state
,
288 struct gallivm_state
*gallivm
,
289 LLVMValueRef context_ptr
,
290 unsigned texture_unit
);
292 /** Obtain multisample stride (returns int32) */
294 (*sample_stride
)(const struct lp_sampler_dynamic_state
*state
,
295 struct gallivm_state
*gallivm
,
296 LLVMValueRef context_ptr
,
297 unsigned texture_unit
);
299 /* These are callbacks for sampler state */
301 /** Obtain texture min lod (returns float) */
303 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
304 struct gallivm_state
*gallivm
,
305 LLVMValueRef context_ptr
,
306 unsigned sampler_unit
);
308 /** Obtain texture max lod (returns float) */
310 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
311 struct gallivm_state
*gallivm
,
312 LLVMValueRef context_ptr
,
313 unsigned sampler_unit
);
315 /** Obtain texture lod bias (returns float) */
317 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
318 struct gallivm_state
*gallivm
,
319 LLVMValueRef context_ptr
,
320 unsigned sampler_unit
);
322 /** Obtain texture border color (returns ptr to float[4]) */
324 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
325 struct gallivm_state
*gallivm
,
326 LLVMValueRef context_ptr
,
327 unsigned sampler_unit
);
330 * Obtain texture cache (returns ptr to lp_build_format_cache).
332 * It's optional: no caching will be done if it's NULL.
335 (*cache_ptr
)(const struct lp_sampler_dynamic_state
*state
,
336 struct gallivm_state
*gallivm
,
337 LLVMValueRef thread_data_ptr
,
343 * Keep all information for sampling code generation in a single place.
345 struct lp_build_sample_context
347 struct gallivm_state
*gallivm
;
349 const struct lp_static_texture_state
*static_texture_state
;
350 const struct lp_static_sampler_state
*static_sampler_state
;
352 struct lp_sampler_dynamic_state
*dynamic_state
;
354 const struct util_format_description
*format_desc
;
356 /* See texture_dims() */
359 /** SIMD vector width */
360 unsigned vector_width
;
362 /** number of mipmaps (valid are 1, length/4, length) */
365 /** number of lod values (valid are 1, length/4, length) */
368 unsigned gather_comp
;
370 boolean no_brilinear
;
371 boolean no_rho_approx
;
374 /** regular scalar float type */
375 struct lp_type float_type
;
376 struct lp_build_context float_bld
;
378 /** float vector type */
379 struct lp_build_context float_vec_bld
;
381 /** regular scalar int type */
382 struct lp_type int_type
;
383 struct lp_build_context int_bld
;
385 /** Incoming coordinates type and build context */
386 struct lp_type coord_type
;
387 struct lp_build_context coord_bld
;
389 /** Signed integer coordinates */
390 struct lp_type int_coord_type
;
391 struct lp_build_context int_coord_bld
;
393 /** Unsigned integer texture size */
394 struct lp_type int_size_in_type
;
395 struct lp_build_context int_size_in_bld
;
397 /** Float incoming texture size */
398 struct lp_type float_size_in_type
;
399 struct lp_build_context float_size_in_bld
;
401 /** Unsigned integer texture size (might be per quad) */
402 struct lp_type int_size_type
;
403 struct lp_build_context int_size_bld
;
405 /** Float texture size (might be per quad) */
406 struct lp_type float_size_type
;
407 struct lp_build_context float_size_bld
;
409 /** Output texels type and build context */
410 struct lp_type texel_type
;
411 struct lp_build_context texel_bld
;
413 /** Float level type */
414 struct lp_type levelf_type
;
415 struct lp_build_context levelf_bld
;
417 /** Int level type */
418 struct lp_type leveli_type
;
419 struct lp_build_context leveli_bld
;
421 /** Float lod type */
422 struct lp_type lodf_type
;
423 struct lp_build_context lodf_bld
;
426 struct lp_type lodi_type
;
427 struct lp_build_context lodi_bld
;
429 /* Common dynamic state values */
430 LLVMValueRef row_stride_array
;
431 LLVMValueRef img_stride_array
;
432 LLVMValueRef base_ptr
;
433 LLVMValueRef mip_offsets
;
435 LLVMValueRef sample_stride
;
437 /** Integer vector with texture width, height, depth */
438 LLVMValueRef int_size
;
440 LLVMValueRef border_color_clamped
;
442 LLVMValueRef context_ptr
;
448 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
449 * this time. Return whether the given mode is supported by that function.
451 static inline boolean
452 lp_is_simple_wrap_mode(unsigned mode
)
455 case PIPE_TEX_WRAP_REPEAT
:
456 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
465 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
468 unsigned char swizzles
[4];
470 swizzles
[0] = bld
->static_texture_state
->swizzle_r
;
471 swizzles
[1] = bld
->static_texture_state
->swizzle_g
;
472 swizzles
[2] = bld
->static_texture_state
->swizzle_b
;
473 swizzles
[3] = bld
->static_texture_state
->swizzle_a
;
475 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
479 * not really dimension as such, this indicates the amount of
480 * "normal" texture coords subject to minification, wrapping etc.
482 static inline unsigned
483 texture_dims(enum pipe_texture_target tex
)
486 case PIPE_TEXTURE_1D
:
487 case PIPE_TEXTURE_1D_ARRAY
:
490 case PIPE_TEXTURE_2D
:
491 case PIPE_TEXTURE_2D_ARRAY
:
492 case PIPE_TEXTURE_RECT
:
493 case PIPE_TEXTURE_CUBE
:
494 case PIPE_TEXTURE_CUBE_ARRAY
:
496 case PIPE_TEXTURE_3D
:
499 assert(0 && "bad texture target in texture_dims()");
504 static inline boolean
505 has_layer_coord(enum pipe_texture_target tex
)
508 case PIPE_TEXTURE_1D_ARRAY
:
509 case PIPE_TEXTURE_2D_ARRAY
:
510 /* cube is not layered but 3rd coord (after cube mapping) behaves the same */
511 case PIPE_TEXTURE_CUBE
:
512 case PIPE_TEXTURE_CUBE_ARRAY
:
521 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
522 unsigned min_img_filter
,
523 unsigned mag_img_filter
);
526 * Derive the sampler static state.
529 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
530 const struct pipe_sampler_state
*sampler
);
534 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
535 const struct pipe_sampler_view
*view
);
538 lp_sampler_static_texture_state_image(struct lp_static_texture_state
*state
,
539 const struct pipe_image_view
*view
);
542 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
544 unsigned texture_index
,
545 unsigned sampler_index
,
549 LLVMValueRef cube_rho
,
550 const struct lp_derivatives
*derivs
,
551 LLVMValueRef lod_bias
, /* optional */
552 LLVMValueRef explicit_lod
, /* optional */
554 LLVMValueRef
*out_lod
,
555 LLVMValueRef
*out_lod_ipart
,
556 LLVMValueRef
*out_lod_fpart
,
557 LLVMValueRef
*out_lod_positive
);
560 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
561 unsigned texture_unit
,
563 LLVMValueRef
*level_out
,
564 LLVMValueRef
*out_of_bounds
);
567 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
568 unsigned texture_unit
,
569 LLVMValueRef lod_ipart
,
570 LLVMValueRef
*lod_fpart_inout
,
571 LLVMValueRef
*level0_out
,
572 LLVMValueRef
*level1_out
);
575 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
580 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
585 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
587 LLVMValueRef
*out_size_vec
,
588 LLVMValueRef
*row_stride_vec
,
589 LLVMValueRef
*img_stride_vec
);
593 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
594 struct lp_build_context
*size_bld
,
595 struct lp_type coord_type
,
597 LLVMValueRef
*out_width
,
598 LLVMValueRef
*out_height
,
599 LLVMValueRef
*out_depth
);
603 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
604 LLVMValueRef flt_size
,
611 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
612 LLVMValueRef
*coords
,
613 const struct lp_derivatives
*derivs_in
, /* optional */
615 struct lp_derivatives
*derivs_out
, /* optional */
616 boolean need_derivs
);
620 lp_build_cube_new_coords(struct lp_build_context
*ivec_bld
,
626 LLVMValueRef max_coord
,
627 LLVMValueRef new_faces
[4],
628 LLVMValueRef new_xcoords
[4][2],
629 LLVMValueRef new_ycoords
[4][2]);
633 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
634 unsigned block_length
,
637 LLVMValueRef
*out_offset
,
638 LLVMValueRef
*out_i
);
642 lp_build_sample_offset(struct lp_build_context
*bld
,
643 const struct util_format_description
*format_desc
,
647 LLVMValueRef y_stride
,
648 LLVMValueRef z_stride
,
649 LLVMValueRef
*out_offset
,
651 LLVMValueRef
*out_j
);
655 lp_build_sample_soa(const struct lp_static_texture_state
*static_texture_state
,
656 const struct lp_static_sampler_state
*static_sampler_state
,
657 struct lp_sampler_dynamic_state
*dynamic_texture_state
,
658 struct gallivm_state
*gallivm
,
659 const struct lp_sampler_params
*params
);
663 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
664 LLVMValueRef coord_f
,
665 LLVMValueRef length_i
,
666 LLVMValueRef length_f
,
667 LLVMValueRef
*coord0_i
,
668 LLVMValueRef
*weight_f
);
672 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
673 const struct lp_static_texture_state
*static_state
,
674 struct lp_sampler_dynamic_state
*dynamic_state
,
675 const struct lp_sampler_size_query_params
*params
);
678 lp_build_sample_nop(struct gallivm_state
*gallivm
,
680 const LLVMValueRef
*coords
,
681 LLVMValueRef texel_out
[4]);
685 lp_build_minify(struct lp_build_context
*bld
,
686 LLVMValueRef base_size
,
691 lp_build_img_op_soa(const struct lp_static_texture_state
*static_texture_state
,
692 struct lp_sampler_dynamic_state
*dynamic_state
,
693 struct gallivm_state
*gallivm
,
694 const struct lp_img_params
*params
);
700 #endif /* LP_BLD_SAMPLE_H */