Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Godot Physics collisions and solver processed on threads #48221

Merged
merged 1 commit into from
May 4, 2021

Conversation

pouleyKetchoupp
Copy link
Contributor

@pouleyKetchoupp pouleyKetchoupp commented Apr 27, 2021

Use ThreadWorkPool to process physics step tasks in multiple threads.
Collisions are all processed in parallel and solving impulses is processed in parallel for rigid body islands.

Additional changes:

  • Proper islands for soft bodies linked to active bodies
  • All moving areas are on separate islands (can be parallelized)
  • Fix inconsistencies with body islands (Kinematic bodies could link bodies together or not depending on the processing order)
  • Completely prevent static bodies to be active (it could cause islands to be wrongly created and cause dangerous multi-threading operations as well as inconsistencies in created islands)
  • Apply impulses only on dynamic bodies to avoid unsafe multi-threaded operations (static bodies can be on multiple islands)
  • Removed inverted iterations when populating body islands, it's now faster in regular order (maybe after fixing inconsistencies) - see discussion Godot Physics solver optimization #47846 (comment) (CC @AndreaCatania)

Test configuration

CPU: AMD Ryzen 9 4900HS with Radeon Graphics, 3000 Mhz, 8 Core(s), 16 Logical Processor(s)
RAM: 16.0 GB
Display: GeForce RTX 2060 with Max-Q Design/PCIe/SSE2

Test scenes

2D - Simple contacts (1 island - 500 rigid bodies)
https://github.com/godotengine/godot-demo-projects/tree/master/2d/physics_tests/tests/performance/test_perf_contacts.tscn
~15% improvement (physics ticks)

2D - Advanced contacts (multiple islands - 9 x 300 rigid bodies)
https://github.com/godotengine/godot-demo-projects/tree/master/2d/physics_tests/tests/performance/test_perf_contact_islands.tscn
~25% improvement (physics ticks)

3D - Simple contacts (1 island - 500 rigid bodies)
https://github.com/godotengine/godot-demo-projects/tree/master/3d/physics_tests/tests/performance/test_perf_contacts.tscn
~30% improvement (physics ticks)

3D - Advanced contacts (multiple islands - 16 x 200 rigid bodies)
https://github.com/godotengine/godot-demo-projects/tree/master/3d/physics_tests/tests/performance/test_perf_contact_islands.tscn
~65% improvement (physics ticks)

These results are measured with a 3.x port of this PR.

The lower results in 2D can be explained by a bottleneck in the broadphase, which takes more than half of the physics tick time. It would be worth checking if replacing the Hash Grid with a 2D dynamic BVH would help with that.
Edit: This is done on the 3.x branch with #48314.

@pouleyKetchoupp pouleyKetchoupp added this to the 4.0 milestone Apr 27, 2021
@pouleyKetchoupp pouleyKetchoupp requested a review from a team April 27, 2021 00:55
@pouleyKetchoupp pouleyKetchoupp force-pushed the solver-multithreaded branch 3 times, most recently from 67ffc42 to 71ab2c7 Compare April 27, 2021 01:22
Use ThreadWorkPool to process physics step tasks in multiple threads. Collisions are all processed in parallel and solving impulses is
processed in parallel for rigid body islands.

Additional changes:
- Proper islands for soft bodies linked to active bodies
- All moving areas are on separate islands (can be parallelized)
- Fix inconsistencies with body islands (Kinematic bodies could link
bodies together or not depending on the processing order)
- Completely prevent static bodies to be active (it could cause islands
to be wrongly created and cause dangerous multi-threading operations as
well as inconsistencies in created islands)
- Apply impulses only on dynamic bodies to avoid unsafe multi-threaded
operations (static bodies can be on multiple islands)
- Removed inverted iterations when populating body islands, it's now
faster in regular order (maybe after fixing inconsistencies)
@akien-mga akien-mga merged commit 508fbf4 into godotengine:master May 4, 2021
@akien-mga
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants