SharpDX 2.5 in DirectX11 in WPF

前端 未结 2 840
情歌与酒
情歌与酒 2021-02-13 23:31

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

相关标签:
2条回答
  • 2021-02-13 23:57

    SharpDX supports WPF via SharpDXElement.

    Take a look in the Samples repository at the Toolkit.sln - all projects that have WPF in their name use SharpDXElement as rendering surface:

    • 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'.

    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.

    0 讨论(0)
  • 2021-02-14 00:13

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

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

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

    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

    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.

    And it should work now.

    0 讨论(0)
提交回复
热议问题