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)
100 struct lp_sampler_params
103 unsigned texture_index
;
104 unsigned sampler_index
;
106 LLVMValueRef context_ptr
;
107 LLVMValueRef thread_data_ptr
;
108 const LLVMValueRef
*coords
;
109 const LLVMValueRef
*offsets
;
111 const struct lp_derivatives
*derivs
;
115 struct lp_sampler_size_query_params
117 struct lp_type int_type
;
118 unsigned texture_unit
;
120 LLVMValueRef context_ptr
;
121 boolean is_sviewinfo
;
122 enum lp_sampler_lod_property lod_property
;
123 LLVMValueRef explicit_lod
;
124 LLVMValueRef
*sizes_out
;
127 #define LP_IMG_LOAD 0
128 #define LP_IMG_STORE 1
129 #define LP_IMG_ATOMIC 2
130 #define LP_IMG_ATOMIC_CAS 3
135 unsigned image_index
;
138 LLVMAtomicRMWBinOp op
;
139 LLVMValueRef exec_mask
;
140 LLVMValueRef context_ptr
;
141 LLVMValueRef thread_data_ptr
;
142 const LLVMValueRef
*coords
;
143 LLVMValueRef indata
[4];
144 LLVMValueRef indata2
[4];
145 LLVMValueRef
*outdata
;
148 * Texture static state.
150 * These are the bits of state from pipe_resource/pipe_sampler_view that
151 * are embedded in the generated code.
153 struct lp_static_texture_state
155 /* pipe_sampler_view's state */
156 enum pipe_format format
;
157 unsigned swizzle_r
:3; /**< PIPE_SWIZZLE_* */
158 unsigned swizzle_g
:3;
159 unsigned swizzle_b
:3;
160 unsigned swizzle_a
:3;
162 /* pipe_texture's state */
163 unsigned target
:4; /**< PIPE_TEXTURE_* */
164 unsigned pot_width
:1; /**< is the width a power of two? */
165 unsigned pot_height
:1;
166 unsigned pot_depth
:1;
167 unsigned level_zero_only
:1;
172 * Sampler static state.
174 * These are the bits of state from pipe_sampler_state that
175 * are embedded in the generated code.
177 struct lp_static_sampler_state
179 /* pipe_sampler_state's state */
183 unsigned min_img_filter
:2;
184 unsigned min_mip_filter
:2;
185 unsigned mag_img_filter
:2;
186 unsigned compare_mode
:1;
187 unsigned compare_func
:3;
188 unsigned normalized_coords
:1;
189 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
190 unsigned lod_bias_non_zero
:1;
191 unsigned max_lod_pos
:1;
192 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
193 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
194 unsigned seamless_cube_map
:1;
197 unsigned force_nearest_s
:1;
198 unsigned force_nearest_t
:1;
203 * Sampler dynamic state.
205 * These are the bits of state from pipe_resource/pipe_sampler_view
206 * as well as from sampler state that are computed at runtime.
208 * There are obtained through callbacks, as we don't want to tie the texture
209 * sampling code generation logic to any particular texture layout or pipe
212 struct lp_sampler_dynamic_state
214 /* First callbacks for sampler view state */
216 /** Obtain the base texture width (or number of elements) (returns int32) */
218 (*width
)(const struct lp_sampler_dynamic_state
*state
,
219 struct gallivm_state
*gallivm
,
220 LLVMValueRef context_ptr
,
221 unsigned texture_unit
);
223 /** Obtain the base texture height (returns int32) */
225 (*height
)(const struct lp_sampler_dynamic_state
*state
,
226 struct gallivm_state
*gallivm
,
227 LLVMValueRef context_ptr
,
228 unsigned texture_unit
);
230 /** Obtain the base texture depth (or array size) (returns int32) */
232 (*depth
)(const struct lp_sampler_dynamic_state
*state
,
233 struct gallivm_state
*gallivm
,
234 LLVMValueRef context_ptr
,
235 unsigned texture_unit
);
237 /** Obtain the first mipmap level (base level) (returns int32) */
239 (*first_level
)(const struct lp_sampler_dynamic_state
*state
,
240 struct gallivm_state
*gallivm
,
241 LLVMValueRef context_ptr
,
242 unsigned texture_unit
);
244 /** Obtain the number of mipmap levels minus one (returns int32) */
246 (*last_level
)(const struct lp_sampler_dynamic_state
*state
,
247 struct gallivm_state
*gallivm
,
248 LLVMValueRef context_ptr
,
249 unsigned texture_unit
);
251 /** Obtain stride in bytes between image rows/blocks (returns int32) */
253 (*row_stride
)(const struct lp_sampler_dynamic_state
*state
,
254 struct gallivm_state
*gallivm
,
255 LLVMValueRef context_ptr
,
256 unsigned texture_unit
);
258 /** Obtain stride in bytes between image slices (returns int32) */
260 (*img_stride
)(const struct lp_sampler_dynamic_state
*state
,
261 struct gallivm_state
*gallivm
,
262 LLVMValueRef context_ptr
,
263 unsigned texture_unit
);
265 /** Obtain pointer to base of texture */
267 (*base_ptr
)(const struct lp_sampler_dynamic_state
*state
,
268 struct gallivm_state
*gallivm
,
269 LLVMValueRef context_ptr
,
270 unsigned texture_unit
);
272 /** Obtain pointer to array of mipmap offsets */
274 (*mip_offsets
)(const struct lp_sampler_dynamic_state
*state
,
275 struct gallivm_state
*gallivm
,
276 LLVMValueRef context_ptr
,
277 unsigned texture_unit
);
279 /* These are callbacks for sampler state */
281 /** Obtain texture min lod (returns float) */
283 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
284 struct gallivm_state
*gallivm
,
285 LLVMValueRef context_ptr
,
286 unsigned sampler_unit
);
288 /** Obtain texture max lod (returns float) */
290 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
291 struct gallivm_state
*gallivm
,
292 LLVMValueRef context_ptr
,
293 unsigned sampler_unit
);
295 /** Obtain texture lod bias (returns float) */
297 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
298 struct gallivm_state
*gallivm
,
299 LLVMValueRef context_ptr
,
300 unsigned sampler_unit
);
302 /** Obtain texture border color (returns ptr to float[4]) */
304 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
305 struct gallivm_state
*gallivm
,
306 LLVMValueRef context_ptr
,
307 unsigned sampler_unit
);
310 * Obtain texture cache (returns ptr to lp_build_format_cache).
312 * It's optional: no caching will be done if it's NULL.
315 (*cache_ptr
)(const struct lp_sampler_dynamic_state
*state
,
316 struct gallivm_state
*gallivm
,
317 LLVMValueRef thread_data_ptr
,
323 * Keep all information for sampling code generation in a single place.
325 struct lp_build_sample_context
327 struct gallivm_state
*gallivm
;
329 const struct lp_static_texture_state
*static_texture_state
;
330 const struct lp_static_sampler_state
*static_sampler_state
;
332 struct lp_sampler_dynamic_state
*dynamic_state
;
334 const struct util_format_description
*format_desc
;
336 /* See texture_dims() */
339 /** SIMD vector width */
340 unsigned vector_width
;
342 /** number of mipmaps (valid are 1, length/4, length) */
345 /** number of lod values (valid are 1, length/4, length) */
349 boolean no_brilinear
;
350 boolean no_rho_approx
;
352 /** regular scalar float type */
353 struct lp_type float_type
;
354 struct lp_build_context float_bld
;
356 /** float vector type */
357 struct lp_build_context float_vec_bld
;
359 /** regular scalar int type */
360 struct lp_type int_type
;
361 struct lp_build_context int_bld
;
363 /** Incoming coordinates type and build context */
364 struct lp_type coord_type
;
365 struct lp_build_context coord_bld
;
367 /** Signed integer coordinates */
368 struct lp_type int_coord_type
;
369 struct lp_build_context int_coord_bld
;
371 /** Unsigned integer texture size */
372 struct lp_type int_size_in_type
;
373 struct lp_build_context int_size_in_bld
;
375 /** Float incoming texture size */
376 struct lp_type float_size_in_type
;
377 struct lp_build_context float_size_in_bld
;
379 /** Unsigned integer texture size (might be per quad) */
380 struct lp_type int_size_type
;
381 struct lp_build_context int_size_bld
;
383 /** Float texture size (might be per quad) */
384 struct lp_type float_size_type
;
385 struct lp_build_context float_size_bld
;
387 /** Output texels type and build context */
388 struct lp_type texel_type
;
389 struct lp_build_context texel_bld
;
391 /** Float level type */
392 struct lp_type levelf_type
;
393 struct lp_build_context levelf_bld
;
395 /** Int level type */
396 struct lp_type leveli_type
;
397 struct lp_build_context leveli_bld
;
399 /** Float lod type */
400 struct lp_type lodf_type
;
401 struct lp_build_context lodf_bld
;
404 struct lp_type lodi_type
;
405 struct lp_build_context lodi_bld
;
407 /* Common dynamic state values */
408 LLVMValueRef row_stride_array
;
409 LLVMValueRef img_stride_array
;
410 LLVMValueRef base_ptr
;
411 LLVMValueRef mip_offsets
;
414 /** Integer vector with texture width, height, depth */
415 LLVMValueRef int_size
;
417 LLVMValueRef border_color_clamped
;
419 LLVMValueRef context_ptr
;
425 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
426 * this time. Return whether the given mode is supported by that function.
428 static inline boolean
429 lp_is_simple_wrap_mode(unsigned mode
)
432 case PIPE_TEX_WRAP_REPEAT
:
433 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
442 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
445 unsigned char swizzles
[4];
447 swizzles
[0] = bld
->static_texture_state
->swizzle_r
;
448 swizzles
[1] = bld
->static_texture_state
->swizzle_g
;
449 swizzles
[2] = bld
->static_texture_state
->swizzle_b
;
450 swizzles
[3] = bld
->static_texture_state
->swizzle_a
;
452 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
456 * not really dimension as such, this indicates the amount of
457 * "normal" texture coords subject to minification, wrapping etc.
459 static inline unsigned
460 texture_dims(enum pipe_texture_target tex
)
463 case PIPE_TEXTURE_1D
:
464 case PIPE_TEXTURE_1D_ARRAY
:
467 case PIPE_TEXTURE_2D
:
468 case PIPE_TEXTURE_2D_ARRAY
:
469 case PIPE_TEXTURE_RECT
:
470 case PIPE_TEXTURE_CUBE
:
471 case PIPE_TEXTURE_CUBE_ARRAY
:
473 case PIPE_TEXTURE_3D
:
476 assert(0 && "bad texture target in texture_dims()");
481 static inline boolean
482 has_layer_coord(enum pipe_texture_target tex
)
485 case PIPE_TEXTURE_1D_ARRAY
:
486 case PIPE_TEXTURE_2D_ARRAY
:
487 /* cube is not layered but 3rd coord (after cube mapping) behaves the same */
488 case PIPE_TEXTURE_CUBE
:
489 case PIPE_TEXTURE_CUBE_ARRAY
:
498 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
499 unsigned min_img_filter
,
500 unsigned mag_img_filter
);
503 * Derive the sampler static state.
506 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
507 const struct pipe_sampler_state
*sampler
);
511 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
512 const struct pipe_sampler_view
*view
);
515 lp_sampler_static_texture_state_image(struct lp_static_texture_state
*state
,
516 const struct pipe_image_view
*view
);
519 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
521 unsigned texture_index
,
522 unsigned sampler_index
,
526 LLVMValueRef cube_rho
,
527 const struct lp_derivatives
*derivs
,
528 LLVMValueRef lod_bias
, /* optional */
529 LLVMValueRef explicit_lod
, /* optional */
531 LLVMValueRef
*out_lod
,
532 LLVMValueRef
*out_lod_ipart
,
533 LLVMValueRef
*out_lod_fpart
,
534 LLVMValueRef
*out_lod_positive
);
537 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
538 unsigned texture_unit
,
540 LLVMValueRef
*level_out
,
541 LLVMValueRef
*out_of_bounds
);
544 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
545 unsigned texture_unit
,
546 LLVMValueRef lod_ipart
,
547 LLVMValueRef
*lod_fpart_inout
,
548 LLVMValueRef
*level0_out
,
549 LLVMValueRef
*level1_out
);
552 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
557 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
562 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
564 LLVMValueRef
*out_size_vec
,
565 LLVMValueRef
*row_stride_vec
,
566 LLVMValueRef
*img_stride_vec
);
570 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
571 struct lp_build_context
*size_bld
,
572 struct lp_type coord_type
,
574 LLVMValueRef
*out_width
,
575 LLVMValueRef
*out_height
,
576 LLVMValueRef
*out_depth
);
580 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
581 LLVMValueRef flt_size
,
588 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
589 LLVMValueRef
*coords
,
590 const struct lp_derivatives
*derivs_in
, /* optional */
592 struct lp_derivatives
*derivs_out
, /* optional */
593 boolean need_derivs
);
597 lp_build_cube_new_coords(struct lp_build_context
*ivec_bld
,
603 LLVMValueRef max_coord
,
604 LLVMValueRef new_faces
[4],
605 LLVMValueRef new_xcoords
[4][2],
606 LLVMValueRef new_ycoords
[4][2]);
610 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
611 unsigned block_length
,
614 LLVMValueRef
*out_offset
,
615 LLVMValueRef
*out_i
);
619 lp_build_sample_offset(struct lp_build_context
*bld
,
620 const struct util_format_description
*format_desc
,
624 LLVMValueRef y_stride
,
625 LLVMValueRef z_stride
,
626 LLVMValueRef
*out_offset
,
628 LLVMValueRef
*out_j
);
632 lp_build_sample_soa(const struct lp_static_texture_state
*static_texture_state
,
633 const struct lp_static_sampler_state
*static_sampler_state
,
634 struct lp_sampler_dynamic_state
*dynamic_texture_state
,
635 struct gallivm_state
*gallivm
,
636 const struct lp_sampler_params
*params
);
640 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
641 LLVMValueRef coord_f
,
642 LLVMValueRef length_i
,
643 LLVMValueRef length_f
,
644 LLVMValueRef
*coord0_i
,
645 LLVMValueRef
*weight_f
);
649 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
650 const struct lp_static_texture_state
*static_state
,
651 struct lp_sampler_dynamic_state
*dynamic_state
,
652 const struct lp_sampler_size_query_params
*params
);
655 lp_build_sample_nop(struct gallivm_state
*gallivm
,
657 const LLVMValueRef
*coords
,
658 LLVMValueRef texel_out
[4]);
662 lp_build_minify(struct lp_build_context
*bld
,
663 LLVMValueRef base_size
,
668 lp_build_img_op_soa(const struct lp_static_texture_state
*static_texture_state
,
669 struct lp_sampler_dynamic_state
*dynamic_state
,
670 struct gallivm_state
*gallivm
,
671 const struct lp_img_params
*params
);
677 #endif /* LP_BLD_SAMPLE_H */