RigsOfRods / rigs-of-rods

Main development repository for Rigs of Rods soft-body physics simulator
https://www.rigsofrods.org
GNU General Public License v3.0
1.01k stars 177 forks source link

Future: proper multithreaded architecture #816

Open ohlidalp opened 8 years ago

ohlidalp commented 8 years ago

This is a tech proposal document.

At the moment, RoR's multithreading is flawed. Read description of milestone future-proper-mt for details and general design goals.

General design goals (recap)

StateAreaCurrentDesired
OK Collisions - static objects On map load, a physics-only collision triangles are created ~
TODO Collisions - heightmap Direct ray-queries to OGRE terrainOptimized by porting height-lookup code from OGRE. TODO: fix support for multi-page terrains! Separate collision-only heightmap (+bump map?)
TODO Sleeping vehicle - engine Updated on main thread Updated on sim thread
OK Active vehicle - engine Updated on sim thread ~
TODO Active vehicle - (auto)gearbox Updated on main thread (sync?!) Updated on sim thread
TODO Active vehicle - brakes Updated on main thread (sync?!) Updated on sim thread
TODO Active vehicle - TC/ALB Updated on main thread (sync?!) Updated on sim thread
TODO GFX - Flares/Beacons Updated on sim thread Update in main thread
TODO GUI - Vehicle HUD Updated on maintrhead, direct data queries to engine/rig Update on mainthread, read buffered data

To research

Act1: At the beginning, I tried to replace vehicle engine code (the BeamEngine part, updated on sim-thread) with Lua script. This turned out to be impossible because existing implementation works closely together with brakes/gearbox/etc on main thread, without syncing.

Act2: I tried to create properly threaded powertrain: all simulation on BeamFactory-thread, all GUI updates and input buffering on main-thread. This project got pretty far, the code is here. However, as I progressed, I realized it doesn't make sense to refactor just the powertrain when there are many other elements (hooks, ties, triggers, etc...) suffering from exactly the same issue.

Act3: This project. I want to apply the knowledge from Act2 to entire simulation code.

ohlidalp commented 5 years ago

Related workarounds and rants: https://github.com/RigsOfRods/rigs-of-rods/pull/1752/files/080ed5e49774c03337b63c59303be150b520773f#diff-11319c6cae8caeca2be4dde205bd243b

ulteq commented 5 years ago

Triggers (IIRC main thread, should be sim)

Triggers are definitely updated on the sim thread: https://github.com/RigsOfRods/rigs-of-rods/blob/master/source/main/physics/BeamForcesEuler.cpp#L1217

Hooks - locking (IIRC main thread, should be sim)

Hooks as well: https://github.com/RigsOfRods/rigs-of-rods/blob/master/source/main/physics/BeamForcesEuler.cpp#L1135

This turned out to be impossible because existing implementation works closely together with brakes/gearbox/etc on main thread, without syncing.

The engine updates in the main thread happen while the sim thread is paused.

The sim thread is paused here: https://github.com/RigsOfRods/rigs-of-rods/blob/master/source/main/gameplay/RoRFrameListener.cpp#L1478

And the engine updates happen here (inside of the input event handling): https://github.com/RigsOfRods/rigs-of-rods/blob/master/source/main/gameplay/RoRFrameListener.cpp#L1718