Sharing opengl resources (OpenGL ES 2.0 Multithreading)

时间:2017-06-15 09:32:15

标签: c++ multithreading opengl-es

I have developed an OpenGL ES 2.0 win32 application, that works fine in a single thread. But I also understand that UI thread and a rendering thread should be separate.

Currently my game loop looks something like that:

done = 0;

while(!done)
{
  msg = GetMessage(..); // getting messages from OS

  if(msg == QUIT) // the window has been closed
  {
    done = 1;
  }

  DispatchMessage(msg,..); //Calling KeyDown, KeyUp events to handle user input;

  DrawCall(...); //Render a frame

  Update(..); // Update
}

Please view it as a pseudo code, cause i don't want to bother you with details at this point.

So my next step was to turn done into an std::atomic_int and create a function

RenderThreadMain()
{
   while(!done.load())
   {
      Draw(...);
   }
}

and create a std::unique_ptr<std::thread> m_renderThread variable. As you can guess nothing has worked for me so far, so i made my code as stupid and simple as possible in order to make sure i don't break anything with the order i call methods in. So right now my game loop works like this.

done.store(0);
bool created = false;

while(!done)
{
  msg = GetMessage(..); // getting messages from OS

  if(msg == QUIT) // the window has been closed
  {
    done.store(1);
  }

  DispatchMessage(msg,..); //Calling KeyDown, KeyUp events to handle user input;

  // to make sure, that my problem is not related to the fact, that i'm rendering too early.
  if(!created)
  {
      m_renderThread = std::make_unique<std::thread>(RenderThreadMain, ...);
      created = true;
  }

  Update(..); // Update
}

But this doesn't work. On every draw call, when i try to somehow access or use my buffers \ textures anything else, i get the GL_INVALID_OPERATION error code.

So my guess would be, that the problem is in me calling glGenBuffers(mk_bufferNumber, m_bufferIds); in the main thread during initialization and then calling glBindBuffer(GL_ARRAY_BUFFER, m_bufferIds[0]); in a render thread during the draw call. (the same applies to every openGL object i have)

But I don't now if i'm right or wrong.

0 个答案:

没有答案