Merge branch 'nouveau-gallium-0.1' into darktama-gallium-0.1
[mesa.git] / src / mesa / pipe / draw / draw_vertex_shader.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 * Keith Whitwell <keith@tungstengraphics.com>
31 * Brian Paul
32 */
33
34 #include "pipe/p_util.h"
35 #include "pipe/p_shader_tokens.h"
36 #include "draw_private.h"
37 #include "draw_context.h"
38 #include "draw_vertex.h"
39
40 #include "x86/rtasm/x86sse.h"
41
42 #include "pipe/llvm/gallivm.h"
43
44
45 #define DBG_VS 0
46
47
48 static INLINE unsigned
49 compute_clipmask(const float *clip, /*const*/ float plane[][4], unsigned nr)
50 {
51 unsigned mask = 0;
52 unsigned i;
53
54 for (i = 0; i < nr; i++) {
55 if (dot4(clip, plane[i]) < 0)
56 mask |= (1<<i);
57 }
58
59 return mask;
60 }
61
62
63 typedef void (XSTDCALL *codegen_function) (
64 const struct tgsi_exec_vector *input,
65 struct tgsi_exec_vector *output,
66 float (*constant)[4],
67 struct tgsi_exec_vector *temporary );
68
69
70 /**
71 * Transform vertices with the current vertex program/shader
72 * Up to four vertices can be shaded at a time.
73 * \param vbuffer the input vertex data
74 * \param elts indexes of four input vertices
75 * \param count number of vertices to shade [1..4]
76 * \param vOut array of pointers to four output vertices
77 */
78 static void
79 run_vertex_program(struct draw_context *draw,
80 unsigned elts[4], unsigned count,
81 struct vertex_header *vOut[])
82 {
83 struct tgsi_exec_machine *machine = &draw->machine;
84 unsigned int j;
85
86 ALIGN16_DECL(struct tgsi_exec_vector, inputs, PIPE_ATTRIB_MAX);
87 ALIGN16_DECL(struct tgsi_exec_vector, outputs, PIPE_ATTRIB_MAX);
88 const float *scale = draw->viewport.scale;
89 const float *trans = draw->viewport.translate;
90
91 assert(count <= 4);
92 assert(draw->vertex_shader->state->output_semantic_name[0]
93 == TGSI_SEMANTIC_POSITION);
94
95 /* Consts does not require 16 byte alignment. */
96 machine->Consts = (float (*)[4]) draw->user.constants;
97
98 machine->Inputs = ALIGN16_ASSIGN(inputs);
99 machine->Outputs = ALIGN16_ASSIGN(outputs);
100
101 draw_vertex_fetch( draw, machine, elts, count );
102
103 /* run shader */
104 #if defined(__i386__) || defined(__386__)
105 if (draw->use_sse) {
106 /* SSE */
107 codegen_function func = (codegen_function) x86_get_func( &draw->vertex_shader->sse2_program );
108 func(
109 machine->Inputs,
110 machine->Outputs,
111 machine->Consts,
112 machine->Temps );
113 }
114 else
115 #endif
116 {
117 /* interpreter */
118 tgsi_exec_machine_run( machine );
119 }
120
121 /* store machine results */
122 for (j = 0; j < count; j++) {
123 unsigned slot;
124 float x, y, z, w;
125
126 /* Handle attr[0] (position) specially:
127 *
128 * XXX: Computing the clipmask should be done in the vertex
129 * program as a set of DP4 instructions appended to the
130 * user-provided code.
131 */
132 x = vOut[j]->clip[0] = machine->Outputs[0].xyzw[0].f[j];
133 y = vOut[j]->clip[1] = machine->Outputs[0].xyzw[1].f[j];
134 z = vOut[j]->clip[2] = machine->Outputs[0].xyzw[2].f[j];
135 w = vOut[j]->clip[3] = machine->Outputs[0].xyzw[3].f[j];
136
137 vOut[j]->clipmask = compute_clipmask(vOut[j]->clip, draw->plane, draw->nr_planes);
138 vOut[j]->edgeflag = 1;
139
140 /* divide by w */
141 w = 1.0f / w;
142 x *= w;
143 y *= w;
144 z *= w;
145
146 /* Viewport mapping */
147 vOut[j]->data[0][0] = x * scale[0] + trans[0];
148 vOut[j]->data[0][1] = y * scale[1] + trans[1];
149 vOut[j]->data[0][2] = z * scale[2] + trans[2];
150 vOut[j]->data[0][3] = w;
151
152 #if DBG_VS
153 printf("output[%d]win: %f %f %f %f\n", j,
154 vOut[j]->data[0][0],
155 vOut[j]->data[0][1],
156 vOut[j]->data[0][2],
157 vOut[j]->data[0][3]);
158 #endif
159 /* Remaining attributes are packed into sequential post-transform
160 * vertex attrib slots.
161 */
162 for (slot = 1; slot < draw->vertex_info.num_attribs; slot++) {
163 vOut[j]->data[slot][0] = machine->Outputs[slot].xyzw[0].f[j];
164 vOut[j]->data[slot][1] = machine->Outputs[slot].xyzw[1].f[j];
165 vOut[j]->data[slot][2] = machine->Outputs[slot].xyzw[2].f[j];
166 vOut[j]->data[slot][3] = machine->Outputs[slot].xyzw[3].f[j];
167 #if DBG_VS
168 printf("output[%d][%d]: %f %f %f %f\n", j, slot,
169 vOut[j]->data[slot][0],
170 vOut[j]->data[slot][1],
171 vOut[j]->data[slot][2],
172 vOut[j]->data[slot][3]);
173 #endif
174 }
175 } /* loop over vertices */
176 }
177
178
179 /**
180 * Run the vertex shader on all vertices in the vertex queue.
181 * Called by the draw module when the vertx cache needs to be flushed.
182 */
183 void draw_vertex_shader_queue_flush( struct draw_context *draw )
184 {
185 unsigned i, j;
186
187 // fprintf(stderr, " q(%d) ", draw->vs.queue_nr );
188 #ifdef MESA_LLVM
189 if (draw->vertex_shader->llvm_prog) {
190 draw_vertex_shader_queue_flush_llvm(draw);
191 return;
192 }
193 #endif
194
195 /* run vertex shader on vertex cache entries, four per invokation */
196 for (i = 0; i < draw->vs.queue_nr; i += 4) {
197 struct vertex_header *dests[4];
198 unsigned elts[4];
199 int n;
200
201 for (j = 0; j < 4; j++) {
202 elts[j] = draw->vs.queue[i + j].elt;
203 dests[j] = draw->vs.queue[i + j].dest;
204 }
205
206 n = MIN2(4, draw->vs.queue_nr - i);
207 assert(n > 0);
208 assert(n <= 4);
209
210 run_vertex_program(draw, elts, n, dests);
211 }
212
213 draw->vs.queue_nr = 0;
214 }
215
216
217 void *
218 draw_create_vertex_shader(struct draw_context *draw,
219 const struct pipe_shader_state *shader)
220 {
221 struct draw_vertex_shader *vs;
222
223 vs = CALLOC_STRUCT( draw_vertex_shader );
224 if (vs == NULL) {
225 return NULL;
226 }
227
228 vs->state = shader;
229
230 #if defined(__i386__) || defined(__386__)
231 if (draw->use_sse) {
232 /* cast-away const */
233 struct pipe_shader_state *sh = (struct pipe_shader_state *) shader;
234
235 x86_init_func( &vs->sse2_program );
236 tgsi_emit_sse2( sh->tokens, &vs->sse2_program );
237 }
238 #endif
239 #ifdef MESA_LLVM
240 vs->llvm_prog = gallivm_from_tgsi(shader->tokens, GALLIVM_VS);
241 draw->engine = gallivm_global_cpu_engine();
242 if (!draw->engine) {
243 draw->engine = gallivm_cpu_engine_create(vs->llvm_prog);
244 }
245 else
246 gallivm_cpu_jit_compile(draw->engine, vs->llvm_prog);
247 #endif
248
249 return vs;
250 }
251
252 void draw_bind_vertex_shader(struct draw_context *draw,
253 void *vcso)
254 {
255 draw_flush(draw);
256 draw->vertex_shader = (struct draw_vertex_shader*)(vcso);
257
258 /* specify the fragment program to interpret/execute */
259 tgsi_exec_machine_init(&draw->machine,
260 draw->vertex_shader->state->tokens,
261 PIPE_MAX_SAMPLERS,
262 NULL /*samplers*/ );
263 }
264
265 void draw_delete_vertex_shader(struct draw_context *draw,
266 void *vcso)
267 {
268 struct draw_vertex_shader *vs;
269
270 vs = (struct draw_vertex_shader *) vcso;
271
272 #if defined(__i386__) || defined(__386__)
273 x86_release_func( (struct x86_function *) &vs->sse2_program );
274 #endif
275
276 FREE( vs );
277 }