gpmark
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/main.cpp Wed Jun 05 22:33:37 2013 +0300 1.3 @@ -0,0 +1,492 @@ 1.4 +#include <SDL/SDL.h> 1.5 +#include <math.h> 1.6 + 1.7 +#include "main.h" 1.8 +#include "bitfonts.h" 1.9 + 1.10 +#include "blitting.h" 1.11 +#include "plasma.h" 1.12 +#include "rotozoomer.h" 1.13 +#include "radialblur.h" 1.14 +#include "bunny3d.h" 1.15 +#include "render3d.h" 1.16 + 1.17 +#if defined ProjectCaanoo || defined ProjectWiz 1.18 + #include <unistd.h> 1.19 +#endif 1.20 + 1.21 +#ifdef ProjectWin 1.22 + #undef main 1.23 +#endif 1.24 + 1.25 +SDL_Surface *screen = NULL; 1.26 +SDL_Joystick *joy = NULL; 1.27 + 1.28 +int fps = 0, pframe = 0, nframe = 0, atime = 0; 1.29 +int frametime = 0; 1.30 +int quit = 0; 1.31 +int desc_next = 0; 1.32 + 1.33 +int where_is = MENU; 1.34 + 1.35 +// ------------------------------------- 1.36 + 1.37 +char sbuffer[256]; 1.38 +unsigned char fonts[59*64]; 1.39 + 1.40 +void InitFonts() 1.41 +{ 1.42 + int i = 0; 1.43 + for (int n=0; n<59; n++) 1.44 + { 1.45 + for (int y=0; y<8; y++) 1.46 + { 1.47 + int c = bitfonts[i++]; 1.48 + for (int x=0; x<8; x++) 1.49 + { 1.50 + fonts[(n << 6) + x + (y<<3)] = ((c >> (7 - x)) & 1) * 255; 1.51 + } 1.52 + } 1.53 + } 1.54 +} 1.55 + 1.56 +void DrawFontX2(int xp, int yp, int ch, unsigned short *vram) 1.57 +{ 1.58 + if (xp <0 || xp > 312) return; 1.59 + vram += xp + yp * ScreenWidth; 1.60 + for (int y=0; y<8; y++) 1.61 + { 1.62 + int yc = yp + y; 1.63 + if ((yc>=1) && (yc<ScreenHeight - 1)) 1.64 + { 1.65 + int yi = y << 3; 1.66 + for (int x=0; x<8; x++) 1.67 + { 1.68 + unsigned char c = fonts[(ch << 6) + yi + x] * 0x01010101; 1.69 + *vram |= c; 1.70 + *(vram+1) |= c; 1.71 + *(vram+ScreenWidth) |= c; 1.72 + *(vram+ScreenWidth+1) |= c; 1.73 + vram+=2; 1.74 + } 1.75 + vram-=16; 1.76 + } 1.77 + vram+=2*ScreenWidth; 1.78 + } 1.79 +} 1.80 + 1.81 +void DrawFont(int xp, int yp, int ch, unsigned short *vram) 1.82 +{ 1.83 + if (xp <0 || xp > 312) return; 1.84 + vram += xp + yp * ScreenWidth; 1.85 + for (int y=0; y<8; y++) 1.86 + { 1.87 + int yc = yp + y; 1.88 + if ((yc>=1) && (yc<ScreenHeight - 1)) 1.89 + { 1.90 + int yi = y << 3; 1.91 + for (int x=0; x<8; x++) 1.92 + { 1.93 + *vram++ |= (fonts[(ch << 6) + yi + x] * 0x01010101); 1.94 + } 1.95 + vram-=8; 1.96 + } 1.97 + vram+=ScreenWidth; 1.98 + } 1.99 +} 1.100 + 1.101 +void DrawText(int xtp, int ytp, int cn, bool zoom, char *text, unsigned short *vram) 1.102 +{ 1.103 + for (int n = 0; n<cn; n++) 1.104 + { 1.105 + char c = *text++; 1.106 + if (c>96 && c<123) c-=32; 1.107 + 1.108 + if (!zoom) 1.109 + { 1.110 + if (c>31 && c<92) DrawFont(xtp, ytp, c - 32, vram); 1.111 + else if (c==0) n = cn; 1.112 + xtp+=8; if (xtp>ScreenWidth -8 -1) n = cn; 1.113 + } 1.114 + else 1.115 + { 1.116 + if (c>31 && c<92) DrawFontX2(xtp, ytp, c - 32, vram); 1.117 + else if (c==0) n = cn; 1.118 + xtp+=16; if (xtp>ScreenWidth -16 -1) n = cn; 1.119 + } 1.120 + } 1.121 +} 1.122 + 1.123 +void ShowFPS(unsigned short *vram) 1.124 +{ 1.125 + if (SDL_GetTicks() - atime >= 1000) 1.126 + { 1.127 + atime = SDL_GetTicks(); 1.128 + fps = nframe - pframe; 1.129 + pframe = nframe; 1.130 + } 1.131 + sprintf(sbuffer, "FPS = %d", fps); 1.132 + DrawText(8, 8, 16, false, sbuffer, vram); 1.133 +} 1.134 + 1.135 +void ClearScreen(unsigned short *vram) 1.136 +{ 1.137 + memset(vram, 0, 2*ScreenSize); 1.138 +} 1.139 + 1.140 +void speeddown(int ticks) 1.141 +{ 1.142 + do{}while(SDL_GetTicks() - frametime < ticks); 1.143 + frametime = SDL_GetTicks(); 1.144 +} 1.145 + 1.146 +// ------------------------------------- 1.147 + 1.148 +void InitEffects() 1.149 +{ 1.150 + InitBlitting(); 1.151 + InitPlasma(); 1.152 + InitRotozoomer(); 1.153 + InitRadialblur(); 1.154 + 1.155 + initRender3D(); 1.156 + InitBunny3D(); 1.157 +} 1.158 + 1.159 +void Init() 1.160 +{ 1.161 + SDL_Init (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); 1.162 + SDL_ShowCursor(SDL_DISABLE); 1.163 + screen = SDL_SetVideoMode (ScreenWidth, ScreenHeight, 16, SDL_SWSURFACE); 1.164 + 1.165 + if (SDL_NumJoysticks() > 0) 1.166 + joy = SDL_JoystickOpen(0); 1.167 + 1.168 + InitFonts(); 1.169 + InitEffects(); 1.170 +} 1.171 + 1.172 +void KeyCommands() 1.173 +{ 1.174 + SDL_Event event; 1.175 + 1.176 +#ifdef ProjectWin 1.177 + while (SDL_PollEvent (&event)) 1.178 + { 1.179 + switch (event.type) 1.180 + { 1.181 + case SDL_KEYDOWN: 1.182 + if (event.key.keysym.sym==SDLK_ESCAPE) 1.183 + { 1.184 + if (where_is==RES) 1.185 + quit = 1; 1.186 + else if (where_is==BENCH) 1.187 + where_is = RES; 1.188 + } 1.189 + if (event.key.keysym.sym==SDLK_SPACE) 1.190 + if (where_is==DESC) 1.191 + desc_next = 1; 1.192 + break; 1.193 + 1.194 + case SDL_QUIT: 1.195 + quit = 1; 1.196 + break; 1.197 + 1.198 + default: 1.199 + break; 1.200 + } 1.201 + } 1.202 +#else 1.203 + while (SDL_PollEvent (&event)) 1.204 + { 1.205 + switch (event.type) 1.206 + { 1.207 + case SDL_JOYBUTTONDOWN: 1.208 + if ( event.jbutton.button == BUTTON_HOME ) 1.209 + { 1.210 + if (where_is==RES) 1.211 + quit = 1; 1.212 + else if (where_is==BENCH) 1.213 + where_is = RES; 1.214 + } 1.215 + 1.216 + if (event.jbutton.button==BUTTON_A) 1.217 + if (where_is==DESC) 1.218 + desc_next = 1; 1.219 + break; 1.220 + } 1.221 + } 1.222 +#endif 1.223 +} 1.224 + 1.225 +// ------------------------------------- 1.226 + 1.227 +char* test_name[NUM_TESTS]; 1.228 +char* test_description[NUM_TESTS]; 1.229 +int total_frames[NUM_TESTS]; 1.230 +int start_frames[NUM_TESTS+1]; 1.231 +float test_fps[NUM_TESTS]; 1.232 + 1.233 +int test_time_start; 1.234 + 1.235 +void InitBench() 1.236 +{ 1.237 + nframe = 0; 1.238 + 1.239 + test_name[TEST_BLITTING] = "Blitting Test"; 1.240 + test_description[TEST_BLITTING] = "A simple memset blitting test. Tests how fast it is to write stuff to the video ram."; 1.241 + total_frames[TEST_BLITTING] = 3000; 1.242 + 1.243 + test_name[TEST_PLASMA] = "Plasma"; 1.244 + test_description[TEST_PLASMA] = "A lightweight demo effect. Addition of integer sine values read from small LUTs."; 1.245 + total_frames[TEST_PLASMA] = 3000; 1.246 + 1.247 + test_name[TEST_ROTOZOOMER] = "Rotozoomer"; 1.248 + test_description[TEST_ROTOZOOMER] = "Rotozoomer test 1. Zooming in and out."; 1.249 + total_frames[TEST_ROTOZOOMER] = 2000; 1.250 + 1.251 + test_name[TEST_ROTOZOOMER_NEAR] = "Rotozoomer Near"; 1.252 + test_description[TEST_ROTOZOOMER_NEAR] = "Rotozoomer test 2. Close up, optimum speed, cache does not affect."; 1.253 + total_frames[TEST_ROTOZOOMER_NEAR] = 2500; 1.254 + 1.255 + test_name[TEST_ROTOZOOMER_FAR] = "Rotozoomer Far"; 1.256 + test_description[TEST_ROTOZOOMER_FAR] = "Rotozoomer test 3. Far in the distance. Slowest version because of cache misses. Good tests for cache performance."; 1.257 + total_frames[TEST_ROTOZOOMER_FAR] = 1500; 1.258 + 1.259 + test_name[TEST_RADIALBLUR] = "Radial Blur"; 1.260 + 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."; 1.261 + total_frames[TEST_RADIALBLUR] = 500; 1.262 + 1.263 + test_name[TEST_BUNNY3D] = "3D Bunny"; 1.264 + test_description[TEST_BUNNY3D] = "This is a massive model (69451 polygons) and a total overkill for most handhelds. Also, this is software rendering."; 1.265 + total_frames[TEST_BUNNY3D] = 80; 1.266 + 1.267 + int sum_frames = 0; 1.268 + int i = 0; 1.269 + for (i=0; i<NUM_TESTS; i++) 1.270 + { 1.271 + test_fps[i] = 0; 1.272 + start_frames[i] = sum_frames; 1.273 + sum_frames += total_frames[i]; 1.274 + } 1.275 + start_frames[i] = sum_frames; 1.276 +} 1.277 + 1.278 +void RasterScreen(int ntime, int sr, int sg, int sb, unsigned short *vram) 1.279 +{ 1.280 + float t = (float)ntime / 4.0f; 1.281 + 1.282 + for (int y=0; y<ScreenHeight; y++) 1.283 + { 1.284 + int yp = (int)(sin((y+4*t)/15.0f)*cos((y-3*t)/63.0f)*15 + 16); 1.285 + for (int x=0; x<ScreenWidth; x++) 1.286 + { 1.287 + *vram++ = ((yp>>sr)<<11) | (((yp << 1)>>sg) << 5) | (yp>>sb); 1.288 + } 1.289 + } 1.290 +} 1.291 + 1.292 +void SquaresScreen(int ntime, unsigned short *vram) 1.293 +{ 1.294 + int t = ntime >> 1; 1.295 + 1.296 + for (int y=0; y<ScreenHeight; y++) 1.297 + { 1.298 + for (int x=0; x<ScreenWidth; x++) 1.299 + { 1.300 + int c = (((x - t) & 15)==0 || ((y - t) & 15)==0) * 15; 1.301 + *vram++ = ((c>>3)<<11) | (((c << 1)>>2) << 5) | (c>>1); 1.302 + } 1.303 + } 1.304 +} 1.305 + 1.306 +void RenderTestDescription(int test_num, int ntime, unsigned short *vram) 1.307 +{ 1.308 + sprintf(sbuffer, test_name[test_num]); 1.309 + DrawText(8, 8, 16, true, sbuffer, vram); 1.310 + 1.311 + sprintf(sbuffer, test_description[test_num]); 1.312 + DrawText(384 - 4 * ntime, 160, 256, false, sbuffer, vram); 1.313 +} 1.314 + 1.315 +void DrawBar(int char_y, float percentage, unsigned short *vram) 1.316 +{ 1.317 + int xp = (int)((ScreenWidth - 4) * percentage); 1.318 + 1.319 + unsigned short cbar = (7 << 11) | (31 << 5) | 15; 1.320 + 1.321 + for (int y=0; y<9; y++) 1.322 + { 1.323 + int yi = char_y * 11 + y + 2; 1.324 + for (int x=0; x<=xp; x++) 1.325 + { 1.326 + int xi = 2 + x; 1.327 + int i = yi * ScreenWidth + xi; 1.328 + *(vram + i) |= cbar; 1.329 + } 1.330 + } 1.331 +} 1.332 + 1.333 +void RenderTestResults(int ntime, float res_max, unsigned short *vram) 1.334 +{ 1.335 + int tmaxframe = 64; 1.336 + float tscale = (float)ntime / float(tmaxframe); 1.337 + if (tscale > 1.0f) tscale = 1.0f; 1.338 + 1.339 + for (int i=0; i<NUM_TESTS; i++) 1.340 + { 1.341 + sprintf(sbuffer, "%s", test_name[i]); 1.342 + DrawText(8, i * 11 + 3, 32, false, sbuffer, vram); 1.343 + 1.344 + sprintf(sbuffer, "%.1f", test_fps[i]); 1.345 + DrawText(256, i * 11 + 3, 32, false, sbuffer, vram); 1.346 + 1.347 + DrawBar(i, (test_fps[i] / res_max) * tscale, vram); 1.348 + } 1.349 +} 1.350 + 1.351 +void show_test_description(int test_num, unsigned short *vram) 1.352 +{ 1.353 + where_is = DESC; 1.354 + 1.355 + int tframe = 0; 1.356 + desc_next = 0; 1.357 + 1.358 + srand(SDL_GetTicks()); 1.359 + 1.360 + int sr = (rand() % 2) + 1; 1.361 + int sg = (rand() % 2) + 1; 1.362 + int sb = (rand() % 2) + 1; 1.363 + 1.364 + int l = 2 * strlen(test_description[test_num]) + 2 * 56; 1.365 + 1.366 + while (!desc_next && tframe < l) 1.367 + { 1.368 + RasterScreen(tframe, sr, sg, sb, vram); 1.369 + RenderTestDescription(test_num, tframe, vram); 1.370 + SDL_Flip(screen); 1.371 + speeddown(20); 1.372 + KeyCommands(); 1.373 + tframe++; 1.374 + } 1.375 +} 1.376 + 1.377 +float find_max_result() 1.378 +{ 1.379 + float max = 0.0f; 1.380 + for (int i=0; i<NUM_TESTS; i++) 1.381 + { 1.382 + if (test_fps[i] > max) max = test_fps[i]; 1.383 + } 1.384 + 1.385 + return max; 1.386 +} 1.387 + 1.388 +void show_test_results(unsigned short *vram) 1.389 +{ 1.390 + where_is = RES; 1.391 + 1.392 + int tframe = 0; 1.393 + 1.394 + float fmax = find_max_result(); 1.395 + 1.396 + while (!quit) 1.397 + { 1.398 + SquaresScreen(tframe, vram); 1.399 + RenderTestResults(tframe, fmax, vram); 1.400 + SDL_Flip(screen); 1.401 + speeddown(20); 1.402 + KeyCommands(); 1.403 + tframe++; 1.404 + } 1.405 +} 1.406 + 1.407 + 1.408 +void RunBenchScript(int ntime, unsigned short *vram) 1.409 +{ 1.410 + for (int i=0; i<=NUM_TESTS; i++) 1.411 + { 1.412 + if (nframe == start_frames[i]) 1.413 + { 1.414 + if (i<NUM_TESTS) 1.415 + show_test_description(i, vram); 1.416 + 1.417 + test_time_start = SDL_GetTicks(); 1.418 + } 1.419 + 1.420 + if (nframe == start_frames[i]-1) 1.421 + test_fps[i-1] = (float)total_frames[i-1] / ((float)(SDL_GetTicks() - test_time_start) / 1000.0f); 1.422 + } 1.423 + 1.424 + if (where_is==RES) 1.425 + show_test_results(vram); 1.426 + 1.427 + if (nframe >= start_frames[TEST_BLITTING] && nframe < start_frames[TEST_PLASMA]) 1.428 + { 1.429 + RunBlitting(ntime, vram); 1.430 + } 1.431 + else if (nframe >= start_frames[TEST_PLASMA] && nframe < start_frames[TEST_ROTOZOOMER]) 1.432 + { 1.433 + RunPlasma(ntime, vram); 1.434 + } 1.435 + else if (nframe >= start_frames[TEST_ROTOZOOMER] && nframe < start_frames[TEST_ROTOZOOMER_NEAR]) 1.436 + { 1.437 + RunRotozoomerNormal(ntime, vram); 1.438 + } 1.439 + else if (nframe >= start_frames[TEST_ROTOZOOMER_NEAR] && nframe < start_frames[TEST_ROTOZOOMER_FAR]) 1.440 + { 1.441 + RunRotozoomerNear(ntime, vram); 1.442 + } 1.443 + else if (nframe >= start_frames[TEST_ROTOZOOMER_FAR] && nframe < start_frames[TEST_RADIALBLUR]) 1.444 + { 1.445 + RunRotozoomerFar(ntime, vram); 1.446 + } 1.447 + else if (nframe >= start_frames[TEST_RADIALBLUR] && nframe < start_frames[TEST_BUNNY3D]) 1.448 + { 1.449 + RunRadialblur(ntime, vram); 1.450 + } 1.451 + else if (nframe >= start_frames[TEST_BUNNY3D] && nframe < start_frames[NUM_TESTS]) 1.452 + { 1.453 + RunBunny3D(ntime, vram); 1.454 + } 1.455 + else 1.456 + { 1.457 + show_test_results(vram); 1.458 + } 1.459 +} 1.460 + 1.461 +void RunBench() 1.462 +{ 1.463 + while (!quit) 1.464 + { 1.465 + where_is = BENCH; 1.466 + KeyCommands(); 1.467 + 1.468 + unsigned short *vram = (unsigned short*)screen->pixels; 1.469 + RunBenchScript(nframe, vram); 1.470 + 1.471 + ShowFPS(vram); 1.472 + 1.473 + SDL_Flip(screen); 1.474 + nframe++; 1.475 + } 1.476 +} 1.477 + 1.478 +void BackToSystem() 1.479 +{ 1.480 + #if defined ProjectCaanoo || defined ProjectWiz 1.481 + SDL_Quit(); 1.482 + chdir("/usr/gp2x"); 1.483 + execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL); 1.484 + #endif 1.485 +} 1.486 + 1.487 +int main (int argc, char *argv[]) 1.488 +{ 1.489 + Init(); 1.490 + 1.491 + InitBench(); 1.492 + RunBench(); 1.493 + 1.494 + BackToSystem(); 1.495 +}