3
\$\begingroup\$

A few years ago I have followed RasterTek's Direct3D11 tutorials to create a simple application in C++. I'm not trying to convert this very simple sample to C# using SharpDX.

I've got the Window (which I create natively, exactly as in the tutorial) and message loop all set-up. Everything seems to be working. If I make the window's background gray I even see it being gray for a split second, before DirectX takes ownership of it and makes the Window contents black.

So it seems I've done something right, but not everything. Nothing is rendered to the window, not even the clear color. I've compared my code multiple times to my own C++ implementation (which works) and the tutorial's code, but there seems to be a bug in my implementation. Its probably related to how SharpDx wraps the Direct3D library. Maybe I've missed something?

Below are (what I believe to be) the bare essentials. Anyone spot the error? (Or alternatively anyone who can direct me to a good starters tutorial which covers the nuances of SharpDx?)

    private readonly SwapChain SwapChain; private readonly RenderTargetView RenderTargetView; private readonly Texture2D DepthStencilBuffer;
    private readonly DepthStencilState DepthStencilState; private readonly DepthStencilView DepthStencilView; private readonly RasterizerState RasterState;

    private readonly D3D11.Device Device;private readonly DeviceContext DeviceContext;

    public D3DObject(int screenWidth, int screenHeight, IntPtr hwnd)
    {           
        // Set up the swap chain             
        var swapChainDescription = new SwapChainDescription()
        {
            BufferCount = 1,
            Flags = SwapChainFlags.None,
            IsWindowed = true,
            ModeDescription = new ModeDescription(screenWidth, screenHeight, new Rational(0, 1), Format.R8G8B8A8_UNorm),
            OutputHandle = hwnd,
            SampleDescription = new SampleDescription(1, 0),
            SwapEffect = SwapEffect.Discard,
            Usage = Usage.RenderTargetOutput,
        };

        D3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, swapChainDescription, out this.Device, out this.SwapChain);            
        this.DeviceContext = new DeviceContext(this.Device);

        // Get the backbuffer from the swap chain
        var backBuffer = this.SwapChain.GetBackBuffer<D3D11.Resource>(0);

        // Create a view of this backbuffer
        this.RenderTargetView = new RenderTargetView(this.Device, backBuffer);

        // Set the render target on the window            
        this.DeviceContext.OutputMerger.SetRenderTargets(this.RenderTargetView);

        // Set-up the rasterizer
        var rasterizerStateDescription = new RasterizerStateDescription()
        {
            CullMode = CullMode.Back,
            DepthBias = 0,
            DepthBiasClamp = 0.0f,
            FillMode = FillMode.Solid,
            IsAntialiasedLineEnabled = false,
            IsDepthClipEnabled = true,
            IsFrontCounterClockwise = false,
            IsMultisampleEnabled = false,
            IsScissorEnabled = false,
            SlopeScaledDepthBias = 0.0f
        };

        this.RasterState = new RasterizerState(this.Device, rasterizerStateDescription);

        this.DeviceContext.Rasterizer.State = this.RasterState;            
        this.DeviceContext.Rasterizer.SetViewport(0, 0, screenWidth, screenHeight);
    }

    // Called at the start of rendering
    public void BeginScene(Color4 color) // Color is CornFlowerBlue: {Alpha:1 Red:0,3921569 Green:0,5843138 Blue:0,9294118}
    {
        this.DeviceContext.ClearRenderTargetView(this.RenderTargetView, color);            
    }

    // Note: nothing is rendered in between

    // Called at the end of rendering
    public void EndScene()
    {
        this.SwapChain.Present(0, PresentFlags.None);
    }

Note: I create the device with the DebugFlag, but I also see no debug overlay.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.