Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Trying to load textures on seperate thread, cannot bind context.  (Read 2069 times)
0 Members and 1 Guest are viewing this topic.
Offline superjoebob

Senior Newbie





« Posted 2011-05-14 00:11:03 »

I've been trying non stop for 2 days now and I'm about ready go insane over this problem. I'm trying to load textures on an alternate thread, allowing the main thread to display an animated loader to display progress. I'm aware of the issues this can lead to, but I've been reading around a lot (on this forum, mostly) and it sounds like it's possible.

The problem I'm having, is that no matter what I do, when I call GLContext.makeCurrent() in my loader thread with my shared context, I get one of 2 errors.

Here's a few things I've tried:
GLPbuffer buf = GLDrawableFactory.getFactory().createGLPbuffer( new GLCapabilities(), null, 1, 1, mContext);
mLoaderThread.context = buf.getContext();
mLoaderThread.start();

(mLoaderThread run())
mContext.makeCurrent(); -Error

Exception in thread "Thread-4" javax.media.opengl.GLException: wglShareLists(0x10000, 0x30001) failed: error code 0
   at com.sun.opengl.impl.windows.WindowsGLContext.create(WindowsGLContext.java:136)
   at com.sun.opengl.impl.windows.WindowsGLContext.makeCurrentImpl(WindowsGLContext.java:150)
   at com.sun.opengl.impl.windows.WindowsPbufferGLContext.makeCurrentImpl(WindowsPbufferGLContext.java:102)
   at com.sun.opengl.impl.GLContextImpl.makeCurrent(GLContextImpl.java:134)
   at pixelgame.core.CLoaderThread.run(CLoaderThread.java:55)

GLContext context = canvas.createContext( canvas.getContext() );
mLoaderThread.context = buf.getContext();
mLoaderThread.start();

(mLoaderThread run())
mContext.makeCurrent();-Error

Exception in thread "Thread-4" javax.media.opengl.GLException: Surface already locked
   at com.sun.opengl.impl.windows.WindowsOnscreenGLDrawable.lockSurface(WindowsOnscreenGLDrawable.java:157)
   at com.sun.opengl.impl.windows.WindowsOnscreenGLContext.makeCurrentImpl(WindowsOnscreenGLContext.java:57)
   at com.sun.opengl.impl.GLContextImpl.makeCurrent(GLContextImpl.java:134)
   at pixelgame.core.CLoaderThread.run(CLoaderThread.java:51)

I've also tried making my loading thread a GLCanvas object, and giving it an animator the same way my main thread is created. Without Threading.disableSingleThreading(), this would work, but the loader would block the main thread. When I tried using Threading.disableSingleThreading(), it started throwing the first error again. I've also tried creating the shared context in my constructor directly after creating the canvas, but this led to the same errors.

Obviously I don't know how to properly set up a shared GLContext and make it current on a thread. Any ideas what I could be doing wrong?





Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #1 - Posted 2011-05-14 00:31:13 »

IMHO it's much, much easier to keep all of your opengl calls on one thread and ignore the makeCurrent stuff, as it's horrendously error prone and tends to uncover bugs and quirks in drivers.

You can still do all the heavy lifting (like reading the image off disk and decoding it into RGBA bytes) in a seperate thread, and just keep the texture upload in the gl thread. That's pretty quick anyway, and if you have really big textures you can split them over multiple frames if you need to. This is what I do for texture loading and it doesn't impact the framerate at all.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline superjoebob

Senior Newbie





« Reply #2 - Posted 2011-05-14 04:30:37 »

Alright Orangy Tang, I'm convinced. If it's really that unsafe then I suppose I don't want it in my applet. I managed to get some pretty decent load times using your method.

Thanks  Grin!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Z-Knight

Senior Member


Medals: 1



« Reply #3 - Posted 2011-05-16 14:55:32 »

IMHO it's much, much easier to keep all of your opengl calls on one thread and ignore the makeCurrent stuff, as it's horrendously error prone and tends to uncover bugs and quirks in drivers.

You can still do all the heavy lifting (like reading the image off disk and decoding it into RGBA bytes) in a seperate thread, and just keep the texture upload in the gl thread. That's pretty quick anyway, and if you have really big textures you can split them over multiple frames if you need to. This is what I do for texture loading and it doesn't impact the framerate at all.

+1, I do 3D model loading and texture loading on a separate thread and update a variable flag that tracks when they are done. I then force a canvas display update and it checks if that flag is set to then take appropriate action - such as creating 3D objects, binding the textures, etc.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2011-05-16 18:15:40 »

+another, I do the same thing, as Orangy originally advised my long ago. Smiley

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (74 views)
2014-09-10 06:39:09

Tekkerue (38 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (28 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (35 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!