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