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
65 * Texture static state.
67 * These are the bits of state from pipe_resource/pipe_sampler_view that
68 * are embedded in the generated code.
70 struct lp_static_texture_state
72 /* pipe_sampler_view's state */
73 enum pipe_format format
;
74 unsigned swizzle_r
:3; /**< PIPE_SWIZZLE_* */
79 /* pipe_texture's state */
80 unsigned target
:4; /**< PIPE_TEXTURE_* */
81 unsigned pot_width
:1; /**< is the width a power of two? */
82 unsigned pot_height
:1;
84 unsigned level_zero_only
:1;
89 * Sampler static state.
91 * These are the bits of state from pipe_sampler_state that
92 * are embedded in the generated code.
94 struct lp_static_sampler_state
96 /* pipe_sampler_state's state */
100 unsigned min_img_filter
:2;
101 unsigned min_mip_filter
:2;
102 unsigned mag_img_filter
:2;
103 unsigned compare_mode
:1;
104 unsigned compare_func
:3;
105 unsigned normalized_coords
:1;
106 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
107 unsigned lod_bias_non_zero
:1;
108 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
109 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
112 unsigned force_nearest_s
:1;
113 unsigned force_nearest_t
:1;
118 * Sampler dynamic state.
120 * These are the bits of state from pipe_resource/pipe_sampler_view
121 * as well as from sampler state that are computed at runtime.
123 * There are obtained through callbacks, as we don't want to tie the texture
124 * sampling code generation logic to any particular texture layout or pipe
127 struct lp_sampler_dynamic_state
129 /* First callbacks for sampler view state */
131 /** Obtain the base texture width (or number of elements) (returns int32) */
133 (*width
)( const struct lp_sampler_dynamic_state
*state
,
134 struct gallivm_state
*gallivm
,
135 unsigned texture_unit
);
137 /** Obtain the base texture height (returns int32) */
139 (*height
)( const struct lp_sampler_dynamic_state
*state
,
140 struct gallivm_state
*gallivm
,
141 unsigned texture_unit
);
143 /** Obtain the base texture depth (or array size) (returns int32) */
145 (*depth
)( const struct lp_sampler_dynamic_state
*state
,
146 struct gallivm_state
*gallivm
,
147 unsigned texture_unit
);
149 /** Obtain the first mipmap level (base level) (returns int32) */
151 (*first_level
)( const struct lp_sampler_dynamic_state
*state
,
152 struct gallivm_state
*gallivm
,
153 unsigned texture_unit
);
155 /** Obtain the number of mipmap levels minus one (returns int32) */
157 (*last_level
)( const struct lp_sampler_dynamic_state
*state
,
158 struct gallivm_state
*gallivm
,
159 unsigned texture_unit
);
161 /** Obtain stride in bytes between image rows/blocks (returns int32) */
163 (*row_stride
)( const struct lp_sampler_dynamic_state
*state
,
164 struct gallivm_state
*gallivm
,
165 unsigned texture_unit
);
167 /** Obtain stride in bytes between image slices (returns int32) */
169 (*img_stride
)( const struct lp_sampler_dynamic_state
*state
,
170 struct gallivm_state
*gallivm
,
171 unsigned texture_unit
);
173 /** Obtain pointer to base of texture */
175 (*base_ptr
)( const struct lp_sampler_dynamic_state
*state
,
176 struct gallivm_state
*gallivm
,
177 unsigned texture_unit
);
179 /** Obtain pointer to array of mipmap offsets */
181 (*mip_offsets
)( const struct lp_sampler_dynamic_state
*state
,
182 struct gallivm_state
*gallivm
,
183 unsigned texture_unit
);
185 /* These are callbacks for sampler state */
187 /** Obtain texture min lod (returns float) */
189 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
190 struct gallivm_state
*gallivm
, unsigned sampler_unit
);
192 /** Obtain texture max lod (returns float) */
194 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
195 struct gallivm_state
*gallivm
, unsigned sampler_unit
);
197 /** Obtain texture lod bias (returns float) */
199 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
200 struct gallivm_state
*gallivm
, unsigned sampler_unit
);
202 /** Obtain texture border color (returns ptr to float[4]) */
204 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
205 struct gallivm_state
*gallivm
, unsigned sampler_unit
);
210 * Keep all information for sampling code generation in a single place.
212 struct lp_build_sample_context
214 struct gallivm_state
*gallivm
;
216 const struct lp_static_texture_state
*static_texture_state
;
217 const struct lp_static_sampler_state
*static_sampler_state
;
219 struct lp_sampler_dynamic_state
*dynamic_state
;
221 const struct util_format_description
*format_desc
;
223 /* See texture_dims() */
226 /** SIMD vector width */
227 unsigned vector_width
;
229 /** number of lod values (valid are 1, length/4, length) */
232 /** regular scalar float type */
233 struct lp_type float_type
;
234 struct lp_build_context float_bld
;
236 /** float vector type */
237 struct lp_build_context float_vec_bld
;
239 /** regular scalar int type */
240 struct lp_type int_type
;
241 struct lp_build_context int_bld
;
243 /** Incoming coordinates type and build context */
244 struct lp_type coord_type
;
245 struct lp_build_context coord_bld
;
247 /** Signed integer coordinates */
248 struct lp_type int_coord_type
;
249 struct lp_build_context int_coord_bld
;
251 /** Unsigned integer texture size */
252 struct lp_type int_size_in_type
;
253 struct lp_build_context int_size_in_bld
;
255 /** Float incoming texture size */
256 struct lp_type float_size_in_type
;
257 struct lp_build_context float_size_in_bld
;
259 /** Unsigned integer texture size (might be per quad) */
260 struct lp_type int_size_type
;
261 struct lp_build_context int_size_bld
;
263 /** Float texture size (might be per quad) */
264 struct lp_type float_size_type
;
265 struct lp_build_context float_size_bld
;
267 /** Output texels type and build context */
268 struct lp_type texel_type
;
269 struct lp_build_context texel_bld
;
271 /** Float level type */
272 struct lp_type levelf_type
;
273 struct lp_build_context levelf_bld
;
275 /** Int level type */
276 struct lp_type leveli_type
;
277 struct lp_build_context leveli_bld
;
279 /* Common dynamic state values */
280 LLVMValueRef row_stride_array
;
281 LLVMValueRef img_stride_array
;
282 LLVMValueRef base_ptr
;
283 LLVMValueRef mip_offsets
;
285 /** Integer vector with texture width, height, depth */
286 LLVMValueRef int_size
;
292 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
293 * this time. Return whether the given mode is supported by that function.
295 static INLINE boolean
296 lp_is_simple_wrap_mode(unsigned mode
)
299 case PIPE_TEX_WRAP_REPEAT
:
300 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
309 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
312 unsigned char swizzles
[4];
314 swizzles
[0] = bld
->static_texture_state
->swizzle_r
;
315 swizzles
[1] = bld
->static_texture_state
->swizzle_g
;
316 swizzles
[2] = bld
->static_texture_state
->swizzle_b
;
317 swizzles
[3] = bld
->static_texture_state
->swizzle_a
;
319 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
323 * not really dimension as such, this indicates the amount of
324 * "normal" texture coords subject to minification, wrapping etc.
326 static INLINE
unsigned
327 texture_dims(enum pipe_texture_target tex
)
330 case PIPE_TEXTURE_1D
:
331 case PIPE_TEXTURE_1D_ARRAY
:
334 case PIPE_TEXTURE_2D
:
335 case PIPE_TEXTURE_2D_ARRAY
:
336 case PIPE_TEXTURE_RECT
:
337 case PIPE_TEXTURE_CUBE
:
339 case PIPE_TEXTURE_3D
:
342 assert(0 && "bad texture target in texture_dims()");
349 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
350 unsigned min_img_filter
,
351 unsigned mag_img_filter
);
354 * Derive the sampler static state.
357 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
358 const struct pipe_sampler_state
*sampler
);
362 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
363 const struct pipe_sampler_view
*view
);
367 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
368 unsigned texture_index
,
369 unsigned sampler_index
,
373 LLVMValueRef cube_rho
,
374 const struct lp_derivatives
*derivs
,
375 LLVMValueRef lod_bias
, /* optional */
376 LLVMValueRef explicit_lod
, /* optional */
378 LLVMValueRef
*out_lod_ipart
,
379 LLVMValueRef
*out_lod_fpart
);
382 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
383 unsigned texture_unit
,
385 LLVMValueRef
*level_out
,
386 LLVMValueRef
*out_of_bounds
);
389 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
390 unsigned texture_unit
,
391 LLVMValueRef lod_ipart
,
392 LLVMValueRef
*lod_fpart_inout
,
393 LLVMValueRef
*level0_out
,
394 LLVMValueRef
*level1_out
);
397 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
402 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
407 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
409 LLVMValueRef
*out_size_vec
,
410 LLVMValueRef
*row_stride_vec
,
411 LLVMValueRef
*img_stride_vec
);
415 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
416 struct lp_build_context
*size_bld
,
417 struct lp_type coord_type
,
419 LLVMValueRef
*out_width
,
420 LLVMValueRef
*out_height
,
421 LLVMValueRef
*out_depth
);
425 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
426 LLVMValueRef flt_size
,
433 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
437 const struct lp_derivatives
*derivs
, /* optional */
439 LLVMValueRef
*face_s
,
440 LLVMValueRef
*face_t
,
442 boolean need_derivs
);
446 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
447 unsigned block_length
,
450 LLVMValueRef
*out_offset
,
451 LLVMValueRef
*out_i
);
455 lp_build_sample_offset(struct lp_build_context
*bld
,
456 const struct util_format_description
*format_desc
,
460 LLVMValueRef y_stride
,
461 LLVMValueRef z_stride
,
462 LLVMValueRef
*out_offset
,
464 LLVMValueRef
*out_j
);
468 lp_build_sample_soa(struct gallivm_state
*gallivm
,
469 const struct lp_static_texture_state
*static_texture_state
,
470 const struct lp_static_sampler_state
*static_sampler_state
,
471 struct lp_sampler_dynamic_state
*dynamic_texture_state
,
472 struct lp_type fp_type
,
474 unsigned texture_index
,
475 unsigned sampler_index
,
476 const LLVMValueRef
*coords
,
477 const LLVMValueRef
*offsets
,
478 const struct lp_derivatives
*derivs
,
479 LLVMValueRef lod_bias
,
480 LLVMValueRef explicit_lod
,
482 LLVMValueRef texel_out
[4]);
486 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
487 LLVMValueRef coord_f
,
488 LLVMValueRef length_i
,
489 LLVMValueRef length_f
,
490 LLVMValueRef
*coord0_i
,
491 LLVMValueRef
*weight_f
);
495 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
496 const struct lp_static_texture_state
*static_state
,
497 struct lp_sampler_dynamic_state
*dynamic_state
,
498 struct lp_type int_type
,
499 unsigned texture_unit
,
501 boolean is_sviewinfo
,
503 LLVMValueRef explicit_lod
,
504 LLVMValueRef
*sizes_out
);
507 lp_build_sample_nop(struct gallivm_state
*gallivm
,
509 const LLVMValueRef
*coords
,
510 LLVMValueRef texel_out
[4]);
514 lp_build_minify(struct lp_build_context
*bld
,
515 LLVMValueRef base_size
,
519 #endif /* LP_BLD_SAMPLE_H */