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 ? */
93 * Sampler dynamic state.
95 * These are the bits of state from pipe_resource and pipe_sampler_state that
96 * are computed in runtime.
98 * There are obtained through callbacks, as we don't want to tie the texture
99 * sampling code generation logic to any particular texture layout or pipe
102 struct lp_sampler_dynamic_state
105 /** Obtain the base texture width (returns int32) */
107 (*width
)( const struct lp_sampler_dynamic_state
*state
,
108 struct gallivm_state
*gallivm
,
111 /** Obtain the base texture height (returns int32) */
113 (*height
)( const struct lp_sampler_dynamic_state
*state
,
114 struct gallivm_state
*gallivm
,
117 /** Obtain the base texture depth (returns int32) */
119 (*depth
)( const struct lp_sampler_dynamic_state
*state
,
120 struct gallivm_state
*gallivm
,
123 /** Obtain the first mipmap level (base level) (returns int32) */
125 (*first_level
)( const struct lp_sampler_dynamic_state
*state
,
126 struct gallivm_state
*gallivm
,
129 /** Obtain the number of mipmap levels minus one (returns int32) */
131 (*last_level
)( const struct lp_sampler_dynamic_state
*state
,
132 struct gallivm_state
*gallivm
,
135 /** Obtain stride in bytes between image rows/blocks (returns int32) */
137 (*row_stride
)( const struct lp_sampler_dynamic_state
*state
,
138 struct gallivm_state
*gallivm
,
141 /** Obtain stride in bytes between image slices (returns int32) */
143 (*img_stride
)( const struct lp_sampler_dynamic_state
*state
,
144 struct gallivm_state
*gallivm
,
147 /** Obtain pointer to array of pointers to mimpap levels */
149 (*data_ptr
)( const struct lp_sampler_dynamic_state
*state
,
150 struct gallivm_state
*gallivm
,
153 /** Obtain texture min lod (returns float) */
155 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
156 struct gallivm_state
*gallivm
, unsigned unit
);
158 /** Obtain texture max lod (returns float) */
160 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
161 struct gallivm_state
*gallivm
, unsigned unit
);
163 /** Obtain texture lod bias (returns float) */
165 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
166 struct gallivm_state
*gallivm
, unsigned unit
);
168 /** Obtain texture border color (returns ptr to float[4]) */
170 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
171 struct gallivm_state
*gallivm
, unsigned unit
);
176 * Keep all information for sampling code generation in a single place.
178 struct lp_build_sample_context
180 struct gallivm_state
*gallivm
;
182 const struct lp_sampler_static_state
*static_state
;
184 struct lp_sampler_dynamic_state
*dynamic_state
;
186 const struct util_format_description
*format_desc
;
188 /* See texture_dims() */
191 /** regular scalar float type */
192 struct lp_type float_type
;
193 struct lp_build_context float_bld
;
195 /** float vector type */
196 struct lp_build_context float_vec_bld
;
198 /** regular scalar float type */
199 struct lp_type int_type
;
200 struct lp_build_context int_bld
;
202 /** Incoming coordinates type and build context */
203 struct lp_type coord_type
;
204 struct lp_build_context coord_bld
;
206 /** Signed integer coordinates */
207 struct lp_type int_coord_type
;
208 struct lp_build_context int_coord_bld
;
210 /** Unsigned integer texture size */
211 struct lp_type int_size_type
;
212 struct lp_build_context int_size_bld
;
214 /** Unsigned integer texture size */
215 struct lp_type float_size_type
;
216 struct lp_build_context float_size_bld
;
218 /** Output texels type and build context */
219 struct lp_type texel_type
;
220 struct lp_build_context texel_bld
;
222 /* Common dynamic state values */
226 LLVMValueRef row_stride_array
;
227 LLVMValueRef img_stride_array
;
228 LLVMValueRef data_array
;
230 /** Integer vector with texture width, height, depth */
231 LLVMValueRef int_size
;
237 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
238 * this time. Return whether the given mode is supported by that function.
240 static INLINE boolean
241 lp_is_simple_wrap_mode(unsigned mode
)
244 case PIPE_TEX_WRAP_REPEAT
:
245 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
254 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
257 unsigned char swizzles
[4];
259 swizzles
[0] = bld
->static_state
->swizzle_r
;
260 swizzles
[1] = bld
->static_state
->swizzle_g
;
261 swizzles
[2] = bld
->static_state
->swizzle_b
;
262 swizzles
[3] = bld
->static_state
->swizzle_a
;
264 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
268 static INLINE
unsigned
269 texture_dims(enum pipe_texture_target tex
)
272 case PIPE_TEXTURE_1D
:
274 case PIPE_TEXTURE_2D
:
275 case PIPE_TEXTURE_RECT
:
276 case PIPE_TEXTURE_CUBE
:
278 case PIPE_TEXTURE_3D
:
281 assert(0 && "bad texture target in texture_dims()");
288 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
289 unsigned min_img_filter
,
290 unsigned mag_img_filter
);
293 * Derive the sampler static state.
296 lp_sampler_static_state(struct lp_sampler_static_state
*state
,
297 const struct pipe_sampler_view
*view
,
298 const struct pipe_sampler_state
*sampler
);
302 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
304 const LLVMValueRef ddx
[4],
305 const LLVMValueRef ddy
[4],
306 LLVMValueRef lod_bias
, /* optional */
307 LLVMValueRef explicit_lod
, /* optional */
309 LLVMValueRef
*out_lod_ipart
,
310 LLVMValueRef
*out_lod_fpart
);
313 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
316 LLVMValueRef
*level_out
);
319 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
321 LLVMValueRef lod_ipart
,
322 LLVMValueRef
*lod_fpart_inout
,
323 LLVMValueRef
*level0_out
,
324 LLVMValueRef
*level1_out
);
327 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
331 lp_build_get_const_mipmap_level(struct lp_build_sample_context
*bld
,
336 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
338 LLVMValueRef
*out_size_vec
,
339 LLVMValueRef
*row_stride_vec
,
340 LLVMValueRef
*img_stride_vec
);
344 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
345 struct lp_type size_type
,
346 struct lp_type coord_type
,
348 LLVMValueRef
*out_width
,
349 LLVMValueRef
*out_height
,
350 LLVMValueRef
*out_depth
);
354 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
355 LLVMValueRef flt_size
,
362 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
367 LLVMValueRef
*face_s
,
368 LLVMValueRef
*face_t
);
372 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
373 unsigned block_length
,
376 LLVMValueRef
*out_offset
,
377 LLVMValueRef
*out_i
);
381 lp_build_sample_offset(struct lp_build_context
*bld
,
382 const struct util_format_description
*format_desc
,
386 LLVMValueRef y_stride
,
387 LLVMValueRef z_stride
,
388 LLVMValueRef
*out_offset
,
390 LLVMValueRef
*out_j
);
394 lp_build_sample_soa(struct gallivm_state
*gallivm
,
395 const struct lp_sampler_static_state
*static_state
,
396 struct lp_sampler_dynamic_state
*dynamic_state
,
397 struct lp_type fp_type
,
400 const LLVMValueRef
*coords
,
401 const LLVMValueRef
*ddx
,
402 const LLVMValueRef
*ddy
,
403 LLVMValueRef lod_bias
,
404 LLVMValueRef explicit_lod
,
405 LLVMValueRef texel_out
[4]);
408 lp_build_sample_nop(struct gallivm_state
*gallivm
, struct lp_type type
,
409 LLVMValueRef texel_out
[4]);
412 #endif /* LP_BLD_SAMPLE_H */