Texture filtering: mipmaps

In my previous article about texture filtering I mentioned that scaling down images using  point sampling, or scaling to less than half size using linear filtering, looks bad because some source pixels are discarded.

Why exactly is this a problem?

Try this thought experiment:

We have a 256×256 texture, which is being scaled down by a factor of 1/256, so the entire image covers just a single screen pixel. What color should this pixel be?

If we use point sampling, only one pixel of the 256×256 image will be used, and the others discarded. This can give pretty random results depending on which specific pixel happens to be chosen!

Consider what happens if we tile our texture, so the scaled down image is repeated 100 times. If each repetition lines up exactly with one screen pixel, they will all choose the same source pixel, but if repetitions and screen pixels do not line up (maybe you are scaling down by 1/255 or 1/257 rather than 1/256, or using a 3D perspective projection), each repetition will end up choosing a different random location in the source texture. This is basically the same thing as:

    for each destination pixel:
        destination = sourceTexture.GetPixel(random.GetNext(), random.GetNext());

which is not going to give the best looking results  :-)

It would obviously be better if we could average all the pixels from the source texture, rather than having to choose just one of them. But averaging 256×256 color values is too slow to do in realtime.

When you can’t afford to compute something on the fly, it is time to precalculate…

A mipmap is a precalculated copy of an image that has been shrunk to a lower resolution using a more accurate, higher quality filtering algorithm than would be possible in realtime on the GPU. Mipmaps are arranged in a chain where each image is half the size of the previous one, for instance:

  • Original = 256×256
  • Mip 1 = 128×128
  • Mip 2 = 64×64
  • Mip 3 = 32×32
  • Mip 4 = 16×16
  • Mip 5 = 8×8
  • Mip 6 = 4×4
  • Mip 7 = 2×2
  • Mip 8 = 1×1

The GPU will automatically choose the appropriate image depending on how much the texture is being shrunk down.

To draw with mipmaps, you need two things:

  • You must include mipmap data when creating your textures (for instance by setting the Generate Mipmaps content processor parameter)

  • You must set SamplerState.MipFilter
    • TextureFilter.None = do not use mipmaps
    • TextureFilter.Point = low quality (but sometimes faster) mipmapping
    • TextureFilter.Linear = higher quality mipmapping
    • If you draw using BasicEffect, this will automatically set MipFilter = Linear

The difference between Point and Linear mip filtering is how the mipmap level is chosen:

  • MipFilter = Point
    • Chooses the appropriate mip level depending on how much the texture is being shrunk
    • If this is a fractional value, rounds up to the next larger mip level
    • This means the mip image may still need to be shrunk by a small amount
    • But linear filtering is good for shrinking as long as we don’t go below half size
    • To shrink below half size, we’d just choose a smaller mip level instead
    • There can sometimes be visible artifacts when switching between mip levels
  • MipFilter = Linear
    • Samples from both the next larger and next smaller mip levels
    • Interpolates between them based on how close the current image scale is to the two mip levels
    • Avoids artifacts along boundaries between mip levels
    • But we must now sample 8 rather than 4 source pixels for each destination, so this can cost more if your app is texture fetch limited

Let’s see this in action. Here is the linear filtered terrain from my previous post:


And now using mipmaps:


Note how the distant hills appear smooth and free from noisy aliasing.


Popular urban myth #1

"Mipmaps take up too much memory"

Actually, mipmaps use little extra memory, thanks to the power of powers:

  • Mip 1 = 1/4 the original size
  • Mip 2 = 1/16 the original size
  • Mip 3 = 1/64 the original size
  • Mip 4 = 1/256 the original size

If you continue this sequence, you’ll see that an entire mipmap chain all the way down to 1×1 takes up just 1/3 more memory than the original texture. So it’s a negligible overhead, especially once you get past that first mip.


Popular urban myth #2

"Mipmaps are slower for the GPU to render"

In fact, they are often much faster! If you aren’t scaling down a texture, having mipmaps won’t cost anything. But when you are scaling down, mipmaps can save crazy amounts of memory bandwidth.

Remember our example of a tiled 256×256 texture, where each repeat is being scaled down to 1×1 (a common situation in things like terrain rendering). Without mipmaps, every destination pixel will sample a radically different location in the source texture, so the GPU must jump around fetching colors from different areas of memory. GPUs typically have very small texture caches, relying on the fact that textures tend to be accessed sequentially, so this access pattern will thrash the cache and can bring even a high end card to its knees. But with mipmaps, the GPU can simply load a small mip level which will easily fit in the cache, and can then render many destination pixels without having to go back to main memory.

