fcc9cbfec543b55fb70bc01912d3c6ed86ca07c4
[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][4],
58 float (*consts)[4]);
59
60 #if 0
61 const struct tgsi_exec_vector *input,
62 struct tgsi_exec_vector *output,
63 float (*constant)[4], /* 3 */
64 struct tgsi_exec_vector *temporary, /* 4 */
65 float (*immediates)[4], /* 5 */
66 const float (*aos_input)[4], /* 6 */
67 uint num_inputs, /* 7 */
68 uint input_stride, /* 8 */
69 float (*aos_output)[4], /* 9 */
70 uint num_outputs, /* 10 */
71 uint output_stride ); /* 11 */
72 #endif
73
74 struct draw_ppc_vertex_shader {
75 struct draw_vertex_shader base;
76 struct ppc_function ppc_program;
77
78 codegen_function func;
79
80 struct tgsi_exec_machine *machine;
81 };
82
83
84 static void
85 vs_ppc_prepare( struct draw_vertex_shader *base,
86 struct draw_context *draw )
87 {
88 }
89
90
91
92 /* Simplified vertex shader interface for the pt paths. Given the
93 * complexity of code-generating all the above operations together,
94 * it's time to try doing all the other stuff separately.
95 */
96 static void
97 vs_ppc_run_linear( struct draw_vertex_shader *base,
98 const float (*input)[4],
99 float (*output)[4],
100 const float (*constants)[4],
101 unsigned count,
102 unsigned input_stride,
103 unsigned output_stride )
104 {
105 struct draw_ppc_vertex_shader *shader = (struct draw_ppc_vertex_shader *)base;
106 struct tgsi_exec_machine *machine = shader->machine;
107 unsigned int i;
108
109 #define MAX_VERTICES 4
110
111 /* loop over verts */
112 for (i = 0; i < count; i += MAX_VERTICES) {
113 const uint max_vertices = MIN2(MAX_VERTICES, count - i);
114 float inputs_soa[PIPE_MAX_SHADER_INPUTS][4][4] ALIGN16_ATTRIB;
115 float outputs_soa[PIPE_MAX_SHADER_OUTPUTS][4][4] ALIGN16_ATTRIB;
116 float temps_soa[TGSI_EXEC_NUM_TEMPS][4][4] ALIGN16_ATTRIB;
117 uint attr;
118
119 /* convert (up to) four input verts to SoA format */
120 for (attr = 0; attr < base->info.num_inputs; attr++) {
121 const float *vIn = (const float *) input;
122 uint vert;
123 for (vert = 0; vert < max_vertices; vert++) {
124 #if 0
125 if (attr==0)
126 printf("Input v%d a%d: %f %f %f %f\n",
127 vert, attr, vIn[0], vIn[1], vIn[2], vIn[3]);
128 #endif
129 inputs_soa[attr][0][vert] = vIn[attr * 4 + 0];
130 inputs_soa[attr][1][vert] = vIn[attr * 4 + 1];
131 inputs_soa[attr][2][vert] = vIn[attr * 4 + 2];
132 inputs_soa[attr][3][vert] = vIn[attr * 4 + 3];
133 vIn += input_stride / 4;
134 }
135 }
136
137 /* run compiled shader
138 */
139 #if 0
140 shader->func(machine->Inputs,
141 machine->Outputs,
142 (float (*)[4])constants,
143 machine->Temps,
144 (float (*)[4])shader->base.immediates,
145 input,
146 base->info.num_inputs,
147 input_stride,
148 output,
149 base->info.num_outputs,
150 output_stride );
151 #else
152 shader->func(inputs_soa, outputs_soa, temps_soa,
153 (float (*)[4][4]) shader->base.immediates,
154 (float (*)[4]) constants);
155
156 /*output[0][0] = input[0][0] * 0.5;*/
157 #endif
158
159 /* convert (up to) four output verts from SoA back to AoS format */
160 for (attr = 0; attr < base->info.num_outputs; attr++) {
161 float *vOut = (float *) output;
162 uint vert;
163 for (vert = 0; vert < max_vertices; vert++) {
164 vOut[attr * 4 + 0] = outputs_soa[attr][0][vert];
165 vOut[attr * 4 + 1] = outputs_soa[attr][1][vert];
166 vOut[attr * 4 + 2] = outputs_soa[attr][2][vert];
167 vOut[attr * 4 + 3] = outputs_soa[attr][3][vert];
168 #if 0
169 if (attr==0)
170 printf("Output v%d a%d: %f %f %f %f\n",
171 vert, attr, vOut[0], vOut[1], vOut[2], vOut[3]);
172 #endif
173 vOut += output_stride / 4;
174 }
175 }
176
177 /* advance to next group of four input/output verts */
178 input = (const float (*)[4])((const char *)input + input_stride * max_vertices);
179 output = (float (*)[4])((char *)output + output_stride * max_vertices);
180 }
181 }
182
183
184
185
186 static void
187 vs_ppc_delete( struct draw_vertex_shader *base )
188 {
189 struct draw_ppc_vertex_shader *shader = (struct draw_ppc_vertex_shader *)base;
190
191 ppc_release_func( &shader->ppc_program );
192
193 align_free( (void *) shader->base.immediates );
194
195 FREE( (void*) shader->base.state.tokens );
196 FREE( shader );
197 }
198
199
200 struct draw_vertex_shader *
201 draw_create_vs_ppc(struct draw_context *draw,
202 const struct pipe_shader_state *templ)
203 {
204 struct draw_ppc_vertex_shader *vs;
205
206 vs = CALLOC_STRUCT( draw_ppc_vertex_shader );
207 if (vs == NULL)
208 return NULL;
209
210 /* we make a private copy of the tokens */
211 vs->base.state.tokens = tgsi_dup_tokens(templ->tokens);
212 if (!vs->base.state.tokens)
213 goto fail;
214
215 tgsi_scan_shader(templ->tokens, &vs->base.info);
216
217 vs->base.draw = draw;
218 #if 0
219 if (1)
220 vs->base.create_varient = draw_vs_varient_aos_ppc;
221 else
222 #endif
223 vs->base.create_varient = draw_vs_varient_generic;
224 vs->base.prepare = vs_ppc_prepare;
225 vs->base.run_linear = vs_ppc_run_linear;
226 vs->base.delete = vs_ppc_delete;
227
228 vs->base.immediates = align_malloc(TGSI_EXEC_NUM_IMMEDIATES * 4 * 4 *
229 sizeof(float), 16);
230
231 vs->machine = &draw->vs.machine;
232
233 ppc_init_func( &vs->ppc_program, 2000 ); /* XXX fix limit */
234
235 if (!tgsi_emit_ppc( (struct tgsi_token *) vs->base.state.tokens,
236 &vs->ppc_program,
237 (float (*)[4])vs->base.immediates,
238 TRUE ))
239 goto fail;
240
241 vs->func = (codegen_function) ppc_get_func( &vs->ppc_program );
242 if (!vs->func) {
243 goto fail;
244 }
245
246 return &vs->base;
247
248 fail:
249 debug_error("tgsi_emit_ppc() failed, falling back to interpreter\n");
250
251 ppc_release_func( &vs->ppc_program );
252
253 FREE(vs);
254 return NULL;
255 }
256
257
258
259 #else /* PIPE_ARCH_PPC */
260
261
262 struct draw_vertex_shader *
263 draw_create_vs_ppc( struct draw_context *draw,
264 const struct pipe_shader_state *templ )
265 {
266 return (void *) 0;
267 }
268
269
270 #endif /* PIPE_ARCH_PPC */