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"
47 struct pipe_sampler_view
;
48 struct pipe_sampler_state
;
49 struct util_format_description
;
51 struct lp_build_context
;
55 * Helper struct holding all derivatives needed for sampling
64 enum lp_sampler_lod_property
{
65 LP_SAMPLER_LOD_SCALAR
,
66 LP_SAMPLER_LOD_PER_ELEMENT
,
67 LP_SAMPLER_LOD_PER_QUAD
71 enum lp_sampler_lod_control
{
72 LP_SAMPLER_LOD_IMPLICIT
,
74 LP_SAMPLER_LOD_EXPLICIT
,
75 LP_SAMPLER_LOD_DERIVATIVES
,
79 enum lp_sampler_op_type
{
80 LP_SAMPLER_OP_TEXTURE
,
86 #define LP_SAMPLER_SHADOW (1 << 0)
87 #define LP_SAMPLER_OFFSETS (1 << 1)
88 #define LP_SAMPLER_OP_TYPE_SHIFT 2
89 #define LP_SAMPLER_OP_TYPE_MASK (3 << 2)
90 #define LP_SAMPLER_LOD_CONTROL_SHIFT 4
91 #define LP_SAMPLER_LOD_CONTROL_MASK (3 << 4)
92 #define LP_SAMPLER_LOD_PROPERTY_SHIFT 6
93 #define LP_SAMPLER_LOD_PROPERTY_MASK (3 << 6)
95 struct lp_sampler_params
98 unsigned texture_index
;
99 unsigned sampler_index
;
101 LLVMValueRef context_ptr
;
102 const LLVMValueRef
*coords
;
103 const LLVMValueRef
*offsets
;
105 const struct lp_derivatives
*derivs
;
111 * Texture static state.
113 * These are the bits of state from pipe_resource/pipe_sampler_view that
114 * are embedded in the generated code.
116 struct lp_static_texture_state
118 /* pipe_sampler_view's state */
119 enum pipe_format format
;
120 unsigned swizzle_r
:3; /**< PIPE_SWIZZLE_* */
121 unsigned swizzle_g
:3;
122 unsigned swizzle_b
:3;
123 unsigned swizzle_a
:3;
125 /* pipe_texture's state */
126 unsigned target
:4; /**< PIPE_TEXTURE_* */
127 unsigned pot_width
:1; /**< is the width a power of two? */
128 unsigned pot_height
:1;
129 unsigned pot_depth
:1;
130 unsigned level_zero_only
:1;
135 * Sampler static state.
137 * These are the bits of state from pipe_sampler_state that
138 * are embedded in the generated code.
140 struct lp_static_sampler_state
142 /* pipe_sampler_state's state */
146 unsigned min_img_filter
:2;
147 unsigned min_mip_filter
:2;
148 unsigned mag_img_filter
:2;
149 unsigned compare_mode
:1;
150 unsigned compare_func
:3;
151 unsigned normalized_coords
:1;
152 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
153 unsigned lod_bias_non_zero
:1;
154 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
155 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
156 unsigned seamless_cube_map
:1;
159 unsigned force_nearest_s
:1;
160 unsigned force_nearest_t
:1;
165 * Sampler dynamic state.
167 * These are the bits of state from pipe_resource/pipe_sampler_view
168 * as well as from sampler state that are computed at runtime.
170 * There are obtained through callbacks, as we don't want to tie the texture
171 * sampling code generation logic to any particular texture layout or pipe
174 struct lp_sampler_dynamic_state
176 /* First callbacks for sampler view state */
178 /** Obtain the base texture width (or number of elements) (returns int32) */
180 (*width
)(const struct lp_sampler_dynamic_state
*state
,
181 struct gallivm_state
*gallivm
,
182 LLVMValueRef context_ptr
,
183 unsigned texture_unit
);
185 /** Obtain the base texture height (returns int32) */
187 (*height
)(const struct lp_sampler_dynamic_state
*state
,
188 struct gallivm_state
*gallivm
,
189 LLVMValueRef context_ptr
,
190 unsigned texture_unit
);
192 /** Obtain the base texture depth (or array size) (returns int32) */
194 (*depth
)(const struct lp_sampler_dynamic_state
*state
,
195 struct gallivm_state
*gallivm
,
196 LLVMValueRef context_ptr
,
197 unsigned texture_unit
);
199 /** Obtain the first mipmap level (base level) (returns int32) */
201 (*first_level
)(const struct lp_sampler_dynamic_state
*state
,
202 struct gallivm_state
*gallivm
,
203 LLVMValueRef context_ptr
,
204 unsigned texture_unit
);
206 /** Obtain the number of mipmap levels minus one (returns int32) */
208 (*last_level
)(const struct lp_sampler_dynamic_state
*state
,
209 struct gallivm_state
*gallivm
,
210 LLVMValueRef context_ptr
,
211 unsigned texture_unit
);
213 /** Obtain stride in bytes between image rows/blocks (returns int32) */
215 (*row_stride
)(const struct lp_sampler_dynamic_state
*state
,
216 struct gallivm_state
*gallivm
,
217 LLVMValueRef context_ptr
,
218 unsigned texture_unit
);
220 /** Obtain stride in bytes between image slices (returns int32) */
222 (*img_stride
)(const struct lp_sampler_dynamic_state
*state
,
223 struct gallivm_state
*gallivm
,
224 LLVMValueRef context_ptr
,
225 unsigned texture_unit
);
227 /** Obtain pointer to base of texture */
229 (*base_ptr
)(const struct lp_sampler_dynamic_state
*state
,
230 struct gallivm_state
*gallivm
,
231 LLVMValueRef context_ptr
,
232 unsigned texture_unit
);
234 /** Obtain pointer to array of mipmap offsets */
236 (*mip_offsets
)(const struct lp_sampler_dynamic_state
*state
,
237 struct gallivm_state
*gallivm
,
238 LLVMValueRef context_ptr
,
239 unsigned texture_unit
);
241 /* These are callbacks for sampler state */
243 /** Obtain texture min lod (returns float) */
245 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
246 struct gallivm_state
*gallivm
,
247 LLVMValueRef context_ptr
,
248 unsigned sampler_unit
);
250 /** Obtain texture max lod (returns float) */
252 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
253 struct gallivm_state
*gallivm
,
254 LLVMValueRef context_ptr
,
255 unsigned sampler_unit
);
257 /** Obtain texture lod bias (returns float) */
259 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
260 struct gallivm_state
*gallivm
,
261 LLVMValueRef context_ptr
,
262 unsigned sampler_unit
);
264 /** Obtain texture border color (returns ptr to float[4]) */
266 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
267 struct gallivm_state
*gallivm
,
268 LLVMValueRef context_ptr
,
269 unsigned sampler_unit
);
274 * Keep all information for sampling code generation in a single place.
276 struct lp_build_sample_context
278 struct gallivm_state
*gallivm
;
280 const struct lp_static_texture_state
*static_texture_state
;
281 const struct lp_static_sampler_state
*static_sampler_state
;
283 struct lp_sampler_dynamic_state
*dynamic_state
;
285 const struct util_format_description
*format_desc
;
287 /* See texture_dims() */
290 /** SIMD vector width */
291 unsigned vector_width
;
293 /** number of mipmaps (valid are 1, length/4, length) */
296 /** number of lod values (valid are 1, length/4, length) */
299 /** regular scalar float type */
300 struct lp_type float_type
;
301 struct lp_build_context float_bld
;
303 /** float vector type */
304 struct lp_build_context float_vec_bld
;
306 /** regular scalar int type */
307 struct lp_type int_type
;
308 struct lp_build_context int_bld
;
310 /** Incoming coordinates type and build context */
311 struct lp_type coord_type
;
312 struct lp_build_context coord_bld
;
314 /** Signed integer coordinates */
315 struct lp_type int_coord_type
;
316 struct lp_build_context int_coord_bld
;
318 /** Unsigned integer texture size */
319 struct lp_type int_size_in_type
;
320 struct lp_build_context int_size_in_bld
;
322 /** Float incoming texture size */
323 struct lp_type float_size_in_type
;
324 struct lp_build_context float_size_in_bld
;
326 /** Unsigned integer texture size (might be per quad) */
327 struct lp_type int_size_type
;
328 struct lp_build_context int_size_bld
;
330 /** Float texture size (might be per quad) */
331 struct lp_type float_size_type
;
332 struct lp_build_context float_size_bld
;
334 /** Output texels type and build context */
335 struct lp_type texel_type
;
336 struct lp_build_context texel_bld
;
338 /** Float level type */
339 struct lp_type levelf_type
;
340 struct lp_build_context levelf_bld
;
342 /** Int level type */
343 struct lp_type leveli_type
;
344 struct lp_build_context leveli_bld
;
346 /** Float lod type */
347 struct lp_type lodf_type
;
348 struct lp_build_context lodf_bld
;
351 struct lp_type lodi_type
;
352 struct lp_build_context lodi_bld
;
354 /* Common dynamic state values */
355 LLVMValueRef row_stride_array
;
356 LLVMValueRef img_stride_array
;
357 LLVMValueRef base_ptr
;
358 LLVMValueRef mip_offsets
;
360 /** Integer vector with texture width, height, depth */
361 LLVMValueRef int_size
;
363 LLVMValueRef border_color_clamped
;
365 LLVMValueRef context_ptr
;
371 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
372 * this time. Return whether the given mode is supported by that function.
374 static INLINE boolean
375 lp_is_simple_wrap_mode(unsigned mode
)
378 case PIPE_TEX_WRAP_REPEAT
:
379 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
388 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
391 unsigned char swizzles
[4];
393 swizzles
[0] = bld
->static_texture_state
->swizzle_r
;
394 swizzles
[1] = bld
->static_texture_state
->swizzle_g
;
395 swizzles
[2] = bld
->static_texture_state
->swizzle_b
;
396 swizzles
[3] = bld
->static_texture_state
->swizzle_a
;
398 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
402 * not really dimension as such, this indicates the amount of
403 * "normal" texture coords subject to minification, wrapping etc.
405 static INLINE
unsigned
406 texture_dims(enum pipe_texture_target tex
)
409 case PIPE_TEXTURE_1D
:
410 case PIPE_TEXTURE_1D_ARRAY
:
413 case PIPE_TEXTURE_2D
:
414 case PIPE_TEXTURE_2D_ARRAY
:
415 case PIPE_TEXTURE_RECT
:
416 case PIPE_TEXTURE_CUBE
:
417 case PIPE_TEXTURE_CUBE_ARRAY
:
419 case PIPE_TEXTURE_3D
:
422 assert(0 && "bad texture target in texture_dims()");
427 static INLINE boolean
428 has_layer_coord(enum pipe_texture_target tex
)
431 case PIPE_TEXTURE_1D_ARRAY
:
432 case PIPE_TEXTURE_2D_ARRAY
:
433 /* cube is not layered but 3rd coord (after cube mapping) behaves the same */
434 case PIPE_TEXTURE_CUBE
:
435 case PIPE_TEXTURE_CUBE_ARRAY
:
444 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
445 unsigned min_img_filter
,
446 unsigned mag_img_filter
);
449 * Derive the sampler static state.
452 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
453 const struct pipe_sampler_state
*sampler
);
457 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
458 const struct pipe_sampler_view
*view
);
462 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
463 unsigned texture_index
,
464 unsigned sampler_index
,
468 LLVMValueRef cube_rho
,
469 const struct lp_derivatives
*derivs
,
470 LLVMValueRef lod_bias
, /* optional */
471 LLVMValueRef explicit_lod
, /* optional */
473 LLVMValueRef
*out_lod_ipart
,
474 LLVMValueRef
*out_lod_fpart
,
475 LLVMValueRef
*out_lod_positive
);
478 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
479 unsigned texture_unit
,
481 LLVMValueRef
*level_out
,
482 LLVMValueRef
*out_of_bounds
);
485 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
486 unsigned texture_unit
,
487 LLVMValueRef lod_ipart
,
488 LLVMValueRef
*lod_fpart_inout
,
489 LLVMValueRef
*level0_out
,
490 LLVMValueRef
*level1_out
);
493 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
498 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
503 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
505 LLVMValueRef
*out_size_vec
,
506 LLVMValueRef
*row_stride_vec
,
507 LLVMValueRef
*img_stride_vec
);
511 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
512 struct lp_build_context
*size_bld
,
513 struct lp_type coord_type
,
515 LLVMValueRef
*out_width
,
516 LLVMValueRef
*out_height
,
517 LLVMValueRef
*out_depth
);
521 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
522 LLVMValueRef flt_size
,
529 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
530 LLVMValueRef
*coords
,
531 const struct lp_derivatives
*derivs_in
, /* optional */
533 struct lp_derivatives
*derivs_out
, /* optional */
534 boolean need_derivs
);
538 lp_build_cube_new_coords(struct lp_build_context
*ivec_bld
,
544 LLVMValueRef max_coord
,
545 LLVMValueRef new_faces
[4],
546 LLVMValueRef new_xcoords
[4][2],
547 LLVMValueRef new_ycoords
[4][2]);
551 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
552 unsigned block_length
,
555 LLVMValueRef
*out_offset
,
556 LLVMValueRef
*out_i
);
560 lp_build_sample_offset(struct lp_build_context
*bld
,
561 const struct util_format_description
*format_desc
,
565 LLVMValueRef y_stride
,
566 LLVMValueRef z_stride
,
567 LLVMValueRef
*out_offset
,
569 LLVMValueRef
*out_j
);
573 lp_build_sample_soa(const struct lp_static_texture_state
*static_texture_state
,
574 const struct lp_static_sampler_state
*static_sampler_state
,
575 struct lp_sampler_dynamic_state
*dynamic_texture_state
,
576 struct gallivm_state
*gallivm
,
577 const struct lp_sampler_params
*params
);
581 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
582 LLVMValueRef coord_f
,
583 LLVMValueRef length_i
,
584 LLVMValueRef length_f
,
585 LLVMValueRef
*coord0_i
,
586 LLVMValueRef
*weight_f
);
590 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
591 const struct lp_static_texture_state
*static_state
,
592 struct lp_sampler_dynamic_state
*dynamic_state
,
593 struct lp_type int_type
,
594 unsigned texture_unit
,
596 LLVMValueRef context_ptr
,
597 boolean is_sviewinfo
,
598 enum lp_sampler_lod_property lod_property
,
599 LLVMValueRef explicit_lod
,
600 LLVMValueRef
*sizes_out
);
603 lp_build_sample_nop(struct gallivm_state
*gallivm
,
605 const LLVMValueRef
*coords
,
606 LLVMValueRef texel_out
[4]);
610 lp_build_minify(struct lp_build_context
*bld
,
611 LLVMValueRef base_size
,
616 #endif /* LP_BLD_SAMPLE_H */