Hello Guest

Threading and Swing with LWJGL

  • 8 Replies
  • 16537 Views
Threading and Swing with LWJGL
« on: October 22, 2010, 13:30:32 »
Recently my friend and I decided to start a small game project using LWJGL. We had a working game, but we were having issues where if a portion of the code started to slow down when under heavy processing. We decided to separate that into a thread so that it can run that on a seperate CPU and stop choking the rendering thread. After doing this, all of the swing processing starting acting totally weird. Upon launching the application all swing controls are running and my glcanvas is updating. The swing controls are totally functional up to this point. As soon as I try to click in the GLCanvas or press a key while the mouse is hovering over the glcanvas the Swing controls stop responding. None of the input will work inside the GLCanvas either. The glcanvas will continue to run, but cannot be interacted with. If at any point you try to drag the window it freezes everything and crashes. All of this happens whether or not the Inputs have been created. I've tried launching the threads inside a SwingWorker.invokelater method, but that didn't help.

Is this just a bad idea; trying to thread long running background processes? I know the overhead of context switching can be pretty heavy, but I was thinking it would be nice if you could start loading the next part of the map into the tree on the other unused processor, or maybe if the rendering thread started to bog down we could keep the physics thread updating at a usable pace. Is this a Swing thing? I'm starting to wonder if I'm creating an anti-pattern... Any insight will be greatly appreciated.

Re: Threading and Swing with LWJGL
« Reply #1 on: October 22, 2010, 15:58:17 »
This is part of the threading issues encounered when rendering canvas outside of swing edt.
There are two mehtods: either use Display.update (be sure to have delegated to the cvs with setparent(-)within a user thread or call canvas.update(graphics) from within the swing edt (better with invokeandwait, or you may be out_of_sync) .
I use for myself the second method, as i m sure to keep control over both ogl thread and edt. But this leads to some issues with capturing swingevents events (should be fast and reliable by using pools and stacks). :D

Re: Threading and Swing with LWJGL
« Reply #2 on: October 22, 2010, 16:11:23 »
Why not get rid of Swing? Just make a pure LWJGL app using Display.create(). You can use TWL for the user interface.

If you need a resizable window try Display.setParent(). See TWL Theme Editor's Main for details on how to use it.

Re: Threading and Swing with LWJGL
« Reply #3 on: October 22, 2010, 16:47:24 »
@BroumBroum: I'm going to try method two and see what happens (though I probably won't be able to until later tonight)

@Matthias: TWL has been under consideration for some time, but are trees supported? It would take a significant interface rewrite, but it certainly does look a lot cleaner. Trees aren't really a show stopper as the only thing I use them for is for texture/entity browsing. Should I just use a table with a combo box perhaps?

Thanks for both of your comments; I think TWL will probably be what we settle with, but I would really like a temporary fix for swing so I can get back to work on my sparse voxel octree rather than rewriting the interface right this second. Hindsight is 20/20, but I really wish we had started with TWL or something like it from the beginning. Swing is a real pain...

Re: Threading and Swing with LWJGL
« Reply #4 on: October 22, 2010, 17:07:44 »
TWL has a TreeTable class - it displays a tree in the first column. If you only show a single column then you have your classical tree widget :) Take a look at the theme editor (webstart) it uses the TreeTable. You need a theme to test the theme editor, just download the TWL Examples source or the TWL Theme Editor source and load one of these themes.

Re: Threading and Swing with LWJGL
« Reply #5 on: October 22, 2010, 20:37:23 »
... The glcanvas will continue to run, but cannot be interacted with. If at any point you try to drag the window it freezes everything and crashes. All of this happens whether or not the Inputs have been created. I've tried launching the threads inside a SwingWorker.invokelater method, but that didn't help.
...
I've got one question to you : did you monitor this issue with JConsole ? Seems like some thing happens in the native java API or your graphics driver.
Moreover this reminds me the ATI driver issue with a volatile frame buffer a year or two ago. ::)

*

Offline basil

  • **
  • 81
Re: Threading and Swing with LWJGL
« Reply #6 on: October 22, 2010, 23:09:34 »
i bet your controls freeze up cos' you dont poll the devices.

Code: [Select]
Display.processMessages();
multithreading is a good idea basicly. ensure all swing calls are made in EDT, it goes crazy sometimes otherwise. substance look&feel helps with that. -> http://www.pushing-pixels.org/?p=368

and dont call graphics2D methods from diferent threads :)
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html

.. anyway, isn't that glcanvas thingy bugged anyway ?

Re: Threading and Swing with LWJGL
« Reply #7 on: October 23, 2010, 10:40:56 »
...
multithreading is a good idea basicly. ensure all swing calls are made in EDT, it goes crazy sometimes otherwise. substance look&feel helps with that. -> http://www.pushing-pixels.org/?p=368

and dont call graphics2D methods from diferent threads :)
...
I agree, Swing UI requires to strictly rely on the EDT. Indeed, whenever the AWTTreeLock is invoked, EDT should be in use.
Quote
.. anyway, isn't that glcanvas thingy bugged anyway ?
Not at all, but it behaves as an usual Canvas, where at the same time opengGL requires that if it is created (new AWTGLCanvas()) on another Thread than EDT, then everything must stay on that Thread, which is definitely NOT possible, as said above about Swing UI. ;)
Hence, Display seems to me "a priori" not to be the safest way (not sure about what Thread is used to create the GLContext) if you want to integrate the Canvas into a Swing UI, though I can be mistaken.
« Last Edit: October 23, 2010, 10:44:50 by broumbroum »

*

Offline basil

  • **
  • 81
Re: Threading and Swing with LWJGL
« Reply #8 on: October 23, 2010, 11:03:42 »
aaah right, that was the AWTGLCanvas.