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)
102 struct lp_sampler_params
105 unsigned texture_index
;
106 unsigned sampler_index
;
108 LLVMValueRef context_ptr
;
109 LLVMValueRef thread_data_ptr
;
110 const LLVMValueRef
*coords
;
111 const LLVMValueRef
*offsets
;
113 const struct lp_derivatives
*derivs
;
117 struct lp_sampler_size_query_params
119 struct lp_type int_type
;
120 unsigned texture_unit
;
122 LLVMValueRef context_ptr
;
123 boolean is_sviewinfo
;
124 enum lp_sampler_lod_property lod_property
;
125 LLVMValueRef explicit_lod
;
126 LLVMValueRef
*sizes_out
;
129 #define LP_IMG_LOAD 0
130 #define LP_IMG_STORE 1
131 #define LP_IMG_ATOMIC 2
132 #define LP_IMG_ATOMIC_CAS 3
137 unsigned image_index
;
140 LLVMAtomicRMWBinOp op
;
141 LLVMValueRef exec_mask
;
142 LLVMValueRef context_ptr
;
143 LLVMValueRef thread_data_ptr
;
144 const LLVMValueRef
*coords
;
145 LLVMValueRef indata
[4];
146 LLVMValueRef indata2
[4];
147 LLVMValueRef
*outdata
;
150 * Texture static state.
152 * These are the bits of state from pipe_resource/pipe_sampler_view that
153 * are embedded in the generated code.
155 struct lp_static_texture_state
157 /* pipe_sampler_view's state */
158 enum pipe_format format
;
159 unsigned swizzle_r
:3; /**< PIPE_SWIZZLE_* */
160 unsigned swizzle_g
:3;
161 unsigned swizzle_b
:3;
162 unsigned swizzle_a
:3;
164 /* pipe_texture's state */
165 unsigned target
:4; /**< PIPE_TEXTURE_* */
166 unsigned pot_width
:1; /**< is the width a power of two? */
167 unsigned pot_height
:1;
168 unsigned pot_depth
:1;
169 unsigned level_zero_only
:1;
174 * Sampler static state.
176 * These are the bits of state from pipe_sampler_state that
177 * are embedded in the generated code.
179 struct lp_static_sampler_state
181 /* pipe_sampler_state's state */
185 unsigned min_img_filter
:2;
186 unsigned min_mip_filter
:2;
187 unsigned mag_img_filter
:2;
188 unsigned compare_mode
:1;
189 unsigned compare_func
:3;
190 unsigned normalized_coords
:1;
191 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
192 unsigned lod_bias_non_zero
:1;
193 unsigned max_lod_pos
:1;
194 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
195 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
196 unsigned seamless_cube_map
:1;
199 unsigned force_nearest_s
:1;
200 unsigned force_nearest_t
:1;
205 * Sampler dynamic state.
207 * These are the bits of state from pipe_resource/pipe_sampler_view
208 * as well as from sampler state that are computed at runtime.
210 * There are obtained through callbacks, as we don't want to tie the texture
211 * sampling code generation logic to any particular texture layout or pipe
214 struct lp_sampler_dynamic_state
216 /* First callbacks for sampler view state */
218 /** Obtain the base texture width (or number of elements) (returns int32) */
220 (*width
)(const struct lp_sampler_dynamic_state
*state
,
221 struct gallivm_state
*gallivm
,
222 LLVMValueRef context_ptr
,
223 unsigned texture_unit
);
225 /** Obtain the base texture height (returns int32) */
227 (*height
)(const struct lp_sampler_dynamic_state
*state
,
228 struct gallivm_state
*gallivm
,
229 LLVMValueRef context_ptr
,
230 unsigned texture_unit
);
232 /** Obtain the base texture depth (or array size) (returns int32) */
234 (*depth
)(const struct lp_sampler_dynamic_state
*state
,
235 struct gallivm_state
*gallivm
,
236 LLVMValueRef context_ptr
,
237 unsigned texture_unit
);
239 /** Obtain the first mipmap level (base level) (returns int32) */
241 (*first_level
)(const struct lp_sampler_dynamic_state
*state
,
242 struct gallivm_state
*gallivm
,
243 LLVMValueRef context_ptr
,
244 unsigned texture_unit
);
246 /** Obtain the number of mipmap levels minus one (returns int32) */
248 (*last_level
)(const struct lp_sampler_dynamic_state
*state
,
249 struct gallivm_state
*gallivm
,
250 LLVMValueRef context_ptr
,
251 unsigned texture_unit
);
253 /** Obtain stride in bytes between image rows/blocks (returns int32) */
255 (*row_stride
)(const struct lp_sampler_dynamic_state
*state
,
256 struct gallivm_state
*gallivm
,
257 LLVMValueRef context_ptr
,
258 unsigned texture_unit
);
260 /** Obtain stride in bytes between image slices (returns int32) */
262 (*img_stride
)(const struct lp_sampler_dynamic_state
*state
,
263 struct gallivm_state
*gallivm
,
264 LLVMValueRef context_ptr
,
265 unsigned texture_unit
);
267 /** Obtain pointer to base of texture */
269 (*base_ptr
)(const struct lp_sampler_dynamic_state
*state
,
270 struct gallivm_state
*gallivm
,
271 LLVMValueRef context_ptr
,
272 unsigned texture_unit
);
274 /** Obtain pointer to array of mipmap offsets */
276 (*mip_offsets
)(const struct lp_sampler_dynamic_state
*state
,
277 struct gallivm_state
*gallivm
,
278 LLVMValueRef context_ptr
,
279 unsigned texture_unit
);
281 /* These are callbacks for sampler state */
283 /** Obtain texture min lod (returns float) */
285 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
286 struct gallivm_state
*gallivm
,
287 LLVMValueRef context_ptr
,
288 unsigned sampler_unit
);
290 /** Obtain texture max lod (returns float) */
292 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
293 struct gallivm_state
*gallivm
,
294 LLVMValueRef context_ptr
,
295 unsigned sampler_unit
);
297 /** Obtain texture lod bias (returns float) */
299 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
300 struct gallivm_state
*gallivm
,
301 LLVMValueRef context_ptr
,
302 unsigned sampler_unit
);
304 /** Obtain texture border color (returns ptr to float[4]) */
306 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
307 struct gallivm_state
*gallivm
,
308 LLVMValueRef context_ptr
,
309 unsigned sampler_unit
);
312 * Obtain texture cache (returns ptr to lp_build_format_cache).
314 * It's optional: no caching will be done if it's NULL.
317 (*cache_ptr
)(const struct lp_sampler_dynamic_state
*state
,
318 struct gallivm_state
*gallivm
,
319 LLVMValueRef thread_data_ptr
,
325 * Keep all information for sampling code generation in a single place.
327 struct lp_build_sample_context
329 struct gallivm_state
*gallivm
;
331 const struct lp_static_texture_state
*static_texture_state
;
332 const struct lp_static_sampler_state
*static_sampler_state
;
334 struct lp_sampler_dynamic_state
*dynamic_state
;
336 const struct util_format_description
*format_desc
;
338 /* See texture_dims() */
341 /** SIMD vector width */
342 unsigned vector_width
;
344 /** number of mipmaps (valid are 1, length/4, length) */
347 /** number of lod values (valid are 1, length/4, length) */
350 unsigned gather_comp
;
352 boolean no_brilinear
;
353 boolean no_rho_approx
;
355 /** regular scalar float type */
356 struct lp_type float_type
;
357 struct lp_build_context float_bld
;
359 /** float vector type */
360 struct lp_build_context float_vec_bld
;
362 /** regular scalar int type */
363 struct lp_type int_type
;
364 struct lp_build_context int_bld
;
366 /** Incoming coordinates type and build context */
367 struct lp_type coord_type
;
368 struct lp_build_context coord_bld
;
370 /** Signed integer coordinates */
371 struct lp_type int_coord_type
;
372 struct lp_build_context int_coord_bld
;
374 /** Unsigned integer texture size */
375 struct lp_type int_size_in_type
;
376 struct lp_build_context int_size_in_bld
;
378 /** Float incoming texture size */
379 struct lp_type float_size_in_type
;
380 struct lp_build_context float_size_in_bld
;
382 /** Unsigned integer texture size (might be per quad) */
383 struct lp_type int_size_type
;
384 struct lp_build_context int_size_bld
;
386 /** Float texture size (might be per quad) */
387 struct lp_type float_size_type
;
388 struct lp_build_context float_size_bld
;
390 /** Output texels type and build context */
391 struct lp_type texel_type
;
392 struct lp_build_context texel_bld
;
394 /** Float level type */
395 struct lp_type levelf_type
;
396 struct lp_build_context levelf_bld
;
398 /** Int level type */
399 struct lp_type leveli_type
;
400 struct lp_build_context leveli_bld
;
402 /** Float lod type */
403 struct lp_type lodf_type
;
404 struct lp_build_context lodf_bld
;
407 struct lp_type lodi_type
;
408 struct lp_build_context lodi_bld
;
410 /* Common dynamic state values */
411 LLVMValueRef row_stride_array
;
412 LLVMValueRef img_stride_array
;
413 LLVMValueRef base_ptr
;
414 LLVMValueRef mip_offsets
;
417 /** Integer vector with texture width, height, depth */
418 LLVMValueRef int_size
;
420 LLVMValueRef border_color_clamped
;
422 LLVMValueRef context_ptr
;
428 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
429 * this time. Return whether the given mode is supported by that function.
431 static inline boolean
432 lp_is_simple_wrap_mode(unsigned mode
)
435 case PIPE_TEX_WRAP_REPEAT
:
436 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
445 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
448 unsigned char swizzles
[4];
450 swizzles
[0] = bld
->static_texture_state
->swizzle_r
;
451 swizzles
[1] = bld
->static_texture_state
->swizzle_g
;
452 swizzles
[2] = bld
->static_texture_state
->swizzle_b
;
453 swizzles
[3] = bld
->static_texture_state
->swizzle_a
;
455 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
459 * not really dimension as such, this indicates the amount of
460 * "normal" texture coords subject to minification, wrapping etc.
462 static inline unsigned
463 texture_dims(enum pipe_texture_target tex
)
466 case PIPE_TEXTURE_1D
:
467 case PIPE_TEXTURE_1D_ARRAY
:
470 case PIPE_TEXTURE_2D
:
471 case PIPE_TEXTURE_2D_ARRAY
:
472 case PIPE_TEXTURE_RECT
:
473 case PIPE_TEXTURE_CUBE
:
474 case PIPE_TEXTURE_CUBE_ARRAY
:
476 case PIPE_TEXTURE_3D
:
479 assert(0 && "bad texture target in texture_dims()");
484 static inline boolean
485 has_layer_coord(enum pipe_texture_target tex
)
488 case PIPE_TEXTURE_1D_ARRAY
:
489 case PIPE_TEXTURE_2D_ARRAY
:
490 /* cube is not layered but 3rd coord (after cube mapping) behaves the same */
491 case PIPE_TEXTURE_CUBE
:
492 case PIPE_TEXTURE_CUBE_ARRAY
:
501 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
502 unsigned min_img_filter
,
503 unsigned mag_img_filter
);
506 * Derive the sampler static state.
509 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
510 const struct pipe_sampler_state
*sampler
);
514 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
515 const struct pipe_sampler_view
*view
);
518 lp_sampler_static_texture_state_image(struct lp_static_texture_state
*state
,
519 const struct pipe_image_view
*view
);
522 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
524 unsigned texture_index
,
525 unsigned sampler_index
,
529 LLVMValueRef cube_rho
,
530 const struct lp_derivatives
*derivs
,
531 LLVMValueRef lod_bias
, /* optional */
532 LLVMValueRef explicit_lod
, /* optional */
534 LLVMValueRef
*out_lod
,
535 LLVMValueRef
*out_lod_ipart
,
536 LLVMValueRef
*out_lod_fpart
,
537 LLVMValueRef
*out_lod_positive
);
540 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
541 unsigned texture_unit
,
543 LLVMValueRef
*level_out
,
544 LLVMValueRef
*out_of_bounds
);
547 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
548 unsigned texture_unit
,
549 LLVMValueRef lod_ipart
,
550 LLVMValueRef
*lod_fpart_inout
,
551 LLVMValueRef
*level0_out
,
552 LLVMValueRef
*level1_out
);
555 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
560 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
565 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
567 LLVMValueRef
*out_size_vec
,
568 LLVMValueRef
*row_stride_vec
,
569 LLVMValueRef
*img_stride_vec
);
573 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
574 struct lp_build_context
*size_bld
,
575 struct lp_type coord_type
,
577 LLVMValueRef
*out_width
,
578 LLVMValueRef
*out_height
,
579 LLVMValueRef
*out_depth
);
583 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
584 LLVMValueRef flt_size
,
591 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
592 LLVMValueRef
*coords
,
593 const struct lp_derivatives
*derivs_in
, /* optional */
595 struct lp_derivatives
*derivs_out
, /* optional */
596 boolean need_derivs
);
600 lp_build_cube_new_coords(struct lp_build_context
*ivec_bld
,
606 LLVMValueRef max_coord
,
607 LLVMValueRef new_faces
[4],
608 LLVMValueRef new_xcoords
[4][2],
609 LLVMValueRef new_ycoords
[4][2]);
613 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
614 unsigned block_length
,
617 LLVMValueRef
*out_offset
,
618 LLVMValueRef
*out_i
);
622 lp_build_sample_offset(struct lp_build_context
*bld
,
623 const struct util_format_description
*format_desc
,
627 LLVMValueRef y_stride
,
628 LLVMValueRef z_stride
,
629 LLVMValueRef
*out_offset
,
631 LLVMValueRef
*out_j
);
635 lp_build_sample_soa(const struct lp_static_texture_state
*static_texture_state
,
636 const struct lp_static_sampler_state
*static_sampler_state
,
637 struct lp_sampler_dynamic_state
*dynamic_texture_state
,
638 struct gallivm_state
*gallivm
,
639 const struct lp_sampler_params
*params
);
643 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
644 LLVMValueRef coord_f
,
645 LLVMValueRef length_i
,
646 LLVMValueRef length_f
,
647 LLVMValueRef
*coord0_i
,
648 LLVMValueRef
*weight_f
);
652 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
653 const struct lp_static_texture_state
*static_state
,
654 struct lp_sampler_dynamic_state
*dynamic_state
,
655 const struct lp_sampler_size_query_params
*params
);
658 lp_build_sample_nop(struct gallivm_state
*gallivm
,
660 const LLVMValueRef
*coords
,
661 LLVMValueRef texel_out
[4]);
665 lp_build_minify(struct lp_build_context
*bld
,
666 LLVMValueRef base_size
,
671 lp_build_img_op_soa(const struct lp_static_texture_state
*static_texture_state
,
672 struct lp_sampler_dynamic_state
*dynamic_state
,
673 struct gallivm_state
*gallivm
,
674 const struct lp_img_params
*params
);
680 #endif /* LP_BLD_SAMPLE_H */