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