wip: don't wait for physics data in render thread
This commit is contained in:
27
src/main.cpp
27
src/main.cpp
@ -55,23 +55,24 @@ auto main(int argc, char *argv[]) -> int {
|
|||||||
state.UpdateGraph(); // Add state added after user input
|
state.UpdateGraph(); // Add state added after user input
|
||||||
|
|
||||||
// Read positions from physics thread
|
// Read positions from physics thread
|
||||||
|
FrameMarkStart("MainThreadConsumeLock");
|
||||||
{
|
{
|
||||||
std::unique_lock<LockableBase(std::mutex)> lock(physics.state.data_mtx);
|
std::unique_lock<LockableBase(std::mutex)> lock(physics.state.data_mtx);
|
||||||
physics.state.data_ready_cnd.wait(lock, [&] {
|
|
||||||
return physics.state.data_ready || !physics.state.running.load();
|
// Only copy data if any has been produced
|
||||||
});
|
if (physics.state.data_ready) {
|
||||||
if (!physics.state.running.load()) {
|
masses = physics.state.masses;
|
||||||
// Running turned false while we were waiting for the condition
|
springs = physics.state.springs;
|
||||||
break;
|
|
||||||
|
physics.state.data_ready = false;
|
||||||
|
physics.state.data_consumed = true;
|
||||||
|
|
||||||
|
lock.unlock();
|
||||||
|
// Notify the physics thread that data has been consumed
|
||||||
|
physics.state.data_consumed_cnd.notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
masses = physics.state.masses;
|
|
||||||
springs = physics.state.springs;
|
|
||||||
|
|
||||||
physics.state.data_ready = false;
|
|
||||||
physics.state.data_consumed = true;
|
|
||||||
}
|
}
|
||||||
physics.state.data_consumed_cnd.notify_all();
|
FrameMarkEnd("MainThreadConsumeLock");
|
||||||
|
|
||||||
// Update the camera after the physics, so target lock is smooth
|
// Update the camera after the physics, so target lock is smooth
|
||||||
std::size_t current_index = state.CurrentMassIndex();
|
std::size_t current_index = state.CurrentMassIndex();
|
||||||
|
|||||||
@ -201,12 +201,14 @@ auto ThreadedPhysics::PhysicsThread(ThreadedPhysics::PhysicsState &state)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Physics update
|
// Physics update
|
||||||
|
// TODO: Respect thread-local deltatime + calculate UPS
|
||||||
mass_springs.ClearForces();
|
mass_springs.ClearForces();
|
||||||
mass_springs.CalculateSpringForces();
|
mass_springs.CalculateSpringForces();
|
||||||
mass_springs.CalculateRepulsionForces();
|
mass_springs.CalculateRepulsionForces();
|
||||||
mass_springs.VerletUpdate(TIMESTEP * SIM_SPEED);
|
mass_springs.VerletUpdate(TIMESTEP * SIM_SPEED);
|
||||||
|
|
||||||
// Publish the positions for the renderer (copy)
|
// Publish the positions for the renderer (copy)
|
||||||
|
FrameMarkStart("PhysicsThreadProduceLock");
|
||||||
{
|
{
|
||||||
std::unique_lock<LockableBase(std::mutex)> lock(state.data_mtx);
|
std::unique_lock<LockableBase(std::mutex)> lock(state.data_mtx);
|
||||||
state.data_consumed_cnd.wait(
|
state.data_consumed_cnd.wait(
|
||||||
@ -231,9 +233,10 @@ auto ThreadedPhysics::PhysicsThread(ThreadedPhysics::PhysicsState &state)
|
|||||||
state.data_ready = true;
|
state.data_ready = true;
|
||||||
state.data_consumed = false;
|
state.data_consumed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify the rendering thread that new data is available
|
// Notify the rendering thread that new data is available
|
||||||
state.data_ready_cnd.notify_all();
|
state.data_ready_cnd.notify_all();
|
||||||
|
FrameMarkEnd("PhysicsThreadProduceLock");
|
||||||
|
|
||||||
FrameMarkEnd("PhysicsThread");
|
FrameMarkEnd("PhysicsThread");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user