Introduction

Part 2 ended with a problem: when a texture is far away, each screen pixel covers many texels, but bilinear filtering only reads the four nearest the pixel’s centre. The rest are ignored, so the colour depends on exactly where the centre lands, and distant textures shimmer as the camera moves. This post covers the standard fix:

  • Mipmaps: a chain of smaller, pre-averaged copies of the texture
  • How the graphics card picks which copy to use
  • Trilinear filtering, which blends between copies
  • The LOD bias setting

Later, the ddx and ddy post on mipmaps shows exactly how the graphics card measures how many texels a pixel covers. This post focuses on the mipmaps themselves.

The Idea: Average in Advance

The right colour for a pixel that covers, say, a 4 Ă— 4 patch of texels is the average of all 16. Reading 16 texels for every pixel would be slow, and a pixel could cover thousands. So instead, the averaging is done once, in advance.

A mipmap chain (from the Latin multum in parvo, “much in little”) stores the texture at a series of sizes. Level 0 is the full texture. Level 1 is half the width and half the height, each texel the average of a 2 Ă— 2 block of level 0. Level 2 halves it again, and so on, down to a single texel holding the average colour of the whole texture.

A 256 Ă— 256 texture and its mip chain, made by averaging 2 Ă— 2 blocks

Tick Show every level at the same size to see each level magnified. Each one is a blurrier version of the one before. With Fine stripes, the stripes survive a few levels, then suddenly every texel is half black and half white, and the level becomes flat grey. That’s correct: from far enough away, fine stripes really should look grey.

The cost is surprisingly small. Each level has a quarter as many texels as the one before, so the whole chain adds 1/4 + 1/16 + 1/64 + … which comes to only one third more memory than the texture alone.

Choosing a Level

For each pixel, the graphics card estimates how many texels the pixel covers, by comparing its UV with its neighbouring pixels’ UVs: if the UV jumps a long way from one pixel to the next, each pixel must cover many texels. If one pixel step covers about one texel, it uses level 0. If it covers about 2 texels, level 1, where texels are twice as big; 4 texels, level 2; 8 texels, level 3; and so on. In general, the level is how many times you’d have to halve the number of texels per pixel to get down to 1 (mathematicians call this the base-2 logarithm). This number is called the level of detail, or LOD.

The LOD is usually a fraction, like 2.4, and there are two ways to handle that:

  • Nearest level: round to the nearest level and use bilinear filtering within it.
  • Trilinear filtering: use bilinear filtering in both neighbouring levels (2 and 3 here) and blend the two results, 60% level 2 and 40% level 3. That’s eight texels in all, blended in three directions: hence tri-linear.

Try It

The floor below repeats a texture many times into the distance. It scrolls by itself (press Pause to stop it): compare the filtering modes, and try the different textures.

A textured floor with different minification filters

Things to notice:

  • No mipmaps looks sharp in a still picture, but the distance is full of noise and swirling patterns (moirĂ©), and they crawl as it moves.
  • Mipmaps, nearest level is calm, but you can see lines across the floor where it suddenly switches level. They move with the camera, which is very distracting in a game.
  • Trilinear hides the switches completely. Choose Show mip levels as colours to see the bands that the blending smooths over.
  • The LOD bias adds a fixed amount to every pixel’s level. A negative bias sharpens textures, but brings the shimmering back; a positive bias blurs them. Games sometimes use a small negative bias when they render at a lower resolution and then upscale.

Trilinear filtering’s one weakness is that distant surfaces seen at a steep angle, like the far end of this floor, look too blurry. That’s because the level is chosen from the longest side of the pixel’s footprint: the patch of texture it covers. Anisotropic filtering, the next part, fixes that.

Under the hood

Mipmaps are usually generated offline, by the tools that prepare a game’s textures, using better filters than a simple 2 Ă— 2 average, then stored compressed. They can also be generated when the game loads, with a single call in most graphics APIs, which the driver usually implements by running its own small shader on the graphics card. Mipmaps also make rendering faster: a distant surface that reads small levels touches far less memory, and neighbouring pixels read neighbouring texels, so the texture cache works much better. Without mipmaps, every pixel of a distant surface jumps to a different, far-apart spot in a large texture.

Summary

  • A mipmap chain stores the texture at half, quarter, eighth size and so on, each pre-averaged, for only â…“ more memory.
  • The graphics card picks a level from how many texels a pixel covers, measured by comparing neighbouring pixels’ UVs.
  • Trilinear filtering blends the two nearest levels, hiding the switch from one to the next.
  • Without mipmaps, distant textures shimmer; with them, they’re stable, faster and slightly blurry.

Further Reading