SharpDX 2.5 in DirectX11 in WPF
Asked Answered
T

2

12

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

Can someone suggest another way of implementing?

Tomas answered 20/9, 2013 at 8:33 Comment(1)
Why can't you port the DX10 sample? Details will help others help you. Also, here's a similar question: https://mcmap.net/q/1011690/-wpf-and-directx-11-via-d3dimage/1339280Hypothalamus
O
4

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.

Overwork answered 6/11, 2013 at 12:16 Comment(2)
The links to SharpDXElement are broken. Please update them.Aphorism
Also, I'm having a hard time finding toolkit.sln or any projects with WPF in their names.Aphorism
O
3

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.

Oratorio answered 5/11, 2013 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.