Menu

cann't create shared context properly

Using GLFW
tristam92
2016-02-10
2016-02-17
  • tristam92

    tristam92 - 2016-02-10

    Hi, everyone. I'm not professional in opengl. So I need your help.

    So, currently I've started new iteration of game engine(I'm working in game company). We decide to use as base for engine cocos2dx 3.7 and write some wrappers for our script system and etc.

    Now I want to add background loading of assets(texture and so on) using multi thread context of glfw.

    I added some changes to GLViewImpl::initWithRect. Now I passing callback that should get main context and from that context new one as shared.

    if (beforeFinalInitCallback) 
    { 
         (*beforeFinalInitCallback)(_mainWindow); 
    }
    glfwMakeContextCurrent(_mainWindow);
    
    and callback looks like this
    
    [](void* glWindow)
    {
      std::thread thread([glWindow]()
      { 
               auto context = glfwCreateWindow(1, 1, "", 0, static_cast<GLFWwindow*>(glWindow)); 
               glfwMakeContextCurrent(context); 
               context = glfwGetCurrentContext(); 
    
      });
    };
    

    Even if I remove thread wrapping(it will be the same thread as in initWithRect here), I still can't create context(return null pointer).

    But if I move that code to inside of initWithRect and get this

    std::function<void(void*)> pFunc = [](void* glWindow)
        {
    
            std::thread thread([glWindow]()
            {
            auto context = glfwCreateWindow(1, 1, "", 0, static_cast<GLFWwindow*>(glWindow));
            glfwMakeContextCurrent(context);
            context = glfwGetCurrentContext();
    
            });
        };
        pFunc(_mainWindow);
        glfwMakeContextCurrent(_mainWindow);
    

    it's start working. Context creating and set as current normaly.

    It's frustrating me. I can't get where is error or something like that. I checked all params(getting all pointers valid and right).

    Can please someone help me. I fighting with that thing 3 days already. I can't use TextureCache async methods cause I need render my scene with animation while another scene loading in background.

     
  • Doug Binks

    Doug Binks - 2016-02-10

    According to the docs for glfwCreateWindow the context to sare resources with may not be current on any other thread.

    So it may be simplest to make your contexts on the main thread (using an invisible window for one), and then make the invisible context current on your loading thread.

     
  • tristam92

    tristam92 - 2016-02-10

    But thats I want to achieve)). I cann't create shared context outside of library class(I wont leave library classes untouched to easy upgrade in future. I just can get what is wrong. shared window(invisble) can not be as current in my loading.

     
  • tristam92

    tristam92 - 2016-02-16

    So, any suggestions? or topic is closed?

     
  • Doug Binks

    Doug Binks - 2016-02-16

    As per my earlier response https://sourceforge.net/p/glfw/discussion/247562/thread/ebbe004d/#980a I think you should first try creating the windows on your current thread, then pass the context for the second window to the loading thread. This isn't what your earlier code appears to be trying to do - have you tried this approach yet?

     
  • tristam92

    tristam92 - 2016-02-17

    Yes, I tried this. But still has error. Context not setting as current for second thread.