How do I setup MSAA in D3D11?

Edit: I've changed the code now everything gets rendered into an MSAA Render Target, and calling ResolveSubresource just before the Present call. This seems to work if I Draw directly to the MSAA Render Target, but Drawing it into another Render target first breaks the Anti-Aliasing effect. Visual Studio's Graphics Analyzer shows that indeed, the Anti-Aliasing effect is applied on the first Render Target (a Texture-Bindable Render Target), but not the second (MSAA Render Target).

I'm currently trying to remove jaggies from my render, but I'm a bit confused on how to actually set thing up. Currently, Right now I'm using the TEXTURE2DMS as the dimension for everything, and for now arbitrarily setting up the Sample count to 2, and the Quality to 0.

DirectX setup:

Swapchain.SampleDesc.Count = 2;
Swapchain.SampleDesc.Quality = 0;
DepthStencil.SampleDesc.Count = 2; 
DepthStencil.SampleDesc.Quality = 0;
DepthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;

RenderTarget(Texture) Setup:

TextureDesc.SampleDesc.Count = 2;
RenderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS;
DepthDesc.SampleDesc.Count = 2;
DepthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
ShaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;

I also have 2 Pixel Shaders:

struct Pin
{
    float4 Pos : SV_POSITION;
    float4 Col : COLOR0;
    float2 Tex : TEXCOORD0;
    float2 Texel : TEXCOORD1;
};

//High pass filter
Texture2D txBlank : register( t0 );
SamplerState samLinear : register( s0 );

float RGB2RelativeLuminance(float4 c)
{
    return (c.x * 0.2126) + (c.y * 0.7152) + (c.z * 0.0722);
}

float4 PS(Pin input) : SV_TARGET
{
    float4 color = float4(0, 0, 0, 1);
    float treshhold = 0.3f;

    float4 temp = txBlank.Sample(samLinear, input.Tex) * input.Col;
    if(RGB2RelativeLuminance(temp) > treshhold)
        color = temp;

    return color;
}

//Basic
Texture2DMS<float4> txImage : register( t0 );
SamplerState samLinear : register( s0 );

float4 PS(Pin input) : SV_TARGET
{
    return txImage.Load(input.Pos, 1) * input.Col;
}

Rasterizer:

rasterDesc.AntialiasedLineEnable = true;
rasterDesc.CullMode = D3D11_CULL_BACK;
rasterDesc.DepthBias = 0;
rasterDesc.DepthBiasClamp = 0.0f;
rasterDesc.DepthClipEnable = true;
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.FrontCounterClockwise = false;
rasterDesc.MultisampleEnable = true;
rasterDesc.ScissorEnable = false;
rasterDesc.SlopeScaledDepthBias = 0.0f;

And right now, my Program goes like:

  1. (Init)Initialize D3D Device, Swapchain, Context, Rasterizer, Render Target, Depth Stencil, Viewport
  2. (Init)Create Render Target(Texture), I'll refer to this as 'ViewTex'
  3. (Draw)Set ViewTex as Render Target
  4. (Draw)Set a Blank Texture to Slot 0
  5. (Draw)Set 'High pass filter' as the Shader
  6. (Draw)Draw the Object
  7. (Draw)Set Render Target to Window
  8. (Draw)Set ViewTex to Slot 0 Texture
  9. (Draw)Set 'Basic' as the Shader
  10. (Draw)Draw a Rectangle that covers the whole Render Target
  11. (Draw)Unset Slot 0 Texture

Currently, this code doesn't work, and I'm not really sure why, Here's a Close-up of a screenshot of the render: link

Am I misunderstanding something? Is setting up Multisample not equal to MSAA?

Upvotes: 0

Views: 5358

Answers (1)

I've figured it out, Here's what I did to fix the code:

  1. I Followed the instruction here How to fix this multisampling error when creating a swapchain? , which gets the MSAA Working.

  2. I rewrote my Basic Shader to

    Texture2DMS<float4> txImage : register( t0 );
    float4 PS(Pin input) : SV_TARGET
    {
        float4 returncol = float4(0, 0, 0, 0);
        for(uint i = 0; i < 4; i++)
        {
            returncol += txImage.Load(input.Pos, i);
        }
        return returncol / 4;
    }
    

And it works now.

Upvotes: 0

Related Questions