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;
108
LLVMValueRef texture_index_offset;
110
LLVMValueRef context_ptr;
111
LLVMValueRef thread_data_ptr;
112
const LLVMValueRef *coords;
113
const LLVMValueRef *offsets;
114
LLVMValueRef ms_index;
116
LLVMValueRef aniso_filter_table;
117
const struct lp_derivatives *derivs;
121
struct lp_sampler_size_query_params
123
struct lp_type int_type;
124
unsigned texture_unit;
125
LLVMValueRef texture_unit_offset;
127
LLVMValueRef context_ptr;
128
boolean is_sviewinfo;
130
enum lp_sampler_lod_property lod_property;
131
LLVMValueRef explicit_lod;
132
LLVMValueRef *sizes_out;
135
#define LP_IMG_LOAD 0
136
#define LP_IMG_STORE 1
137
#define LP_IMG_ATOMIC 2
138
#define LP_IMG_ATOMIC_CAS 3
143
unsigned image_index;
144
LLVMValueRef image_index_offset;
147
LLVMAtomicRMWBinOp op;
148
LLVMValueRef exec_mask;
149
LLVMValueRef context_ptr;
150
LLVMValueRef thread_data_ptr;
151
const LLVMValueRef *coords;
152
LLVMValueRef ms_index;
153
LLVMValueRef indata[4];
154
LLVMValueRef indata2[4];
155
LLVMValueRef *outdata;
158
* Texture static state.
160
* These are the bits of state from pipe_resource/pipe_sampler_view that
161
* are embedded in the generated code.
163
struct lp_static_texture_state
165
/* pipe_sampler_view's state */
166
enum pipe_format format;
167
unsigned swizzle_r:3; /**< PIPE_SWIZZLE_* */
168
unsigned swizzle_g:3;
169
unsigned swizzle_b:3;
170
unsigned swizzle_a:3;
172
/* pipe_texture's state */
173
enum pipe_texture_target target:5; /**< PIPE_TEXTURE_* */
174
unsigned pot_width:1; /**< is the width a power of two? */
175
unsigned pot_height:1;
176
unsigned pot_depth:1;
177
unsigned level_zero_only:1;
182
* Sampler static state.
184
* These are the bits of state from pipe_sampler_state that
185
* are embedded in the generated code.
187
struct lp_static_sampler_state
189
/* pipe_sampler_state's state */
193
unsigned min_img_filter:2;
194
unsigned min_mip_filter:2;
195
unsigned mag_img_filter:2;
196
unsigned compare_mode:1;
197
unsigned compare_func:3;
198
unsigned normalized_coords:1;
199
unsigned min_max_lod_equal:1; /**< min_lod == max_lod ? */
200
unsigned lod_bias_non_zero:1;
201
unsigned max_lod_pos:1;
202
unsigned apply_min_lod:1; /**< min_lod > 0 ? */
203
unsigned apply_max_lod:1; /**< max_lod < last_level ? */
204
unsigned seamless_cube_map:1;
206
unsigned reduction_mode:2;
211
* Sampler dynamic state.
213
* These are the bits of state from pipe_resource/pipe_sampler_view
214
* as well as from sampler state that are computed at runtime.
216
* There are obtained through callbacks, as we don't want to tie the texture
217
* sampling code generation logic to any particular texture layout or pipe
220
struct lp_sampler_dynamic_state
222
/* First callbacks for sampler view state */
224
/** Obtain the base texture width (or number of elements) (returns int32) */
226
(*width)(const struct lp_sampler_dynamic_state *state,
227
struct gallivm_state *gallivm,
228
LLVMValueRef context_ptr,
229
unsigned texture_unit, LLVMValueRef texture_unit_offset);
231
/** Obtain the base texture height (returns int32) */
233
(*height)(const struct lp_sampler_dynamic_state *state,
234
struct gallivm_state *gallivm,
235
LLVMValueRef context_ptr,
236
unsigned texture_unit, LLVMValueRef texture_unit_offset);
238
/** Obtain the base texture depth (or array size) (returns int32) */
240
(*depth)(const struct lp_sampler_dynamic_state *state,
241
struct gallivm_state *gallivm,
242
LLVMValueRef context_ptr,
243
unsigned texture_unit, LLVMValueRef texture_unit_offset);
245
/** Obtain the first mipmap level (base level) (returns int32) */
247
(*first_level)(const struct lp_sampler_dynamic_state *state,
248
struct gallivm_state *gallivm,
249
LLVMValueRef context_ptr,
250
unsigned texture_unit, LLVMValueRef texture_unit_offset);
252
/** Obtain the number of mipmap levels minus one (returns int32) */
254
(*last_level)(const struct lp_sampler_dynamic_state *state,
255
struct gallivm_state *gallivm,
256
LLVMValueRef context_ptr,
257
unsigned texture_unit, LLVMValueRef texture_unit_offset);
259
/** Obtain stride in bytes between image rows/blocks (returns int32) */
261
(*row_stride)(const struct lp_sampler_dynamic_state *state,
262
struct gallivm_state *gallivm,
263
LLVMValueRef context_ptr,
264
unsigned texture_unit, LLVMValueRef texture_unit_offset);
266
/** Obtain stride in bytes between image slices (returns int32) */
268
(*img_stride)(const struct lp_sampler_dynamic_state *state,
269
struct gallivm_state *gallivm,
270
LLVMValueRef context_ptr,
271
unsigned texture_unit, LLVMValueRef texture_unit_offset);
273
/** Obtain pointer to base of texture */
275
(*base_ptr)(const struct lp_sampler_dynamic_state *state,
276
struct gallivm_state *gallivm,
277
LLVMValueRef context_ptr,
278
unsigned texture_unit, LLVMValueRef texture_unit_offset);
280
/** Obtain pointer to array of mipmap offsets */
282
(*mip_offsets)(const struct lp_sampler_dynamic_state *state,
283
struct gallivm_state *gallivm,
284
LLVMValueRef context_ptr,
285
unsigned texture_unit, LLVMValueRef texture_unit_offset);
287
/** Obtain number of samples (returns int32) */
289
(*num_samples)(const struct lp_sampler_dynamic_state *state,
290
struct gallivm_state *gallivm,
291
LLVMValueRef context_ptr,
292
unsigned texture_unit, LLVMValueRef texture_unit_offset);
294
/** Obtain multisample stride (returns int32) */
296
(*sample_stride)(const struct lp_sampler_dynamic_state *state,
297
struct gallivm_state *gallivm,
298
LLVMValueRef context_ptr,
299
unsigned texture_unit, LLVMValueRef texture_unit_offset);
301
/* These are callbacks for sampler state */
303
/** Obtain texture min lod (returns float) */
305
(*min_lod)(const struct lp_sampler_dynamic_state *state,
306
struct gallivm_state *gallivm,
307
LLVMValueRef context_ptr,
308
unsigned sampler_unit);
310
/** Obtain texture max lod (returns float) */
312
(*max_lod)(const struct lp_sampler_dynamic_state *state,
313
struct gallivm_state *gallivm,
314
LLVMValueRef context_ptr,
315
unsigned sampler_unit);
317
/** Obtain texture lod bias (returns float) */
319
(*lod_bias)(const struct lp_sampler_dynamic_state *state,
320
struct gallivm_state *gallivm,
321
LLVMValueRef context_ptr,
322
unsigned sampler_unit);
324
/** Obtain texture border color (returns ptr to float[4]) */
326
(*border_color)(const struct lp_sampler_dynamic_state *state,
327
struct gallivm_state *gallivm,
328
LLVMValueRef context_ptr,
329
unsigned sampler_unit);
331
/** Obtain maximum anisotropy */
333
(*max_aniso)(const struct lp_sampler_dynamic_state *state,
334
struct gallivm_state *gallivm,
335
LLVMValueRef context_ptr,
336
unsigned sampler_unit);
339
* Obtain texture cache (returns ptr to lp_build_format_cache).
341
* It's optional: no caching will be done if it's NULL.
344
(*cache_ptr)(const struct lp_sampler_dynamic_state *state,
345
struct gallivm_state *gallivm,
346
LLVMValueRef thread_data_ptr,
352
* Keep all information for sampling code generation in a single place.
354
struct lp_build_sample_context
356
struct gallivm_state *gallivm;
358
const struct lp_static_texture_state *static_texture_state;
359
const struct lp_static_sampler_state *static_sampler_state;
361
struct lp_sampler_dynamic_state *dynamic_state;
363
const struct util_format_description *format_desc;
365
/* See texture_dims() */
368
/** SIMD vector width */
369
unsigned vector_width;
371
/** number of mipmaps (valid are 1, length/4, length) */
374
/** number of lod values (valid are 1, length/4, length) */
377
unsigned gather_comp;
379
boolean no_brilinear;
380
boolean no_rho_approx;
383
/** regular scalar float type */
384
struct lp_type float_type;
385
struct lp_build_context float_bld;
387
/** float vector type */
388
struct lp_build_context float_vec_bld;
390
/** regular scalar int type */
391
struct lp_type int_type;
392
struct lp_build_context int_bld;
394
/** Incoming coordinates type and build context */
395
struct lp_type coord_type;
396
struct lp_build_context coord_bld;
398
/** Signed integer coordinates */
399
struct lp_type int_coord_type;
400
struct lp_build_context int_coord_bld;
402
/** Unsigned integer texture size */
403
struct lp_type int_size_in_type;
404
struct lp_build_context int_size_in_bld;
406
/** Float incoming texture size */
407
struct lp_type float_size_in_type;
408
struct lp_build_context float_size_in_bld;
410
/** Unsigned integer texture size (might be per quad) */
411
struct lp_type int_size_type;
412
struct lp_build_context int_size_bld;
414
/** Float texture size (might be per quad) */
415
struct lp_type float_size_type;
416
struct lp_build_context float_size_bld;
418
/** Output texels type and build context */
419
struct lp_type texel_type;
420
struct lp_build_context texel_bld;
422
/** Float level type */
423
struct lp_type levelf_type;
424
struct lp_build_context levelf_bld;
426
/** Int level type */
427
struct lp_type leveli_type;
428
struct lp_build_context leveli_bld;
430
/** Float lod type */
431
struct lp_type lodf_type;
432
struct lp_build_context lodf_bld;
435
struct lp_type lodi_type;
436
struct lp_build_context lodi_bld;
438
/* Common dynamic state values */
439
LLVMValueRef row_stride_array;
440
LLVMValueRef img_stride_array;
441
LLVMValueRef base_ptr;
442
LLVMValueRef mip_offsets;
444
LLVMValueRef sample_stride;
446
/** Integer vector with texture width, height, depth */
447
LLVMValueRef int_size;
449
LLVMValueRef border_color_clamped;
451
LLVMValueRef context_ptr;
453
LLVMValueRef aniso_filter_table;
457
* Indirect texture access context
459
* This is used to store info across building
460
* and indirect texture switch statement.
462
struct lp_build_sample_array_switch {
463
struct gallivm_state *gallivm;
464
struct lp_sampler_params params;
465
unsigned base, range;
466
LLVMValueRef switch_ref;
467
LLVMBasicBlockRef merge_ref;
471
struct lp_build_img_op_array_switch {
472
struct gallivm_state *gallivm;
473
struct lp_img_params params;
474
unsigned base, range;
475
LLVMValueRef switch_ref;
476
LLVMBasicBlockRef merge_ref;
481
* We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
482
* this time. Return whether the given mode is supported by that function.
484
static inline boolean
485
lp_is_simple_wrap_mode(unsigned mode)
488
case PIPE_TEX_WRAP_REPEAT:
489
case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
498
apply_sampler_swizzle(struct lp_build_sample_context *bld,
501
unsigned char swizzles[4];
503
swizzles[0] = bld->static_texture_state->swizzle_r;
504
swizzles[1] = bld->static_texture_state->swizzle_g;
505
swizzles[2] = bld->static_texture_state->swizzle_b;
506
swizzles[3] = bld->static_texture_state->swizzle_a;
508
lp_build_swizzle_soa_inplace(&bld->texel_bld, texel, swizzles);
512
* not really dimension as such, this indicates the amount of
513
* "normal" texture coords subject to minification, wrapping etc.
515
static inline unsigned
516
texture_dims(enum pipe_texture_target tex)
519
case PIPE_TEXTURE_1D:
520
case PIPE_TEXTURE_1D_ARRAY:
523
case PIPE_TEXTURE_2D:
524
case PIPE_TEXTURE_2D_ARRAY:
525
case PIPE_TEXTURE_RECT:
526
case PIPE_TEXTURE_CUBE:
527
case PIPE_TEXTURE_CUBE_ARRAY:
529
case PIPE_TEXTURE_3D:
532
assert(0 && "bad texture target in texture_dims()");
537
static inline boolean
538
has_layer_coord(enum pipe_texture_target tex)
541
case PIPE_TEXTURE_1D_ARRAY:
542
case PIPE_TEXTURE_2D_ARRAY:
543
/* cube is not layered but 3rd coord (after cube mapping) behaves the same */
544
case PIPE_TEXTURE_CUBE:
545
case PIPE_TEXTURE_CUBE_ARRAY:
554
lp_sampler_wrap_mode_uses_border_color(unsigned mode,
555
unsigned min_img_filter,
556
unsigned mag_img_filter);
559
* Derive the sampler static state.
562
lp_sampler_static_sampler_state(struct lp_static_sampler_state *state,
563
const struct pipe_sampler_state *sampler);
567
lp_sampler_static_texture_state(struct lp_static_texture_state *state,
568
const struct pipe_sampler_view *view);
571
lp_sampler_static_texture_state_image(struct lp_static_texture_state *state,
572
const struct pipe_image_view *view);
575
lp_build_lod_selector(struct lp_build_sample_context *bld,
577
unsigned texture_index,
578
unsigned sampler_index,
582
LLVMValueRef cube_rho,
583
const struct lp_derivatives *derivs,
584
LLVMValueRef lod_bias, /* optional */
585
LLVMValueRef explicit_lod, /* optional */
587
LLVMValueRef max_aniso,
588
LLVMValueRef *out_lod,
589
LLVMValueRef *out_lod_ipart,
590
LLVMValueRef *out_lod_fpart,
591
LLVMValueRef *out_lod_positive);
594
lp_build_nearest_mip_level(struct lp_build_sample_context *bld,
595
unsigned texture_unit,
597
LLVMValueRef *level_out,
598
LLVMValueRef *out_of_bounds);
601
lp_build_linear_mip_levels(struct lp_build_sample_context *bld,
602
unsigned texture_unit,
603
LLVMValueRef lod_ipart,
604
LLVMValueRef *lod_fpart_inout,
605
LLVMValueRef *level0_out,
606
LLVMValueRef *level1_out);
609
lp_build_get_mipmap_level(struct lp_build_sample_context *bld,
614
lp_build_get_mip_offsets(struct lp_build_sample_context *bld,
619
lp_build_mipmap_level_sizes(struct lp_build_sample_context *bld,
621
LLVMValueRef *out_size_vec,
622
LLVMValueRef *row_stride_vec,
623
LLVMValueRef *img_stride_vec);
627
lp_build_extract_image_sizes(struct lp_build_sample_context *bld,
628
struct lp_build_context *size_bld,
629
struct lp_type coord_type,
631
LLVMValueRef *out_width,
632
LLVMValueRef *out_height,
633
LLVMValueRef *out_depth);
637
lp_build_unnormalized_coords(struct lp_build_sample_context *bld,
638
LLVMValueRef flt_size,
645
lp_build_cube_lookup(struct lp_build_sample_context *bld,
646
LLVMValueRef *coords,
647
const struct lp_derivatives *derivs_in, /* optional */
649
struct lp_derivatives *derivs_out, /* optional */
650
boolean need_derivs);
654
lp_build_cube_new_coords(struct lp_build_context *ivec_bld,
660
LLVMValueRef max_coord,
661
LLVMValueRef new_faces[4],
662
LLVMValueRef new_xcoords[4][2],
663
LLVMValueRef new_ycoords[4][2]);
667
lp_build_sample_partial_offset(struct lp_build_context *bld,
668
unsigned block_length,
671
LLVMValueRef *out_offset,
672
LLVMValueRef *out_i);
676
lp_build_sample_offset(struct lp_build_context *bld,
677
const struct util_format_description *format_desc,
681
LLVMValueRef y_stride,
682
LLVMValueRef z_stride,
683
LLVMValueRef *out_offset,
685
LLVMValueRef *out_j);
689
lp_build_sample_soa(const struct lp_static_texture_state *static_texture_state,
690
const struct lp_static_sampler_state *static_sampler_state,
691
struct lp_sampler_dynamic_state *dynamic_texture_state,
692
struct gallivm_state *gallivm,
693
const struct lp_sampler_params *params);
697
lp_build_coord_repeat_npot_linear(struct lp_build_sample_context *bld,
698
LLVMValueRef coord_f,
699
LLVMValueRef length_i,
700
LLVMValueRef length_f,
701
LLVMValueRef *coord0_i,
702
LLVMValueRef *weight_f);
706
lp_build_size_query_soa(struct gallivm_state *gallivm,
707
const struct lp_static_texture_state *static_state,
708
struct lp_sampler_dynamic_state *dynamic_state,
709
const struct lp_sampler_size_query_params *params);
712
lp_build_sample_nop(struct gallivm_state *gallivm,
714
const LLVMValueRef *coords,
715
LLVMValueRef texel_out[4]);
719
lp_build_minify(struct lp_build_context *bld,
720
LLVMValueRef base_size,
725
lp_build_img_op_soa(const struct lp_static_texture_state *static_texture_state,
726
struct lp_sampler_dynamic_state *dynamic_state,
727
struct gallivm_state *gallivm,
728
const struct lp_img_params *params,
729
LLVMValueRef outdata[4]);
732
lp_build_sample_array_init_soa(struct lp_build_sample_array_switch *switch_info,
733
struct gallivm_state *gallivm,
734
const struct lp_sampler_params *params,
736
unsigned base, unsigned range);
739
lp_build_sample_array_case_soa(struct lp_build_sample_array_switch *switch_info,
741
const struct lp_static_texture_state *static_texture_state,
742
const struct lp_static_sampler_state *static_sampler_state,
743
struct lp_sampler_dynamic_state *dynamic_texture_state);
745
void lp_build_sample_array_fini_soa(struct lp_build_sample_array_switch *switch_info);
748
lp_build_image_op_switch_soa(struct lp_build_img_op_array_switch *switch_info,
749
struct gallivm_state *gallivm,
750
const struct lp_img_params *params,
752
unsigned base, unsigned range);
755
lp_build_image_op_array_case(struct lp_build_img_op_array_switch *switch_info,
757
const struct lp_static_texture_state *static_texture_state,
758
struct lp_sampler_dynamic_state *dynamic_state);
760
void lp_build_image_op_array_fini_soa(struct lp_build_img_op_array_switch *switch_info);
763
lp_build_reduce_filter(struct lp_build_context *bld,
764
enum pipe_tex_reduction_mode mode,
772
lp_build_reduce_filter_2d(struct lp_build_context *bld,
773
enum pipe_tex_reduction_mode mode,
785
lp_build_reduce_filter_3d(struct lp_build_context *bld,
786
enum pipe_tex_reduction_mode mode,
802
const float *lp_build_sample_aniso_filter_table(void);
807
#endif /* LP_BLD_SAMPLE_H */