gpt4 book ai didi

rust - 从前端包中的包引用 Gfx 后端类型

转载 作者:行者123 更新时间:2023-11-29 08:27:09 25 4
gpt4 key购买 nike

我正在关注 a tutorial on the gfx library .以下代码用于初始化窗口系统,它使用隐式类型绑定(bind)返回各种位。

let (window, mut device, mut factory, color_view, mut depth_view) =
gfx_glutin::init::<ColorFormat, DepthFormat>(windowbuilder, contextbuilder, &events_loop);

为了可读性,我决定尝试将教程中的绘图代码提取到它自己的函数中。

fn draw_triangle(factory: &Factory) {
let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
const TRIANGLE: [Vertex; 3] = [
Vertex {
pos: [-0.5, -0.5, 0.0, 1.0],
color: [1.0, 0.0, 0.0],
},
Vertex {
pos: [0.5, -0.5, 0.0, 1.0],
color: [0.0, 1.0, 0.0],
},
Vertex {
pos: [0.0, 0.5, 0.0, 1.0],
color: [0.0, 0.0, 1.0],
},
];
//Identity Matrix
const TRANSFORM: Transform = Transform {
transform: [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
],
};

let (vertex_buffer, slice) = factory.create_vertex_buffer_with_slice(&TRIANGLE, ());
let transform_buffer = factory.create_constant_buffer(1);
let data = pipe::Data {
vbuf: vertex_buffer,
transform: transform_buffer,
out: color_view.clone(),
};
//Put in main loop before swap buffers and device clean-up method
encoder.clear(&color_view, BLACK); //clear the framebuffer with a color(color needs to be an array of 4 f32s, RGBa)
encoder.update_buffer(&data.transform, &[TRANSFORM], 0); //update buffers
encoder.draw(&slice, &pso, &data); // draw commands with buffer data and attached pso
encoder.flush(&mut device); // execute draw commands
}

这里的问题是 Factory(从第一个代码片段传入)不存在于该文件的范围内,实际上属于存在于 gfx_glutin_window crate 中的 crate。

编译器很高兴在第一段代码中隐式定义它,但看起来我不能在不声明对所述 crate 的硬依赖的情况下显式引用它。 (即我无法通过 gfx_glutin_window crate 获取它)。

我不确定我是否完全没有注意到这里的标记,或者这是否是设计使然。尽管我可以想象在这种情况下项目依赖项与 transient 依赖项发生冲突的场景。

最佳答案

一方面,gfx_window_glutin 确实可能会受益于重新导出数据类型,因为它的公共(public) API 正在有效地返回这些类型的实例。这以前在其他 crate 中发生过,例如 pathfinder re-exporting num_traits ,并且可以通过在主存储库中提交问题来提出类似于 gfx_window_glutin 的内容。

另一方面,实际上建议构建在 Gfx 后端实现上通用的代码。两者 FactoryResources是封装底层实现的特征类型,因此您应该在代码中使用这些约束。

use gfx::{Factory, Resources};

fn draw_triangle<R, F>(factory : &F)
where
R: Resources,
F: Factory<R>
{
// ...
}

关于rust - 从前端包中的包引用 Gfx 后端类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47412199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com