Multisampling

From DmWiki

Multisampling is a method of antialiasing supported by most new graphics cards. Essentially, it performs supersampling, but only at the edges of polygons; the interior of a polygon is not affected. Functionally, multisampling works by storing multiple color, depth, and other values for each pixel in the frame buffer, while only evaluating the pixel shader once for each pixel.

Terminology

A sample is one sub-pixel location at which color, depth, and other values are stored. A sample is thought of as being a zero-sized point, while a pixel is thought of as being a square. Ordinary rasterization therefore has one sample per pixel, located at the center of the pixel square. In multisampling, there are multiple samples within the pixel square (typically 2, 4, or 8), but there is generally no way for an application to determine where in the pixel they are located. This is because the exact sample location is up to the hardware, and may even vary from one pixel to another.

A texture sample is a point at which the texture color is looked up, lighting calculations are performed, pixel shader evaluated, etc. In multisampling, there is never more than one texture sample per pixel, even though there are many color/depth samples.

A multisample buffer is a buffer that contains space for all the extra samples. There may be a regular frame buffer that contains one sample per pixel, and a separate multisample buffer that contains the remaining samples. However, the application usually need not be concerned with this.

Comparison with supersampling

Supersampling works by simply scaling up the resolution of the frame buffer. For example, if you're working in a 800x600 screen resolution, then (for 4X supersampling) it uses a 1600x1200 buffer internally. So every pixel on the screen corresponds to four pixels in the internal buffer. The complete scene is rendered in this high resolution, and then it is downsampled (shrunk) to the screen resolution by averaging the groups of four pixels together.

Multi-sampling is an optimization of this. Rendering in a 1600x1200 resolution requires four times more work than rendering in 800x600 mode, so there's a huge performance hit. What multisampling does is sample textures only once per group of samples (in this case, there would be 4 samples corresponding to one pixel on the 800x600 screen), and do the lighting calculations also just once. It still renders the triangles in the high resolution, so especially for edges, it knows which of the four samples belong to the current polygon. So after the scene is rendered we again average the pixels of the big buffer together, so we get anti-aliasing at the edges. In other words, the edges are super-sampled, while the interior of the polygons is rendered as if it was in 800x600 resolution.

The only disadvantage of multi-sampling over super-sampling is that aliasing effects can occur inside the polygon. But mostly this is not as noticable as jaggies, and there is also mipmapping and anisotropic filtering to help with this situation.

Multisampling can also save video card memory and memory bandwidth over ordinary supersampling. Since only one texture sample is taken per four screen samples, it usually doesn't have to be stored four times; however, for pixels around edges they do have to be stored separately. So there's a (hardware specific) smart compression technique used in practice, and this compression saves bandwidth versus supersampling (the multisampling algorithm itself only saves fill rate).

Multisample masks

Direct3D and OpenGL both allow applications to set the multisample mask when rendering. In this case, when a polygon is drawn, it is only drawn to some of the samples in each pixel that it covers; which samples are controlled by the mask. For instance, when using 4X multisampling, the four low-order bits in the mask (corresponding to values 1, 2, 4, and 8) each correspond to a sample position; and that sample is only drawn to if the corresponding bit is a 1.

This allows, for example a motion blur effect. Render the object four times, each time a little bit moved, to each of the four pixel groups. The averaged result on the screen will give the object a blur effect. The multisample mask can also be employed for a transparency effect. However, this is rather limited since, if you have 4 samples, you effectively can only produce alpha values of 0, 0.25, 0.5, 0.75, or 1.


DevMaster navigation