So mipmaps are not just for reducing aliasing: they can actually speed up rendering, too.

Shawn’s Recommendation™: if you are going to draw a texture in 3D, or planning to scale it down to less than half size, you should always use mipmaps.

Comments (20)

  1. Excellent article as always, thank you.

  2. fn2000 says:

    Do I need always generate all levels for mip-maps? Can I have lowest level let’s say to be at 16×16?



  3. ShawnHargreaves says:

    > Do I need always generate all levels for mip-maps? Can I have lowest level let’s say to be at 16×16?

    You can do that if you want, but why would you want to?

    Partial mipmap chains are hardly ever a useful thing in practice (excepting a couple of very specific cases where you are implementing manual shader filtering for sprite sheets).

  4. MrSoundless says:


    What if the original is 512×512?

    Would there still be 8 Mips which would result in mip 8 being 2px x 2 px, or would it scale down until it reached a mip where the texture is 1px x 1 px (which would be mip9 in this case) ?

    Thanks, great article

  5. ShawnHargreaves says:

    > What if the original is 512×512?

    A complete mip chain goes all the way down to 1×1, using however many images are necessary to get there.

    You can create partial mipmap chains if you want to stop at some larger size, but that is rarely a useful thing to do.

  6. Mi says:

    can you get access to a specific mipmap from the chain? For example I’d like to have access to the 1×1 texture to use as exposure average in hrd mapping

  7. ShawnHargreaves says:

    can you get access to a specific mipmap from the chain?

    Several of the sampler state values control how mipmaps are selected.

    You can also specify this explicitly using tex2lod in your pixel shader.

  8. tush says:

    i am rendering the scene onto the texture rather than backbuffer using setRenderTarget in one pass(i am using directx 9.0 shaders). then i am blending that texture with another one and getting the output onto the screen(thats on backbuffer) in second pass. everything is working fine i am also getting the o/p but the problem is the rendered scene which is stored on texture is getting shrunk while rendering on screen. i have also changed the texture size but its not working tried wid projection matrix also.

  9. Ken says:

    Hi Shawn, does the original texture have to have square dimensions, & does the length have to be a power of 2?

  10. ShawnHargreaves says:

    Square textures are not required by any modern hardware. Non pow2 support depends on if you target Reach or HiDef: blogs.msdn.com/…/reach-vs-hidef.aspx

  11. LEM says:

    Is it possible to use mipmaps to scale up?

    My original texture is 32×32 and I want to display it as 64×64 or higher? Can I do that?


  12. ShawnHargreaves says:

    > Is it possible to use mipmaps to scale up?

    You can scale up using texture filtering (your choice of point, linear, or anisotropic) but mipmaps aren't really applicable here.

    What would a precalculated 64×64 mipmap for a 32×32 source image be? That would basically just mean changing your source image to be 64×64 instead, which is entirely possible but not what we would normally call mipmapping!

  13. LEM says:

    It makes sense. Thanks, Shawn.

  14. Sibop says:

    I wasn't able to find much info on it, but I was curious about XNA's mipmap generation pipeline. Could you tell me if it does sRGB or gamma aware downsampling, say something around pow(2.2)? I was going to give it a try using a standard black and white striped texture, but figured you might be able to give more detailed info about it. Thanks.

  15. Fermin says:

    Excellent article.

    One question, what if I'm using a texture atlas, say with 4 possible textures in a single one?

    I guess the mipmap is just one for the whole texture, so it would cause a lot of glitches, right?

  16. ShawnHargreaves says:

    > what if I'm using a texture atlas?

    Funnily enough, my next post in this series talks about exactly that issue:


  17. Fermin says:

    >Funnily enough, my next post in this series talks about exactly that issue:


    There should be some sort of prev-next buttons, I came here by search engine so I was not aware of that!!

    Thank you twice

  18. Daisy says:

    the article is very easy to understand :) thx

  19. Andon M. Coleman says:

    "Mipmaps are arranged in a chain where each image is half the size of the previous one, for instance"

    More precisely, the mipchain reduces size 1/2 in each dimension per-reduced LOD. It would be true that a 1D mipchain is 1/2 the size for each LOD, but a 2D mipchain is 1/4 (0.5 * 0.5).

    You've actually gone and later demonstrated this for 2D textures when you discussed the size of mipmapped textures. The geometric series (1 + 1/4 + 1/16 + 1/64 + …) converges to 4/3.

  20. NANO says:

    I wanted to extract just the highest level of the mipmap 1×1, could you please provide a sample code as to how I could do that.