Merge branch 'lp-offset-twoside'
[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
56 struct draw_sse_vertex_shader {
57 struct draw_vertex_shader base;
58 struct x86_function sse2_program;
59
60 tgsi_sse2_vs_func func;
61
62 struct tgsi_exec_machine *machine;
63 };
64
65
66 static void
67 vs_sse_prepare( struct draw_vertex_shader *base,
68 struct draw_context *draw )
69 {
70 struct draw_sse_vertex_shader *shader = (struct draw_sse_vertex_shader *)base;
71 struct tgsi_exec_machine *machine = shader->machine;
72
73 machine->Samplers = draw->vs.samplers;
74 }
75
76
77
78 /* Simplified vertex shader interface for the pt paths. Given the
79 * complexity of code-generating all the above operations together,
80 * it's time to try doing all the other stuff separately.
81 */
82 static void
83 vs_sse_run_linear( struct draw_vertex_shader *base,
84 const float (*input)[4],
85 float (*output)[4],
86 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
87 const unsigned const_size[PIPE_MAX_CONSTANT_BUFFERS],
88 unsigned count,
89 unsigned input_stride,
90 unsigned output_stride )
91 {
92 struct draw_sse_vertex_shader *shader = (struct draw_sse_vertex_shader *)base;
93 struct tgsi_exec_machine *machine = shader->machine;
94 unsigned int i;
95
96 /* By default, execute all channels. XXX move this inside the loop
97 * below when we support shader conditionals/loops.
98 */
99 tgsi_set_exec_mask(machine, 1, 1, 1, 1);
100
101 for (i = 0; i < count; i += MAX_TGSI_VERTICES) {
102 unsigned int max_vertices = MIN2(MAX_TGSI_VERTICES, count - i);
103
104 if (max_vertices < 4) {
105 /* disable the unused execution channels */
106 tgsi_set_exec_mask(machine,
107 1,
108 max_vertices > 1,
109 max_vertices > 2,
110 0);
111 }
112
113 /* run compiled shader
114 */
115 shader->func(machine,
116 (const float (*)[4])constants[0],
117 shader->base.immediates,
118 input,
119 base->info.num_inputs,
120 input_stride,
121 output,
122 base->info.num_outputs,
123 output_stride );
124
125 input = (const float (*)[4])((const char *)input + input_stride * max_vertices);
126 output = (float (*)[4])((char *)output + output_stride * max_vertices);
127 }
128 }
129
130
131
132
133 static void
134 vs_sse_delete( struct draw_vertex_shader *base )
135 {
136 struct draw_sse_vertex_shader *shader = (struct draw_sse_vertex_shader *)base;
137
138 x86_release_func( &shader->sse2_program );
139
140 align_free( (void *) shader->base.immediates );
141
142 FREE( (void*) shader->base.state.tokens );
143 FREE( shader );
144 }
145
146
147 struct draw_vertex_shader *
148 draw_create_vs_sse(struct draw_context *draw,
149 const struct pipe_shader_state *templ)
150 {
151 struct draw_sse_vertex_shader *vs;
152
153 if (!rtasm_cpu_has_sse2())
154 return NULL;
155
156 vs = CALLOC_STRUCT( draw_sse_vertex_shader );
157 if (vs == NULL)
158 return NULL;
159
160 /* we make a private copy of the tokens */
161 vs->base.state.tokens = tgsi_dup_tokens(templ->tokens);
162 if (!vs->base.state.tokens)
163 goto fail;
164
165 tgsi_scan_shader(templ->tokens, &vs->base.info);
166
167 vs->base.draw = draw;
168 if (1)
169 vs->base.create_varient = draw_vs_create_varient_aos_sse;
170 else
171 vs->base.create_varient = draw_vs_create_varient_generic;
172 vs->base.prepare = vs_sse_prepare;
173 vs->base.run_linear = vs_sse_run_linear;
174 vs->base.delete = vs_sse_delete;
175
176 vs->base.immediates = align_malloc(TGSI_EXEC_NUM_IMMEDIATES * 4 *
177 sizeof(float), 16);
178
179 vs->machine = draw->vs.machine;
180
181 x86_init_func( &vs->sse2_program );
182
183 if (!tgsi_emit_sse2( (struct tgsi_token *) vs->base.state.tokens,
184 &vs->sse2_program,
185 (float (*)[4])vs->base.immediates,
186 TRUE ))
187 goto fail;
188
189 vs->func = (tgsi_sse2_vs_func) x86_get_func( &vs->sse2_program );
190 if (!vs->func) {
191 goto fail;
192 }
193
194 return &vs->base;
195
196 fail:
197 debug_error("tgsi_emit_sse2() failed, falling back to interpreter\n");
198
199 x86_release_func( &vs->sse2_program );
200
201 FREE(vs);
202 return NULL;
203 }
204
205
206
207 #else
208
209 struct draw_vertex_shader *
210 draw_create_vs_sse( struct draw_context *draw,
211 const struct pipe_shader_state *templ )
212 {
213 return (void *) 0;
214 }
215
216
217 #endif
218