WPF 中 DirectX11 中的 SharpDX 2.5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18912194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:41:07  来源:igfitidea点击:

SharpDX 2.5 in DirectX11 in WPF

c#wpfxamldirectx-11sharpdx

提问by martinyyyy

I'm trying to implement DirectX 11 using SharpDX 2.5 into WPF. Sadly http://directx4wpf.codeplex.com/and http://sharpdxwpf.codeplex.com/don't work properly with SharpDX 2.5. I was also not able to port the WPFHost DX10 sample to DX11 and the full code package of this example is down: http://www.indiedev.de/wiki/DirectX_in_WPF_integrieren

我正在尝试使用 SharpDX 2.5 在 WPF 中实现 DirectX 11。遗憾的是http://directx4wpf.codeplex.com/http://sharpdxwpf.codeplex.com/在 SharpDX 2.5 上不能正常工作。我也无法将 WPFHost DX10 示例移植到 DX11,并且此示例的完整代码包已关闭:http: //www.indiedev.de/wiki/DirectX_in_WPF_integrieren

Can someone suggest another way of implementing?

有人可以建议另一种实施方式吗?

采纳答案by Artiom Ciumac

SharpDX supports WPF via SharpDXElement.

SharpDX 通过SharpDXElement支持 WPF 。

Take a look in the Samplesrepository at the Toolkit.sln- all projects that have WPFin their name use SharpDXElementas rendering surface:

示例存储库中查看Toolkit.sln- 所有WPF名称中都SharpDXElement用作渲染表面的项目:

  • MiniCube.WPF- demonstrates basic SharpDX-WPF integration;
  • MiniCube.SwitchContext.WPF- demonstrates basic scenario when lifetime of the Game instance is different from the lifetime of SharpDXElement (in other words - when there is need to switch game rendering on another surface).
  • MiniCube.SwitchContext.WPF.MVVM- same as above, but more 'MVVM-way'.
  • MiniCube.WPF- 演示基本的 SharpDX-WPF 集成;
  • MiniCube.SwitchContext.WPF- 演示了当游戏实例的生命周期与 SharpDXElement 的生命周期不同时的基本场景(换句话说 - 当需要在另一个表面上切换游戏渲染时)。
  • MiniCube.SwitchContext.WPF.MVVM- 与上面相同,但更多的是“MVVM 方式”。

Update: SharpDX.Toolkit has been deprecated and it is not maintained anymore. It is moved to a separate repository. The Toolkit samples were deleted, however I changed the link to a changeset where they are still present.

更新:SharpDX.Toolkit 已被弃用,不再维护。它被移动到一个单独的存储库。Toolkit 示例已删除,但是我将链接更改为它们仍然存在的变更集。

回答by Ante

You can still use http://sharpdxwpf.codeplex.com/.

您仍然可以使用http://sharpdxwpf.codeplex.com/

In order to work properly with SharpDX 2.5.0 you need to do a few modifications.

为了与 SharpDX 2.5.0 一起正常工作,您需要做一些修改。

1) In project Sharp.WPF in class DXUtils.cs in method

1)在方法类DXUtils.cs中的Sharp.WPF项目中

Direct3D11.Buffer CreateBuffer<T>(this Direct3D11.Device device, T[] range)

add this line

添加这一行

stream.Position = 0;

just after

刚过

stream.WriteRange(range);

So fixed method looks like this:

所以固定方法看起来像这样:

public static Direct3D11.Buffer CreateBuffer<T>(this Direct3D11.Device device, T[] range)
        where T : struct
    {
        int sizeInBytes = Marshal.SizeOf(typeof(T));
        using (var stream = new DataStream(range.Length * sizeInBytes, true, true))
        {
            stream.WriteRange(range);
            stream.Position = 0; // fix
            return new Direct3D11.Buffer(device, stream, new Direct3D11.BufferDescription
            {
                BindFlags = Direct3D11.BindFlags.VertexBuffer,
                SizeInBytes = (int)stream.Length,
                CpuAccessFlags = Direct3D11.CpuAccessFlags.None,
                OptionFlags = Direct3D11.ResourceOptionFlags.None,
                StructureByteStride = 0,
                Usage = Direct3D11.ResourceUsage.Default,
            });
        }
    }

2) And in class D3D11 in file D3D11.cs rename this

2) 在 D3D11.cs 文件中的类 D3D11 中重命名

m_device.ImmediateContext.Rasterizer.SetViewports(new Viewport(0, 0, w, h, 0.0f, 1.0f));

into this

进入这个

  m_device.ImmediateContext.Rasterizer.SetViewport(new Viewport(0, 0, w, h, 0.0f, 1.0f));

i.e. SetViewports into SetViewport.

即 SetViewports 到 SetViewport。

And it should work now.

它现在应该可以工作了。