Coding Horror

programming and human factors

Anisotropic Filtering

I've talked about Bilinear vs. Bicubic filtering before in the context of 2D images, but bilinear filtering is a key ingredient in 3D graphics, too. When a texture is applied to a polygon, the texture may be scaled up or down to fit, depending on your screen resolution. This is done via bilinear filtering.

A full discussion of 3D graphics is way outside the scope of this post-- plus I don't want to bore you to death with concepts like trilinear filtering and mip-mapping. But I do want to highlight one particular peculiarity of bitmap scaling in 3D graphics. As you rotate a texture-mapped polygon away from the viewer, simple bilinear filtering and mip-mapping cause the texture to lose detail as the angle increases:

Texture without anisotropic filtering

Now, some detail loss with distance is intentional. That's essentially what mip-mapping is. if we didn't mip-map into the distance, the image would look extremely noisy:

No mip-mapping Mip-mapping
texture without mip-mapping texture with mip-mapping

The problem with simple mip-mapping and bilinear filtering is that they're too simple. Much more detail should be retained into the distance. And that's what anisotropic filtering does:

Texture with anisotropic filtering

Because you're typically viewing most of the polygons in the world at an angle at any given time, anisotropic filtering has a profound impact on image quality. Here are some screenshots I took from the PC game FlatOut which illustrate the dramatic difference between standard filtering and anisotropic filtering:

Standard filtering 16x Anisotropic filtering
Flatout screenshot detail, standard filtering Flatout screenshot detail, 16x anisotropic filtering
Flatout screenshot detail, standard filtering Flatout screenshot detail, 16x anisotropic filtering

These are detail elements cropped from the full-size 1024x768 screenshots: standard, anisotropic.

Proper anisotropic filtering is computationally expensive, even on dedicated 3D hardware. And the performance penalty increases with resolution. ATI was the first 3d hardware vendor to introduce some anisotropic filtering optimizations-- some would say shortcuts-- in their cards which allowed much higher performance. There is one small caveat, however: at some angles, textures don't get fully filtered. ATI effectively optimized for common angles you'd see in 3D level geometry (floor, walls, ceiling) at the cost of the others.

For better or worse, these optimizations are now relatively standard now even on nVidia cards. I think it's a reasonable tradeoff for the increased image quality and performance.

In my opinion, anisotropic filtering is the most important single image quality setting available on today's 3D hardware. It's like Freedom Rock: make sure you've turned it up, man!

Written by Jeff Atwood

Indoor enthusiast. Co-founder of Stack Overflow and Discourse. Disclaimer: I have no idea what I'm talking about. Find me here: https://infosec.exchange/@codinghorror