Multisampling is designed to counter the effects of aliasing at the edges of a primitive, when it is rasterized into fragments. Multisampling can be also applied to transparent textures, like wire fences, blades of grass or the leaves of trees. In this case, it is called 'alpha-to-coverage' and replaces the legacy alpha test.
A multisample buffer contains multiple samples per pixel, with each sample having it's own color, depth and stencil values. The term 'coverage' refers to a bitmask that is used to determine which of these samples will be updated: a coverage value of 1 indicates that the relevant sample will be updated; a value of 0 indicates it will be left untouched.
EnableCap.SampleAlphaToCoverage is used, the coverage is obtained by interpreting the alpha as a percentage: an alpha of 0.0 means that no samples are covered, while a value of 1.0 indicates that all samples are covered. For example, a multisample buffer with 4 samples per pixel and an Alpha value of 0.5 indicates that half of the samples are covered (their coverage bit is 1) and two are not covered (coverage bit is 0).
The coverage bitmask of incoming fragments can be set in a Fragment Shader with the variable gl_Coverage.
To enable alpha-to-coverage, enable multisampling (
GL.Enable(EnableCap.Multisample)) and make sure that
GL.GetInteger(GetPName.SampleBuffers, out buffers) is 1. If
EnableCap.Multisample is disabled but
GetPName.SampleBuffers is 1, alpha-to-coverage will be disabled.
There are three OpenGL states related to alpha-to-coverage, they are controlled by
For each sample at the current pixel, the Alpha value is read and used to generate a temporary coverage bitmask which is then combined through a bitwise AND with the fragment's coverage bitmask. Only samples who's bit is set to 1 after the bitwise AND are updated.
GL.SampleCoverage( value, invert )the temporary coverage bitmask is generated by the value parameter - and if the invert parameter is true it is bitwise inverted - before the bitwise AND with the fragment's coverage bitmask.
Each Alpha value is replaced by 1.0.
The values set by the command
GL.SampleCoverage( value, invert ) are only used when EnableCap.SampleCoverage is enabled.
- value is a single-precision float used to specify the Alpha value used to create the coverage bitmask.
- invert is a boolean toggle to control whether the bitmask is bitwise inverted before the AND operation.
The states of EnableCap.Multisample, EnableCap.SampleAlphaToCoverage, EnableCap.SampleCoverage and EnableCap.AlphaToOne can be queried with
Result = GL.IsEnabled( cap )
The value set by GL.SampleCoverage() can be queried with
GL.GetFloat( GetPName.SampleCoverageValue, ... )
The boolean set by GL.SampleCoverage() can be queried with
GL.GetBoolean( GetPName.SampleCoverageInvert, ... )