gallium: Windows CE portability fixes.
[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 "pipe/p_config.h"
35
36 #include "draw_vs.h"
37
38 #if defined(PIPE_ARCH_X86)
39
40 #include "pipe/p_util.h"
41 #include "pipe/p_shader_tokens.h"
42
43 #include "draw_private.h"
44 #include "draw_context.h"
45
46 #include "rtasm/rtasm_cpu.h"
47 #include "rtasm/rtasm_x86sse.h"
48 #include "tgsi/exec/tgsi_sse2.h"
49 #include "tgsi/util/tgsi_parse.h"
50
51 #define SSE_MAX_VERTICES 4
52 #define SSE_SWIZZLES 1
53
54 #if SSE_SWIZZLES
55 typedef void (XSTDCALL *codegen_function) (
56 const struct tgsi_exec_vector *input, /* 1 */
57 struct tgsi_exec_vector *output, /* 2 */
58 float (*constant)[4], /* 3 */
59 struct tgsi_exec_vector *temporary, /* 4 */
60 float (*immediates)[4], /* 5 */
61 const float (*aos_input)[4], /* 6 */
62 uint num_inputs, /* 7 */
63 uint input_stride, /* 8 */
64 float (*aos_output)[4], /* 9 */
65 uint num_outputs, /* 10 */
66 uint output_stride ); /* 11 */
67 #else
68 typedef void (XSTDCALL *codegen_function) (
69 const struct tgsi_exec_vector *input,
70 struct tgsi_exec_vector *output,
71 float (*constant)[4],
72 struct tgsi_exec_vector *temporary,
73 float (*immediates)[4] );
74 #endif
75
76 struct draw_sse_vertex_shader {
77 struct draw_vertex_shader base;
78 struct x86_function sse2_program;
79
80 codegen_function func;
81
82 struct tgsi_exec_machine *machine;
83
84 float immediates[TGSI_EXEC_NUM_IMMEDIATES][4];
85 };
86
87
88 static void
89 vs_sse_prepare( struct draw_vertex_shader *base,
90 struct draw_context *draw )
91 {
92 }
93
94
95
96 /* Simplified vertex shader interface for the pt paths. Given the
97 * complexity of code-generating all the above operations together,
98 * it's time to try doing all the other stuff separately.
99 */
100 static void
101 vs_sse_run_linear( struct draw_vertex_shader *base,
102 const float (*input)[4],
103 float (*output)[4],
104 const float (*constants)[4],
105 unsigned count,
106 unsigned input_stride,
107 unsigned output_stride )
108 {
109 struct draw_sse_vertex_shader *shader = (struct draw_sse_vertex_shader *)base;
110 struct tgsi_exec_machine *machine = shader->machine;
111 unsigned int i;
112
113 for (i = 0; i < count; i += MAX_TGSI_VERTICES) {
114 unsigned int max_vertices = MIN2(MAX_TGSI_VERTICES, count - i);
115
116 #if SSE_SWIZZLES
117 /* run compiled shader
118 */
119 shader->func(machine->Inputs,
120 machine->Outputs,
121 (float (*)[4])constants,
122 machine->Temps,
123 shader->immediates,
124 input,
125 base->info.num_inputs,
126 input_stride,
127 output,
128 base->info.num_outputs,
129 output_stride );
130
131 input = (const float (*)[4])((const char *)input + input_stride * max_vertices);
132 output = (float (*)[4])((char *)output + output_stride * max_vertices);
133 #else
134 unsigned int j, slot;
135
136 /* Swizzle inputs.
137 */
138 for (j = 0; j < max_vertices; j++) {
139 for (slot = 0; slot < base->info.num_inputs; slot++) {
140 machine->Inputs[slot].xyzw[0].f[j] = input[slot][0];
141 machine->Inputs[slot].xyzw[1].f[j] = input[slot][1];
142 machine->Inputs[slot].xyzw[2].f[j] = input[slot][2];
143 machine->Inputs[slot].xyzw[3].f[j] = input[slot][3];
144 }
145
146 input = (const float (*)[4])((const char *)input + input_stride);
147 }
148
149 /* run compiled shader
150 */
151 shader->func(machine->Inputs,
152 machine->Outputs,
153 (float (*)[4])constants,
154 machine->Temps,
155 shader->immediates);
156
157 /* Unswizzle all output results.
158 */
159 for (j = 0; j < max_vertices; j++) {
160 for (slot = 0; slot < base->info.num_outputs; slot++) {
161 output[slot][0] = machine->Outputs[slot].xyzw[0].f[j];
162 output[slot][1] = machine->Outputs[slot].xyzw[1].f[j];
163 output[slot][2] = machine->Outputs[slot].xyzw[2].f[j];
164 output[slot][3] = machine->Outputs[slot].xyzw[3].f[j];
165 }
166
167 output = (float (*)[4])((char *)output + output_stride);
168 }
169 #endif
170 }
171 }
172
173
174
175
176 static void
177 vs_sse_delete( struct draw_vertex_shader *base )
178 {
179 struct draw_sse_vertex_shader *shader = (struct draw_sse_vertex_shader *)base;
180
181 x86_release_func( &shader->sse2_program );
182
183 FREE( (void*) shader->base.state.tokens );
184 FREE( shader );
185 }
186
187
188 struct draw_vertex_shader *
189 draw_create_vs_sse(struct draw_context *draw,
190 const struct pipe_shader_state *templ)
191 {
192 struct draw_sse_vertex_shader *vs;
193
194 if (!rtasm_cpu_has_sse2())
195 return NULL;
196
197 vs = CALLOC_STRUCT( draw_sse_vertex_shader );
198 if (vs == NULL)
199 return NULL;
200
201 /* we make a private copy of the tokens */
202 vs->base.state.tokens = tgsi_dup_tokens(templ->tokens);
203 if (!vs->base.state.tokens)
204 goto fail;
205
206 tgsi_scan_shader(templ->tokens, &vs->base.info);
207
208 vs->base.prepare = vs_sse_prepare;
209 vs->base.run_linear = vs_sse_run_linear;
210 vs->base.delete = vs_sse_delete;
211 vs->machine = &draw->machine;
212
213 x86_init_func( &vs->sse2_program );
214
215 if (!tgsi_emit_sse2( (struct tgsi_token *) vs->base.state.tokens,
216 &vs->sse2_program, vs->immediates, SSE_SWIZZLES ))
217 goto fail;
218
219 vs->func = (codegen_function) x86_get_func( &vs->sse2_program );
220 if (!vs->func) {
221 goto fail;
222 }
223
224 return &vs->base;
225
226 fail:
227 fprintf(stderr, "tgsi_emit_sse2() failed, falling back to interpreter\n");
228
229 x86_release_func( &vs->sse2_program );
230
231 FREE(vs);
232 return NULL;
233 }
234
235
236
237 #else
238
239 struct draw_vertex_shader *
240 draw_create_vs_sse( struct draw_context *draw,
241 const struct pipe_shader_state *templ )
242 {
243 return (void *) 0;
244 }
245
246
247 #endif
248