a4f72c40eff1e77ff5b759fa4b5e87d66756f9d2
[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 "util/u_math.h"
35 #include "util/u_memory.h"
36 #include "pipe/p_config.h"
37
38 #include "draw_vs.h"
39
40 #if defined(PIPE_ARCH_X86)
41
42 #include "pipe/p_shader_tokens.h"
43
44 #include "draw_private.h"
45 #include "draw_context.h"
46
47 #include "rtasm/rtasm_cpu.h"
48 #include "rtasm/rtasm_x86sse.h"
49 #include "tgsi/tgsi_sse2.h"
50 #include "tgsi/tgsi_parse.h"
51 #include "tgsi/tgsi_exec.h"
52
53 #define SSE_MAX_VERTICES 4
54
55 typedef void (PIPE_CDECL *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
68 struct draw_sse_vertex_shader {
69 struct draw_vertex_shader base;
70 struct x86_function sse2_program;
71
72 codegen_function func;
73
74 struct tgsi_exec_machine *machine;
75 };
76
77
78 static void
79 vs_sse_prepare( struct draw_vertex_shader *base,
80 struct draw_context *draw )
81 {
82 }
83
84
85
86 /* Simplified vertex shader interface for the pt paths. Given the
87 * complexity of code-generating all the above operations together,
88 * it's time to try doing all the other stuff separately.
89 */
90 static void
91 vs_sse_run_linear( struct draw_vertex_shader *base,
92 const float (*input)[4],
93 float (*output)[4],
94 const float (*constants)[4],
95 unsigned count,
96 unsigned input_stride,
97 unsigned output_stride )
98 {
99 struct draw_sse_vertex_shader *shader = (struct draw_sse_vertex_shader *)base;
100 struct tgsi_exec_machine *machine = shader->machine;
101 unsigned int i;
102
103 /* By default, execute all channels. XXX move this inside the loop
104 * below when we support shader conditionals/loops.
105 */
106 tgsi_set_exec_mask(machine, 1, 1, 1, 1);
107
108 for (i = 0; i < count; i += MAX_TGSI_VERTICES) {
109 unsigned int max_vertices = MIN2(MAX_TGSI_VERTICES, count - i);
110
111 if (max_vertices < 4) {
112 /* disable the unused execution channels */
113 tgsi_set_exec_mask(machine,
114 1,
115 max_vertices > 1,
116 max_vertices > 2,
117 0);
118 }
119
120 /* run compiled shader
121 */
122 shader->func(machine->Inputs,
123 machine->Outputs,
124 (float (*)[4])constants,
125 machine->Temps,
126 (float (*)[4])shader->base.immediates,
127 input,
128 base->info.num_inputs,
129 input_stride,
130 output,
131 base->info.num_outputs,
132 output_stride );
133
134 input = (const float (*)[4])((const char *)input + input_stride * max_vertices);
135 output = (float (*)[4])((char *)output + output_stride * max_vertices);
136 }
137 }
138
139
140
141
142 static void
143 vs_sse_delete( struct draw_vertex_shader *base )
144 {
145 struct draw_sse_vertex_shader *shader = (struct draw_sse_vertex_shader *)base;
146
147 x86_release_func( &shader->sse2_program );
148
149 align_free( (void *) shader->base.immediates );
150
151 FREE( (void*) shader->base.state.tokens );
152 FREE( shader );
153 }
154
155
156 struct draw_vertex_shader *
157 draw_create_vs_sse(struct draw_context *draw,
158 const struct pipe_shader_state *templ)
159 {
160 struct draw_sse_vertex_shader *vs;
161
162 if (!rtasm_cpu_has_sse2())
163 return NULL;
164
165 vs = CALLOC_STRUCT( draw_sse_vertex_shader );
166 if (vs == NULL)
167 return NULL;
168
169 /* we make a private copy of the tokens */
170 vs->base.state.tokens = tgsi_dup_tokens(templ->tokens);
171 if (!vs->base.state.tokens)
172 goto fail;
173
174 tgsi_scan_shader(templ->tokens, &vs->base.info);
175
176 vs->base.draw = draw;
177 if (1)
178 vs->base.create_varient = draw_vs_varient_aos_sse;
179 else
180 vs->base.create_varient = draw_vs_varient_generic;
181 vs->base.prepare = vs_sse_prepare;
182 vs->base.run_linear = vs_sse_run_linear;
183 vs->base.delete = vs_sse_delete;
184
185 vs->base.immediates = align_malloc(TGSI_EXEC_NUM_IMMEDIATES * 4 *
186 sizeof(float), 16);
187
188 vs->machine = draw->vs.machine;
189
190 x86_init_func( &vs->sse2_program );
191
192 if (!tgsi_emit_sse2( (struct tgsi_token *) vs->base.state.tokens,
193 &vs->sse2_program,
194 (float (*)[4])vs->base.immediates,
195 TRUE ))
196 goto fail;
197
198 vs->func = (codegen_function) x86_get_func( &vs->sse2_program );
199 if (!vs->func) {
200 goto fail;
201 }
202
203 return &vs->base;
204
205 fail:
206 debug_error("tgsi_emit_sse2() failed, falling back to interpreter\n");
207
208 x86_release_func( &vs->sse2_program );
209
210 FREE(vs);
211 return NULL;
212 }
213
214
215
216 #else
217
218 struct draw_vertex_shader *
219 draw_create_vs_sse( struct draw_context *draw,
220 const struct pipe_shader_state *templ )
221 {
222 return (void *) 0;
223 }
224
225
226 #endif
227