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 util_format_description
;
54 struct lp_build_context
;
58 * Helper struct holding all derivatives needed for sampling
67 enum lp_sampler_lod_property
{
68 LP_SAMPLER_LOD_SCALAR
,
69 LP_SAMPLER_LOD_PER_ELEMENT
,
70 LP_SAMPLER_LOD_PER_QUAD
74 enum lp_sampler_lod_control
{
75 LP_SAMPLER_LOD_IMPLICIT
,
77 LP_SAMPLER_LOD_EXPLICIT
,
78 LP_SAMPLER_LOD_DERIVATIVES
,
82 enum lp_sampler_op_type
{
83 LP_SAMPLER_OP_TEXTURE
,
89 #define LP_SAMPLER_SHADOW (1 << 0)
90 #define LP_SAMPLER_OFFSETS (1 << 1)
91 #define LP_SAMPLER_OP_TYPE_SHIFT 2
92 #define LP_SAMPLER_OP_TYPE_MASK (3 << 2)
93 #define LP_SAMPLER_LOD_CONTROL_SHIFT 4
94 #define LP_SAMPLER_LOD_CONTROL_MASK (3 << 4)
95 #define LP_SAMPLER_LOD_PROPERTY_SHIFT 6
96 #define LP_SAMPLER_LOD_PROPERTY_MASK (3 << 6)
98 struct lp_sampler_params
101 unsigned texture_index
;
102 unsigned sampler_index
;
104 LLVMValueRef context_ptr
;
105 LLVMValueRef thread_data_ptr
;
106 const LLVMValueRef
*coords
;
107 const LLVMValueRef
*offsets
;
109 const struct lp_derivatives
*derivs
;
113 struct lp_sampler_size_query_params
115 struct lp_type int_type
;
116 unsigned texture_unit
;
118 LLVMValueRef context_ptr
;
119 boolean is_sviewinfo
;
120 enum lp_sampler_lod_property lod_property
;
121 LLVMValueRef explicit_lod
;
122 LLVMValueRef
*sizes_out
;
125 * Texture static state.
127 * These are the bits of state from pipe_resource/pipe_sampler_view that
128 * are embedded in the generated code.
130 struct lp_static_texture_state
132 /* pipe_sampler_view's state */
133 enum pipe_format format
;
134 unsigned swizzle_r
:3; /**< PIPE_SWIZZLE_* */
135 unsigned swizzle_g
:3;
136 unsigned swizzle_b
:3;
137 unsigned swizzle_a
:3;
139 /* pipe_texture's state */
140 unsigned target
:4; /**< PIPE_TEXTURE_* */
141 unsigned pot_width
:1; /**< is the width a power of two? */
142 unsigned pot_height
:1;
143 unsigned pot_depth
:1;
144 unsigned level_zero_only
:1;
149 * Sampler static state.
151 * These are the bits of state from pipe_sampler_state that
152 * are embedded in the generated code.
154 struct lp_static_sampler_state
156 /* pipe_sampler_state's state */
160 unsigned min_img_filter
:2;
161 unsigned min_mip_filter
:2;
162 unsigned mag_img_filter
:2;
163 unsigned compare_mode
:1;
164 unsigned compare_func
:3;
165 unsigned normalized_coords
:1;
166 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
167 unsigned lod_bias_non_zero
:1;
168 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
169 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
170 unsigned seamless_cube_map
:1;
173 unsigned force_nearest_s
:1;
174 unsigned force_nearest_t
:1;
179 * Sampler dynamic state.
181 * These are the bits of state from pipe_resource/pipe_sampler_view
182 * as well as from sampler state that are computed at runtime.
184 * There are obtained through callbacks, as we don't want to tie the texture
185 * sampling code generation logic to any particular texture layout or pipe
188 struct lp_sampler_dynamic_state
190 /* First callbacks for sampler view state */
192 /** Obtain the base texture width (or number of elements) (returns int32) */
194 (*width
)(const struct lp_sampler_dynamic_state
*state
,
195 struct gallivm_state
*gallivm
,
196 LLVMValueRef context_ptr
,
197 unsigned texture_unit
);
199 /** Obtain the base texture height (returns int32) */
201 (*height
)(const struct lp_sampler_dynamic_state
*state
,
202 struct gallivm_state
*gallivm
,
203 LLVMValueRef context_ptr
,
204 unsigned texture_unit
);
206 /** Obtain the base texture depth (or array size) (returns int32) */
208 (*depth
)(const struct lp_sampler_dynamic_state
*state
,
209 struct gallivm_state
*gallivm
,
210 LLVMValueRef context_ptr
,
211 unsigned texture_unit
);
213 /** Obtain the first mipmap level (base level) (returns int32) */
215 (*first_level
)(const struct lp_sampler_dynamic_state
*state
,
216 struct gallivm_state
*gallivm
,
217 LLVMValueRef context_ptr
,
218 unsigned texture_unit
);
220 /** Obtain the number of mipmap levels minus one (returns int32) */
222 (*last_level
)(const struct lp_sampler_dynamic_state
*state
,
223 struct gallivm_state
*gallivm
,
224 LLVMValueRef context_ptr
,
225 unsigned texture_unit
);
227 /** Obtain stride in bytes between image rows/blocks (returns int32) */
229 (*row_stride
)(const struct lp_sampler_dynamic_state
*state
,
230 struct gallivm_state
*gallivm
,
231 LLVMValueRef context_ptr
,
232 unsigned texture_unit
);
234 /** Obtain stride in bytes between image slices (returns int32) */
236 (*img_stride
)(const struct lp_sampler_dynamic_state
*state
,
237 struct gallivm_state
*gallivm
,
238 LLVMValueRef context_ptr
,
239 unsigned texture_unit
);
241 /** Obtain pointer to base of texture */
243 (*base_ptr
)(const struct lp_sampler_dynamic_state
*state
,
244 struct gallivm_state
*gallivm
,
245 LLVMValueRef context_ptr
,
246 unsigned texture_unit
);
248 /** Obtain pointer to array of mipmap offsets */
250 (*mip_offsets
)(const struct lp_sampler_dynamic_state
*state
,
251 struct gallivm_state
*gallivm
,
252 LLVMValueRef context_ptr
,
253 unsigned texture_unit
);
255 /* These are callbacks for sampler state */
257 /** Obtain texture min lod (returns float) */
259 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
260 struct gallivm_state
*gallivm
,
261 LLVMValueRef context_ptr
,
262 unsigned sampler_unit
);
264 /** Obtain texture max lod (returns float) */
266 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
267 struct gallivm_state
*gallivm
,
268 LLVMValueRef context_ptr
,
269 unsigned sampler_unit
);
271 /** Obtain texture lod bias (returns float) */
273 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
274 struct gallivm_state
*gallivm
,
275 LLVMValueRef context_ptr
,
276 unsigned sampler_unit
);
278 /** Obtain texture border color (returns ptr to float[4]) */
280 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
281 struct gallivm_state
*gallivm
,
282 LLVMValueRef context_ptr
,
283 unsigned sampler_unit
);
286 * Obtain texture cache (returns ptr to lp_build_format_cache).
288 * It's optional: no caching will be done if it's NULL.
291 (*cache_ptr
)(const struct lp_sampler_dynamic_state
*state
,
292 struct gallivm_state
*gallivm
,
293 LLVMValueRef thread_data_ptr
,
299 * Keep all information for sampling code generation in a single place.
301 struct lp_build_sample_context
303 struct gallivm_state
*gallivm
;
305 const struct lp_static_texture_state
*static_texture_state
;
306 const struct lp_static_sampler_state
*static_sampler_state
;
308 struct lp_sampler_dynamic_state
*dynamic_state
;
310 const struct util_format_description
*format_desc
;
312 /* See texture_dims() */
315 /** SIMD vector width */
316 unsigned vector_width
;
318 /** number of mipmaps (valid are 1, length/4, length) */
321 /** number of lod values (valid are 1, length/4, length) */
324 /** regular scalar float type */
325 struct lp_type float_type
;
326 struct lp_build_context float_bld
;
328 /** float vector type */
329 struct lp_build_context float_vec_bld
;
331 /** regular scalar int type */
332 struct lp_type int_type
;
333 struct lp_build_context int_bld
;
335 /** Incoming coordinates type and build context */
336 struct lp_type coord_type
;
337 struct lp_build_context coord_bld
;
339 /** Signed integer coordinates */
340 struct lp_type int_coord_type
;
341 struct lp_build_context int_coord_bld
;
343 /** Unsigned integer texture size */
344 struct lp_type int_size_in_type
;
345 struct lp_build_context int_size_in_bld
;
347 /** Float incoming texture size */
348 struct lp_type float_size_in_type
;
349 struct lp_build_context float_size_in_bld
;
351 /** Unsigned integer texture size (might be per quad) */
352 struct lp_type int_size_type
;
353 struct lp_build_context int_size_bld
;
355 /** Float texture size (might be per quad) */
356 struct lp_type float_size_type
;
357 struct lp_build_context float_size_bld
;
359 /** Output texels type and build context */
360 struct lp_type texel_type
;
361 struct lp_build_context texel_bld
;
363 /** Float level type */
364 struct lp_type levelf_type
;
365 struct lp_build_context levelf_bld
;
367 /** Int level type */
368 struct lp_type leveli_type
;
369 struct lp_build_context leveli_bld
;
371 /** Float lod type */
372 struct lp_type lodf_type
;
373 struct lp_build_context lodf_bld
;
376 struct lp_type lodi_type
;
377 struct lp_build_context lodi_bld
;
379 /* Common dynamic state values */
380 LLVMValueRef row_stride_array
;
381 LLVMValueRef img_stride_array
;
382 LLVMValueRef base_ptr
;
383 LLVMValueRef mip_offsets
;
386 /** Integer vector with texture width, height, depth */
387 LLVMValueRef int_size
;
389 LLVMValueRef border_color_clamped
;
391 LLVMValueRef context_ptr
;
397 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
398 * this time. Return whether the given mode is supported by that function.
400 static inline boolean
401 lp_is_simple_wrap_mode(unsigned mode
)
404 case PIPE_TEX_WRAP_REPEAT
:
405 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
414 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
417 unsigned char swizzles
[4];
419 swizzles
[0] = bld
->static_texture_state
->swizzle_r
;
420 swizzles
[1] = bld
->static_texture_state
->swizzle_g
;
421 swizzles
[2] = bld
->static_texture_state
->swizzle_b
;
422 swizzles
[3] = bld
->static_texture_state
->swizzle_a
;
424 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
428 * not really dimension as such, this indicates the amount of
429 * "normal" texture coords subject to minification, wrapping etc.
431 static inline unsigned
432 texture_dims(enum pipe_texture_target tex
)
435 case PIPE_TEXTURE_1D
:
436 case PIPE_TEXTURE_1D_ARRAY
:
439 case PIPE_TEXTURE_2D
:
440 case PIPE_TEXTURE_2D_ARRAY
:
441 case PIPE_TEXTURE_RECT
:
442 case PIPE_TEXTURE_CUBE
:
443 case PIPE_TEXTURE_CUBE_ARRAY
:
445 case PIPE_TEXTURE_3D
:
448 assert(0 && "bad texture target in texture_dims()");
453 static inline boolean
454 has_layer_coord(enum pipe_texture_target tex
)
457 case PIPE_TEXTURE_1D_ARRAY
:
458 case PIPE_TEXTURE_2D_ARRAY
:
459 /* cube is not layered but 3rd coord (after cube mapping) behaves the same */
460 case PIPE_TEXTURE_CUBE
:
461 case PIPE_TEXTURE_CUBE_ARRAY
:
470 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
471 unsigned min_img_filter
,
472 unsigned mag_img_filter
);
475 * Derive the sampler static state.
478 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
479 const struct pipe_sampler_state
*sampler
);
483 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
484 const struct pipe_sampler_view
*view
);
488 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
489 unsigned texture_index
,
490 unsigned sampler_index
,
494 LLVMValueRef cube_rho
,
495 const struct lp_derivatives
*derivs
,
496 LLVMValueRef lod_bias
, /* optional */
497 LLVMValueRef explicit_lod
, /* optional */
499 LLVMValueRef
*out_lod_ipart
,
500 LLVMValueRef
*out_lod_fpart
,
501 LLVMValueRef
*out_lod_positive
);
504 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
505 unsigned texture_unit
,
507 LLVMValueRef
*level_out
,
508 LLVMValueRef
*out_of_bounds
);
511 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
512 unsigned texture_unit
,
513 LLVMValueRef lod_ipart
,
514 LLVMValueRef
*lod_fpart_inout
,
515 LLVMValueRef
*level0_out
,
516 LLVMValueRef
*level1_out
);
519 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
524 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
529 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
531 LLVMValueRef
*out_size_vec
,
532 LLVMValueRef
*row_stride_vec
,
533 LLVMValueRef
*img_stride_vec
);
537 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
538 struct lp_build_context
*size_bld
,
539 struct lp_type coord_type
,
541 LLVMValueRef
*out_width
,
542 LLVMValueRef
*out_height
,
543 LLVMValueRef
*out_depth
);
547 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
548 LLVMValueRef flt_size
,
555 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
556 LLVMValueRef
*coords
,
557 const struct lp_derivatives
*derivs_in
, /* optional */
559 struct lp_derivatives
*derivs_out
, /* optional */
560 boolean need_derivs
);
564 lp_build_cube_new_coords(struct lp_build_context
*ivec_bld
,
570 LLVMValueRef max_coord
,
571 LLVMValueRef new_faces
[4],
572 LLVMValueRef new_xcoords
[4][2],
573 LLVMValueRef new_ycoords
[4][2]);
577 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
578 unsigned block_length
,
581 LLVMValueRef
*out_offset
,
582 LLVMValueRef
*out_i
);
586 lp_build_sample_offset(struct lp_build_context
*bld
,
587 const struct util_format_description
*format_desc
,
591 LLVMValueRef y_stride
,
592 LLVMValueRef z_stride
,
593 LLVMValueRef
*out_offset
,
595 LLVMValueRef
*out_j
);
599 lp_build_sample_soa(const struct lp_static_texture_state
*static_texture_state
,
600 const struct lp_static_sampler_state
*static_sampler_state
,
601 struct lp_sampler_dynamic_state
*dynamic_texture_state
,
602 struct gallivm_state
*gallivm
,
603 const struct lp_sampler_params
*params
);
607 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
608 LLVMValueRef coord_f
,
609 LLVMValueRef length_i
,
610 LLVMValueRef length_f
,
611 LLVMValueRef
*coord0_i
,
612 LLVMValueRef
*weight_f
);
616 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
617 const struct lp_static_texture_state
*static_state
,
618 struct lp_sampler_dynamic_state
*dynamic_state
,
619 const struct lp_sampler_size_query_params
*params
);
622 lp_build_sample_nop(struct gallivm_state
*gallivm
,
624 const LLVMValueRef
*coords
,
625 LLVMValueRef texel_out
[4]);
629 lp_build_minify(struct lp_build_context
*bld
,
630 LLVMValueRef base_size
,
638 #endif /* LP_BLD_SAMPLE_H */