SharpDX render in WPF

谁说我不能喝 提交于 2019-12-03 00:07:11

SharpDX Toolkit has support for WPF via a Direct3D11-to-Direct3D9 shared texture. It is implemented in the SharpDXElement class.

You may not be able to reuse this as is because Direct2D (which you are using to draw) can interop either with Direct3D11.1 or Direct3D10 and SharpDX uses Direct3D11 for WPF support, so you will need to tweak the solution a little bit.

Basically you need to do the following:

  • Initialize Direct3D (10 or 11.1).
  • Initialize Direct2D.
  • Create the D3D render target with Direct2D support and the Shared flag (here is how it is done in SharpDX).
  • Initialize Direct3D9.
  • Create the shared texture.
  • Bind the texture to an D3DImage.

Do not forget to call D3DImage.AddDirtyRect when the contents of the render target are updated.

From the code you provided it is not clear if you are doing all initializations only once or not, so try to call any initialization code only once and reuse the render target - just clear it at the beginning of every frame. This is mandatory to get a decent performance.

Update: SharpDX.Toolkit has been deprecated and it is not maintained anymore. It is moved to a separate repository.

If you want to share a directx surface with WPF, the best option is using a WPF D3DImage. It promises to work without copying if you use the right color format.

I have only used it with Directx9, but it is possible that its compatible with Direct2D too, but if it isn't D3d9 can draw lines too.

There's a great codeproject article with examples. From managed code using slimdx or sharpdx the only nonobvious caveat is that D3DImage retains a reference count to your DirectX surface, so you need to null the backbuffer expicitly when you want to reset your d3d device.

You can use Smartrak/WpfSharpDxControl: Provides WPF control to host SharpDx content.

It uses sharpDx in WPF and the wpf can add Win32HwndControl to HwndHost.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!