views:

1120

answers:

2

Currently I am loading an image in to memory on a 2nd thread, and then during the display loop (if there is a texture load required), load the texture.

I discovered that I could not load the texture on the 2nd thread because OpenGL didn't like that; perhaps this is possible but I did something wrong - so please correct me if this is actually possible.

On the other hand, if my failure was valid - how do I load a texture without disrupting the rendering loop? Currently the textures take around 1 second to load from memory, and although this isn't a major issue, it can be slightly irritating for the user.

+2  A: 

You can load a texture from disk to memory on any thread you like, using any tool you wish for reading the files.

However, when you bind it to OpenGL, it's going to need to be handled on the same thread as the rendering for that OpenGL context. That being said, this discussion suggests that using a PBO in a second thread is an option, and can speed up the process.

Reed Copsey
First paragraph not so helpful, sort of repeating what I said hehe ;) but the 2nd paragraph looks very helpful, thanks for the link! I'll certainly check it out :)
nbolton
Just saying that what you were doing there is valid - I wasn't sure from how you wrote it whether that was the case. Good luck!
Reed Copsey
Understood. Thanks! :)
nbolton
A: 

You can certainly load the texture from disk into RAM in any number of threads you like, but OpenGL won't upload to VRAM in multiple threads for the reason mentioned in Reed's answer.

Given the loading from disk is the slowest part, thats the bit you'll probably want to thread. The loading thread(s) build up a queue of textures to be uploaded, then this queue is consumed by the thread that owns the GL context (mind your access to that queue by the various threads however). You could also consider a non-threaded approach of uploading N textures per frame, where N is a number that doesn't slow the rendering down too much.

Justicle
@Justin, thanks for taking the time to answer. Perhaps my question is phrased badly; at present the heavy duty loading is done in the 2nd thread (this takes about 10 or so seconds to load because there's some image processing) then it takes only a brief moment to copy the pixels into VRAM during the GLUT loop; however while brief it does take an "uncomfortable" amount of time in terms of user experience.
nbolton
Okey, that makes sense - post an answer here when you figure out what works for you!
Justicle