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