1
votes

Sorry because of my bad English. I am trying to write a very simple DirectX 11 on Windows 10 app with SharpDX, which draws a triangle at the middle of the windows. The problem is the triangle is not displayed while I can still change the background color (using ClearRenderTargetView). I verified that the render function is called periodically and my triangle is front-face (clockwise). What I have tried:

  • Disable back-face culling
  • Set static width and height
  • Try other primitives (line, triangle strip)
  • Change vertex shader input from float3 to float4 and vice-versa

I found that this post has very similar symptoms but still not work!

I have posted my code on GitHub at: https://github.com/minhcly/UWP3DTest

Here is my initialization code (where I think the problem resides):

    D3D11.Device2 device;
    D3D11.DeviceContext deviceContext;
    DXGI.SwapChain2 swapChain;
    D3D11.Texture2D backBufferTexture;
    D3D11.RenderTargetView backBufferView;

    private void InitializeD3D()
    {
        using (D3D11.Device defaultDevice = new D3D11.Device(D3D.DriverType.Hardware, D3D11.DeviceCreationFlags.Debug))
            this.device = defaultDevice.QueryInterface<D3D11.Device2>();
        this.deviceContext = this.device.ImmediateContext2;

        DXGI.SwapChainDescription1 swapChainDescription = new DXGI.SwapChainDescription1()
        {
            AlphaMode = DXGI.AlphaMode.Ignore,
            BufferCount = 2,
            Format = DXGI.Format.R8G8B8A8_UNorm,
            Height = (int)(this.swapChainPanel.RenderSize.Height),
            Width = (int)(this.swapChainPanel.RenderSize.Width),
            SampleDescription = new DXGI.SampleDescription(1, 0),
            Scaling = SharpDX.DXGI.Scaling.Stretch,
            Stereo = false,
            SwapEffect = DXGI.SwapEffect.FlipSequential,
            Usage = DXGI.Usage.RenderTargetOutput
        };

        using (DXGI.Device3 dxgiDevice3 = this.device.QueryInterface<DXGI.Device3>())
        using (DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent<DXGI.Factory3>())
        {
            DXGI.SwapChain1 swapChain1 = new DXGI.SwapChain1(dxgiFactory3, this.device, ref swapChainDescription);
            this.swapChain = swapChain1.QueryInterface<DXGI.SwapChain2>();
        }

        using (DXGI.ISwapChainPanelNative nativeObject = ComObject.As<DXGI.ISwapChainPanelNative>(this.swapChainPanel))
            nativeObject.SwapChain = this.swapChain;

        this.backBufferTexture = this.swapChain.GetBackBuffer<D3D11.Texture2D>(0);
        this.backBufferView = new D3D11.RenderTargetView(this.device, this.backBufferTexture);
        this.deviceContext.OutputMerger.SetRenderTargets(this.backBufferView);

        deviceContext.Rasterizer.State = new D3D11.RasterizerState(device, new D3D11.RasterizerStateDescription()
        {
            CullMode = D3D11.CullMode.None,
            FillMode = D3D11.FillMode.Solid,
            IsMultisampleEnabled = true
        });
        deviceContext.Rasterizer.SetViewport(0, 0, (int)swapChainPanel.Width, (int)swapChainPanel.Height);

        CompositionTarget.Rendering += CompositionTarget_Rendering;
        Application.Current.Suspending += Current_Suspending;

        isDXInitialized = true;
    }

InitScene() function:

    D3D11.Buffer triangleVertBuffer;
    D3D11.VertexShader vs;
    D3D11.PixelShader ps;
    D3D11.InputLayout vertLayout;
    RawVector3[] verts;

    private void InitScene()
    {
        D3D11.InputElement[] inputElements = new D3D11.InputElement[]
        {
            new D3D11.InputElement("POSITION", 0, DXGI.Format.R32G32B32_Float, 0)
        };

        using (CompilationResult vsResult = ShaderBytecode.CompileFromFile("vs.hlsl", "main", "vs_4_0"))
        {
            vs = new D3D11.VertexShader(device, vsResult.Bytecode.Data);
            vertLayout = new D3D11.InputLayout(device, vsResult.Bytecode, inputElements);
        }

        using (CompilationResult psResult = ShaderBytecode.CompileFromFile("ps.hlsl", "main", "ps_4_0"))
            ps = new D3D11.PixelShader(device, psResult.Bytecode.Data);

        deviceContext.VertexShader.Set(vs);
        deviceContext.PixelShader.Set(ps);

        verts = new RawVector3[] {
            new RawVector3( 0.0f, 0.5f, 0.5f ),
            new RawVector3( 0.5f, -0.5f, 0.5f ),
            new RawVector3( -0.5f, -0.5f, 0.5f )
        };
        triangleVertBuffer = D3D11.Buffer.Create(device, D3D11.BindFlags.VertexBuffer, verts);

        deviceContext.InputAssembler.InputLayout = vertLayout;
        deviceContext.InputAssembler.PrimitiveTopology = D3D.PrimitiveTopology.TriangleList;
    }

Render function:

    private void RenderScene()
    {
        this.deviceContext.ClearRenderTargetView(this.backBufferView, new RawColor4(red, green, blue, 0));

        deviceContext.InputAssembler.SetVertexBuffers(0,
            new D3D11.VertexBufferBinding(triangleVertBuffer, Utilities.SizeOf<RawVector3>(), 0));
        deviceContext.Draw(verts.Length, 0);

        this.swapChain.Present(0, DXGI.PresentFlags.None);
    }

Thank you for your help.

1

1 Answers

1
votes

I have solved the problem. I have used the Graphics Debugger in Visual Studio and detect that the OutputMerger has no RenderTarget. So I move the line

this.deviceContext.OutputMerger.SetRenderTargets(this.backBufferView);

to the RenderScene() function and it works. However, I can't understand why I must reset this every frame. I'm new in Direct3D so if any one has an anwswer, please comment. Thank you.

P/s: I have committed the working project on the GitHub for anyone who encounters the same problem with me.