Skip to content

WaitForVBlank #2

Description

@32th-System

Posting this here so it doesn't get lost

32th System

I have an idea on frame limiting to 60 fps on a 144hz monitor with WaitForVBlank:
144 / 60 = 2 with a remainder of 24. 24/60 can be simplified to 2/5. Based on this, this is my idea:
WaitForVBlank would be called twice every frame. It would be called a third time based on this criteria:
You have a 5 byte circular buffer { 0, 1, 0, 1, 0 } and you keep track of an index i into this buffer. Because it's a circular buffer, if i ever overflows outside the buffer it loops back around to the start. Every frame you take the byte at position i in the buffer and increment i. If the byte at i is 1, you WaitForVBlank one more time. If it's 0, you don't

--

Khangaroo

seems to check out

fn main() {
    const LAG_FRAMES: &[bool] = &[false, true, false, true, false];
    const SECONDS: usize = 60;

    let mut lag_idx = 0;
    let mut drawn_frames = 0;
    let mut time = 0.0;
    while time < SECONDS as f64 {
        // Wait for vblank twice
        time += 2.0 / 144.0;
        // Check if we should wait another frame
        if LAG_FRAMES[lag_idx % LAG_FRAMES.len()] {
            time += 1.0 / 144.0;
        }
        drawn_frames += 1;
        lag_idx += 1;
    }

    // Drawn frames: 3601, expected 3600
    println!("Drawn frames: {}, expected {}", drawn_frames, SECONDS * 60);
}

though i wonder how this can be generalized
actually it doesn't seem that hard

fn main() {
    const LAG_FRAMES: &[bool] = &[false, false, false, true];
    const SECONDS: usize = 60;

    let mut lag_idx = 0;
    let mut drawn_frames = 0;
    let mut time = 0.0;
    while time < SECONDS as f64 {
        // Wait for vblank
        time += 1.0 / 75.0;
        // Check if we should wait another frame
        if LAG_FRAMES[lag_idx % LAG_FRAMES.len()] {
            time += 1.0 / 75.0;
        }
        drawn_frames += 1;
        lag_idx += 1;
    }

    println!("Drawn frames: {}, expected {}", drawn_frames, SECONDS * 60);
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions