2ebf05526a15be594d2fa60fa60d2c0ad1fd9ecd
[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_util.h"
36 #include "pipe/p_shader_tokens.h"
37 #include "draw_private.h"
38 #include "draw_context.h"
39 #include "draw_vs.h"
40
41 #include "tgsi/util/tgsi_parse.h"
42
43 #ifdef MESA_LLVM
44
45 #include "gallivm/gallivm.h"
46
47 struct draw_llvm_vertex_shader {
48 struct draw_vertex_shader base;
49 struct gallivm_prog *llvm_prog;
50 struct tgsi_exec_machine *machine;
51 };
52
53
54 static void
55 vs_llvm_prepare( struct draw_vertex_shader *base,
56 struct draw_context *draw )
57 {
58 }
59
60
61
62
63 static void
64 vs_llvm_run_linear( struct draw_vertex_shader *base,
65 const float (*input)[4],
66 float (*output)[4],
67 const float (*constants)[4],
68 unsigned count,
69 unsigned input_stride,
70 unsigned output_stride )
71 {
72 struct draw_llvm_vertex_shader *shader =
73 (struct draw_llvm_vertex_shader *)base;
74
75 struct tgsi_exec_machine *machine = shader->machine;
76 unsigned int i, j;
77 unsigned slot;
78
79
80 for (i = 0; i < count; i += MAX_TGSI_VERTICES) {
81 unsigned int max_vertices = MIN2(MAX_TGSI_VERTICES, count - i);
82
83 /* Swizzle inputs.
84 */
85 for (j = 0; j < max_vertices; j++) {
86 for (slot = 0; slot < base->info.num_inputs; slot++) {
87 machine->Inputs[slot].xyzw[0].f[j] = input[slot][0];
88 machine->Inputs[slot].xyzw[1].f[j] = input[slot][1];
89 machine->Inputs[slot].xyzw[2].f[j] = input[slot][2];
90 machine->Inputs[slot].xyzw[3].f[j] = input[slot][3];
91 }
92
93 input = (const float (*)[4])((const char *)input + input_stride);
94 }
95
96 /* run shader */
97 gallivm_cpu_vs_exec(shader->llvm_prog,
98 machine->Inputs,
99 machine->Outputs,
100 (float (*)[4]) constants,
101 machine->Temps);
102
103
104 /* Unswizzle all output results
105 */
106 for (slot = 1; slot < base->info.num_inputs; slot++) {
107 output[slot][0] = machine->Outputs[slot].xyzw[0].f[j];
108 output[slot][1] = machine->Outputs[slot].xyzw[1].f[j];
109 output[slot][2] = machine->Outputs[slot].xyzw[2].f[j];
110 output[slot][3] = machine->Outputs[slot].xyzw[3].f[j];
111
112 output = (float (*)[4])((char *)output + output_stride);
113 }
114 }
115 }
116
117
118
119 static void
120 vs_llvm_delete( struct draw_vertex_shader *base )
121 {
122 struct draw_llvm_vertex_shader *shader =
123 (struct draw_llvm_vertex_shader *)base;
124
125 /* Do something to free compiled shader:
126 */
127
128 FREE( (void*) shader->base.state.tokens );
129 FREE( shader );
130 }
131
132
133
134
135 struct draw_vertex_shader *
136 draw_create_vs_llvm(struct draw_context *draw,
137 const struct pipe_shader_state *templ)
138 {
139 struct draw_llvm_vertex_shader *vs;
140 uint nt = tgsi_num_tokens(templ->tokens);
141
142 vs = CALLOC_STRUCT( draw_llvm_vertex_shader );
143 if (vs == NULL)
144 return NULL;
145
146 /* we make a private copy of the tokens */
147 vs->base.state.tokens = mem_dup(templ->tokens, nt * sizeof(templ->tokens[0]));
148
149 tgsi_scan_shader(vs->base.state.tokens, &vs->base.info);
150
151 vs->base.prepare = vs_llvm_prepare;
152 vs->base.run_linear = vs_llvm_run_linear;
153 vs->base.delete = vs_llvm_delete;
154 vs->machine = &draw->machine;
155
156 {
157 struct gallivm_ir *ir = gallivm_ir_new(GALLIVM_VS);
158 gallivm_ir_set_layout(ir, GALLIVM_SOA);
159 gallivm_ir_set_components(ir, 4);
160 gallivm_ir_fill_from_tgsi(ir, vs->base.state.tokens);
161 vs->llvm_prog = gallivm_ir_compile(ir);
162 gallivm_ir_delete(ir);
163 }
164
165 draw->engine = gallivm_global_cpu_engine();
166
167 /* XXX: Why are there two versions of this? Shouldn't creating the
168 * engine be a separate operation to compiling a shader?
169 */
170 if (!draw->engine) {
171 draw->engine = gallivm_cpu_engine_create(vs->llvm_prog);
172 }
173 else {
174 gallivm_cpu_jit_compile(draw->engine, vs->llvm_prog);
175 }
176
177 return &vs->base;
178 }
179
180
181
182
183
184 #else
185
186 struct draw_vertex_shader *
187 draw_create_vs_llvm(struct draw_context *draw,
188 const struct pipe_shader_state *shader)
189 {
190 return NULL;
191 }
192
193 #endif