This is geared towards all the LibGDX users with the need for more performance!
I made an optimized version of the good old SpriteBatch, called TextureArraySpriteBatch.
It allows you to draw Sprites with different Textures within a single draw call. The old SpriteBatch issues a new draw call per Texture change which can lead to a large performance decrease on mobile.
It is a drop-in alternative with the same interface as SpriteBatch, no extra work needed if you want to use it!.
It should work on all platforms, but IOS and WebGL are untested right now.
Why do you need this?
The old SpriteBatch issues a new draw call every time you switch Texture, forcing you to use large Atlases and/or to sort by Texture unless you want your game to be bottlenecked by the amount of draw calls generated!
The new TextureArraySpriteBatch on the other hand uses a … drum roll … Texture Array to bind multiple Textures at once. This means that if you draw Texture A, then Texture B, then Texture C, it will bind 3 textures and then issue a single draw call (instead of 3). If you repeat this process 1000 times it will still only bind the 3 Textures once and then issue a single draw call! This gives a significant performance boost when using many textures, especially on mobile!
Most modern Desktop-GPUs offer at least 32 Texture slots, most mobile and integrated GPUs offer 8 Texture slots. The TextureArraySpriteBatch tries to use as many Texture slots as possible and will preferably swap out Textures that haven’t been used for the longest time if the number of slots isn’t enough, this is known as the Least Recently Used (LRU) strategy which is used very often in caches.
You can try this new SpriteBatch right friggin now:
- Download or just copy TextureArraySpriteBatch.java from Github
- Create the package/folder “com.badlogic.gdx.graphics.g2d” in your src directory
- Put TextureArraySpriteBatch.java into that package
- Use TextureArraySpriteBatch instead of SpriteBatch, the interface is the same!
I would be very grateful if you could give me some feedback! Does it work for you? Is it faster or slower in your use case?
Doesn’t matter if you’re using Desktop/Android/iOS or WebGL, all feedback is welcome!
I also opened an Issue on Github, check it out if you want to get some background info. I would like to have this integrated into LibGDX, so a bit of bug searching is always good.