Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / auxiliary / draw / draw_llvm.h
1 /**************************************************************************
2  *
3  * Copyright 2010 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 #ifndef DRAW_LLVM_H
29 #define DRAW_LLVM_H
30
31 #include "draw/draw_private.h"
32
33 #include "draw/draw_vs.h"
34 #include "gallivm/lp_bld_sample.h"
35
36 #include "pipe/p_context.h"
37 #include "util/u_simple_list.h"
38
39 #include <llvm-c/Core.h>
40 #include <llvm-c/Analysis.h>
41 #include <llvm-c/Target.h>
42 #include <llvm-c/ExecutionEngine.h>
43
44
45 struct draw_llvm;
46 struct llvm_vertex_shader;
47
48 struct draw_jit_texture
49 {
50    uint32_t width;
51    uint32_t height;
52    uint32_t depth;
53    uint32_t first_level;
54    uint32_t last_level;
55    uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
56    uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
57    const void *data[PIPE_MAX_TEXTURE_LEVELS];
58    float min_lod;
59    float max_lod;
60    float lod_bias;
61    float border_color[4];
62 };
63
64 enum {
65    DRAW_JIT_TEXTURE_WIDTH = 0,
66    DRAW_JIT_TEXTURE_HEIGHT,
67    DRAW_JIT_TEXTURE_DEPTH,
68    DRAW_JIT_TEXTURE_FIRST_LEVEL,
69    DRAW_JIT_TEXTURE_LAST_LEVEL,
70    DRAW_JIT_TEXTURE_ROW_STRIDE,
71    DRAW_JIT_TEXTURE_IMG_STRIDE,
72    DRAW_JIT_TEXTURE_DATA,
73    DRAW_JIT_TEXTURE_MIN_LOD,
74    DRAW_JIT_TEXTURE_MAX_LOD,
75    DRAW_JIT_TEXTURE_LOD_BIAS,
76    DRAW_JIT_TEXTURE_BORDER_COLOR,
77    DRAW_JIT_TEXTURE_NUM_FIELDS  /* number of fields above */
78 };
79
80 enum {
81    DRAW_JIT_VERTEX_VERTEX_ID = 0,
82    DRAW_JIT_VERTEX_CLIP,
83    DRAW_JIT_VERTEX_DATA
84 };
85
86 /**
87  * This structure is passed directly to the generated vertex shader.
88  *
89  * It contains the derived state.
90  *
91  * Changes here must be reflected in the draw_jit_context_* macros.
92  * Changes to the ordering should be avoided.
93  *
94  * Only use types with a clear size and padding here, in particular prefer the
95  * stdint.h types to the basic integer types.
96  */
97 struct draw_jit_context
98 {
99    const float *vs_constants;
100    const float *gs_constants;
101    float (*planes) [12][4];
102    float *viewport;
103
104    struct draw_jit_texture textures[PIPE_MAX_VERTEX_SAMPLERS];
105 };
106
107
108 #define draw_jit_context_vs_constants(_gallivm, _ptr) \
109    lp_build_struct_get(_gallivm, _ptr, 0, "vs_constants")
110
111 #define draw_jit_context_gs_constants(_gallivm, _ptr) \
112    lp_build_struct_get(_gallivm, _ptr, 1, "gs_constants")
113
114 #define draw_jit_context_planes(_gallivm, _ptr) \
115    lp_build_struct_get(_gallivm, _ptr, 2, "planes")
116
117 #define draw_jit_context_viewport(_gallivm, _ptr) \
118    lp_build_struct_get(_gallivm, _ptr, 3, "viewport")
119
120 #define DRAW_JIT_CTX_TEXTURES 4
121
122 #define draw_jit_context_textures(_gallivm, _ptr) \
123    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
124
125 #define draw_jit_header_id(_gallivm, _ptr)              \
126    lp_build_struct_get_ptr(_gallivm, _ptr, 0, "id")
127
128 #define draw_jit_header_clip(_gallivm, _ptr) \
129    lp_build_struct_get_ptr(_gallivm, _ptr, 1, "clip")
130
131 #define draw_jit_header_data(_gallivm, _ptr)            \
132    lp_build_struct_get_ptr(_gallivm, _ptr, 2, "data")
133
134
135 #define draw_jit_vbuffer_stride(_gallivm, _ptr)         \
136    lp_build_struct_get(_gallivm, _ptr, 0, "stride")
137
138 #define draw_jit_vbuffer_offset(_gallivm, _ptr)         \
139    lp_build_struct_get(_gallivm, _ptr, 1, "buffer_offset")
140
141
142 typedef int
143 (*draw_jit_vert_func)(struct draw_jit_context *context,
144                       struct vertex_header *io,
145                       const char *vbuffers[PIPE_MAX_ATTRIBS],
146                       unsigned start,
147                       unsigned count,
148                       unsigned stride,
149                       struct pipe_vertex_buffer *vertex_buffers,
150                       unsigned instance_id);
151
152
153 typedef int
154 (*draw_jit_vert_func_elts)(struct draw_jit_context *context,
155                            struct vertex_header *io,
156                            const char *vbuffers[PIPE_MAX_ATTRIBS],
157                            const unsigned *fetch_elts,
158                            unsigned fetch_count,
159                            unsigned stride,
160                            struct pipe_vertex_buffer *vertex_buffers,
161                            unsigned instance_id);
162
163 struct draw_llvm_variant_key
164 {
165    unsigned nr_vertex_elements:8;
166    unsigned nr_samplers:8;
167    unsigned clamp_vertex_color:1;
168    unsigned clip_xy:1;
169    unsigned clip_z:1;
170    unsigned clip_user:1;
171    unsigned clip_halfz:1;
172    unsigned bypass_viewport:1;
173    unsigned need_edgeflags:1;
174    unsigned nr_planes:4;
175    unsigned pad:5;
176
177    /* Variable number of vertex elements:
178     */
179    struct pipe_vertex_element vertex_element[1];
180
181    /* Followed by variable number of samplers:
182     */
183 /*   struct lp_sampler_static_state sampler; */
184 };
185
186 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
187    (sizeof(struct draw_llvm_variant_key) +      \
188     PIPE_MAX_VERTEX_SAMPLERS * sizeof(struct lp_sampler_static_state) + \
189     (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
190
191
192 static INLINE size_t
193 draw_llvm_variant_key_size(unsigned nr_vertex_elements,
194                            unsigned nr_samplers)
195 {
196    return (sizeof(struct draw_llvm_variant_key) +
197            nr_samplers * sizeof(struct lp_sampler_static_state) +
198            (nr_vertex_elements - 1) * sizeof(struct pipe_vertex_element));
199 }
200
201
202 static INLINE struct lp_sampler_static_state *
203 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key *key)
204 {
205    return (struct lp_sampler_static_state *)
206       &key->vertex_element[key->nr_vertex_elements];
207 }
208
209
210
211 struct draw_llvm_variant_list_item
212 {
213    struct draw_llvm_variant *base;
214    struct draw_llvm_variant_list_item *next, *prev;
215 };
216
217 struct draw_llvm_variant
218 {
219    LLVMValueRef function;
220    LLVMValueRef function_elts;
221    draw_jit_vert_func jit_func;
222    draw_jit_vert_func_elts jit_func_elts;
223
224    struct llvm_vertex_shader *shader;
225
226    struct draw_llvm *llvm;
227    struct draw_llvm_variant_list_item list_item_global;
228    struct draw_llvm_variant_list_item list_item_local;
229
230    /* key is variable-sized, must be last */
231    struct draw_llvm_variant_key key;
232 };
233
234 struct llvm_vertex_shader {
235    struct draw_vertex_shader base;
236
237    unsigned variant_key_size;
238    struct draw_llvm_variant_list_item variants;
239    unsigned variants_created;
240    unsigned variants_cached;
241 };
242
243 struct draw_llvm {
244    struct draw_context *draw;
245
246    struct draw_jit_context jit_context;
247
248    struct gallivm_state *gallivm;
249
250    struct draw_llvm_variant_list_item vs_variants_list;
251    int nr_variants;
252
253    /* LLVM JIT builder types */
254    LLVMTypeRef context_ptr_type;
255    LLVMTypeRef buffer_ptr_type;
256    LLVMTypeRef vb_ptr_type;
257    LLVMTypeRef vertex_header_ptr_type;
258 };
259
260
261 static INLINE struct llvm_vertex_shader *
262 llvm_vertex_shader(struct draw_vertex_shader *vs)
263 {
264    return (struct llvm_vertex_shader *)vs;
265 }
266
267
268 struct draw_llvm *
269 draw_llvm_create(struct draw_context *draw, struct gallivm_state *gallivm);
270
271 void
272 draw_llvm_destroy(struct draw_llvm *llvm);
273
274 struct draw_llvm_variant *
275 draw_llvm_create_variant(struct draw_llvm *llvm,
276                          unsigned num_vertex_header_attribs,
277                          const struct draw_llvm_variant_key *key);
278
279 void
280 draw_llvm_destroy_variant(struct draw_llvm_variant *variant);
281
282 struct draw_llvm_variant_key *
283 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
284
285 LLVMValueRef
286 draw_llvm_translate_from(struct gallivm_state *gallivm,
287                          LLVMValueRef vbuffer,
288                          enum pipe_format from_format);
289
290 struct lp_build_sampler_soa *
291 draw_llvm_sampler_soa_create(const struct lp_sampler_static_state *static_state,
292                              LLVMValueRef context_ptr);
293
294 void
295 draw_llvm_set_sampler_state(struct draw_context *draw);
296
297 void
298 draw_llvm_set_mapped_texture(struct draw_context *draw,
299                              unsigned sampler_idx,
300                              uint32_t width, uint32_t height, uint32_t depth,
301                              uint32_t first_level, uint32_t last_level,
302                              uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
303                              uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
304                              const void *data[PIPE_MAX_TEXTURE_LEVELS]);
305
306 #endif