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

Rust 这个(gfx rs)glutin窗口真的需要一整秒钟才能加载吗?,rust,glfw,Rust,Glfw,我正试图开始使用,只是从中复制源代码,以确保运行。我注意到窗户要花很长时间才能装满。它似乎打开得比较快,但我可以看到一个完全空白(白色)的窗口近一秒钟。我做了一些计时检查,看看代码的哪一部分让事情陷入困境 货舱 [package] name = "graphics" version = "0.1.0" [dependencies] gfx = "0.14" gfx_macros = "0.2" glutin = &

我正试图开始使用,只是从中复制源代码,以确保运行。我注意到窗户要花很长时间才能装满。它似乎打开得比较快,但我可以看到一个完全空白(白色)的窗口近一秒钟。我做了一些计时检查,看看代码的哪一部分让事情陷入困境

货舱

[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 = "*"
梅因

#[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");
}
#[宏使用]
外部板条箱gfx;
外部板条箱玻璃纤维窗玻璃纤维胶;
外置板条箱明胶;
外置板条箱时间;
外部板条箱fps_计数器;
使用时间::PreciseTime;
使用gfx::traits::FactoryExt;
使用gfx::设备;
//使用fps_计数器::fps计数器;
使用std::io;
//使用std::time::Duration;
//使用std::thread::sleep;
发布类型ColorFormat=gfx::format::Rgba8;
发布类型DepthFormat=gfx::format::DepthStencil;
gfx_定义!{
顶点{
位置:[f32;2]=“a_pos”,
颜色:[f32;3]=“a_颜色”,
}
管道{
vbuf:gfx::VertexBuffer=(),
out:gfx::RenderTarget=“Target0”,
}
}
常数三角形:[顶点;3]=[
顶点{pos:[-0.5,-0.5],颜色:[1.0,0.0,0.0]},
顶点{pos:[0.5,-0.5],颜色:[0.0,1.0,0.0]},
顶点{pos:[0.0,0.5],颜色:[0.0,0.0,1.0]}
];
常量透明颜色:[f32;4]=[0.1,0.2,0.3,1.0];
pub fn main(){
让mut start=PreciseTime::now();
let builder=glutin::WindowBuilder::new()
.带有标题(“三角形示例“.to_string())
.具有_尺寸(1024768)
.with_vsync();
让mut end=PreciseTime::now();
println!(“窗口生成器:{}”,start.to(end.num_毫秒());
start=PreciseTime::now();
let(窗口、mut设备、mut工厂、主颜色、mut主深度)=
gfx_window_glutin::init::(生成器);
end=PreciseTime::now();
println!((“初始化窗口:{}”,start.to(end.num_毫秒());
start=PreciseTime::now();
让mut编码器:gfx::encoder=factory.create_命令_buffer().into();
让pso=factory.create\u pipeline\u simple(
包括_字节!(“着色器/三角形_150.glslv”),
包括_字节!(“着色器/三角形_150.glslf”),
管道::新建()
).unwrap();
let(顶点缓冲区,切片)=工厂。使用切片(&三角形,())创建顶点缓冲区;
让mut data=pipe::data{
vbuf:顶点缓冲区,
输出:主颜色
};
//让mut fps=FPSCounter::new();
//让mut count=0;
end=PreciseTime::now();
println!((“设置:{}”,start.to(end.num_毫秒());
'主:环路{
//循环事件
用于窗口中的事件。轮询_事件(){
比赛项目{
glutin::Event::KeyboardInput(,,Some(glutin::VirtualKeyCode::Escape))|
glutin::Event::Closed=>break'main,
glutin::事件::已调整大小(\u宽度,\u高度)=>{
gfx_window_glutin::更新_视图(&window,&mut data.out,&mut main_depth);
},
_ => {},
}
}
//画框
编码器。清除(&data.out,清除颜色);
编码器。绘制(&slice,&pso,&data);
编码器、冲洗(和mut设备);
window.swap_buffers().unwrap();
device.cleanup();
//计数=fps.tick();
}
//println!(“fps:{}”,计数);
//让mut输入=String::new();
//io::stdin()。读取行(&mut输入)
//.expect(“读取行失败”);
}
上面的输出显示初始化窗口的调用几乎需要一秒钟的时间

窗口生成器:0

初始化窗口:933

设置:3

所涉行:

let (window, mut device, mut factory, main_color, mut main_depth) =
    gfx_window_glutin::init::<ColorFormat, DepthFormat>(builder);
let(窗口、mut设备、mut工厂、主颜色、mut主深度)=
gfx_window_glutin::init::(生成器);
这通过货物运行、调试和发布以及直接从构建输出文件夹运行可执行文件来实现

这是意料之中的事吗

系统信息:

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

感觉这将是一个系统配置问题。我鼓励你添加你正在使用的操作系统,你正在使用的视频卡驱动程序,OpenGL的版本,甚至硬件的细节。我的另一个猜测是,它实际上是着色器的运行时编译;您也可以尝试删除程序的某些部分,看看是否能将其识别为真正的罪魁祸首。@Shepmaster添加了我的规格。我还刚刚升级了我的NVIDIA驱动程序,但似乎没有任何帮助。这甚至可能让事情变得更糟(现在平均超过2秒),但我不能确定这是原因。编辑:使用我的集成图形卡运行会使其下降到50毫秒?有人知道为什么吗?@plast1k您是否探讨了着色器运行时编译的可能性?也许您可以在编译时编译它们?您可以搜索有关预编译着色器的一些问题,如下所示: