Mercurial > hg > Members > kono > Cerium
annotate TaskManager/Test/test_render/viewer.cpp @ 176:08e2bb36639b
fix
author | gongo@localhost.localdomain |
---|---|
date | Tue, 16 Dec 2008 17:01:24 +0900 |
parents | d1f2ce3bdb2f |
children | e742053c4c87 |
rev | line source |
---|---|
140 | 1 #include <SDL.h> |
109 | 2 #include "viewer.h" |
120 | 3 #include "viewer_types.h" |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
4 #include "SceneGraph.h" |
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
5 #include "scene_graph_pack.h" |
109 | 6 #include "sys.h" |
7 #include "Func.h" | |
8 #include "error.h" | |
9 #include "TaskManager.h" | |
140 | 10 #include "Keyboard.h" |
11 #include "Joystick.h" | |
109 | 12 |
13 extern void post2runLoop(void *); | |
14 extern void post2runDraw(void *); | |
15 | |
16 /* measure for FPS (Frame Per Second) */ | |
17 int start_time; | |
18 int this_time; | |
19 int frames; | |
20 | |
140 | 21 extern SceneGraphPtr scene_graph; |
22 extern SceneGraphPtr scene_graph_viewer; | |
109 | 23 |
176 | 24 /* Data Pack sent to Other CPUs (ex. SPE) */ |
109 | 25 SceneGraphPack *sgpack; |
26 PolygonPack *ppack; | |
27 SpanPackPtr spackList; | |
28 SpanPackPtr *spackList_ptr; | |
29 int spackList_length; | |
30 int spackList_length_align; | |
31 | |
140 | 32 /** |
176 | 33 * |
34 */ | |
35 | |
36 /** | |
140 | 37 * Joystick があればそれを使い、 |
38 * 無ければキーボードを返す | |
39 */ | |
40 static Pad* | |
41 create_controller(void) | |
42 { | |
43 if (SDL_NumJoysticks()) { | |
44 SDL_Joystick *joy = SDL_JoystickOpen(0); | |
45 if (!joy) { | |
46 fprintf(stderr, "%s: failed to open joystick", __FUNCTION__); | |
47 return new Keyboard; | |
48 } else { | |
49 printf("Use Joystick\n"); | |
50 return new Joystick(joy); | |
51 } | |
52 } else { | |
53 printf("Use Keyboard\n"); | |
54 return new Keyboard; | |
55 } | |
56 } | |
57 | |
58 | |
109 | 59 Viewer::Viewer(int b, int w, int h, int _num) |
60 { | |
61 bpp = b; | |
62 width = w; | |
63 height = h; | |
64 spe_num = _num; | |
65 } | |
66 | |
67 int | |
68 Viewer::get_ticks() | |
69 { | |
70 int time; | |
71 time = SDL_GetTicks(); | |
72 return time; | |
73 } | |
74 | |
75 bool | |
76 Viewer::quit_check() | |
77 { | |
78 SDL_Event event; | |
79 | |
80 while(SDL_PollEvent(&event)) { | |
81 if (event.type==SDL_QUIT) { | |
82 return true; | |
83 } | |
84 } | |
85 | |
86 Uint8 *keys=SDL_GetKeyState(NULL); | |
87 | |
88 if (keys[SDLK_q] == SDL_PRESSED) { | |
89 return true; | |
90 } | |
91 | |
92 return false; | |
93 } | |
94 | |
95 void | |
96 Viewer::quit() | |
97 { | |
98 SDL_Quit(); | |
99 } | |
100 | |
101 void | |
102 Viewer::swap_buffers() | |
103 { | |
104 SDL_GL_SwapBuffers(); | |
105 } | |
106 | |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
107 extern void node_init(void); |
144 | 108 extern void create_cube_split(int); |
153
c8c8bb4e9e58
画像のサイズテストを加える -sg = [2,3,4]
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
150
diff
changeset
|
109 extern void create_snake_bg(int); |
158 | 110 extern void universe_init(void); |
142 | 111 |
109 | 112 void |
144 | 113 Viewer::run_init(char *xml, int sg_number) |
109 | 114 { |
115 HTaskPtr task_next; | |
167
c8b868871dce
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
158
diff
changeset
|
116 HTaskPtr task_tex; |
109 | 117 |
118 start_time = get_ticks(); | |
119 this_time = 0; | |
120 frames = 0; | |
121 | |
140 | 122 //scene_graph = SceneGraph::createFromXMLfile(xml); |
123 /** | |
124 * これを実行すると、 | |
125 * scene_graph にオリジナルの SceneGraph Object のリストが、 | |
126 * scene_graph_viewer に描画用の SceneGraph が生成される | |
127 */ | |
142 | 128 //SceneGraph::createFromXMLfile(xml); |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
129 //polygon->viewer = this; |
140 | 130 |
144 | 131 switch (sg_number) { |
132 case 0: | |
133 case 1: | |
134 create_cube_split(sg_number); | |
135 break; | |
136 case 2: | |
153
c8c8bb4e9e58
画像のサイズテストを加える -sg = [2,3,4]
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
150
diff
changeset
|
137 case 3: |
c8c8bb4e9e58
画像のサイズテストを加える -sg = [2,3,4]
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
150
diff
changeset
|
138 case 4: |
c8c8bb4e9e58
画像のサイズテストを加える -sg = [2,3,4]
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
150
diff
changeset
|
139 create_snake_bg(sg_number); |
144 | 140 break; |
153
c8c8bb4e9e58
画像のサイズテストを加える -sg = [2,3,4]
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
150
diff
changeset
|
141 case 5: |
158 | 142 universe_init(); |
143 break; | |
144 case 6: | |
144 | 145 node_init(); |
146 break; | |
147 default: | |
148 node_init(); | |
149 break; | |
150 } | |
142 | 151 |
140 | 152 scene_graph->controller = create_controller(); |
109 | 153 |
154 sgpack = (SceneGraphPack*)manager->malloc(sizeof(SceneGraphPack)); | |
155 sgpack->init(); | |
156 ppack = (PolygonPack*)manager->malloc(sizeof(PolygonPack)); | |
157 | |
158 spackList_length = (this->height + split_screen_h - 1) / split_screen_h; | |
159 spackList = (SpanPack*)manager->malloc(sizeof(SpanPack)*spackList_length); | |
160 | |
176 | 161 /** |
162 * SPU に送る address list は 16 バイト倍数でないといけない。 | |
163 * spackList_length*sizeof(SpanPack*) が 16 バイト倍数になるような | |
164 * length_align を求めている。はみ出した部分は使われない | |
165 * (ex) spackList_length が 13 の場合 | |
166 * spackList_length_align = 16; | |
167 * 実際に送るデータは64バイトになるのでOK | |
168 * 14,15,16 の部分は何も入らない。 | |
169 */ | |
109 | 170 spackList_length_align = (spackList_length + 3)&(~3); |
171 | |
172 /* 各 SPU が持つ、SpanPack の address list */ | |
167
c8b868871dce
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
158
diff
changeset
|
173 spackList_ptr = |
c8b868871dce
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
158
diff
changeset
|
174 (SpanPack**)manager->malloc(sizeof(SpanPack*)*spackList_length_align); |
109 | 175 |
176 for (int i = 0; i < spackList_length; i++) { | |
177 spackList_ptr[i] = &spackList[i]; | |
178 } | |
179 | |
126
7635f223fc7d
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
180 for (int i = 1; i <= spackList_length; i++) { |
7635f223fc7d
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
181 spackList[i-1].init(i*split_screen_h); |
7635f223fc7d
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
182 } |
7635f223fc7d
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
183 |
109 | 184 task_next = manager->create_task(TASK_DUMMY); |
185 task_next->set_post(&post2runLoop, NULL); | |
186 | |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
187 #if 0 |
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
188 // 諸事情で、今は SceneGraphPack を作らずに |
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
189 // そのまま SceneGraph でやっています |
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
190 HTaskPtr task_sgp; |
109 | 191 task_sgp = manager->create_task(TASK_CREATE_SGP); |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
192 task_sgp->add_param((uint32)scene_graph); |
109 | 193 task_sgp->add_param((uint32)sgpack); |
194 task_next->wait_for(task_sgp); | |
195 task_sgp->spawn(); | |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
196 #endif |
109 | 197 |
167
c8b868871dce
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
158
diff
changeset
|
198 for (int i = 0; i < spe_num; i++) { |
c8b868871dce
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
158
diff
changeset
|
199 task_tex = manager->create_task(TASK_INIT_TEXTURE); |
176 | 200 /* ここはもう少しわかりやすい使い方がいいかもしれぬ。こんなもん? */ |
167
c8b868871dce
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
158
diff
changeset
|
201 task_tex->set_cpu((CPU_TYPE)((int)SPE_0 + i)); |
c8b868871dce
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
158
diff
changeset
|
202 task_next->wait_for(task_tex); |
c8b868871dce
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
158
diff
changeset
|
203 task_tex->spawn(); |
c8b868871dce
DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
gongo@localhost.localdomain
parents:
158
diff
changeset
|
204 } |
120 | 205 |
109 | 206 task_next->spawn(); |
207 } | |
208 | |
209 void | |
210 Viewer::run_loop(void) | |
211 { | |
212 HTaskPtr task_create_pp = NULL; | |
213 HTaskPtr task_create_sp = NULL; | |
214 HTaskPtr task_next; | |
215 bool quit_flg; | |
216 | |
217 quit_flg = quit_check(); | |
218 | |
219 if (quit_flg == true) { | |
220 this_time = get_ticks(); | |
221 run_finish(); | |
222 return; | |
223 } | |
224 | |
225 clean_pixels(); | |
226 | |
227 for (int i = 1; i <= spackList_length; i++) { | |
126
7635f223fc7d
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
228 spackList[i-1].reinit(i*split_screen_h); |
109 | 229 } |
230 | |
231 task_next = manager->create_task(TASK_DUMMY); | |
232 task_next->set_post(post2runDraw, NULL); | |
233 | |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
234 #if 0 |
157 | 235 // SceneGraphPack の update |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
236 HTaskPtr task_update_sgp = NULL; |
109 | 237 task_update_sgp = manager->create_task(TASK_UPDATE_SGP); |
238 task_update_sgp->add_inData(sgpack, sizeof(SceneGraphPack)); | |
239 task_update_sgp->add_outData(sgpack, sizeof(SceneGraphPack)); | |
240 task_update_sgp->add_param(width); | |
241 task_update_sgp->add_param(height); | |
242 task_next->wait_for(task_update_sgp); | |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
243 task_update_sgp->spawn(); |
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
244 #else |
140 | 245 scene_graph->controller->check(); |
246 scene_graph_viewer->all_execute(width, height); | |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
247 #endif |
109 | 248 |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
249 #if 0 |
157 | 250 // SceneGraphPack(配列) -> PolygonPack |
109 | 251 task_create_pp = manager->create_task(TASK_CREATE_PP); |
252 task_create_pp->add_inData(sgpack, sizeof(SceneGraphPack)); | |
253 task_create_pp->add_param((uint32)ppack); | |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
254 #else |
157 | 255 // SceneGraph(木構造) -> PolygonPack |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
256 task_create_pp = manager->create_task(TASK_CREATE_PP2); |
140 | 257 task_create_pp->add_param((uint32)scene_graph_viewer); |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
258 task_create_pp->add_param((uint32)ppack); |
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
259 #endif |
109 | 260 task_next->wait_for(task_create_pp); |
261 | |
262 int range_base = spe_num; | |
263 // 切り上げのつもり | |
264 int range = (spackList_length + range_base - 1) / range_base; | |
265 | |
266 for (int i = 0; i < range_base; i++) { | |
267 int index_start = range*i; | |
268 int index_end = (index_start + range >= spackList_length) | |
269 ? spackList_length : index_start + range; | |
270 | |
271 task_create_sp = manager->create_task(TASK_CREATE_SPAN); | |
272 task_create_sp->add_inData(ppack, sizeof(PolygonPack)); | |
273 task_create_sp->add_inData(spackList_ptr, | |
274 sizeof(SpanPack*)*spackList_length_align); | |
275 task_create_sp->add_inData(&spackList[index_start], sizeof(SpanPack)); | |
276 | |
277 task_create_sp->add_param(index_start); | |
278 | |
279 /** | |
133
435e0d24db39
DrawSpan のテクスチャ座標を求める部分で、width と height が逆に使われてた。
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
130
diff
changeset
|
280 * ex. screen_height が 480, spenum が 6 の場合、各SPEのy担当範囲 |
109 | 281 * [ 1.. 80] [ 81..160] [161..240] |
282 * [241..320] [321..400] [401..480] | |
283 * | |
284 * ex. screen_height が 1080, spenum が 5 の場合、 | |
285 * [ 1..216] [217..432] [433..648] | |
286 * [649..864] [865..1080] | |
287 */ | |
288 task_create_sp->add_param(index_start*split_screen_h + 1); | |
289 task_create_sp->add_param(index_end*split_screen_h); | |
290 | |
291 task_next->wait_for(task_create_sp); | |
292 task_create_sp->wait_for(task_create_pp); | |
293 | |
294 task_create_sp->set_cpu(SPE_ANY); | |
295 task_create_sp->spawn(); | |
296 } | |
297 | |
298 task_create_pp->spawn(); | |
299 task_next->spawn(); | |
300 } | |
301 | |
302 void | |
303 Viewer::run_draw(void) | |
304 { | |
305 HTaskPtr task_next; | |
306 HTaskPtr task_draw; | |
307 | |
308 task_next = manager->create_task(TASK_DUMMY); | |
309 task_next->set_post(post2runLoop, NULL); | |
310 | |
311 ppack->clear(); | |
312 | |
313 for (int i = 0; i < spackList_length; i++) { | |
314 SpanPack *spack = &spackList[i]; | |
315 int startx = 1; | |
316 int endx = split_screen_w; | |
317 | |
318 int start_y = spack->info.y_top - split_screen_h + 1; | |
157 | 319 //int end_y = spack->info.y_top; |
109 | 320 int rangey = (start_y + split_screen_h - 1 > this->height) |
321 ? this->height - start_y + 1 : split_screen_h; | |
322 | |
323 while (startx < this->width) { | |
324 if (spack->info.size > 0) { | |
325 // Draw SpanPack | |
326 task_draw = manager->create_task(TASK_DRAW_SPAN); | |
327 task_draw->add_inData(spack, sizeof(SpanPack)); | |
328 } else { | |
329 // Draw Background (現在は塗りつぶし) | |
330 task_draw = manager->create_task(TASK_DRAW_BACK); | |
170 | 331 task_draw->add_param(0xffffffff); |
109 | 332 } |
176 | 333 |
109 | 334 for (int k = 0; k < rangey; k++) { |
335 task_draw->add_outData( | |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
336 &pixels[(startx-1)+this->width*(k+start_y-1)], |
109 | 337 (endx - startx + 1)*sizeof(int)); |
338 } | |
339 | |
340 task_draw->add_param(startx); | |
341 task_draw->add_param(endx); | |
342 task_draw->add_param(rangey); | |
343 task_draw->set_cpu(SPE_ANY); | |
344 task_next->wait_for(task_draw); | |
345 task_draw->spawn(); | |
346 | |
347 startx += split_screen_w; | |
348 endx += split_screen_w; | |
349 | |
350 if (endx > this->width) { | |
351 endx = this->width; | |
352 } | |
353 } | |
354 } | |
355 | |
356 task_next->spawn(); | |
357 | |
358 frames++; | |
359 } | |
360 | |
361 void | |
362 Viewer::run_finish(void) | |
363 { | |
364 if (this_time != start_time) { | |
365 printf("%f FPS\n", (((float)frames)/(this_time-start_time))*1000.0); | |
366 } | |
367 | |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
368 scene_graph->delete_data(); |
140 | 369 scene_graph->controller->close(); |
137
6cf991f28c6c
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
370 delete scene_graph; |
109 | 371 |
372 quit(); | |
373 } |