Texture aliasing

Published April 29, 2011
Advertisement
Aliasing can occur any time you resample a texture, for instance to scale it, rotate it, or map it onto a 3D model. Thanks to our friend the Nyquist threshold, the resulting problems get worse the lower a frequency you sample at, ie. the smaller you shrink it.

Check out this awesome 32x32 test image:

2705.Untitled_5F00_15AECA6E.png

Zoomed 4x so you can see in detail:

0652.image_5F00_5C2BBA76.png

This test code draws six copies, first with no transform, then rotated, followed by straight and rotated versions at half and quarter size:

spriteBatch.Begin(0, null, SamplerState.PointClamp, null, null); spriteBatch.Draw(testTexture, new Vector2(20, 24), null, Color.White, 0, new Vector2(16, 16), 1, 0, 0); spriteBatch.Draw(testTexture, new Vector2(60, 24), null, Color.White, 1, new Vector2(16, 16), 1, 0, 0); spriteBatch.Draw(testTexture, new Vector2(95, 24), null, Color.White, 0, new Vector2(16, 16), 0.5f, 0, 0); spriteBatch.Draw(testTexture, new Vector2(120, 24), null, Color.White, 1, new Vector2(16, 16), 0.5f, 0, 0); spriteBatch.Draw(testTexture, new Vector2(140, 24), null, Color.White, 0, new Vector2(16, 16), 0.25f, 0, 0); spriteBatch.Draw(testTexture, new Vector2(155, 24), null, Color.White, 1, new Vector2(16, 16), 0.25f, 0, 0); spriteBatch.End();

Note the use of SamplerState.PointClamp, which turns off the antialiasing SpriteBatch would normally apply by default.

Resulting image:

5432.image_5F00_3C10ADB9.png

Zoomed 4x:

2388.image_5F00_401AFB8B.png

This has almost too many aliasing problems to mention! Some that I find particularly bothersome are:

  • The black diagonal lines have pieces missing in the full size rotated image
  • The half size non rotated image has lots of problems:
    • The green vertical line, and also the red and blue horizontal lines, are entirely missing
    • It ended up with just two rather than three sets of black diagonal lines
    • The black border remains on the bottom and right, but is missing on the top and left
  • The two quarter size images are entirely different:
    • Non rotated version has only a blue line, no red or green at all
    • Rotated version has scattered red and green pixels, but no blue
    • Imagine how horrible it would look if we animated this sprite spinning, as it would flicker back and forth between different colors!
  • Remember how multisampling smooths triangle edges but does not affect their interior? We can confirm this by turning on 4x multisampling, and noting how the only difference is smoother borders on the rotated images:

    6521.image_5F00_34F17141.png

    With 4x supersampling, on the other hand, texture aliasing is indeed reduced:

    1715.image_5F00_7FE4E210.png

    But this is still far from perfect. The full size rotated and half size non rotated images look pretty nice, but the half size rotated and both quarter size versions have flaws. We'd still get nasty color pulsation if we animated a rotation of that quarter size version. This is because 4x supersampling only gives a 2x improvement in sampling frequency along each of the horizontal and vertical axes. That can reduce aliasing, but is not enough to keep us above the Nyquist threshold if we transform the texture by a factor of more than 2x.

    Whatever is a poor graphics programmer to do?

    To start with, we shouldn't have made life difficult for ourselves by specifying point sampling! If we turn off supersampling and multisampling, then change our SpriteBatch.Begin call to use the default SamplerState.LinearClamp, bilinear filtering produces a much nicer image than the point sampled version we started out with:

    8662.image_5F00_0DB7280C.png

    Unlike multisampling and supersampling, texture filtering does not smooth the border of the rotated images, but it does a nice job to reduce aliasing inside our full size rotated and half size non rotated images. Bilinear filtering lets us down in the same places as supersampling. Because the GPU only takes four filter taps per texture fetch (two horizontal and two vertical), we get 2x more headroom before we will run into aliasing problems, but a 2x improvement cannot entirely eliminate the Nyquist threshold, so our quarter size images remain nasty.

    The solution is to combine bilinear filtering with precalculated mipmaps (plus anisotropic filtering if you can afford it). No matter how small the texture is scaled, the GPU can now simply select the appropriate mip level to sample from, always remaining just below the Nyquist threshold regardless of what transforms are applied.

    With mipmaps enabled, even our smallest images now keep the same color intensity and hue, with no stray pixels or random changes of shape, so we can animate our sprite rotating or scaling with no aliasing problems at all:

    6518.image_5F00_1B896E07.png

    aggbug.aspx?PostID=10159661

    Source
    Previous Entry Multisampling
    0 likes 0 comments

    Comments

    Nobody has left a comment. You can be the first!
    You must log in to join the conversation.
    Don't have a GameDev.net account? Sign up!
    Advertisement