无法在Windows 10 Universal App上使用SharpDX绘制基元(但仍能清除背景)

时间:2016-07-28 16:02:44

标签: c# directx directx-11 windows-10-universal sharpdx

抱歉因为我的英语不好。我试图在Windows 10应用程序上使用SharpDX编写一个非常简单的DirectX 11,它在窗口中间绘制一个三角形。问题是当我仍然可以更改背景颜色时不显示三角形(使用ClearRenderTargetView)。我确认定期调用渲染函数,我的三角形是正面(顺时针)。我尝试过:

  • 禁用背面剔除
  • 设置静态宽度和高度
  • 尝试其他基元(线条,三角形条带)
  • 将顶点着色器输入从float3更改为float4,反之亦然

我发现这个post有非常相似的症状,但仍无效!

我已将代码发布在GitHub上:https://github.com/minhcly/UWP3DTest

这是我的初始化代码(我认为问题所在的地方):

    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()函数:

    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;
    }

渲染功能:

    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);
    }

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我已经解决了这个问题。我在Visual Studio中使用了图形调试器,并检测到OutputMerger没有RenderTarget。所以我移动了一行

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

到RenderScene()函数,它的工作原理。但是,我无法理解为什么我必须重置每一帧。我是Direct3D的新手,所以如果有人有anwswer,请发表评论。谢谢。

P / s:我已经在GitHub上为那些遇到同样问题的人提交了工作项目。