Inconsistent behavior when using external OpenGL context

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
fractile
Gremlin
Posts: 199
Joined: Thu Jan 13, 2005 2:35 pm
Location: Tampere, Finland
x 15
Contact:

Inconsistent behavior when using external OpenGL context

Post by fractile »

I have previously let Ogre (v1.9) create the OpenGL context and things have been working mostly well. Now that I'm trying to make it use the context created by GLFW, things got weird again. With tips from other threads here (like creating the window before loading plugins, etc.) I got to this point. This works fine on my laptop (Xubuntu 16.04, Intel Skylake), but on another PC (Xubuntu 16.04, Intel Haswell) this shows either an empty window or blinking first rendered frame. My old version which didn't use "currentGLContext" or "externalGLControl" works on both machines, but I would like to let GLFW handle the windows and swapping.

Does anyone spot any obvious errors in following code?

Code: Select all

    // Create window with requested properties
    glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
    glfwWindowHint(GLFW_SAMPLES, fsaa);
    m_Window = glfwCreateWindow(width, height, m_Title.c_str(), monitor, 0);

    // Set created GL context current (NOTE: this has to be done before loading Ogre renderer,
    // otherwise GLXBadContextTag errors occur at exit)
    glfwMakeContextCurrent(m_Window);

    // Load OpenGL render system
    m_Root->loadPlugin("lib/RenderSystem_GL.so");

    // Choose first available renderer
    Ogre::RenderSystemList renderers = m_Root->getAvailableRenderers();
    if(renderers.empty())
    {
        fg::Log::error("No renderers available");
        OGRE_EXCEPT(Ogre::Exception::ERR_INTERNAL_ERROR, "No renderers available",
            "OgreApplication::initialize");
    }
    m_Root->setRenderSystem(renderers[0]);

    // Initialize Ogre
    m_Root->initialise(false);

    // Set up Ogre render window
    Ogre::NameValuePairList misc;
    misc["parentWindowHandle"] = Ogre::StringConverter::toString((unsigned long)glfwGetX11Window(m_Window));
    misc["currentGLContext"] = "true";
    misc["externalGLControl"] = "true";
    m_RenderWindow = m_Root->createRenderWindow(m_Title, width, height, false, &misc);
Main loop:

Code: Select all

    while(true)
    {
        Ogre::WindowEventUtilities::messagePump();

        if(!m_Root->renderOneFrame())
        {
            break;
        }

        glfwSwapBuffers(m_Window);
        glfwPollEvents();
    }
frostbyte
Orc Shaman
Posts: 737
Joined: Fri May 31, 2013 2:28 am
x 65

Re: Inconsistent behavior when using external OpenGL context

Post by frostbyte »

