Merge branch 'gallium_draw_llvm'
[mesa.git] / src / gallium / auxiliary / draw / draw_llvm.h
1 #ifndef DRAW_LLVM_H
2 #define DRAW_LLVM_H
3
4 #include "draw/draw_private.h"
5
6 #include "pipe/p_context.h"
7
8 #include <llvm-c/Core.h>
9 #include <llvm-c/Analysis.h>
10 #include <llvm-c/Target.h>
11 #include <llvm-c/ExecutionEngine.h>
12
13 struct draw_jit_texture
14 {
15 uint32_t width;
16 uint32_t height;
17 uint32_t stride;
18 const void *data;
19 };
20
21 enum {
22 DRAW_JIT_TEXTURE_WIDTH = 0,
23 DRAW_JIT_TEXTURE_HEIGHT,
24 DRAW_JIT_TEXTURE_STRIDE,
25 DRAW_JIT_TEXTURE_DATA
26 };
27
28 enum {
29 DRAW_JIT_VERTEX_VERTEX_ID = 0,
30 DRAW_JIT_VERTEX_CLIP,
31 DRAW_JIT_VERTEX_DATA
32 };
33
34 /**
35 * This structure is passed directly to the generated vertex shader.
36 *
37 * It contains the derived state.
38 *
39 * Changes here must be reflected in the draw_jit_context_* macros.
40 * Changes to the ordering should be avoided.
41 *
42 * Only use types with a clear size and padding here, in particular prefer the
43 * stdint.h types to the basic integer types.
44 */
45 struct draw_jit_context
46 {
47 const float *vs_constants;
48 const float *gs_constants;
49
50
51 struct draw_jit_texture textures[PIPE_MAX_SAMPLERS];
52 };
53
54
55 #define draw_jit_context_vs_constants(_builder, _ptr) \
56 lp_build_struct_get(_builder, _ptr, 0, "vs_constants")
57
58 #define draw_jit_context_gs_constants(_builder, _ptr) \
59 lp_build_struct_get(_builder, _ptr, 1, "gs_constants")
60
61 #define DRAW_JIT_CONTEXT_TEXTURES_INDEX 2
62
63 #define draw_jit_context_textures(_builder, _ptr) \
64 lp_build_struct_get_ptr(_builder, _ptr, DRAW_JIT_CONTEXT_TEXTURES_INDEX, "textures")
65
66
67
68 #define draw_jit_header_id(_builder, _ptr) \
69 lp_build_struct_get_ptr(_builder, _ptr, 0, "id")
70
71 #define draw_jit_header_clip(_builder, _ptr) \
72 lp_build_struct_get(_builder, _ptr, 1, "clip")
73
74 #define draw_jit_header_data(_builder, _ptr) \
75 lp_build_struct_get_ptr(_builder, _ptr, 2, "data")
76
77 /* we are construction a function of the form:
78
79 struct vertex_header {
80 uint32 vertex_id;
81
82 float clip[4];
83 float data[][4];
84 };
85
86 struct draw_jit_context
87 {
88 const float *vs_constants;
89 const float *gs_constants;
90
91 struct draw_jit_texture textures[PIPE_MAX_SAMPLERS];
92 };
93
94 void
95 draw_shader(struct draw_jit_context *context,
96 struct vertex_header *io,
97 const void *vbuffers[PIPE_MAX_ATTRIBS],
98 unsigned start,
99 unsigned count,
100 unsigned stride)
101 {
102 // do a fetch and a run vertex shader
103 for (int i = 0; i < count; ++i) {
104 struct vertex_header *header = &io[i];
105 header->vertex_id = 0xffff;
106 // follows code-genarted fetch/translate section
107 // for each vertex_element ...
108 codegened_translate(header->data[num_element],
109 context->vertex_elements[num_element],
110 context->vertex_buffers,
111 context->vbuffers);
112
113 codegened_vertex_shader(header->data, context->vs_constants);
114 }
115
116 for (int i = 0; i < count; i += context->primitive_size) {
117 struct vertex_header *prim[MAX_PRIMITIVE_SIZE];
118 for (int j = 0; j < context->primitive_size; ++j) {
119 header[j] = &io[i + j];
120 }
121 codegened_geometry_shader(prim, gs_constants);
122 }
123 }
124 */
125
126 typedef void
127 (*draw_jit_vert_func)(struct draw_jit_context *context,
128 struct vertex_header *io,
129 const char *vbuffers[PIPE_MAX_ATTRIBS],
130 unsigned start,
131 unsigned count,
132 unsigned stride);
133
134 struct draw_llvm {
135 struct draw_context *draw;
136
137 struct draw_jit_context jit_context;
138
139 LLVMModuleRef module;
140 LLVMExecutionEngineRef engine;
141 LLVMModuleProviderRef provider;
142 LLVMTargetDataRef target;
143 LLVMPassManagerRef pass;
144
145 LLVMTypeRef context_ptr_type;
146 LLVMTypeRef vertex_header_ptr_type;
147 LLVMTypeRef buffer_ptr_type;
148 };
149
150
151 struct draw_llvm_variant_key
152 {
153 struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS];
154 unsigned nr_vertex_buffers;
155 struct pipe_vertex_element vertex_element[PIPE_MAX_ATTRIBS];
156 unsigned nr_vertex_elements;
157 struct pipe_shader_state vs;
158 };
159
160 struct draw_llvm_variant
161 {
162 struct draw_llvm_variant_key key;
163 LLVMValueRef function;
164 draw_jit_vert_func jit_func;
165
166 struct draw_llvm_variant *next;
167 };
168
169 struct draw_llvm *
170 draw_llvm_create(struct draw_context *draw);
171
172 void
173 draw_llvm_destroy(struct draw_llvm *llvm);
174
175 struct draw_llvm_variant *
176 draw_llvm_prepare(struct draw_llvm *llvm, int num_inputs);
177
178 void
179 draw_llvm_make_variant_key(struct draw_llvm *llvm,
180 struct draw_llvm_variant_key *key);
181
182 LLVMValueRef
183 draw_llvm_translate_from(LLVMBuilderRef builder,
184 LLVMValueRef vbuffer,
185 enum pipe_format from_format);
186 #endif