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
;
115 * Texture static state.
117 * These are the bits of state from pipe_resource/pipe_sampler_view that
118 * are embedded in the generated code.
120 struct lp_static_texture_state
122 /* pipe_sampler_view's state */
123 enum pipe_format format
;
124 unsigned swizzle_r
:3; /**< PIPE_SWIZZLE_* */
125 unsigned swizzle_g
:3;
126 unsigned swizzle_b
:3;
127 unsigned swizzle_a
:3;
129 /* pipe_texture's state */
130 unsigned target
:4; /**< PIPE_TEXTURE_* */
131 unsigned pot_width
:1; /**< is the width a power of two? */
132 unsigned pot_height
:1;
133 unsigned pot_depth
:1;
134 unsigned level_zero_only
:1;
139 * Sampler static state.
141 * These are the bits of state from pipe_sampler_state that
142 * are embedded in the generated code.
144 struct lp_static_sampler_state
146 /* pipe_sampler_state's state */
150 unsigned min_img_filter
:2;
151 unsigned min_mip_filter
:2;
152 unsigned mag_img_filter
:2;
153 unsigned compare_mode
:1;
154 unsigned compare_func
:3;
155 unsigned normalized_coords
:1;
156 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
157 unsigned lod_bias_non_zero
:1;
158 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
159 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
160 unsigned seamless_cube_map
:1;
163 unsigned force_nearest_s
:1;
164 unsigned force_nearest_t
:1;
169 * Sampler dynamic state.
171 * These are the bits of state from pipe_resource/pipe_sampler_view
172 * as well as from sampler state that are computed at runtime.
174 * There are obtained through callbacks, as we don't want to tie the texture
175 * sampling code generation logic to any particular texture layout or pipe
178 struct lp_sampler_dynamic_state
180 /* First callbacks for sampler view state */
182 /** Obtain the base texture width (or number of elements) (returns int32) */
184 (*width
)(const struct lp_sampler_dynamic_state
*state
,
185 struct gallivm_state
*gallivm
,
186 LLVMValueRef context_ptr
,
187 unsigned texture_unit
);
189 /** Obtain the base texture height (returns int32) */
191 (*height
)(const struct lp_sampler_dynamic_state
*state
,
192 struct gallivm_state
*gallivm
,
193 LLVMValueRef context_ptr
,
194 unsigned texture_unit
);
196 /** Obtain the base texture depth (or array size) (returns int32) */
198 (*depth
)(const struct lp_sampler_dynamic_state
*state
,
199 struct gallivm_state
*gallivm
,
200 LLVMValueRef context_ptr
,
201 unsigned texture_unit
);
203 /** Obtain the first mipmap level (base level) (returns int32) */
205 (*first_level
)(const struct lp_sampler_dynamic_state
*state
,
206 struct gallivm_state
*gallivm
,
207 LLVMValueRef context_ptr
,
208 unsigned texture_unit
);
210 /** Obtain the number of mipmap levels minus one (returns int32) */
212 (*last_level
)(const struct lp_sampler_dynamic_state
*state
,
213 struct gallivm_state
*gallivm
,
214 LLVMValueRef context_ptr
,
215 unsigned texture_unit
);
217 /** Obtain stride in bytes between image rows/blocks (returns int32) */
219 (*row_stride
)(const struct lp_sampler_dynamic_state
*state
,
220 struct gallivm_state
*gallivm
,
221 LLVMValueRef context_ptr
,
222 unsigned texture_unit
);
224 /** Obtain stride in bytes between image slices (returns int32) */
226 (*img_stride
)(const struct lp_sampler_dynamic_state
*state
,
227 struct gallivm_state
*gallivm
,
228 LLVMValueRef context_ptr
,
229 unsigned texture_unit
);
231 /** Obtain pointer to base of texture */
233 (*base_ptr
)(const struct lp_sampler_dynamic_state
*state
,
234 struct gallivm_state
*gallivm
,
235 LLVMValueRef context_ptr
,
236 unsigned texture_unit
);
238 /** Obtain pointer to array of mipmap offsets */
240 (*mip_offsets
)(const struct lp_sampler_dynamic_state
*state
,
241 struct gallivm_state
*gallivm
,
242 LLVMValueRef context_ptr
,
243 unsigned texture_unit
);
245 /* These are callbacks for sampler state */
247 /** Obtain texture min lod (returns float) */
249 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
250 struct gallivm_state
*gallivm
,
251 LLVMValueRef context_ptr
,
252 unsigned sampler_unit
);
254 /** Obtain texture max lod (returns float) */
256 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
257 struct gallivm_state
*gallivm
,
258 LLVMValueRef context_ptr
,
259 unsigned sampler_unit
);
261 /** Obtain texture lod bias (returns float) */
263 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
264 struct gallivm_state
*gallivm
,
265 LLVMValueRef context_ptr
,
266 unsigned sampler_unit
);
268 /** Obtain texture border color (returns ptr to float[4]) */
270 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
271 struct gallivm_state
*gallivm
,
272 LLVMValueRef context_ptr
,
273 unsigned sampler_unit
);
276 * Obtain texture cache (returns ptr to lp_build_format_cache).
278 * It's optional: no caching will be done if it's NULL.
281 (*cache_ptr
)(const struct lp_sampler_dynamic_state
*state
,
282 struct gallivm_state
*gallivm
,
283 LLVMValueRef thread_data_ptr
,
289 * Keep all information for sampling code generation in a single place.
291 struct lp_build_sample_context
293 struct gallivm_state
*gallivm
;
295 const struct lp_static_texture_state
*static_texture_state
;
296 const struct lp_static_sampler_state
*static_sampler_state
;
298 struct lp_sampler_dynamic_state
*dynamic_state
;
300 const struct util_format_description
*format_desc
;
302 /* See texture_dims() */
305 /** SIMD vector width */
306 unsigned vector_width
;
308 /** number of mipmaps (valid are 1, length/4, length) */
311 /** number of lod values (valid are 1, length/4, length) */
314 /** regular scalar float type */
315 struct lp_type float_type
;
316 struct lp_build_context float_bld
;
318 /** float vector type */
319 struct lp_build_context float_vec_bld
;
321 /** regular scalar int type */
322 struct lp_type int_type
;
323 struct lp_build_context int_bld
;
325 /** Incoming coordinates type and build context */
326 struct lp_type coord_type
;
327 struct lp_build_context coord_bld
;
329 /** Signed integer coordinates */
330 struct lp_type int_coord_type
;
331 struct lp_build_context int_coord_bld
;
333 /** Unsigned integer texture size */
334 struct lp_type int_size_in_type
;
335 struct lp_build_context int_size_in_bld
;
337 /** Float incoming texture size */
338 struct lp_type float_size_in_type
;
339 struct lp_build_context float_size_in_bld
;
341 /** Unsigned integer texture size (might be per quad) */
342 struct lp_type int_size_type
;
343 struct lp_build_context int_size_bld
;
345 /** Float texture size (might be per quad) */
346 struct lp_type float_size_type
;
347 struct lp_build_context float_size_bld
;
349 /** Output texels type and build context */
350 struct lp_type texel_type
;
351 struct lp_build_context texel_bld
;
353 /** Float level type */
354 struct lp_type levelf_type
;
355 struct lp_build_context levelf_bld
;
357 /** Int level type */
358 struct lp_type leveli_type
;
359 struct lp_build_context leveli_bld
;
361 /** Float lod type */
362 struct lp_type lodf_type
;
363 struct lp_build_context lodf_bld
;
366 struct lp_type lodi_type
;
367 struct lp_build_context lodi_bld
;
369 /* Common dynamic state values */
370 LLVMValueRef row_stride_array
;
371 LLVMValueRef img_stride_array
;
372 LLVMValueRef base_ptr
;
373 LLVMValueRef mip_offsets
;
376 /** Integer vector with texture width, height, depth */
377 LLVMValueRef int_size
;
379 LLVMValueRef border_color_clamped
;
381 LLVMValueRef context_ptr
;
387 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
388 * this time. Return whether the given mode is supported by that function.
390 static inline boolean
391 lp_is_simple_wrap_mode(unsigned mode
)
394 case PIPE_TEX_WRAP_REPEAT
:
395 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
404 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
407 unsigned char swizzles
[4];
409 swizzles
[0] = bld
->static_texture_state
->swizzle_r
;
410 swizzles
[1] = bld
->static_texture_state
->swizzle_g
;
411 swizzles
[2] = bld
->static_texture_state
->swizzle_b
;
412 swizzles
[3] = bld
->static_texture_state
->swizzle_a
;
414 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
418 * not really dimension as such, this indicates the amount of
419 * "normal" texture coords subject to minification, wrapping etc.
421 static inline unsigned
422 texture_dims(enum pipe_texture_target tex
)
425 case PIPE_TEXTURE_1D
:
426 case PIPE_TEXTURE_1D_ARRAY
:
429 case PIPE_TEXTURE_2D
:
430 case PIPE_TEXTURE_2D_ARRAY
:
431 case PIPE_TEXTURE_RECT
:
432 case PIPE_TEXTURE_CUBE
:
433 case PIPE_TEXTURE_CUBE_ARRAY
:
435 case PIPE_TEXTURE_3D
:
438 assert(0 && "bad texture target in texture_dims()");
443 static inline boolean
444 has_layer_coord(enum pipe_texture_target tex
)
447 case PIPE_TEXTURE_1D_ARRAY
:
448 case PIPE_TEXTURE_2D_ARRAY
:
449 /* cube is not layered but 3rd coord (after cube mapping) behaves the same */
450 case PIPE_TEXTURE_CUBE
:
451 case PIPE_TEXTURE_CUBE_ARRAY
:
460 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
461 unsigned min_img_filter
,
462 unsigned mag_img_filter
);
465 * Derive the sampler static state.
468 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
469 const struct pipe_sampler_state
*sampler
);
473 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
474 const struct pipe_sampler_view
*view
);
478 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
479 unsigned texture_index
,
480 unsigned sampler_index
,
484 LLVMValueRef cube_rho
,
485 const struct lp_derivatives
*derivs
,
486 LLVMValueRef lod_bias
, /* optional */
487 LLVMValueRef explicit_lod
, /* optional */
489 LLVMValueRef
*out_lod_ipart
,
490 LLVMValueRef
*out_lod_fpart
,
491 LLVMValueRef
*out_lod_positive
);
494 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
495 unsigned texture_unit
,
497 LLVMValueRef
*level_out
,
498 LLVMValueRef
*out_of_bounds
);
501 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
502 unsigned texture_unit
,
503 LLVMValueRef lod_ipart
,
504 LLVMValueRef
*lod_fpart_inout
,
505 LLVMValueRef
*level0_out
,
506 LLVMValueRef
*level1_out
);
509 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
514 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
519 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
521 LLVMValueRef
*out_size_vec
,
522 LLVMValueRef
*row_stride_vec
,
523 LLVMValueRef
*img_stride_vec
);
527 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
528 struct lp_build_context
*size_bld
,
529 struct lp_type coord_type
,
531 LLVMValueRef
*out_width
,
532 LLVMValueRef
*out_height
,
533 LLVMValueRef
*out_depth
);
537 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
538 LLVMValueRef flt_size
,
545 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
546 LLVMValueRef
*coords
,
547 const struct lp_derivatives
*derivs_in
, /* optional */
549 struct lp_derivatives
*derivs_out
, /* optional */
550 boolean need_derivs
);
554 lp_build_cube_new_coords(struct lp_build_context
*ivec_bld
,
560 LLVMValueRef max_coord
,
561 LLVMValueRef new_faces
[4],
562 LLVMValueRef new_xcoords
[4][2],
563 LLVMValueRef new_ycoords
[4][2]);
567 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
568 unsigned block_length
,
571 LLVMValueRef
*out_offset
,
572 LLVMValueRef
*out_i
);
576 lp_build_sample_offset(struct lp_build_context
*bld
,
577 const struct util_format_description
*format_desc
,
581 LLVMValueRef y_stride
,
582 LLVMValueRef z_stride
,
583 LLVMValueRef
*out_offset
,
585 LLVMValueRef
*out_j
);
589 lp_build_sample_soa(const struct lp_static_texture_state
*static_texture_state
,
590 const struct lp_static_sampler_state
*static_sampler_state
,
591 struct lp_sampler_dynamic_state
*dynamic_texture_state
,
592 struct gallivm_state
*gallivm
,
593 const struct lp_sampler_params
*params
);
597 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
598 LLVMValueRef coord_f
,
599 LLVMValueRef length_i
,
600 LLVMValueRef length_f
,
601 LLVMValueRef
*coord0_i
,
602 LLVMValueRef
*weight_f
);
606 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
607 const struct lp_static_texture_state
*static_state
,
608 struct lp_sampler_dynamic_state
*dynamic_state
,
609 struct lp_type int_type
,
610 unsigned texture_unit
,
612 LLVMValueRef context_ptr
,
613 boolean is_sviewinfo
,
614 enum lp_sampler_lod_property lod_property
,
615 LLVMValueRef explicit_lod
,
616 LLVMValueRef
*sizes_out
);
619 lp_build_sample_nop(struct gallivm_state
*gallivm
,
621 const LLVMValueRef
*coords
,
622 LLVMValueRef texel_out
[4]);
626 lp_build_minify(struct lp_build_context
*bld
,
627 LLVMValueRef base_size
,
635 #endif /* LP_BLD_SAMPLE_H */