Skip to content

Implement accumulator pattern for precise frame rate limiting #16484

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

Merged

Conversation

ertugrulcetin
Copy link
Contributor

This commit replaces the simple frame time threshold check with an accumulator-based approach for more accurate frame rate control. Changes include:

  • Remove the "+1" adjustment in maxFPS calculation that caused target FPS to drift
  • Add a time accumulator to track elapsed time between frames
  • Only render when sufficient time has accumulated
  • Prevent accumulator from growing too large during slow frames

Fixes issue where setting maxFPS to 60 resulted in actual FPS of 48.

This commit replaces the simple frame time threshold check with an accumulator-based
approach for more accurate frame rate control. Changes include:

- Remove the "+1" adjustment in maxFPS calculation that caused target FPS to drift
- Add a time accumulator to track elapsed time between frames
- Only render when sufficient time has accumulated
- Prevent accumulator from growing too large during slow frames

Fixes issue where setting maxFPS to 60 resulted in actual FPS of 48.
@bjsplat
Copy link
Collaborator

bjsplat commented Apr 12, 2025

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@bjsplat
Copy link
Collaborator

bjsplat commented Apr 12, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Apr 12, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Apr 12, 2025

@ertugrulcetin
Copy link
Contributor Author

Works as expected:
Screenshot 2025-04-12 at 13 43 44

@ertugrulcetin
Copy link
Contributor Author

ertugrulcetin commented Apr 14, 2025

@deltakosh @Popov72 what do you guys think about this update?

Copy link
Member

@RaananW RaananW left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
However, I would appreciate someone that has the context regarding the vbl sync (value + 1) provide a bit more explanation whether or not it is still needed here.

@RaananW RaananW marked this pull request as draft April 14, 2025 17:05
@ertugrulcetin
Copy link
Contributor Author

@deltakosh @Popov72 can we merge this?

@deltakosh deltakosh marked this pull request as ready for review April 22, 2025 20:55
@deltakosh deltakosh enabled auto-merge (squash) April 22, 2025 20:56
@deltakosh deltakosh merged commit a6010b7 into BabylonJS:master Apr 22, 2025
17 checks passed
@deltakosh
Copy link
Contributor

Thanks! I did not see your ping among the million I got :D

@ertugrulcetin ertugrulcetin deleted the fix/accurate-frame-rate-limiting branch April 23, 2025 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants