Search

Search papers and lessons

Mipmap

Level 2 · 12 min read

A mipmap is a stack of pre-shrunk copies of a texture, so a renderer can pick the copy that matches how small a surface appears on screen and avoid shimmering.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • Probability
  • Electromagnetism
  • Electrical engineering
  • ReadReady to readNot yet

TL;DR

A mipmap is a stack of copies of one texture, each half the width and height of the one before. When a textured surface is drawn small on screen, the renderer reads from a smaller copy, which holds the average colour of the detail it replaces. This stops the picture from flickering as the camera moves, and it costs only about a third more memory than the texture itself. It is the standard way games and graphics hardware draw surfaces at any distance.

Builds on: Image Pyramid, Bilinear Interpolation, Logarithm, Anti-Aliasing · Leads to: texture filtering in graphics hardware, and anisotropic filtering for surfaces seen at a steep angle.

Where it's used

Video games. A game world has walls, roads and terrain covered with textures of brick, asphalt or grass, often 1024 or 2048 texels wide. Seen up close each texel is a few screen pixels; seen far away, a whole tile of the wall fits into one pixel. The renderer chooses the mipmap level from the distance, so the far wall shows an average grey-brown instead of a random sample of bricks.

Flight and driving simulators. Terrain is drawn across many kilometres. The ground near the aircraft uses the full-resolution texture, and the horizon uses levels that are tiny in memory and smooth on screen. Without mipmaps, the horizon would shimmer as the aircraft moved.

Web maps. Map services such as Google Maps serve their imagery as a pyramid of tiles, one set per zoom level. Each zoom level has four times as many tiles as the one above, and the viewer requests the level that matches the zoom. This is the same stack of half-size copies, applied to map tiles rather than to a texture on a surface.

How it works

Take a texture that is one row of eight texels, alternating dark and bright: 0, 8, 0, 8, 0, 8, 0, 8. A texel is one cell of a texture, the way a pixel is one cell of a picture. Suppose the texture is drawn on a surface so small that one screen pixel covers four texels.

  1. Build the mipmap once, before drawing. Level 0 is the original row. Level 1 averages each pair of neighbouring texels: (0+8)/2=4(0 + 8)/2 = 4 for every pair, so level 1 is [4,4,4,4][4, 4, 4, 4]. Check: level 1 has half as many texels, and its average is still 4.
  2. Keep halving until one texel is left. Level 2 is [4,4][4, 4], and level 3 is [4][4]. Check: each level is half the size of the one before, so the stack has four levels.
  3. Measure how many texels fall under one pixel. Here it is four. The method takes the base-2 logarithm of that number: log⁡24=2\log_2 4 = 2. This value is called the level of detail. Check: a pixel covering one texel gives log⁡21=0\log_2 1 = 0, the original level.
  4. Read from the level that matches. Level 2 has one texel for every four original texels, which is the same scale as the pixel. Read it and get 4. Check: the true average of the four texels under the pixel is (0+8+0+8)/4=4(0 + 8 + 0 + 8)/4 = 4, so the answer is right.
  5. Compare with reading from level 0. Reading the original row at one spot returns 0 or 8, depending on which texel the pixel lands on. As the camera moves, the answer jumps between them. This jumping is aliasing, the shimmer that mipmaps remove. Check: the level-2 answer stays 4 wherever the surface moves within that pixel.

Step 4 needs the level chosen by the logarithm, so the rule is: the number of texels under the pixel is ρ\rho, the level is d=log⁡2ρd = \log_2 \rho, and the renderer reads the level nearest dd.

1234567800.511.522.53texels under one pixel (ρ)mipmap level d
  • level of detail d = log2 of the ratio
Each doubling of the texels under a pixel moves the renderer up one mipmap level. A ratio of 4 reads level 2.
flowchart LR
  A["Texture: level 0"] --> B["Average pairs: level 1"]
  B --> C["Average pairs: level 2, and so on to one texel"]
  D["Texels under one pixel"] --> E["Level of detail: log2 of the count"]
  E --> F["Read the matching level"]
  C --> F

The math

Definition: Mipmap chain

Let M0M_0 be the texture, with NN texels across. The level MlM_l is made from level Ml−1M_{l-1} by averaging each 2×22 \times 2 block of texels:

Ml(i,j)=14∑a,b∈{0,1}Ml−1(2i+a,  2j+b)M_l(i, j) = \frac{1}{4} \sum_{a, b \in \{0, 1\}} M_{l-1}(2i + a,\; 2j + b)

Level ll has N/2lN / 2^l texels across, so the chain ends at level log⁡2N\log_2 N, where one texel is left. A texture with N=2kN = 2^k texels across has k+1k + 1 levels.

