26
|
1 #include <iostream>
|
|
2 #include <SDL.h>
|
|
3 #include "polygon.h"
|
|
4 #include "viewer.h"
|
|
5 #include "sys.h"
|
81
|
6 #include "spu_span.h"
|
105
|
7 #include "Func.h"
|
26
|
8 using namespace std;
|
|
9
|
28
|
10 #define redMask 0x00ff0000
|
|
11 #define greenMask 0x0000ff00
|
|
12 #define blueMask 0x000000ff
|
|
13 #define alphaMask 0
|
|
14
|
48
|
15 extern int create_sgp(Polygon *sg, SceneGraphPack *sgp);
|
26
|
16
|
|
17 Viewer::Viewer(int b, int w, int h)
|
|
18 {
|
28
|
19 bpp = b;
|
|
20 width = w;
|
|
21 height = h;
|
26
|
22 }
|
|
23
|
102
|
24 extern int get_fbdev_addr(void);
|
|
25 unsigned int fbdev_addr;
|
26
|
26
|
|
27 void Viewer::sdl_init()
|
|
28 {
|
67
|
29 if (SDL_Init( SDL_INIT_VIDEO ) < 0) {
|
28
|
30 fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
|
|
31 exit( 1 );
|
26
|
32 }
|
89
|
33
|
102
|
34 #ifdef _DEBUG
|
67
|
35 screen = SDL_SetVideoMode( width, height, bpp, SDL_HWSURFACE);
|
|
36 if (screen == NULL) {
|
|
37 fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
|
|
38 SDL_Quit();
|
|
39 exit(1);
|
|
40 }
|
70
|
41 #else
|
102
|
42
|
|
43 fbdev_addr = get_fbdev_addr();
|
|
44 # if 0
|
71
|
45 void *_pixels = new Uint32[width*height*32/8];
|
70
|
46 screen = SDL_CreateRGBSurfaceFrom(_pixels, width, height, 32,
|
|
47 width*4, redMask, greenMask,
|
|
48 blueMask, alphaMask);
|
102
|
49 # else
|
|
50 screen = SDL_CreateRGBSurfaceFrom((void*)fbdev_addr, width, height, 32,
|
|
51 width*4, redMask, greenMask,
|
|
52 blueMask, alphaMask);
|
|
53 # endif
|
70
|
54 #endif
|
26
|
55 }
|
|
56
|
|
57
|
|
58 int Viewer::get_ticks()
|
|
59 {
|
28
|
60 int time;
|
|
61 time = SDL_GetTicks();
|
|
62 return time;
|
26
|
63 }
|
|
64
|
|
65 bool Viewer::quit_check()
|
|
66 {
|
28
|
67 bool quit = false;
|
|
68 SDL_Event event;
|
|
69 while(SDL_PollEvent(&event))
|
26
|
70 {
|
28
|
71 if(event.type==SDL_QUIT)
|
26
|
72 {
|
28
|
73 quit = true;
|
|
74 return quit;
|
26
|
75 }
|
|
76 }
|
28
|
77 return quit;
|
26
|
78 }
|
|
79
|
|
80 void Viewer::quit()
|
|
81 {
|
28
|
82 SDL_Quit();
|
26
|
83 }
|
|
84
|
|
85
|
|
86 void Viewer::swap_buffers()
|
|
87 {
|
28
|
88 SDL_GL_SwapBuffers();
|
26
|
89 }
|
|
90
|
|
91
|
51
|
92 void
|
|
93 Viewer::write_pixel(int x, int y,float z, Uint32 rgb)
|
|
94 {
|
28
|
95 SDL_PixelFormat *pf;
|
|
96 pf = screen->format;
|
|
97 x += width/2;
|
|
98 y += height/2;
|
26
|
99
|
28
|
100 static int diffz,diffz1;
|
51
|
101
|
28
|
102 diffz1 = diffz;
|
|
103 diffz = (zRow[x][y]>z);
|
51
|
104 if (z < zRow[x][y]) {
|
99
|
105 if (x < width && x > 0 && y > 0 && y < height) {
|
|
106 zRow[x][y] = z;
|
|
107 y = height - y;
|
|
108 pixels[width*y + x] = rgb;
|
|
109 }
|
26
|
110 }
|
|
111 }
|
|
112
|
51
|
113 void
|
|
114 Viewer::write_line(float x1, float y1, float x2, float y2, Uint32 rgb)
|
26
|
115 {
|
51
|
116 if (x1 > x2) {
|
28
|
117 float x=0;
|
|
118 float y=0;
|
|
119 x=x1;
|
|
120 y=y1;
|
|
121 x1 = x2;
|
|
122 y1 = y2;
|
|
123 x2 = x;
|
|
124 y2 = y;
|
26
|
125 }
|
51
|
126
|
28
|
127 float s = y1;
|
26
|
128
|
51
|
129 if ((int)x1 == (int)x2) {
|
|
130 if (y1 > y2) {
|
28
|
131 float y=0;
|
|
132 y = y1;
|
|
133 y1 = y2;
|
|
134 y2 = y;
|
26
|
135 }
|
51
|
136
|
|
137 for (float i=y1; i<y2; i++) {
|
28
|
138 //write_pixel((int)x1,(int)i);
|
|
139 write_pixel((int)x1,(int)i,0,rgb);
|
26
|
140 }
|
51
|
141 } else {
|
28
|
142 float t = (y2 - y1)/(x2 - x1);
|
51
|
143 if (t < -1) {
|
28
|
144 float f = 0;
|
51
|
145 for (float i=x1; i<x2; i++) {
|
|
146 for (float a=(int)t; a<0; a++) {
|
28
|
147 write_pixel((int)i,(int)s,0,rgb);
|
|
148 s--;
|
26
|
149 }
|
51
|
150
|
28
|
151 f += t-(int)t;
|
51
|
152
|
|
153 if (f <= -1) {
|
28
|
154 write_pixel((int)i,(int)s,0,rgb);
|
|
155 f = 0;
|
|
156 s--;
|
26
|
157 }
|
|
158 }
|
51
|
159 } else if (t <= 1) {
|
|
160 for(float i=x1; i<x2; i++) {
|
28
|
161 //write_pixel((int)i,(int)s);
|
|
162 write_pixel((int)i,(int)s,0,rgb);
|
|
163 s += t;
|
26
|
164 }
|
51
|
165 } else {
|
28
|
166 float f = 0;
|
51
|
167 for (float i=x1; i<x2; i++) {
|
|
168 for (float a=0; a<(int)t; a++) {
|
28
|
169 write_pixel((int)i,(int)s,0,rgb);
|
|
170 s++;
|
26
|
171 }
|
51
|
172
|
28
|
173 f += t-(int)t;
|
51
|
174
|
|
175 if (f >= 1) {
|
28
|
176 write_pixel((int)i,(int)s,0,rgb);
|
|
177 f = 0;
|
|
178 s++;
|
26
|
179 }
|
|
180 }
|
|
181 }
|
|
182 }
|
|
183 }
|
|
184
|
|
185 void Viewer::write_triangle(float x1, float y1, float x2, float y2, float x3, float y3, Uint32 rgb)
|
|
186 {
|
28
|
187 write_line(x1,y1,x2,y2,rgb);
|
|
188 write_line(x2,y2,x3,y3,rgb);
|
|
189 write_line(x3,y3,x1,y1,rgb);
|
26
|
190 }
|
|
191
|
|
192 void Viewer::clean_pixels()
|
|
193 {
|
28
|
194 for(int i=0; i<width*height; i++)
|
26
|
195 {
|
28
|
196 pixels[i] = 0x00;
|
26
|
197 }
|
|
198 }
|
|
199
|
|
200 void Viewer::graph_line()
|
|
201 {
|
28
|
202 int xl = width*height/2;
|
|
203 int yl = width/2;
|
|
204 for(int i=0; i<width; i++)
|
26
|
205 {
|
28
|
206 for(int t=0; t<height; t+=20)
|
26
|
207 {
|
28
|
208 pixels[width*t+i] = 0x5a;
|
26
|
209 }
|
28
|
210 pixels[xl +i] = 0xff;
|
26
|
211 }
|
28
|
212 for(int i=0; i<height; i++)
|
26
|
213 {
|
28
|
214 for(int t=0; t<width; t+=20)
|
26
|
215 {
|
28
|
216 pixels[i*width+t] = 0x5a;
|
26
|
217 }
|
28
|
218 pixels[i*width+yl] = 0xff;
|
26
|
219 }
|
|
220 }
|
|
221
|
54
|
222 int start_time;
|
|
223 int this_time;
|
|
224 int frames;
|
|
225 SDL_Surface *bitmap;
|
|
226 SDL_PixelFormat *pixelFormat;
|
|
227 Uint32 background;
|
|
228 Polygon *polygon;
|
88
|
229 SceneGraphPack *sgp;
|
|
230 PolygonPack *pp;
|
89
|
231 SPUSPANLIST *ssl;
|
81
|
232 DmaBuffer *sgp_buff;
|
|
233 DmaBuffer *pp_buff;
|
|
234 DmaBuffer *ssl_buff;
|
|
235
|
99
|
236 SPANPACK send_pack[6][10] __attribute__((aligned(16)));
|
94
|
237
|
99
|
238 void *__texture;
|
|
239
|
42
|
240 void
|
|
241 Viewer::run_init()
|
|
242 {
|
|
243 HTaskPtr task;
|
54
|
244
|
|
245 start_time = get_ticks();
|
|
246 this_time = 0;
|
|
247 frames = 0;
|
|
248
|
|
249 pixelFormat = screen->format;
|
|
250 background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
|
|
251 polygon = new Polygon;
|
|
252 polygon->set_data("cube.xml");
|
|
253 polygon->viewer = this;
|
|
254
|
88
|
255 #if 0 // USE DOUBLE BUFFER
|
54
|
256 sgp_buff = manager->allocate(sizeof(SceneGraphPack));
|
85
|
257 pp_buff = manager->allocate(sizeof(PolygonPack));
|
81
|
258 ssl_buff = manager->allocate(sizeof(SPUSPANLIST));
|
|
259
|
86
|
260 // DmaBuffer の初期化、かっこいい方法ないか
|
|
261 for (int i = 0; i < 2; i ++) {
|
|
262 sgp_buff->get_buffer(&sgp);
|
|
263 ssl_buff->get_buffer(&ssl);
|
79
|
264
|
89
|
265 create_sgp(polygon, sgp);
|
|
266 sgp->ssl = ssl;
|
|
267
|
|
268 sgp_buff->swap_buffer();
|
86
|
269 ssl_buff->swap_buffer();
|
|
270 }
|
88
|
271 #else
|
94
|
272 posix_memalign((void**)&sgp, 16, sizeof(SceneGraphPack));
|
|
273 posix_memalign((void**)&pp, 16, sizeof(SceneGraphPack));
|
|
274 posix_memalign((void**)&ssl, 16, sizeof(SceneGraphPack));
|
|
275 //sgp = new SceneGraphPack;
|
|
276 //pp = new PolygonPack;
|
|
277 //ssl = new SPUSPANLIST;
|
88
|
278 create_sgp(polygon, sgp);
|
89
|
279 sgp->ssl = ssl;
|
88
|
280 #endif
|
42
|
281
|
|
282 pixels = new Uint32[width*height];
|
|
283
|
99
|
284 //graph_line();
|
42
|
285
|
54
|
286 bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32,
|
42
|
287 width*4, redMask, greenMask,
|
|
288 blueMask, alphaMask);
|
|
289
|
105
|
290 task = manager->create_task(VIEWER_RUN_LOOP, 0, 0, 0, NULL);
|
63
|
291 task->spawn();
|
99
|
292
|
102
|
293 posix_memalign((void**)&__texture, 16, 128*128*3);
|
|
294 memcpy(__texture, polygon->texture_image->pixels, 128*128*3);
|
105
|
295 HTaskPtr task_init_tex
|
|
296 = manager->create_task(TASK_INIT_TEXTURE, 0,
|
|
297 (uint32)__texture, 0, NULL);
|
102
|
298 task_init_tex->set_cpu(CPU_SPE);
|
|
299 task_init_tex->spawn();
|
42
|
300 }
|
|
301
|
|
302 void
|
48
|
303 Viewer::run_loop(void)
|
42
|
304 {
|
|
305 HTaskPtr task_update_sgp = NULL;
|
81
|
306 HTaskPtr task_create_pp = NULL;
|
|
307 HTaskPtr task_create_sp = NULL;
|
|
308 HTaskPtr task_finish = NULL;
|
42
|
309
|
48
|
310 HTaskPtr task;
|
76
|
311 bool quit_flg;
|
|
312
|
|
313 quit_flg = quit_check();
|
|
314
|
|
315 if (quit_flg == true) {
|
54
|
316 this_time = get_ticks();
|
105
|
317 task_finish = manager->create_task(VIEWER_RUN_FINISH, 0, 0, 0, NULL);
|
63
|
318 task_finish->spawn();
|
42
|
319 return;
|
|
320 }
|
|
321
|
89
|
322 // clean_pixels や zRow_init は、
|
|
323 // spe で fb に draw する時は必要ない。
|
|
324 // ppe 側で draw する時にだけ呼ぶべき。
|
102
|
325 //clean_pixels();
|
|
326 //zRow_init();
|
42
|
327
|
89
|
328 // これ自身、一つのタスクとして回す方がよいか
|
102
|
329 //graph_line();
|
42
|
330
|
81
|
331 task_update_sgp
|
105
|
332 = manager->create_task(TASK_UPDATE_SGP, sizeof(SceneGraphPack),
|
88
|
333 (uint32)sgp, (uint32)sgp, NULL);
|
|
334 task_create_pp
|
105
|
335 = manager->create_task(TASK_CREATE_PP, sizeof(SceneGraphPack),
|
88
|
336 (uint32)sgp, (uint32)pp, NULL);
|
89
|
337 task_create_sp
|
105
|
338 = manager->create_task(TASK_CREATE_SPAN, sizeof(PolygonPack),
|
92
|
339 (uint32)pp, 0, NULL);
|
88
|
340
|
|
341 #if 0
|
|
342
|
85
|
343 task_draw_finish = manager->create();
|
|
344 for (int i = 0; i < draw_spe_num; i++) {
|
|
345 task_spu_draw[i]
|
|
346 = manager->create_task(fd_create_pp, sizeof(SceneGraphPack),
|
|
347 spuspan_buff, pp_buff, NULL);
|
|
348 task_spu_draw[i]->set_depend(task_spu_span);
|
|
349 task_draw_finish->set_depend(tas_spu_draw[i]);
|
|
350 }
|
81
|
351 #endif
|
105
|
352 task = manager->create_task(VIEWER_RUN_DRAW, 0, 0, 0, NULL);
|
48
|
353
|
63
|
354 task->set_depend(task_update_sgp);
|
|
355 task->set_depend(task_create_pp);
|
89
|
356 task->set_depend(task_create_sp);
|
|
357 task_create_sp->set_depend(task_create_pp);
|
85
|
358
|
74
|
359 //task_update_sgp->set_cpu(CPU_SPE);
|
89
|
360 //task_create_pp->set_cpu(CPU_SPE);
|
70
|
361
|
63
|
362 task_update_sgp->spawn();
|
|
363 task_create_pp->spawn();
|
89
|
364 task_create_sp->spawn();
|
63
|
365 task->spawn();
|
54
|
366
|
73
|
367 //sgp_buff->swap_buffer();
|
48
|
368 }
|
42
|
369
|
89
|
370 /**
|
|
371 * 本当はタスクとして TestDraw を選ぶ
|
|
372 */
|
|
373
|
101
|
374 //#define DRAW_POLYGON
|
|
375 #define DRAW_SPANPACK
|
94
|
376 //#define DRAW_SPUSPAN
|
48
|
377 void
|
|
378 Viewer::run_draw(void)
|
|
379 {
|
|
380 HTaskPtr task;
|
99
|
381 HTaskPtr task_draw[6][10];
|
48
|
382
|
105
|
383 //fd = manager->open("ViewerRunLoop");
|
|
384 //task = manager->create_task(fd, 0, 0, 0, NULL);
|
|
385 task = manager->create_task(VIEWER_RUN_LOOP, 0, 0, 0, NULL);
|
94
|
386
|
102
|
387 #if 1
|
99
|
388 for (int j = 0; j < 6; j++) {
|
|
389 for (int i = 0; i < 10; i++) {
|
|
390 if (ssl->ss[j].spp[i].info.size < 1) continue;
|
105
|
391 // memcpy はもちろんだめ。だけど。。。
|
|
392 // SPUSPAN->ss は配列で、各要素がアライメントとれてないと駄目。
|
99
|
393 memcpy(&send_pack[j][i], &ssl->ss[j].spp[i], sizeof(SPANPACK));
|
|
394 task_draw[j][i]
|
105
|
395 = manager->create_task(TASK_DRAW, sizeof(SPANPACK),
|
99
|
396 (uint32)&send_pack[j][i], fbdev_addr, NULL);
|
|
397 task_draw[j][i]->set_cpu(CPU_SPE);
|
|
398 task->set_depend(task_draw[j][i]);
|
|
399 task_draw[j][i]->spawn();
|
|
400 }
|
94
|
401 }
|
99
|
402 #endif
|
94
|
403 task->spawn();
|
102
|
404
|
|
405 frames++;
|
|
406
|
|
407 return;
|
99
|
408
|
88
|
409 #if 0 // USE DOUBLE BUFFER
|
81
|
410 PolygonPack *pp;
|
89
|
411 SPUSPANLIST *ssl;
|
54
|
412
|
81
|
413 pp_buff->get_buffer(&pp);
|
89
|
414 ssl_buff->get_buffer(&ssl);
|
88
|
415 #endif
|
54
|
416
|
89
|
417 #ifdef DRAW_POLYGON
|
101
|
418 //polygon->draw(pp); // test draw of PolygonPack
|
|
419 polygon->draw(sgp); // test draw of PolygonPack
|
89
|
420 #else
|
|
421 # ifdef DRAW_SPANPACK // test draw of SpanPack
|
101
|
422 for (int j = 0; j < 6; j++) {
|
|
423 for (int i = 0; i < 10; i++) {
|
|
424 polygon->draw(&ssl->ss[j].spp[i]);
|
|
425 }
|
89
|
426 }
|
|
427 # else
|
|
428 polygon->draw(&ssl->ss[0]);
|
|
429 polygon->draw(&ssl->ss[1]);
|
|
430 polygon->draw(&ssl->ss[2]);
|
|
431 polygon->draw(&ssl->ss[3]);
|
|
432 polygon->draw(&ssl->ss[4]);
|
|
433 polygon->draw(&ssl->ss[5]);
|
|
434 # endif
|
|
435 #endif
|
99
|
436
|
101
|
437 SDL_BlitSurface(bitmap, NULL, screen, NULL);
|
|
438 SDL_UpdateRect(screen, 0, 0, 0, 0);
|
42
|
439 }
|
|
440
|
85
|
441 #if 0
|
|
442 void
|
|
443 Viewer::draw_finish(void)
|
|
444 {
|
|
445 //
|
|
446
|
|
447 HTaskPtr task = manager->create(task_r);
|
|
448
|
|
449 }
|
|
450 #endif
|
|
451
|
42
|
452 void
|
48
|
453 Viewer::run_finish(void)
|
42
|
454 {
|
54
|
455 if (this_time != start_time) {
|
|
456 cout<< (((float)frames)/(this_time-start_time))*1000.0 << " FPS\n";
|
42
|
457 }
|
|
458
|
54
|
459 SDL_FreeSurface(bitmap);
|
42
|
460 delete [] pixels;
|
54
|
461 polygon->delete_data();
|
|
462 delete polygon;
|
76
|
463 //delete sgp_buff;
|
|
464 //delete pp_buff;
|
99
|
465
|
|
466 free(__texture);
|
42
|
467 quit();
|
|
468 }
|
|
469
|
28
|
470 void
|
|
471 Viewer::zRow_init()
|
|
472 {
|
|
473 for (int i = 0; i < width; i++) {
|
|
474 for (int j = 0; j < height; j++) {
|
|
475 zRow[i][j] = 65535;
|
26
|
476 }
|
28
|
477 }
|
26
|
478 }
|