PerformanceTest.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. // Jolt includes
  4. #include <Jolt.h>
  5. #include <RegisterTypes.h>
  6. #include <Core/TempAllocator.h>
  7. #include <Core/JobSystemThreadPool.h>
  8. #include <Physics/PhysicsSettings.h>
  9. #include <Physics/PhysicsSystem.h>
  10. #include <Physics/Ragdoll/Ragdoll.h>
  11. #include <Physics/PhysicsScene.h>
  12. #include <Physics/Collision/CastResult.h>
  13. #include <Physics/Collision/RayCast.h>
  14. #ifdef JPH_DEBUG_RENDERER
  15. #include <Renderer/DebugRendererRecorder.h>
  16. #include <Core/StreamWrapper.h>
  17. #endif // JPH_DEBUG_RENDERER
  18. // STL includes
  19. #include <iostream>
  20. #include <thread>
  21. #include <chrono>
  22. using namespace JPH;
  23. using namespace std;
  24. namespace Layers
  25. {
  26. static constexpr uint8 NON_MOVING = 0;
  27. static constexpr uint8 MOVING = 1;
  28. static constexpr uint8 NUM_LAYERS = 2;
  29. };
  30. bool MyObjectCanCollide(ObjectLayer inObject1, ObjectLayer inObject2)
  31. {
  32. switch (inObject1)
  33. {
  34. case Layers::NON_MOVING:
  35. return inObject2 == Layers::MOVING; // Non moving only collides with moving
  36. case Layers::MOVING:
  37. return true; // Moving collides with everything
  38. default:
  39. JPH_ASSERT(false);
  40. return false;
  41. }
  42. };
  43. namespace BroadPhaseLayers
  44. {
  45. static constexpr BroadPhaseLayer NON_MOVING(0);
  46. static constexpr BroadPhaseLayer MOVING(1);
  47. };
  48. bool MyBroadPhaseCanCollide(ObjectLayer inLayer1, BroadPhaseLayer inLayer2)
  49. {
  50. switch (inLayer1)
  51. {
  52. case Layers::NON_MOVING:
  53. return inLayer2 == BroadPhaseLayers::MOVING;
  54. case Layers::MOVING:
  55. return true;
  56. default:
  57. JPH_ASSERT(false);
  58. return false;
  59. }
  60. }
  61. // Test configuration
  62. const float cHorizontalSeparation = 4.0f;
  63. const float cVerticalSeparation = 0.6f;
  64. #ifdef _DEBUG
  65. const int cPileSize = 5;
  66. const int cNumRows = 2;
  67. const int cNumCols = 2;
  68. #else
  69. const int cPileSize = 10;
  70. const int cNumRows = 4;
  71. const int cNumCols = 4;
  72. #endif
  73. const float cDeltaTime = 1.0f / 60.0f;
  74. // Program entry point
  75. int main(int argc, char** argv)
  76. {
  77. // Parse command line parameters
  78. int specified_quality = -1;
  79. int specified_threads = -1;
  80. bool enable_profiler = false;
  81. bool enable_debug_renderer = false;
  82. for (int arg = 1; arg < argc; ++arg)
  83. {
  84. if (strncmp(argv[arg], "-q=", 3) == 0)
  85. {
  86. // Parse quality
  87. if (strcmp(argv[arg] + 3, "discrete") == 0)
  88. {
  89. specified_quality = 0;
  90. }
  91. else if (strcmp(argv[arg] + 3, "linearcast") == 0)
  92. {
  93. specified_quality = 1;
  94. }
  95. else
  96. {
  97. cerr << "Invalid quality" << endl;
  98. return 1;
  99. }
  100. }
  101. else if (strncmp(argv[arg], "-t=", 3) == 0)
  102. {
  103. // Parse threads
  104. specified_threads = atoi(argv[arg] + 3);
  105. }
  106. else if (strcmp(argv[arg], "-p") == 0)
  107. {
  108. enable_profiler = true;
  109. }
  110. else if (strcmp(argv[arg], "-r") == 0)
  111. {
  112. enable_debug_renderer = true;
  113. }
  114. else if (strcmp(argv[arg], "-h") == 0)
  115. {
  116. // Print usage
  117. cerr << "Usage: PerformanceTest [-q=<quality>] [-t=<threads>] [-p] [-r]" << endl
  118. << "-q: Test only with specified quality (discrete, linearcast)" << endl
  119. << "-t: Test only with N threads" << endl
  120. << "-p: Write out profiles" << endl
  121. << "-r: Record debug renderer output for JoltViewer" << endl;
  122. return 0;
  123. }
  124. }
  125. // Register all Jolt physics types
  126. RegisterTypes();
  127. // Create temp allocator
  128. TempAllocatorImpl temp_allocator(10 * 1024 * 1024);
  129. // Load ragdoll
  130. Ref<RagdollSettings> ragdoll_settings;
  131. if (!ObjectStreamIn::sReadObject("Assets/Human.tof", ragdoll_settings))
  132. {
  133. cerr << "Unable to load ragdoll" << endl;
  134. return 1;
  135. }
  136. for (BodyCreationSettings &body : ragdoll_settings->mParts)
  137. body.mObjectLayer = Layers::MOVING;
  138. // Init ragdoll
  139. ragdoll_settings->GetSkeleton()->CalculateParentJointIndices();
  140. ragdoll_settings->Stabilize();
  141. ragdoll_settings->CalculateBodyIndexToConstraintIndex();
  142. ragdoll_settings->CalculateConstraintIndexToBodyIdxPair();
  143. // Load animation
  144. Ref<SkeletalAnimation> animation;
  145. if (!ObjectStreamIn::sReadObject("Assets/Human/dead_pose1.tof", animation))
  146. {
  147. cerr << "Unable to load animation" << endl;
  148. return 1;
  149. }
  150. // Sample pose
  151. SkeletonPose pose;
  152. pose.SetSkeleton(ragdoll_settings->GetSkeleton());
  153. animation->Sample(0.0f, pose);
  154. // Read the scene
  155. Ref<PhysicsScene> scene;
  156. if (!ObjectStreamIn::sReadObject("Assets/terrain2.bof", scene))
  157. {
  158. cerr << "Unable to load terrain" << endl;
  159. return 1;
  160. }
  161. for (BodyCreationSettings &body : scene->GetBodies())
  162. {
  163. body.mObjectLayer = Layers::NON_MOVING;
  164. body.mAllowSleeping = false;
  165. }
  166. scene->FixInvalidScales();
  167. // Create mapping table from object layer to broadphase layer
  168. ObjectToBroadPhaseLayer object_to_broadphase;
  169. object_to_broadphase.resize(Layers::NUM_LAYERS);
  170. object_to_broadphase[Layers::NON_MOVING] = BroadPhaseLayers::NON_MOVING;
  171. object_to_broadphase[Layers::MOVING] = BroadPhaseLayers::MOVING;
  172. // Start profiling this thread
  173. JPH_PROFILE_THREAD_START("Main");
  174. // Trace header
  175. cout << "Motion Quality, Thread Count, Steps / Second, Hash" << endl;
  176. constexpr uint cMaxIterations = 500;
  177. // Iterate motion qualities
  178. for (uint mq = 0; mq < 2; ++mq)
  179. {
  180. // Skip quality if another was specified
  181. if (specified_quality != -1 && mq != (uint)specified_quality)
  182. continue;
  183. // Determine motion quality
  184. EMotionQuality motion_quality = mq == 0? EMotionQuality::Discrete : EMotionQuality::LinearCast;
  185. string motion_quality_str = mq == 0? "Discrete" : "LinearCast";
  186. // Set motion quality on ragdoll
  187. for (BodyCreationSettings &body : ragdoll_settings->mParts)
  188. body.mMotionQuality = motion_quality;
  189. // Determine which thread counts to test
  190. vector<uint> thread_permutations;
  191. if (specified_threads > 0)
  192. thread_permutations.push_back((uint)specified_threads - 1);
  193. else
  194. for (uint num_threads = 0; num_threads < thread::hardware_concurrency(); ++num_threads)
  195. thread_permutations.push_back(num_threads);
  196. // Test thread permutations
  197. for (uint num_threads : thread_permutations)
  198. {
  199. // Create job system with desired number of threads
  200. JobSystemThreadPool job_system(cMaxPhysicsJobs, cMaxPhysicsBarriers, num_threads);
  201. // Create physics system
  202. PhysicsSystem physics_system;
  203. physics_system.Init(10240, 0, 65536, 10240, object_to_broadphase, MyBroadPhaseCanCollide, MyObjectCanCollide);
  204. // Add background geometry
  205. scene->CreateBodies(&physics_system);
  206. // Create ragdoll piles
  207. vector<Ref<Ragdoll>> ragdolls;
  208. mt19937 random;
  209. uniform_real_distribution<float> angle(0.0f, JPH_PI);
  210. CollisionGroup::GroupID group_id = 1;
  211. for (int row = 0; row < cNumRows; ++row)
  212. for (int col = 0; col < cNumCols; ++col)
  213. {
  214. // Determine start location of ray
  215. Vec3 start = Vec3(cHorizontalSeparation * (col - (cNumCols - 1) / 2.0f), 100, cHorizontalSeparation * (row - (cNumRows - 1) / 2.0f));
  216. // Cast ray down to terrain
  217. RayCastResult hit;
  218. Vec3 ray_direction(0, -200, 0);
  219. RayCast ray { start, ray_direction };
  220. if (physics_system.GetNarrowPhaseQuery().CastRay(ray, hit, SpecifiedBroadPhaseLayerFilter(BroadPhaseLayers::NON_MOVING), SpecifiedObjectLayerFilter(Layers::NON_MOVING)))
  221. start = start + hit.mFraction * ray_direction;
  222. for (int i = 0; i < cPileSize; ++i)
  223. {
  224. // Create ragdoll
  225. Ref<Ragdoll> ragdoll = ragdoll_settings->CreateRagdoll(group_id++, nullptr, &physics_system);
  226. // Override root
  227. SkeletonPose pose_copy = pose;
  228. SkeletonPose::JointState &root = pose_copy.GetJoint(0);
  229. root.mTranslation = start + Vec3(0, cVerticalSeparation * (i + 1), 0);
  230. root.mRotation = Quat::sRotation(Vec3::sAxisY(), angle(random)) * root.mRotation;
  231. pose_copy.CalculateJointMatrices();
  232. // Drive to pose
  233. ragdoll->SetPose(pose_copy);
  234. ragdoll->DriveToPoseUsingMotors(pose_copy);
  235. ragdoll->AddToPhysicsSystem(EActivation::Activate);
  236. // Keep reference
  237. ragdolls.push_back(ragdoll);
  238. }
  239. }
  240. #ifdef JPH_DEBUG_RENDERER
  241. // Open output
  242. ofstream renderer_file;
  243. if (enable_debug_renderer)
  244. renderer_file.open(("performance_test_" + ToLower(motion_quality_str) + "_th" + ConvertToString(num_threads + 1) + ".JoltRecording").c_str(), ofstream::out | ofstream::binary | ofstream::trunc);
  245. StreamOutWrapper renderer_stream(renderer_file);
  246. DebugRendererRecorder renderer(renderer_stream);
  247. #endif // JPH_DEBUG_RENDERER
  248. chrono::nanoseconds total_duration(0);
  249. // Step the world for a fixed amount of iterations
  250. for (uint iterations = 0; iterations < cMaxIterations; ++iterations)
  251. {
  252. JPH_PROFILE_NEXTFRAME();
  253. // Start measuring
  254. chrono::high_resolution_clock::time_point clock_start = chrono::high_resolution_clock::now();
  255. // Do a physics step
  256. physics_system.Update(cDeltaTime, 1, 1, &temp_allocator, &job_system);
  257. // Stop measuring
  258. chrono::high_resolution_clock::time_point clock_end = chrono::high_resolution_clock::now();
  259. total_duration += chrono::duration_cast<chrono::nanoseconds>(clock_end - clock_start);
  260. #ifdef JPH_DEBUG_RENDERER
  261. if (enable_debug_renderer)
  262. {
  263. // Draw the state of the world
  264. BodyManager::DrawSettings settings;
  265. physics_system.DrawBodies(settings, &renderer);
  266. // Mark end of frame
  267. renderer.EndFrame();
  268. }
  269. #endif // JPH_DEBUG_RENDERER
  270. // Dump profile information every 100 iterations
  271. if (enable_profiler && iterations % 100 == 0)
  272. {
  273. JPH_PROFILE_DUMP(ToLower(motion_quality_str) + "_th" + ConvertToString(num_threads + 1) + "_it" + ConvertToString(iterations));
  274. }
  275. }
  276. // Calculate hash of all positions and rotations of the bodies
  277. size_t hash = 0;
  278. BodyInterface &bi = physics_system.GetBodyInterfaceNoLock();
  279. for (Ragdoll *ragdoll : ragdolls)
  280. for (BodyID id : ragdoll->GetBodyIDs())
  281. {
  282. Vec3 pos = bi.GetPosition(id);
  283. Quat rot = bi.GetRotation(id);
  284. hash_combine(hash, pos.GetX(), pos.GetY(), pos.GetZ(), rot.GetX(), rot.GetY(), rot.GetZ(), rot.GetW());
  285. }
  286. // Remove ragdolls
  287. for (Ragdoll *ragdoll : ragdolls)
  288. ragdoll->RemoveFromPhysicsSystem();
  289. // Trace stat line
  290. cout << motion_quality_str << ", " << num_threads + 1 << ", " << double(cMaxIterations) / (1.0e-9 * total_duration.count()) << ", " << hash << endl;
  291. }
  292. }
  293. // End profiling this thread
  294. JPH_PROFILE_THREAD_END();
  295. return 0;
  296. }