PerformanceTest.cpp 12 KB

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