C++ OpenGL 上的多线程渲染
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11097170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multithreaded Rendering on OpenGL
提问by filipehd
I have a multithreaded application, in which I'm trying to render with different threads. First I tried to use the same Rendering Context between all threads, but I was getting NULL current contexts for other threads. I've read on the internet that one context can only be current at one thread at a time.
我有一个多线程应用程序,我试图在其中使用不同的线程进行渲染。首先,我尝试在所有线程之间使用相同的渲染上下文,但我为其他线程获取了 NULL 当前上下文。我在互联网上读到一个上下文一次只能在一个线程中是最新的。
So I decided to make something different. I create a window, I get the HDC from it and create the first RC. AFter that, I share this HDC between threads, and in every new thread I create I obtain a new RC from the same HDC and I make it current for that thread. Everytime I do it, the RC returned is always different (usually the previous value + 1). I make an assertion to check if wglGetCurrentContext()
returns a RC, and it looks like it returns the one that was just created. But after making the rendering, i get no rendering and if I call GetLastError()
I obtain error 6 (invalid handle??)
所以我决定做一些不同的事情。我创建一个窗口,从中获取 HDC 并创建第一个 RC。之后,我在线程之间共享这个 HDC,并且在我创建的每个新线程中,我从同一个 HDC 获取一个新的 RC,并使其成为该线程的最新版本。每次我这样做时,返回的 RC 总是不同的(通常是以前的值 + 1)。我做了一个断言来检查是否wglGetCurrentContext()
返回了一个 RC,看起来它返回了刚刚创建的那个。但是在进行渲染后,我没有渲染,如果我调用GetLastError()
我会得到错误 6(句柄无效??)
So, does this mean that, despite every new call of wglCreateContext()
gives me a new value, somehow it means that all these different values are the same "Connection channel" to the OpenGL calls?
那么,这是否意味着,尽管每次新调用wglCreateContext()
都给了我一个新值,但不知何故这意味着所有这些不同的值对于 OpenGL 调用都是相同的“连接通道”?
Does this mean that I will always have to invalid the previous Rendering Context on a thread, and activate it on the new one? I really have to make this sync all the time or is there any other way to work arround this problem?
这是否意味着我将始终必须使线程上的先前渲染上下文无效,并在新线程上激活它?我真的必须一直保持同步,还是有其他方法可以解决这个问题?
回答by datenwolf
I have a multithreaded application, in which I'm trying to render with different threads.
我有一个多线程应用程序,我试图在其中使用不同的线程进行渲染。
DON'T!!!
别!!!
You will gain nothingfrom trying to multithread your renderer. Basically you're running into one large race condition and the driver will just be busy synchronizing the threads to somehow make sense of it.
尝试多线程渲染器将一无所获。基本上你遇到了一个大的竞争条件,驱动程序将忙于同步线程以某种方式理解它。
To gain best rendering performance keep all OpenGL operations to only one thread. All parallelization happens for free on the GPU.
为了获得最佳渲染性能,请将所有 OpenGL 操作保持在一个线程中。所有并行化都在 GPU 上免费进行。
回答by Maurizio Benedetti
I suggest to read the following wiki articlefrom the OpenGL Consortium.
我建议阅读来自 OpenGL Consortium的以下 wiki 文章。
In simple words, it depends a lot on what you mean for multi threading in regards to OpenGl, if you have one thread doing the rendering part and one (or more) doing other jobs (i.e. AI, Physics, game logic etc) it is a perfectly right.
简而言之,这在很大程度上取决于您对 OpenGl 的多线程的含义,如果您有一个线程执行渲染部分而一个(或多个)执行其他工作(即 AI、物理、游戏逻辑等),则是完全正确。
If you wish to have multiple threads messing up with OpenGL, you cannot, or better, you could but it will really give you more troubles than advantages.
如果您希望多个线程与 OpenGL 混淆,您不能,或者更好,您可以,但它确实会给您带来更多的麻烦而不是优势。
Try to read the following FAQ on parallel OpenGL usage to have a better idea on this concept:
尝试阅读以下有关并行 OpenGL 使用的常见问题解答,以更好地了解此概念:
http://www.equalizergraphics.com/documentation/parallelOpenGLFAQ.html
http://www.equalizergraphics.com/documentation/parallelOpenGLFAQ.html
回答by JoR
In some cases it may make sense to use multiple rendering contexts in different threads. I have used such a design to load image data from filesystem and push this data into a texture.
在某些情况下,在不同线程中使用多个渲染上下文可能是有意义的。我已经使用这样的设计从文件系统加载图像数据并将这些数据推送到纹理中。
回答by geowar
OpenGL on Mac OS X is single-thread safe; to enable it:
Mac OS X 上的 OpenGL 是单线程安全的;启用它:
#include <OpenGL/OpenGL.h>
CGLError err = 0;
CGLContextObj ctx = CGLGetCurrentContext();
// Enable the multi-threading
err = CGLEnable( ctx, kCGLCEMPEngine);
if (err != kCGLNoError ) {
// Multi-threaded execution is possibly not available
// Insert your code to take appropriate action
}
See: Concurrency and OpenGL - Apple Developer
请参阅: 并发和 OpenGL - Apple Developer