PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/CS/migrated/branches/R0_16/apps/perftest/perftest.cpp

#
C++ | 581 lines | 455 code | 71 blank | 55 comment | 55 complexity | 084498676f1123aabfc714740859da74 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0
  1. /*
  2. Copyright (C) 2000 by Jorrit Tyberghein
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this library; if not, write to the Free
  13. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include "cssysdef.h"
  16. #include "cssys/system.h"
  17. #include "csutil/inifile.h"
  18. #include "apps/perftest/perftest.h"
  19. #include "csgfxldr/csimage.h"
  20. #include "igraph3d.h"
  21. #include "itxtmgr.h"
  22. #include "iconsole.h"
  23. //------------------------------ We need the VFS plugin -----
  24. REGISTER_STATIC_LIBRARY (vfs)
  25. //-----------------------------------------------------------------------------
  26. void SetupPolygonDPFX (iGraphics3D* /*g3d*/, G3DPolygonDPFX& poly,
  27. float x1, float y1, float x2, float y2)
  28. {
  29. poly.num = 4;
  30. poly.vertices[0].sx = x1;
  31. poly.vertices[0].sy = y2;
  32. poly.vertices[0].z = 4.;
  33. poly.vertices[0].u = 0;
  34. poly.vertices[0].v = 0;
  35. poly.vertices[0].r = 1;
  36. poly.vertices[0].g = 0;
  37. poly.vertices[0].b = 0;
  38. poly.vertices[1].sx = x2;
  39. poly.vertices[1].sy = y2;
  40. poly.vertices[1].z = 4.;
  41. poly.vertices[1].u = 1;
  42. poly.vertices[1].v = 0;
  43. poly.vertices[1].r = 0;
  44. poly.vertices[1].g = 1;
  45. poly.vertices[1].b = 0;
  46. poly.vertices[2].sx = x2;
  47. poly.vertices[2].sy = y1;
  48. poly.vertices[2].z = 4.;
  49. poly.vertices[2].u = 1;
  50. poly.vertices[2].v = 1;
  51. poly.vertices[2].r = 0;
  52. poly.vertices[2].g = 0;
  53. poly.vertices[2].b = 1;
  54. poly.vertices[3].sx = x1;
  55. poly.vertices[3].sy = y1;
  56. poly.vertices[3].z = 4.;
  57. poly.vertices[3].u = 0;
  58. poly.vertices[3].v = 1;
  59. poly.vertices[3].r = 1;
  60. poly.vertices[3].g = 1;
  61. poly.vertices[3].b = 0;
  62. poly.use_fog = false;
  63. poly.inv_aspect = 1./400.;
  64. poly.txt_handle = NULL;
  65. poly.flat_color_r = 255;
  66. poly.flat_color_g = 255;
  67. poly.flat_color_b = 255;
  68. }
  69. //-----------------------------------------------------------------------------
  70. void SinglePolygonTester::Setup (iGraphics3D* g3d, PerfTest* perftest)
  71. {
  72. draw = 0;
  73. SetupPolygonDPFX (g3d, poly, 10, 10, g3d->GetWidth ()-10, g3d->GetHeight ()-10);
  74. poly.txt_handle = perftest->GetTexture (0);
  75. }
  76. void SinglePolygonTester::Draw (iGraphics3D* g3d)
  77. {
  78. draw++;
  79. g3d->SetRenderState (G3DRENDERSTATE_ZBUFFERMODE, CS_ZBUF_FILL);
  80. g3d->StartPolygonFX (poly.txt_handle, CS_FX_COPY|CS_FX_GOURAUD);
  81. g3d->DrawPolygonFX (poly);
  82. g3d->FinishPolygonFX ();
  83. }
  84. Tester* SinglePolygonTester::NextTester ()
  85. {
  86. return new SinglePolygonTesterFlat ();
  87. }
  88. //-----------------------------------------------------------------------------
  89. void SinglePolygonTesterFlat::Setup (iGraphics3D* g3d, PerfTest* /*perftest*/)
  90. {
  91. draw = 0;
  92. SetupPolygonDPFX (g3d, poly, 10, 10, g3d->GetWidth ()-10, g3d->GetHeight ()-10);
  93. }
  94. void SinglePolygonTesterFlat::Draw (iGraphics3D* g3d)
  95. {
  96. draw++;
  97. g3d->SetRenderState (G3DRENDERSTATE_ZBUFFERMODE, CS_ZBUF_FILL);
  98. g3d->StartPolygonFX (NULL, CS_FX_COPY|CS_FX_GOURAUD);
  99. g3d->DrawPolygonFX (poly);
  100. g3d->FinishPolygonFX ();
  101. }
  102. Tester* SinglePolygonTesterFlat::NextTester ()
  103. {
  104. return new SinglePolygonTesterAlpha ();
  105. }
  106. //-----------------------------------------------------------------------------
  107. void SinglePolygonTesterAlpha::Setup (iGraphics3D* g3d, PerfTest* perftest)
  108. {
  109. draw = 0;
  110. SetupPolygonDPFX (g3d, poly, 10, 10, g3d->GetWidth ()-10, g3d->GetHeight ()-10);
  111. poly.txt_handle = perftest->GetTexture (0);
  112. }
  113. void SinglePolygonTesterAlpha::Draw (iGraphics3D* g3d)
  114. {
  115. draw++;
  116. g3d->SetRenderState (G3DRENDERSTATE_ZBUFFERMODE, CS_ZBUF_FILL);
  117. g3d->StartPolygonFX (poly.txt_handle, CS_FX_SETALPHA(.5) |CS_FX_GOURAUD);
  118. g3d->DrawPolygonFX (poly);
  119. g3d->FinishPolygonFX ();
  120. }
  121. Tester* SinglePolygonTesterAlpha::NextTester ()
  122. {
  123. return new MultiPolygonTester ();
  124. }
  125. //-----------------------------------------------------------------------------
  126. void MultiPolygonTester::Setup (iGraphics3D* g3d, PerfTest* perftest)
  127. {
  128. draw = 0;
  129. int x, y;
  130. int w = g3d->GetWidth ()-20;
  131. int h = g3d->GetHeight ()-20;
  132. for (y = 0 ; y < NUM_MULTIPOLTEST ; y++)
  133. for (x = 0 ; x < NUM_MULTIPOLTEST ; x++)
  134. {
  135. SetupPolygonDPFX (g3d, poly[x][y], 10+x*w/NUM_MULTIPOLTEST,
  136. 10+y*h/NUM_MULTIPOLTEST,
  137. 10+(x+1)*w/NUM_MULTIPOLTEST, 10+(y+1)*h/NUM_MULTIPOLTEST);
  138. poly[x][y].txt_handle = perftest->GetTexture (0);
  139. }
  140. }
  141. void MultiPolygonTester::Draw (iGraphics3D* g3d)
  142. {
  143. draw++;
  144. g3d->SetRenderState (G3DRENDERSTATE_ZBUFFERMODE, CS_ZBUF_FILL);
  145. g3d->StartPolygonFX (poly[0][0].txt_handle, CS_FX_COPY|CS_FX_GOURAUD);
  146. int x, y;
  147. for (y = 0 ; y < NUM_MULTIPOLTEST ; y++)
  148. for (x = 0 ; x < NUM_MULTIPOLTEST ; x++)
  149. {
  150. g3d->DrawPolygonFX (poly[x][y]);
  151. }
  152. g3d->FinishPolygonFX ();
  153. }
  154. Tester* MultiPolygonTester::NextTester ()
  155. {
  156. return new MultiPolygon2Tester ();
  157. }
  158. //-----------------------------------------------------------------------------
  159. void MultiPolygon2Tester::Setup (iGraphics3D* g3d, PerfTest* perftest)
  160. {
  161. draw = 0;
  162. int x, y;
  163. int w = g3d->GetWidth ()-20;
  164. int h = g3d->GetHeight ()-20;
  165. for (y = 0 ; y < NUM_MULTIPOLTEST ; y++)
  166. for (x = 0 ; x < NUM_MULTIPOLTEST ; x++)
  167. {
  168. SetupPolygonDPFX (g3d, poly[x][y], 10+x*w/NUM_MULTIPOLTEST,
  169. 10+y*h/NUM_MULTIPOLTEST,
  170. 10+(x+1)*w/NUM_MULTIPOLTEST, 10+(y+1)*h/NUM_MULTIPOLTEST);
  171. poly[x][y].txt_handle = perftest->GetTexture (0);
  172. }
  173. }
  174. void MultiPolygon2Tester::Draw (iGraphics3D* g3d)
  175. {
  176. draw++;
  177. g3d->SetRenderState (G3DRENDERSTATE_ZBUFFERMODE, CS_ZBUF_FILL);
  178. int x, y;
  179. for (y = 0 ; y < NUM_MULTIPOLTEST ; y++)
  180. for (x = 0 ; x < NUM_MULTIPOLTEST ; x++)
  181. {
  182. g3d->StartPolygonFX (poly[x][y].txt_handle, CS_FX_COPY|CS_FX_GOURAUD);
  183. g3d->DrawPolygonFX (poly[x][y]);
  184. g3d->FinishPolygonFX ();
  185. }
  186. }
  187. Tester* MultiPolygon2Tester::NextTester ()
  188. {
  189. return new MultiTexture1Tester ();
  190. }
  191. //-----------------------------------------------------------------------------
  192. void MultiTexture1Tester::Setup (iGraphics3D* g3d, PerfTest* perftest)
  193. {
  194. draw = 0;
  195. int x, y;
  196. int w = g3d->GetWidth ()-20;
  197. int h = g3d->GetHeight ()-20;
  198. int i = 0;
  199. for (y = 0 ; y < NUM_MULTIPOLTEST ; y++)
  200. for (x = 0 ; x < NUM_MULTIPOLTEST ; x++)
  201. {
  202. SetupPolygonDPFX (g3d, poly[x][y], 10+x*w/NUM_MULTIPOLTEST,
  203. 10+y*h/NUM_MULTIPOLTEST,
  204. 10+(x+1)*w/NUM_MULTIPOLTEST, 10+(y+1)*h/NUM_MULTIPOLTEST);
  205. poly[x][y].txt_handle = perftest->GetTexture (i%4);
  206. i++;
  207. }
  208. }
  209. void MultiTexture1Tester::Draw (iGraphics3D* g3d)
  210. {
  211. draw++;
  212. g3d->SetRenderState (G3DRENDERSTATE_ZBUFFERMODE, CS_ZBUF_FILL);
  213. int x, y;
  214. for (y = 0 ; y < NUM_MULTIPOLTEST ; y++)
  215. for (x = 0 ; x < NUM_MULTIPOLTEST ; x++)
  216. {
  217. g3d->StartPolygonFX (poly[x][y].txt_handle, CS_FX_COPY);
  218. g3d->DrawPolygonFX (poly[x][y]);
  219. g3d->FinishPolygonFX ();
  220. }
  221. }
  222. Tester* MultiTexture1Tester::NextTester ()
  223. {
  224. return new MultiTexture2Tester ();
  225. }
  226. //-----------------------------------------------------------------------------
  227. void MultiTexture2Tester::Setup (iGraphics3D* g3d, PerfTest* perftest)
  228. {
  229. draw = 0;
  230. int x, y;
  231. int w = g3d->GetWidth ()-20;
  232. int h = g3d->GetHeight ()-20;
  233. int i = 0;
  234. int div = (NUM_MULTIPOLTEST*NUM_MULTIPOLTEST)/4;
  235. for (y = 0 ; y < NUM_MULTIPOLTEST ; y++)
  236. for (x = 0 ; x < NUM_MULTIPOLTEST ; x++)
  237. {
  238. SetupPolygonDPFX (g3d, poly[x][y], 10+x*w/NUM_MULTIPOLTEST,
  239. 10+y*h/NUM_MULTIPOLTEST,
  240. 10+(x+1)*w/NUM_MULTIPOLTEST, 10+(y+1)*h/NUM_MULTIPOLTEST);
  241. poly[x][y].txt_handle = perftest->GetTexture (i/div);
  242. i++;
  243. }
  244. }
  245. void MultiTexture2Tester::Draw (iGraphics3D* g3d)
  246. {
  247. draw++;
  248. g3d->SetRenderState (G3DRENDERSTATE_ZBUFFERMODE, CS_ZBUF_FILL);
  249. iTextureHandle* prev = NULL;
  250. int x, y;
  251. for (y = 0 ; y < NUM_MULTIPOLTEST ; y++)
  252. for (x = 0 ; x < NUM_MULTIPOLTEST ; x++)
  253. {
  254. if (poly[x][y].txt_handle != prev)
  255. {
  256. if (prev != NULL) g3d->FinishPolygonFX ();
  257. g3d->StartPolygonFX (poly[x][y].txt_handle, CS_FX_COPY);
  258. prev = poly[x][y].txt_handle;
  259. }
  260. g3d->DrawPolygonFX (poly[x][y]);
  261. }
  262. g3d->FinishPolygonFX ();
  263. }
  264. Tester* MultiTexture2Tester::NextTester ()
  265. {
  266. return new MeshTester ();
  267. }
  268. //-----------------------------------------------------------------------------
  269. void MeshTester::Setup (iGraphics3D* g3d, PerfTest* perftest)
  270. {
  271. draw = 0;
  272. mesh.num_vertices = (NUM_MULTIPOLTEST+1)*(NUM_MULTIPOLTEST/2+1);
  273. mesh.num_vertices_pool = 1;
  274. mesh.num_textures = 1;
  275. mesh.num_triangles = NUM_MULTIPOLTEST*NUM_MULTIPOLTEST;
  276. mesh.triangles = new csTriangle [mesh.num_triangles];
  277. mesh.use_vertex_color = false;
  278. mesh.do_clip = false;
  279. mesh.do_fog = false;
  280. mesh.do_mirror = false;
  281. mesh.do_morph_texels = false;
  282. mesh.do_morph_colors = false;
  283. mesh.vertex_mode = G3DTriangleMesh::VM_VIEWSPACE;
  284. mesh.fxmode = CS_FX_COPY;
  285. mesh.morph_factor = 0;
  286. mesh.vertices[0] = new csVector3 [mesh.num_vertices];
  287. mesh.texels[0][0] = new csVector2 [mesh.num_vertices];
  288. mesh.txt_handle[0] = perftest->GetTexture (0);
  289. int i;
  290. int x, y;
  291. float w = (g3d->GetWidth ()-20)/2;
  292. float h = (g3d->GetHeight ()-20);
  293. i = 0;
  294. for (y = 0 ; y <= NUM_MULTIPOLTEST/2 ; y++)
  295. for (x = 0 ; x <= NUM_MULTIPOLTEST ; x++)
  296. {
  297. mesh.vertices[0][i].Set (
  298. w * float (x-NUM_MULTIPOLTEST/2) / float (NUM_MULTIPOLTEST/2),
  299. h * float (y-NUM_MULTIPOLTEST/2) / float (NUM_MULTIPOLTEST/2) + h/2,
  300. 1.);
  301. mesh.texels[0][0][i].Set (
  302. float (x) / float (NUM_MULTIPOLTEST),
  303. float (y) / float (NUM_MULTIPOLTEST/2)
  304. );
  305. i++;
  306. }
  307. i = 0;
  308. for (y = 0 ; y < NUM_MULTIPOLTEST/2 ; y++)
  309. for (x = 0 ; x < NUM_MULTIPOLTEST ; x++)
  310. {
  311. mesh.triangles[i].c = y*(NUM_MULTIPOLTEST+1) + x;
  312. mesh.triangles[i].b = y*(NUM_MULTIPOLTEST+1) + x + 1;
  313. mesh.triangles[i].a = (y+1)*(NUM_MULTIPOLTEST+1) + x;
  314. i++;
  315. mesh.triangles[i].c = y*(NUM_MULTIPOLTEST+1) + x + 1;
  316. mesh.triangles[i].b = (y+1)*(NUM_MULTIPOLTEST+1) + x + 1;
  317. mesh.triangles[i].a = (y+1)*(NUM_MULTIPOLTEST+1) + x;
  318. i++;
  319. }
  320. }
  321. void MeshTester::Draw (iGraphics3D* g3d)
  322. {
  323. draw++;
  324. g3d->SetRenderState (G3DRENDERSTATE_ZBUFFERMODE, CS_ZBUF_FILL);
  325. //g3d->SetObjectToCamera ();
  326. g3d->SetPerspectiveAspect (1);//g3d->GetHeight ());
  327. g3d->DrawTriangleMesh (mesh);
  328. }
  329. Tester* MeshTester::NextTester ()
  330. {
  331. delete [] mesh.triangles;
  332. delete [] mesh.vertices[0];
  333. delete [] mesh.texels[0][0];
  334. return NULL;
  335. }
  336. //-----------------------------------------------------------------------------
  337. PerfTest::PerfTest ()
  338. {
  339. }
  340. PerfTest::~PerfTest ()
  341. {
  342. }
  343. void cleanup ()
  344. {
  345. System->console_out ("Cleaning up...\n");
  346. delete System;
  347. }
  348. iTextureHandle* PerfTest::LoadTexture (char* file)
  349. {
  350. iTextureManager* txtmgr = G3D->GetTextureManager ();
  351. size_t size;
  352. char* buf;
  353. iImage* image;
  354. buf = VFS->ReadFile (file, size);
  355. if (!buf || !size)
  356. {
  357. Printf (MSG_FATAL_ERROR, "Error loading texture '%s'!\n", file);
  358. exit (-1);
  359. }
  360. image = csImageLoader::Load ((UByte*)buf, size,
  361. txtmgr->GetTextureFormat ());
  362. delete [] buf;
  363. if (!image) exit (-1);
  364. iTextureHandle* texture = txtmgr->RegisterTexture (image, CS_TEXTURE_3D);
  365. if (!texture) exit (-1);
  366. image->DecRef ();
  367. return texture;
  368. }
  369. bool PerfTest::Initialize (int argc, const char* const argv[],
  370. const char *iConfigName)
  371. {
  372. if (!superclass::Initialize (argc, argv, iConfigName))
  373. return false;
  374. // Open the main system. This will open all the previously loaded plug-ins.
  375. if (!Open ("Crystal Space 3D Performance Tester"))
  376. {
  377. Printf (MSG_FATAL_ERROR, "Error opening system!\n");
  378. cleanup ();
  379. exit (1);
  380. }
  381. // Setup the texture manager
  382. iTextureManager* txtmgr = G3D->GetTextureManager ();
  383. txtmgr->SetVerbose (true);
  384. // Initialize the texture manager
  385. txtmgr->ResetPalette ();
  386. // Initialize textures.
  387. texture[0] = LoadTexture ("/lib/std/stone4.gif");
  388. texture[1] = LoadTexture ("/lib/std/mystone2.gif");
  389. texture[2] = LoadTexture ("/lib/std/andrew_marble4.gif");
  390. texture[3] = LoadTexture ("/lib/std/andrew_wood.gif");
  391. txtmgr->PrepareTextures ();
  392. // Allocate a uniformly distributed in R,G,B space palette for console
  393. // The console will crash on some platforms if this isn't initialize properly
  394. int r,g,b;
  395. for (r = 0; r < 8; r++)
  396. for (g = 0; g < 8; g++)
  397. for (b = 0; b < 4; b++)
  398. txtmgr->ReserveColor (r * 32, g * 32, b * 64);
  399. txtmgr->SetPalette ();
  400. // Initialize the console
  401. if (Console != NULL)
  402. {
  403. // Setup console colors
  404. Console->CacheColors (txtmgr);
  405. ConsoleReady = true;
  406. // Don't let messages before this one appear
  407. Console->Clear ();
  408. }
  409. // Some commercials...
  410. Printf (MSG_INITIALIZATION,
  411. "Crystal Space 3D Performance Tester 0.1.\n");
  412. txtmgr->SetPalette ();
  413. // Update the console with the new palette
  414. if (System->Console != NULL)
  415. System->Console->CacheColors (txtmgr);
  416. current_tester = new SinglePolygonTester ();
  417. needs_setup = true;
  418. return true;
  419. }
  420. static time_t last_time;
  421. void PerfTest::NextFrame (time_t elapsed_time, time_t current_time)
  422. {
  423. superclass::NextFrame (elapsed_time, current_time);
  424. // Tell 3D driver we're going to display 3D things.
  425. if (!G3D->BeginDraw (CSDRAW_3DGRAPHICS)) return;
  426. // Setup if needed.
  427. if (needs_setup)
  428. {
  429. current_tester->Setup (G3D, this);
  430. last_time = current_time;
  431. }
  432. // Do the test frame.
  433. if (current_tester)
  434. {
  435. current_tester->Draw (G3D);
  436. if (current_time-last_time >= 10000)
  437. {
  438. Printf (MSG_INITIALIZATION, "%f FPS\n",
  439. current_tester->GetCount ()/10.);
  440. Tester* next_tester = current_tester->NextTester ();
  441. delete current_tester;
  442. current_tester = next_tester;
  443. if (current_tester)
  444. {
  445. needs_setup = true;
  446. current_tester->Setup (G3D, this);
  447. last_time = current_time;
  448. }
  449. }
  450. }
  451. // Start drawing 2D graphics.
  452. if (needs_setup)
  453. {
  454. if (!G3D->BeginDraw (CSDRAW_2DGRAPHICS)) return;
  455. Console->Clear ();
  456. char desc[255];
  457. current_tester->Description (desc);
  458. Printf (MSG_INITIALIZATION, desc);
  459. needs_setup = false;
  460. }
  461. // Drawing code ends here.
  462. G3D->FinishDraw ();
  463. // Print the final output.
  464. G3D->Print (NULL);
  465. }
  466. bool PerfTest::HandleEvent (csEvent &Event)
  467. {
  468. if (superclass::HandleEvent (Event))
  469. return true;
  470. if ((Event.Type == csevKeyDown) && (Event.Key.Code == CSKEY_ESC))
  471. {
  472. StartShutdown ();
  473. return true;
  474. }
  475. else if ((Event.Type == csevKeyDown) && (Event.Key.Code == ' '))
  476. {
  477. last_time -= 10000;
  478. return true;
  479. }
  480. return false;
  481. }
  482. /*---------------------------------------------------------------------*
  483. * Main function
  484. *---------------------------------------------------------------------*/
  485. int main (int argc, char* argv[])
  486. {
  487. srand (time (NULL));
  488. // Create our main class.
  489. System = new PerfTest ();
  490. // We want at least the minimal set of plugins
  491. System->RequestPlugin ("crystalspace.kernel.vfs:VFS");
  492. System->RequestPlugin ("crystalspace.graphics3d.software:VideoDriver");
  493. System->RequestPlugin ("crystalspace.console.stdout:Console");
  494. // Initialize the main system. This will load all needed plug-ins
  495. // (3D, 2D, network, sound, ...) and initialize them.
  496. if (!System->Initialize (argc, argv, NULL))
  497. {
  498. System->Printf (MSG_FATAL_ERROR, "Error initializing system!\n");
  499. cleanup ();
  500. exit (1);
  501. }
  502. // Main loop.
  503. System->Loop ();
  504. // Cleanup.
  505. cleanup ();
  506. return 0;
  507. }