gallivm: support array textures
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_sample.h
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Texture sampling.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35 #ifndef LP_BLD_SAMPLE_H
36 #define LP_BLD_SAMPLE_H
37
38
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"
44
45
46 struct pipe_resource;
47 struct pipe_sampler_view;
48 struct pipe_sampler_state;
49 struct util_format_description;
50 struct lp_type;
51 struct lp_build_context;
52
53
54 /**
55 * Helper struct holding all derivatives needed for sampling
56 */
57 struct lp_derivatives
58 {
59 LLVMValueRef ddx_ddy[2];
60 };
61
62
63 /**
64 * Sampler static state.
65 *
66 * These are the bits of state from pipe_resource and pipe_sampler_state that
67 * are embedded in the generated code.
68 */
69 struct lp_sampler_static_state
70 {
71 /* pipe_sampler_view's state */
72 enum pipe_format format;
73 unsigned swizzle_r:3; /**< PIPE_SWIZZLE_* */
74 unsigned swizzle_g:3;
75 unsigned swizzle_b:3;
76 unsigned swizzle_a:3;
77
78 /* pipe_texture's state */
79 unsigned target:3; /**< PIPE_TEXTURE_* */
80 unsigned pot_width:1; /**< is the width a power of two? */
81 unsigned pot_height:1;
82 unsigned pot_depth:1;
83
84 /* pipe_sampler_state's state */
85 unsigned wrap_s:3;
86 unsigned wrap_t:3;
87 unsigned wrap_r:3;
88 unsigned min_img_filter:2;
89 unsigned min_mip_filter:2;
90 unsigned mag_img_filter:2;
91 unsigned compare_mode:1;
92 unsigned compare_func:3;
93 unsigned normalized_coords:1;
94 unsigned min_max_lod_equal:1; /**< min_lod == max_lod ? */
95 unsigned lod_bias_non_zero:1;
96 unsigned apply_min_lod:1; /**< min_lod > 0 ? */
97 unsigned apply_max_lod:1; /**< max_lod < last_level ? */
98
99 /* Hacks */
100 unsigned force_nearest_s:1;
101 unsigned force_nearest_t:1;
102 };
103
104
105 /**
106 * Sampler dynamic state.
107 *
108 * These are the bits of state from pipe_resource and pipe_sampler_state that
109 * are computed in runtime.
110 *
111 * There are obtained through callbacks, as we don't want to tie the texture
112 * sampling code generation logic to any particular texture layout or pipe
113 * driver.
114 */
115 struct lp_sampler_dynamic_state
116 {
117
118 /** Obtain the base texture width (returns int32) */
119 LLVMValueRef
120 (*width)( const struct lp_sampler_dynamic_state *state,
121 struct gallivm_state *gallivm,
122 unsigned unit);
123
124 /** Obtain the base texture height (returns int32) */
125 LLVMValueRef
126 (*height)( const struct lp_sampler_dynamic_state *state,
127 struct gallivm_state *gallivm,
128 unsigned unit);
129
130 /** Obtain the base texture depth (returns int32) */
131 LLVMValueRef
132 (*depth)( const struct lp_sampler_dynamic_state *state,
133 struct gallivm_state *gallivm,
134 unsigned unit);
135
136 /** Obtain the first mipmap level (base level) (returns int32) */
137 LLVMValueRef
138 (*first_level)( const struct lp_sampler_dynamic_state *state,
139 struct gallivm_state *gallivm,
140 unsigned unit);
141
142 /** Obtain the number of mipmap levels minus one (returns int32) */
143 LLVMValueRef
144 (*last_level)( const struct lp_sampler_dynamic_state *state,
145 struct gallivm_state *gallivm,
146 unsigned unit);
147
148 /** Obtain stride in bytes between image rows/blocks (returns int32) */
149 LLVMValueRef
150 (*row_stride)( const struct lp_sampler_dynamic_state *state,
151 struct gallivm_state *gallivm,
152 unsigned unit);
153
154 /** Obtain stride in bytes between image slices (returns int32) */
155 LLVMValueRef
156 (*img_stride)( const struct lp_sampler_dynamic_state *state,
157 struct gallivm_state *gallivm,
158 unsigned unit);
159
160 /** Obtain pointer to base of texture */
161 LLVMValueRef
162 (*base_ptr)( const struct lp_sampler_dynamic_state *state,
163 struct gallivm_state *gallivm,
164 unsigned unit);
165
166 /** Obtain pointer to array of mipmap offsets */
167 LLVMValueRef
168 (*mip_offsets)( const struct lp_sampler_dynamic_state *state,
169 struct gallivm_state *gallivm,
170 unsigned unit);
171
172 /** Obtain texture min lod (returns float) */
173 LLVMValueRef
174 (*min_lod)(const struct lp_sampler_dynamic_state *state,
175 struct gallivm_state *gallivm, unsigned unit);
176
177 /** Obtain texture max lod (returns float) */
178 LLVMValueRef
179 (*max_lod)(const struct lp_sampler_dynamic_state *state,
180 struct gallivm_state *gallivm, unsigned unit);
181
182 /** Obtain texture lod bias (returns float) */
183 LLVMValueRef
184 (*lod_bias)(const struct lp_sampler_dynamic_state *state,
185 struct gallivm_state *gallivm, unsigned unit);
186
187 /** Obtain texture border color (returns ptr to float[4]) */
188 LLVMValueRef
189 (*border_color)(const struct lp_sampler_dynamic_state *state,
190 struct gallivm_state *gallivm, unsigned unit);
191 };
192
193
194 /**
195 * Keep all information for sampling code generation in a single place.
196 */
197 struct lp_build_sample_context
198 {
199 struct gallivm_state *gallivm;
200
201 const struct lp_sampler_static_state *static_state;
202
203 struct lp_sampler_dynamic_state *dynamic_state;
204
205 const struct util_format_description *format_desc;
206
207 /* See texture_dims() */
208 unsigned dims;
209
210 /** SIMD vector width */
211 unsigned vector_width;
212
213 /** number of lod values (valid are 1, length/4, length) */
214 unsigned num_lods;
215
216 /** regular scalar float type */
217 struct lp_type float_type;
218 struct lp_build_context float_bld;
219
220 /** float vector type */
221 struct lp_build_context float_vec_bld;
222
223 /** regular scalar int type */
224 struct lp_type int_type;
225 struct lp_build_context int_bld;
226
227 /** Incoming coordinates type and build context */
228 struct lp_type coord_type;
229 struct lp_build_context coord_bld;
230
231 /** Signed integer coordinates */
232 struct lp_type int_coord_type;
233 struct lp_build_context int_coord_bld;
234
235 /** Unsigned integer texture size */
236 struct lp_type int_size_in_type;
237 struct lp_build_context int_size_in_bld;
238
239 /** Float incoming texture size */
240 struct lp_type float_size_in_type;
241 struct lp_build_context float_size_in_bld;
242
243 /** Unsigned integer texture size (might be per quad) */
244 struct lp_type int_size_type;
245 struct lp_build_context int_size_bld;
246
247 /** Float texture size (might be per quad) */
248 struct lp_type float_size_type;
249 struct lp_build_context float_size_bld;
250
251 /** Output texels type and build context */
252 struct lp_type texel_type;
253 struct lp_build_context texel_bld;
254
255 /** Float per-quad type */
256 struct lp_type perquadf_type;
257 struct lp_build_context perquadf_bld;
258
259 /** Int per-quad type */
260 struct lp_type perquadi_type;
261 struct lp_build_context perquadi_bld;
262
263 /* Common dynamic state values */
264 LLVMValueRef row_stride_array;
265 LLVMValueRef img_stride_array;
266 LLVMValueRef base_ptr;
267 LLVMValueRef mip_offsets;
268
269 /** Integer vector with texture width, height, depth */
270 LLVMValueRef int_size;
271 };
272
273
274
275 /**
276 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
277 * this time. Return whether the given mode is supported by that function.
278 */
279 static INLINE boolean
280 lp_is_simple_wrap_mode(unsigned mode)
281 {
282 switch (mode) {
283 case PIPE_TEX_WRAP_REPEAT:
284 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
285 return TRUE;
286 default:
287 return FALSE;
288 }
289 }
290
291
292 static INLINE void
293 apply_sampler_swizzle(struct lp_build_sample_context *bld,
294 LLVMValueRef *texel)
295 {
296 unsigned char swizzles[4];
297
298 swizzles[0] = bld->static_state->swizzle_r;
299 swizzles[1] = bld->static_state->swizzle_g;
300 swizzles[2] = bld->static_state->swizzle_b;
301 swizzles[3] = bld->static_state->swizzle_a;
302
303 lp_build_swizzle_soa_inplace(&bld->texel_bld, texel, swizzles);
304 }
305
306 /*
307 * not really dimension as such, this indicates the amount of
308 * "normal" texture coords subject to minification, wrapping etc.
309 */
310 static INLINE unsigned
311 texture_dims(enum pipe_texture_target tex)
312 {
313 switch (tex) {
314 case PIPE_TEXTURE_1D:
315 case PIPE_TEXTURE_1D_ARRAY:
316 case PIPE_BUFFER:
317 return 1;
318 case PIPE_TEXTURE_2D:
319 case PIPE_TEXTURE_2D_ARRAY:
320 case PIPE_TEXTURE_RECT:
321 case PIPE_TEXTURE_CUBE:
322 return 2;
323 case PIPE_TEXTURE_3D:
324 return 3;
325 default:
326 assert(0 && "bad texture target in texture_dims()");
327 return 2;
328 }
329 }
330
331
332 boolean
333 lp_sampler_wrap_mode_uses_border_color(unsigned mode,
334 unsigned min_img_filter,
335 unsigned mag_img_filter);
336
337 /**
338 * Derive the sampler static state.
339 */
340 void
341 lp_sampler_static_state(struct lp_sampler_static_state *state,
342 const struct pipe_sampler_view *view,
343 const struct pipe_sampler_state *sampler);
344
345
346 void
347 lp_build_lod_selector(struct lp_build_sample_context *bld,
348 unsigned unit,
349 const struct lp_derivatives *derivs,
350 LLVMValueRef lod_bias, /* optional */
351 LLVMValueRef explicit_lod, /* optional */
352 unsigned mip_filter,
353 LLVMValueRef *out_lod_ipart,
354 LLVMValueRef *out_lod_fpart);
355
356 void
357 lp_build_nearest_mip_level(struct lp_build_sample_context *bld,
358 unsigned unit,
359 LLVMValueRef lod,
360 LLVMValueRef *level_out);
361
362 void
363 lp_build_linear_mip_levels(struct lp_build_sample_context *bld,
364 unsigned unit,
365 LLVMValueRef lod_ipart,
366 LLVMValueRef *lod_fpart_inout,
367 LLVMValueRef *level0_out,
368 LLVMValueRef *level1_out);
369
370 LLVMValueRef
371 lp_build_get_mipmap_level(struct lp_build_sample_context *bld,
372 LLVMValueRef level);
373
374
375 LLVMValueRef
376 lp_build_get_mip_offsets(struct lp_build_sample_context *bld,
377 LLVMValueRef level);
378
379
380 void
381 lp_build_mipmap_level_sizes(struct lp_build_sample_context *bld,
382 LLVMValueRef ilevel,
383 LLVMValueRef *out_size_vec,
384 LLVMValueRef *row_stride_vec,
385 LLVMValueRef *img_stride_vec);
386
387
388 void
389 lp_build_extract_image_sizes(struct lp_build_sample_context *bld,
390 struct lp_build_context *size_bld,
391 struct lp_type coord_type,
392 LLVMValueRef size,
393 LLVMValueRef *out_width,
394 LLVMValueRef *out_height,
395 LLVMValueRef *out_depth);
396
397
398 void
399 lp_build_unnormalized_coords(struct lp_build_sample_context *bld,
400 LLVMValueRef flt_size,
401 LLVMValueRef *s,
402 LLVMValueRef *t,
403 LLVMValueRef *r);
404
405
406 void
407 lp_build_cube_lookup(struct lp_build_sample_context *bld,
408 LLVMValueRef s,
409 LLVMValueRef t,
410 LLVMValueRef r,
411 LLVMValueRef *face,
412 LLVMValueRef *face_s,
413 LLVMValueRef *face_t);
414
415
416 void
417 lp_build_sample_partial_offset(struct lp_build_context *bld,
418 unsigned block_length,
419 LLVMValueRef coord,
420 LLVMValueRef stride,
421 LLVMValueRef *out_offset,
422 LLVMValueRef *out_i);
423
424
425 void
426 lp_build_sample_offset(struct lp_build_context *bld,
427 const struct util_format_description *format_desc,
428 LLVMValueRef x,
429 LLVMValueRef y,
430 LLVMValueRef z,
431 LLVMValueRef y_stride,
432 LLVMValueRef z_stride,
433 LLVMValueRef *out_offset,
434 LLVMValueRef *out_i,
435 LLVMValueRef *out_j);
436
437
438 void
439 lp_build_sample_soa(struct gallivm_state *gallivm,
440 const struct lp_sampler_static_state *static_state,
441 struct lp_sampler_dynamic_state *dynamic_state,
442 struct lp_type fp_type,
443 boolean is_fetch,
444 unsigned unit,
445 const LLVMValueRef *coords,
446 const LLVMValueRef *offsets,
447 const struct lp_derivatives *derivs,
448 LLVMValueRef lod_bias,
449 LLVMValueRef explicit_lod,
450 LLVMValueRef texel_out[4]);
451
452
453 void
454 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context *bld,
455 LLVMValueRef coord_f,
456 LLVMValueRef length_i,
457 LLVMValueRef length_f,
458 LLVMValueRef *coord0_i,
459 LLVMValueRef *weight_f);
460
461
462 void
463 lp_build_size_query_soa(struct gallivm_state *gallivm,
464 const struct lp_sampler_static_state *static_state,
465 struct lp_sampler_dynamic_state *dynamic_state,
466 struct lp_type int_type,
467 unsigned unit,
468 LLVMValueRef explicit_lod,
469 LLVMValueRef *sizes_out);
470
471 void
472 lp_build_sample_nop(struct gallivm_state *gallivm,
473 struct lp_type type,
474 const LLVMValueRef *coords,
475 LLVMValueRef texel_out[4]);
476
477
478 LLVMValueRef
479 lp_build_minify(struct lp_build_context *bld,
480 LLVMValueRef base_size,
481 LLVMValueRef level);
482
483
484 #endif /* LP_BLD_SAMPLE_H */