Mercurial > hg > Members > kono > Cerium
annotate Renderer/Engine/viewer.cc @ 576:cc989924f469
minor fix
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 23 Oct 2009 17:02:47 +0900 |
parents | 0f13810d4492 |
children | 1733f3cbfa28 |
rev | line source |
---|---|
507 | 1 #include <SDL.h> |
2 #include "viewer.h" | |
3 #include "viewer_types.h" | |
4 #include "SceneGraph.h" | |
5 #include "SceneGraphRoot.h" | |
6 #include "scene_graph_pack.h" | |
7 #include "sys.h" | |
8 #include "Func.h" | |
9 #include "error.h" | |
10 #include "TaskManager.h" | |
11 #include <wchar.h> | |
12 #include "Pad.h" | |
511 | 13 #include "Application.h" |
575
0f13810d4492
Linda API worked. (slightly unreliable)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
572
diff
changeset
|
14 #include "lindaapi.h" |
507 | 15 |
523 | 16 static void post2runLoop(SchedTask *s,void *viewer,void *s1); |
17 static void post2runDraw(SchedTask *s,void *viewer,void *s1); | |
18 static void post2speRendering(SchedTask *s,void *viewer,void *s1); | |
19 static void post2speDraw(SchedTask *s,void *viewer,void *s1); | |
20 static void post2runMoveDrawLoop(SchedTask *s,void *viewer,void *s1); | |
507 | 21 |
22 /* measure for FPS (Frame Per Second) */ | |
23 int start_time; | |
24 int this_time; | |
25 int frames; | |
514 | 26 // static void post2speRunLoop(void *viewer); |
27 //static void post2runMove(void *viewer); | |
28 //static void post2exchange_sgroot(void *viewer); | |
29 //static void post2speRunLoop(void *viewer); | |
507 | 30 |
31 //SceneGraphRootPtr sgroot_2; | |
32 | |
33 /* Data Pack sent to Other CPUs (ex. SPE) */ | |
34 SceneGraphPack *sgpack; | |
35 PolygonPack *ppack; | |
36 SpanPackPtr spackList; | |
37 SpanPackPtr *spackList_ptr; | |
38 | |
39 int spackList_length; | |
40 int spackList_length_align; | |
41 | |
42 /** | |
43 * | |
44 */ | |
45 | |
46 Viewer::Viewer(int b, int w, int h, int _num) | |
47 { | |
48 bpp = b; | |
49 width = w; | |
50 height = h; | |
51 spe_num = _num; | |
52 } | |
53 | |
54 int | |
55 Viewer::get_ticks(void) | |
56 { | |
57 int time; | |
58 time = SDL_GetTicks(); | |
59 return time; | |
60 } | |
61 | |
62 bool | |
63 Viewer::quit_check(void) | |
64 { | |
65 SDL_Event event; | |
66 | |
67 while(SDL_PollEvent(&event)) { | |
68 if (event.type==SDL_QUIT) { | |
69 return true; | |
70 } | |
71 } | |
72 | |
73 Uint8 *keys=SDL_GetKeyState(NULL); | |
74 | |
75 if (keys[SDLK_q] == SDL_PRESSED) { | |
76 return true; | |
77 } | |
78 | |
79 return false; | |
80 } | |
81 | |
82 void | |
83 Viewer::quit(void) | |
84 { | |
85 SDL_Quit(); | |
86 } | |
87 | |
88 void | |
89 Viewer::swap_buffers(void) | |
90 { | |
91 SDL_GL_SwapBuffers(); | |
92 } | |
93 | |
94 | |
95 void | |
509
8148c81d2660
Cerium Rendering Library
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
507
diff
changeset
|
96 Viewer::run_init(TaskManager *manager, Application *app) |
507 | 97 { |
98 this->manager = manager; | |
99 | |
100 start_time = get_ticks(); | |
101 this_time = 0; | |
102 frames = 0; | |
103 | |
104 sgroot = new SceneGraphRoot(this->width, this->height); | |
572 | 105 sgroot->tmanager = manager; |
509
8148c81d2660
Cerium Rendering Library
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
507
diff
changeset
|
106 |
511 | 107 MainLoop *mainloop = app->init(this, this->width, this->height); |
507 | 108 |
511 | 109 mainloop->mainLoop(); |
507 | 110 } |
111 | |
112 | |
113 HTaskPtr | |
114 Viewer::initLoop() | |
115 { | |
116 HTaskPtr task_next; | |
117 HTaskPtr task_tex; | |
118 | |
119 sgpack = (SceneGraphPack*)manager->allocate(sizeof(SceneGraphPack)); | |
120 sgpack->init(); | |
121 ppack = (PolygonPack*)manager->allocate(sizeof(PolygonPack)); | |
122 | |
123 spackList_length = (this->height + split_screen_h - 1) / split_screen_h; | |
124 spackList = (SpanPack*)manager->allocate(sizeof(SpanPack)*spackList_length); | |
125 | |
126 /** | |
127 * SPU に送る address list は 16 バイト倍数でないといけない。 | |
128 * spackList_length*sizeof(SpanPack*) が 16 バイト倍数になるような | |
129 * length_align を求めている。はみ出した部分は使われない | |
130 * (ex) spackList_length が 13 の場合 | |
131 * spackList_length_align = 16; | |
132 * 実際に送るデータは64バイトになるのでOK | |
133 * 14,15,16 の部分は何も入らない。 | |
134 */ | |
135 spackList_length_align = (spackList_length + 3)&(~3); | |
136 | |
137 /* 各 SPU が持つ、SpanPack の address list */ | |
138 spackList_ptr = | |
139 (SpanPack**)manager->allocate(sizeof(SpanPack*)*spackList_length_align); | |
140 | |
141 for (int i = 0; i < spackList_length; i++) { | |
142 spackList_ptr[i] = &spackList[i]; | |
143 } | |
144 | |
145 for (int i = 1; i <= spackList_length; i++) { | |
146 spackList[i-1].init(i*split_screen_h); | |
147 } | |
148 | |
149 task_next = manager->create_task(TASK_DUMMY); | |
150 | |
151 for (int i = 0; i < spe_num; i++) { | |
152 task_tex = manager->create_task(TASK_INIT_TEXTURE); | |
153 /* | |
154 * ここはもう少しわかりやすい使い方がいいかもしれぬ。こんなもん? | |
155 */ | |
156 task_tex->set_cpu((CPU_TYPE)((int)SPE_0 + i)); | |
157 task_next->wait_for(task_tex); | |
158 task_tex->spawn(); | |
159 } | |
160 | |
161 return task_next; | |
162 } | |
163 | |
164 /* Loop って言っても1回しか実行されない */ | |
165 void | |
166 Viewer::speLoop() | |
167 { | |
168 HTaskPtr task_next = initLoop(); | |
169 // key の情報を格納する領域を確保する (global_alloc(KEY_STATUS)) | |
170 HTaskPtr init_key_task = manager->create_task(INIT_KEY_TASK); | |
171 init_key_task->set_cpu(SPE_0); | |
172 init_key_task->spawn(); | |
173 | |
174 // SPE に送信する KEY_STATUS の領域確保 | |
175 key_stat *key = (key_stat*)manager->allocate(sizeof(key_stat)); | |
176 this->keyPtr = key; | |
177 | |
178 // post2runLoop は旧バージョン用なので post2speRunLoop の様なものを別につくるべき | |
179 //task_next->set_post(post2speRunLoop, (void*)this); // set_post(function(this->run_loop()), NULL) | |
180 //task_next->spawn(); | |
181 // TASK_INIT_TEXTURE が全て終わったら DUMMY_TASK が Viewer::run_loop() を呼ぶ | |
182 | |
183 /* test */ | |
184 | |
185 HTaskPtr task_switch = manager->create_task(TASK_SWITCH); | |
186 task_switch->wait_for(task_next); | |
514 | 187 task_switch->set_post(post2runMoveDrawLoop, (void*)this, 0); |
507 | 188 task_switch->spawn(); |
189 } | |
190 | |
191 void | |
192 Viewer::getKey() | |
193 { | |
194 Pad *pad = sgroot->getController(); | |
195 if (pad->right.isHold()) { | |
196 keyPtr->right = HOLD; | |
197 } else if (pad->right.isPush()) { | |
198 keyPtr->right = PUSH; | |
199 } else { | |
200 keyPtr->right = NONE; | |
201 } | |
202 | |
203 if (pad->left.isHold()) { | |
204 keyPtr->left = HOLD; | |
205 } else if (pad->left.isPush()) { | |
206 keyPtr->left = PUSH; | |
207 } else { | |
208 keyPtr->left = NONE; | |
209 } | |
210 | |
211 if (pad->up.isHold()) { | |
212 keyPtr->up = HOLD; | |
213 } else if (pad->up.isPush()) { | |
214 keyPtr->up = PUSH; | |
215 } else { | |
216 keyPtr->up = NONE; | |
217 } | |
218 | |
219 if (pad->down.isHold()) { | |
220 keyPtr->down = HOLD; | |
221 } else if (pad->down.isPush()) { | |
222 keyPtr->down = PUSH; | |
223 } else { | |
224 keyPtr->down = NONE; | |
225 } | |
226 | |
227 if (pad->circle.isHold()) { | |
228 keyPtr->circle = HOLD; | |
229 } else if (pad->circle.isPush()) { | |
230 keyPtr->circle = PUSH; | |
231 } else { | |
232 keyPtr->circle = NONE; | |
233 } | |
234 } | |
235 | |
236 static void | |
514 | 237 post2runMoveDrawLoop(SchedTask *m, void *viewer_, void *arg) |
507 | 238 { |
239 Viewer *viewer = (Viewer*)viewer_; | |
240 | |
241 // 同じ PPE 上なので sgroot(ポインタ) を add_param で送る。 | |
242 //HTaskPtr send_key_task = viewer->manager->create_task(SEND_KEY); | |
243 //send_key_task->add_param((int)sgroot); | |
244 // set input data -> viewer keyPtr | |
245 viewer->getKey(); | |
246 HTaskPtr update_key = viewer->manager->create_task(UPDATE_KEY); | |
247 update_key->add_inData(viewer->keyPtr, sizeof(key_stat)); | |
248 update_key->set_cpu(SPE_0); | |
249 update_key->spawn(); | |
250 | |
251 /* TASK_MOVE は外から引数で取ってくるべき */ | |
252 //HTaskPtr move_task = viewer->manager->create_task(viewer->app->move_taskid); | |
253 HTaskPtr move_task = viewer->manager->create_task(TASK_MOVE); | |
254 //move_task->add_param(sgroot); | |
255 | |
256 //HTaskPtr draw_task = viewer->manager->create_task(TASK_DRAW); | |
257 | |
258 /* rendering task test */ | |
259 HTaskPtr draw_task = viewer->manager->create_task(TASK_DUMMY); | |
260 HTaskPtr draw_dummy = viewer->manager->create_task(TASK_DUMMY); | |
261 | |
262 HTaskPtr switch_task = viewer->manager->create_task(TASK_SWITCH); | |
263 viewer->draw_dummy = draw_dummy; | |
264 switch_task->wait_for(draw_dummy); | |
514 | 265 draw_task->set_post(post2speRendering, (void*)viewer, 0); |
507 | 266 |
267 switch_task->wait_for(move_task); | |
268 switch_task->wait_for(draw_task); | |
269 move_task->spawn(); | |
270 draw_task->spawn(); | |
271 | |
514 | 272 switch_task->set_post(post2runMoveDrawLoop, (void*)viewer, 0); |
507 | 273 switch_task->spawn(); |
274 | |
275 } | |
276 | |
277 #if 0 | |
278 static void | |
279 post2speRunLoop(void *viewer_) | |
280 { | |
281 Viewer *viewer = (Viewer *)viewer_; | |
282 HTaskPtr task_next = viewer->manager->create_task(TASK_DUMMY); | |
283 viewer->run_move(task_next); | |
284 } | |
285 #endif | |
286 | |
287 void | |
288 Viewer::mainLoop() | |
289 { | |
290 HTaskPtr task_next = initLoop(); | |
291 | |
514 | 292 task_next->set_post(&post2runLoop, (void *)this, 0); // set_post(function(this->run_loop()), NULL) |
507 | 293 task_next->spawn(); |
294 } | |
295 | |
296 #if 0 | |
297 void | |
298 post2runMove(void *viewer_) | |
299 { | |
300 Viewer *viewer = (Viewer*)viewer_; | |
301 HTaskPtr task_next = viewer->manager->create_task(TASK_DUMMY); | |
302 viewer->run_move(task_next); | |
303 } | |
304 #endif | |
305 | |
306 | |
307 void | |
308 Viewer::run_move(HTaskPtr task_next) | |
309 { | |
310 sgroot->updateControllerState(); | |
311 sgroot->allExecute(width, height); | |
312 } | |
313 | |
314 void | |
315 Viewer::run_collision() | |
316 { | |
317 } | |
318 | |
319 void | |
514 | 320 post2rendering(SchedTask *s, void *viewer_, void *arg) |
507 | 321 { |
322 Viewer *viewer = (Viewer *)viewer_; | |
323 HTaskPtr task_next = viewer->manager->create_task(TASK_DUMMY); | |
324 viewer->rendering(task_next); | |
325 } | |
326 | |
327 void | |
328 Viewer::rendering(HTaskPtr task_next) | |
329 { | |
330 common_rendering(task_next); | |
331 | |
332 // Barrier 同期 | |
333 // run_draw() を呼ぶ post2runDraw | |
514 | 334 task_next->set_post(post2runDraw, (void*)this, 0); // set_post(function(this->run_draw()), NULL) |
507 | 335 task_next->spawn(); |
336 | |
337 // TASK_CREATE_SPAN が全て終わったら DUMMY_TASK が Viewer::run_draw() を呼ぶ | |
338 } | |
339 | |
340 static void | |
514 | 341 post2runLoop(SchedTask *s, void *viewer_, void *arg) |
507 | 342 { |
343 Viewer *viewer = (Viewer*)viewer_; | |
344 HTaskPtr task_next = viewer->manager->create_task(TASK_DUMMY); | |
345 viewer->run_loop(task_next); | |
346 | |
575
0f13810d4492
Linda API worked. (slightly unreliable)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
572
diff
changeset
|
347 psx_sync_n(); |
507 | 348 } |
349 | |
350 void | |
351 Viewer::run_loop(HTaskPtr task_next) | |
352 { | |
353 bool quit_flg; | |
354 quit_flg = quit_check(); | |
355 if (quit_flg == true) { | |
356 this_time = get_ticks(); | |
357 run_finish(); | |
358 return; | |
359 } | |
360 | |
361 clean_pixels(); | |
362 | |
363 for (int i = 1; i <= spackList_length; i++) { | |
364 spackList[i-1].reinit(i*split_screen_h); | |
365 } | |
366 | |
367 //run_move(task_next); | |
368 sgroot->updateControllerState(); | |
369 sgroot->allExecute(width, height); | |
370 //sgroot->checkRemove(); | |
371 | |
372 // ここから下は Rendering という関数にする | |
373 rendering(task_next); | |
374 } | |
375 | |
376 static void | |
514 | 377 post2runDraw(SchedTask *s, void *viewer_, void *arg) |
507 | 378 { |
379 Viewer *viewer = (Viewer*)viewer_; | |
380 HTaskPtr task_next = viewer->manager->create_task(TASK_DUMMY); | |
381 viewer->run_draw(task_next); | |
382 | |
383 } | |
384 | |
385 void | |
386 Viewer::run_draw(HTaskPtr task_next) // 引数に post2runLoop を入れるようにする | |
387 { | |
388 common_draw(task_next); | |
389 | |
514 | 390 task_next->set_post(post2runLoop, (void*)this, 0); // set_post(function(this->run_loop()), NULL) |
507 | 391 task_next->spawn(); |
392 // TASK_DRAW_SPAN が全て終わったら DUMMY_TASK が Viewer::run_loop() を呼ぶ | |
393 | |
394 frames++; | |
395 } | |
396 | |
397 void | |
398 Viewer::run_finish(void) | |
399 { | |
400 if (this_time != start_time) { | |
401 printf("%f FPS\n", (((float)frames)/(this_time-start_time))*1000.0); | |
402 } | |
403 | |
404 delete sgroot; | |
405 // delete sgroot_2; | |
406 quit(); | |
407 } | |
408 | |
409 static void | |
514 | 410 post2speRendering(SchedTask *s, void *viewer_, void *arg) |
507 | 411 { |
412 Viewer *viewer = (Viewer*)viewer_; | |
413 HTaskPtr task_next = viewer->manager->create_task(TASK_DUMMY); | |
414 viewer->spe_rendering(task_next); | |
415 } | |
416 | |
417 void | |
418 Viewer::spe_rendering(HTaskPtr task_next) | |
419 { | |
420 common_rendering(task_next); | |
421 | |
422 this->draw_dummy->wait_for(task_next); | |
514 | 423 task_next->set_post(post2speDraw, (void*)this, 0); |
507 | 424 task_next->spawn(); |
425 | |
426 } | |
427 | |
428 static void | |
514 | 429 post2speDraw(SchedTask *s, void *viewer_, void *arg) |
507 | 430 { |
431 Viewer *viewer = (Viewer*)viewer_; | |
432 HTaskPtr task_next = viewer->manager->create_task(TASK_DUMMY); | |
433 viewer->spe_draw(task_next); | |
434 } | |
435 | |
436 void | |
437 Viewer::spe_draw(HTaskPtr task_next) | |
438 { | |
439 common_draw(task_next); | |
440 | |
441 this->draw_dummy->wait_for(task_next); | |
442 task_next->spawn(); | |
443 this->draw_dummy->spawn(); | |
444 | |
445 frames++; | |
446 } | |
447 | |
448 void | |
449 Viewer::common_rendering(HTaskPtr task_next) | |
450 { | |
451 HTaskPtr task_create_pp = manager->create_task(TASK_CREATE_PP2); | |
452 | |
453 // SceneGraph(木構造) -> PolygonPack | |
454 | |
455 task_create_pp->add_param((uint32)sgroot->getDrawSceneGraph()); | |
456 task_create_pp->add_param((uint32)ppack); | |
457 | |
458 task_next->wait_for(task_create_pp); | |
459 | |
460 int range_base = spe_num; | |
461 // 切り上げのつもり | |
462 int range = (spackList_length + range_base - 1) / range_base; | |
463 | |
464 for (int i = 0; i < range_base; i++) { | |
465 int index_start = range*i; | |
466 int index_end = (index_start + range >= spackList_length) | |
467 ? spackList_length : index_start + range; | |
468 | |
469 HTaskPtr task_create_sp = manager->create_task(TASK_CREATE_SPAN); | |
470 task_create_sp->add_inData(ppack, sizeof(PolygonPack)); | |
471 task_create_sp->add_inData(spackList_ptr, | |
472 sizeof(SpanPack*)*spackList_length_align); | |
473 task_create_sp->add_inData(&spackList[index_start], sizeof(SpanPack)); | |
474 | |
475 task_create_sp->add_param(index_start); | |
476 | |
477 /** | |
478 * ex. screen_height が 480, spenum が 6 の場合、各SPEのy担当範囲 | |
479 * [ 1.. 80] [ 81..160] [161..240] | |
480 * [241..320] [321..400] [401..480] | |
481 * | |
482 * ex. screen_height が 1080, spenum が 5 の場合、 | |
483 * [ 1..216] [217..432] [433..648] | |
484 * [649..864] [865..1080] | |
485 */ | |
486 task_create_sp->add_param(index_start*split_screen_h + 1); | |
487 task_create_sp->add_param(index_end*split_screen_h); | |
488 | |
489 task_next->wait_for(task_create_sp); | |
490 task_create_sp->wait_for(task_create_pp); | |
491 | |
492 task_create_sp->set_cpu(SPE_ANY); | |
493 task_create_sp->spawn(); | |
494 } | |
495 | |
496 task_create_pp->spawn(); | |
497 } | |
498 | |
499 void | |
500 Viewer::common_draw(HTaskPtr task_next) | |
501 { | |
502 HTaskPtr task_draw; | |
503 | |
504 //task_next = manager->create_task(TASK_DUMMY); | |
505 //task_next->set_post(post2runLoop, (void*)this); | |
506 | |
507 ppack->clear(); | |
508 for (int i = 0; i < spackList_length; i++) { | |
509 SpanPack *spack = &spackList[i]; | |
510 int startx = 1; | |
511 int endx = split_screen_w; | |
512 | |
513 int starty = spack->info.y_top - split_screen_h + 1; | |
514 //int endy = spack->info.y_top; | |
515 int rangey = (starty + split_screen_h - 1 > this->height) | |
516 ? this->height - starty + 1 : split_screen_h; | |
517 | |
518 while (startx < this->width) { | |
519 if (spack->info.size > 0) { | |
520 // Draw SpanPack | |
521 task_draw = manager->create_task(TASK_DRAW_SPAN); | |
522 task_draw->add_inData(spack, sizeof(SpanPack)); | |
523 | |
524 task_draw->add_param( | |
525 (uint32)&pixels[(startx-1) + this->width*(starty-1)]); | |
526 task_draw->add_param(this->width); | |
527 } else { | |
528 // 7.7.3 SL1 Data Cache Range Set to Zero コマンド | |
529 // を使って、DMAでclearするべき... ということは、 | |
530 // それもSPEでやる方が良い? | |
531 memset(&pixels[(startx-1)+this->width*(starty-1)], | |
532 0, (this->width)*sizeof(int)*rangey); | |
533 break; | |
534 } | |
535 | |
536 task_draw->add_param(startx); | |
537 task_draw->add_param(endx); | |
538 task_draw->add_param(rangey); | |
539 task_draw->set_cpu(SPE_ANY); | |
540 task_next->wait_for(task_draw); | |
541 task_draw->spawn(); | |
542 | |
543 startx += split_screen_w; | |
544 endx += split_screen_w; | |
545 | |
546 if (endx > this->width) { | |
547 endx = this->width; | |
548 } | |
549 } | |
550 } | |
551 } | |
552 | |
553 /* end */ |