Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've been trying to come up with a good solution for preloading textures asynchronously, but in Android with OpenGL ES you cannot create a texture out of context.

Would the proper solution be to load up a ByteBuffer with data from my png image, then when I'm back on the render thread call glTexImage2D? What I don't understand is whether or not OpenGL will actually copy the buffer into it's own location, or directly access mine. The latter approach would be ideal.

Could anyone give me clarification?

share|improve this question
    
Why exactly you want to load textures on another thread? It is so much simpler to let this on the main context one, though you may have a good reason to do it. –  AlphSpirit Jul 22 '14 at 17:54
1  
Loading textures -- or anything large -- from disk is unpredictable (often slow). It's generally better to defer such tasks to a worker thread so as not to stall the main thread, interrupting UI interaction. –  Josh Petrie Jul 22 '14 at 18:18

1 Answer 1

up vote 0 down vote accepted

Would the proper solution be to load up a ByteBuffer with data from my png image, then when I'm back on the render thread call glTexImage2D? What I don't understand is whether or not OpenGL will actually copy the buffer into it's own location, or directly access mine. The latter approach would be ideal.

This is basically what you want to do. Load the texture data from a worker thread, and once the load is complete construct the OpenGL texture resource (from that data) on the main/render thread.

Once you call glTexImage2D, you don't need to keep your copy of the texture data in-memory any longer (you can delete it) unless you need it for some other purpose. OpenGL will copy the data it needs elsewhere.

share|improve this answer
    
Ah darn I wish OpenGL just pointed to my buffer so I could keep it around. I guess it's fairly simple just to load it asynchronously and pop it into OpenGL and keep it there. Thanks –  Josplode Jul 22 '14 at 19:35
    
"Just pointing to the buffer" would be pretty difficult from a hardware design perspective, unfortunately, since the GPU is a discrete unit of hardware. –  Josh Petrie Jul 22 '14 at 20:06
    
Well for a PC, but I had the idea that Android phones just use regular RAM for video memory. I could be mistaken –  Josplode Jul 22 '14 at 20:12
    
Wouldn't matter -- you're talking about a core OpenGL API which would want to maintain cross-platform abstractions. Anything that would do something like what you are describing (and it might be possible, just not via glTexImage2D) would be an extension. You should perhaps browse the extensions registry, you may find something useful. –  Josh Petrie Jul 22 '14 at 20:25
    
cf. "texture swizzling" and related topics. The format of the image loaded from disk may be suboptimal for access by the hardware, so being able to rewrite the texture can provide significant performance improvements. So not only would avoiding the copy be a restrictive API design for non-UMA systems, it could also cause a performance hit. –  fadden Jul 28 '14 at 14:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.