GL Contexts

Problems building or running the engine, queries about how to use features etc.
User avatar
eventhorizon
Kobold
Posts: 32
Joined: Wed Oct 20, 2010 4:56 am
x 12

GL Contexts

Post by eventhorizon »

Ogre Version: 14.2.6 :?:
Operating System: Windows, Mac, Linux :?:
Render System: OpenGL/OpenGL3 :?:

With my project, I'm working on multithreading it's sim engines, by running each engine's runloop in a separate thread. So far it's working very well, and works mostly fine (except with some sync issues) on DirectX11. Since my app mostly uses OpenGL, GL uses graphics contexts (unlike DX11) and I'm currently finding a way for other threads to access the primary thread's GL context in order to run the related Ogre graphics functions on it (mainly just the texture/material functions with mutexes, rendering code is all done in the primary thread). I'm wondering if others have done something similar to this, and I'm going over the Ogre source code to see what I can do in it to solve this problem. Previously I was having the app copy some data between threads, so that it could execute the graphics code on the primary thread, but since the code was becoming cumbersome, I thought switching contexts would be a better and simpler idea.

Here's some of the context switch testing code I have (this doesn't work in the simulator at the moment, due to header and linking issues). What I'm doing is trying to store the app's GL context from the primary thread, and then have the other threads switch to that stored context if possible.
In the code, the main thread runs GetGLContext() and stores the result, the secondary thread then runs SwitchGraphicsContext() to apply that stored context to itself, not sure if I'm doing it right though:

Code: Select all

#include <RenderSystems/GL/OgreGLRenderSystem.h>

//GL graphics context
Ogre::GLContext *gl_context;

Ogre::GLContext* GetGLContext()
{
        //get application OpenGL context
        Ogre::RenderSystem *rendersystem = mRoot->getRenderSystem();
        Ogre::GLRenderSystem* system = (Ogre::GLRenderSystem*)rendersystem;
        return system->_getMainContext();
}

void SwitchGraphicsContext()
{	
	//switch OpenGL graphics context to the primary thread's context

    Ogre::RenderSystem *rendersystem = mRoot->getRenderSystem();
    Ogre::GLRenderSystem* system = (Ogre::GLRenderSystem*)rendersystem;
    system->_switchContext(gl_context);
}

I'm mainly wondering what the best way to do this might be. I might try modifying some of the Ogre source to see if I can come up with a solution too. I posted about my project in the Showcase board, but it's here:
https://www.skyscrapersim.net

User avatar
eventhorizon
Kobold
Posts: 32
Joined: Wed Oct 20, 2010 4:56 am
x 12

Re: GL Contexts

Post by eventhorizon »

Another idea I have, instead of doing context switching, is to move all texture code out to the simulator's frontend (which runs on the main thread), since eventually I'll split the app into client and server for multiplayer, so this idea might work too.

paroj
OGRE Team Member
OGRE Team Member
Posts: 2143
Joined: Sun Mar 30, 2014 2:51 pm
x 1153

Re: GL Contexts

Post by paroj »

There is an API to create a shared context for threads:
https://ogrecave.github.io/ogre/api/lat ... c2d03a6ced

you can access some of the OpenGL stuff there:
https://stackoverflow.com/questions/558 ... le-sharing

note that this is preferable to context switching, as switching is heavyweight and can easily negate any advantages you get by threading.

User avatar
eventhorizon
Kobold
Posts: 32
Joined: Wed Oct 20, 2010 4:56 am
x 12

Re: GL Contexts

Post by eventhorizon »

Thank you for that, I followed the steps in the API guide and everything's working fine now.