Skip to content
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

Feature-Request: Ability to limit FPS #56

Closed
reformat0r opened this issue Sep 7, 2021 · 4 comments · Fixed by #63
Closed

Feature-Request: Ability to limit FPS #56

reformat0r opened this issue Sep 7, 2021 · 4 comments · Fixed by #63

Comments

@reformat0r
Copy link

reformat0r commented Sep 7, 2021

Hey!
The examples update time-dependent uniforms by adding a constant value in each frame.
Since FPS are not limited, on my machine, this results in animations that are way too fast.
Additionally, this leads to the GPU running at full capacity at all times, wasting resources.

Naive solution:

const MAX_FPS: f32 = 60.0;
const INTER_FRAME_MILLIS: u64 = (1000.0 / MAX_FPS) as u64;
fn draw(gfx: &mut Graphics, state: &mut State) {
    let start = std::time::Instant::now();

    let mut renderer = gfx.create_renderer();
    // Draw something!
    gfx.render(&renderer);

    // Update some uniform
    state.angle += 0.01;

    let elapsed_millis = start.elapsed().as_millis() as u64;
    if elapsed_millis < INTER_FRAME_MILLIS {
        std::thread::sleep(std::time::Duration::from_millis(INTER_FRAME_MILLIS - elapsed_millis));
    }
}

Maybe it'd make sense for the framework to provide the time that has elapsed since starting the program.
The solution above does not cover the case where we fall below expected FPS.
Uniforms should just depend on the time since start to make them independent from FPS anyway.

@Nazariglez
Copy link
Owner

Hey @reformat0r !

Sorry for the late response. The App struct contains some time information.

  • app.timer.delta_f32() -> f32
  • app.timer.delta() -> Instant
  • app.timer.init_time() -> Instant

You can use the delta to calculate your uniforms, or if you want, you can check the init time or the delta to avoid draw to the GPU some frames.

@reformat0r
Copy link
Author

reformat0r commented Sep 25, 2021

Nice!

  • Do you think the framework should provide a way for the user to set the target (max) FPS?
  • Should I change the examples such that they use the delta-function to compute the uniforms? Otherwise people might wonder why they don't exhibit consistent behavior...

@Nazariglez
Copy link
Owner

I think it's a good idea to provide a way to limit the frames. Let me think about it a little because it's possible to do this with a plugin, but it will need some changes in how the app loop works probably (to be sure that the delta times are right).

Said that I think that it's also a good idea to use the delta time to set the values of the uniforms or any other value that creates animation. Just to avoid weird visual glitches if the frame is not always the same.

@Nazariglez
Copy link
Owner

This is done in the main branch right now. If you're loading the types using notan::prelude::* you can just use .add_plugin(FpsPlugin::new(60)) to use the plugin.

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 a pull request may close this issue.

2 participants