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 * Texture static state.
66 * These are the bits of state from pipe_resource/pipe_sampler_view that
67 * are embedded in the generated code.
69 struct lp_static_texture_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
:4; /**< PIPE_TEXTURE_* */
80 unsigned pot_width
:1; /**< is the width a power of two? */
81 unsigned pot_height
:1;
83 unsigned level_zero_only
:1;
88 * Sampler static state.
90 * These are the bits of state from pipe_sampler_state that
91 * are embedded in the generated code.
93 struct lp_static_sampler_state
95 /* pipe_sampler_state's state */
99 unsigned min_img_filter
:2;
100 unsigned min_mip_filter
:2;
101 unsigned mag_img_filter
:2;
102 unsigned compare_mode
:1;
103 unsigned compare_func
:3;
104 unsigned normalized_coords
:1;
105 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
106 unsigned lod_bias_non_zero
:1;
107 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
108 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
111 unsigned force_nearest_s
:1;
112 unsigned force_nearest_t
:1;
117 * Sampler dynamic state.
119 * These are the bits of state from pipe_resource/pipe_sampler_view
120 * as well as from sampler state that are computed at runtime.
122 * There are obtained through callbacks, as we don't want to tie the texture
123 * sampling code generation logic to any particular texture layout or pipe
126 struct lp_sampler_dynamic_state
128 /* First callbacks for sampler view state */
130 /** Obtain the base texture width (returns int32) */
132 (*width
)( const struct lp_sampler_dynamic_state
*state
,
133 struct gallivm_state
*gallivm
,
134 unsigned texture_unit
);
136 /** Obtain the base texture height (returns int32) */
138 (*height
)( const struct lp_sampler_dynamic_state
*state
,
139 struct gallivm_state
*gallivm
,
140 unsigned texture_unit
);
142 /** Obtain the base texture depth (returns int32) */
144 (*depth
)( const struct lp_sampler_dynamic_state
*state
,
145 struct gallivm_state
*gallivm
,
146 unsigned texture_unit
);
148 /** Obtain the first mipmap level (base level) (returns int32) */
150 (*first_level
)( const struct lp_sampler_dynamic_state
*state
,
151 struct gallivm_state
*gallivm
,
152 unsigned texture_unit
);
154 /** Obtain the number of mipmap levels minus one (returns int32) */
156 (*last_level
)( const struct lp_sampler_dynamic_state
*state
,
157 struct gallivm_state
*gallivm
,
158 unsigned texture_unit
);
160 /** Obtain stride in bytes between image rows/blocks (returns int32) */
162 (*row_stride
)( const struct lp_sampler_dynamic_state
*state
,
163 struct gallivm_state
*gallivm
,
164 unsigned texture_unit
);
166 /** Obtain stride in bytes between image slices (returns int32) */
168 (*img_stride
)( const struct lp_sampler_dynamic_state
*state
,
169 struct gallivm_state
*gallivm
,
170 unsigned texture_unit
);
172 /** Obtain pointer to base of texture */
174 (*base_ptr
)( const struct lp_sampler_dynamic_state
*state
,
175 struct gallivm_state
*gallivm
,
176 unsigned texture_unit
);
178 /** Obtain pointer to array of mipmap offsets */
180 (*mip_offsets
)( const struct lp_sampler_dynamic_state
*state
,
181 struct gallivm_state
*gallivm
,
182 unsigned texture_unit
);
184 /* These are callbacks for sampler state */
186 /** Obtain texture min lod (returns float) */
188 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
189 struct gallivm_state
*gallivm
, unsigned sampler_unit
);
191 /** Obtain texture max lod (returns float) */
193 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
194 struct gallivm_state
*gallivm
, unsigned sampler_unit
);
196 /** Obtain texture lod bias (returns float) */
198 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
199 struct gallivm_state
*gallivm
, unsigned sampler_unit
);
201 /** Obtain texture border color (returns ptr to float[4]) */
203 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
204 struct gallivm_state
*gallivm
, unsigned sampler_unit
);
209 * Keep all information for sampling code generation in a single place.
211 struct lp_build_sample_context
213 struct gallivm_state
*gallivm
;
215 const struct lp_static_texture_state
*static_texture_state
;
216 const struct lp_static_sampler_state
*static_sampler_state
;
218 struct lp_sampler_dynamic_state
*dynamic_state
;
220 const struct util_format_description
*format_desc
;
222 /* See texture_dims() */
225 /** SIMD vector width */
226 unsigned vector_width
;
228 /** number of lod values (valid are 1, length/4, length) */
231 /** regular scalar float type */
232 struct lp_type float_type
;
233 struct lp_build_context float_bld
;
235 /** float vector type */
236 struct lp_build_context float_vec_bld
;
238 /** regular scalar int type */
239 struct lp_type int_type
;
240 struct lp_build_context int_bld
;
242 /** Incoming coordinates type and build context */
243 struct lp_type coord_type
;
244 struct lp_build_context coord_bld
;
246 /** Signed integer coordinates */
247 struct lp_type int_coord_type
;
248 struct lp_build_context int_coord_bld
;
250 /** Unsigned integer texture size */
251 struct lp_type int_size_in_type
;
252 struct lp_build_context int_size_in_bld
;
254 /** Float incoming texture size */
255 struct lp_type float_size_in_type
;
256 struct lp_build_context float_size_in_bld
;
258 /** Unsigned integer texture size (might be per quad) */
259 struct lp_type int_size_type
;
260 struct lp_build_context int_size_bld
;
262 /** Float texture size (might be per quad) */
263 struct lp_type float_size_type
;
264 struct lp_build_context float_size_bld
;
266 /** Output texels type and build context */
267 struct lp_type texel_type
;
268 struct lp_build_context texel_bld
;
270 /** Float per-quad type */
271 struct lp_type perquadf_type
;
272 struct lp_build_context perquadf_bld
;
274 /** Int per-quad type */
275 struct lp_type perquadi_type
;
276 struct lp_build_context perquadi_bld
;
278 /* Common dynamic state values */
279 LLVMValueRef row_stride_array
;
280 LLVMValueRef img_stride_array
;
281 LLVMValueRef base_ptr
;
282 LLVMValueRef mip_offsets
;
284 /** Integer vector with texture width, height, depth */
285 LLVMValueRef int_size
;
291 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
292 * this time. Return whether the given mode is supported by that function.
294 static INLINE boolean
295 lp_is_simple_wrap_mode(unsigned mode
)
298 case PIPE_TEX_WRAP_REPEAT
:
299 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
308 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
311 unsigned char swizzles
[4];
313 swizzles
[0] = bld
->static_texture_state
->swizzle_r
;
314 swizzles
[1] = bld
->static_texture_state
->swizzle_g
;
315 swizzles
[2] = bld
->static_texture_state
->swizzle_b
;
316 swizzles
[3] = bld
->static_texture_state
->swizzle_a
;
318 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
322 * not really dimension as such, this indicates the amount of
323 * "normal" texture coords subject to minification, wrapping etc.
325 static INLINE
unsigned
326 texture_dims(enum pipe_texture_target tex
)
329 case PIPE_TEXTURE_1D
:
330 case PIPE_TEXTURE_1D_ARRAY
:
333 case PIPE_TEXTURE_2D
:
334 case PIPE_TEXTURE_2D_ARRAY
:
335 case PIPE_TEXTURE_RECT
:
336 case PIPE_TEXTURE_CUBE
:
338 case PIPE_TEXTURE_3D
:
341 assert(0 && "bad texture target in texture_dims()");
348 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
349 unsigned min_img_filter
,
350 unsigned mag_img_filter
);
353 * Derive the sampler static state.
356 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
357 const struct pipe_sampler_state
*sampler
);
361 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
362 const struct pipe_sampler_view
*view
);
366 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
367 unsigned texture_index
,
368 unsigned sampler_index
,
369 const struct lp_derivatives
*derivs
,
370 LLVMValueRef lod_bias
, /* optional */
371 LLVMValueRef explicit_lod
, /* optional */
373 LLVMValueRef
*out_lod_ipart
,
374 LLVMValueRef
*out_lod_fpart
);
377 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
378 unsigned texture_unit
,
380 LLVMValueRef
*level_out
);
383 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
384 unsigned texture_unit
,
385 LLVMValueRef lod_ipart
,
386 LLVMValueRef
*lod_fpart_inout
,
387 LLVMValueRef
*level0_out
,
388 LLVMValueRef
*level1_out
);
391 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
396 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
401 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
403 LLVMValueRef
*out_size_vec
,
404 LLVMValueRef
*row_stride_vec
,
405 LLVMValueRef
*img_stride_vec
);
409 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
410 struct lp_build_context
*size_bld
,
411 struct lp_type coord_type
,
413 LLVMValueRef
*out_width
,
414 LLVMValueRef
*out_height
,
415 LLVMValueRef
*out_depth
);
419 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
420 LLVMValueRef flt_size
,
427 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
432 LLVMValueRef
*face_s
,
433 LLVMValueRef
*face_t
);
437 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
438 unsigned block_length
,
441 LLVMValueRef
*out_offset
,
442 LLVMValueRef
*out_i
);
446 lp_build_sample_offset(struct lp_build_context
*bld
,
447 const struct util_format_description
*format_desc
,
451 LLVMValueRef y_stride
,
452 LLVMValueRef z_stride
,
453 LLVMValueRef
*out_offset
,
455 LLVMValueRef
*out_j
);
459 lp_build_sample_soa(struct gallivm_state
*gallivm
,
460 const struct lp_static_texture_state
*static_texture_state
,
461 const struct lp_static_sampler_state
*static_sampler_state
,
462 struct lp_sampler_dynamic_state
*dynamic_texture_state
,
463 struct lp_type fp_type
,
465 unsigned texture_index
,
466 unsigned sampler_index
,
467 const LLVMValueRef
*coords
,
468 const LLVMValueRef
*offsets
,
469 const struct lp_derivatives
*derivs
,
470 LLVMValueRef lod_bias
,
471 LLVMValueRef explicit_lod
,
472 LLVMValueRef texel_out
[4]);
476 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
477 LLVMValueRef coord_f
,
478 LLVMValueRef length_i
,
479 LLVMValueRef length_f
,
480 LLVMValueRef
*coord0_i
,
481 LLVMValueRef
*weight_f
);
485 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
486 const struct lp_static_texture_state
*static_state
,
487 struct lp_sampler_dynamic_state
*dynamic_state
,
488 struct lp_type int_type
,
489 unsigned texture_unit
,
490 boolean need_nr_mips
,
491 LLVMValueRef explicit_lod
,
492 LLVMValueRef
*sizes_out
);
495 lp_build_sample_nop(struct gallivm_state
*gallivm
,
497 const LLVMValueRef
*coords
,
498 LLVMValueRef texel_out
[4]);
502 lp_build_minify(struct lp_build_context
*bld
,
503 LLVMValueRef base_size
,
507 #endif /* LP_BLD_SAMPLE_H */