Monday 18 October 2010

Anti-Aliasing with XNA 4

I've spent all weekend struggling with getting Anti-Aliasing (AA) working with XNA 4.  I compared my old XNA 3.1 version with my updated XNA 4 version and noticed that the graphics on the menus looked awful on the new one!

This was obviously because AA, multi-sampling, was not enabled on the new version.  Some of the settings used to enable this in XNA 3.1 had been replaced in XNA 4.  I therefore spent a lot of the weekend on the assumption that I just needed to get the settings right within XNA.

I asked for help on the XNA forums so you can read the saga there as well.

To demonstrate the problem, the following rather rough picture is a small portion of the screen illustrating the effect:

The following much smoother shot shows what I was expecting:
To cut a long frustrating story short.  On advice I created a small app that had nothing but one model and a background and that helped to narrow the problem down.  It was not within the XNA code but it was in the HLSL shader code.

These three lines turn off anti-aliasing in XNA 4.0 and do nothing in XNA 3.1!
MinFilter = Point;
MagFilter = Point;
MipFilter = Point;
 
I removed those three lines and everything was back to what I expected.  This is the end result:
 

I've gone a little further and checked out the other options for the texture filters when set within the HLSL fx file.  I got the options from the following web page:
http://msdn.microsoft.com/en-us/library/bb322811(VS.85).aspx

The results are:
Min, Mag and Mip filters = Point -> AA gets turned off
Min, Mag and Mip filters = Linear -> AA works
Min and Mag filters = Anisotropic -> AA works
Mip filter = None -> AA works but the result was grainy
Min and Mag filters = PyramidalQuad -> Failed to compile
Min and Mag filters = GaussianQuad -> Failed to compile

My conclusion is that I should remove everything from my HLSL effect files that can now be set within XNA 4.0 and I should set those from within XNA.  As always one of Shawn Hargreaves blog posts is most useful for that.

[Edit:  change of plan again.  I completely re-wrote my shaders and now the filtering is in XNA code... again.]

4 comments:

Anonymous said...

You are so mistaking anti-aliasing with texture filtering...

John C Brown said...

Not exactly mistaking. Anti-Aliasing is the effect I want to achieve and as far as I know that is done using texture filtering and multisampling.

Anonymous said...

Great blog! Thanks for linking to pertinent stuff too (MSDN / Shawn's blog, etc).

John C Brown said...

Thank you for the compliments.