2016-07-28 49 views
0

對不起,因爲我的英語不好。我正試圖用SharpDX在Windows 10應用程序上編寫一個非常簡單的DirectX 11,它在窗口中間繪製了一個三角形。問題是三角形不顯示,而我仍然可以更改背景顏色(使用ClearRenderTargetView)。我驗證了渲染函數是週期性調用的,而且我的三角形是前面(順時針)。我曾嘗試:無法在Windows 10通用應用程序上使用SharpDX繪製圖元(但仍能清除背景)

  • 禁用背面剔除
  • 設置靜態的寬度和高度
  • 嘗試其他的圖元(線,三角形條帶)從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); 
    } 

謝謝你的幫助。

回答

0

我已經解決了這個問題。我在Visual Studio中使用了圖形調試器,並檢測到OutputMerger沒有RenderTarget。所以我把

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

移動到RenderScene()函數,它的工作原理。不過,我不明白爲什麼我必須重新設定每一幀。我是Direct3D的新成員,如果有任何人有任何問題,請發表評論。謝謝。

P/s:我已經在GitHub上爲任何遇到同樣問題的人提交了工作項目。

相關問題