【问题标题】:Use Multiple OpenGL Contexts使用多个 OpenGL 上下文
【发布时间】:2013-02-04 04:40:04
【问题描述】:

在firebreath(mac os)上写一个插件,画一个视频 创建一个窗口来获取上下文,现在我希望在窗口中绘制我的库,该库在另一个线程中运行。

我该怎么做?

【问题讨论】:

    标签: multithreading macos opengl operating-system firebreath


    【解决方案1】:

    您可以从多个线程使用 OpenGL 上下文,只要您一次从不从多个线程同时使用它。例如

    线程 A:

    [myContext makeCurrentContext];
    // Do something with the context...
    // ... then release it on the thread.
    [NSOpenGLContext clearCurrentContext];
    // Tell Thread B that we released the context.
    // Wait for Thread B to finish...
    // Grab the context again.
    [myContext makeCurrentContext];
    // Do something with the context...
    

    线程 B:

    // Wait for Thread A to release the context...
    [myContext makeCurrentContext];
    // Do something with the context...
    // ... then release it on the thread.
    [NSOpenGLContext clearCurrentContext];
    // Let Thread A know, that we are done with the context.
    

    另一种可能性是使用辅助共享上下文。共享上下文与其父上下文共享相同的资源,因此您可以在共享上下文中创建纹理(在辅助线程上使用),在辅助线程上将视频渲染到该纹理,然后让主线程渲染纹理(在将下一帧渲染到辅助线程上的纹理之前,它也可以在主线程的父上下文中使用)到屏幕。

    更新

    与上述 CGL 框架的代码相同:

    线程 A:

    err = CGLSetCurrentContext(myContext);
    // Do something with the context...
    // ... then release it on the thread.
    err = CGLSetCurrentContext(NULL);
    // Tell Thread B that we released the context.
    // Wait for Thread B to finish...
    // Grab the context again.
    err = CGLSetCurrentContext(myContext);
    // Do something with the context...
    

    线程 B:

    // Wait for Thread A to release the context...
    err = CGLSetCurrentContext(myContext);
    // Do something with the context...
    // ... then release it on the thread.
    err = CGLSetCurrentContext(NULL);
    // Let Thread A know, that we are done with the context.
    

    【讨论】:

    • 在流中我只创建了窗口,更多的没有这样做。谢谢,我会试试你的选择。但我不反对 c 代码。我有 CGLContextObj
    • 在 CGLSetCurrentContext (dh-> share) 之后;任何方法都会删除 opengl 插件
    • @user 我添加了一个CGL示例代码; clearCurrentThread 应该已经清除了当前上下文(对不起,我的错误)。 “丢弃 OpenGL 插件”是什么意思?在这种情况下,什么是“丢弃”?没有任何代码很难帮助你;在错误的时间设置错误的上下文当然会带来麻烦。此外,在设置自己的上下文之前,您可能希望保存当前设置的上下文并在以后恢复它,因为其他代码可能依赖于它的上下文永远不会改变。
    • @user 系统也不会阻止在多个线程上设置相同的上下文(例如,上下文不是在线程 A 上“取消设置”,只是因为它在线程 B 上设置了)并且它也不会导致错误消息,但是在多个线程上使用上下文设置可能会导致麻烦,因为上下文并非旨在同时在多个线程上设置(但取决于实现)。
    • 我纠正了跌倒。但这不会在窗口中绘制第二个线程,只有第一个! ERR = CGLSetCurrentContext (NULL) - 没有尝试过,我会尝试 - 我会写。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 2015-07-07
    • 1970-01-01
    相关资源
    最近更新 更多