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"
50 struct pipe_sampler_view
;
51 struct pipe_sampler_state
;
52 struct pipe_image_view
;
53 struct util_format_description
;
55 struct lp_build_context
;
59 * Helper struct holding all derivatives needed for sampling
68 enum lp_sampler_lod_property
{
69 LP_SAMPLER_LOD_SCALAR
,
70 LP_SAMPLER_LOD_PER_ELEMENT
,
71 LP_SAMPLER_LOD_PER_QUAD
75 enum lp_sampler_lod_control
{
76 LP_SAMPLER_LOD_IMPLICIT
,
78 LP_SAMPLER_LOD_EXPLICIT
,
79 LP_SAMPLER_LOD_DERIVATIVES
,
83 enum lp_sampler_op_type
{
84 LP_SAMPLER_OP_TEXTURE
,
91 #define LP_SAMPLER_SHADOW (1 << 0)
92 #define LP_SAMPLER_OFFSETS (1 << 1)
93 #define LP_SAMPLER_OP_TYPE_SHIFT 2
94 #define LP_SAMPLER_OP_TYPE_MASK (3 << 2)
95 #define LP_SAMPLER_LOD_CONTROL_SHIFT 4
96 #define LP_SAMPLER_LOD_CONTROL_MASK (3 << 4)
97 #define LP_SAMPLER_LOD_PROPERTY_SHIFT 6
98 #define LP_SAMPLER_LOD_PROPERTY_MASK (3 << 6)
99 #define LP_SAMPLER_GATHER_COMP_SHIFT 8
100 #define LP_SAMPLER_GATHER_COMP_MASK (3 << 8)
101 #define LP_SAMPLER_FETCH_MS (1 << 10)
103 struct lp_sampler_params
106 unsigned texture_index
;
107 unsigned sampler_index
;
109 LLVMValueRef context_ptr
;
110 LLVMValueRef thread_data_ptr
;
111 const LLVMValueRef
*coords
;
112 const LLVMValueRef
*offsets
;
113 LLVMValueRef ms_index
;
115 const struct lp_derivatives
*derivs
;
119 struct lp_sampler_size_query_params
121 struct lp_type int_type
;
122 unsigned texture_unit
;
124 LLVMValueRef context_ptr
;
125 boolean is_sviewinfo
;
126 enum lp_sampler_lod_property lod_property
;
127 LLVMValueRef explicit_lod
;
128 LLVMValueRef
*sizes_out
;
131 #define LP_IMG_LOAD 0
132 #define LP_IMG_STORE 1
133 #define LP_IMG_ATOMIC 2
134 #define LP_IMG_ATOMIC_CAS 3
139 unsigned image_index
;
142 LLVMAtomicRMWBinOp op
;
143 LLVMValueRef exec_mask
;
144 LLVMValueRef context_ptr
;
145 LLVMValueRef thread_data_ptr
;
146 const LLVMValueRef
*coords
;
147 LLVMValueRef ms_index
;
148 LLVMValueRef indata
[4];
149 LLVMValueRef indata2
[4];
150 LLVMValueRef
*outdata
;
153 * Texture static state.
155 * These are the bits of state from pipe_resource/pipe_sampler_view that
156 * are embedded in the generated code.
158 struct lp_static_texture_state
160 /* pipe_sampler_view's state */
161 enum pipe_format format
;
162 unsigned swizzle_r
:3; /**< PIPE_SWIZZLE_* */
163 unsigned swizzle_g
:3;
164 unsigned swizzle_b
:3;
165 unsigned swizzle_a
:3;
167 /* pipe_texture's state */
168 unsigned target
:4; /**< PIPE_TEXTURE_* */
169 unsigned pot_width
:1; /**< is the width a power of two? */
170 unsigned pot_height
:1;
171 unsigned pot_depth
:1;
172 unsigned level_zero_only
:1;
177 * Sampler static state.
179 * These are the bits of state from pipe_sampler_state that
180 * are embedded in the generated code.
182 struct lp_static_sampler_state
184 /* pipe_sampler_state's state */
188 unsigned min_img_filter
:2;
189 unsigned min_mip_filter
:2;
190 unsigned mag_img_filter
:2;
191 unsigned compare_mode
:1;
192 unsigned compare_func
:3;
193 unsigned normalized_coords
:1;
194 unsigned min_max_lod_equal
:1; /**< min_lod == max_lod ? */
195 unsigned lod_bias_non_zero
:1;
196 unsigned max_lod_pos
:1;
197 unsigned apply_min_lod
:1; /**< min_lod > 0 ? */
198 unsigned apply_max_lod
:1; /**< max_lod < last_level ? */
199 unsigned seamless_cube_map
:1;
202 unsigned force_nearest_s
:1;
203 unsigned force_nearest_t
:1;
208 * Sampler dynamic state.
210 * These are the bits of state from pipe_resource/pipe_sampler_view
211 * as well as from sampler state that are computed at runtime.
213 * There are obtained through callbacks, as we don't want to tie the texture
214 * sampling code generation logic to any particular texture layout or pipe
217 struct lp_sampler_dynamic_state
219 /* First callbacks for sampler view state */
221 /** Obtain the base texture width (or number of elements) (returns int32) */
223 (*width
)(const struct lp_sampler_dynamic_state
*state
,
224 struct gallivm_state
*gallivm
,
225 LLVMValueRef context_ptr
,
226 unsigned texture_unit
);
228 /** Obtain the base texture height (returns int32) */
230 (*height
)(const struct lp_sampler_dynamic_state
*state
,
231 struct gallivm_state
*gallivm
,
232 LLVMValueRef context_ptr
,
233 unsigned texture_unit
);
235 /** Obtain the base texture depth (or array size) (returns int32) */
237 (*depth
)(const struct lp_sampler_dynamic_state
*state
,
238 struct gallivm_state
*gallivm
,
239 LLVMValueRef context_ptr
,
240 unsigned texture_unit
);
242 /** Obtain the first mipmap level (base level) (returns int32) */
244 (*first_level
)(const struct lp_sampler_dynamic_state
*state
,
245 struct gallivm_state
*gallivm
,
246 LLVMValueRef context_ptr
,
247 unsigned texture_unit
);
249 /** Obtain the number of mipmap levels minus one (returns int32) */
251 (*last_level
)(const struct lp_sampler_dynamic_state
*state
,
252 struct gallivm_state
*gallivm
,
253 LLVMValueRef context_ptr
,
254 unsigned texture_unit
);
256 /** Obtain stride in bytes between image rows/blocks (returns int32) */
258 (*row_stride
)(const struct lp_sampler_dynamic_state
*state
,
259 struct gallivm_state
*gallivm
,
260 LLVMValueRef context_ptr
,
261 unsigned texture_unit
);
263 /** Obtain stride in bytes between image slices (returns int32) */
265 (*img_stride
)(const struct lp_sampler_dynamic_state
*state
,
266 struct gallivm_state
*gallivm
,
267 LLVMValueRef context_ptr
,
268 unsigned texture_unit
);
270 /** Obtain pointer to base of texture */
272 (*base_ptr
)(const struct lp_sampler_dynamic_state
*state
,
273 struct gallivm_state
*gallivm
,
274 LLVMValueRef context_ptr
,
275 unsigned texture_unit
);
277 /** Obtain pointer to array of mipmap offsets */
279 (*mip_offsets
)(const struct lp_sampler_dynamic_state
*state
,
280 struct gallivm_state
*gallivm
,
281 LLVMValueRef context_ptr
,
282 unsigned texture_unit
);
284 /** Obtain number of samples (returns int32) */
286 (*num_samples
)(const struct lp_sampler_dynamic_state
*state
,
287 struct gallivm_state
*gallivm
,
288 LLVMValueRef context_ptr
,
289 unsigned texture_unit
);
291 /** Obtain multisample stride (returns int32) */
293 (*sample_stride
)(const struct lp_sampler_dynamic_state
*state
,
294 struct gallivm_state
*gallivm
,
295 LLVMValueRef context_ptr
,
296 unsigned texture_unit
);
298 /* These are callbacks for sampler state */
300 /** Obtain texture min lod (returns float) */
302 (*min_lod
)(const struct lp_sampler_dynamic_state
*state
,
303 struct gallivm_state
*gallivm
,
304 LLVMValueRef context_ptr
,
305 unsigned sampler_unit
);
307 /** Obtain texture max lod (returns float) */
309 (*max_lod
)(const struct lp_sampler_dynamic_state
*state
,
310 struct gallivm_state
*gallivm
,
311 LLVMValueRef context_ptr
,
312 unsigned sampler_unit
);
314 /** Obtain texture lod bias (returns float) */
316 (*lod_bias
)(const struct lp_sampler_dynamic_state
*state
,
317 struct gallivm_state
*gallivm
,
318 LLVMValueRef context_ptr
,
319 unsigned sampler_unit
);
321 /** Obtain texture border color (returns ptr to float[4]) */
323 (*border_color
)(const struct lp_sampler_dynamic_state
*state
,
324 struct gallivm_state
*gallivm
,
325 LLVMValueRef context_ptr
,
326 unsigned sampler_unit
);
329 * Obtain texture cache (returns ptr to lp_build_format_cache).
331 * It's optional: no caching will be done if it's NULL.
334 (*cache_ptr
)(const struct lp_sampler_dynamic_state
*state
,
335 struct gallivm_state
*gallivm
,
336 LLVMValueRef thread_data_ptr
,
342 * Keep all information for sampling code generation in a single place.
344 struct lp_build_sample_context
346 struct gallivm_state
*gallivm
;
348 const struct lp_static_texture_state
*static_texture_state
;
349 const struct lp_static_sampler_state
*static_sampler_state
;
351 struct lp_sampler_dynamic_state
*dynamic_state
;
353 const struct util_format_description
*format_desc
;
355 /* See texture_dims() */
358 /** SIMD vector width */
359 unsigned vector_width
;
361 /** number of mipmaps (valid are 1, length/4, length) */
364 /** number of lod values (valid are 1, length/4, length) */
367 unsigned gather_comp
;
369 boolean no_brilinear
;
370 boolean no_rho_approx
;
373 /** regular scalar float type */
374 struct lp_type float_type
;
375 struct lp_build_context float_bld
;
377 /** float vector type */
378 struct lp_build_context float_vec_bld
;
380 /** regular scalar int type */
381 struct lp_type int_type
;
382 struct lp_build_context int_bld
;
384 /** Incoming coordinates type and build context */
385 struct lp_type coord_type
;
386 struct lp_build_context coord_bld
;
388 /** Signed integer coordinates */
389 struct lp_type int_coord_type
;
390 struct lp_build_context int_coord_bld
;
392 /** Unsigned integer texture size */
393 struct lp_type int_size_in_type
;
394 struct lp_build_context int_size_in_bld
;
396 /** Float incoming texture size */
397 struct lp_type float_size_in_type
;
398 struct lp_build_context float_size_in_bld
;
400 /** Unsigned integer texture size (might be per quad) */
401 struct lp_type int_size_type
;
402 struct lp_build_context int_size_bld
;
404 /** Float texture size (might be per quad) */
405 struct lp_type float_size_type
;
406 struct lp_build_context float_size_bld
;
408 /** Output texels type and build context */
409 struct lp_type texel_type
;
410 struct lp_build_context texel_bld
;
412 /** Float level type */
413 struct lp_type levelf_type
;
414 struct lp_build_context levelf_bld
;
416 /** Int level type */
417 struct lp_type leveli_type
;
418 struct lp_build_context leveli_bld
;
420 /** Float lod type */
421 struct lp_type lodf_type
;
422 struct lp_build_context lodf_bld
;
425 struct lp_type lodi_type
;
426 struct lp_build_context lodi_bld
;
428 /* Common dynamic state values */
429 LLVMValueRef row_stride_array
;
430 LLVMValueRef img_stride_array
;
431 LLVMValueRef base_ptr
;
432 LLVMValueRef mip_offsets
;
434 LLVMValueRef sample_stride
;
436 /** Integer vector with texture width, height, depth */
437 LLVMValueRef int_size
;
439 LLVMValueRef border_color_clamped
;
441 LLVMValueRef context_ptr
;
447 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
448 * this time. Return whether the given mode is supported by that function.
450 static inline boolean
451 lp_is_simple_wrap_mode(unsigned mode
)
454 case PIPE_TEX_WRAP_REPEAT
:
455 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
464 apply_sampler_swizzle(struct lp_build_sample_context
*bld
,
467 unsigned char swizzles
[4];
469 swizzles
[0] = bld
->static_texture_state
->swizzle_r
;
470 swizzles
[1] = bld
->static_texture_state
->swizzle_g
;
471 swizzles
[2] = bld
->static_texture_state
->swizzle_b
;
472 swizzles
[3] = bld
->static_texture_state
->swizzle_a
;
474 lp_build_swizzle_soa_inplace(&bld
->texel_bld
, texel
, swizzles
);
478 * not really dimension as such, this indicates the amount of
479 * "normal" texture coords subject to minification, wrapping etc.
481 static inline unsigned
482 texture_dims(enum pipe_texture_target tex
)
485 case PIPE_TEXTURE_1D
:
486 case PIPE_TEXTURE_1D_ARRAY
:
489 case PIPE_TEXTURE_2D
:
490 case PIPE_TEXTURE_2D_ARRAY
:
491 case PIPE_TEXTURE_RECT
:
492 case PIPE_TEXTURE_CUBE
:
493 case PIPE_TEXTURE_CUBE_ARRAY
:
495 case PIPE_TEXTURE_3D
:
498 assert(0 && "bad texture target in texture_dims()");
503 static inline boolean
504 has_layer_coord(enum pipe_texture_target tex
)
507 case PIPE_TEXTURE_1D_ARRAY
:
508 case PIPE_TEXTURE_2D_ARRAY
:
509 /* cube is not layered but 3rd coord (after cube mapping) behaves the same */
510 case PIPE_TEXTURE_CUBE
:
511 case PIPE_TEXTURE_CUBE_ARRAY
:
520 lp_sampler_wrap_mode_uses_border_color(unsigned mode
,
521 unsigned min_img_filter
,
522 unsigned mag_img_filter
);
525 * Derive the sampler static state.
528 lp_sampler_static_sampler_state(struct lp_static_sampler_state
*state
,
529 const struct pipe_sampler_state
*sampler
);
533 lp_sampler_static_texture_state(struct lp_static_texture_state
*state
,
534 const struct pipe_sampler_view
*view
);
537 lp_sampler_static_texture_state_image(struct lp_static_texture_state
*state
,
538 const struct pipe_image_view
*view
);
541 lp_build_lod_selector(struct lp_build_sample_context
*bld
,
543 unsigned texture_index
,
544 unsigned sampler_index
,
548 LLVMValueRef cube_rho
,
549 const struct lp_derivatives
*derivs
,
550 LLVMValueRef lod_bias
, /* optional */
551 LLVMValueRef explicit_lod
, /* optional */
553 LLVMValueRef
*out_lod
,
554 LLVMValueRef
*out_lod_ipart
,
555 LLVMValueRef
*out_lod_fpart
,
556 LLVMValueRef
*out_lod_positive
);
559 lp_build_nearest_mip_level(struct lp_build_sample_context
*bld
,
560 unsigned texture_unit
,
562 LLVMValueRef
*level_out
,
563 LLVMValueRef
*out_of_bounds
);
566 lp_build_linear_mip_levels(struct lp_build_sample_context
*bld
,
567 unsigned texture_unit
,
568 LLVMValueRef lod_ipart
,
569 LLVMValueRef
*lod_fpart_inout
,
570 LLVMValueRef
*level0_out
,
571 LLVMValueRef
*level1_out
);
574 lp_build_get_mipmap_level(struct lp_build_sample_context
*bld
,
579 lp_build_get_mip_offsets(struct lp_build_sample_context
*bld
,
584 lp_build_mipmap_level_sizes(struct lp_build_sample_context
*bld
,
586 LLVMValueRef
*out_size_vec
,
587 LLVMValueRef
*row_stride_vec
,
588 LLVMValueRef
*img_stride_vec
);
592 lp_build_extract_image_sizes(struct lp_build_sample_context
*bld
,
593 struct lp_build_context
*size_bld
,
594 struct lp_type coord_type
,
596 LLVMValueRef
*out_width
,
597 LLVMValueRef
*out_height
,
598 LLVMValueRef
*out_depth
);
602 lp_build_unnormalized_coords(struct lp_build_sample_context
*bld
,
603 LLVMValueRef flt_size
,
610 lp_build_cube_lookup(struct lp_build_sample_context
*bld
,
611 LLVMValueRef
*coords
,
612 const struct lp_derivatives
*derivs_in
, /* optional */
614 struct lp_derivatives
*derivs_out
, /* optional */
615 boolean need_derivs
);
619 lp_build_cube_new_coords(struct lp_build_context
*ivec_bld
,
625 LLVMValueRef max_coord
,
626 LLVMValueRef new_faces
[4],
627 LLVMValueRef new_xcoords
[4][2],
628 LLVMValueRef new_ycoords
[4][2]);
632 lp_build_sample_partial_offset(struct lp_build_context
*bld
,
633 unsigned block_length
,
636 LLVMValueRef
*out_offset
,
637 LLVMValueRef
*out_i
);
641 lp_build_sample_offset(struct lp_build_context
*bld
,
642 const struct util_format_description
*format_desc
,
646 LLVMValueRef y_stride
,
647 LLVMValueRef z_stride
,
648 LLVMValueRef
*out_offset
,
650 LLVMValueRef
*out_j
);
654 lp_build_sample_soa(const struct lp_static_texture_state
*static_texture_state
,
655 const struct lp_static_sampler_state
*static_sampler_state
,
656 struct lp_sampler_dynamic_state
*dynamic_texture_state
,
657 struct gallivm_state
*gallivm
,
658 const struct lp_sampler_params
*params
);
662 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context
*bld
,
663 LLVMValueRef coord_f
,
664 LLVMValueRef length_i
,
665 LLVMValueRef length_f
,
666 LLVMValueRef
*coord0_i
,
667 LLVMValueRef
*weight_f
);
671 lp_build_size_query_soa(struct gallivm_state
*gallivm
,
672 const struct lp_static_texture_state
*static_state
,
673 struct lp_sampler_dynamic_state
*dynamic_state
,
674 const struct lp_sampler_size_query_params
*params
);
677 lp_build_sample_nop(struct gallivm_state
*gallivm
,
679 const LLVMValueRef
*coords
,
680 LLVMValueRef texel_out
[4]);
684 lp_build_minify(struct lp_build_context
*bld
,
685 LLVMValueRef base_size
,
690 lp_build_img_op_soa(const struct lp_static_texture_state
*static_texture_state
,
691 struct lp_sampler_dynamic_state
*dynamic_state
,
692 struct gallivm_state
*gallivm
,
693 const struct lp_img_params
*params
);
699 #endif /* LP_BLD_SAMPLE_H */