Merge remote branch 'origin/master' into nv50-compiler
[mesa.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 #define DRAW_MAX_TEXTURE_LEVELS 13 /* 4K x 4K for now */
45
46 struct draw_llvm;
47 struct llvm_vertex_shader;
48
49 struct draw_jit_texture
50 {
51 uint32_t width;
52 uint32_t height;
53 uint32_t depth;
54 uint32_t last_level;
55 uint32_t row_stride[DRAW_MAX_TEXTURE_LEVELS];
56 uint32_t img_stride[DRAW_MAX_TEXTURE_LEVELS];
57 const void *data[DRAW_MAX_TEXTURE_LEVELS];
58 };
59
60 enum {
61 DRAW_JIT_TEXTURE_WIDTH = 0,
62 DRAW_JIT_TEXTURE_HEIGHT,
63 DRAW_JIT_TEXTURE_DEPTH,
64 DRAW_JIT_TEXTURE_LAST_LEVEL,
65 DRAW_JIT_TEXTURE_ROW_STRIDE,
66 DRAW_JIT_TEXTURE_IMG_STRIDE,
67 DRAW_JIT_TEXTURE_DATA,
68 DRAW_JIT_TEXTURE_NUM_FIELDS /* number of fields above */
69 };
70
71 enum {
72 DRAW_JIT_VERTEX_VERTEX_ID = 0,
73 DRAW_JIT_VERTEX_CLIP,
74 DRAW_JIT_VERTEX_DATA
75 };
76
77 /**
78 * This structure is passed directly to the generated vertex shader.
79 *
80 * It contains the derived state.
81 *
82 * Changes here must be reflected in the draw_jit_context_* macros.
83 * Changes to the ordering should be avoided.
84 *
85 * Only use types with a clear size and padding here, in particular prefer the
86 * stdint.h types to the basic integer types.
87 */
88 struct draw_jit_context
89 {
90 const float *vs_constants;
91 const float *gs_constants;
92
93
94 struct draw_jit_texture textures[PIPE_MAX_VERTEX_SAMPLERS];
95 };
96
97
98 #define draw_jit_context_vs_constants(_builder, _ptr) \
99 lp_build_struct_get(_builder, _ptr, 0, "vs_constants")
100
101 #define draw_jit_context_gs_constants(_builder, _ptr) \
102 lp_build_struct_get(_builder, _ptr, 1, "gs_constants")
103
104 #define DRAW_JIT_CTX_TEXTURES 2
105
106 #define draw_jit_context_textures(_builder, _ptr) \
107 lp_build_struct_get_ptr(_builder, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
108
109
110
111 #define draw_jit_header_id(_builder, _ptr) \
112 lp_build_struct_get_ptr(_builder, _ptr, 0, "id")
113
114 #define draw_jit_header_clip(_builder, _ptr) \
115 lp_build_struct_get(_builder, _ptr, 1, "clip")
116
117 #define draw_jit_header_data(_builder, _ptr) \
118 lp_build_struct_get_ptr(_builder, _ptr, 2, "data")
119
120
121 #define draw_jit_vbuffer_stride(_builder, _ptr) \
122 lp_build_struct_get(_builder, _ptr, 0, "stride")
123
124 #define draw_jit_vbuffer_max_index(_builder, _ptr) \
125 lp_build_struct_get(_builder, _ptr, 1, "max_index")
126
127 #define draw_jit_vbuffer_offset(_builder, _ptr) \
128 lp_build_struct_get(_builder, _ptr, 2, "buffer_offset")
129
130
131 typedef void
132 (*draw_jit_vert_func)(struct draw_jit_context *context,
133 struct vertex_header *io,
134 const char *vbuffers[PIPE_MAX_ATTRIBS],
135 unsigned start,
136 unsigned count,
137 unsigned stride,
138 struct pipe_vertex_buffer *vertex_buffers,
139 unsigned instance_id);
140
141
142 typedef void
143 (*draw_jit_vert_func_elts)(struct draw_jit_context *context,
144 struct vertex_header *io,
145 const char *vbuffers[PIPE_MAX_ATTRIBS],
146 const unsigned *fetch_elts,
147 unsigned fetch_count,
148 unsigned stride,
149 struct pipe_vertex_buffer *vertex_buffers,
150 unsigned instance_id);
151
152 struct draw_llvm_variant_key
153 {
154 unsigned nr_vertex_elements:16;
155 unsigned nr_samplers:16;
156
157 /* Variable number of vertex elements:
158 */
159 struct pipe_vertex_element vertex_element[1];
160
161 /* Followed by variable number of samplers:
162 */
163 /* struct lp_sampler_static_state sampler; */
164 };
165
166 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
167 (sizeof(struct draw_llvm_variant_key) + \
168 PIPE_MAX_VERTEX_SAMPLERS * sizeof(struct lp_sampler_static_state) + \
169 (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
170
171
172 static INLINE size_t
173 draw_llvm_variant_key_size(unsigned nr_vertex_elements,
174 unsigned nr_samplers)
175 {
176 return (sizeof(struct draw_llvm_variant_key) +
177 nr_samplers * sizeof(struct lp_sampler_static_state) +
178 (nr_vertex_elements - 1) * sizeof(struct pipe_vertex_element));
179 }
180
181
182 static INLINE struct lp_sampler_static_state *
183 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key *key)
184 {
185 return (struct lp_sampler_static_state *)
186 &key->vertex_element[key->nr_vertex_elements];
187 }
188
189
190
191 struct draw_llvm_variant_list_item
192 {
193 struct draw_llvm_variant *base;
194 struct draw_llvm_variant_list_item *next, *prev;
195 };
196
197 struct draw_llvm_variant
198 {
199 LLVMValueRef function;
200 LLVMValueRef function_elts;
201 draw_jit_vert_func jit_func;
202 draw_jit_vert_func_elts jit_func_elts;
203
204 struct llvm_vertex_shader *shader;
205
206 struct draw_llvm *llvm;
207 struct draw_llvm_variant_list_item list_item_global;
208 struct draw_llvm_variant_list_item list_item_local;
209
210 /* key is variable-sized, must be last */
211 struct draw_llvm_variant_key key;
212 /* key is variable-sized, must be last */
213 };
214
215 struct llvm_vertex_shader {
216 struct draw_vertex_shader base;
217
218 unsigned variant_key_size;
219 struct draw_llvm_variant_list_item variants;
220 unsigned variants_created;
221 unsigned variants_cached;
222 };
223
224 struct draw_llvm {
225 struct draw_context *draw;
226
227 struct draw_jit_context jit_context;
228
229 struct draw_llvm_variant_list_item vs_variants_list;
230 int nr_variants;
231
232 LLVMModuleRef module;
233 LLVMExecutionEngineRef engine;
234 LLVMModuleProviderRef provider;
235 LLVMTargetDataRef target;
236 LLVMPassManagerRef pass;
237
238 LLVMTypeRef context_ptr_type;
239 LLVMTypeRef vertex_header_ptr_type;
240 LLVMTypeRef buffer_ptr_type;
241 LLVMTypeRef vb_ptr_type;
242 };
243
244 static INLINE struct llvm_vertex_shader *
245 llvm_vertex_shader(struct draw_vertex_shader *vs)
246 {
247 return (struct llvm_vertex_shader *)vs;
248 }
249
250
251 struct draw_llvm *
252 draw_llvm_create(struct draw_context *draw);
253
254 void
255 draw_llvm_destroy(struct draw_llvm *llvm);
256
257 struct draw_llvm_variant *
258 draw_llvm_create_variant(struct draw_llvm *llvm,
259 unsigned num_vertex_header_attribs,
260 const struct draw_llvm_variant_key *key);
261
262 void
263 draw_llvm_destroy_variant(struct draw_llvm_variant *variant);
264
265 struct draw_llvm_variant_key *
266 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
267
268 LLVMValueRef
269 draw_llvm_translate_from(LLVMBuilderRef builder,
270 LLVMValueRef vbuffer,
271 enum pipe_format from_format);
272
273 struct lp_build_sampler_soa *
274 draw_llvm_sampler_soa_create(const struct lp_sampler_static_state *static_state,
275 LLVMValueRef context_ptr);
276
277 void
278 draw_llvm_set_mapped_texture(struct draw_context *draw,
279 unsigned sampler_idx,
280 uint32_t width, uint32_t height, uint32_t depth,
281 uint32_t last_level,
282 uint32_t row_stride[DRAW_MAX_TEXTURE_LEVELS],
283 uint32_t img_stride[DRAW_MAX_TEXTURE_LEVELS],
284 const void *data[DRAW_MAX_TEXTURE_LEVELS]);
285
286 #endif