简体   繁体   中英

Avoiding dropped frames in glium

I'm using glium as my opengl bindings, but it's impossible to get a reliable 60 FPS.

A minimal testcase is

#[macro_use]
extern crate glium;
extern crate clock_ticks;

use glium::Surface;
use glium::glutin;

fn main() {
    use glium::DisplayBuild;

    let display = glutin::WindowBuilder::new()
        .build_glium()
        .unwrap();

    let frames = 60 * 5;
    let trials = 3;

    for _ in 0.. trials {
        let start_ns = clock_ticks::precise_time_ns();
        for _ in 0..frames {
            display.draw().finish().unwrap();
        }

        let duration_ns = clock_ticks::precise_time_ns() - start_ns;
        let duration_s = (duration_ns as f64) / 1_000_000_000f64;
        let fps = (frames as f64) / duration_s;
        let dropped = (duration_s - (frames as f64 * (1f64/60f64))) / (1f64/60f64);

        println!("{} frames in {:.6} seconds = {:.3} fps (estimated {:.1} frames dropped)", frames, duration_s, fps, dropped);
    }
}

Where I would expect 60 FPS, but is frequently showing 59 FPS when I'm running it (in OSX). The project is available on github for ease of compiling and running.

Is there any way I can tweak glium so that it won't drop frames? OSX is overriding the vsync setting, so there's no way not to wait for vsync between each frame.

Yes, like @8bitree, I suspected that you are measuring incorrectly, rather than it being an actual problem. On my system, Debian:

steve@warmachine:~/tmp/guess$ cargo run
     Running `target/debug/guess`
300 frames in 4.427656 seconds = 67.756 fps (estimated -34.3 frames dropped)
300 frames in 0.006892 seconds = 43529.834 fps (estimated -299.6 frames dropped)
300 frames in 0.006522 seconds = 45997.412 fps (estimated -299.6 frames dropped)
steve@warmachine:~/tmp/guess$ cargo run
     Running `target/debug/guess`
300 frames in 4.953447 seconds = 60.564 fps (estimated -2.8 frames dropped)
300 frames in 4.999410 seconds = 60.007 fps (estimated -0.0 frames dropped)
300 frames in 1.608712 seconds = 186.485 fps (estimated -203.5 frames dropped)

So yeah, something is a bit... odd.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM