Improving Texture Loading

Hi

In my project I have to load a texture that is a spritesheet used for a [icode]Theme[/icode] for my GUI controls. The theme loads fine, but it takes about 30ms (only 1ms for theme creation, 29ms for texture loading) so I would like to speed up my texture loading. As of now I load many of my textures through serialized objects where the png data is in a byte array. However the process still takes too long. I was hoping someone on JGO could help me find some ways to speed up the efficiency of my code.

Here is the pastebin for my Texture class, my ObjectLoading class, and my SerializableBufferWrapper class: http://pastebin.java-gaming.org/f3717614b0e19

CopyableCougar4

First question: why is that too slow? 30ms is a 33rd of a second. That seems pretty quick to me for what sounds like a 1-time operation.

It’s too slow because it happens before anything is rendered but after the display is created. I will live with it if there aren’t ways to speed it up, but it would be nice to speed it up.

CopyableCougar4

Use smaller texture or stream it.

If by streaming it you mean using a FileInputStream, I am because of the deserialization, but if you mean something else then please clarify.

CopyableCougar4

If it’s really that noticeable then surely you can read the files in on a separate thread before or while the display is being created.

Otherwise first thing is if your’e using serialization make sure to wrap the FileInputStream in a BufferedInputStream and then the ObjectInputStream, should be considerably faster.

Wrapping the File streams in Buffered streams shaved about 5-10ms off the process so far :slight_smile:

CopyableCougar4

Really the fastest way would be to store the raw buffers in files and read them in with an NIO FileChannel. No serialization or anything.

How would I store the dimensions of the texture? Would I just write those to the same buffer?

CopyableCougar4

You could dedicate the first X bytes to header information like that, read it in any way you like, and then read the rest into the buffer, or use a separate properties file.

Hello

Maybe you should display something (a message with a progress bar for example) that doesn’t need any resource stored in a file while loading a few files when the end user launches your game. You can still improve texture loading but imagine that you have to load a lot more images, you’ll need to indicate that you’re loading something instead of just showing a black screen.

Unless you need to load more than 10 of these images, I don’t see any problem… Loading takes 33ms? You would load 33 images in 1 second. How is that slow? A lot of games take at least 10 seconds to load at the start of the game and they keep reloading stuff when already playing, which takes more time. I bet you don’t have more than 5 images…

Maybe this could be useful: http://www.java-gaming.org/topics/tutorial-stutter-free-texture-streaming-with-lwjgl/32571/view.html

Simple fix: Draw a loading screen BEFORE you load your images.

@Longarmx That’s a bit what I suggested.

@theagentd Nice tutorial but it works with OpenGL drivers reliable enough to manage multi-threading.

Edit.: Sven does something similar in the JOGL media player:
http://forum.jogamp.org/transfer-images-from-TextureData-to-a-Texture-off-of-the-EDT-in-JOGL-tp4032678p4032694.html

I fully agree. Even if he got texture loading down to a few ms, he’d eventually reach a point where loading all his textures takes noticeable time. Unless of course his game consisted of a dozen or so images total. So a loading screen is more or less inevitable no matter how fast you load your textures.