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