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"
|
26
|
7 using namespace std;
|
|
8
|
28
|
9 #define redMask 0x00ff0000
|
|
10 #define greenMask 0x0000ff00
|
|
11 #define blueMask 0x000000ff
|
|
12 #define alphaMask 0
|
|
13
|
48
|
14 extern int create_sgp(Polygon *sg, SceneGraphPack *sgp);
|
26
|
15 extern int update_sgp(SceneGraphPack *sgp, SceneGraphPack *_sgp);
|
48
|
16 extern int create_pp(SceneGraphPack *sgp, PolygonPack *pp);
|
26
|
17
|
|
18 Viewer::Viewer(int b, int w, int h)
|
|
19 {
|
28
|
20 bpp = b;
|
|
21 width = w;
|
|
22 height = h;
|
26
|
23 }
|
|
24
|
|
25
|
|
26 void Viewer::sdl_init()
|
|
27 {
|
67
|
28 if (SDL_Init( SDL_INIT_VIDEO ) < 0) {
|
28
|
29 fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
|
|
30 exit( 1 );
|
26
|
31 }
|
89
|
32
|
101
|
33 #ifndef _DEBUG
|
67
|
34 screen = SDL_SetVideoMode( width, height, bpp, SDL_HWSURFACE);
|
|
35 if (screen == NULL) {
|
|
36 fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
|
|
37 SDL_Quit();
|
|
38 exit(1);
|
|
39 }
|
70
|
40 #else
|
71
|
41 void *_pixels = new Uint32[width*height*32/8];
|
70
|
42 //posix_memalign(&pixels, width*heigh/8, DEFAULT_ALIGNMENT);
|
|
43 screen = SDL_CreateRGBSurfaceFrom(_pixels, width, height, 32,
|
|
44 width*4, redMask, greenMask,
|
|
45 blueMask, alphaMask);
|
|
46 #endif
|
26
|
47 }
|
|
48
|
|
49
|
|
50 int Viewer::get_ticks()
|
|
51 {
|
28
|
52 int time;
|
|
53 time = SDL_GetTicks();
|
|
54 return time;
|
26
|
55 }
|
|
56
|
|
57 bool Viewer::quit_check()
|
|
58 {
|
28
|
59 bool quit = false;
|
|
60 SDL_Event event;
|
|
61 while(SDL_PollEvent(&event))
|
26
|
62 {
|
28
|
63 if(event.type==SDL_QUIT)
|
26
|
64 {
|
28
|
65 quit = true;
|
|
66 return quit;
|
26
|
67 }
|
|
68 }
|
28
|
69 return quit;
|
26
|
70 }
|
|
71
|
|
72 void Viewer::quit()
|
|
73 {
|
28
|
74 SDL_Quit();
|
26
|
75 }
|
|
76
|
|
77
|
|
78 void Viewer::swap_buffers()
|
|
79 {
|
28
|
80 SDL_GL_SwapBuffers();
|
26
|
81 }
|
|
82
|
|
83
|
51
|
84 void
|
|
85 Viewer::write_pixel(int x, int y,float z, Uint32 rgb)
|
|
86 {
|
28
|
87 SDL_PixelFormat *pf;
|
|
88 pf = screen->format;
|
|
89 x += width/2;
|
|
90 y += height/2;
|
26
|
91
|
28
|
92 static int diffz,diffz1;
|
51
|
93
|
28
|
94 diffz1 = diffz;
|
|
95 diffz = (zRow[x][y]>z);
|
51
|
96 if (z < zRow[x][y]) {
|
99
|
97 if (x < width && x > 0 && y > 0 && y < height) {
|
|
98 zRow[x][y] = z;
|
|
99 y = height - y;
|
|
100 pixels[width*y + x] = rgb;
|
|
101 }
|
26
|
102 }
|
|
103 }
|
|
104
|
51
|
105 void
|
|
106 Viewer::write_line(float x1, float y1, float x2, float y2, Uint32 rgb)
|
26
|
107 {
|
51
|
108 if (x1 > x2) {
|
28
|
109 float x=0;
|
|
110 float y=0;
|
|
111 x=x1;
|
|
112 y=y1;
|
|
113 x1 = x2;
|
|
114 y1 = y2;
|
|
115 x2 = x;
|
|
116 y2 = y;
|
26
|
117 }
|
51
|
118
|
28
|
119 float s = y1;
|
26
|
120
|
51
|
121 if ((int)x1 == (int)x2) {
|
|
122 if (y1 > y2) {
|
28
|
123 float y=0;
|
|
124 y = y1;
|
|
125 y1 = y2;
|
|
126 y2 = y;
|
26
|
127 }
|
51
|
128
|
|
129 for (float i=y1; i<y2; i++) {
|
28
|
130 //write_pixel((int)x1,(int)i);
|
|
131 write_pixel((int)x1,(int)i,0,rgb);
|
26
|
132 }
|
51
|
133 } else {
|
28
|
134 float t = (y2 - y1)/(x2 - x1);
|
51
|
135 if (t < -1) {
|
28
|
136 float f = 0;
|
51
|
137 for (float i=x1; i<x2; i++) {
|
|
138 for (float a=(int)t; a<0; a++) {
|
28
|
139 write_pixel((int)i,(int)s,0,rgb);
|
|
140 s--;
|
26
|
141 }
|
51
|
142
|
28
|
143 f += t-(int)t;
|
51
|
144
|
|
145 if (f <= -1) {
|
28
|
146 write_pixel((int)i,(int)s,0,rgb);
|
|
147 f = 0;
|
|
148 s--;
|
26
|
149 }
|
|
150 }
|
51
|
151 } else if (t <= 1) {
|
|
152 for(float i=x1; i<x2; i++) {
|
28
|
153 //write_pixel((int)i,(int)s);
|
|
154 write_pixel((int)i,(int)s,0,rgb);
|
|
155 s += t;
|
26
|
156 }
|
51
|
157 } else {
|
28
|
158 float f = 0;
|
51
|
159 for (float i=x1; i<x2; i++) {
|
|
160 for (float a=0; a<(int)t; a++) {
|
28
|
161 write_pixel((int)i,(int)s,0,rgb);
|
|
162 s++;
|
26
|
163 }
|
51
|
164
|
28
|
165 f += t-(int)t;
|
51
|
166
|
|
167 if (f >= 1) {
|
28
|
168 write_pixel((int)i,(int)s,0,rgb);
|
|
169 f = 0;
|
|
170 s++;
|
26
|
171 }
|
|
172 }
|
|
173 }
|
|
174 }
|
|
175 }
|
|
176
|
|
177 void Viewer::write_triangle(float x1, float y1, float x2, float y2, float x3, float y3, Uint32 rgb)
|
|
178 {
|
28
|
179 write_line(x1,y1,x2,y2,rgb);
|
|
180 write_line(x2,y2,x3,y3,rgb);
|
|
181 write_line(x3,y3,x1,y1,rgb);
|
26
|
182 }
|
|
183
|
|
184 void Viewer::clean_pixels()
|
|
185 {
|
28
|
186 for(int i=0; i<width*height; i++)
|
26
|
187 {
|
28
|
188 pixels[i] = 0x00;
|
26
|
189 }
|
|
190 }
|
|
191
|
|
192 void Viewer::graph_line()
|
|
193 {
|
28
|
194 int xl = width*height/2;
|
|
195 int yl = width/2;
|
|
196 for(int i=0; i<width; i++)
|
26
|
197 {
|
28
|
198 for(int t=0; t<height; t+=20)
|
26
|
199 {
|
28
|
200 pixels[width*t+i] = 0x5a;
|
26
|
201 }
|
28
|
202 pixels[xl +i] = 0xff;
|
26
|
203 }
|
28
|
204 for(int i=0; i<height; i++)
|
26
|
205 {
|
28
|
206 for(int t=0; t<width; t+=20)
|
26
|
207 {
|
28
|
208 pixels[i*width+t] = 0x5a;
|
26
|
209 }
|
28
|
210 pixels[i*width+yl] = 0xff;
|
26
|
211 }
|
|
212 }
|
|
213
|
54
|
214 int start_time;
|
|
215 int this_time;
|
|
216 int frames;
|
|
217 SDL_Surface *bitmap;
|
|
218 SDL_PixelFormat *pixelFormat;
|
|
219 Uint32 background;
|
|
220 Polygon *polygon;
|
88
|
221 SceneGraphPack *sgp;
|
|
222 PolygonPack *pp;
|
89
|
223 SPUSPANLIST *ssl;
|
81
|
224 DmaBuffer *sgp_buff;
|
|
225 DmaBuffer *pp_buff;
|
|
226 DmaBuffer *ssl_buff;
|
|
227
|
99
|
228 SPANPACK send_pack[6][10] __attribute__((aligned(16)));
|
94
|
229 unsigned int fbdev_addr;
|
|
230
|
99
|
231 void *__texture;
|
|
232
|
94
|
233 extern int get_fbdev_addr(void);
|
54
|
234
|
42
|
235 void
|
|
236 Viewer::run_init()
|
|
237 {
|
|
238 HTaskPtr task;
|
|
239 int fd;
|
|
240
|
88
|
241 //SceneGraphPack *sgp;
|
|
242 //PolygonPack *pp;
|
|
243 //SPUSPANLIST *ssl;
|
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
|
|
290 fd = manager->open("ViewerRunLoop");
|
48
|
291 task = manager->create_task(fd, 0, 0, 0, NULL);
|
63
|
292 task->spawn();
|
99
|
293
|
101
|
294 //posix_memalign((void**)&__texture, 16, 128*128*3);
|
|
295 //memcpy(__texture, polygon->texture_image->pixels, 128*128*3);
|
|
296 //HTaskPtr task_init_tex = manager->create_task(0, 0, (uint32)__texture, 0, NULL);
|
|
297 //task_init_tex->set_cpu(CPU_SPE);
|
|
298 //task_init_tex->spawn();
|
94
|
299
|
101
|
300 //fbdev_addr = get_fbdev_addr();
|
42
|
301 }
|
|
302
|
|
303 void
|
48
|
304 Viewer::run_loop(void)
|
42
|
305 {
|
|
306 HTaskPtr task_update_sgp = NULL;
|
81
|
307 HTaskPtr task_create_pp = NULL;
|
|
308 HTaskPtr task_create_sp = NULL;
|
|
309 HTaskPtr task_finish = NULL;
|
42
|
310 int fd_update_sgp;
|
|
311 int fd_create_pp;
|
81
|
312 int fd_create_sp;
|
42
|
313 int fd_finish;
|
|
314
|
48
|
315 HTaskPtr task;
|
|
316 int fd;
|
76
|
317 bool quit_flg;
|
|
318
|
|
319 quit_flg = quit_check();
|
|
320
|
|
321 if (quit_flg == true) {
|
54
|
322 this_time = get_ticks();
|
42
|
323 fd_finish = manager->open("ViewerRunFinish");
|
48
|
324 task_finish = manager->create_task(fd_finish, 0, 0, 0, NULL);
|
63
|
325 task_finish->spawn();
|
42
|
326 return;
|
|
327 }
|
|
328
|
89
|
329 // clean_pixels ¤ä zRow_init ¤Ï¡¢
|
|
330 // spe ¤Ç fb ¤Ë draw ¤¹¤ë»þ¤ÏɬÍפʤ¤¡£
|
|
331 // ppe ¦¤Ç draw ¤¹¤ë»þ¤Ë¤À¤±¸Æ¤Ö¤Ù¤¡£
|
101
|
332 clean_pixels();
|
|
333 zRow_init();
|
42
|
334
|
89
|
335 // ¤³¤ì¼«¿È¡¢°ì¤Ä¤Î¥¿¥¹¥¯¤È¤·¤Æ²ó¤¹Êý¤¬¤è¤¤¤«
|
101
|
336 graph_line();
|
42
|
337
|
|
338 fd_update_sgp = manager->open("UpdateSGP");
|
|
339 fd_create_pp = manager->open("CreatePP");
|
86
|
340 fd_create_sp = manager->open("CreateSP");
|
48
|
341 fd = manager->open("ViewerRunDraw");
|
81
|
342
|
88
|
343 #if 0 // USE DOUBLE BUFFER
|
81
|
344 task_update_sgp
|
|
345 = manager->create_task(fd_update_sgp, sizeof(SceneGraphPack),
|
|
346 sgp_buff, sgp_buff, NULL);
|
|
347 task_create_pp
|
|
348 = manager->create_task(fd_create_pp, sizeof(SceneGraphPack),
|
|
349 sgp_buff, pp_buff, NULL);
|
89
|
350 task_create_sp
|
|
351 = manager->create_task(fd_create_sp, sizeof(SceneGraphPack),
|
|
352 pp_buff, ssl_buff, NULL);
|
88
|
353 #else
|
|
354 task_update_sgp
|
|
355 = manager->create_task(fd_update_sgp, sizeof(SceneGraphPack),
|
|
356 (uint32)sgp, (uint32)sgp, NULL);
|
|
357 task_create_pp
|
|
358 = manager->create_task(fd_create_pp, sizeof(SceneGraphPack),
|
|
359 (uint32)sgp, (uint32)pp, NULL);
|
89
|
360 task_create_sp
|
92
|
361 = manager->create_task(fd_create_sp, sizeof(PolygonPack),
|
|
362 (uint32)pp, 0, NULL);
|
88
|
363 #endif
|
|
364
|
|
365 #if 0
|
|
366
|
85
|
367 task_draw_finish = manager->create();
|
|
368 for (int i = 0; i < draw_spe_num; i++) {
|
|
369 task_spu_draw[i]
|
|
370 = manager->create_task(fd_create_pp, sizeof(SceneGraphPack),
|
|
371 spuspan_buff, pp_buff, NULL);
|
|
372 task_spu_draw[i]->set_depend(task_spu_span);
|
|
373 task_draw_finish->set_depend(tas_spu_draw[i]);
|
|
374 }
|
81
|
375 #endif
|
48
|
376 task = manager->create_task(fd, 0, 0, 0, NULL);
|
|
377
|
63
|
378 task->set_depend(task_update_sgp);
|
|
379 task->set_depend(task_create_pp);
|
89
|
380 task->set_depend(task_create_sp);
|
|
381 task_create_sp->set_depend(task_create_pp);
|
85
|
382
|
74
|
383 //task_update_sgp->set_cpu(CPU_SPE);
|
89
|
384 //task_create_pp->set_cpu(CPU_SPE);
|
70
|
385
|
63
|
386 task_update_sgp->spawn();
|
|
387 task_create_pp->spawn();
|
89
|
388 task_create_sp->spawn();
|
63
|
389 task->spawn();
|
54
|
390
|
73
|
391 //sgp_buff->swap_buffer();
|
48
|
392 }
|
42
|
393
|
89
|
394 /**
|
|
395 * ËÜÅö¤Ï¥¿¥¹¥¯¤È¤·¤Æ TestDraw ¤òÁª¤Ö
|
|
396 */
|
|
397
|
101
|
398 //#define DRAW_POLYGON
|
|
399 #define DRAW_SPANPACK
|
94
|
400 //#define DRAW_SPUSPAN
|
48
|
401 void
|
|
402 Viewer::run_draw(void)
|
|
403 {
|
|
404 HTaskPtr task;
|
99
|
405 HTaskPtr task_draw[6][10];
|
48
|
406 int fd;
|
|
407
|
94
|
408 fd = manager->open("ViewerRunLoop");
|
|
409 task = manager->create_task(fd, 0, 0, 0, NULL);
|
|
410
|
101
|
411 #if 0
|
99
|
412 for (int j = 0; j < 6; j++) {
|
|
413 for (int i = 0; i < 10; i++) {
|
|
414 if (ssl->ss[j].spp[i].info.size < 1) continue;
|
|
415 memcpy(&send_pack[j][i], &ssl->ss[j].spp[i], sizeof(SPANPACK));
|
|
416 task_draw[j][i]
|
|
417 = manager->create_task(1, sizeof(SPANPACK),
|
|
418 (uint32)&send_pack[j][i], fbdev_addr, NULL);
|
|
419 task_draw[j][i]->set_cpu(CPU_SPE);
|
|
420 task->set_depend(task_draw[j][i]);
|
|
421 task_draw[j][i]->spawn();
|
|
422 }
|
94
|
423 }
|
99
|
424 #endif
|
94
|
425 task->spawn();
|
99
|
426
|
101
|
427 //return;
|
99
|
428
|
88
|
429 #if 0 // USE DOUBLE BUFFER
|
81
|
430 PolygonPack *pp;
|
89
|
431 SPUSPANLIST *ssl;
|
54
|
432
|
81
|
433 pp_buff->get_buffer(&pp);
|
89
|
434 ssl_buff->get_buffer(&ssl);
|
88
|
435 #endif
|
54
|
436
|
89
|
437 #ifdef DRAW_POLYGON
|
101
|
438 //polygon->draw(pp); // test draw of PolygonPack
|
|
439 polygon->draw(sgp); // test draw of PolygonPack
|
89
|
440 #else
|
|
441 # ifdef DRAW_SPANPACK // test draw of SpanPack
|
101
|
442 for (int j = 0; j < 6; j++) {
|
|
443 for (int i = 0; i < 10; i++) {
|
|
444 polygon->draw(&ssl->ss[j].spp[i]);
|
|
445 }
|
89
|
446 }
|
|
447 # else
|
|
448 polygon->draw(&ssl->ss[0]);
|
|
449 polygon->draw(&ssl->ss[1]);
|
|
450 polygon->draw(&ssl->ss[2]);
|
|
451 polygon->draw(&ssl->ss[3]);
|
|
452 polygon->draw(&ssl->ss[4]);
|
|
453 polygon->draw(&ssl->ss[5]);
|
|
454 # endif
|
|
455 #endif
|
99
|
456
|
101
|
457 SDL_BlitSurface(bitmap, NULL, screen, NULL);
|
|
458 SDL_UpdateRect(screen, 0, 0, 0, 0);
|
|
459
|
|
460 frames++;
|
|
461
|
99
|
462 return;
|
42
|
463 }
|
|
464
|
85
|
465 #if 0
|
|
466 void
|
|
467 Viewer::draw_finish(void)
|
|
468 {
|
|
469 //
|
|
470
|
|
471 HTaskPtr task = manager->create(task_r);
|
|
472
|
|
473 }
|
|
474 #endif
|
|
475
|
42
|
476 void
|
48
|
477 Viewer::run_finish(void)
|
42
|
478 {
|
54
|
479 if (this_time != start_time) {
|
|
480 cout<< (((float)frames)/(this_time-start_time))*1000.0 << " FPS\n";
|
42
|
481 }
|
|
482
|
54
|
483 SDL_FreeSurface(bitmap);
|
42
|
484 delete [] pixels;
|
54
|
485 polygon->delete_data();
|
|
486 delete polygon;
|
76
|
487 //delete sgp_buff;
|
|
488 //delete pp_buff;
|
99
|
489
|
|
490 free(__texture);
|
42
|
491 quit();
|
|
492 }
|
|
493
|
28
|
494 void
|
|
495 Viewer::zRow_init()
|
|
496 {
|
|
497 for (int i = 0; i < width; i++) {
|
|
498 for (int j = 0; j < height; j++) {
|
|
499 zRow[i][j] = 65535;
|
26
|
500 }
|
28
|
501 }
|
26
|
502 }
|