c98d7fb393bc2c5cd2c7be02a30a7abe0798d417
[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 HAVE_LLVM_H
29 #define HAVE_LLVM_H
30
31 #include "draw/draw_private.h"
32
33 #include "draw/draw_vs.h"
34
35 #include "pipe/p_context.h"
36 #include "util/u_simple_list.h"
37
38 #include <llvm-c/Core.h>
39 #include <llvm-c/Analysis.h>
40 #include <llvm-c/Target.h>
41 #include <llvm-c/ExecutionEngine.h>
42
43 struct draw_llvm;
44 struct llvm_vertex_shader;
45
46 struct draw_jit_texture
47 {
48 uint32_t width;
49 uint32_t height;
50 uint32_t stride;
51 const void *data;
52 };
53
54 enum {
55 DRAW_JIT_TEXTURE_WIDTH = 0,
56 DRAW_JIT_TEXTURE_HEIGHT,
57 DRAW_JIT_TEXTURE_STRIDE,
58 DRAW_JIT_TEXTURE_DATA
59 };
60
61 enum {
62 DRAW_JIT_VERTEX_VERTEX_ID = 0,
63 DRAW_JIT_VERTEX_CLIP,
64 DRAW_JIT_VERTEX_DATA
65 };
66
67 /**
68 * This structure is passed directly to the generated vertex shader.
69 *
70 * It contains the derived state.
71 *
72 * Changes here must be reflected in the draw_jit_context_* macros.
73 * Changes to the ordering should be avoided.
74 *
75 * Only use types with a clear size and padding here, in particular prefer the
76 * stdint.h types to the basic integer types.
77 */
78 struct draw_jit_context
79 {
80 const float *vs_constants;
81 const float *gs_constants;
82
83
84 struct draw_jit_texture textures[PIPE_MAX_SAMPLERS];
85 };
86
87
88 #define draw_jit_context_vs_constants(_builder, _ptr) \
89 lp_build_struct_get(_builder, _ptr, 0, "vs_constants")
90
91 #define draw_jit_context_gs_constants(_builder, _ptr) \
92 lp_build_struct_get(_builder, _ptr, 1, "gs_constants")
93
94 #define DRAW_JIT_CONTEXT_TEXTURES_INDEX 2
95
96 #define draw_jit_context_textures(_builder, _ptr) \
97 lp_build_struct_get_ptr(_builder, _ptr, DRAW_JIT_CONTEXT_TEXTURES_INDEX, "textures")
98
99
100
101 #define draw_jit_header_id(_builder, _ptr) \
102 lp_build_struct_get_ptr(_builder, _ptr, 0, "id")
103
104 #define draw_jit_header_clip(_builder, _ptr) \
105 lp_build_struct_get(_builder, _ptr, 1, "clip")
106
107 #define draw_jit_header_data(_builder, _ptr) \
108 lp_build_struct_get_ptr(_builder, _ptr, 2, "data")
109
110
111 #define draw_jit_vbuffer_stride(_builder, _ptr) \
112 lp_build_struct_get(_builder, _ptr, 0, "stride")
113
114 #define draw_jit_vbuffer_max_index(_builder, _ptr) \
115 lp_build_struct_get(_builder, _ptr, 1, "max_index")
116
117 #define draw_jit_vbuffer_offset(_builder, _ptr) \
118 lp_build_struct_get(_builder, _ptr, 2, "buffer_offset")
119
120
121 typedef void
122 (*draw_jit_vert_func)(struct draw_jit_context *context,
123 struct vertex_header *io,
124 const char *vbuffers[PIPE_MAX_ATTRIBS],
125 unsigned start,
126 unsigned count,
127 unsigned stride,
128 struct pipe_vertex_buffer *vertex_buffers);
129
130
131 typedef void
132 (*draw_jit_vert_func_elts)(struct draw_jit_context *context,
133 struct vertex_header *io,
134 const char *vbuffers[PIPE_MAX_ATTRIBS],
135 const unsigned *fetch_elts,
136 unsigned fetch_count,
137 unsigned stride,
138 struct pipe_vertex_buffer *vertex_buffers);
139
140 struct draw_llvm_variant_key
141 {
142 struct pipe_vertex_element vertex_element[PIPE_MAX_ATTRIBS];
143 unsigned nr_vertex_elements;
144 struct pipe_shader_state vs;
145 };
146
147 struct draw_llvm_variant_list_item
148 {
149 struct draw_llvm_variant *base;
150 struct draw_llvm_variant_list_item *next, *prev;
151 };
152
153 struct draw_llvm_variant
154 {
155 struct draw_llvm_variant_key key;
156 LLVMValueRef function;
157 LLVMValueRef function_elts;
158 draw_jit_vert_func jit_func;
159 draw_jit_vert_func_elts jit_func_elts;
160
161 struct llvm_vertex_shader *shader;
162
163 struct draw_llvm *llvm;
164 struct draw_llvm_variant_list_item list_item_global;
165 struct draw_llvm_variant_list_item list_item_local;
166 };
167
168 struct llvm_vertex_shader {
169 struct draw_vertex_shader base;
170
171 struct draw_llvm_variant_list_item variants;
172 unsigned variants_created;
173 unsigned variants_cached;
174 };
175
176 struct draw_llvm {
177 struct draw_context *draw;
178
179 struct draw_jit_context jit_context;
180
181 struct draw_llvm_variant_list_item vs_variants_list;
182 int nr_variants;
183
184 LLVMModuleRef module;
185 LLVMExecutionEngineRef engine;
186 LLVMModuleProviderRef provider;
187 LLVMTargetDataRef target;
188 LLVMPassManagerRef pass;
189
190 LLVMTypeRef context_ptr_type;
191 LLVMTypeRef vertex_header_ptr_type;
192 LLVMTypeRef buffer_ptr_type;
193 LLVMTypeRef vb_ptr_type;
194 };
195
196 static struct llvm_vertex_shader *
197 llvm_vertex_shader(struct draw_vertex_shader *vs)
198 {
199 return (struct llvm_vertex_shader *)vs;
200 }
201
202
203 struct draw_llvm *
204 draw_llvm_create(struct draw_context *draw);
205
206 void
207 draw_llvm_destroy(struct draw_llvm *llvm);
208
209 struct draw_llvm_variant *
210 draw_llvm_create_variant(struct draw_llvm *llvm, int num_inputs);
211
212 void
213 draw_llvm_destroy_variant(struct draw_llvm_variant *variant);
214
215 void
216 draw_llvm_make_variant_key(struct draw_llvm *llvm,
217 struct draw_llvm_variant_key *key);
218
219 LLVMValueRef
220 draw_llvm_translate_from(LLVMBuilderRef builder,
221 LLVMValueRef vbuffer,
222 enum pipe_format from_format);
223
224 #endif