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 float min_max_lod
; /**< only valid when min_max_lod_equal=1 */
89 unsigned force_nearest_s
:1;
90 unsigned force_nearest_t
:1;
95 * Sampler dynamic state.
97 * These are the bits of state from pipe_resource and pipe_sampler_state that
98 * are computed in runtime.
100 * There are obtained through callbacks, as we don't want to tie the texture
101 * sampling code generation logic to any particular texture layout or pipe
104 struct lp_sampler_dynamic_state
107 /** Obtain the base texture width. */
109 (*width
)( const struct lp_sampler_dynamic_state
*state
,
110 LLVMBuilderRef builder
,
113 /** Obtain the base texture height. */
115 (*height
)( const struct lp_sampler_dynamic_state
*state
,
116 LLVMBuilderRef builder
,
119 /** Obtain the base texture depth. */
121 (*depth
)( const struct lp_sampler_dynamic_state
*state
,
122 LLVMBuilderRef builder
,
125 /** Obtain the number of mipmap levels (minus one). */
127 (*last_level
)( const struct lp_sampler_dynamic_state
*state
,
128 LLVMBuilderRef builder
,
132 (*row_stride
)( const struct lp_sampler_dynamic_state
*state
,
133 LLVMBuilderRef builder
,
137 (*img_stride
)( const struct lp_sampler_dynamic_state
*state
,
138 LLVMBuilderRef builder
,
142 (*data_ptr
)( const struct lp_sampler_dynamic_state
*state
,
143 LLVMBuilderRef builder
,
146 /** Obtain texture min lod */
148 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
149 LLVMBuilderRef builder
, unsigned unit
);
151 /** Obtain texture max lod */
153 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
154 LLVMBuilderRef builder
, unsigned unit
);
156 /** Obtain texture lod bias */
158 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
159 LLVMBuilderRef builder
, unsigned unit
);
161 /** Obtain texture border color */
163 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
164 LLVMBuilderRef builder
, unsigned unit
);
169 * Keep all information for sampling code generation in a single place.
171 struct lp_build_sample_context
173 LLVMBuilderRef builder
;
175 const struct lp_sampler_static_state
*static_state
;
177 struct lp_sampler_dynamic_state
*dynamic_state
;
179 const struct util_format_description
*format_desc
;
181 /** regular scalar float type */
182 struct lp_type float_type
;
183 struct lp_build_context float_bld
;
185 /** float vector type */
186 struct lp_build_context float_vec_bld
;
188 /** regular scalar float type */
189 struct lp_type int_type
;
190 struct lp_build_context int_bld
;
192 /** Incoming coordinates type and build context */
193 struct lp_type coord_type
;
194 struct lp_build_context coord_bld
;
196 /** Unsigned integer coordinates */
197 struct lp_type uint_coord_type
;
198 struct lp_build_context uint_coord_bld
;
200 /** Signed integer coordinates */
201 struct lp_type int_coord_type
;
202 struct lp_build_context int_coord_bld
;
204 /** Output texels type and build context */
205 struct lp_type texel_type
;
206 struct lp_build_context texel_bld
;
212 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
213 * this time. Return whether the given mode is supported by that function.
215 static INLINE boolean
216 lp_is_simple_wrap_mode(unsigned mode
)
219 case PIPE_TEX_WRAP_REPEAT
:
220 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
229 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
232 unsigned char swizzles
[4];
234 swizzles
[0] = bld
->static_state
->swizzle_r
;
235 swizzles
[1] = bld
->static_state
->swizzle_g
;
236 swizzles
[2] = bld
->static_state
->swizzle_b
;
237 swizzles
[3] = bld
->static_state
->swizzle_a
;
239 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
244 texture_dims(enum pipe_texture_target tex
)
247 case PIPE_TEXTURE_1D
:
249 case PIPE_TEXTURE_2D
:
250 case PIPE_TEXTURE_RECT
:
251 case PIPE_TEXTURE_CUBE
:
253 case PIPE_TEXTURE_3D
:
256 assert(0 && "bad texture target in texture_dims()");
263 * Derive the sampler static state.
266 lp_sampler_static_state(struct lp_sampler_static_state
*state
,
267 const struct pipe_sampler_view
*view
,
268 const struct pipe_sampler_state
*sampler
);
272 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
274 const LLVMValueRef ddx
[4],
275 const LLVMValueRef ddy
[4],
276 LLVMValueRef lod_bias
, /* optional */
277 LLVMValueRef explicit_lod
, /* optional */
283 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
286 LLVMValueRef
*level_out
);
289 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
292 LLVMValueRef
*level0_out
,
293 LLVMValueRef
*level1_out
,
294 LLVMValueRef
*weight_out
);
297 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
298 LLVMValueRef data_array
, LLVMValueRef level
);
301 lp_build_get_const_mipmap_level(struct lp_build_sample_context
*bld
,
302 LLVMValueRef data_array
, int level
);
306 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
308 LLVMValueRef width_vec
,
309 LLVMValueRef height_vec
,
310 LLVMValueRef depth_vec
,
311 LLVMValueRef ilevel0
,
312 LLVMValueRef ilevel1
,
313 LLVMValueRef row_stride_array
,
314 LLVMValueRef img_stride_array
,
315 LLVMValueRef
*width0_vec
,
316 LLVMValueRef
*width1_vec
,
317 LLVMValueRef
*height0_vec
,
318 LLVMValueRef
*height1_vec
,
319 LLVMValueRef
*depth0_vec
,
320 LLVMValueRef
*depth1_vec
,
321 LLVMValueRef
*row_stride0_vec
,
322 LLVMValueRef
*row_stride1_vec
,
323 LLVMValueRef
*img_stride0_vec
,
324 LLVMValueRef
*img_stride1_vec
);
328 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
333 LLVMValueRef
*face_s
,
334 LLVMValueRef
*face_t
);
338 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
339 unsigned block_length
,
342 LLVMValueRef
*out_offset
,
343 LLVMValueRef
*out_i
);
347 lp_build_sample_offset(struct lp_build_context
*bld
,
348 const struct util_format_description
*format_desc
,
352 LLVMValueRef y_stride
,
353 LLVMValueRef z_stride
,
354 LLVMValueRef
*out_offset
,
356 LLVMValueRef
*out_j
);
360 lp_build_sample_soa(LLVMBuilderRef builder
,
361 const struct lp_sampler_static_state
*static_state
,
362 struct lp_sampler_dynamic_state
*dynamic_state
,
363 struct lp_type fp_type
,
366 const LLVMValueRef
*coords
,
367 const LLVMValueRef
*ddx
,
368 const LLVMValueRef
*ddy
,
369 LLVMValueRef lod_bias
,
370 LLVMValueRef explicit_lod
,
371 LLVMValueRef texel_out
[4]);
374 lp_build_sample_nop(struct lp_type type
,
375 LLVMValueRef texel_out
[4]);
378 #endif /* LP_BLD_SAMPLE_H */