Managing and Understanding Texture Fragmentation

Godot Version

4.3

Question

Please correct me if I am wrong.

From what I understand, when you load a texture into vram, it will be put into a contiguous space.

If certain textures got unloaded, the space that was occupied before will now be empty. However, this leaves gaps between other textures that do not get unloaded.

As the game runs further, the player could be going into new areas or meet new objects which require loading the respective textures into vram. During loading the textures, the system will try to fit the textures into contiguous spaces.

But as the player play the game further and further, having more loading and unloading would lead to more fragmentation if the game is the type that loads and unloads on the fly.

Because of this, here are my questions:

  1. How does the engine manage which position on vram to put the texture on? For example, suppose we have a 1 GB VRAM and we just started the game and we are about to load our very first texture, we allocate a 10 MB texture, will the engine put this in the first byte of VRAM up to the 10Mth byte or does it have its own process knowing which range of bytes on vram are empty and it will put textures that it thinks is the best position?

  2. In our Godot code, we simply load and unload the textures, but can we do anything about fragmentation if it occurs? For example, if I have a loading screen in the game that deallocate all textures and re-allocate only the textures that will definitely be used back, will this solve fragmentation?

  3. In Godot, what would be a proper way to manage this?

I don’t really know how Godot handles it, but there are strategies you can use if you’re worried about it.

One of the main things you could do is used uncompressed textures and ensure you use a few fixed (probably power of two) sized textures. If all your textures are (say) 256x256 or 1024x1024 and all RGBA8888, there will only be two sizes of texture, so new incoming textures will fit perfectly in the holes left behind by evicted textures.

A gigabyte is a lot of VRAM though, unless you’re doing something fairly texture-intensive. It is, for example, 256 texture pages at 1024x1024x32bpp. Your game may need more than that, in which case you might need to care, but you might want to work out roughly what your game needs before you worry about whether it will fit.