Search code examples
opengllibgdxtexturesgpuimage

How does TextureFilters (MipMap generation) affect performance?


I've been reading that choosing texture filter (specifically MipMap generation) may have an impact on performance of the application but I can't really get my head around how it works. So a few questions:

  1. Are MipMaps generated every frame or only once when the texture is loaded?
  2. If every frame, does it still get regenerated if the scene is static (the texture size and position is constant) from one frame to another. Like if you have a static UI does that perform worse when using MipMap filtering?
  3. If only once, why does it affect performance, and in what way?

I'm wondering since I've discovered that everything looks a lot better when using (in LibGDX):

param.genMipMaps = true;
param.minFilter = TextureFilter.MipMapLinearLinear;
param.magFilter = TextureFilter.Linear;

But why isn't this standard/best practice? What are the drawbacks? If, for my application, it doesn't reduce fps, are there any other drawbacks? More GPU/CPU intensive? Higher battery consumption (for mobile devices)?


Solution

  • Mipmaps have to be generated whenever the texture data changes. As long as the texture doesn't change, there is also no need to recreate them.

    They influence performance because the read operation for every texel gets slower. Depending on which filter type you use, the GPU has to read multiple texels from multiple mipmap levels to calculate the final color.

    For example, GL_NEAREST will only read 1 texel and return that. GL_LINEAR will already have to read 4 texels from the one mipmap level and perform a bilinear interpolation. If you now enable mipmapping, then also information from a second texture level will influence the outcome. With GL_LINEAR_MIPMAP_LINEAR, the GPU will do a linear lookup (4 texel) in the mipmap level greater than the required size and one linear lookup in the mipmap level smaller than requested. The result from these two lookups will then be interpolated to gain the final color. So all in all, a GL_LINEAR_MIPMAL_LINEAR lookup might read 8 texels and perform 2 bilinear interpolations and a linear interpolation between the two levels (trilinear interpolation).

    Another consideration is GPU memory consumption. Mipmaps need to be stored somewhere on the gpu and take up approximately 1/3 more space than without mipmaps.

    For more details about Mipmapping one should also read the Wikipedia Article which explains the concept very well. As stated by others in comments, also this blog gives a good overview about texture filtering methods.

    Note, that the explanation here assumes 2-dimensional textures. Also note, that graphics cards may very well optimize the process, but the technique described is how it works in theory.