Search code examples
slimdxdirectx-10multisampling

SlimDX DirectX10 multisampling not working


i am setting up my SlimDX project in DirectX10 but I somehow can't get multisampling to work. Here is my device initialization:

    // Create swap chain description
    DXGI.SwapChainDescription swapChainDesc = new DXGI.SwapChainDescription()
    {
        BufferCount = 1,
        Usage = DXGI.Usage.RenderTargetOutput,
        OutputHandle = Window.Handle,
        IsWindowed = true,
        ModeDescription = new DXGI.ModeDescription(0, 0, new Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm),
        SampleDescription = new DXGI.SampleDescription(2, 0),
        Flags = DXGI.SwapChainFlags.AllowModeSwitch,
        SwapEffect = DXGI.SwapEffect.Discard
    };

    // Get device
    DX10.Device.CreateWithSwapChain(null, DX10.DriverType.Hardware, DX10.DeviceCreationFlags.None, swapChainDesc, out Device, out SwapChain);

    // Get back buffer
    using (DX10.Texture2D texture = DX10.Resource.FromSwapChain<DX10.Texture2D>(SwapChain, 0))
        BackBuffer = new DX10.RenderTargetView(Device, texture);

    // Set viewport
    var viewport = new DX10.Viewport(0, 0, Window.ClientSize.Width, Window.ClientSize.Height);
    Device.OutputMerger.SetTargets(BackBuffer);
    Device.Rasterizer.SetViewports(viewport);

    // Disable alt+enter
    using (var factory = SwapChain.GetParent<DXGI.Factory>())
        factory.SetWindowAssociation(Window.Handle, DXGI.WindowAssociationFlags.IgnoreAll);

    // Create depth buffer description
    DX10.Texture2DDescription depthDesc = new DX10.Texture2DDescription
    {
        Width = Window.ClientSize.Width,
        Height = Window.ClientSize.Height,
        MipLevels = 1,
        ArraySize = 1,
        Format = DXGI.Format.D24_UNorm_S8_UInt,
        SampleDescription = new DXGI.SampleDescription(1, 0),
        Usage = DX10.ResourceUsage.Default,
        BindFlags = DX10.BindFlags.DepthStencil,
        CpuAccessFlags = DX10.CpuAccessFlags.None,
        OptionFlags = DX10.ResourceOptionFlags.None
    };

    // Get depth buffer
    DX10.Texture2D depthStencilBuffer = new DX10.Texture2D(Device, depthDesc);
    DX10.DepthStencilView depthStencilView = new DX10.DepthStencilView(Device, depthStencilBuffer);
    depthStencilBuffer.Dispose();

I am rendering a simple box. I also checked

Device.CheckMultisampleQualityLevels(DXGI.Format.R8G8B8A8_UNorm, 4)

with 1, 2, 4, 8 and 16 samples. Each time thefunction returns 1. However, when I put 1 into the SwapChainDescription SampleDescription Quality, the device creation throws an error:

E_INVALIDARG: An invalid parameter was passed to the returning function (-2147024809)

I can basically only put 0 for quality count. On my notebook however I can choose different quality levels, but multisampling isn't working there either, and both PC and notebook graphics cards support multisampling.

Is there something I'm doing wrong?


Solution

  • Ok, seemed loke a noob question right now... I simply forgot to initialize the Rasterizer:

    SlimDX.Direct3D10.RasterizerStateDescription rasteriserDesc = new SlimDX.Direct3D10.RasterizerStateDescription()
    {
        CullMode = SlimDX.Direct3D10.CullMode.Back,
        DepthBias = 0,
        DepthBiasClamp = 0,
        FillMode = SlimDX.Direct3D10.FillMode.Solid,
        IsAntialiasedLineEnabled = true,
        IsDepthClipEnabled = true,
        IsFrontCounterclockwise = false,
        IsMultisampleEnabled = true,
        IsScissorEnabled = false,
        SlopeScaledDepthBias = 0
    };
    
    Device.Rasterizer.State = SlimDX.Direct3D10.RasterizerState.FromDescription(device, rasteriserDesc);
    

    Now the Multisampling is working as it should.