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
;
126 enum lp_sampler_lod_property lod_property
;
127 LLVMValueRef explicit_lod
;
128 LLVMValueRef
*sizes_out
;
131 #define LP_IMG_LOAD 0
132 #define LP_IMG_STORE 1
133 #define LP_IMG_ATOMIC 2
134 #define LP_IMG_ATOMIC_CAS 3
139 unsigned image_index
;
142 LLVMAtomicRMWBinOp op
;
143 LLVMValueRef exec_mask
;
144 LLVMValueRef context_ptr
;
145 LLVMValueRef thread_data_ptr
;
146 const LLVMValueRef
*coords
;
147 LLVMValueRef indata
[4];
148 LLVMValueRef indata2
[4];
149 LLVMValueRef
*outdata
;
152 * Texture static state.
154 * These are the bits of state from pipe_resource/pipe_sampler_view that
155 * are embedded in the generated code.
157 struct lp_static_texture_state
159 /* pipe_sampler_view's state */
160 enum pipe_format format
;
161 unsigned swizzle_r
:3; /**< PIPE_SWIZZLE_* */
162 unsigned swizzle_g
:3;
163 unsigned swizzle_b
:3;
164 unsigned swizzle_a
:3;
166 /* pipe_texture's state */
167 unsigned target
:4; /**< PIPE_TEXTURE_* */
168 unsigned pot_width
:1; /**< is the width a power of two? */
169 unsigned pot_height
:1;
170 unsigned pot_depth
:1;
171 unsigned level_zero_only
:1;
176 * Sampler static state.
178 * These are the bits of state from pipe_sampler_state that
179 * are embedded in the generated code.
181 struct lp_static_sampler_state
183 /* pipe_sampler_state's state */
187 unsigned min_img_filter
:2;
188 unsigned min_mip_filter
:2;
189 unsigned mag_img_filter
:2;
190 unsigned compare_mode
:1;
191 unsigned compare_func
:3;
192 unsigned normalized_coords
:1;
193 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
194 unsigned lod_bias_non_zero
:1;
195 unsigned max_lod_pos
:1;
196 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
197 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
198 unsigned seamless_cube_map
:1;
201 unsigned force_nearest_s
:1;
202 unsigned force_nearest_t
:1;
207 * Sampler dynamic state.
209 * These are the bits of state from pipe_resource/pipe_sampler_view
210 * as well as from sampler state that are computed at runtime.
212 * There are obtained through callbacks, as we don't want to tie the texture
213 * sampling code generation logic to any particular texture layout or pipe
216 struct lp_sampler_dynamic_state
218 /* First callbacks for sampler view state */
220 /** Obtain the base texture width (or number of elements) (returns int32) */
222 (*width
)(const struct lp_sampler_dynamic_state
*state
,
223 struct gallivm_state
*gallivm
,
224 LLVMValueRef context_ptr
,
225 unsigned texture_unit
);
227 /** Obtain the base texture height (returns int32) */
229 (*height
)(const struct lp_sampler_dynamic_state
*state
,
230 struct gallivm_state
*gallivm
,
231 LLVMValueRef context_ptr
,
232 unsigned texture_unit
);
234 /** Obtain the base texture depth (or array size) (returns int32) */
236 (*depth
)(const struct lp_sampler_dynamic_state
*state
,
237 struct gallivm_state
*gallivm
,
238 LLVMValueRef context_ptr
,
239 unsigned texture_unit
);
241 /** Obtain the first mipmap level (base level) (returns int32) */
243 (*first_level
)(const struct lp_sampler_dynamic_state
*state
,
244 struct gallivm_state
*gallivm
,
245 LLVMValueRef context_ptr
,
246 unsigned texture_unit
);
248 /** Obtain the number of mipmap levels minus one (returns int32) */
250 (*last_level
)(const struct lp_sampler_dynamic_state
*state
,
251 struct gallivm_state
*gallivm
,
252 LLVMValueRef context_ptr
,
253 unsigned texture_unit
);
255 /** Obtain stride in bytes between image rows/blocks (returns int32) */
257 (*row_stride
)(const struct lp_sampler_dynamic_state
*state
,
258 struct gallivm_state
*gallivm
,
259 LLVMValueRef context_ptr
,
260 unsigned texture_unit
);
262 /** Obtain stride in bytes between image slices (returns int32) */
264 (*img_stride
)(const struct lp_sampler_dynamic_state
*state
,
265 struct gallivm_state
*gallivm
,
266 LLVMValueRef context_ptr
,
267 unsigned texture_unit
);
269 /** Obtain pointer to base of texture */
271 (*base_ptr
)(const struct lp_sampler_dynamic_state
*state
,
272 struct gallivm_state
*gallivm
,
273 LLVMValueRef context_ptr
,
274 unsigned texture_unit
);
276 /** Obtain pointer to array of mipmap offsets */
278 (*mip_offsets
)(const struct lp_sampler_dynamic_state
*state
,
279 struct gallivm_state
*gallivm
,
280 LLVMValueRef context_ptr
,
281 unsigned texture_unit
);
283 /** Obtain number of samples (returns int32) */
285 (*num_samples
)(const struct lp_sampler_dynamic_state
*state
,
286 struct gallivm_state
*gallivm
,
287 LLVMValueRef context_ptr
,
288 unsigned texture_unit
);
290 /** Obtain multisample stride (returns int32) */
292 (*sample_stride
)(const struct lp_sampler_dynamic_state
*state
,
293 struct gallivm_state
*gallivm
,
294 LLVMValueRef context_ptr
,
295 unsigned texture_unit
);
297 /* These are callbacks for sampler state */
299 /** Obtain texture min lod (returns float) */
301 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
302 struct gallivm_state
*gallivm
,
303 LLVMValueRef context_ptr
,
304 unsigned sampler_unit
);
306 /** Obtain texture max lod (returns float) */
308 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
309 struct gallivm_state
*gallivm
,
310 LLVMValueRef context_ptr
,
311 unsigned sampler_unit
);
313 /** Obtain texture lod bias (returns float) */
315 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
316 struct gallivm_state
*gallivm
,
317 LLVMValueRef context_ptr
,
318 unsigned sampler_unit
);
320 /** Obtain texture border color (returns ptr to float[4]) */
322 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
323 struct gallivm_state
*gallivm
,
324 LLVMValueRef context_ptr
,
325 unsigned sampler_unit
);
328 * Obtain texture cache (returns ptr to lp_build_format_cache).
330 * It's optional: no caching will be done if it's NULL.
333 (*cache_ptr
)(const struct lp_sampler_dynamic_state
*state
,
334 struct gallivm_state
*gallivm
,
335 LLVMValueRef thread_data_ptr
,
341 * Keep all information for sampling code generation in a single place.
343 struct lp_build_sample_context
345 struct gallivm_state
*gallivm
;
347 const struct lp_static_texture_state
*static_texture_state
;
348 const struct lp_static_sampler_state
*static_sampler_state
;
350 struct lp_sampler_dynamic_state
*dynamic_state
;
352 const struct util_format_description
*format_desc
;
354 /* See texture_dims() */
357 /** SIMD vector width */
358 unsigned vector_width
;
360 /** number of mipmaps (valid are 1, length/4, length) */
363 /** number of lod values (valid are 1, length/4, length) */
366 unsigned gather_comp
;
368 boolean no_brilinear
;
369 boolean no_rho_approx
;
372 /** regular scalar float type */
373 struct lp_type float_type
;
374 struct lp_build_context float_bld
;
376 /** float vector type */
377 struct lp_build_context float_vec_bld
;
379 /** regular scalar int type */
380 struct lp_type int_type
;
381 struct lp_build_context int_bld
;
383 /** Incoming coordinates type and build context */
384 struct lp_type coord_type
;
385 struct lp_build_context coord_bld
;
387 /** Signed integer coordinates */
388 struct lp_type int_coord_type
;
389 struct lp_build_context int_coord_bld
;
391 /** Unsigned integer texture size */
392 struct lp_type int_size_in_type
;
393 struct lp_build_context int_size_in_bld
;
395 /** Float incoming texture size */
396 struct lp_type float_size_in_type
;
397 struct lp_build_context float_size_in_bld
;
399 /** Unsigned integer texture size (might be per quad) */
400 struct lp_type int_size_type
;
401 struct lp_build_context int_size_bld
;
403 /** Float texture size (might be per quad) */
404 struct lp_type float_size_type
;
405 struct lp_build_context float_size_bld
;
407 /** Output texels type and build context */
408 struct lp_type texel_type
;
409 struct lp_build_context texel_bld
;
411 /** Float level type */
412 struct lp_type levelf_type
;
413 struct lp_build_context levelf_bld
;
415 /** Int level type */
416 struct lp_type leveli_type
;
417 struct lp_build_context leveli_bld
;
419 /** Float lod type */
420 struct lp_type lodf_type
;
421 struct lp_build_context lodf_bld
;
424 struct lp_type lodi_type
;
425 struct lp_build_context lodi_bld
;
427 /* Common dynamic state values */
428 LLVMValueRef row_stride_array
;
429 LLVMValueRef img_stride_array
;
430 LLVMValueRef base_ptr
;
431 LLVMValueRef mip_offsets
;
433 LLVMValueRef sample_stride
;
435 /** Integer vector with texture width, height, depth */
436 LLVMValueRef int_size
;
438 LLVMValueRef border_color_clamped
;
440 LLVMValueRef context_ptr
;
446 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
447 * this time. Return whether the given mode is supported by that function.
449 static inline boolean
450 lp_is_simple_wrap_mode(unsigned mode
)
453 case PIPE_TEX_WRAP_REPEAT
:
454 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
463 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
466 unsigned char swizzles
[4];
468 swizzles
[0] = bld
->static_texture_state
->swizzle_r
;
469 swizzles
[1] = bld
->static_texture_state
->swizzle_g
;
470 swizzles
[2] = bld
->static_texture_state
->swizzle_b
;
471 swizzles
[3] = bld
->static_texture_state
->swizzle_a
;
473 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
477 * not really dimension as such, this indicates the amount of
478 * "normal" texture coords subject to minification, wrapping etc.
480 static inline unsigned
481 texture_dims(enum pipe_texture_target tex
)
484 case PIPE_TEXTURE_1D
:
485 case PIPE_TEXTURE_1D_ARRAY
:
488 case PIPE_TEXTURE_2D
:
489 case PIPE_TEXTURE_2D_ARRAY
:
490 case PIPE_TEXTURE_RECT
:
491 case PIPE_TEXTURE_CUBE
:
492 case PIPE_TEXTURE_CUBE_ARRAY
:
494 case PIPE_TEXTURE_3D
:
497 assert(0 && "bad texture target in texture_dims()");
502 static inline boolean
503 has_layer_coord(enum pipe_texture_target tex
)
506 case PIPE_TEXTURE_1D_ARRAY
:
507 case PIPE_TEXTURE_2D_ARRAY
:
508 /* cube is not layered but 3rd coord (after cube mapping) behaves the same */
509 case PIPE_TEXTURE_CUBE
:
510 case PIPE_TEXTURE_CUBE_ARRAY
:
519 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
520 unsigned min_img_filter
,
521 unsigned mag_img_filter
);
524 * Derive the sampler static state.
527 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
528 const struct pipe_sampler_state
*sampler
);
532 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
533 const struct pipe_sampler_view
*view
);
536 lp_sampler_static_texture_state_image(struct lp_static_texture_state
*state
,
537 const struct pipe_image_view
*view
);
540 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
542 unsigned texture_index
,
543 unsigned sampler_index
,
547 LLVMValueRef cube_rho
,
548 const struct lp_derivatives
*derivs
,
549 LLVMValueRef lod_bias
, /* optional */
550 LLVMValueRef explicit_lod
, /* optional */
552 LLVMValueRef
*out_lod
,
553 LLVMValueRef
*out_lod_ipart
,
554 LLVMValueRef
*out_lod_fpart
,
555 LLVMValueRef
*out_lod_positive
);
558 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
559 unsigned texture_unit
,
561 LLVMValueRef
*level_out
,
562 LLVMValueRef
*out_of_bounds
);
565 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
566 unsigned texture_unit
,
567 LLVMValueRef lod_ipart
,
568 LLVMValueRef
*lod_fpart_inout
,
569 LLVMValueRef
*level0_out
,
570 LLVMValueRef
*level1_out
);
573 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
578 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
583 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
585 LLVMValueRef
*out_size_vec
,
586 LLVMValueRef
*row_stride_vec
,
587 LLVMValueRef
*img_stride_vec
);
591 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
592 struct lp_build_context
*size_bld
,
593 struct lp_type coord_type
,
595 LLVMValueRef
*out_width
,
596 LLVMValueRef
*out_height
,
597 LLVMValueRef
*out_depth
);
601 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
602 LLVMValueRef flt_size
,
609 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
610 LLVMValueRef
*coords
,
611 const struct lp_derivatives
*derivs_in
, /* optional */
613 struct lp_derivatives
*derivs_out
, /* optional */
614 boolean need_derivs
);
618 lp_build_cube_new_coords(struct lp_build_context
*ivec_bld
,
624 LLVMValueRef max_coord
,
625 LLVMValueRef new_faces
[4],
626 LLVMValueRef new_xcoords
[4][2],
627 LLVMValueRef new_ycoords
[4][2]);
631 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
632 unsigned block_length
,
635 LLVMValueRef
*out_offset
,
636 LLVMValueRef
*out_i
);
640 lp_build_sample_offset(struct lp_build_context
*bld
,
641 const struct util_format_description
*format_desc
,
645 LLVMValueRef y_stride
,
646 LLVMValueRef z_stride
,
647 LLVMValueRef
*out_offset
,
649 LLVMValueRef
*out_j
);
653 lp_build_sample_soa(const struct lp_static_texture_state
*static_texture_state
,
654 const struct lp_static_sampler_state
*static_sampler_state
,
655 struct lp_sampler_dynamic_state
*dynamic_texture_state
,
656 struct gallivm_state
*gallivm
,
657 const struct lp_sampler_params
*params
);
661 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
662 LLVMValueRef coord_f
,
663 LLVMValueRef length_i
,
664 LLVMValueRef length_f
,
665 LLVMValueRef
*coord0_i
,
666 LLVMValueRef
*weight_f
);
670 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
671 const struct lp_static_texture_state
*static_state
,
672 struct lp_sampler_dynamic_state
*dynamic_state
,
673 const struct lp_sampler_size_query_params
*params
);
676 lp_build_sample_nop(struct gallivm_state
*gallivm
,
678 const LLVMValueRef
*coords
,
679 LLVMValueRef texel_out
[4]);
683 lp_build_minify(struct lp_build_context
*bld
,
684 LLVMValueRef base_size
,
689 lp_build_img_op_soa(const struct lp_static_texture_state
*static_texture_state
,
690 struct lp_sampler_dynamic_state
*dynamic_state
,
691 struct gallivm_state
*gallivm
,
692 const struct lp_img_params
*params
);
698 #endif /* LP_BLD_SAMPLE_H */