Can't Enable MSAA

Started by
3 comments, last by MJP 10 years, 7 months ago

Hello,

I've been doing some development in Direct3D 11 and C++ and I can't seem to be able to enable MSAA. I am getting horrible jagged lines when rendering a simple primitive. Here is what I have done so far:

SwapChain Creation


        // Display Mode
        // --------------------------------------------------------------------

        DXGI_RATIONAL RefreshRate = {60, 1};

        DXGI_MODE_DESC DisplayMode = {0};

        DisplayMode.Width = Width;
        DisplayMode.Height = Height;
        DisplayMode.RefreshRate = RefreshRate;
        DisplayMode.Format = DXGI_FORMAT_R8G8B8A8_UNORM;

        // --------------------------------------------------------------------

        // SwapChain Description
        // --------------------------------------------------------------------

        DXGI_SAMPLE_DESC Sampling = {4, 0};

        DXGI_SWAP_CHAIN_DESC SCDescription = {0};

        SCDescription.BufferCount = 1;
        SCDescription.BufferDesc = DisplayMode;
        SCDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
        SCDescription.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
        SCDescription.OutputWindow = RenderWindow;
        SCDescription.SampleDesc = Sampling;
        SCDescription.Windowed = TRUE;

        // --------------------------------------------------------------------

RenderTarget and DepthStencil and Rasterizer


        // Create RenderTarget
        // --------------------------------------------------------------------

        ID3D11Texture2D *Resource = NULL;
        m_SwapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)&Resource);

        D3D11_RENDER_TARGET_VIEW_DESC RTDescription;
        ZeroMemory(&RTDescription, sizeof(D3D11_RENDER_TARGET_VIEW_DESC));

        RTDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        RTDescription.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS;

        m_Device->CreateRenderTargetView(Resource, &RTDescription, 
            &m_RenderTarget);

        Resource->Release();
        Resource = NULL;

        // --------------------------------------------------------------------

        // Create DepthStencil
        // --------------------------------------------------------------------

        D3D11_TEXTURE2D_DESC TextureDescription = {0};

        TextureDescription.BindFlags = D3D11_BIND_DEPTH_STENCIL;

        TextureDescription.Width = Width;
        TextureDescription.Height = Height;
        TextureDescription.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
        TextureDescription.SampleDesc = Sampling;

        TextureDescription.ArraySize = 1;
        TextureDescription.MipLevels = 1;

        ID3D11Texture2D *DepthResource = NULL;
        m_Device->CreateTexture2D(&TextureDescription, 
            NULL, &DepthResource);

        D3D11_DEPTH_STENCIL_VIEW_DESC DSDescription;
        ZeroMemory(&DSDescription, sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC));

        DSDescription.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
        DSDescription.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;

        m_Device->CreateDepthStencilView(DepthResource, 
            &DSDescription, &m_DepthStencil);

        DepthResource->Release();
        DepthResource = NULL;

        // --------------------------------------------------------------------

        m_DeviceContext->
            OMSetRenderTargets(1, &m_RenderTarget, m_DepthStencil);

        // Customize Rasterizer
        // --------------------------------------------------------------------

        D3D11_RASTERIZER_DESC RasterizerDescription;
        ZeroMemory(&RasterizerDescription, sizeof(D3D11_RASTERIZER_DESC));

        RasterizerDescription.AntialiasedLineEnable = TRUE;
        RasterizerDescription.CullMode = D3D11_CULL_BACK;
        RasterizerDescription.DepthClipEnable = TRUE;
        RasterizerDescription.FillMode = D3D11_FILL_SOLID;
        RasterizerDescription.MultisampleEnable = TRUE;

        ID3D11RasterizerState *RasterizerState = NULL;

        m_Device->CreateRasterizerState
            (&RasterizerDescription, &RasterizerState);

        m_DeviceContext->RSSetState(RasterizerState);

        RasterizerState->Release();
        RasterizerState = NULL;

        // --------------------------------------------------------------------

Am I missing something? It should be noted that I am targeting D3D_FEATURE_LEVEL_9_3 and that my graphics card is capable of handling the above sampling count and quality.

Any help would be greatly appreciated!

Advertisement

I think you need to set the quality higher. You select 4 samples at quality 0, while you probably want to use a higher number. Try calling CheckMultisampleQualityLevels (http://msdn.microsoft.com/en-us/library/windows/desktop/ff476499%28v=vs.85%29.aspx) and using numQualityLevels - 1 for the quality.

Thanks for the reply Erik.

I've actually managed to track down my problem and it is not anti-aliasing based. The jagged edges I am seeing are actually because of the filtering on my texture.


        D3D11_SAMPLER_DESC SamplerDescription;
        ZeroMemory(&SamplerDescription, sizeof(D3D11_SAMPLER_DESC));

        SamplerDescription.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
        SamplerDescription.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
        SamplerDescription.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
        SamplerDescription.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
        SamplerDescription.Filter = D3D11_FILTER_ANISOTROPIC;
        SamplerDescription.MaxAnisotropy = 1;
        SamplerDescription.MaxLOD = D3D11_FLOAT32_MAX;

        ID3D11SamplerState *SamplerState = NULL;

        m_Device->CreateSamplerState(&SamplerDescription, &SamplerState);
        m_DeviceContext->PSSetSamplers(0, 1, &SamplerState);

        SamplerState->Release();
        SamplerState = NULL;

When my filter is set to D3D11_FILTER_MIN_MAG_MIP_POINT, I get some insane jaggedness. So I changed my filter to D3D11_FILTER_ANISOTROPIC. Now my jaggedness is gone, but my texture is incredibly blurry. Does anyone have any thoughts?

SamplerDescription.MaxAnisotropy = 1;

Maybe that is your issue. Try setting it to 16. Or determine the maximum supported:


if (deviceCaps->RasterCaps & D3DPRASTERCAPS_ANISOTROPY)
        SamplerDescrition.MaxAnisotrophy = deviceCaps->MaxAnisotropy;

The "Quality" level doesn't literally refer to the quality of the antialiasing, it's primarily used to expose different MSAA variations supported by the driver. For instance you can use Nvidia's CSAA functionality through a quality level. A quality of 0 always gives you standard MSAA using the specified number of samples.

As for the bluriness, if it's occurring on surfaces being viewed at a grazing angle then you should follow the above suggestion and try increasing your MaxAnisotropy.

This topic is closed to new replies.

Advertisement