draw: move vertex header init out of fetch_shade_pipeline.c
[mesa.git] / src / gallium / auxiliary / draw / draw_vs_sse.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 "draw_vs.h"
35
36 #if defined(__i386__) || defined(__386__)
37
38 #include "pipe/p_util.h"
39 #include "pipe/p_shader_tokens.h"
40
41 #include "draw_private.h"
42 #include "draw_context.h"
43
44 #include "rtasm/rtasm_x86sse.h"
45 #include "tgsi/exec/tgsi_sse2.h"
46 #include "tgsi/util/tgsi_parse.h"
47
48 #define SSE_MAX_VERTICES 4
49
50 typedef void (XSTDCALL *codegen_function) (
51 const struct tgsi_exec_vector *input,
52 struct tgsi_exec_vector *output,
53 float (*constant)[4],
54 struct tgsi_exec_vector *temporary,
55 float (*immediates)[4] );
56
57
58 struct draw_sse_vertex_shader {
59 struct draw_vertex_shader base;
60 struct x86_function sse2_program;
61 codegen_function func;
62 float immediates[TGSI_EXEC_NUM_IMMEDIATES][4];
63 };
64
65
66 static void
67 vs_sse_prepare( struct draw_vertex_shader *base,
68 struct draw_context *draw )
69 {
70 draw_update_vertex_fetch( draw );
71 }
72
73 /**
74 * Transform vertices with the current vertex program/shader
75 * Up to four vertices can be shaded at a time.
76 * \param vbuffer the input vertex data
77 * \param elts indexes of four input vertices
78 * \param count number of vertices to shade [1..4]
79 * \param vOut array of pointers to four output vertices
80 */
81 static boolean
82 vs_sse_run( struct draw_vertex_shader *base,
83 struct draw_context *draw,
84 const unsigned *elts,
85 unsigned count,
86 void *vOut )
87 {
88 struct draw_sse_vertex_shader *shader = (struct draw_sse_vertex_shader *)base;
89 struct tgsi_exec_machine *machine = &draw->machine;
90 unsigned int i, j;
91 unsigned int clipped = 0;
92
93 ALIGN16_DECL(struct tgsi_exec_vector, inputs, PIPE_MAX_ATTRIBS);
94 ALIGN16_DECL(struct tgsi_exec_vector, outputs, PIPE_MAX_ATTRIBS);
95 const float *scale = draw->viewport.scale;
96 const float *trans = draw->viewport.translate;
97
98 assert(draw->vertex_shader->info.output_semantic_name[0]
99 == TGSI_SEMANTIC_POSITION);
100
101 /* Consts does not require 16 byte alignment. */
102 machine->Consts = (float (*)[4]) draw->user.constants;
103 machine->Inputs = ALIGN16_ASSIGN(inputs);
104 if (draw->rasterizer->bypass_vs) {
105 /* outputs are just the inputs */
106 machine->Outputs = machine->Inputs;
107 }
108 else {
109 machine->Outputs = ALIGN16_ASSIGN(outputs);
110 }
111
112 for (i = 0; i < count; i += SSE_MAX_VERTICES) {
113 unsigned int max_vertices = MIN2(SSE_MAX_VERTICES, count - i);
114 /* Fetch vertices. This may at some point be integrated into the
115 * compiled shader -- that would require a reorganization where
116 * multiple versions of the compiled shader might exist,
117 * specialized for each fetch state.
118 */
119 draw->vertex_fetch.fetch_func(draw, machine, &elts[i], max_vertices);
120
121 if (!draw->rasterizer->bypass_vs) {
122 /* run compiled shader
123 */
124 shader->func(machine->Inputs,
125 machine->Outputs,
126 machine->Consts,
127 machine->Temps,
128 shader->immediates);
129 }
130
131 /* XXX: Computing the clipmask and emitting results should be done
132 * in the vertex program as a set of instructions appended to
133 * the user-provided code.
134 */
135 for (j = 0; j < max_vertices; j++) {
136 unsigned slot;
137 float x, y, z, w;
138 struct vertex_header *out =
139 draw_header_from_block(vOut, i + j);
140
141 x = out->clip[0] = machine->Outputs[0].xyzw[0].f[j];
142 y = out->clip[1] = machine->Outputs[0].xyzw[1].f[j];
143 z = out->clip[2] = machine->Outputs[0].xyzw[2].f[j];
144 w = out->clip[3] = machine->Outputs[0].xyzw[3].f[j];
145
146 if (!draw->rasterizer->bypass_clipping) {
147 out->clipmask = compute_clipmask(out->clip, draw->plane,
148 draw->nr_planes);
149 clipped += out->clipmask;
150
151 /* divide by w */
152 w = 1.0f / w;
153 x *= w;
154 y *= w;
155 z *= w;
156 }
157 else {
158 out->clipmask = 0;
159 }
160 out->edgeflag = 1;
161 out->vertex_id = UNDEFINED_VERTEX_ID;
162
163 if (!draw->identity_viewport) {
164 /* Viewport mapping */
165 out->data[0][0] = x * scale[0] + trans[0];
166 out->data[0][1] = y * scale[1] + trans[1];
167 out->data[0][2] = z * scale[2] + trans[2];
168 out->data[0][3] = w;
169 }
170 else {
171 out->data[0][0] = x;
172 out->data[0][1] = y;
173 out->data[0][2] = z;
174 out->data[0][3] = w;
175 }
176
177 /* Remaining attributes are packed into sequential post-transform
178 * vertex attrib slots.
179 */
180 for (slot = 1; slot < draw->num_vs_outputs; slot++) {
181 out->data[slot][0] = machine->Outputs[slot].xyzw[0].f[j];
182 out->data[slot][1] = machine->Outputs[slot].xyzw[1].f[j];
183 out->data[slot][2] = machine->Outputs[slot].xyzw[2].f[j];
184 out->data[slot][3] = machine->Outputs[slot].xyzw[3].f[j];
185 }
186 #if 0 /*DEBUG*/
187 printf("%d) Post xform vert:\n", i + j);
188 for (slot = 0; slot < draw->num_vs_outputs; slot++) {
189 printf("\t%d: %f %f %f %f\n", slot,
190 out->data[slot][0],
191 out->data[slot][1],
192 out->data[slot][2],
193 out->data[slot][3]);
194 }
195 #endif
196 }
197 }
198 return clipped != 0;
199 }
200
201
202
203 static void
204 vs_sse_delete( struct draw_vertex_shader *base )
205 {
206 struct draw_sse_vertex_shader *shader = (struct draw_sse_vertex_shader *)base;
207
208 x86_release_func( &shader->sse2_program );
209
210 FREE( (void*) shader->base.state.tokens );
211 FREE( shader );
212 }
213
214
215 struct draw_vertex_shader *
216 draw_create_vs_sse(struct draw_context *draw,
217 const struct pipe_shader_state *templ)
218 {
219 struct draw_sse_vertex_shader *vs;
220 uint nt = tgsi_num_tokens(templ->tokens);
221
222 if (!draw->use_sse)
223 return NULL;
224
225 vs = CALLOC_STRUCT( draw_sse_vertex_shader );
226 if (vs == NULL)
227 return NULL;
228
229 /* we make a private copy of the tokens */
230 vs->base.state.tokens = mem_dup(templ->tokens, nt * sizeof(templ->tokens[0]));
231 vs->base.prepare = vs_sse_prepare;
232 vs->base.run = vs_sse_run;
233 vs->base.delete = vs_sse_delete;
234
235 x86_init_func( &vs->sse2_program );
236
237 if (!tgsi_emit_sse2( (struct tgsi_token *) vs->base.state.tokens,
238 &vs->sse2_program, vs->immediates ))
239 goto fail;
240
241 vs->func = (codegen_function) x86_get_func( &vs->sse2_program );
242
243 return &vs->base;
244
245 fail:
246 fprintf(stderr, "tgsi_emit_sse2() failed, falling back to interpreter\n");
247
248 x86_release_func( &vs->sse2_program );
249
250 FREE(vs);
251 return NULL;
252 }
253
254
255
256 #else
257
258 struct draw_vertex_shader *
259 draw_create_vs_sse( struct draw_context *draw,
260 const struct pipe_shader_state *templ )
261 {
262 return (void *) 0;
263 }
264
265
266 #endif
267