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