PerformanceTest.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. // Jolt includes
  5. #include <Jolt/Jolt.h>
  6. #include <Jolt/ConfigurationString.h>
  7. #include <Jolt/RegisterTypes.h>
  8. #include <Jolt/Core/Factory.h>
  9. #include <Jolt/Core/TempAllocator.h>
  10. #include <Jolt/Core/JobSystemThreadPool.h>
  11. #include <Jolt/Physics/PhysicsSettings.h>
  12. #include <Jolt/Physics/PhysicsSystem.h>
  13. #include <Jolt/Physics/Collision/NarrowPhaseStats.h>
  14. #include <Jolt/Physics/StateRecorderImpl.h>
  15. #include <Jolt/Physics/DeterminismLog.h>
  16. #ifdef JPH_DEBUG_RENDERER
  17. #include <Jolt/Renderer/DebugRendererRecorder.h>
  18. #include <Jolt/Core/StreamWrapper.h>
  19. #endif // JPH_DEBUG_RENDERER
  20. #ifdef JPH_PLATFORM_ANDROID
  21. #include <android/log.h>
  22. #include <android_native_app_glue.h>
  23. #endif // JPH_PLATFORM_ANDROID
  24. // STL includes
  25. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  26. #include <iostream>
  27. #include <thread>
  28. #include <chrono>
  29. #include <memory>
  30. #include <cstdarg>
  31. #include <filesystem>
  32. JPH_SUPPRESS_WARNINGS_STD_END
  33. using namespace JPH;
  34. using namespace JPH::literals;
  35. using namespace std;
  36. // Disable common warnings triggered by Jolt
  37. JPH_SUPPRESS_WARNINGS
  38. // Local includes
  39. #include "RagdollScene.h"
  40. #include "ConvexVsMeshScene.h"
  41. #include "PyramidScene.h"
  42. #include "LargeMeshScene.h"
  43. #include "CharacterVirtualScene.h"
  44. #include "MaxBodiesScene.h"
  45. // Time step for physics
  46. constexpr float cDeltaTime = 1.0f / 60.0f;
  47. static void TraceImpl(const char *inFMT, ...)
  48. {
  49. // Format the message
  50. va_list list;
  51. va_start(list, inFMT);
  52. char buffer[1024];
  53. vsnprintf(buffer, sizeof(buffer), inFMT, list);
  54. va_end(list);
  55. // Print to the TTY
  56. #ifndef JPH_PLATFORM_ANDROID
  57. cout << buffer << endl;
  58. #else
  59. __android_log_write(ANDROID_LOG_INFO, "Jolt", buffer);
  60. #endif
  61. }
  62. // Program entry point
  63. int main(int argc, char** argv)
  64. {
  65. // Install callbacks
  66. Trace = TraceImpl;
  67. // Register allocation hook
  68. RegisterDefaultAllocator();
  69. // Helper function that creates the default scene
  70. #ifdef JPH_OBJECT_STREAM
  71. auto create_ragdoll_scene = []{ return unique_ptr<PerformanceTestScene>(new RagdollScene(4, 10, 0.6f)); };
  72. #else
  73. auto create_ragdoll_scene = []{ return unique_ptr<PerformanceTestScene>(new ConvexVsMeshScene); };
  74. #endif // JPH_OBJECT_STREAM
  75. // Parse command line parameters
  76. int specified_quality = -1;
  77. int specified_threads = -1;
  78. uint max_iterations = 500;
  79. bool disable_sleep = false;
  80. bool enable_profiler = false;
  81. #ifdef JPH_DEBUG_RENDERER
  82. bool enable_debug_renderer = false;
  83. #endif // JPH_DEBUG_RENDERER
  84. bool enable_per_frame_recording = false;
  85. bool record_state = false;
  86. bool validate_state = false;
  87. unique_ptr<PerformanceTestScene> scene;
  88. const char *validate_hash = nullptr;
  89. int repeat = 1;
  90. for (int argidx = 1; argidx < argc; ++argidx)
  91. {
  92. const char *arg = argv[argidx];
  93. if (strncmp(arg, "-s=", 3) == 0)
  94. {
  95. // Parse scene
  96. if (strcmp(arg + 3, "Ragdoll") == 0)
  97. scene = create_ragdoll_scene();
  98. #ifdef JPH_OBJECT_STREAM
  99. else if (strcmp(arg + 3, "RagdollSinglePile") == 0)
  100. scene = unique_ptr<PerformanceTestScene>(new RagdollScene(1, 160, 0.4f));
  101. #endif // JPH_OBJECT_STREAM
  102. else if (strcmp(arg + 3, "ConvexVsMesh") == 0)
  103. scene = unique_ptr<PerformanceTestScene>(new ConvexVsMeshScene);
  104. else if (strcmp(arg + 3, "Pyramid") == 0)
  105. scene = unique_ptr<PerformanceTestScene>(new PyramidScene);
  106. else if (strcmp(arg + 3, "LargeMesh") == 0)
  107. scene = unique_ptr<PerformanceTestScene>(new LargeMeshScene);
  108. else if (strcmp(arg + 3, "CharacterVirtual") == 0)
  109. scene = unique_ptr<PerformanceTestScene>(new CharacterVirtualScene);
  110. else if (strcmp(arg + 3, "MaxBodies") == 0)
  111. scene = unique_ptr<MaxBodiesScene>(new MaxBodiesScene);
  112. else
  113. {
  114. Trace("Invalid scene");
  115. return 1;
  116. }
  117. }
  118. else if (strncmp(arg, "-i=", 3) == 0)
  119. {
  120. // Parse max iterations
  121. max_iterations = (uint)atoi(arg + 3);
  122. }
  123. else if (strncmp(arg, "-q=", 3) == 0)
  124. {
  125. // Parse quality
  126. if (strcmp(arg + 3, "Discrete") == 0)
  127. specified_quality = 0;
  128. else if (strcmp(arg + 3, "LinearCast") == 0)
  129. specified_quality = 1;
  130. else
  131. {
  132. Trace("Invalid quality");
  133. return 1;
  134. }
  135. }
  136. else if (strncmp(arg, "-t=max", 6) == 0)
  137. {
  138. // Default to number of threads on the system
  139. specified_threads = thread::hardware_concurrency();
  140. }
  141. else if (strncmp(arg, "-t=", 3) == 0)
  142. {
  143. // Parse threads
  144. specified_threads = atoi(arg + 3);
  145. }
  146. else if (strcmp(arg, "-no_sleep") == 0)
  147. {
  148. disable_sleep = true;
  149. }
  150. else if (strcmp(arg, "-p") == 0)
  151. {
  152. enable_profiler = true;
  153. }
  154. #ifdef JPH_DEBUG_RENDERER
  155. else if (strcmp(arg, "-r") == 0)
  156. {
  157. enable_debug_renderer = true;
  158. }
  159. #endif // JPH_DEBUG_RENDERER
  160. else if (strcmp(arg, "-f") == 0)
  161. {
  162. enable_per_frame_recording = true;
  163. }
  164. else if (strcmp(arg, "-rs") == 0)
  165. {
  166. record_state = true;
  167. }
  168. else if (strcmp(arg, "-vs") == 0)
  169. {
  170. validate_state = true;
  171. }
  172. else if (strncmp(arg, "-validate_hash=", 15) == 0)
  173. {
  174. validate_hash = arg + 15;
  175. }
  176. else if (strncmp(arg, "-repeat=", 8) == 0)
  177. {
  178. // Parse repeat count
  179. repeat = atoi(arg + 8);
  180. }
  181. else if (strcmp(arg, "-h") == 0)
  182. {
  183. // Print usage
  184. Trace("Usage:\n"
  185. "-s=<scene>: Select scene (Ragdoll, RagdollSinglePile, ConvexVsMesh, Pyramid)\n"
  186. "-i=<num physics steps>: Number of physics steps to simulate (default 500)\n"
  187. "-q=<quality>: Test only with specified quality (Discrete, LinearCast)\n"
  188. "-t=<num threads>: Test only with N threads (default is to iterate over 1 .. num hardware threads)\n"
  189. "-t=max: Test with the number of threads available on the system\n"
  190. "-p: Write out profiles\n"
  191. "-r: Record debug renderer output for JoltViewer\n"
  192. "-f: Record per frame timings\n"
  193. "-no_sleep: Disable sleeping\n"
  194. "-rs: Record state\n"
  195. "-vs: Validate state\n"
  196. "-validate_hash=<hash>: Validate hash (return 0 if successful, 1 if failed)\n"
  197. "-repeat=<num>: Repeat all tests <num> times");
  198. return 0;
  199. }
  200. }
  201. // Create a factory
  202. Factory::sInstance = new Factory();
  203. // Register all Jolt physics types
  204. RegisterTypes();
  205. // Show used instruction sets
  206. Trace(GetConfigurationString());
  207. // If no scene was specified use the default scene
  208. if (scene == nullptr)
  209. scene = create_ragdoll_scene();
  210. // Output scene we're running
  211. Trace("Running scene: %s", scene->GetName());
  212. // Create temp allocator
  213. TempAllocatorImpl temp_allocator(scene->GetTempAllocatorSizeMB() * 1024 * 1024);
  214. // Find the asset path
  215. bool found = false;
  216. filesystem::path asset_path(argv[0]);
  217. filesystem::path root_path = asset_path.root_path();
  218. while (asset_path != root_path)
  219. {
  220. asset_path = asset_path.parent_path();
  221. if (filesystem::exists(asset_path / "Assets"))
  222. {
  223. found = true;
  224. break;
  225. }
  226. }
  227. if (!found) // Note that argv[0] can be a relative path like './PerformanceTest' so we also scan up using '..'
  228. for (int i = 0; i < 5; ++i)
  229. {
  230. asset_path /= "..";
  231. if (filesystem::exists(asset_path / "Assets"))
  232. {
  233. found = true;
  234. break;
  235. }
  236. }
  237. if (!found)
  238. asset_path = "Assets";
  239. else
  240. asset_path /= "Assets";
  241. asset_path /= "";
  242. // Load the scene
  243. if (!scene->Load(String(asset_path.string())))
  244. return 1;
  245. // Create mapping table from object layer to broadphase layer
  246. BPLayerInterfaceImpl broad_phase_layer_interface;
  247. // Create class that filters object vs broadphase layers
  248. ObjectVsBroadPhaseLayerFilterImpl object_vs_broadphase_layer_filter;
  249. // Create class that filters object vs object layers
  250. ObjectLayerPairFilterImpl object_vs_object_layer_filter;
  251. // Start profiling this program
  252. JPH_PROFILE_START("Main");
  253. // Trace header
  254. Trace("Motion Quality, Thread Count, Steps / Second, Hash");
  255. // Repeat test
  256. for (int r = 0; r < repeat; ++r)
  257. {
  258. // Iterate motion qualities
  259. for (uint mq = 0; mq < 2; ++mq)
  260. {
  261. // Skip quality if another was specified
  262. if (specified_quality != -1 && mq != (uint)specified_quality)
  263. continue;
  264. // Determine motion quality
  265. EMotionQuality motion_quality = mq == 0? EMotionQuality::Discrete : EMotionQuality::LinearCast;
  266. String motion_quality_str = mq == 0? "Discrete" : "LinearCast";
  267. // Determine which thread counts to test
  268. Array<uint> thread_permutations;
  269. if (specified_threads > 0)
  270. thread_permutations.push_back((uint)specified_threads - 1);
  271. else
  272. for (uint num_threads = 0; num_threads < thread::hardware_concurrency(); ++num_threads)
  273. thread_permutations.push_back(num_threads);
  274. // Test thread permutations
  275. for (uint num_threads : thread_permutations)
  276. {
  277. // Create job system with desired number of threads
  278. JobSystemThreadPool job_system(cMaxPhysicsJobs, cMaxPhysicsBarriers, num_threads);
  279. // Create physics system
  280. PhysicsSystem physics_system;
  281. physics_system.Init(scene->GetMaxBodies(), 0, scene->GetMaxBodyPairs(), scene->GetMaxContactConstraints(), broad_phase_layer_interface, object_vs_broadphase_layer_filter, object_vs_object_layer_filter);
  282. // Start test scene
  283. scene->StartTest(physics_system, motion_quality);
  284. // Disable sleeping if requested
  285. if (disable_sleep)
  286. {
  287. const BodyLockInterface &bli = physics_system.GetBodyLockInterfaceNoLock();
  288. BodyIDVector body_ids;
  289. physics_system.GetBodies(body_ids);
  290. for (BodyID id : body_ids)
  291. {
  292. BodyLockWrite lock(bli, id);
  293. if (lock.Succeeded())
  294. {
  295. Body &body = lock.GetBody();
  296. if (!body.IsStatic())
  297. body.SetAllowSleeping(false);
  298. }
  299. }
  300. }
  301. // Optimize the broadphase to prevent an expensive first frame
  302. physics_system.OptimizeBroadPhase();
  303. // A tag used to identify the test
  304. String tag = ToLower(motion_quality_str) + "_th" + ConvertToString(num_threads + 1);
  305. #ifdef JPH_DEBUG_RENDERER
  306. // Open renderer output
  307. ofstream renderer_file;
  308. if (enable_debug_renderer)
  309. renderer_file.open(("performance_test_" + tag + ".jor").c_str(), ofstream::out | ofstream::binary | ofstream::trunc);
  310. StreamOutWrapper renderer_stream(renderer_file);
  311. DebugRendererRecorder renderer(renderer_stream);
  312. #endif // JPH_DEBUG_RENDERER
  313. // Open per frame timing output
  314. ofstream per_frame_file;
  315. if (enable_per_frame_recording)
  316. {
  317. per_frame_file.open(("per_frame_" + tag + ".csv").c_str(), ofstream::out | ofstream::trunc);
  318. per_frame_file << "Frame, Time (ms)" << endl;
  319. }
  320. ofstream record_state_file;
  321. ifstream validate_state_file;
  322. if (record_state)
  323. record_state_file.open(("state_" + ToLower(motion_quality_str) + ".bin").c_str(), ofstream::out | ofstream::binary | ofstream::trunc);
  324. else if (validate_state)
  325. validate_state_file.open(("state_" + ToLower(motion_quality_str) + ".bin").c_str(), ifstream::in | ifstream::binary);
  326. chrono::nanoseconds total_duration(0);
  327. // Step the world for a fixed amount of iterations
  328. for (uint iterations = 0; iterations < max_iterations; ++iterations)
  329. {
  330. JPH_PROFILE_NEXTFRAME();
  331. JPH_DET_LOG("Iteration: " << iterations);
  332. // Start measuring
  333. chrono::high_resolution_clock::time_point clock_start = chrono::high_resolution_clock::now();
  334. // Update the test
  335. scene->UpdateTest(physics_system, temp_allocator, cDeltaTime);
  336. // Do a physics step
  337. physics_system.Update(cDeltaTime, 1, &temp_allocator, &job_system);
  338. // Stop measuring
  339. chrono::high_resolution_clock::time_point clock_end = chrono::high_resolution_clock::now();
  340. chrono::nanoseconds duration = chrono::duration_cast<chrono::nanoseconds>(clock_end - clock_start);
  341. total_duration += duration;
  342. #ifdef JPH_DEBUG_RENDERER
  343. if (enable_debug_renderer)
  344. {
  345. // Draw the state of the world
  346. BodyManager::DrawSettings settings;
  347. physics_system.DrawBodies(settings, &renderer);
  348. // Mark end of frame
  349. renderer.EndFrame();
  350. }
  351. #endif // JPH_DEBUG_RENDERER
  352. // Record time taken this iteration
  353. if (enable_per_frame_recording)
  354. per_frame_file << iterations << ", " << (1.0e-6 * duration.count()) << endl;
  355. // Dump profile information every 100 iterations
  356. if (enable_profiler && iterations % 100 == 0)
  357. {
  358. JPH_PROFILE_DUMP(tag + "_it" + ConvertToString(iterations));
  359. }
  360. if (record_state)
  361. {
  362. // Record state
  363. StateRecorderImpl recorder;
  364. physics_system.SaveState(recorder);
  365. // Write to file
  366. string data = recorder.GetData();
  367. uint32 size = uint32(data.size());
  368. record_state_file.write((char *)&size, sizeof(size));
  369. record_state_file.write(data.data(), size);
  370. }
  371. else if (validate_state)
  372. {
  373. // Read state
  374. uint32 size = 0;
  375. validate_state_file.read((char *)&size, sizeof(size));
  376. string data;
  377. data.resize(size);
  378. validate_state_file.read(data.data(), size);
  379. // Copy to validator
  380. StateRecorderImpl validator;
  381. validator.WriteBytes(data.data(), size);
  382. // Validate state
  383. validator.SetValidating(true);
  384. physics_system.RestoreState(validator);
  385. }
  386. #ifdef JPH_ENABLE_DETERMINISM_LOG
  387. const BodyLockInterface &bli = physics_system.GetBodyLockInterfaceNoLock();
  388. BodyIDVector body_ids;
  389. physics_system.GetBodies(body_ids);
  390. for (BodyID id : body_ids)
  391. {
  392. BodyLockRead lock(bli, id);
  393. const Body &body = lock.GetBody();
  394. if (!body.IsStatic())
  395. JPH_DET_LOG(id << ": p: " << body.GetPosition() << " r: " << body.GetRotation() << " v: " << body.GetLinearVelocity() << " w: " << body.GetAngularVelocity());
  396. }
  397. #endif // JPH_ENABLE_DETERMINISM_LOG
  398. }
  399. // Calculate hash of all positions and rotations of the bodies
  400. uint64 hash = HashBytes(nullptr, 0); // Ensure we start with the proper seed
  401. BodyInterface &bi = physics_system.GetBodyInterfaceNoLock();
  402. BodyIDVector body_ids;
  403. physics_system.GetBodies(body_ids);
  404. for (BodyID id : body_ids)
  405. {
  406. RVec3 pos = bi.GetPosition(id);
  407. hash = HashBytes(&pos, 3 * sizeof(Real), hash);
  408. Quat rot = bi.GetRotation(id);
  409. hash = HashBytes(&rot, sizeof(Quat), hash);
  410. }
  411. // Let the scene hash its own state
  412. scene->UpdateHash(hash);
  413. // Convert hash to string
  414. stringstream hash_stream;
  415. hash_stream << "0x" << hex << hash << dec;
  416. string hash_str = hash_stream.str();
  417. // Stop test scene
  418. scene->StopTest(physics_system);
  419. // Trace stat line
  420. Trace("%s, %d, %f, %s", motion_quality_str.c_str(), num_threads + 1, double(max_iterations) / (1.0e-9 * total_duration.count()), hash_str.c_str());
  421. // Check hash code
  422. if (validate_hash != nullptr && hash_str != validate_hash)
  423. {
  424. Trace("Fail hash validation. Was: %s, expected: %s", hash_str.c_str(), validate_hash);
  425. return 1;
  426. }
  427. }
  428. }
  429. }
  430. #ifdef JPH_TRACK_NARROWPHASE_STATS
  431. NarrowPhaseStat::sReportStats();
  432. #endif // JPH_TRACK_NARROWPHASE_STATS
  433. // Unregisters all types with the factory and cleans up the default material
  434. UnregisterTypes();
  435. // Destroy the factory
  436. delete Factory::sInstance;
  437. Factory::sInstance = nullptr;
  438. // End profiling this program
  439. JPH_PROFILE_END();
  440. return 0;
  441. }
  442. #ifdef JPH_PLATFORM_ANDROID
  443. // Main entry point for android
  444. void android_main(struct android_app *ioApp)
  445. {
  446. // Run the regular main function
  447. const char *args[] = { "Unused", "-s=ConvexVsMesh", "-t=max" };
  448. main(size(args), (char **)args);
  449. }
  450. #endif // JPH_PLATFORM_ANDROID