07 Dec 2011 Fast Approximate Anti-Aliasing (FXAA)

Anti-aliasing has an intimidating name, but what it does for our computer displays is rather fundamental. Think of it this way -- a line has infinite resolution, but our digital displays vì chưng not.

Bạn đang xem: What is fxaa in video games?

So when we "snap" a line to lớn the pixel grid on our display, we can compensate by imagineering partial pixels along the line, pretending we have a much higher resolution display than we actually do. Like so:

*

As you can see on these little squiggly black lines I drew, anti-aliasing produces a superior image by using grey pixels khổng lồ simulate partial pixels along the edges of the line. It is a hack, but as hacks go, it"s pretty darn effective. Of course, the proper solution to lớn this problem is to have extremely high resolution displays in the first place. But other than tiny handheld devices, I wouldn"t hold your breath for that khổng lồ happen any time soon.

This also applies to lớn much more complex 3d graphics scenes. Perhaps even more so, since adding motion amplifies the aliasing effects of all those crawling lines that hóa trang the edges of the scene.

*

But anti-aliasing, particularly at 30 or 60 frames per second in a complex state of the art game, with millions of polygons & effects active, is not cheap. Per my answer here, you can generally expect a performance cost of at least 25% for proper 4X anti-aliasing. And that is for the most optimized version of anti-aliasing we"ve been able lớn come up with:Super-Sampled Anti-Aliasing (SSAA). The oldest trick in the book - I danh mục it as universal because you can use it pretty much anywhere: forward or deferred rendering, it also anti-aliases alpha cutouts, and it gives you better texture sampling at high anisotropy too. Basically, you render the image at a higher resolution & down-sample with a filter when done. Sharp edges become anti-aliased as they are down-sized. Of course, there"s a reason why people don"t use SSAA: it costs a fortune. Whatever your fill rate bill, it"s 4x for even minimal SSAA.

Multi-Sampled Anti-Aliasing (MSAA). This is what you typically have in hardware on a modern graphics card. The graphics thẻ renders to a surface that is larger than the final image, but in shading each "cluster" of samples (that will over up in a single pixel on the final screen) the px shader is run only once. We save a ton of fill rate, but we still burn memory bandwidth. This technique does not anti-alias any effects coming out of the shader, because the shader runs at 1x, so alpha cutouts are jagged. This is the most common way to lớn run a forward-rendering game. MSAA does not work for a deferred renderer because lighting decisions are made after the MSAA is "resolved" (down-sized) lớn its final image size.

Coverage Sample Anti-Aliasing (CSAA). A further optimization on MSAA from NVidia . Besides running the shader at 1x & the framebuffer at 4x, the GPU"s rasterizer is run at 16x. So while the depth buffer produces better anti-aliasing, the intermediate shades of blending produced are even better.

Pretty much all "modern" anti-aliasing is some variant of the MSAA hack, and even that costs a quarter of your framerate. That"s prohibitively expensive, unless you have so much performance you don"t even care, which will rarely be true for any recent game. While the crawling lines of aliasing vì chưng bother me, I don"t feel anti-aliasing alone is worth giving up a quarter of my framerate and/or turning down other details khổng lồ pay for it.But that was before I learned that there are some emerging alternatives khổng lồ MSAA. And then, much khổng lồ my surprise, these alternatives started showing up as actual graphics options in this season"s PC games -- Battlefield 3, Skyrim, Batman: Arkham City, & so on. What is this FXAA thing, & how does it work? Let"s see it in action:
*
*
*
No AA4x MSAAFXAA
(this is a zoomed fragment; click through khổng lồ see the full screen)FXAA stands for Fast Approximate Anti-Aliasing, và it"s an even more clever hack than MSAA, because it ignores polygons and line edges, and simply analyzes the pixels on the screen. It is a px shader program documented in this PDF that runs every frame in a scant millisecond or two. Where it sees pixels that create an artificial edge, it smooths them. It is, in the words of the author, "the simplest & easiest thing to integrate và use".

*
FXAA has two major advantages:FXAA smooths edges in all pixels on the screen, including those inside alpha-blended textures & those resulting from px shader effects, which were previously immune to lớn the effects of MSAA without oddball workarounds.It"s fast. Very, very fast. Version 3 of the FXAA algorithm takes about 1.3 milliseconds per frame on a $100 video card. Earlier versions were found to be double the tốc độ of 4x MSAA, so you"re looking at a modest 12 or 13 percent cost in framerate lớn enable FXAA -- and in return you get a considerable reduction in aliasing.The only downside, and it is minor, is that you may see a bit of unwanted edge "reduction" inside textures or in other places. I"m not sure if it"s fair to gọi this a downside, but FXAA can"t directly be applied lớn older games; games have to be specifically coded to call the FXAA px shader before they draw the game"s user interface, otherwise it will happily smooth the edges of on-screen HUD elements, too.The FXAA method is so good, in fact, it makes all other forms of full-screen anti-aliasing pretty much obsolete overnight. If you have an FXAA option in your game, you should enable it immediately và ignore any other AA options.FXAA is an excellent example of the power of simple hacks và heuristics. But it"s also a great demonstration of how attacking programming problems from a different angle -- that is, rather than thinking of the screen as a collection of polygons & lines, think of it as a collection of pixels -- can enable you to lớn solve computationally difficult problems faster và arguably better than anyone thought possible.

Xem thêm: Cách Rèn Luyện Tác Phong Làm Việc Là Gì ? 6 Tác Phong Giúp Bạn Thành Công

What"s your next career move? Stack Overflow Careers has the best job listings from great companies, whether you"re looking for opportunities at a startup or Fortune 500. You can search our job listings or create a profile and let employers find you.