2ce30b9a02bb85f70a728746004673b3a98d4f82
[mesa.git] / src / gallium / auxiliary / draw / draw_vs_llvm.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 /*
29 * Authors:
30 * Zack Rusin
31 * Keith Whitwell <keith@tungstengraphics.com>
32 * Brian Paul
33 */
34
35 #include "pipe/p_shader_tokens.h"
36 #include "draw_private.h"
37 #include "draw_context.h"
38 #include "draw_vs.h"
39
40 #include "tgsi/tgsi_parse.h"
41
42 #ifdef MESA_LLVM
43
44 #include "gallivm/gallivm.h"
45
46 struct draw_llvm_vertex_shader {
47 struct draw_vertex_shader base;
48 struct gallivm_prog *llvm_prog;
49 struct tgsi_exec_machine *machine;
50 };
51
52
53 static void
54 vs_llvm_prepare( struct draw_vertex_shader *base,
55 struct draw_context *draw )
56 {
57 }
58
59
60
61
62 static void
63 vs_llvm_run_linear( struct draw_vertex_shader *base,
64 const float (*input)[4],
65 float (*output)[4],
66 const float (*constants)[4],
67 unsigned count,
68 unsigned input_stride,
69 unsigned output_stride )
70 {
71 struct draw_llvm_vertex_shader *shader =
72 (struct draw_llvm_vertex_shader *)base;
73
74 gallivm_cpu_vs_exec(shader->llvm_prog, shader->machine,
75 input, base->info.num_inputs, output, base->info.num_outputs,
76 constants, count, input_stride, output_stride);
77 }
78
79
80
81 static void
82 vs_llvm_delete( struct draw_vertex_shader *base )
83 {
84 struct draw_llvm_vertex_shader *shader =
85 (struct draw_llvm_vertex_shader *)base;
86
87 /* Do something to free compiled shader:
88 */
89
90 FREE( (void*) shader->base.state.tokens );
91 FREE( shader );
92 }
93
94
95
96
97 struct draw_vertex_shader *
98 draw_create_vs_llvm(struct draw_context *draw,
99 const struct pipe_shader_state *templ)
100 {
101 struct draw_llvm_vertex_shader *vs;
102
103 vs = CALLOC_STRUCT( draw_llvm_vertex_shader );
104 if (vs == NULL)
105 return NULL;
106
107 /* we make a private copy of the tokens */
108 vs->base.state.tokens = tgsi_dup_tokens(templ->tokens);
109 if (!vs->base.state.tokens) {
110 FREE(vs);
111 return NULL;
112 }
113
114 tgsi_scan_shader(vs->base.state.tokens, &vs->base.info);
115
116 vs->base.draw = draw;
117 vs->base.prepare = vs_llvm_prepare;
118 vs->base.create_varient = draw_vs_varient_generic;
119 vs->base.run_linear = vs_llvm_run_linear;
120 vs->base.delete = vs_llvm_delete;
121 vs->machine = &draw->vs.machine;
122
123 {
124 struct gallivm_ir *ir = gallivm_ir_new(GALLIVM_VS);
125 gallivm_ir_set_layout(ir, GALLIVM_SOA);
126 gallivm_ir_set_components(ir, 4);
127 gallivm_ir_fill_from_tgsi(ir, vs->base.state.tokens);
128 vs->llvm_prog = gallivm_ir_compile(ir);
129 gallivm_ir_delete(ir);
130 }
131
132 draw->vs.engine = gallivm_global_cpu_engine();
133
134 /* XXX: Why are there two versions of this? Shouldn't creating the
135 * engine be a separate operation to compiling a shader?
136 */
137 if (!draw->vs.engine) {
138 draw->vs.engine = gallivm_cpu_engine_create(vs->llvm_prog);
139 }
140 else {
141 gallivm_cpu_jit_compile(draw->vs.engine, vs->llvm_prog);
142 }
143
144 return &vs->base;
145 }
146
147
148
149
150
151 #else
152
153 struct draw_vertex_shader *
154 draw_create_vs_llvm(struct draw_context *draw,
155 const struct pipe_shader_state *shader)
156 {
157 return NULL;
158 }
159
160 #endif