Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. 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??)

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?

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?

like image 412
filipehd Avatar asked Jun 19 '12 08:06

filipehd


People also ask

Can OpenGL be multithreaded?

Guidelines for Threading OpenGL ApplicationsUse only one thread per context. OpenGL commands for a specific context are not thread safe. You should never have more than one thread accessing a single context simultaneously. Contexts that are on different threads can share object resources.

Should I turn on multithreaded rendering?

“The graphics setting can improve CPU performance and graphics quality on powerful devices.” Usually, multithreaded rendering is a no-brainer that should always be enabled to improve your system's performance during action-packed moments in the game.

Is Vulkan multithreaded?

Introduction. Multithreading is a cornerstone of Vulkan. Vulkan allows application to spread rendering workload across multiple CPU threads. This can have huge benefit for complex applications.

What does multithreaded rendering mean?

Multithreaded rendering is a setting you can turn on or off in Fortnite. Multithreaded rendering splits drawing work across multiple threads and can improve performance on CPUs with multiple cores. But multithreaded rendering can cause hitching and lower FPS on weaker CPUs.


1 Answers

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. All parallelization happens for free on the GPU.

like image 93
datenwolf Avatar answered Nov 10 '22 01:11

datenwolf