bevy_framepace icon indicating copy to clipboard operation
bevy_framepace copied to clipboard

PID controller for sleep error

Open UkoeHB opened this issue 2 years ago • 1 comments

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).

  • The PID controller fixes the 3-5 FPS frame loss that the current design suffers from.
  • The PID controller does NOT appear compatible with vsync when 'delay waiting for a frame to present' is not incorporated. I noticed worse latency when vsync is enabled. It seems to be random luck that the current design works well with vsync.

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

UkoeHB avatar Apr 18 '23 18:04 UkoeHB

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

aevyrie avatar Apr 18 '23 18:04 aevyrie