Mincus's picture

OpenTK GameWindow and multi-threading.

Using a GameWindow I tried to run another thread loading textures in the background whilst the main thread processed drawing to screen and discovered this won't work due to each thread needing its own OpenGL context (at least that's my understanding of the problem).

Just wondering if there's an easy way to set up a second thread that will share the context or am I better off using a GLControl maybe?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
renanyoy's picture

if there a way to do multithreading and background loading rendering with openTK window, as it's basic needed feature in all projects now ??

renanyoy's picture

if there a way to do multithreading and background loading rendering with openTK window, as it's basic needed feature in all projects now ??

Lazzu's picture
renanyoy wrote:

if there a way to do multithreading and background loading rendering with openTK window, as it's basic needed feature in all projects now ??

AFAIK there is, but I am not getting my implementation to work.

---

Debug info tells me, that two contexts are created and they are shared. However, my resources are not being shared. What I do is this:

1. Create GameWindow and call context.MakeCurrent(null); in main thread.
2. Create another GameWindow and call Context.MakeCurrent(window.WindowInfo); in background thread
3. Call MakeCurrent(); in main game window thread
4. Load resources in background thread
5. In main thread determine if the resources are loaded, and if they are, use them

The last step crashes the program when I am trying to draw polygons in main thread with VAO created in the background thread. OpenTK throws an exception: OpenTK.Graphics.GraphicsErrorException: InvalidOperation
The line that throws the exception: GL.BindVertexArray(mesh.VAO);
I made sure that the VAO is generated, but it seems it is not shared with the main thread from the background thread.

Have I misunderstood something, or am I doing something wrong? Or not doing something?

Lazzu's picture

I got it now working. I learned that VAO is not shared between contexts. Now I push everything to the GPU memory in the background thread, and after that is done, I create VAO in the drawing thread and start drawing.