Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / auxiliary / draw / draw_vs_ppc.c
1 /**************************************************************************
2 *
3 * Copyright 2008 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_PPC)
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_ppc.h"
49 #include "tgsi/tgsi_ppc.h"
50 #include "tgsi/tgsi_parse.h"
51
52
53
54 typedef void (PIPE_CDECL *codegen_function) (float (*inputs)[4][4],
55 float (*outputs)[4][4],
56 float (*temps)[4][4],
57 float (*immeds)[4],
58 float (*consts)[4],
59 const float *builtins);
60
61
62 struct draw_ppc_vertex_shader {
63 struct draw_vertex_shader base;
64 struct ppc_function ppc_program;
65
66 codegen_function func;
67 };
68
69
70 static void
71 vs_ppc_prepare( struct draw_vertex_shader *base,
72 struct draw_context *draw )
73 {
74 /* nothing */
75 }
76
77
78 /**
79 * Simplified vertex shader interface for the pt paths. Given the
80 * complexity of code-generating all the above operations together,
81 * it's time to try doing all the other stuff separately.
82 */
83 static void
84 vs_ppc_run_linear( struct draw_vertex_shader *base,
85 const float (*input)[4],
86 float (*output)[4],
87 const float (*constants)[4],
88 unsigned count,
89 unsigned input_stride,
90 unsigned output_stride )
91 {
92 struct draw_ppc_vertex_shader *shader = (struct draw_ppc_vertex_shader *)base;
93 unsigned int i;
94
95 #define MAX_VERTICES 4
96
97 /* loop over verts */
98 for (i = 0; i < count; i += MAX_VERTICES) {
99 const uint max_vertices = MIN2(MAX_VERTICES, count - i);
100 float inputs_soa[PIPE_MAX_SHADER_INPUTS][4][4] ALIGN16_ATTRIB;
101 float outputs_soa[PIPE_MAX_SHADER_OUTPUTS][4][4] ALIGN16_ATTRIB;
102 float temps_soa[TGSI_EXEC_NUM_TEMPS][4][4] ALIGN16_ATTRIB;
103 uint attr;
104
105 /* convert (up to) four input verts to SoA format */
106 for (attr = 0; attr < base->info.num_inputs; attr++) {
107 const float *vIn = (const float *) input;
108 uint vert;
109 for (vert = 0; vert < max_vertices; vert++) {
110 #if 0
111 if (attr==0)
112 printf("Input v%d a%d: %f %f %f %f\n",
113 vert, attr, vIn[0], vIn[1], vIn[2], vIn[3]);
114 #endif
115 inputs_soa[attr][0][vert] = vIn[attr * 4 + 0];
116 inputs_soa[attr][1][vert] = vIn[attr * 4 + 1];
117 inputs_soa[attr][2][vert] = vIn[attr * 4 + 2];
118 inputs_soa[attr][3][vert] = vIn[attr * 4 + 3];
119 vIn += input_stride / 4;
120 }
121 }
122
123 /* run compiled shader
124 */
125 shader->func(inputs_soa, outputs_soa, temps_soa,
126 (float (*)[4]) shader->base.immediates,
127 (float (*)[4]) constants,
128 ppc_builtin_constants);
129
130 /* convert (up to) four output verts from SoA back to AoS format */
131 for (attr = 0; attr < base->info.num_outputs; attr++) {
132 float *vOut = (float *) output;
133 uint vert;
134 for (vert = 0; vert < max_vertices; vert++) {
135 vOut[attr * 4 + 0] = outputs_soa[attr][0][vert];
136 vOut[attr * 4 + 1] = outputs_soa[attr][1][vert];
137 vOut[attr * 4 + 2] = outputs_soa[attr][2][vert];
138 vOut[attr * 4 + 3] = outputs_soa[attr][3][vert];
139 #if 0
140 if (attr==0)
141 printf("Output v%d a%d: %f %f %f %f\n",
142 vert, attr, vOut[0], vOut[1], vOut[2], vOut[3]);
143 #endif
144 vOut += output_stride / 4;
145 }
146 }
147
148 /* advance to next group of four input/output verts */
149 input = (const float (*)[4])((const char *)input + input_stride * max_vertices);
150 output = (float (*)[4])((char *)output + output_stride * max_vertices);
151 }
152 }
153
154
155 static void
156 vs_ppc_delete( struct draw_vertex_shader *base )
157 {
158 struct draw_ppc_vertex_shader *shader = (struct draw_ppc_vertex_shader *)base;
159
160 ppc_release_func( &shader->ppc_program );
161
162 align_free( (void *) shader->base.immediates );
163
164 FREE( (void*) shader->base.state.tokens );
165 FREE( shader );
166 }
167
168
169 struct draw_vertex_shader *
170 draw_create_vs_ppc(struct draw_context *draw,
171 const struct pipe_shader_state *templ)
172 {
173 struct draw_ppc_vertex_shader *vs;
174
175 vs = CALLOC_STRUCT( draw_ppc_vertex_shader );
176 if (vs == NULL)
177 return NULL;
178
179 /* we make a private copy of the tokens */
180 vs->base.state.tokens = tgsi_dup_tokens(templ->tokens);
181 if (!vs->base.state.tokens)
182 goto fail;
183
184 tgsi_scan_shader(templ->tokens, &vs->base.info);
185
186 vs->base.draw = draw;
187 #if 0
188 if (1)
189 vs->base.create_varient = draw_vs_varient_aos_ppc;
190 else
191 #endif
192 vs->base.create_varient = draw_vs_varient_generic;
193 vs->base.prepare = vs_ppc_prepare;
194 vs->base.run_linear = vs_ppc_run_linear;
195 vs->base.delete = vs_ppc_delete;
196
197 vs->base.immediates = align_malloc(TGSI_EXEC_NUM_IMMEDIATES * 4 *
198 sizeof(float), 16);
199
200 ppc_init_func( &vs->ppc_program );
201
202 if (!tgsi_emit_ppc( (struct tgsi_token *) vs->base.state.tokens,
203 &vs->ppc_program,
204 (float (*)[4]) vs->base.immediates,
205 TRUE ))
206 goto fail;
207
208 vs->func = (codegen_function) ppc_get_func( &vs->ppc_program );
209 if (!vs->func) {
210 goto fail;
211 }
212
213 return &vs->base;
214
215 fail:
216 /*
217 debug_error("tgsi_emit_ppc() failed, falling back to interpreter\n");
218 */
219
220 ppc_release_func( &vs->ppc_program );
221
222 FREE(vs);
223 return NULL;
224 }
225
226
227
228 #else /* PIPE_ARCH_PPC */
229
230
231 struct draw_vertex_shader *
232 draw_create_vs_ppc( struct draw_context *draw,
233 const struct pipe_shader_state *templ )
234 {
235 return (void *) 0;
236 }
237
238
239 #endif /* PIPE_ARCH_PPC */