aevyrie / bevy_framepace

Framepacing and framelimiting for Bevy
https://crates.io/crates/bevy_framepace
Apache License 2.0
245 stars 23 forks source link

PID controller for sleep error #36

Open UkoeHB opened 1 year ago

UkoeHB commented 1 year ago

Attached is a patch that adds a PID controller. I found that the derivative factor adds stutter, but using proportional and integral factors improves the 'sleep error' (difference between target sleep time and actual sleep time) dramatically. The PID design is fundamentally different from the current controller, which is setting the sleep time equal to the previous error, whereas the PID controller tries to reduce that error to zero (which makes more sense to me).

I hacked the bevy render function to record frame presentation delays and incorporated those into the framepace app. Doing so resulted in extremely low latencies with vsync enabled (even better than the current code, afaict). It would require a pretty critical PR to update the core bevy render function in order to get timing info on the frame presentation step.

add_PID_controller.patch

aevyrie commented 1 year ago

Link to full thread on the Bevy discord server: https://discord.com/channels/691052431525675048/1088685202752155719/1095468054747561985