Introduction

Part 1 gave every pixel a UV coordinate saying where to read the texture. But a texture is a grid of texels, and a UV like (0.42, 0.58) almost never lands exactly on the centre of one. So which colour should the pixel get? Choosing it is called texture filtering. This post covers:

  • Where texels actually sit in UV space
  • Nearest filtering, the blocky one
  • Bilinear filtering, which blends the four nearest texels
  • Magnification versus minification: why filtering a texture that’s smaller on screen is a much harder problem

Where Are the Texels?

A texture 8 texels wide covers UVs from 0 to 1, so each texel is 1/8 = 0.125 wide. Texel 0 covers 0 to 0.125, texel 1 covers 0.125 to 0.25, and so on. The centre of texel i is at (i + 0.5) ÷ 8: 0.0625, 0.1875 and so on. Exactly as with pixels and pixel centres in rasterisation, the half matters.

Nearest Filtering

The simplest answer is to use whichever texel the UV falls inside. This is nearest filtering (also called point sampling). It’s fast, as it reads only one texel, but when a texture is stretched over a large area of the screen, each texel becomes a visible square block, and the colour jumps abruptly at the edge of each one.

Bilinear Filtering

Bilinear filtering instead finds the four texel centres that surround the UV and blends them, weighted by how close the UV is to each. It’s two rounds of the linear blending (“lerp”, see Interpolation) from fragment shaders, hence bi-linear: first blend the two bottom texels and the two top texels horizontally, then blend those two results vertically. The colour changes smoothly from one texel centre to the next, with no visible blocks.

The demo below shows a tiny 8 × 8 texture stretched over a large square. Move the pink point over it (hover, tap or drag, or select it and use the arrow keys). The panel on the right zooms in on the four texels around the point, showing each one’s share of the result.

An 8 × 8 texture, magnified with nearest or bilinear filtering

The texture on screen
The four nearest texels

How the four percentages are worked out

First, the UV is turned into texel units: multiply by the texture’s size (8 here) and subtract 0.5, because texel centres sit half a texel in from the edges. The whole-number part says which four texels surround the point; the leftover fraction says how far between their centres it is: fx across and fy up, each from 0 to 1.

Each texel’s share is then how close the point is to it, across times up:

  • Bottom-left: (1 − fx) × (1 − fy)
  • Bottom-right: fx × (1 − fy)
  • Top-left: (1 − fx) × fy
  • Top-right: fx × fy

The four shares always add up to 1 (100%). For example, a point a quarter of the way across and halfway up (fx = 0.25, fy = 0.5) gets 37.5% from each left texel and 12.5% from each right one. Multiplying each texel’s colour by its share and adding them up gives exactly the same answer as the “two rounds of lerp” described above.

Things to try:

  • With Bilinear, put the point exactly on a texel centre (a white dot): that texel gets 100%. Halfway between two centres, they get 50% each. In the middle of four, 25% each.
  • Switch to Nearest and move slowly across a texel boundary: the result jumps instantly from one colour to the other.
  • With Bilinear, notice the edges of the picture. Pixel art looks blurry when it’s magnified with bilinear filtering, which is why games with a pixel-art style deliberately use nearest filtering.
  • Shrink the size on screen right down, until the whole texture is only a few pixels across. Now each screen pixel covers several texels, and neither filter looks right: bits of the picture vanish and reappear as you change the size. That’s the problem the next post solves.

Magnification and Minification

When a texture is drawn bigger than its own resolution, each texel covers several screen pixels. This is magnification, and bilinear filtering handles it well: it just has to fill in smoothly between texels.

When a texture is drawn smaller than its resolution, for example on a distant wall, each pixel covers many texels. This is minification, and it’s far harder. Bilinear filtering still only reads the four texels nearest the pixel’s centre and ignores all the others under the pixel, so the result depends on exactly where the centre lands. As the camera moves, it lands on different texels each frame, and the texture shimmers and sparkles. The fix, mipmapping, is the subject of part 3.

Graphics APIs let you choose the filter separately for each case. In WebGL it’s one setting each (LINEAR means bilinear; you don’t need to follow the code, just notice there are two settings):

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);   // magnification: bilinear
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);   // minification: trilinear (part 3)
Under the hood

Bilinear filtering is done by the graphics card’s texture units in dedicated fixed-function hardware, not by your shader. The four texels are fetched together (in a tiled texture layout they’re usually in the same small block of memory) and blended in one go, so on modern hardware a bilinear sample typically costs about the same as a nearest one. The blend weights are kept to a limited precision: many graphics cards store the position between two texels with only 8 bits, so a bilinear blend can only take 256 distinct steps between them. That’s far more than enough for normal textures, and only shows up if you magnify a texture enormously.

Questions You Might Have

Does bilinear filtering add detail?

No. It can only blend the texels that are there, so a magnified texture looks smooth but soft. Sharp detail up close needs a bigger texture, or tricks such as detail textures or distance fields (SDF Text).

Summary

  • Texel centres sit at (i + 0.5) ÷ size in UV space.
  • Nearest filtering reads the one texel the UV falls in: fast, but blocky when magnified.
  • Bilinear filtering blends the four nearest texels: smooth when magnified.
  • Magnification is easy; minification, where a pixel covers many texels, makes textures shimmer, and needs mipmaps.

Further Reading