don't know about your specific problem but since this code works in one enviroment and not in another
i would dare say the problem is probably not in the code above but with the enviroment...
might be old driver( try updating ), old/different library version being loaded, old headers mixed in etc...
i've once had sever memory leaks relating textures, turned out one of my app dependencies was secretly loading an old version of openGL
it was very tricky to find...this kind of bugs are nasty...
good luck (-:

btw: since you're on linux i guess you can use DOCKER to isolate your dev enviroment from the OS enviroment...
using Docker has many advantages...
the woods are lovely dark and deep
but i have promises to keep
and miles to code before i sleep
and miles to code before i sleep..

coolest videos link( two minutes paper )...
https://www.youtube.com/user/keeroyz/videos
User avatar
fractile
Gremlin
Posts: 199
Joined: Thu Jan 13, 2005 2:35 pm
Location: Tampere, Finland
x 15
Contact:

Re: Inconsistent behavior when using external OpenGL context

Post by fractile »

That's a good point. All relevant library versions should be identical, but drivers are not. I could (and probably will) try updating the Intel driver on the other PC too to see if that fixes the problem. However, I can't expect everybody to install "non-standard" drivers even if that does fix the problem. If that is the case, I will probably have revert to letting Ogre handle contexts.
frostbyte
Orc Shaman
Posts: 737
Joined: Fri May 31, 2013 2:28 am
x 65

Re: Inconsistent behavior when using external OpenGL context

Post by frostbyte »

just a suggestion:
'since ogre does'nt seem to like the glContext created by glfw( for whatever reason )...
maybe you can try doing it the other way around, let ogre create the glContext and make glfw use the glContext created by ogre
you can probably leave the window creation and handling/swaping to glfw if thats what you want
or maybe its better to let glfw create the window from ogres window( if thats possible... )
the woods are lovely dark and deep
but i have promises to keep
and miles to code before i sleep
and miles to code before i sleep..

coolest videos link( two minutes paper )...
https://www.youtube.com/user/keeroyz/videos
User avatar
fractile
Gremlin
Posts: 199
Joined: Thu Jan 13, 2005 2:35 pm
Location: Tampere, Finland
x 15
Contact:

Re: Inconsistent behavior when using external OpenGL context

Post by fractile »

I updated the drivers on the other PC to same version, but it did not change anything. My code still works on the laptop, but not on the desktop PC. I can't think of any relevant differences between the to machines.

I have spent way more time debugging this than I should have. After all, there was nothing critically wrong with the old way. This was all a side quest from fixing broken anti-aliasing on Windows. It turned out that AA must be enabled through GLFW even though everything else is controlled by Ogre.

I'm reverting back to the old way and let Ogre create and control it's own GL context on Linux. That seems to work everywhere.

Thanks for the suggestions, frostbyte.
User avatar
fractile
Gremlin
Posts: 199
Joined: Thu Jan 13, 2005 2:35 pm
Location: Tampere, Finland
x 15
Contact:

Re: Inconsistent behavior when using external OpenGL context

Post by fractile »

I'm back at this. It seems that this behavior is random, not directly dependent on the environment. It happens on both machines, only less frequently on the other.

When application starts, sometimes renderOneFrame() does update the render window, sometimes it doesn't. If I add manual m_RenderWindow->update(false) after renderOneFrame() and before glfwSwapBuffers(), it seems to work reliably. This just doesn't make any sense.
frostbyte
Orc Shaman
Posts: 737
Joined: Fri May 31, 2013 2:28 am
x 65

Re: Inconsistent behavior when using external OpenGL context

Post by frostbyte »

daaa...if you really need to scratch this itch....i don't have real insight, but i try to help anyhow...
1) debug your render-loop, go for the minimal case use, ogre cube, empty glfw window...
infrequent bugs are the trickiest to hunt, traditional debugging is useless( since most of the time there is no bug )
but still maybe if you'll see whats happening inside, you'll get better view on this
2) try the minimal - render loop with latest ogre 1.1x
maybe this was fixed( perhaps even not on purpose ), beside the fact that opengl was completely refactored, i saw a lot of commits regarding opengl state-cache, maybe it was buggy on 1.9
3)
If I add manual m_RenderWindow->update(false) after renderOneFrame() and before glfwSwapBuffers()
this can be either:
a) problem is not with ogre and you are eliminating the glitch in a statistical manner
b) the solution to the bug lies inside m_RenderWindow->update(false) code in which case i suggest to create your own RenderWindow->update() routine( copy it from ogre's code ) and start removing code from it, until you focus on minimal code which fix the bug
4) the toughest suggestion: let it go( try meditation, therapy, whatever :lol: )
cheers...
the woods are lovely dark and deep
but i have promises to keep
and miles to code before i sleep
and miles to code before i sleep..

coolest videos link( two minutes paper )...
https://www.youtube.com/user/keeroyz/videos
User avatar
fractile
Gremlin
Posts: 199
Joined: Thu Jan 13, 2005 2:35 pm
Location: Tampere, Finland
x 15
Contact:

Re: Inconsistent behavior when using external OpenGL context

Post by fractile »

Found it! It was all caused by uninitialized mVisible variable in Ogre::GLXWindow. When it happened to get a false value, renderOneFrame() did not update the window.

Feels good to finally solve this after spending so much time struggling with this. Now on to the next problem: Broken anti-aliasing.
Post Reply