gpmark

view src/main.cpp @ 0:5019d031b485

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 05 Jun 2013 22:33:37 +0300
parents
children
line source
1 #include <SDL/SDL.h>
2 #include <math.h>
4 #include "main.h"
5 #include "bitfonts.h"
7 #include "blitting.h"
8 #include "plasma.h"
9 #include "rotozoomer.h"
10 #include "radialblur.h"
11 #include "bunny3d.h"
12 #include "render3d.h"
14 #if defined ProjectCaanoo || defined ProjectWiz
15 #include <unistd.h>
16 #endif
18 #ifdef ProjectWin
19 #undef main
20 #endif
22 SDL_Surface *screen = NULL;
23 SDL_Joystick *joy = NULL;
25 int fps = 0, pframe = 0, nframe = 0, atime = 0;
26 int frametime = 0;
27 int quit = 0;
28 int desc_next = 0;
30 int where_is = MENU;
32 // -------------------------------------
34 char sbuffer[256];
35 unsigned char fonts[59*64];
37 void InitFonts()
38 {
39 int i = 0;
40 for (int n=0; n<59; n++)
41 {
42 for (int y=0; y<8; y++)
43 {
44 int c = bitfonts[i++];
45 for (int x=0; x<8; x++)
46 {
47 fonts[(n << 6) + x + (y<<3)] = ((c >> (7 - x)) & 1) * 255;
48 }
49 }
50 }
51 }
53 void DrawFontX2(int xp, int yp, int ch, unsigned short *vram)
54 {
55 if (xp <0 || xp > 312) return;
56 vram += xp + yp * ScreenWidth;
57 for (int y=0; y<8; y++)
58 {
59 int yc = yp + y;
60 if ((yc>=1) && (yc<ScreenHeight - 1))
61 {
62 int yi = y << 3;
63 for (int x=0; x<8; x++)
64 {
65 unsigned char c = fonts[(ch << 6) + yi + x] * 0x01010101;
66 *vram |= c;
67 *(vram+1) |= c;
68 *(vram+ScreenWidth) |= c;
69 *(vram+ScreenWidth+1) |= c;
70 vram+=2;
71 }
72 vram-=16;
73 }
74 vram+=2*ScreenWidth;
75 }
76 }
78 void DrawFont(int xp, int yp, int ch, unsigned short *vram)
79 {
80 if (xp <0 || xp > 312) return;
81 vram += xp + yp * ScreenWidth;
82 for (int y=0; y<8; y++)
83 {
84 int yc = yp + y;
85 if ((yc>=1) && (yc<ScreenHeight - 1))
86 {
87 int yi = y << 3;
88 for (int x=0; x<8; x++)
89 {
90 *vram++ |= (fonts[(ch << 6) + yi + x] * 0x01010101);
91 }
92 vram-=8;
93 }
94 vram+=ScreenWidth;
95 }
96 }
98 void DrawText(int xtp, int ytp, int cn, bool zoom, char *text, unsigned short *vram)
99 {
100 for (int n = 0; n<cn; n++)
101 {
102 char c = *text++;
103 if (c>96 && c<123) c-=32;
105 if (!zoom)
106 {
107 if (c>31 && c<92) DrawFont(xtp, ytp, c - 32, vram);
108 else if (c==0) n = cn;
109 xtp+=8; if (xtp>ScreenWidth -8 -1) n = cn;
110 }
111 else
112 {
113 if (c>31 && c<92) DrawFontX2(xtp, ytp, c - 32, vram);
114 else if (c==0) n = cn;
115 xtp+=16; if (xtp>ScreenWidth -16 -1) n = cn;
116 }
117 }
118 }
120 void ShowFPS(unsigned short *vram)
121 {
122 if (SDL_GetTicks() - atime >= 1000)
123 {
124 atime = SDL_GetTicks();
125 fps = nframe - pframe;
126 pframe = nframe;
127 }
128 sprintf(sbuffer, "FPS = %d", fps);
129 DrawText(8, 8, 16, false, sbuffer, vram);
130 }
132 void ClearScreen(unsigned short *vram)
133 {
134 memset(vram, 0, 2*ScreenSize);
135 }
137 void speeddown(int ticks)
138 {
139 do{}while(SDL_GetTicks() - frametime < ticks);
140 frametime = SDL_GetTicks();
141 }
143 // -------------------------------------
145 void InitEffects()
146 {
147 InitBlitting();
148 InitPlasma();
149 InitRotozoomer();
150 InitRadialblur();
152 initRender3D();
153 InitBunny3D();
154 }
156 void Init()
157 {
158 SDL_Init (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
159 SDL_ShowCursor(SDL_DISABLE);
160 screen = SDL_SetVideoMode (ScreenWidth, ScreenHeight, 16, SDL_SWSURFACE);
162 if (SDL_NumJoysticks() > 0)
163 joy = SDL_JoystickOpen(0);
165 InitFonts();
166 InitEffects();
167 }
169 void KeyCommands()
170 {
171 SDL_Event event;
173 #ifdef ProjectWin
174 while (SDL_PollEvent (&event))
175 {
176 switch (event.type)
177 {
178 case SDL_KEYDOWN:
179 if (event.key.keysym.sym==SDLK_ESCAPE)
180 {
181 if (where_is==RES)
182 quit = 1;
183 else if (where_is==BENCH)
184 where_is = RES;
185 }
186 if (event.key.keysym.sym==SDLK_SPACE)
187 if (where_is==DESC)
188 desc_next = 1;
189 break;
191 case SDL_QUIT:
192 quit = 1;
193 break;
195 default:
196 break;
197 }
198 }
199 #else
200 while (SDL_PollEvent (&event))
201 {
202 switch (event.type)
203 {
204 case SDL_JOYBUTTONDOWN:
205 if ( event.jbutton.button == BUTTON_HOME )
206 {
207 if (where_is==RES)
208 quit = 1;
209 else if (where_is==BENCH)
210 where_is = RES;
211 }
213 if (event.jbutton.button==BUTTON_A)
214 if (where_is==DESC)
215 desc_next = 1;
216 break;
217 }
218 }
219 #endif
220 }
222 // -------------------------------------
224 char* test_name[NUM_TESTS];
225 char* test_description[NUM_TESTS];
226 int total_frames[NUM_TESTS];
227 int start_frames[NUM_TESTS+1];
228 float test_fps[NUM_TESTS];
230 int test_time_start;
232 void InitBench()
233 {
234 nframe = 0;
236 test_name[TEST_BLITTING] = "Blitting Test";
237 test_description[TEST_BLITTING] = "A simple memset blitting test. Tests how fast it is to write stuff to the video ram.";
238 total_frames[TEST_BLITTING] = 3000;
240 test_name[TEST_PLASMA] = "Plasma";
241 test_description[TEST_PLASMA] = "A lightweight demo effect. Addition of integer sine values read from small LUTs.";
242 total_frames[TEST_PLASMA] = 3000;
244 test_name[TEST_ROTOZOOMER] = "Rotozoomer";
245 test_description[TEST_ROTOZOOMER] = "Rotozoomer test 1. Zooming in and out.";
246 total_frames[TEST_ROTOZOOMER] = 2000;
248 test_name[TEST_ROTOZOOMER_NEAR] = "Rotozoomer Near";
249 test_description[TEST_ROTOZOOMER_NEAR] = "Rotozoomer test 2. Close up, optimum speed, cache does not affect.";
250 total_frames[TEST_ROTOZOOMER_NEAR] = 2500;
252 test_name[TEST_ROTOZOOMER_FAR] = "Rotozoomer Far";
253 test_description[TEST_ROTOZOOMER_FAR] = "Rotozoomer test 3. Far in the distance. Slowest version because of cache misses. Good tests for cache performance.";
254 total_frames[TEST_ROTOZOOMER_FAR] = 1500;
256 test_name[TEST_RADIALBLUR] = "Radial Blur";
257 test_description[TEST_RADIALBLUR] = "A true RGB effect with lot's of reads and huge arrays. The way this effect was written is not cache friendly and that could be also a good test for cache or memory read performance.";
258 total_frames[TEST_RADIALBLUR] = 500;
260 test_name[TEST_BUNNY3D] = "3D Bunny";
261 test_description[TEST_BUNNY3D] = "This is a massive model (69451 polygons) and a total overkill for most handhelds. Also, this is software rendering.";
262 total_frames[TEST_BUNNY3D] = 80;
264 int sum_frames = 0;
265 int i = 0;
266 for (i=0; i<NUM_TESTS; i++)
267 {
268 test_fps[i] = 0;
269 start_frames[i] = sum_frames;
270 sum_frames += total_frames[i];
271 }
272 start_frames[i] = sum_frames;
273 }
275 void RasterScreen(int ntime, int sr, int sg, int sb, unsigned short *vram)
276 {
277 float t = (float)ntime / 4.0f;
279 for (int y=0; y<ScreenHeight; y++)
280 {
281 int yp = (int)(sin((y+4*t)/15.0f)*cos((y-3*t)/63.0f)*15 + 16);
282 for (int x=0; x<ScreenWidth; x++)
283 {
284 *vram++ = ((yp>>sr)<<11) | (((yp << 1)>>sg) << 5) | (yp>>sb);
285 }
286 }
287 }
289 void SquaresScreen(int ntime, unsigned short *vram)
290 {
291 int t = ntime >> 1;
293 for (int y=0; y<ScreenHeight; y++)
294 {
295 for (int x=0; x<ScreenWidth; x++)
296 {
297 int c = (((x - t) & 15)==0 || ((y - t) & 15)==0) * 15;
298 *vram++ = ((c>>3)<<11) | (((c << 1)>>2) << 5) | (c>>1);
299 }
300 }
301 }
303 void RenderTestDescription(int test_num, int ntime, unsigned short *vram)
304 {
305 sprintf(sbuffer, test_name[test_num]);
306 DrawText(8, 8, 16, true, sbuffer, vram);
308 sprintf(sbuffer, test_description[test_num]);
309 DrawText(384 - 4 * ntime, 160, 256, false, sbuffer, vram);
310 }
312 void DrawBar(int char_y, float percentage, unsigned short *vram)
313 {
314 int xp = (int)((ScreenWidth - 4) * percentage);
316 unsigned short cbar = (7 << 11) | (31 << 5) | 15;
318 for (int y=0; y<9; y++)
319 {
320 int yi = char_y * 11 + y + 2;
321 for (int x=0; x<=xp; x++)
322 {
323 int xi = 2 + x;
324 int i = yi * ScreenWidth + xi;
325 *(vram + i) |= cbar;
326 }
327 }
328 }
330 void RenderTestResults(int ntime, float res_max, unsigned short *vram)
331 {
332 int tmaxframe = 64;
333 float tscale = (float)ntime / float(tmaxframe);
334 if (tscale > 1.0f) tscale = 1.0f;
336 for (int i=0; i<NUM_TESTS; i++)
337 {
338 sprintf(sbuffer, "%s", test_name[i]);
339 DrawText(8, i * 11 + 3, 32, false, sbuffer, vram);
341 sprintf(sbuffer, "%.1f", test_fps[i]);
342 DrawText(256, i * 11 + 3, 32, false, sbuffer, vram);
344 DrawBar(i, (test_fps[i] / res_max) * tscale, vram);
345 }
346 }
348 void show_test_description(int test_num, unsigned short *vram)
349 {
350 where_is = DESC;
352 int tframe = 0;
353 desc_next = 0;
355 srand(SDL_GetTicks());
357 int sr = (rand() % 2) + 1;
358 int sg = (rand() % 2) + 1;
359 int sb = (rand() % 2) + 1;
361 int l = 2 * strlen(test_description[test_num]) + 2 * 56;
363 while (!desc_next && tframe < l)
364 {
365 RasterScreen(tframe, sr, sg, sb, vram);
366 RenderTestDescription(test_num, tframe, vram);
367 SDL_Flip(screen);
368 speeddown(20);
369 KeyCommands();
370 tframe++;
371 }
372 }
374 float find_max_result()
375 {
376 float max = 0.0f;
377 for (int i=0; i<NUM_TESTS; i++)
378 {
379 if (test_fps[i] > max) max = test_fps[i];
380 }
382 return max;
383 }
385 void show_test_results(unsigned short *vram)
386 {
387 where_is = RES;
389 int tframe = 0;
391 float fmax = find_max_result();
393 while (!quit)
394 {
395 SquaresScreen(tframe, vram);
396 RenderTestResults(tframe, fmax, vram);
397 SDL_Flip(screen);
398 speeddown(20);
399 KeyCommands();
400 tframe++;
401 }
402 }
405 void RunBenchScript(int ntime, unsigned short *vram)
406 {
407 for (int i=0; i<=NUM_TESTS; i++)
408 {
409 if (nframe == start_frames[i])
410 {
411 if (i<NUM_TESTS)
412 show_test_description(i, vram);
414 test_time_start = SDL_GetTicks();
415 }
417 if (nframe == start_frames[i]-1)
418 test_fps[i-1] = (float)total_frames[i-1] / ((float)(SDL_GetTicks() - test_time_start) / 1000.0f);
419 }
421 if (where_is==RES)
422 show_test_results(vram);
424 if (nframe >= start_frames[TEST_BLITTING] && nframe < start_frames[TEST_PLASMA])
425 {
426 RunBlitting(ntime, vram);
427 }
428 else if (nframe >= start_frames[TEST_PLASMA] && nframe < start_frames[TEST_ROTOZOOMER])
429 {
430 RunPlasma(ntime, vram);
431 }
432 else if (nframe >= start_frames[TEST_ROTOZOOMER] && nframe < start_frames[TEST_ROTOZOOMER_NEAR])
433 {
434 RunRotozoomerNormal(ntime, vram);
435 }
436 else if (nframe >= start_frames[TEST_ROTOZOOMER_NEAR] && nframe < start_frames[TEST_ROTOZOOMER_FAR])
437 {
438 RunRotozoomerNear(ntime, vram);
439 }
440 else if (nframe >= start_frames[TEST_ROTOZOOMER_FAR] && nframe < start_frames[TEST_RADIALBLUR])
441 {
442 RunRotozoomerFar(ntime, vram);
443 }
444 else if (nframe >= start_frames[TEST_RADIALBLUR] && nframe < start_frames[TEST_BUNNY3D])
445 {
446 RunRadialblur(ntime, vram);
447 }
448 else if (nframe >= start_frames[TEST_BUNNY3D] && nframe < start_frames[NUM_TESTS])
449 {
450 RunBunny3D(ntime, vram);
451 }
452 else
453 {
454 show_test_results(vram);
455 }
456 }
458 void RunBench()
459 {
460 while (!quit)
461 {
462 where_is = BENCH;
463 KeyCommands();
465 unsigned short *vram = (unsigned short*)screen->pixels;
466 RunBenchScript(nframe, vram);
468 ShowFPS(vram);
470 SDL_Flip(screen);
471 nframe++;
472 }
473 }
475 void BackToSystem()
476 {
477 #if defined ProjectCaanoo || defined ProjectWiz
478 SDL_Quit();
479 chdir("/usr/gp2x");
480 execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);
481 #endif
482 }
484 int main (int argc, char *argv[])
485 {
486 Init();
488 InitBench();
489 RunBench();
491 BackToSystem();
492 }