Search code examples
c#directxrenderingdirectx-11sharpdx

Sharpdx - Depthstencil not working when using MRT (Multiple Render Targets)


been trying to change a renderer I wrote from SlimDX to SharpDX and ran into a problem. I want to render to multiple render targets (in this case color and object ID for picking)

This is the initialization of the rendertargets (all with same dimension and multisample setting)

        //Swapchain, Device, Primary Rendertarget
        var description = new SwapChainDescription()
        {
            BufferCount = 1,
            Usage = Usage.RenderTargetOutput,
            OutputHandle = Form.Handle,
            IsWindowed = true,
            ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
            SampleDescription = new SampleDescription(1, 0),
            Flags = SwapChainFlags.AllowModeSwitch,
            SwapEffect = SwapEffect.Discard
        };

        this.Device = new Device(adapter);
        this.SwapChain = new SwapChain(factory, Device, description);

        this.backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(SwapChain, 0);
        this.RenderTargetView = new RenderTargetView(Device, backBuffer);

        //Depthbuffer
        Texture2DDescription descDepth = new Texture2DDescription();
        descDepth.Width = (int)Viewport.Width;
        descDepth.Height = (int)Viewport.Height;
        descDepth.MipLevels = 1;
        descDepth.ArraySize = 1;
        descDepth.Format = Format.D32_Float;
        descDepth.Usage = ResourceUsage.Default;
        descDepth.SampleDescription = new SampleDescription(1, 0);
        descDepth.BindFlags = BindFlags.DepthStencil;
        descDepth.CpuAccessFlags = 0;
        descDepth.OptionFlags = 0;

        using (Texture2D depthStencil = new Texture2D(Device, descDepth))
        {
            depthView = new DepthStencilView(Device, depthStencil);
        }

        //Rendertargetview for the ID
        Texture2DDescription IdMapDesc = new Texture2DDescription();
        IdMapDesc.Width = (int)Viewport.Width;
        IdMapDesc.Height = (int)Viewport.Height;
        IdMapDesc.ArraySize = 1;
        IdMapDesc.MipLevels = 1;
        IdMapDesc.Format = Format.R16_UInt;
        IdMapDesc.Usage = ResourceUsage.Default;
        IdMapDesc.SampleDescription = new SampleDescription(1, 0);
        IdMapDesc.BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget;
        IdMapDesc.CpuAccessFlags = 0;
        IdMapDesc.OptionFlags = 0;

        using (Texture2D idMap = new Texture2D(Device, IdMapDesc))
        {
            idView = new RenderTargetView(Device, idMap);
        }

This is how I do the Rendering

public override void Render()
    {
        Context.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1f, 0);

        Context.OutputMerger.SetTargets(depthView, RenderTargetView);

        staticMeshRenderer.UpdateCameraConstants();

        foreach(TerrainSegment segment in terrain.SegmentMap) 
        {
            terrainRenderer.Draw(segment, null);
        }

        objectManager.DrawContent(Device);
    }

producing this output (can't post images, it's a scene with working depthstencil)

However when using multiple rendertargets like this

Context.OutputMerger.SetTargets(depthView, RenderTargetView, idView);

the Depthstencil stops doing its job. HLSL code used for both attempts:

struct PS_Output
{
    float4 Color        : SV_TARGET0;
    uint ID             : SV_TARGET1;
};

PS_Output PShader(VS_OutputStatic input)
{
    PS_Output output;
    output.ID = 3; //test
    output.Color = Diffuse.Sample(StateLinear, input.TexCoords).rgba;
    return output;
}

What am I doing wrong here? Thanks in advance!


Solution

  • Debugging with RenderDoc showed that my Buffer Dimensions were NOT the same. Fixing that solved the problem.