这个(gfx-rs)glutin窗口真的需要一整秒才能加载吗?

时间:2017-08-13 03:14:19

标签: rust glfw

我正在尝试使用gfx-rs并且只是简单地复制the triangle example中的源代码,以确保运行。我注意到窗口需要花费相当多的时间来完全加载。它似乎打开相对较快,但我可以看到一个完全空白(白色)窗口接近一秒钟。我进行了一些时间检查,看看代码的哪一部分正在堵塞事情。

Cargo.toml

[package]
name = "graphics"
version = "0.1.0"

[dependencies]
gfx = "0.14"
gfx_macros = "0.2"
glutin = "0.7.1"
gfx_window_glutin = "0.14"
time = "*"
fps_counter = "*"

main.rs

#[macro_use]
extern crate gfx;
extern crate gfx_window_glutin;
extern crate glutin;
extern crate time;
extern crate fps_counter;

use time::PreciseTime;
use gfx::traits::FactoryExt;
use gfx::Device;
// use fps_counter::FPSCounter;
use std::io;
// use std::time::Duration;
// use std::thread::sleep;


pub type ColorFormat = gfx::format::Rgba8;
pub type DepthFormat = gfx::format::DepthStencil;

gfx_defines!{
    vertex Vertex {
        pos: [f32; 2] = "a_Pos",
        color: [f32; 3] = "a_Color",
    }

    pipeline pipe {
        vbuf: gfx::VertexBuffer<Vertex> = (),
        out: gfx::RenderTarget<ColorFormat> = "Target0",
    }
}

const TRIANGLE: [Vertex; 3] = [
    Vertex { pos: [ -0.5, -0.5 ], color: [1.0, 0.0, 0.0] },
    Vertex { pos: [  0.5, -0.5 ], color: [0.0, 1.0, 0.0] },
    Vertex { pos: [  0.0,  0.5 ], color: [0.0, 0.0, 1.0] }
];

const CLEAR_COLOR: [f32; 4] = [0.1, 0.2, 0.3, 1.0];

pub fn main() {
    let mut start = PreciseTime::now();
    let builder = glutin::WindowBuilder::new()
        .with_title("Triangle example".to_string())
        .with_dimensions(1024, 768)
        .with_vsync();
    let mut end = PreciseTime::now();
    println!("window builder: {}", start.to(end).num_milliseconds());
    start = PreciseTime::now();
    let (window, mut device, mut factory, main_color, mut main_depth) =
        gfx_window_glutin::init::<ColorFormat, DepthFormat>(builder);
    end = PreciseTime::now();
    println!("init window: {}", start.to(end).num_milliseconds());

    start = PreciseTime::now();
    let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
    let pso = factory.create_pipeline_simple(
        include_bytes!("shader/triangle_150.glslv"),
        include_bytes!("shader/triangle_150.glslf"),
        pipe::new()
    ).unwrap();
    let (vertex_buffer, slice) = factory.create_vertex_buffer_with_slice(&TRIANGLE, ());
    let mut data = pipe::Data {
        vbuf: vertex_buffer,
        out: main_color
    };

    // let mut fps = FPSCounter::new();
    // let mut count = 0;
    end = PreciseTime::now();
    println!("setup: {}", start.to(end).num_milliseconds());

    'main: loop {
        // loop over events
        for event in window.poll_events() {
            match event {
                glutin::Event::KeyboardInput(_, _, Some(glutin::VirtualKeyCode::Escape)) |
                glutin::Event::Closed => break 'main,
                glutin::Event::Resized(_width, _height) => {
                    gfx_window_glutin::update_views(&window, &mut data.out, &mut main_depth);
                },
                _ => {},
            }
        }
        // draw a frame
        encoder.clear(&data.out, CLEAR_COLOR);
        encoder.draw(&slice, &pso, &data);
        encoder.flush(&mut device);
        window.swap_buffers().unwrap();
        device.cleanup();
        // count = fps.tick();
    }

    // println!("fps: {}", count);
    // let mut entered = String::new();
    // io::stdin().read_line(&mut entered)
    //     .expect("Failed to read line");
}

上面的输出显示初始化窗口的调用花了将近一秒钟。

  

窗口构建器:0

     

初始窗口:933

     

设置:3

有问题的一行:

let (window, mut device, mut factory, main_color, mut main_depth) =
    gfx_window_glutin::init::<ColorFormat, DepthFormat>(builder);

这通过货运,调试和发布以及直接从构建输出文件夹运行可执行文件来实现。

这是预期的吗?

系统信息:

  • Windows 10,64位
  • i7-4720HQ,16GB RAM,GTX-960M
  • OpenGL 4.4
  • Rust 1.19.0

0 个答案:

没有答案