LWJGL Forum

Programming => OpenGL => Topic started by: Kefik on May 06, 2006, 10:44:30

Title: Threading and LWJGL
Post by: Kefik on May 06, 2006, 10:44:30
Hi is there a way to execute GL commands (from GL11) in different thread then one which is running a window?

I would like to have object which is running OpenGL window but you can tell him to render different DrawLists from another threads. So I need to have access to commands like glGenLists / glBegin ... in another thread to do that.

Is there a way to do it?
Title: According to Wiki...
Post by: Kefik on May 06, 2006, 10:58:36
I have to release the context to allow other thread to acquire it.

To acquire context: Display.makeCurrent()
To release context: Display.releaseContext()

Problem is I can't find that method Display.releaseContext() in LWJGL v0.9.9 in Display class. Am I blind?
Title: Threading and LWJGL
Post by: Kefik on May 06, 2006, 11:13:11
Looks like that Display.releaseConext() should call context.releaseCurrentContext() ...
Title: Threading and LWJGL
Post by: Kefik on May 06, 2006, 15:11:36
As I thought...

I've compiled LWJGL and to org.lwjgl.opengl.Display added a static method releaseContext()

public static void releaseContext() throws LWJGLException{
   Context.releaseCurrentContext();
}

Now it seems to work ... but I don't know if this is the right way to do that.

Oh, and don't forget to do the synchronizaton between threads.
I've created a singleton DisplayMonitor, which I've been using as an argument for synchronized(DisplayMonitor.mutex) statement.

So it goes like this ... in each thread, where you want to use gl commands, use Display, or whatever do it like this:

synchronized(yourMonitor){
   Display.makeCurrent();

   ... do whatever you want ...

   Display.releaseContext();
}
Title: Threading and LWJGL
Post by: darkprophet on May 06, 2006, 22:10:34
Display.releaseContext() is for 1.0, not for 0.99. If your updating your logic from another thread, watch out for input :)

DP
Title: Input
Post by: Kefik on May 15, 2006, 06:40:11
Do you mean a phase when you're rendering the frame?

Anyway both "logic" and "renderer" is using a mutex so there can't be more threads in one time working with GL11 and it works just fine.

Is there something more a should be aware of?
Title: Threading and LWJGL
Post by: darkprophet on May 15, 2006, 22:24:28
You can't have multiple threads doing GL11 calls, well, technically you can, but you need to release the context and make it current on the new thread that needs GL (which is very slow)...

It is suggested you do all of your rendering in 1 thread. Your logic can be in another as long as it isn't updating data you're rendering at the moment (think translation and glTranslatef). You could be updating the the translation and youve just written to x when the rendering thread comes along and renders the geometry, your left with y and z not correct. Thats just 1 call, there are hundreds more...

DP
Title: Threading and LWJGL
Post by: funsheep on May 16, 2006, 04:40:42
i would suggest, you use one thread for rendering, and all other "logic-threads" tell him what to do.

so you need some sort of 3D object that contains information like translation, rotation, how to render, etc.
and some objects, that can be used to manipulate the 3D object (i'll call them Actions).

now the logic-threads can give the renderthread lots of Actions, which the renderthread stores in a queue. before each frame, the renderthread process every action and then draws the updated 3D objects with their translation, rotation, etc.

so you would avoid context-handling between threads (like darkprophet said: very slow), and you can aviod extensive synchronization, which also makes a program slow.