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