0

I have an application with a main window. It has it's own thread and opengl context. This application also has one worker thread per processor to create and upload software rendered textures. This works perfectly well.

My worry is that the worker thread opengl contexts are created with the device_context of the main window. So in the case of a dual processor system, that means that 3 opengl contexts are bound to the same window device_context. They are all created in the main window thread, then sharelist is called to make them share textures, and then each of the gl_context is activated in only one thread, one for the main window thread, one for each worker thread.

As I wrote earlier, this works perfectly on all the machines I've tested it on but I've seen no documentation that describes what I am doing, and the only confirmation that what I'm doing is fine I've found is the last comment on this link: OpenGL multiple rendering contexts in one window .

So, I was hoping someone was aware of confirmation that this will work on all systems.

edit: I forgot to mention it earlier, but there is one issue, it is that if both worker threads are calling wglmakecurrent(common_window_device_context,private_opengl_context) simultanously, one of them will fail with glgeterror=GL_INVALID_OPERATION . The solution is in that case to call sleep(1) to allow time for the same call in other thread to finish and then call wglmakecurrent again and then everything works super smooth. At least on all machines I've tested.

2
  • 2
    I wouldn't call sleeping a solution to this problem. You want an actual critical section to prevent two threads from doing this at the same time. Commented Apr 13, 2014 at 1:00
  • Good point, it was a quick fix I implemented friday, I'll do that on monday. Any idea on the main question though? Seems to work and uses minimal resources but ?? Commented Apr 13, 2014 at 1:18

1 Answer 1

1

So, I was hoping someone was aware of confirmation that this will work on all systems.

It's perfectly fine to have multiple OpenGL contexts from different threads being active on the same device context. The worst that can happen is, that the interaction of OpenGL synchronization points creates severe performance problems and undefined rendering results. This is only a problem if you were making render calls from multiple OpenGL contexts. However you seem to want to use them purely for I/O work, i.e. loading textures of VBO data. As long as you don't from the render thread refer to the data objects manipulated by the worker threads no synchronization point side effects happen, and you're in the clear.

The exact semantics of multiple context interaction are defined in the OpenGL-3 and later specification. I suggest you read it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.