简体   繁体   English

如何从 Rust 中的其他线程绘制 OpenGL 三角形

[英]How to draw OpenGL triangle from other thread in Rust

I'm learning OpenGL is Rust by this example: https://github.com/glium/glium/blob/84f82d3098fbc75aa22160b47bf0a2bdada01f07/examples/triangle.rs#L141我正在通过这个例子学习 OpenGL 是 Rust: https : //github.com/glium/glium/blob/84f82d3098fbc75aa22160b47bf0a2bdada01f07/examples/triangle.rs#L141

It uses a window wrapper called glutin, and draws the triangle once like this:它使用了一个叫做 glutin 的窗口包装器,并像这样绘制了一次三角形:

let draw = move || {
    // building the uniforms
    let uniforms = uniform! {
        matrix: [
            [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.0f32]
        ]
    };

    // drawing a frame
    let mut target = display.draw();
    target.clear_color(0.0, 0.0, 0.0, 0.0);
    target.draw(&vertex_buffer, &index_buffer, &program, &uniforms, &Default::default()).unwrap();
    target.finish().unwrap();
};

The problem is: how can I draw from other threads?问题是:我如何从其他线程中绘制? In order to call draw I'd have to share it with the other thread, but types like program , index_buffer , vertex_buffer , display are not Send+Sync .为了调用draw我必须与另一个线程共享它,但是像programindex_buffervertex_bufferdisplay这样的类型不是Send+Sync

Drawing from other threads is crucial in a window application so I guess they thought of it and there should be a simple way to do it, but I'm lost.从其他线程绘制在窗口应用程序中至关重要,所以我猜他们想到了它,应该有一个简单的方法来做到这一点,但我迷路了。

You cannot do that with Glium, but this is unlikely to be a problem.你不能用 Glium 做到这一点,但这不太可能成为问题。

As per issue #459 , the memory model was reiterated to no longer allow for graphical resources in the API to be shared with multiple threads.根据问题#459 ,内存模型被重申为不再允许 API 中的图形资源与多个线程共享。 Older versions of Glium were synchronizing calls through a background thread anyway, which means that there was no gain in parallelizing these calls.旧版本的 Glium 无论如何都通过后台线程同步调用,这意味着并行化这些调用没有任何好处。 And although there are ways for OpenGL to have multiple contexts and to share certain resources among them, it is too complicated and inefficient for a Rust API to ensure safe and proper usage.尽管 OpenGL 有多种方法可以拥有多个上下文并在它们之间共享某些资源,但对于 Rust API 来说,它太复杂且效率低下,无法确保安全和正确的使用。

Drawing from other threads is crucial in a window application从其他线程绘制在窗口应用程序中至关重要

Note that being unable to perform draw operations through multiple threads does not mean that the application itself becomes single-threaded.请注意,无法通过多个线程执行绘制操作并不意味着应用程序本身变为单线程。 This is not crucial.这不是关键。 The best way to work with this is to dedicate the same thread to rendering and channel commands from other threads into it.解决这个问题的最好方法是将同一线程专用于渲染,并将来自其他线程的命令引入其中。

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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