r/howdidtheycodeit • u/bluegreenjelly • Jul 28 '22
Question How Does FPS Limiting Work
It just dawned on me I have no idea how limiting FPS in an engine works.
The piece I'm most uncertain about is what to do with the time after you've hit the cap. For example, if cap at 60fps and we get through everything in 0.75 seconds what happens to the remaining 0.25? If the engine sits idle for that time until the next second, won't there be a notable jitter in either input handling or rendering as we wait for the 0.25 to expire? If that tracks and I haven't missed something, or am otherwise completely off, it seems to suggest there is some method of sequencing the 60 frames across the full second but I don't suspect you could possibly know how long a frame will take to calculate before it is going to be dealt with.
I hope the question I am asking in that paragraph is clear. I also think there is a whole lot about rendering that I'm not aware of so if that is the case I'd love to be taught.
15
u/nvec ProProgrammer Jul 28 '22 edited Jul 28 '22
I think you may be misunderstanding how the maths works.
When you cap at 60fps you're not asking it to render 60 frames as quickly as possible every second and then wait for the remainder, instead you're asking it to wait 1/60s after rendering a frame before you display the next one.
I believe (but aren't sure, not played with this bit of the render pipeline for frame limits) that what actually happens normally is that it actually has two display buffers, called Double Buffering. Each of these buffers is basically a screenshot which the GPU could choose to display at any time.
As soon as a frame is displayed a new frame is rendered as quickly as possible to the unused buffer (known as the back buffer). Now when the 1/60s time has elapsed it switches to the back buffer and allowing the next frame to be rendered to the now unused buffer.
Two buffers, one displayed, one being rendered to.
The advantages are that if it could run (for example) 240fps at maximum then most monitors couldn't display that number of frames and so we'd be wasting a lot of rendering. If we're capped at 60fps then the GPU would only need to render 60 useful frames instead of 240 mostly wasted every second which either allows us to use the GPU for other things such as physics (or other computation), or just reduce the amount of work the GPU has to do and so reduce power and fan noise.
Edit: A lot will be said about the delay this causes as the image will be delayed up to 1/60s instead of the 1/240s that the 240fps renderer throwing away frames would give. It should be remembered though that this is still much faster than the human response time which is somewhere between 1/3s and 1/10s as shown [here](https://dotesports.com/general/news/how-to-increase-reaction-time-in-gaming) is an article with some numbers. Scientific studies I've seen tended to have slower reactions, up to 0.5s, but also were looking at people in general rather than FPS players, there's a certain amount of self-selection bias here in that if your reactions aren't great you're not likely to enjoy competitive FPS as much and move onto other things.
3
u/francisk0 Jul 28 '22
The way it works is not counting rendered frames a second and idle when reaching 60. We know we want frames that take ~16ms, so if a given frame took ~12ms then we idle. So eventually when your second is about to finish, so will the number of frames.
28
u/TheSkiGeek Jul 28 '22
Yes, normally you just sit there idle until the next time you're supposed to draw. Same as if you're doing vsync.
Often input is only handled on frame boundaries. And the whole point of an FPS limit (or vsync) is to keep the frame rate constant (assuming the CPU and GPU can keep up with the limit). Why would that be "jittery" if you're holding a constant framerate?
Even if the framerate isn't constant, if it's high enough (>30 is usually okay, >60 is going to be fine for almost anyone), the "jitter" there is very very small. At 60FPS a frame takes ~16.7ms, nobody (except maybe crazy competitive people playing twitch shooters or fighting games) is going to notice if an input takes 5ms or 10ms or 15ms or 20ms to get processed.
Yes, normally you would space them evenly if you're trying to keep a 60FPS cap. When you're done with one frame you track how much time has passed since the last frame, and if it was <16.7ms you wait that long until starting the next one.
No, but normally you'd set your minimum specs such that the game can normally keep the framerate you want. Or on a game console you'd write the game so that the console can keep up.
If you're on PC and the user picks settings so that it's a slideshow... well, that's their problem. It's also possible to decouple the rendering and input processing/game simulation, so that slow rendering doesn't make the game feel too unresponsive.
4
u/pixelgriffin Jul 28 '22
Your initial assumption is more or less correct, as long as there is free time between frames the engine is likely doing nothing, although this gets muddier as you thread things. A basic way to think about this is using a high resolution timer to check if some time has passed, and if it has you will run your logic and render out to your screen. The amount of time passed is based on your capped frame rate. If you capped at 60 as per your example we would only render a frame every 1/60 seconds (~0.0166s).
As for input and visual "jitter" this does happen. Visually, you will notice your simulation getting choppier as the frame rate drops. Input is usually sampled at either a much higher frequency or through a buffer so that it isn't dropped when frame rates sink so players don't notice it.