d8a068d5497a3f418549bae708016956b2df8fa6
[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 /** regular scalar float type */
214 struct lp_type float_type;
215 struct lp_build_context float_bld;
216
217 /** float vector type */
218 struct lp_build_context float_vec_bld;
219
220 /** regular scalar int type */
221 struct lp_type int_type;
222 struct lp_build_context int_bld;
223
224 /** Incoming coordinates type and build context */
225 struct lp_type coord_type;
226 struct lp_build_context coord_bld;
227
228 /** Signed integer coordinates */
229 struct lp_type int_coord_type;
230 struct lp_build_context int_coord_bld;
231
232 /** Unsigned integer texture size */
233 struct lp_type int_size_type;
234 struct lp_build_context int_size_bld;
235
236 /** Unsigned integer texture size */
237 struct lp_type float_size_type;
238 struct lp_build_context float_size_bld;
239
240 /** Output texels type and build context */
241 struct lp_type texel_type;
242 struct lp_build_context texel_bld;
243
244 /** Float per-quad type */
245 struct lp_type perquadf_type;
246 struct lp_build_context perquadf_bld;
247
248 /** Int per-quad type */
249 struct lp_type perquadi_type;
250 struct lp_build_context perquadi_bld;
251
252 /* Common dynamic state values */
253 LLVMValueRef row_stride_array;
254 LLVMValueRef img_stride_array;
255 LLVMValueRef base_ptr;
256 LLVMValueRef mip_offsets;
257
258 /** Integer vector with texture width, height, depth */
259 LLVMValueRef int_size;
260 };
261
262
263
264 /**
265 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
266 * this time. Return whether the given mode is supported by that function.
267 */
268 static INLINE boolean
269 lp_is_simple_wrap_mode(unsigned mode)
270 {
271 switch (mode) {
272 case PIPE_TEX_WRAP_REPEAT:
273 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
274 return TRUE;
275 default:
276 return FALSE;
277 }
278 }
279
280
281 static INLINE void
282 apply_sampler_swizzle(struct lp_build_sample_context *bld,
283 LLVMValueRef *texel)
284 {
285 unsigned char swizzles[4];
286
287 swizzles[0] = bld->static_state->swizzle_r;
288 swizzles[1] = bld->static_state->swizzle_g;
289 swizzles[2] = bld->static_state->swizzle_b;
290 swizzles[3] = bld->static_state->swizzle_a;
291
292 lp_build_swizzle_soa_inplace(&bld->texel_bld, texel, swizzles);
293 }
294
295
296 static INLINE unsigned
297 texture_dims(enum pipe_texture_target tex)
298 {
299 switch (tex) {
300 case PIPE_TEXTURE_1D:
301 return 1;
302 case PIPE_TEXTURE_2D:
303 case PIPE_TEXTURE_RECT:
304 case PIPE_TEXTURE_CUBE:
305 return 2;
306 case PIPE_TEXTURE_3D:
307 return 3;
308 default:
309 assert(0 && "bad texture target in texture_dims()");
310 return 2;
311 }
312 }
313
314
315 boolean
316 lp_sampler_wrap_mode_uses_border_color(unsigned mode,
317 unsigned min_img_filter,
318 unsigned mag_img_filter);
319
320 /**
321 * Derive the sampler static state.
322 */
323 void
324 lp_sampler_static_state(struct lp_sampler_static_state *state,
325 const struct pipe_sampler_view *view,
326 const struct pipe_sampler_state *sampler);
327
328
329 void
330 lp_build_lod_selector(struct lp_build_sample_context *bld,
331 unsigned unit,
332 const struct lp_derivatives *derivs,
333 LLVMValueRef lod_bias, /* optional */
334 LLVMValueRef explicit_lod, /* optional */
335 unsigned mip_filter,
336 LLVMValueRef *out_lod_ipart,
337 LLVMValueRef *out_lod_fpart);
338
339 void
340 lp_build_nearest_mip_level(struct lp_build_sample_context *bld,
341 unsigned unit,
342 LLVMValueRef lod,
343 LLVMValueRef *level_out);
344
345 void
346 lp_build_linear_mip_levels(struct lp_build_sample_context *bld,
347 unsigned unit,
348 LLVMValueRef lod_ipart,
349 LLVMValueRef *lod_fpart_inout,
350 LLVMValueRef *level0_out,
351 LLVMValueRef *level1_out);
352
353 LLVMValueRef
354 lp_build_get_mipmap_level(struct lp_build_sample_context *bld,
355 LLVMValueRef level);
356
357
358 void
359 lp_build_mipmap_level_sizes(struct lp_build_sample_context *bld,
360 LLVMValueRef ilevel,
361 LLVMValueRef *out_size_vec,
362 LLVMValueRef *row_stride_vec,
363 LLVMValueRef *img_stride_vec);
364
365
366 void
367 lp_build_extract_image_sizes(struct lp_build_sample_context *bld,
368 struct lp_type size_type,
369 struct lp_type coord_type,
370 LLVMValueRef size,
371 LLVMValueRef *out_width,
372 LLVMValueRef *out_height,
373 LLVMValueRef *out_depth);
374
375
376 void
377 lp_build_unnormalized_coords(struct lp_build_sample_context *bld,
378 LLVMValueRef flt_size,
379 LLVMValueRef *s,
380 LLVMValueRef *t,
381 LLVMValueRef *r);
382
383
384 void
385 lp_build_cube_lookup(struct lp_build_sample_context *bld,
386 LLVMValueRef s,
387 LLVMValueRef t,
388 LLVMValueRef r,
389 LLVMValueRef *face,
390 LLVMValueRef *face_s,
391 LLVMValueRef *face_t);
392
393
394 void
395 lp_build_sample_partial_offset(struct lp_build_context *bld,
396 unsigned block_length,
397 LLVMValueRef coord,
398 LLVMValueRef stride,
399 LLVMValueRef *out_offset,
400 LLVMValueRef *out_i);
401
402
403 void
404 lp_build_sample_offset(struct lp_build_context *bld,
405 const struct util_format_description *format_desc,
406 LLVMValueRef x,
407 LLVMValueRef y,
408 LLVMValueRef z,
409 LLVMValueRef y_stride,
410 LLVMValueRef z_stride,
411 LLVMValueRef *out_offset,
412 LLVMValueRef *out_i,
413 LLVMValueRef *out_j);
414
415
416 void
417 lp_build_sample_soa(struct gallivm_state *gallivm,
418 const struct lp_sampler_static_state *static_state,
419 struct lp_sampler_dynamic_state *dynamic_state,
420 struct lp_type fp_type,
421 unsigned unit,
422 unsigned num_coords,
423 const LLVMValueRef *coords,
424 const struct lp_derivatives *derivs,
425 LLVMValueRef lod_bias,
426 LLVMValueRef explicit_lod,
427 LLVMValueRef texel_out[4]);
428
429
430 void
431 lp_build_coord_repeat_npot_linear(struct lp_build_sample_context *bld,
432 LLVMValueRef coord_f,
433 LLVMValueRef length_i,
434 LLVMValueRef length_f,
435 LLVMValueRef *coord0_i,
436 LLVMValueRef *weight_f);
437
438
439 void
440 lp_build_size_query_soa(struct gallivm_state *gallivm,
441 const struct lp_sampler_static_state *static_state,
442 struct lp_sampler_dynamic_state *dynamic_state,
443 struct lp_type int_type,
444 unsigned unit,
445 LLVMValueRef explicit_lod,
446 LLVMValueRef *sizes_out);
447
448 void
449 lp_build_sample_nop(struct gallivm_state *gallivm,
450 struct lp_type type,
451 unsigned num_coords,
452 const LLVMValueRef *coords,
453 LLVMValueRef texel_out[4]);
454
455
456 LLVMValueRef
457 lp_build_minify(struct lp_build_context *bld,
458 LLVMValueRef base_size,
459 LLVMValueRef level);
460
461
462 #endif /* LP_BLD_SAMPLE_H */