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
64 enum lp_sampler_lod_property
{
65 LP_SAMPLER_LOD_SCALAR
,
66 LP_SAMPLER_LOD_PER_ELEMENT
,
67 LP_SAMPLER_LOD_PER_QUAD
71 enum lp_sampler_lod_control
{
72 LP_SAMPLER_LOD_IMPLICIT
,
74 LP_SAMPLER_LOD_EXPLICIT
,
75 LP_SAMPLER_LOD_DERIVATIVES
,
79 #define LP_SAMPLER_SHADOW (1 << 0)
80 #define LP_SAMPLER_OFFSETS (1 << 1)
81 #define LP_SAMPLER_FETCH (1 << 2)
82 #define LP_SAMPLER_LOD_CONTROL_SHIFT 3
83 #define LP_SAMPLER_LOD_CONTROL_MASK (3 << 3)
84 #define LP_SAMPLER_LOD_PROPERTY_SHIFT 5
85 #define LP_SAMPLER_LOD_PROPERTY_MASK (3 << 5)
87 struct lp_sampler_params
90 unsigned texture_index
;
91 unsigned sampler_index
;
93 LLVMValueRef context_ptr
;
94 const LLVMValueRef
*coords
;
95 const LLVMValueRef
*offsets
;
97 const struct lp_derivatives
*derivs
;
103 * Texture static state.
105 * These are the bits of state from pipe_resource/pipe_sampler_view that
106 * are embedded in the generated code.
108 struct lp_static_texture_state
110 /* pipe_sampler_view's state */
111 enum pipe_format format
;
112 unsigned swizzle_r
:3; /**< PIPE_SWIZZLE_* */
113 unsigned swizzle_g
:3;
114 unsigned swizzle_b
:3;
115 unsigned swizzle_a
:3;
117 /* pipe_texture's state */
118 unsigned target
:4; /**< PIPE_TEXTURE_* */
119 unsigned pot_width
:1; /**< is the width a power of two? */
120 unsigned pot_height
:1;
121 unsigned pot_depth
:1;
122 unsigned level_zero_only
:1;
127 * Sampler static state.
129 * These are the bits of state from pipe_sampler_state that
130 * are embedded in the generated code.
132 struct lp_static_sampler_state
134 /* pipe_sampler_state's state */
138 unsigned min_img_filter
:2;
139 unsigned min_mip_filter
:2;
140 unsigned mag_img_filter
:2;
141 unsigned compare_mode
:1;
142 unsigned compare_func
:3;
143 unsigned normalized_coords
:1;
144 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
145 unsigned lod_bias_non_zero
:1;
146 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
147 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
148 unsigned seamless_cube_map
:1;
151 unsigned force_nearest_s
:1;
152 unsigned force_nearest_t
:1;
157 * Sampler dynamic state.
159 * These are the bits of state from pipe_resource/pipe_sampler_view
160 * as well as from sampler state that are computed at runtime.
162 * There are obtained through callbacks, as we don't want to tie the texture
163 * sampling code generation logic to any particular texture layout or pipe
166 struct lp_sampler_dynamic_state
168 /* First callbacks for sampler view state */
170 /** Obtain the base texture width (or number of elements) (returns int32) */
172 (*width
)(const struct lp_sampler_dynamic_state
*state
,
173 struct gallivm_state
*gallivm
,
174 LLVMValueRef context_ptr
,
175 unsigned texture_unit
);
177 /** Obtain the base texture height (returns int32) */
179 (*height
)(const struct lp_sampler_dynamic_state
*state
,
180 struct gallivm_state
*gallivm
,
181 LLVMValueRef context_ptr
,
182 unsigned texture_unit
);
184 /** Obtain the base texture depth (or array size) (returns int32) */
186 (*depth
)(const struct lp_sampler_dynamic_state
*state
,
187 struct gallivm_state
*gallivm
,
188 LLVMValueRef context_ptr
,
189 unsigned texture_unit
);
191 /** Obtain the first mipmap level (base level) (returns int32) */
193 (*first_level
)(const struct lp_sampler_dynamic_state
*state
,
194 struct gallivm_state
*gallivm
,
195 LLVMValueRef context_ptr
,
196 unsigned texture_unit
);
198 /** Obtain the number of mipmap levels minus one (returns int32) */
200 (*last_level
)(const struct lp_sampler_dynamic_state
*state
,
201 struct gallivm_state
*gallivm
,
202 LLVMValueRef context_ptr
,
203 unsigned texture_unit
);
205 /** Obtain stride in bytes between image rows/blocks (returns int32) */
207 (*row_stride
)(const struct lp_sampler_dynamic_state
*state
,
208 struct gallivm_state
*gallivm
,
209 LLVMValueRef context_ptr
,
210 unsigned texture_unit
);
212 /** Obtain stride in bytes between image slices (returns int32) */
214 (*img_stride
)(const struct lp_sampler_dynamic_state
*state
,
215 struct gallivm_state
*gallivm
,
216 LLVMValueRef context_ptr
,
217 unsigned texture_unit
);
219 /** Obtain pointer to base of texture */
221 (*base_ptr
)(const struct lp_sampler_dynamic_state
*state
,
222 struct gallivm_state
*gallivm
,
223 LLVMValueRef context_ptr
,
224 unsigned texture_unit
);
226 /** Obtain pointer to array of mipmap offsets */
228 (*mip_offsets
)(const struct lp_sampler_dynamic_state
*state
,
229 struct gallivm_state
*gallivm
,
230 LLVMValueRef context_ptr
,
231 unsigned texture_unit
);
233 /* These are callbacks for sampler state */
235 /** Obtain texture min lod (returns float) */
237 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
238 struct gallivm_state
*gallivm
,
239 LLVMValueRef context_ptr
,
240 unsigned sampler_unit
);
242 /** Obtain texture max lod (returns float) */
244 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
245 struct gallivm_state
*gallivm
,
246 LLVMValueRef context_ptr
,
247 unsigned sampler_unit
);
249 /** Obtain texture lod bias (returns float) */
251 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
252 struct gallivm_state
*gallivm
,
253 LLVMValueRef context_ptr
,
254 unsigned sampler_unit
);
256 /** Obtain texture border color (returns ptr to float[4]) */
258 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
259 struct gallivm_state
*gallivm
,
260 LLVMValueRef context_ptr
,
261 unsigned sampler_unit
);
266 * Keep all information for sampling code generation in a single place.
268 struct lp_build_sample_context
270 struct gallivm_state
*gallivm
;
272 const struct lp_static_texture_state
*static_texture_state
;
273 const struct lp_static_sampler_state
*static_sampler_state
;
275 struct lp_sampler_dynamic_state
*dynamic_state
;
277 const struct util_format_description
*format_desc
;
279 /* See texture_dims() */
282 /** SIMD vector width */
283 unsigned vector_width
;
285 /** number of mipmaps (valid are 1, length/4, length) */
288 /** number of lod values (valid are 1, length/4, length) */
291 /** regular scalar float type */
292 struct lp_type float_type
;
293 struct lp_build_context float_bld
;
295 /** float vector type */
296 struct lp_build_context float_vec_bld
;
298 /** regular scalar int type */
299 struct lp_type int_type
;
300 struct lp_build_context int_bld
;
302 /** Incoming coordinates type and build context */
303 struct lp_type coord_type
;
304 struct lp_build_context coord_bld
;
306 /** Signed integer coordinates */
307 struct lp_type int_coord_type
;
308 struct lp_build_context int_coord_bld
;
310 /** Unsigned integer texture size */
311 struct lp_type int_size_in_type
;
312 struct lp_build_context int_size_in_bld
;
314 /** Float incoming texture size */
315 struct lp_type float_size_in_type
;
316 struct lp_build_context float_size_in_bld
;
318 /** Unsigned integer texture size (might be per quad) */
319 struct lp_type int_size_type
;
320 struct lp_build_context int_size_bld
;
322 /** Float texture size (might be per quad) */
323 struct lp_type float_size_type
;
324 struct lp_build_context float_size_bld
;
326 /** Output texels type and build context */
327 struct lp_type texel_type
;
328 struct lp_build_context texel_bld
;
330 /** Float level type */
331 struct lp_type levelf_type
;
332 struct lp_build_context levelf_bld
;
334 /** Int level type */
335 struct lp_type leveli_type
;
336 struct lp_build_context leveli_bld
;
338 /** Float lod type */
339 struct lp_type lodf_type
;
340 struct lp_build_context lodf_bld
;
343 struct lp_type lodi_type
;
344 struct lp_build_context lodi_bld
;
346 /* Common dynamic state values */
347 LLVMValueRef row_stride_array
;
348 LLVMValueRef img_stride_array
;
349 LLVMValueRef base_ptr
;
350 LLVMValueRef mip_offsets
;
352 /** Integer vector with texture width, height, depth */
353 LLVMValueRef int_size
;
355 LLVMValueRef border_color_clamped
;
357 LLVMValueRef context_ptr
;
363 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
364 * this time. Return whether the given mode is supported by that function.
366 static INLINE boolean
367 lp_is_simple_wrap_mode(unsigned mode
)
370 case PIPE_TEX_WRAP_REPEAT
:
371 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
380 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
383 unsigned char swizzles
[4];
385 swizzles
[0] = bld
->static_texture_state
->swizzle_r
;
386 swizzles
[1] = bld
->static_texture_state
->swizzle_g
;
387 swizzles
[2] = bld
->static_texture_state
->swizzle_b
;
388 swizzles
[3] = bld
->static_texture_state
->swizzle_a
;
390 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
394 * not really dimension as such, this indicates the amount of
395 * "normal" texture coords subject to minification, wrapping etc.
397 static INLINE
unsigned
398 texture_dims(enum pipe_texture_target tex
)
401 case PIPE_TEXTURE_1D
:
402 case PIPE_TEXTURE_1D_ARRAY
:
405 case PIPE_TEXTURE_2D
:
406 case PIPE_TEXTURE_2D_ARRAY
:
407 case PIPE_TEXTURE_RECT
:
408 case PIPE_TEXTURE_CUBE
:
409 case PIPE_TEXTURE_CUBE_ARRAY
:
411 case PIPE_TEXTURE_3D
:
414 assert(0 && "bad texture target in texture_dims()");
419 static INLINE boolean
420 has_layer_coord(enum pipe_texture_target tex
)
423 case PIPE_TEXTURE_1D_ARRAY
:
424 case PIPE_TEXTURE_2D_ARRAY
:
425 /* cube is not layered but 3rd coord (after cube mapping) behaves the same */
426 case PIPE_TEXTURE_CUBE
:
427 case PIPE_TEXTURE_CUBE_ARRAY
:
436 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
437 unsigned min_img_filter
,
438 unsigned mag_img_filter
);
441 * Derive the sampler static state.
444 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
445 const struct pipe_sampler_state
*sampler
);
449 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
450 const struct pipe_sampler_view
*view
);
454 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
455 unsigned texture_index
,
456 unsigned sampler_index
,
460 LLVMValueRef cube_rho
,
461 const struct lp_derivatives
*derivs
,
462 LLVMValueRef lod_bias
, /* optional */
463 LLVMValueRef explicit_lod
, /* optional */
465 LLVMValueRef
*out_lod_ipart
,
466 LLVMValueRef
*out_lod_fpart
,
467 LLVMValueRef
*out_lod_positive
);
470 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
471 unsigned texture_unit
,
473 LLVMValueRef
*level_out
,
474 LLVMValueRef
*out_of_bounds
);
477 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
478 unsigned texture_unit
,
479 LLVMValueRef lod_ipart
,
480 LLVMValueRef
*lod_fpart_inout
,
481 LLVMValueRef
*level0_out
,
482 LLVMValueRef
*level1_out
);
485 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
490 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
495 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
497 LLVMValueRef
*out_size_vec
,
498 LLVMValueRef
*row_stride_vec
,
499 LLVMValueRef
*img_stride_vec
);
503 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
504 struct lp_build_context
*size_bld
,
505 struct lp_type coord_type
,
507 LLVMValueRef
*out_width
,
508 LLVMValueRef
*out_height
,
509 LLVMValueRef
*out_depth
);
513 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
514 LLVMValueRef flt_size
,
521 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
522 LLVMValueRef
*coords
,
523 const struct lp_derivatives
*derivs_in
, /* optional */
525 struct lp_derivatives
*derivs_out
, /* optional */
526 boolean need_derivs
);
530 lp_build_cube_new_coords(struct lp_build_context
*ivec_bld
,
536 LLVMValueRef max_coord
,
537 LLVMValueRef new_faces
[4],
538 LLVMValueRef new_xcoords
[4][2],
539 LLVMValueRef new_ycoords
[4][2]);
543 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
544 unsigned block_length
,
547 LLVMValueRef
*out_offset
,
548 LLVMValueRef
*out_i
);
552 lp_build_sample_offset(struct lp_build_context
*bld
,
553 const struct util_format_description
*format_desc
,
557 LLVMValueRef y_stride
,
558 LLVMValueRef z_stride
,
559 LLVMValueRef
*out_offset
,
561 LLVMValueRef
*out_j
);
565 lp_build_sample_soa(const struct lp_static_texture_state
*static_texture_state
,
566 const struct lp_static_sampler_state
*static_sampler_state
,
567 struct lp_sampler_dynamic_state
*dynamic_texture_state
,
568 struct gallivm_state
*gallivm
,
569 const struct lp_sampler_params
*params
);
573 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
574 LLVMValueRef coord_f
,
575 LLVMValueRef length_i
,
576 LLVMValueRef length_f
,
577 LLVMValueRef
*coord0_i
,
578 LLVMValueRef
*weight_f
);
582 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
583 const struct lp_static_texture_state
*static_state
,
584 struct lp_sampler_dynamic_state
*dynamic_state
,
585 struct lp_type int_type
,
586 unsigned texture_unit
,
588 LLVMValueRef context_ptr
,
589 boolean is_sviewinfo
,
590 enum lp_sampler_lod_property lod_property
,
591 LLVMValueRef explicit_lod
,
592 LLVMValueRef
*sizes_out
);
595 lp_build_sample_nop(struct gallivm_state
*gallivm
,
597 const LLVMValueRef
*coords
,
598 LLVMValueRef texel_out
[4]);
602 lp_build_minify(struct lp_build_context
*bld
,
603 LLVMValueRef base_size
,
608 #endif /* LP_BLD_SAMPLE_H */