1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/**************************************************************************
*
* Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
* IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
/* main() for Cell SPU code */
#include <stdio.h>
#include <assert.h>
#include <libmisc.h>
#include <spu_mfcio.h>
#include "main.h"
#include "tri.h"
#include "pipe/cell/common.h"
#include "pipe/p_defines.h"
/*
helpful headers:
/usr/lib/gcc/spu/4.1.1/include/spu_mfcio.h
/opt/ibm/cell-sdk/prototype/sysroot/usr/include/libmisc.h
*/
volatile struct cell_init_info init;
struct framebuffer fb;
uint tile[TILE_SIZE][TILE_SIZE] ALIGN16_ATTRIB;
int DefaultTag;
void
wait_on_mask(unsigned tag)
{
mfc_write_tag_mask( tag );
mfc_read_tag_status_any();
}
void
get_tile(const struct framebuffer *fb, uint tx, uint ty, uint *tile,
int tag)
{
uint offset = ty * fb->width_tiles + tx;
uint bytesPerTile = TILE_SIZE * TILE_SIZE * 4;
ubyte *src = (ubyte *) fb->start + offset * bytesPerTile;
assert(tx < fb->width_tiles);
assert(ty < fb->height_tiles);
ASSERT_ALIGN16(tile);
/*
printf("get_tile: dest: %p src: 0x%x size: %d\n",
tile, (unsigned int) src, bytesPerTile);
*/
mfc_get(tile, /* dest in local memory */
(unsigned int) src, /* src in main memory */
bytesPerTile,
tag,
0, /* tid */
0 /* rid */);
}
void
put_tile(const struct framebuffer *fb, uint tx, uint ty, const uint *tile,
int tag)
{
uint offset = ty * fb->width_tiles + tx;
uint bytesPerTile = TILE_SIZE * TILE_SIZE * 4;
ubyte *dst = (ubyte *) fb->start + offset * bytesPerTile;
assert(tx < fb->width_tiles);
assert(ty < fb->height_tiles);
ASSERT_ALIGN16(tile);
/*
printf("put_tile: src: %p dst: 0x%x size: %d\n",
tile, (unsigned int) dst, bytesPerTile);
*/
mfc_put((void *) tile, /* src in local memory */
(unsigned int) dst, /* dst in main memory */
bytesPerTile,
tag,
0, /* tid */
0 /* rid */);
}
static void
clear_tiles(const struct cell_command_clear_tiles *clear)
{
uint num_tiles = fb.width_tiles * fb.height_tiles;
uint i;
uint tile[TILE_SIZE * TILE_SIZE] ALIGN16_ATTRIB;
for (i = 0; i < TILE_SIZE * TILE_SIZE; i++)
tile[i] = clear->value;
/*
printf("SPU: %s num=%d w=%d h=%d\n",
__FUNCTION__, num_tiles, fb.width_tiles, fb.height_tiles);
*/
for (i = init.id; i < num_tiles; i += init.num_spus) {
uint tx = i % fb.width_tiles;
uint ty = i / fb.width_tiles;
put_tile(&fb, tx, ty, tile, DefaultTag);
/* XXX we don't want this here, but it fixes bad tile results */
wait_on_mask(1 << DefaultTag);
}
}
static void
render(const struct cell_command_render *render)
{
const uint num_tiles = fb.width_tiles * fb.height_tiles;
struct cell_prim_buffer prim_buffer ALIGN16_ATTRIB;
int tag = DefaultTag;
uint i, j;
/*
printf("SPU %u: RENDER buffer dst=%p src=%p size=%d\n",
init.id,
&prim_buffer, render->vertex_data, (int)sizeof(prim_buffer));
*/
ASSERT_ALIGN16(render->vertex_data);
ASSERT_ALIGN16(&prim_buffer);
/* get vertex data from main memory */
mfc_get(&prim_buffer, /* dest */
(unsigned int) render->vertex_data, /* src */
sizeof(prim_buffer), /* bytes */
tag,
0, /* tid */
0 /* rid */);
wait_on_mask( 1 << tag ); /* XXX temporary */
/* loop over tiles */
for (i = init.id; i < num_tiles; i += init.num_spus) {
uint tx = i % fb.width_tiles;
uint ty = i / fb.width_tiles;
get_tile(&fb, tx, ty, (uint *) tile, DefaultTag);
wait_on_mask(1 << DefaultTag); /* XXX temporary */
assert(render->prim_type == PIPE_PRIM_TRIANGLES);
/* loop over tris */
for (j = 0; j < render->num_verts; j += 3) {
struct prim_header prim;
/*
printf(" %u: Triangle %g,%g %g,%g %g,%g\n",
init.id,
prim_buffer.vertex[j*3+0][0][0],
prim_buffer.vertex[j*3+0][0][1],
prim_buffer.vertex[j*3+1][0][0],
prim_buffer.vertex[j*3+1][0][1],
prim_buffer.vertex[j*3+2][0][0],
prim_buffer.vertex[j*3+2][0][1]);
*/
/* pos */
COPY_4V(prim.v[0].data[0], prim_buffer.vertex[j+0][0]);
COPY_4V(prim.v[1].data[0], prim_buffer.vertex[j+1][0]);
COPY_4V(prim.v[2].data[0], prim_buffer.vertex[j+2][0]);
/* color */
COPY_4V(prim.v[0].data[1], prim_buffer.vertex[j+0][1]);
COPY_4V(prim.v[1].data[1], prim_buffer.vertex[j+1][1]);
COPY_4V(prim.v[2].data[1], prim_buffer.vertex[j+2][1]);
tri_draw(&prim, tx, ty);
}
put_tile(&fb, tx, ty, (uint *) tile, DefaultTag);
wait_on_mask(1 << DefaultTag); /* XXX temp */
}
}
/**
* Temporary/simple main loop for SPEs: Get a command, execute it, repeat.
*/
static void
main_loop(void)
{
struct cell_command cmd;
int exitFlag = 0;
printf("SPU %u: Enter main loop\n", init.id);
assert((sizeof(struct cell_command) & 0xf) == 0);
ASSERT_ALIGN16(&cmd);
while (!exitFlag) {
unsigned opcode;
int tag = 0;
printf("SPU %u: Wait for cmd...\n", init.id);
/* read/wait from mailbox */
opcode = (unsigned int) spu_read_in_mbox();
printf("SPU %u: got cmd %u\n", init.id, opcode);
/* command payload */
mfc_get(&cmd, /* dest */
(unsigned int) init.cmd, /* src */
sizeof(struct cell_command), /* bytes */
tag,
0, /* tid */
0 /* rid */);
wait_on_mask( 1 << tag );
switch (opcode) {
case CELL_CMD_EXIT:
printf("SPU %u: EXIT\n", init.id);
exitFlag = 1;
break;
case CELL_CMD_FRAMEBUFFER:
printf("SPU %u: FRAMEBUFFER: %d x %d at %p\n", init.id,
cmd.fb.width,
cmd.fb.height,
cmd.fb.start);
fb.width = cmd.fb.width;
fb.height = cmd.fb.height;
fb.width_tiles = (fb.width + TILE_SIZE - 1) / TILE_SIZE;
fb.height_tiles = (fb.height + TILE_SIZE - 1) / TILE_SIZE;
printf("SPU %u: %u x %u tiles\n",
init.id, fb.width_tiles, fb.height_tiles);
fb.start = cmd.fb.start;
break;
case CELL_CMD_CLEAR_TILES:
printf("SPU %u: CLEAR to 0x%08x\n", init.id, cmd.clear.value);
clear_tiles(&cmd.clear);
break;
case CELL_CMD_RENDER:
printf("SPU %u: RENDER %u verts, prim %u\n",
init.id, cmd.render.num_verts, cmd.render.prim_type);
render(&cmd.render);
break;
case CELL_CMD_FINISH:
printf("SPU %u: FINISH\n", init.id);
/* wait for all outstanding DMAs to finish */
mfc_write_tag_mask(~0);
mfc_read_tag_status_all();
/* send mbox message to PPU */
spu_write_out_mbox(CELL_CMD_FINISH);
break;
default:
printf("Bad opcode!\n");
}
}
printf("SPU %u: Exit main loop\n", init.id);
}
/**
* SPE entrypoint.
* Note: example programs declare params as 'unsigned long long' but
* that doesn't work.
*/
int
main(unsigned long speid, unsigned long argp)
{
int tag = 0;
(void) speid;
DefaultTag = 1;
printf("SPU: main() speid=%lu\n", speid);
mfc_get(&init, /* dest */
(unsigned int) argp, /* src */
sizeof(struct cell_init_info), /* bytes */
tag,
0, /* tid */
0 /* rid */);
wait_on_mask( 1 << tag );
main_loop();
return 0;
}
|