简体   繁体   English

OpenGL 上的多线程渲染

[英]Multithreaded Rendering on OpenGL

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.首先,我尝试在所有线程之间使用相同的渲染上下文,但我为其他线程获取了 NULL 当前上下文。 I've read on the internet that one context can only be current at one thread at a time.我在互联网上读到一个上下文一次只能在一个线程中是最新的。

So I decided to make something different.所以我决定做一些不同的事情。 I create a window, I get the HDC from it and create the first RC.我创建一个窗口,从中获取 HDC 并创建第一个 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.之后,我在线程之间共享这个 HDC,并且在我创建的每个新线程中,我从同一个 HDC 获取一个新的 RC,并使其成为该线程的最新版本。 Everytime I do it, the RC returned is always different (usually the previous value + 1).每次我这样做时,返回的 RC 总是不同的(通常是以前的值 + 1)。 I make an assertion to check if wglGetCurrentContext() returns a RC, and it looks like it returns the one that was just created.我做了一个断言来检查wglGetCurrentContext()返回一个 RC,看起来它返回了刚刚创建的那个。 But after making the rendering, i get no rendering and if I call GetLastError() I obtain error 6 (invalid handle??)但是在进行渲染后,我没有渲染,如果我调用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?我真的必须一直保持同步,还是有其他方法可以解决这个问题?

I have a multithreaded application, in which I'm trying to render with different threads.我有一个多线程应用程序,我试图在其中使用不同的线程进行渲染。

DON'T!!!别!!!

You will gain nothing from 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.为了获得最佳渲染性能,请将所有 OpenGL 操作保持在一个线程中。 All parallelization happens for free on the GPU.所有并行化都在 GPU 上免费进行。

I suggest to read the following wiki article from 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 (ie 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

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.我使用这样的设计从文件系统加载图像数据并将这些数据推送到纹理中。

OpenGL on Mac OS X is single-thread safe; Mac OS X 上的 OpenGL 是单线程安全的; to enable it:启用它:

#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

And: Technical Q&A QA1612: OpenGL ES multithreading and ...以及: 技术问答 QA1612:OpenGL ES 多线程和...

https://www.imaginationtech.com/blog/understanding-opengl-es-multi-thread-multi-window-rendering/ https://www.imaginationtech.com/blog/understanding-opengl-es-multi-thread-multi-window-rendering/

When shouldn't I use multi-threaded rendering?什么时候不应该使用多线程渲染?

When you're not CPU limited or load times are not a concern.当您不受 CPU 限制或加载时间不是问题时。

So, if you are CPU limited, separate other threads to do CPU limited job, such as codec, texture upload, calculate...因此,如果您的 CPU 受限,请单独其他线程来执行 CPU 受限的工作,例如编解码器、纹理上传、计算...

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

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