not-fl3 / macroquad

Cross-platform game engine in Rust.
Apache License 2.0
3.28k stars 318 forks source link

Reproducible multi-frame drops for bare loop #311

Open marcianx opened 2 years ago

marcianx commented 2 years ago

I was experimenting with macroquad moving a simple ball around the screen, and I noticed sudden pauses that appear periodically while keeping the key pressed to move the ball. As a simple test, I ran:

use macroquad::prelude::*;

#[macroquad::main("frame drops")]
async fn main() {
    loop {
        if is_key_pressed(KeyCode::Escape) {
            return;
        }

        let next_frame_time = get_frame_time();
        println!("frame time {}", next_frame_time);
        if next_frame_time > 3. / 60. {
            break;
        }
        next_frame().await;
    }
}

which outputs:

frame time 0.016605616  <-- lots of lines similar to this
frame time 0.03383684  <-- then suddenly this and the one after
frame time 0.06664562

suggesting a frame drop followed by 3 frame drops. (It also sometimes drops a single frame and recovers right after.) I can easily reproduce this in both debug and --release. This is on Pop!_OS 21.04 on an 8-core AMD with Radeon RX 580 -- ample specs to run an noop without frame drops.

Any insights?

marcianx commented 2 years ago

Actually, changing the loop body to

        let next_frame_time = get_frame_time();
        print!(".");
        if next_frame_time > 1.5 / 60. {
            println!("\nframe time {}", next_frame_time);
        }
        if next_frame_time > 5. / 60. {
            break;
        }
        next_frame().await;

shows a lot more output of various numbers of frames being dropped in the middle. I used the intermediate dots to determine sequential multi-frame drops, and cleaning up the dots, I easily see this output:

frame time 0.033828974

frame time 0.033765078
frame time 0.049669266

frame time 0.033704042

frame time 0.033545017
frame time 0.0333755

frame time 0.033708572

frame time 0.033925056
frame time 0.04947877

frame time 0.033824205

frame time 0.033686876
frame time 0.049539566

frame time 0.033770084

frame time 0.06733322

frame time 0.033753395

frame time 0.033765793
frame time 0.04961443

Going back to my little demo for moving a ball, the string of sequential frame drops are significantly noticeable. So I was wondering whether there's a configuration that might help me eliminate those.