Threading and LWJGL

Started by Kefik, May 06, 2006, 10:44:30

Previous topic - Next topic

Kefik

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?

Kefik

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?

Kefik

Looks like that Display.releaseConext() should call context.releaseCurrentContext() ...

Kefik

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();
}

darkprophet

Display.releaseContext() is for 1.0, not for 0.99. If your updating your logic from another thread, watch out for input :)

DP

Kefik

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?

darkprophet

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

funsheep

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.