MSAA in DirectX 11
If you just need to read what i’ve done to fix MSAA go into LoST section.
Story
One day one of my friends send me a message with a two simple sentances:
Why your screenshots so jittery and aliased? Fix it!
Without any arguing that i was lazy for that i chose MSAA for my main AA alghorithm. I Thought that it would be easy to implement it! Just add some checks, change some things here add some code here and should be working!
Right?
Wrong!
When i started digging i saw how many things must be done in order to enable and use MSAA. I’ll skip a whole story about how i rage each time when i fail to make MSAA work, so bear with me.
Last day i was reading an article written by Turanszkij and i saw a link to his discord server.
This guy helped me a lot and saved a lot of time for future me.

In case you barly see image or can barely read what he wrote:
@SweetLuna Check the debug layer if it outputs anything when you render to MSAA texture. Also check that you have set a proper sample mask with OMSetBlendState that allows all samples to be written
So thanks to him i finally made MSAA work.
Results
Without MSAA

MSAA x8

Close look at bridge without MSAA

MSAA x8

LoST for MSAA
List of several things that i encountered with when i tried integrating MSAA into my own engine.
- Check if current device/hardware/software can handle MSAA for this exact DXGI_FORMAT.
    
- If yes, then what is max level?
 - If returned Quality > 0 and SampleCount > 1
        
- This device/hardware/software supports MSAA for this format with SampleCount levels.
 
 
 
The way how i check for MSAA support can be found here and here.
- Each RTV that has type of Texture2DMS must have exact RTV but with type of Texture2D.
 - For DSV that has SampleCount > 1 must have:
    
- Must have same DSV with No MSAA and No UAV.
 - Must have same DSV with No MSAA, W/ UAV, and without DSV/RTV.
 
 - Enable MSAA in RasterState.
    
MultisampleEnable = true; - Set Sample Mask for BlendState to 
0xFFFFFFFF.Mine was always set to 1, and i always wondered why only 1st sample had data, when rest of them - don’t?
 - For each RTV MSAA reolve is simple. Just call
    
ID3D11DeviceContext::ResolveSubresource(ID3D11Resource *Dest, UINT DestSubres, ID3D11Resource *Src, UINT SrcSubres, DXGI_FORMAT format); - For depth you do this by hand. I use similar technique as described by Turanszkij for WikedEngine.
 - Add 
SV_SampleIndexfor input into rendering shader.GBuffer main(PS In, bool bIsFront : SV_IsFrontFace, uint SampleIndex : SV_SampleIndex) { - Replace in every shader where you sample original SRV (with MSAA) Texture2D with Texture2DMS.
 - Store for each RTV and DSV(for 3rd one you don’t need this) created their SampleCount.
 
References
- How to resolve an MSAA depthbuffer from wickedengine
 - How to sample a SRV when enable msaa x4?DirectX11
 - How to enable MSAA in DirectX 11
 - Cannot enable 4X MSAA Anti-Aliasing in DirectX 11
 - dx12 open 4x msaa failed
 - Enable MSAA in DirectX 11
 - Sampling MSAA from DirectX 11
 - Multisampling (MSAA) for DirectX11/DirectX10 with D3DImage shared resource
 - Get the MSAA sample number inside pixelshader for OIT
 - Custom MSAA in pixel shader with DX11
 - D3D11 Render to Texture Transparency/MSAA Issue
 - DX9, rendertargets and MSAA
 - Deferred rendering + MRT + MSAA
 - How to enable MSAA in DirectX 11
 - Deferred Shading MSAA Sample
 - DeferredShadingMSAA.cpp
 - Lighting_FS.glsl
 - Deferred MSAA Artifacting
 - Deferred Rendering w/ MSAA - MSAA Z Prepass Idea
 - MSAA in Deferred shading
 - MSAA with Deferred shading
 - Single Pass MSAA Deferred Rendering - Dithered Deferred Rendering Idea
 - Deferred MSAA
 - Beware of SV_Coverage
 - Semantics for HLSL at MSDN
 
Let me know what you think of this article on twitter @Kriegthepshyco or leave a comment below!