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 * Sampler static state.
57 * These are the bits of state from pipe_resource and pipe_sampler_state that
58 * are embedded in the generated code.
60 struct lp_sampler_static_state
62 /* pipe_sampler_view's state */
63 enum pipe_format format
;
64 unsigned swizzle_r
:3; /**< PIPE_SWIZZLE_* */
69 /* pipe_texture's state */
70 unsigned target
:3; /**< PIPE_TEXTURE_* */
71 unsigned pot_width
:1; /**< is the width a power of two? */
72 unsigned pot_height
:1;
75 /* pipe_sampler_state's state */
79 unsigned min_img_filter
:2;
80 unsigned min_mip_filter
:2;
81 unsigned mag_img_filter
:2;
82 unsigned compare_mode
:1;
83 unsigned compare_func
:3;
84 unsigned normalized_coords
:1;
85 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
86 unsigned lod_bias_non_zero
:1;
87 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
88 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
91 unsigned force_nearest_s
:1;
92 unsigned force_nearest_t
:1;
97 * Sampler dynamic state.
99 * These are the bits of state from pipe_resource and pipe_sampler_state that
100 * are computed in runtime.
102 * There are obtained through callbacks, as we don't want to tie the texture
103 * sampling code generation logic to any particular texture layout or pipe
106 struct lp_sampler_dynamic_state
109 /** Obtain the base texture width (returns int32) */
111 (*width
)( const struct lp_sampler_dynamic_state
*state
,
112 struct gallivm_state
*gallivm
,
115 /** Obtain the base texture height (returns int32) */
117 (*height
)( const struct lp_sampler_dynamic_state
*state
,
118 struct gallivm_state
*gallivm
,
121 /** Obtain the base texture depth (returns int32) */
123 (*depth
)( const struct lp_sampler_dynamic_state
*state
,
124 struct gallivm_state
*gallivm
,
127 /** Obtain the first mipmap level (base level) (returns int32) */
129 (*first_level
)( const struct lp_sampler_dynamic_state
*state
,
130 struct gallivm_state
*gallivm
,
133 /** Obtain the number of mipmap levels minus one (returns int32) */
135 (*last_level
)( const struct lp_sampler_dynamic_state
*state
,
136 struct gallivm_state
*gallivm
,
139 /** Obtain stride in bytes between image rows/blocks (returns int32) */
141 (*row_stride
)( const struct lp_sampler_dynamic_state
*state
,
142 struct gallivm_state
*gallivm
,
145 /** Obtain stride in bytes between image slices (returns int32) */
147 (*img_stride
)( const struct lp_sampler_dynamic_state
*state
,
148 struct gallivm_state
*gallivm
,
151 /** Obtain pointer to array of pointers to mimpap levels */
153 (*data_ptr
)( const struct lp_sampler_dynamic_state
*state
,
154 struct gallivm_state
*gallivm
,
157 /** Obtain texture min lod (returns float) */
159 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
160 struct gallivm_state
*gallivm
, unsigned unit
);
162 /** Obtain texture max lod (returns float) */
164 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
165 struct gallivm_state
*gallivm
, unsigned unit
);
167 /** Obtain texture lod bias (returns float) */
169 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
170 struct gallivm_state
*gallivm
, unsigned unit
);
172 /** Obtain texture border color (returns ptr to float[4]) */
174 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
175 struct gallivm_state
*gallivm
, unsigned unit
);
180 * Keep all information for sampling code generation in a single place.
182 struct lp_build_sample_context
184 struct gallivm_state
*gallivm
;
186 const struct lp_sampler_static_state
*static_state
;
188 struct lp_sampler_dynamic_state
*dynamic_state
;
190 const struct util_format_description
*format_desc
;
192 /* See texture_dims() */
195 /** regular scalar float type */
196 struct lp_type float_type
;
197 struct lp_build_context float_bld
;
199 /** float vector type */
200 struct lp_build_context float_vec_bld
;
202 /** regular scalar float type */
203 struct lp_type int_type
;
204 struct lp_build_context int_bld
;
206 /** Incoming coordinates type and build context */
207 struct lp_type coord_type
;
208 struct lp_build_context coord_bld
;
210 /** Signed integer coordinates */
211 struct lp_type int_coord_type
;
212 struct lp_build_context int_coord_bld
;
214 /** Unsigned integer texture size */
215 struct lp_type int_size_type
;
216 struct lp_build_context int_size_bld
;
218 /** Unsigned integer texture size */
219 struct lp_type float_size_type
;
220 struct lp_build_context float_size_bld
;
222 /** Output texels type and build context */
223 struct lp_type texel_type
;
224 struct lp_build_context texel_bld
;
226 /* Common dynamic state values */
230 LLVMValueRef row_stride_array
;
231 LLVMValueRef img_stride_array
;
232 LLVMValueRef data_array
;
234 /** Integer vector with texture width, height, depth */
235 LLVMValueRef int_size
;
241 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
242 * this time. Return whether the given mode is supported by that function.
244 static INLINE boolean
245 lp_is_simple_wrap_mode(unsigned mode
)
248 case PIPE_TEX_WRAP_REPEAT
:
249 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
258 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
261 unsigned char swizzles
[4];
263 swizzles
[0] = bld
->static_state
->swizzle_r
;
264 swizzles
[1] = bld
->static_state
->swizzle_g
;
265 swizzles
[2] = bld
->static_state
->swizzle_b
;
266 swizzles
[3] = bld
->static_state
->swizzle_a
;
268 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
272 static INLINE
unsigned
273 texture_dims(enum pipe_texture_target tex
)
276 case PIPE_TEXTURE_1D
:
278 case PIPE_TEXTURE_2D
:
279 case PIPE_TEXTURE_RECT
:
280 case PIPE_TEXTURE_CUBE
:
282 case PIPE_TEXTURE_3D
:
285 assert(0 && "bad texture target in texture_dims()");
292 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
293 unsigned min_img_filter
,
294 unsigned mag_img_filter
);
297 * Derive the sampler static state.
300 lp_sampler_static_state(struct lp_sampler_static_state
*state
,
301 const struct pipe_sampler_view
*view
,
302 const struct pipe_sampler_state
*sampler
);
306 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
308 const LLVMValueRef ddx
[4],
309 const LLVMValueRef ddy
[4],
310 LLVMValueRef lod_bias
, /* optional */
311 LLVMValueRef explicit_lod
, /* optional */
313 LLVMValueRef
*out_lod_ipart
,
314 LLVMValueRef
*out_lod_fpart
);
317 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
320 LLVMValueRef
*level_out
);
323 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
325 LLVMValueRef lod_ipart
,
326 LLVMValueRef
*lod_fpart_inout
,
327 LLVMValueRef
*level0_out
,
328 LLVMValueRef
*level1_out
);
331 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
335 lp_build_get_const_mipmap_level(struct lp_build_sample_context
*bld
,
340 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
342 LLVMValueRef
*out_size_vec
,
343 LLVMValueRef
*row_stride_vec
,
344 LLVMValueRef
*img_stride_vec
);
348 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
349 struct lp_type size_type
,
350 struct lp_type coord_type
,
352 LLVMValueRef
*out_width
,
353 LLVMValueRef
*out_height
,
354 LLVMValueRef
*out_depth
);
358 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
359 LLVMValueRef flt_size
,
366 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
371 LLVMValueRef
*face_s
,
372 LLVMValueRef
*face_t
);
376 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
377 unsigned block_length
,
380 LLVMValueRef
*out_offset
,
381 LLVMValueRef
*out_i
);
385 lp_build_sample_offset(struct lp_build_context
*bld
,
386 const struct util_format_description
*format_desc
,
390 LLVMValueRef y_stride
,
391 LLVMValueRef z_stride
,
392 LLVMValueRef
*out_offset
,
394 LLVMValueRef
*out_j
);
398 lp_build_sample_soa(struct gallivm_state
*gallivm
,
399 const struct lp_sampler_static_state
*static_state
,
400 struct lp_sampler_dynamic_state
*dynamic_state
,
401 struct lp_type fp_type
,
404 const LLVMValueRef
*coords
,
405 const LLVMValueRef
*ddx
,
406 const LLVMValueRef
*ddy
,
407 LLVMValueRef lod_bias
,
408 LLVMValueRef explicit_lod
,
409 LLVMValueRef texel_out
[4]);
412 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
413 const struct lp_sampler_static_state
*static_state
,
414 struct lp_sampler_dynamic_state
*dynamic_state
,
416 LLVMValueRef explicit_lod
,
417 LLVMValueRef
*sizes_out
);
420 lp_build_sample_nop(struct gallivm_state
*gallivm
, struct lp_type type
,
421 LLVMValueRef texel_out
[4]);
425 lp_build_minify(struct lp_build_context
*bld
,
426 LLVMValueRef base_size
,
430 #endif /* LP_BLD_SAMPLE_H */