How can I check VSYNC status in Rust?

This is very difficult to search for because of that other Rust.

    let frame_length = std::time::Duration::new(0, 4_166_166);
    let mut timer = std::time::Instant::now();

    while window.is_open() && !window.is_key_down(Key::Escape) {
        let elapsed_time = timer.elapsed();

        if elapsed_time >= frame_length {
            canvas.clear();
            let our_input = Input::from_window(&window);

            game.update([our_input; 8]);

            // TODO: let frame_ready = elapsed_time >= some_runtime_determined_refresh_rate;
            // if frame_ready {
            graphics::draw(&game, &mut canvas);
            // }

            window.update_with_buffer(&canvas.buffer).unwrap();
            timer = std::time::Instant::now();
        }
    }

I have a game loop I'm trying to make framerate-independent by running the updating logic at a high rate, but rendering only when the monitor is ready. Unfortunately, monitor manufacturers keep making up new refresh rates with wildly-non-integral multiples of each other, so if I want my game to have a deterministic, fixed timestep and to render smoothly, I believe I need to check the refresh rate at runtime and check whether the loop should render on this step.

If there are other solutions to the problem, such as using VSYNC from Rust without it taking over game logic loop updating frequency, that's cool too.

As an aside, I use a tight loop without sleeping here because it seems to have less judder.

The way you obtain the refresh rate will probably depend entirely on what windowing/graphics library you're using (for example, in SDL2, you'd call display_mode) - there's nothing built in to Rust for this.

Also, if you're trying to write a deterministic game loop, I'd recommend reading 'Fix Your Timestep' - it's probably the most commonly cited resource on this topic. Their approach allows the game loop to cycle as fast as the hardware/vsync will allow, while still only calling update at a fixed rate, and doesn't require sleeping the thread.

1 Like

Okay, I'll contact the author of the graphics lib I use. I tried search various graphics crates on Github for the term "vsync" but it doesn't come up explicitly.

I have read that. I think all I need to follow the advice relevant to me is the way to check whether VSYNC is ready. For a couple reasons, my game logic loop has to be very decoupled from rendering, because I do network prediction and have to roll back and recompute frames for example. I've just elided the details from my OP.

Thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.