The 2×22 \times 2 average is the box filter from Anti-Aliasing, a Convolution with equal weights on each texel of the square. Averaging before sampling is exactly what removes the detail that a smaller grid cannot show, the limit set by the Nyquist sampling limit. In one dimension this is the pairwise average from step 1.

Steps 1 and 2 in symbols: build the chain

For the walkthrough, M1(i)=12(M0(2i)+M0(2i+1))M_1(i) = \frac{1}{2}\big(M_0(2i) + M_0(2i+1)\big). With M0=[0,8,0,8,0,8,0,8]M_0 = [0, 8, 0, 8, 0, 8, 0, 8], every pair sums to 8, so M1=[4,4,4,4]M_1 = [4, 4, 4, 4].

Step 3 in symbols: choose the level

The number of texels under a pixel is ρ\rho. The level of detail is

d=log⁡2ρd = \log_2 \rho

For ρ=4\rho = 4 this is d=2d = 2. Each doubling of ρ\rho adds one to dd, which is why the levels halve in size.

Step 4 in symbols: blend between levels

If d=2d = 2 exactly, read level 2. If dd falls between two levels, say d=1.5d = 1.5, blend the two neighbouring levels. Write t=d−⌊d⌋t = d - \lfloor d \rfloor for the fraction past the lower level, sks_k for the value read from level kk, and then

v=(1−t) s⌊d⌋+t s⌊d⌋+1v = (1 - t)\, s_{\lfloor d \rfloor} + t\, s_{\lfloor d \rfloor + 1}

This is a straight-line blend, the same step that Bilinear Interpolation takes in one direction, applied across levels as well as across the texels inside each level. Blending across two levels is called trilinear filtering.

Memory

Level ll has 1/4l1/4^l of the texels of level 0. The total is

∑l=0∞14l=11−1/4=43\sum_{l=0}^{\infty} \frac{1}{4^l} = \frac{1}{1 - 1/4} = \frac{4}{3}

So the full chain is about one third larger than the base texture. The sum is a geometric series with ratio 1/41/4, and it gives the same total of about 4/34/3 that the Image Pyramid lesson finds for images.

Worked examples

Example
1. Tiny: a texture of four texels

Texels [0,8,0,8][0, 8, 0, 8]. Build the chain and read the level for a pixel covering two texels.

  1. Level 1: (0+8)/2=4(0 + 8)/2 = 4 and (0+8)/2=4(0 + 8)/2 = 4, so level 1 is [4,4][4, 4].
  2. Level 2: (4+4)/2=4(4 + 4)/2 = 4, so level 2 is [4][4].
  3. The pixel covers two texels, so ρ=2\rho = 2 and d=log⁡22=1d = \log_2 2 = 1.
  4. Read level 1: the value is 4.
  5. Interpretation: the true average of the two texels under the pixel is (0+8)/2=4(0 + 8)/2 = 4, so the level gives the right answer for this pixel.
Example
2. Standard: distance changes the level

Texels [0,8,0,8,0,8,0,8][0, 8, 0, 8, 0, 8, 0, 8], with the chain from step 1 of the walkthrough.

  1. A pixel covering one texel: ρ=1\rho = 1, d=0d = 0. Read level 0, which gives 0 or 8 depending on the position.
  2. A pixel covering two texels: ρ=2\rho = 2, d=1d = 1. Read level 1: 4.
  3. A pixel covering four texels: ρ=4\rho = 4, d=2d = 2. Read level 2: 4.
  4. A pixel covering eight texels: ρ=8\rho = 8, d=3d = 3. Read level 3: 4.
  5. Interpretation: as the surface moves away, the pixel covers more texels, the level rises, and the answer settles at the average 4. The surface looks grey instead of flickering between 0 and 8.
Example
3. Realistic: a 2048-texel wall

A brick wall texture is 2048×20482048 \times 2048 texels, with 4 bytes per texel (red, green, blue and alpha). The wall fills a region of the screen that is 512 pixels wide.

  1. The base texture is 2048×2048×4=16,777,2162048 \times 2048 \times 4 = 16{,}777{,}216 bytes, which is 16 MiB.
  2. The number of levels is log⁡22048+1=12\log_2 2048 + 1 = 12, from 2048 texels across down to 1.
  3. The whole chain is 4/34/3 of the base, so 16×4/3≈21.316 \times 4/3 \approx 21.3 MiB. The extra memory is about 5.3 MiB.
  4. The wall spans 512 pixels and the texture has 2048 texels across, so ρ=2048/512=4\rho = 2048 / 512 = 4.
  5. The level is d=log⁡24=2d = \log_2 4 = 2. Level 2 is 512×512512 \times 512 texels, so it has exactly one texel for every pixel across the wall.

