lp_bld_sample.h revision 34c11c87e4e3b5639764abee413c45e918749477
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
46struct pipe_resource;
47struct pipe_sampler_view;
48struct pipe_sampler_state;
49struct util_format_description;
50struct lp_type;
51struct lp_build_context;
52
53
54/**
55 * Sampler static state.
56 *
57 * These are the bits of state from pipe_resource and pipe_sampler_state that
58 * are embedded in the generated code.
59 */
60struct lp_sampler_static_state
61{
62   /* pipe_sampler_view's state */
63   enum pipe_format format;
64   unsigned swizzle_r:3;     /**< PIPE_SWIZZLE_* */
65   unsigned swizzle_g:3;
66   unsigned swizzle_b:3;
67   unsigned swizzle_a:3;
68
69   /* pipe_texture's state */
70   unsigned target:3;        /**< PIPE_TEXTURE_* */
71   unsigned pot_width:1;     /**< is the width a power of two? */
72   unsigned pot_height:1;
73   unsigned pot_depth:1;
74
75   /* pipe_sampler_state's state */
76   unsigned wrap_s:3;
77   unsigned wrap_t:3;
78   unsigned wrap_r:3;
79   unsigned min_img_filter:2;
80   unsigned min_mip_filter:2;
81   unsigned mag_img_filter:2;
82   unsigned compare_mode:1;
83   unsigned compare_func:3;
84   unsigned normalized_coords:1;
85   unsigned min_max_lod_equal:1;  /**< min_lod == max_lod ? */
86   unsigned lod_bias_non_zero:1;
87   unsigned apply_min_lod:1;  /**< min_lod > 0 ? */
88   unsigned apply_max_lod:1;  /**< max_lod < last_level ? */
89};
90
91
92/**
93 * Sampler dynamic state.
94 *
95 * These are the bits of state from pipe_resource and pipe_sampler_state that
96 * are computed in runtime.
97 *
98 * There are obtained through callbacks, as we don't want to tie the texture
99 * sampling code generation logic to any particular texture layout or pipe
100 * driver.
101 */
102struct lp_sampler_dynamic_state
103{
104
105   /** Obtain the base texture width (returns int32) */
106   LLVMValueRef
107   (*width)( const struct lp_sampler_dynamic_state *state,
108             LLVMBuilderRef builder,
109             unsigned unit);
110
111   /** Obtain the base texture height (returns int32) */
112   LLVMValueRef
113   (*height)( const struct lp_sampler_dynamic_state *state,
114              LLVMBuilderRef builder,
115              unsigned unit);
116
117   /** Obtain the base texture depth (returns int32) */
118   LLVMValueRef
119   (*depth)( const struct lp_sampler_dynamic_state *state,
120             LLVMBuilderRef builder,
121             unsigned unit);
122
123   /** Obtain the number of mipmap levels minus one (returns int32) */
124   LLVMValueRef
125   (*last_level)( const struct lp_sampler_dynamic_state *state,
126                  LLVMBuilderRef builder,
127                  unsigned unit);
128
129   /** Obtain stride in bytes between image rows/blocks (returns int32) */
130   LLVMValueRef
131   (*row_stride)( const struct lp_sampler_dynamic_state *state,
132                  LLVMBuilderRef builder,
133                  unsigned unit);
134
135   /** Obtain stride in bytes between image slices (returns int32) */
136   LLVMValueRef
137   (*img_stride)( const struct lp_sampler_dynamic_state *state,
138                  LLVMBuilderRef builder,
139                  unsigned unit);
140
141   /** Obtain pointer to array of pointers to mimpap levels */
142   LLVMValueRef
143   (*data_ptr)( const struct lp_sampler_dynamic_state *state,
144                LLVMBuilderRef builder,
145                unsigned unit);
146
147   /** Obtain texture min lod (returns float) */
148   LLVMValueRef
149   (*min_lod)(const struct lp_sampler_dynamic_state *state,
150              LLVMBuilderRef builder, unsigned unit);
151
152   /** Obtain texture max lod (returns float) */
153   LLVMValueRef
154   (*max_lod)(const struct lp_sampler_dynamic_state *state,
155              LLVMBuilderRef builder, unsigned unit);
156
157   /** Obtain texture lod bias (returns float) */
158   LLVMValueRef
159   (*lod_bias)(const struct lp_sampler_dynamic_state *state,
160               LLVMBuilderRef builder, unsigned unit);
161
162   /** Obtain texture border color (returns ptr to float[4]) */
163   LLVMValueRef
164   (*border_color)(const struct lp_sampler_dynamic_state *state,
165                   LLVMBuilderRef builder, unsigned unit);
166};
167
168
169/**
170 * Keep all information for sampling code generation in a single place.
171 */
172struct lp_build_sample_context
173{
174   LLVMBuilderRef builder;
175
176   const struct lp_sampler_static_state *static_state;
177
178   struct lp_sampler_dynamic_state *dynamic_state;
179
180   const struct util_format_description *format_desc;
181
182   /* See texture_dims() */
183   unsigned dims;
184
185   /** regular scalar float type */
186   struct lp_type float_type;
187   struct lp_build_context float_bld;
188
189   /** float vector type */
190   struct lp_build_context float_vec_bld;
191
192   /** regular scalar float type */
193   struct lp_type int_type;
194   struct lp_build_context int_bld;
195
196   /** Incoming coordinates type and build context */
197   struct lp_type coord_type;
198   struct lp_build_context coord_bld;
199
200   /** Unsigned integer coordinates */
201   struct lp_type uint_coord_type;
202   struct lp_build_context uint_coord_bld;
203
204   /** Signed integer coordinates */
205   struct lp_type int_coord_type;
206   struct lp_build_context int_coord_bld;
207
208   /** Unsigned integer texture size */
209   struct lp_type int_size_type;
210   struct lp_build_context int_size_bld;
211
212   /** Unsigned integer texture size */
213   struct lp_type float_size_type;
214   struct lp_build_context float_size_bld;
215
216   /** Output texels type and build context */
217   struct lp_type texel_type;
218   struct lp_build_context texel_bld;
219
220   /* Common dynamic state values */
221   LLVMValueRef width;
222   LLVMValueRef height;
223   LLVMValueRef depth;
224   LLVMValueRef row_stride_array;
225   LLVMValueRef img_stride_array;
226   LLVMValueRef data_array;
227
228   /** Integer vector with texture width, height, depth */
229   LLVMValueRef int_size;
230};
231
232
233
234/**
235 * We only support a few wrap modes in lp_build_sample_wrap_linear_int() at
236 * this time.  Return whether the given mode is supported by that function.
237 */
238static INLINE boolean
239lp_is_simple_wrap_mode(unsigned mode)
240{
241   switch (mode) {
242   case PIPE_TEX_WRAP_REPEAT:
243   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
244      return TRUE;
245   default:
246      return FALSE;
247   }
248}
249
250
251static INLINE void
252apply_sampler_swizzle(struct lp_build_sample_context *bld,
253                      LLVMValueRef *texel)
254{
255   unsigned char swizzles[4];
256
257   swizzles[0] = bld->static_state->swizzle_r;
258   swizzles[1] = bld->static_state->swizzle_g;
259   swizzles[2] = bld->static_state->swizzle_b;
260   swizzles[3] = bld->static_state->swizzle_a;
261
262   lp_build_swizzle_soa_inplace(&bld->texel_bld, texel, swizzles);
263}
264
265
266static INLINE unsigned
267texture_dims(enum pipe_texture_target tex)
268{
269   switch (tex) {
270   case PIPE_TEXTURE_1D:
271      return 1;
272   case PIPE_TEXTURE_2D:
273   case PIPE_TEXTURE_RECT:
274   case PIPE_TEXTURE_CUBE:
275      return 2;
276   case PIPE_TEXTURE_3D:
277      return 3;
278   default:
279      assert(0 && "bad texture target in texture_dims()");
280      return 2;
281   }
282}
283
284
285boolean
286lp_sampler_wrap_mode_uses_border_color(unsigned mode,
287                                       unsigned min_img_filter,
288                                       unsigned mag_img_filter);
289
290/**
291 * Derive the sampler static state.
292 */
293void
294lp_sampler_static_state(struct lp_sampler_static_state *state,
295                        const struct pipe_sampler_view *view,
296                        const struct pipe_sampler_state *sampler);
297
298
299void
300lp_build_lod_selector(struct lp_build_sample_context *bld,
301                      unsigned unit,
302                      const LLVMValueRef ddx[4],
303                      const LLVMValueRef ddy[4],
304                      LLVMValueRef lod_bias, /* optional */
305                      LLVMValueRef explicit_lod, /* optional */
306                      unsigned mip_filter,
307                      LLVMValueRef *out_lod_ipart,
308                      LLVMValueRef *out_lod_fpart);
309
310void
311lp_build_nearest_mip_level(struct lp_build_sample_context *bld,
312                           unsigned unit,
313                           LLVMValueRef lod,
314                           LLVMValueRef *level_out);
315
316void
317lp_build_linear_mip_levels(struct lp_build_sample_context *bld,
318                           unsigned unit,
319                           LLVMValueRef lod_ipart,
320                           LLVMValueRef *lod_fpart_inout,
321                           LLVMValueRef *level0_out,
322                           LLVMValueRef *level1_out);
323
324LLVMValueRef
325lp_build_get_mipmap_level(struct lp_build_sample_context *bld,
326                          LLVMValueRef level);
327
328LLVMValueRef
329lp_build_get_const_mipmap_level(struct lp_build_sample_context *bld,
330                                int level);
331
332
333void
334lp_build_mipmap_level_sizes(struct lp_build_sample_context *bld,
335                            LLVMValueRef ilevel,
336                            LLVMValueRef *out_size_vec,
337                            LLVMValueRef *row_stride_vec,
338                            LLVMValueRef *img_stride_vec);
339
340
341void
342lp_build_extract_image_sizes(struct lp_build_sample_context *bld,
343                             struct lp_type size_type,
344                             struct lp_type coord_type,
345                             LLVMValueRef size,
346                             LLVMValueRef *out_width,
347                             LLVMValueRef *out_height,
348                             LLVMValueRef *out_depth);
349
350
351void
352lp_build_unnormalized_coords(struct lp_build_sample_context *bld,
353                             LLVMValueRef flt_size,
354                             LLVMValueRef *s,
355                             LLVMValueRef *t,
356                             LLVMValueRef *r);
357
358
359void
360lp_build_cube_lookup(struct lp_build_sample_context *bld,
361                     LLVMValueRef s,
362                     LLVMValueRef t,
363                     LLVMValueRef r,
364                     LLVMValueRef *face,
365                     LLVMValueRef *face_s,
366                     LLVMValueRef *face_t);
367
368
369void
370lp_build_sample_partial_offset(struct lp_build_context *bld,
371                               unsigned block_length,
372                               LLVMValueRef coord,
373                               LLVMValueRef stride,
374                               LLVMValueRef *out_offset,
375                               LLVMValueRef *out_i);
376
377
378void
379lp_build_sample_offset(struct lp_build_context *bld,
380                       const struct util_format_description *format_desc,
381                       LLVMValueRef x,
382                       LLVMValueRef y,
383                       LLVMValueRef z,
384                       LLVMValueRef y_stride,
385                       LLVMValueRef z_stride,
386                       LLVMValueRef *out_offset,
387                       LLVMValueRef *out_i,
388                       LLVMValueRef *out_j);
389
390
391void
392lp_build_sample_soa(LLVMBuilderRef builder,
393                    const struct lp_sampler_static_state *static_state,
394                    struct lp_sampler_dynamic_state *dynamic_state,
395                    struct lp_type fp_type,
396                    unsigned unit,
397                    unsigned num_coords,
398                    const LLVMValueRef *coords,
399                    const LLVMValueRef *ddx,
400                    const LLVMValueRef *ddy,
401                    LLVMValueRef lod_bias,
402                    LLVMValueRef explicit_lod,
403                    LLVMValueRef texel_out[4]);
404
405void
406lp_build_sample_nop(struct lp_type type,
407                    LLVMValueRef texel_out[4]);
408
409
410#endif /* LP_BLD_SAMPLE_H */
411