Mercurial > hg > Members > kono > Cerium
annotate TaskManager/Test/test_render/viewer.cpp @ 132:e7c80537b6aa
add XML
author | gongo@charles.cr.ie.u-ryukyu.ac.jp |
---|---|
date | Tue, 25 Nov 2008 18:19:53 +0900 |
parents | 8a33f4859ed8 |
children | 435e0d24db39 |
rev | line source |
---|---|
109 | 1 #include "viewer.h" |
120 | 2 #include "viewer_types.h" |
109 | 3 #include "polygon.h" |
4 #include "sys.h" | |
5 #include "Func.h" | |
6 #include "error.h" | |
7 #include "TaskManager.h" | |
8 | |
9 extern void post2runLoop(void *); | |
10 extern void post2runDraw(void *); | |
11 | |
12 /* measure for FPS (Frame Per Second) */ | |
13 int start_time; | |
14 int this_time; | |
15 int frames; | |
16 | |
17 Polygon *polygon; | |
18 | |
19 /* Data Pack */ | |
20 SceneGraphPack *sgpack; | |
21 PolygonPack *ppack; | |
22 SpanPackPtr spackList; | |
23 SpanPackPtr *spackList_ptr; | |
24 int spackList_length; | |
25 int spackList_length_align; | |
26 | |
27 void *__texture; | |
28 | |
29 Viewer::Viewer(int b, int w, int h, int _num) | |
30 { | |
31 bpp = b; | |
32 width = w; | |
33 height = h; | |
34 spe_num = _num; | |
35 } | |
36 | |
37 int | |
38 Viewer::get_ticks() | |
39 { | |
40 int time; | |
41 time = SDL_GetTicks(); | |
42 return time; | |
43 } | |
44 | |
45 bool | |
46 Viewer::quit_check() | |
47 { | |
48 SDL_Event event; | |
49 | |
50 while(SDL_PollEvent(&event)) { | |
51 if (event.type==SDL_QUIT) { | |
52 return true; | |
53 } | |
54 } | |
55 | |
56 Uint8 *keys=SDL_GetKeyState(NULL); | |
57 | |
58 if (keys[SDLK_q] == SDL_PRESSED) { | |
59 return true; | |
60 } | |
61 | |
62 return false; | |
63 } | |
64 | |
65 void | |
66 Viewer::quit() | |
67 { | |
68 SDL_Quit(); | |
69 } | |
70 | |
71 void | |
72 Viewer::swap_buffers() | |
73 { | |
74 SDL_GL_SwapBuffers(); | |
75 } | |
76 | |
77 void | |
78 Viewer::run_init(char *xml) | |
79 { | |
80 HTaskPtr task_next; | |
81 HTaskPtr task_sgp; | |
82 HTaskPtr task_init_tex; | |
83 | |
84 start_time = get_ticks(); | |
85 this_time = 0; | |
86 frames = 0; | |
87 | |
88 polygon = Polygon::createFromXMLfile(xml); | |
89 polygon->viewer = this; | |
90 | |
91 sgpack = (SceneGraphPack*)manager->malloc(sizeof(SceneGraphPack)); | |
92 sgpack->init(); | |
93 ppack = (PolygonPack*)manager->malloc(sizeof(PolygonPack)); | |
94 | |
95 spackList_length = (this->height + split_screen_h - 1) / split_screen_h; | |
96 spackList = (SpanPack*)manager->malloc(sizeof(SpanPack)*spackList_length); | |
97 | |
98 // SPU に送る address list は 16 倍数でないといけない。 | |
99 // spackList_length*sizeof(SpanPack*) が 16 倍数になるような | |
100 // length_align を求めている。 | |
101 spackList_length_align = (spackList_length + 3)&(~3); | |
102 | |
103 /* 各 SPU が持つ、SpanPack の address list */ | |
104 spackList_ptr = (SpanPack**)manager->malloc(sizeof(SpanPack*)*spackList_length_align); | |
105 | |
106 for (int i = 0; i < spackList_length; i++) { | |
107 spackList_ptr[i] = &spackList[i]; | |
108 } | |
109 | |
126
7635f223fc7d
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
110 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
|
111 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
|
112 } |
7635f223fc7d
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
113 |
109 | 114 task_next = manager->create_task(TASK_DUMMY); |
115 task_next->set_post(&post2runLoop, NULL); | |
116 | |
117 task_sgp = manager->create_task(TASK_CREATE_SGP); | |
118 task_sgp->add_inData(polygon, sizeof(Polygon)); | |
119 //task_sgp->add_outData(sgpack, sizeof(SceneGraphPack)); | |
120 task_sgp->add_param((uint32)sgpack); | |
121 task_next->wait_for(task_sgp); | |
122 task_sgp->spawn(); | |
123 | |
130
8a33f4859ed8
テクスチャの大きさを128x128以外にも使えるように。(若干バグ有り)
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
128
diff
changeset
|
124 int tex_width = polygon->texture_image->w; |
8a33f4859ed8
テクスチャの大きさを128x128以外にも使えるように。(若干バグ有り)
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
128
diff
changeset
|
125 int tex_height = polygon->texture_image->h; |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
126 int tex_blocksize = tex_width*tex_height*4; |
120 | 127 |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
128 __texture = (void*)manager->malloc(tex_blocksize); |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
129 memcpy(__texture, polygon->texture_image->pixels, tex_blocksize); |
120 | 130 |
131 uint32 *tex_src = (uint32*)polygon->texture_image->pixels; | |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
132 uint32 *tex_dest = (uint32*)manager->malloc(tex_blocksize); |
120 | 133 int tile_size = TEXTURE_SPLIT_PIXEL*TEXTURE_SPLIT_PIXEL; |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
134 int tile_num = tex_width*tex_height/tile_size; |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
135 tile_num = (tile_num + 15)&(~15); |
120 | 136 void **tex_addrList = (void**)manager->malloc(sizeof(void*)*tile_num); |
137 | |
138 { | |
139 int t = 0; | |
140 | |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
141 for (int y = 0; y < tex_height; y += TEXTURE_SPLIT_PIXEL) { |
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
142 for (int x = 0; x < tex_width; x += TEXTURE_SPLIT_PIXEL) { |
120 | 143 for (int j = 0; j < TEXTURE_SPLIT_PIXEL; j++) { |
144 for (int i = 0; i < TEXTURE_SPLIT_PIXEL; i++) { | |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
145 tex_dest[t++] = tex_src[(x+i) + tex_width*(y+j)]; |
120 | 146 } |
147 } | |
148 } | |
149 } | |
150 | |
151 t = 0; | |
152 | |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
153 for (int i = 0, t = 0; i < tex_width*tex_height; i += tile_size, t++) { |
120 | 154 tex_addrList[t] = (void*)&tex_dest[i]; |
155 } | |
156 } | |
157 | |
109 | 158 for (int i = 0; i < spe_num; i++) { |
159 task_init_tex = manager->create_task(TASK_INIT_TEXTURE); | |
120 | 160 task_init_tex->add_inData(tex_addrList, sizeof(void*)*tile_num); |
161 task_init_tex->add_param(tile_num); | |
109 | 162 task_init_tex->set_cpu(SPE_ANY); |
163 task_next->wait_for(task_init_tex); | |
164 task_init_tex->spawn(); | |
165 } | |
166 | |
167 task_next->spawn(); | |
168 } | |
169 | |
170 void | |
171 Viewer::run_loop(void) | |
172 { | |
173 HTaskPtr task_update_sgp = NULL; | |
174 HTaskPtr task_create_pp = NULL; | |
175 HTaskPtr task_create_sp = NULL; | |
176 HTaskPtr task_next; | |
177 bool quit_flg; | |
178 | |
179 quit_flg = quit_check(); | |
180 | |
181 if (quit_flg == true) { | |
182 this_time = get_ticks(); | |
183 run_finish(); | |
184 return; | |
185 } | |
186 | |
187 clean_pixels(); | |
188 | |
189 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
|
190 spackList[i-1].reinit(i*split_screen_h); |
109 | 191 } |
192 | |
193 task_next = manager->create_task(TASK_DUMMY); | |
194 task_next->set_post(post2runDraw, NULL); | |
195 | |
196 task_update_sgp = manager->create_task(TASK_UPDATE_SGP); | |
197 task_update_sgp->add_inData(sgpack, sizeof(SceneGraphPack)); | |
198 task_update_sgp->add_outData(sgpack, sizeof(SceneGraphPack)); | |
199 task_update_sgp->add_param(width); | |
200 task_update_sgp->add_param(height); | |
201 task_next->wait_for(task_update_sgp); | |
202 | |
203 task_create_pp = manager->create_task(TASK_CREATE_PP); | |
204 task_create_pp->add_inData(sgpack, sizeof(SceneGraphPack)); | |
205 task_create_pp->add_param((uint32)ppack); | |
206 //task_create_pp->set_cpu(SPE_ANY); | |
207 task_next->wait_for(task_create_pp); | |
208 | |
209 int range_base = spe_num; | |
210 // 切り上げのつもり | |
211 int range = (spackList_length + range_base - 1) / range_base; | |
212 | |
213 for (int i = 0; i < range_base; i++) { | |
214 int index_start = range*i; | |
215 int index_end = (index_start + range >= spackList_length) | |
216 ? spackList_length : index_start + range; | |
217 | |
218 task_create_sp = manager->create_task(TASK_CREATE_SPAN); | |
219 task_create_sp->add_inData(ppack, sizeof(PolygonPack)); | |
220 task_create_sp->add_inData(spackList_ptr, | |
221 sizeof(SpanPack*)*spackList_length_align); | |
222 task_create_sp->add_inData(&spackList[index_start], sizeof(SpanPack)); | |
223 | |
224 task_create_sp->add_param(index_start); | |
225 | |
226 /** | |
227 * ex. screen_height が 480, spenum が 6 の場合、 | |
228 * [ 1.. 80] [ 81..160] [161..240] | |
229 * [241..320] [321..400] [401..480] | |
230 * | |
231 * ex. screen_height が 1080, spenum が 5 の場合、 | |
232 * [ 1..216] [217..432] [433..648] | |
233 * [649..864] [865..1080] | |
234 */ | |
235 task_create_sp->add_param(index_start*split_screen_h + 1); | |
236 task_create_sp->add_param(index_end*split_screen_h); | |
237 | |
238 task_next->wait_for(task_create_sp); | |
239 task_create_sp->wait_for(task_create_pp); | |
240 | |
241 task_create_sp->set_cpu(SPE_ANY); | |
242 task_create_sp->spawn(); | |
243 } | |
244 | |
245 task_update_sgp->spawn(); | |
246 task_create_pp->spawn(); | |
247 task_next->spawn(); | |
248 } | |
249 | |
250 static int st_rgb = 0xffffff; | |
251 static int st_diff = 0x111111; | |
252 | |
253 void | |
254 Viewer::run_draw(void) | |
255 { | |
256 HTaskPtr task_next; | |
257 HTaskPtr task_draw; | |
258 | |
259 task_next = manager->create_task(TASK_DUMMY); | |
260 task_next->set_post(post2runLoop, NULL); | |
261 | |
262 ppack->clear(); | |
263 | |
264 if (frames % 20 == 0) { | |
265 st_rgb += st_diff; | |
266 } | |
267 if (st_rgb >= 0xeeeeee || st_rgb <= 0) { | |
268 st_diff = -st_diff; | |
269 } | |
270 | |
271 unsigned int diff = 0; | |
272 for (int i = 0; i < spackList_length; i++) { | |
273 SpanPack *spack = &spackList[i]; | |
274 int startx = 1; | |
275 int endx = split_screen_w; | |
276 | |
277 int start_y = spack->info.y_top - split_screen_h + 1; | |
278 int end_y = spack->info.y_top; | |
279 int rangey = (start_y + split_screen_h - 1 > this->height) | |
280 ? this->height - start_y + 1 : split_screen_h; | |
281 | |
282 while (startx < this->width) { | |
283 if (spack->info.size > 0) { | |
284 // Draw SpanPack | |
285 task_draw = manager->create_task(TASK_DRAW_SPAN); | |
286 task_draw->add_inData(spack, sizeof(SpanPack)); | |
287 } else { | |
288 // Draw Background (現在は塗りつぶし) | |
289 //break; | |
290 task_draw = manager->create_task(TASK_DRAW_BACK); | |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
291 task_draw->add_param(0); |
109 | 292 //task_draw->add_param(st_rgb); |
293 } | |
294 | |
295 for (int k = 0; k < rangey; k++) { | |
296 task_draw->add_outData( | |
128
776eca0daa02
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
297 &pixels[(startx-1)+this->width*(k+start_y-1)], |
109 | 298 (endx - startx + 1)*sizeof(int)); |
299 } | |
300 | |
301 task_draw->add_param(startx); | |
302 task_draw->add_param(endx); | |
303 task_draw->add_param(rangey); | |
304 task_draw->set_cpu(SPE_ANY); | |
305 task_next->wait_for(task_draw); | |
306 task_draw->spawn(); | |
307 | |
308 startx += split_screen_w; | |
309 endx += split_screen_w; | |
310 | |
311 if (endx > this->width) { | |
312 endx = this->width; | |
313 } | |
314 } | |
315 } | |
316 | |
317 task_next->spawn(); | |
318 | |
319 frames++; | |
320 } | |
321 | |
322 void | |
323 Viewer::run_finish(void) | |
324 { | |
325 if (this_time != start_time) { | |
326 printf("%f FPS\n", (((float)frames)/(this_time-start_time))*1000.0); | |
327 } | |
328 | |
329 polygon->delete_data(); | |
330 delete polygon; | |
331 | |
332 free(__texture); | |
333 quit(); | |
334 } |