Interpretation: at this distance the renderer reads a texture with the same resolution as the screen, and the extra memory of the chain is a few megabytes. Try it: a 40964096-texel-wide texture is drawn across 256 screen pixels. Which level is used, and how wide is that level? Answer: ρ=4096/256=16\rho = 4096 / 256 = 16, so d=log⁡216=4d = \log_2 16 = 4, the level is 4, and it is 4096/16=2564096 / 16 = 256 texels wide.

Common mistakes and tradeoffs

It is tempting to think that a mipmap makes a texture look sharper. In fact it does the opposite on purpose. Far from the camera the level is blurred, because averaging is the only correct answer for detail too small for the screen. The sharp texture is still shown close up, at level 0.

It is tempting to think a mipmap is the same as resizing the texture on each frame, a smooth resize of the kind Bilinear Interpolation performs. In fact the levels are built once, and then only read. Resizing from the original for every surface on every frame costs far more work than a lookup.

It is tempting to think the extra memory is large. In fact the chain adds one third, so a texture costs 4/34/3 of its base size, not twice. The saving in shimmer and in sampling cost is usually worth the third.

Tradeoff: a square filter blurs slanted surfaces. A floor seen at a grazing angle covers many texels in one direction and few in the other. A single level chosen from the larger count is too blurry along the short direction. Anisotropic filtering, a separate technique, takes several samples along the long direction to fix this, at more cost per pixel.

Tradeoff: power-of-two sizes. Textures whose width and height are powers of two halve cleanly at each level. Other sizes need rounding or resampling, so many engines require or prefer power-of-two textures.

SituationWhat goes wrong without mipmapsBetter choice
A surface far awayShimmer as the camera moves, and sampling cost stays highMipmaps with trilinear filtering
A floor seen at a steep angleMipmaps alone are too blurry along one directionAnisotropic filtering on top of mipmaps
A fixed, screen-sized image, such as a UI spriteMipmaps waste memory, because the image is never drawn smallNo mipmaps
Pixel art, where each texel must stay sharpAveraging blurs the crisp shapesNearest sampling without a chain

History

Mipmapping was introduced in 1983 by Lance Williams in a paper on pyramidal parametrics. The name comes from the Latin phrase multum in parvo, much in a small space, which describes the stack of many resolutions held in one structure. Williams's idea built on the image pyramids that Burt and Adelson had published that same year for image coding.

The method caught on because it solved a visible problem cheaply. Before it, a textured surface far away shimmered badly, and the only fix was to filter every pixel over a large area, which was too slow. A pre-built chain moves that work out of the frame loop, and each frame then needs only a few lookups. Later hardware made the blend between levels and the sampling within each level fixed operations, and mipmapping became a standard feature of graphics systems.

Recap

  • A mipmap is a stack of copies of a texture, each half the width and height of the one before, built by averaging 2×22 \times 2 blocks.
  • The renderer counts the texels under one pixel, takes the base-2 logarithm, and reads the level that matches.
  • Reading the matching level gives the average colour of the detail under the pixel, so the surface stops shimmering.
  • The chain costs 4/34/3 of the base texture, and trilinear filtering blends two levels for a smooth change.
  • Read next: Image Pyramid covers the same stacking idea applied to whole images; Mip-NeRF applies the mipmap idea to 3D scenes; anisotropic filtering, mentioned above, is the usual follow-up for surfaces seen at a steep angle.

Check yourself

QuestionRecall: what does a mipmap level hold, compared with the level above it?Show answer
Each level holds the average of 2×22 \times 2 blocks of the level above it. It has half the width and half the height, so a quarter as many texels, and each texel summarises the detail of four texels one level up.
QuestionCompute: a texture has 64 texels across and a pixel covers 8 texels. Which mipmap level is read, and how many texels across is that level?Show answer
The level of detail is d=log⁡28=3d = \log_2 8 = 3. Level 3 has 64/23=864 / 2^3 = 8 texels across, so each texel at that level covers one pixel's worth of the texture.
QuestionWhy: why does a mipmap avoid shimmering that a single sample from level 0 would show?Show answer
A single sample from level 0 reads one texel, which can be dark or bright depending on the position. As the surface moves, the sample jumps between them. A pre-averaged level gives the mean of all the texels under the pixel, and that mean changes smoothly as the surface moves.
QuestionApplied: a texture is 1024 texels across and a wall covers 128 pixels. What is the level of detail, and how much extra memory does the full chain cost compared with the base texture?Show answer
ρ=1024/128=8\rho = 1024 / 128 = 8, so d=log⁡28=3d = \log_2 8 = 3, and level 3 is read. The chain costs one third more than the base, because the levels add up to 4/34/3 of level 0.

Sources