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
59 LLVMValueRef ddx_ddy
[2];
64 * Sampler static state.
66 * These are the bits of state from pipe_resource and pipe_sampler_state that
67 * are embedded in the generated code.
69 struct lp_sampler_static_state
71 /* pipe_sampler_view's state */
72 enum pipe_format format
;
73 unsigned swizzle_r
:3; /**< PIPE_SWIZZLE_* */
78 /* pipe_texture's state */
79 unsigned target
:3; /**< PIPE_TEXTURE_* */
80 unsigned pot_width
:1; /**< is the width a power of two? */
81 unsigned pot_height
:1;
84 /* pipe_sampler_state's state */
88 unsigned min_img_filter
:2;
89 unsigned min_mip_filter
:2;
90 unsigned mag_img_filter
:2;
91 unsigned compare_mode
:1;
92 unsigned compare_func
:3;
93 unsigned normalized_coords
:1;
94 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
95 unsigned lod_bias_non_zero
:1;
96 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
97 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
100 unsigned force_nearest_s
:1;
101 unsigned force_nearest_t
:1;
106 * Sampler dynamic state.
108 * These are the bits of state from pipe_resource and pipe_sampler_state that
109 * are computed in runtime.
111 * There are obtained through callbacks, as we don't want to tie the texture
112 * sampling code generation logic to any particular texture layout or pipe
115 struct lp_sampler_dynamic_state
118 /** Obtain the base texture width (returns int32) */
120 (*width
)( const struct lp_sampler_dynamic_state
*state
,
121 struct gallivm_state
*gallivm
,
124 /** Obtain the base texture height (returns int32) */
126 (*height
)( const struct lp_sampler_dynamic_state
*state
,
127 struct gallivm_state
*gallivm
,
130 /** Obtain the base texture depth (returns int32) */
132 (*depth
)( const struct lp_sampler_dynamic_state
*state
,
133 struct gallivm_state
*gallivm
,
136 /** Obtain the first mipmap level (base level) (returns int32) */
138 (*first_level
)( const struct lp_sampler_dynamic_state
*state
,
139 struct gallivm_state
*gallivm
,
142 /** Obtain the number of mipmap levels minus one (returns int32) */
144 (*last_level
)( const struct lp_sampler_dynamic_state
*state
,
145 struct gallivm_state
*gallivm
,
148 /** Obtain stride in bytes between image rows/blocks (returns int32) */
150 (*row_stride
)( const struct lp_sampler_dynamic_state
*state
,
151 struct gallivm_state
*gallivm
,
154 /** Obtain stride in bytes between image slices (returns int32) */
156 (*img_stride
)( const struct lp_sampler_dynamic_state
*state
,
157 struct gallivm_state
*gallivm
,
160 /** Obtain pointer to base of texture */
162 (*base_ptr
)( const struct lp_sampler_dynamic_state
*state
,
163 struct gallivm_state
*gallivm
,
166 /** Obtain pointer to array of mipmap offsets */
168 (*mip_offsets
)( const struct lp_sampler_dynamic_state
*state
,
169 struct gallivm_state
*gallivm
,
172 /** Obtain texture min lod (returns float) */
174 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
175 struct gallivm_state
*gallivm
, unsigned unit
);
177 /** Obtain texture max lod (returns float) */
179 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
180 struct gallivm_state
*gallivm
, unsigned unit
);
182 /** Obtain texture lod bias (returns float) */
184 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
185 struct gallivm_state
*gallivm
, unsigned unit
);
187 /** Obtain texture border color (returns ptr to float[4]) */
189 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
190 struct gallivm_state
*gallivm
, unsigned unit
);
195 * Keep all information for sampling code generation in a single place.
197 struct lp_build_sample_context
199 struct gallivm_state
*gallivm
;
201 const struct lp_sampler_static_state
*static_state
;
203 struct lp_sampler_dynamic_state
*dynamic_state
;
205 const struct util_format_description
*format_desc
;
207 /* See texture_dims() */
210 /** SIMD vector width */
211 unsigned vector_width
;
213 /** number of lod values (valid are 1, length/4, length) */
216 /** regular scalar float type */
217 struct lp_type float_type
;
218 struct lp_build_context float_bld
;
220 /** float vector type */
221 struct lp_build_context float_vec_bld
;
223 /** regular scalar int type */
224 struct lp_type int_type
;
225 struct lp_build_context int_bld
;
227 /** Incoming coordinates type and build context */
228 struct lp_type coord_type
;
229 struct lp_build_context coord_bld
;
231 /** Signed integer coordinates */
232 struct lp_type int_coord_type
;
233 struct lp_build_context int_coord_bld
;
235 /** Unsigned integer texture size */
236 struct lp_type int_size_in_type
;
237 struct lp_build_context int_size_in_bld
;
239 /** Float incoming texture size */
240 struct lp_type float_size_in_type
;
241 struct lp_build_context float_size_in_bld
;
243 /** Unsigned integer texture size (might be per quad) */
244 struct lp_type int_size_type
;
245 struct lp_build_context int_size_bld
;
247 /** Float texture size (might be per quad) */
248 struct lp_type float_size_type
;
249 struct lp_build_context float_size_bld
;
251 /** Output texels type and build context */
252 struct lp_type texel_type
;
253 struct lp_build_context texel_bld
;
255 /** Float per-quad type */
256 struct lp_type perquadf_type
;
257 struct lp_build_context perquadf_bld
;
259 /** Int per-quad type */
260 struct lp_type perquadi_type
;
261 struct lp_build_context perquadi_bld
;
263 /* Common dynamic state values */
264 LLVMValueRef row_stride_array
;
265 LLVMValueRef img_stride_array
;
266 LLVMValueRef base_ptr
;
267 LLVMValueRef mip_offsets
;
269 /** Integer vector with texture width, height, depth */
270 LLVMValueRef int_size
;
276 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
277 * this time. Return whether the given mode is supported by that function.
279 static INLINE boolean
280 lp_is_simple_wrap_mode(unsigned mode
)
283 case PIPE_TEX_WRAP_REPEAT
:
284 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
293 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
296 unsigned char swizzles
[4];
298 swizzles
[0] = bld
->static_state
->swizzle_r
;
299 swizzles
[1] = bld
->static_state
->swizzle_g
;
300 swizzles
[2] = bld
->static_state
->swizzle_b
;
301 swizzles
[3] = bld
->static_state
->swizzle_a
;
303 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
307 * not really dimension as such, this indicates the amount of
308 * "normal" texture coords subject to minification, wrapping etc.
310 static INLINE
unsigned
311 texture_dims(enum pipe_texture_target tex
)
314 case PIPE_TEXTURE_1D
:
315 case PIPE_TEXTURE_1D_ARRAY
:
318 case PIPE_TEXTURE_2D
:
319 case PIPE_TEXTURE_2D_ARRAY
:
320 case PIPE_TEXTURE_RECT
:
321 case PIPE_TEXTURE_CUBE
:
323 case PIPE_TEXTURE_3D
:
326 assert(0 && "bad texture target in texture_dims()");
333 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
334 unsigned min_img_filter
,
335 unsigned mag_img_filter
);
338 * Derive the sampler static state.
341 lp_sampler_static_state(struct lp_sampler_static_state
*state
,
342 const struct pipe_sampler_view
*view
,
343 const struct pipe_sampler_state
*sampler
);
347 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
349 const struct lp_derivatives
*derivs
,
350 LLVMValueRef lod_bias
, /* optional */
351 LLVMValueRef explicit_lod
, /* optional */
353 LLVMValueRef
*out_lod_ipart
,
354 LLVMValueRef
*out_lod_fpart
);
357 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
360 LLVMValueRef
*level_out
);
363 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
365 LLVMValueRef lod_ipart
,
366 LLVMValueRef
*lod_fpart_inout
,
367 LLVMValueRef
*level0_out
,
368 LLVMValueRef
*level1_out
);
371 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
376 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
381 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
383 LLVMValueRef
*out_size_vec
,
384 LLVMValueRef
*row_stride_vec
,
385 LLVMValueRef
*img_stride_vec
);
389 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
390 struct lp_build_context
*size_bld
,
391 struct lp_type coord_type
,
393 LLVMValueRef
*out_width
,
394 LLVMValueRef
*out_height
,
395 LLVMValueRef
*out_depth
);
399 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
400 LLVMValueRef flt_size
,
407 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
412 LLVMValueRef
*face_s
,
413 LLVMValueRef
*face_t
);
417 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
418 unsigned block_length
,
421 LLVMValueRef
*out_offset
,
422 LLVMValueRef
*out_i
);
426 lp_build_sample_offset(struct lp_build_context
*bld
,
427 const struct util_format_description
*format_desc
,
431 LLVMValueRef y_stride
,
432 LLVMValueRef z_stride
,
433 LLVMValueRef
*out_offset
,
435 LLVMValueRef
*out_j
);
439 lp_build_sample_soa(struct gallivm_state
*gallivm
,
440 const struct lp_sampler_static_state
*static_state
,
441 struct lp_sampler_dynamic_state
*dynamic_state
,
442 struct lp_type fp_type
,
445 const LLVMValueRef
*coords
,
446 const LLVMValueRef
*offsets
,
447 const struct lp_derivatives
*derivs
,
448 LLVMValueRef lod_bias
,
449 LLVMValueRef explicit_lod
,
450 LLVMValueRef texel_out
[4]);
454 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
455 LLVMValueRef coord_f
,
456 LLVMValueRef length_i
,
457 LLVMValueRef length_f
,
458 LLVMValueRef
*coord0_i
,
459 LLVMValueRef
*weight_f
);
463 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
464 const struct lp_sampler_static_state
*static_state
,
465 struct lp_sampler_dynamic_state
*dynamic_state
,
466 struct lp_type int_type
,
468 LLVMValueRef explicit_lod
,
469 LLVMValueRef
*sizes_out
);
472 lp_build_sample_nop(struct gallivm_state
*gallivm
,
474 const LLVMValueRef
*coords
,
475 LLVMValueRef texel_out
[4]);
479 lp_build_minify(struct lp_build_context
*bld
,
480 LLVMValueRef base_size
,
484 #endif /* LP_BLD_SAMPLE_H */