44022b6e07771073f3dd4afd1c57da6a495720af
[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 #ifdef MESA_LLVM
42
43 #include "llvm/gallivm.h"
44
45 struct draw_llvm_vertex_shader {
46 struct draw_vertex_shader base;
47 struct gallivm_prog *llvm_prog;
48 };
49
50
51 static INLINE unsigned
52 compute_clipmask(const float *clip, /*const*/ float plane[][4], unsigned nr)
53 {
54 unsigned mask = 0;
55 unsigned i;
56
57 /* Do the hardwired planes first:
58 */
59 if (-clip[0] + clip[3] < 0) mask |= CLIP_RIGHT_BIT;
60 if ( clip[0] + clip[3] < 0) mask |= CLIP_LEFT_BIT;
61 if (-clip[1] + clip[3] < 0) mask |= CLIP_TOP_BIT;
62 if ( clip[1] + clip[3] < 0) mask |= CLIP_BOTTOM_BIT;
63 if (-clip[2] + clip[3] < 0) mask |= CLIP_FAR_BIT;
64 if ( clip[2] + clip[3] < 0) mask |= CLIP_NEAR_BIT;
65
66 /* Followed by any remaining ones:
67 */
68 for (i = 6; i < nr; i++) {
69 if (dot4(clip, plane[i]) < 0)
70 mask |= (1<<i);
71 }
72
73 return mask;
74 }
75
76
77
78 static void
79 vs_llvm_prepare( struct draw_vertex_shader *base,
80 struct draw_context *draw )
81 {
82 draw_update_vertex_fetch( draw );
83 }
84
85
86
87 /**
88 * Transform vertices with the current vertex program/shader
89 * Up to four vertices can be shaded at a time.
90 * \param vbuffer the input vertex data
91 * \param elts indexes of four input vertices
92 * \param count number of vertices to shade [1..4]
93 * \param vOut array of pointers to four output vertices
94 */
95 static void
96 vs_llvm_run( struct draw_vertex_shader *base,
97 struct draw_context *draw,
98 const unsigned *elts,
99 unsigned count,
100 struct vertex_header *vOut[] )
101 {
102 struct draw_llvm_vertex_shader *shader =
103 (struct draw_llvm_vertex_shader *)base;
104
105 struct tgsi_exec_machine *machine = &draw->machine;
106 unsigned int j;
107
108 ALIGN16_DECL(struct tgsi_exec_vector, inputs, PIPE_ATTRIB_MAX);
109 ALIGN16_DECL(struct tgsi_exec_vector, outputs, PIPE_ATTRIB_MAX);
110 const float *scale = draw->viewport.scale;
111 const float *trans = draw->viewport.translate;
112
113
114 assert(count <= 4);
115 assert(draw->vertex_shader->state->output_semantic_name[0]
116 == TGSI_SEMANTIC_POSITION);
117
118 /* Consts does not require 16 byte alignment. */
119 machine->Consts = (float (*)[4]) draw->user.constants;
120
121 machine->Inputs = ALIGN16_ASSIGN(inputs);
122 machine->Outputs = ALIGN16_ASSIGN(outputs);
123
124 draw->vertex_fetch.fetch_func( draw, machine, elts, count );
125
126 /* run shader */
127 gallivm_cpu_vs_exec(shader->llvm_prog,
128 machine->Inputs,
129 machine->Outputs,
130 machine->Consts,
131 machine->Temps);
132
133 /* store machine results */
134 for (j = 0; j < count; j++) {
135 unsigned slot;
136 float x, y, z, w;
137
138 x = vOut[j]->clip[0] = machine->Outputs[0].xyzw[0].f[j];
139 y = vOut[j]->clip[1] = machine->Outputs[0].xyzw[1].f[j];
140 z = vOut[j]->clip[2] = machine->Outputs[0].xyzw[2].f[j];
141 w = vOut[j]->clip[3] = machine->Outputs[0].xyzw[3].f[j];
142
143 vOut[j]->clipmask = compute_clipmask(vOut[j]->clip, draw->plane, draw->nr_planes);
144 vOut[j]->edgeflag = 1;
145
146 /* divide by w */
147 w = 1.0f / w;
148 x *= w;
149 y *= w;
150 z *= w;
151
152 /* Viewport mapping */
153 vOut[j]->data[0][0] = x * scale[0] + trans[0];
154 vOut[j]->data[0][1] = y * scale[1] + trans[1];
155 vOut[j]->data[0][2] = z * scale[2] + trans[2];
156 vOut[j]->data[0][3] = w;
157
158 /* Remaining attributes are packed into sequential post-transform
159 * vertex attrib slots.
160 */
161 for (slot = 1; slot < draw->num_vs_outputs; slot++) {
162 vOut[j]->data[slot][0] = machine->Outputs[slot].xyzw[0].f[j];
163 vOut[j]->data[slot][1] = machine->Outputs[slot].xyzw[1].f[j];
164 vOut[j]->data[slot][2] = machine->Outputs[slot].xyzw[2].f[j];
165 vOut[j]->data[slot][3] = machine->Outputs[slot].xyzw[3].f[j];
166 }
167 } /* loop over vertices */
168 }
169
170 static void
171 vs_llvm_delete( struct draw_vertex_shader *base )
172 {
173 struct draw_llvm_vertex_shader *shader =
174 (struct draw_llvm_vertex_shader *)base;
175
176 /* Do something to free compiled shader:
177 */
178
179 FREE( shader );
180 }
181
182
183
184
185 struct draw_vertex_shader *
186 draw_create_vs_llvm(struct draw_context *draw,
187 const struct pipe_shader_state *templ)
188 {
189 struct draw_llvm_vertex_shader *vs;
190
191 vs = CALLOC_STRUCT( draw_llvm_vertex_shader );
192 if (vs == NULL)
193 return NULL;
194
195 vs->base.state = templ;
196 vs->base.prepare = vs_llvm_prepare;
197 vs->base.run = vs_llvm_run;
198 vs->base.delete = vs_llvm_delete;
199
200 {
201 struct gallivm_ir *ir = gallivm_ir_new(GALLIVM_VS);
202 gallivm_ir_set_layout(ir, GALLIVM_SOA);
203 gallivm_ir_set_components(ir, 4);
204 gallivm_ir_fill_from_tgsi(ir, vs->base.state->tokens);
205 vs->llvm_prog = gallivm_ir_compile(ir);
206 gallivm_ir_delete(ir);
207 }
208
209 draw->engine = gallivm_global_cpu_engine();
210
211 /* XXX: Why are there two versions of this? Shouldn't creating the
212 * engine be a separate operation to compiling a shader?
213 */
214 if (!draw->engine) {
215 draw->engine = gallivm_cpu_engine_create(vs->llvm_prog);
216 }
217 else {
218 gallivm_cpu_jit_compile(draw->engine, vs->llvm_prog);
219 }
220
221 return &vs->base;
222 }
223
224
225
226
227
228 #else
229
230 struct draw_vertex_shader *
231 draw_create_vs_llvm(struct draw_context *draw,
232 const struct pipe_shader_state *shader)
233 {
234 return NULL;
235 }
236
237 #endif