1
\$\begingroup\$

Recently, I was browsing the UE4 source code. I was especially interested in the OpenGL implementation on Windows. The OpenGL "Dynamic RHI" (as they call it) implementation can be found in the Engine\Source\Runtime\OpenGLDrv\ directory.

Contexts

After digging the code, I found out that they have multiple contexts with shared data. At the time of the RHI initialization they create two different contexts (FPlatformOpenGLDevice ctor)

  • SharedContext which will be the "parent" of every other context
  • RenderingContext that shares data with SharedContext

From now on, every time a new window is created and a context/viewport creation is requested, a new OpenGL context is created (PlatformCreateOpenGLContext function). Of course these contexts also share data with the SharedContext.

These contexts also own a VAO and a FBO (expect the viewport contexts where the VAO is not created for some reason; see PlatformCreateOpenGLContext function).

OpenGL "facts"

According to multiple sources:

  • A context can only be active in one thread at a given time
  • To issue gl* calls, you need an active context (of course)
  • The drivers basically run in sequential mode - so sending gl commands from multiple threads from multiple contexts will be "queued up" and will be executed in a sequence. And even worse this could cause context switches back-and-forth
  • So basically there is little or no gain by using multiple contexts (if we are using a single window to render into)

UE4 threading

As far as I know (and according to the source code and the documentation) they have a separate "rendering thread" that is used to execute gl commands. There are multiple functions that are interesting:

  • PlatformRenderingContextSetup which sets up the rendering context in the current thread (with a little "trick" on Windows)
  • PlatformBlitToViewport which displays the image on a specific target

My goal

I'm currently working on a basic engine (as a hobby and for educational purposes, needed by the universtiy) that must support multiple windows and "threaded loading". It is important that the resources loaded must be accessible by every window (context) so a shared context will probably be used (that's why I was interested in the UE4's approach).

Also I'd like to understand the reasons behind the UE4 devs' decisions and how their rendering works from a threading + context point of view. Currently I'm not interested in the actual render commands and technologies like deferred shading and so on.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.