使用多个OpenGL上下文

问题描述 投票:1回答:1

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

我该怎么办?

multithreading macos opengl operating-system firebreath
1个回答
2
投票

您可以使用来自多个线程的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.
© www.soinside.com 2019 - 2024. All rights reserved.