architecture – Understand UE4 OpenGL contexts

I recently searched the UE4 source code. I was particularly interested in the OpenGL implementation on Windows. The OpenGL implementation "Dynamic RHI" (as they call it) is in the Engine Source Runtime OpenGLDrv Directory.

contexts

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

  • Shared context this will be the "parent" of every other context
  • rendering context that communicates data Shared context

From now on, each 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 Shared context,

These contexts also have a VAO and an FBO (expect the viewport contexts where the VAO is not created for some reason, see PlatformCreateOpenGLContext Function).

OpenGL "facts"

From several sources:

  • A context can only be active in one thread at a given time
  • Output gl * Calls, you need an active context (of course)
  • The drivers are essentially executed in sequential mode. Sending gl commands from multiple threads from multiple contexts is queued and executed in a sequence. And worse, this can cause the context to change
  • Basically, there is little or no benefit from using multiple contexts (if we use a single window to render)

UE4-Threading

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

  • Platform Rendering Context Setup setting 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 am currently working on a basic machine (as a hobby and educational needed by the university) that needs to support multiple windows and threaded loading. It is important that the loaded resources have to be accessible to each window (context), so probably a common context is used (that's why I was interested in the approach of UE4).

I'd also like to understand the reasons for the decisions of the UE4 developers and how their presentation works Threading + context view, Currently I am not interested in the actual rendering commands and technologies like delayed shading etc.