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