Hello Guest

Loading in a separate Thread

  • 9 Replies
  • 9801 Views
Loading in a separate Thread
« on: May 12, 2011, 15:15:16 »
I'm trying to create a class that performs actions in a separate thread, and can report on its progress while doing so. The general threading I've gotten together, but since each thread has a different context it's not letting me load OpenGL resources (images, fonts, etc.), which is the primary intended purpose for this class.

I looked through the docs and a SharedDrawable seems like the logical thing to use, so I attempted the following:

Code: [Select]
protected void shareContext()
{
try
{
Drawable drawable = Display.getDrawable();
sharedDrawable = new SharedDrawable(drawable);
sharedDrawable.makeCurrent();
}
catch (LWJGLException e)
{
e.printStackTrace();
}
}

However, this causes the following exception:

Code: [Select]
org.lwjgl.LWJGLException: Could not share contexts
at org.lwjgl.opengl.WindowsContextImplementation.nCreate(Native Method)
at org.lwjgl.opengl.WindowsContextImplementation.create(WindowsContextImplementation.java:50)
at org.lwjgl.opengl.Context.<init>(Context.java:131)
at org.lwjgl.opengl.AbstractDrawable.createSharedContext(AbstractDrawable.java:30)
at org.lwjgl.opengl.SharedDrawable.<init>(SharedDrawable.java:50)

So, I think I don't really understand what I'm supposed to do to load OpenGL resources in another thread. Could someone explain how to do this a little better?

FYI, I'm using the Slick-Util texture loading methods.

*

Offline spasi

  • *****
  • 2216
    • WebHotelier
Re: Loading in a separate Thread
« Reply #1 on: May 12, 2011, 17:46:12 »
Could you try if the BackgroundLoadTest works on your machine? It's in the LWJGL test package, class: org.lwjgl.test.opengl.multithread.BackgroundLoadTest. If it works, then have a look at that code, it should be helpful. If not, this could be an LWJGL bug and I'd like more info to find out what happens.

Re: Loading in a separate Thread
« Reply #2 on: May 12, 2011, 18:03:29 »
Same problem:

Code: [Select]
Setting display mode to: 1024 x 768 x 16 @60Hz
-- Background Thread started --
** Sleeping, no texture created yet **
Exception in thread "Thread-1" java.lang.RuntimeException: org.lwjgl.LWJGLException: Could not share contexts
at BackgroundLoading.BackgroundLoader$1.run(BackgroundLoader.java:93)
at java.lang.Thread.run(Unknown Source)
Caused by: org.lwjgl.LWJGLException: Could not share contexts
at org.lwjgl.opengl.WindowsContextImplementation.nCreate(Native Method)
at org.lwjgl.opengl.WindowsContextImplementation.create(WindowsContextImplementation.java:50)
at org.lwjgl.opengl.Context.<init>(Context.java:131)
at org.lwjgl.opengl.AbstractDrawable.createSharedContext(AbstractDrawable.java:30)
at org.lwjgl.opengl.SharedDrawable.<init>(SharedDrawable.java:50)
at BackgroundLoading.BackgroundLoadTest$2.getDrawable(BackgroundLoadTest.java:205)
at BackgroundLoading.BackgroundLoader$1.run(BackgroundLoader.java:90)
... 1 more


*

Offline spasi

  • *****
  • 2216
    • WebHotelier
Re: Loading in a separate Thread
« Reply #3 on: May 13, 2011, 10:38:43 »
- Does it work with a pbuffer? Pass "PB" instead of "SD" to the test.

- Judging by the error message it seems like your GPU driver doesn't support WGL_ARB_create_context (OpenGL 3.0+). Is that correct? What GPU is it and what driver version is installed?

Re: Loading in a separate Thread
« Reply #4 on: May 13, 2011, 13:31:30 »
It does not support a pBuffer, either. I get the same error.

How do I detect GPU and driver? Both are Intel, I know that, and I have  the following information on the Graphics Control Panel:
Code: [Select]
Driver Version: 8.15.10.2202
Processor: x86 Family 6 Model 37 Stepping 5
Accelerator in use: Intel(R) HD Graphics

*

Offline spasi

  • *****
  • 2216
    • WebHotelier
Re: Loading in a separate Thread
« Reply #5 on: May 13, 2011, 13:44:10 »
It may be a driver issue or the GPU simply can't do it. Try updating the driver.

Re: Loading in a separate Thread
« Reply #6 on: May 13, 2011, 13:55:50 »
I'm trying. :\ Here's hoping my Dell will support an nVidia driver...

Re: Loading in a separate Thread
« Reply #7 on: May 13, 2011, 14:16:51 »
Well, apparently my PoS Intel Driver can't support most OpenGL 3.0 functionality. The latest driver for my hardware doesn't work.

However, since I would like to have this functionality supported, can someone tell me if this code works for loading Textures and sounds?

Code: [Select]
protected SharedDrawable sharedDrawable;
private static final Object lock = new Object();

public void run()
{
synchronized (lock)
{
shareContext();
// Do stuff
releaseContext();
}
progress = total;
}

protected void shareContext()
{
try
{
Drawable drawable = Display.getDrawable();
sharedDrawable = new SharedDrawable(drawable);
sharedDrawable.makeCurrent();
}
catch (LWJGLException e)
{
e.printStackTrace();
}
}

protected void releaseContext()
{
sharedDrawable.destroy();
}

I have no way of testing functionality of my end, apparently, so I'd like an affirmation that this is the correct way of doing this.

*

Offline spasi

  • *****
  • 2216
    • WebHotelier
Re: Loading in a separate Thread
« Reply #8 on: May 13, 2011, 14:42:42 »
Looks OK. I don't think you need the lock there btw.

I would advise against doing it blindly though. You really need to test this on a machine with an NV/AMD GPU. Multithreaded rendering (or even simple multithreaded resource loading) usually takes a lot of testing to get right.

Re: Loading in a separate Thread
« Reply #9 on: May 13, 2011, 14:58:58 »
Oh, really? It was in BackgroundLoader so I thought I'd better implement it.

I know, and I'll try to do some outside testing. Thankfully, the only thing I really want to have happen is loading (and not using) of the desired resources, and that shouldn't be too difficult to test.

I've altered the class to include checks if the threaded loading worked or not. This way, if the loading crashes, the developer can delegate to loading the images in the main Thread.