Direct3D Wrapper - C# or Managed C++ ?
-
Hello, I'm interested in writing a managed Direct3D 9/10 wrapper on my own for learning purpose. I pretty much aware about existing MDX (which is deprecate) and XNA (which is multiplatform, but less performant). While searching for something already existing, I found a MDX 9/10/10.1 wrapper written by Ralf Kornmann http://www.codeplex.com/MD3D10[^] This wrapper uses Managed C++ to wrap the D3D Api, but a lot of other OpenGL wrappers, uses a standard C# InterOp syntax, decorating and Marshalling. I have concern about performance issues so my main question is: what is actually faster, wrap Direct3D with Managed C++ or in C# using standard C# InterOp ? Is there some performance issues? What do you think? What I mean by C# Style wrapper:
public static class DirectX
{
/* IDirect3D9* Direct3DCreate9(
UINT SDKVersion
);*/
[DllImport("d3d9.dll", EntryPoint = "Direct3DCreate9", CallingConvention = CallingConvention.Winapi), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Interface)]
public static extern IDirect3D9 Direct3DCreate9(UInt32 SDKVersion);}
/* DECLARE_INTERFACE_(IDirect3D9, IUnknown)*/
[ComVisible(true), ComImport]
[Guid("81BDCBCA-64D4-426d-AE8D-AD0147F4275C"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDirect3D9 // http://msdn.microsoft.com/en-us/library/bb174300(VS.85).aspx
{
/* UINT GetAdapterCount(
);*/
[PreserveSig]
UInt32 GetAdapterCount();/\* HRESULT GetAdapterDisplayMode( UINT Adapter, D3DDISPLAYMODE\* pMode );\*/ \[PreserveSig\] int GetAdapterDisplayMode(UInt32 Adapter, \[In, Out\] D3DDISPLAYMODE pMode); ... }
-- Everything is possible, even the impossible! ^_^
-
Hello, I'm interested in writing a managed Direct3D 9/10 wrapper on my own for learning purpose. I pretty much aware about existing MDX (which is deprecate) and XNA (which is multiplatform, but less performant). While searching for something already existing, I found a MDX 9/10/10.1 wrapper written by Ralf Kornmann http://www.codeplex.com/MD3D10[^] This wrapper uses Managed C++ to wrap the D3D Api, but a lot of other OpenGL wrappers, uses a standard C# InterOp syntax, decorating and Marshalling. I have concern about performance issues so my main question is: what is actually faster, wrap Direct3D with Managed C++ or in C# using standard C# InterOp ? Is there some performance issues? What do you think? What I mean by C# Style wrapper:
public static class DirectX
{
/* IDirect3D9* Direct3DCreate9(
UINT SDKVersion
);*/
[DllImport("d3d9.dll", EntryPoint = "Direct3DCreate9", CallingConvention = CallingConvention.Winapi), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Interface)]
public static extern IDirect3D9 Direct3DCreate9(UInt32 SDKVersion);}
/* DECLARE_INTERFACE_(IDirect3D9, IUnknown)*/
[ComVisible(true), ComImport]
[Guid("81BDCBCA-64D4-426d-AE8D-AD0147F4275C"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDirect3D9 // http://msdn.microsoft.com/en-us/library/bb174300(VS.85).aspx
{
/* UINT GetAdapterCount(
);*/
[PreserveSig]
UInt32 GetAdapterCount();/\* HRESULT GetAdapterDisplayMode( UINT Adapter, D3DDISPLAYMODE\* pMode );\*/ \[PreserveSig\] int GetAdapterDisplayMode(UInt32 Adapter, \[In, Out\] D3DDISPLAYMODE pMode); ... }
-- Everything is possible, even the impossible! ^_^
Kel_ wrote:
what is actually faster, wrap Direct3D with Managed C++ or in C# using standard C#
In theory, both compile to IL so there is no difference. In practise, the compilers may produce slightly differing output. The only way to know for certain would be to run some tests. I suspect there is no best answer for all cases, one way will be better in some circumstances, the other in different circumstances.
Simon
-
Hello, I'm interested in writing a managed Direct3D 9/10 wrapper on my own for learning purpose. I pretty much aware about existing MDX (which is deprecate) and XNA (which is multiplatform, but less performant). While searching for something already existing, I found a MDX 9/10/10.1 wrapper written by Ralf Kornmann http://www.codeplex.com/MD3D10[^] This wrapper uses Managed C++ to wrap the D3D Api, but a lot of other OpenGL wrappers, uses a standard C# InterOp syntax, decorating and Marshalling. I have concern about performance issues so my main question is: what is actually faster, wrap Direct3D with Managed C++ or in C# using standard C# InterOp ? Is there some performance issues? What do you think? What I mean by C# Style wrapper:
public static class DirectX
{
/* IDirect3D9* Direct3DCreate9(
UINT SDKVersion
);*/
[DllImport("d3d9.dll", EntryPoint = "Direct3DCreate9", CallingConvention = CallingConvention.Winapi), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Interface)]
public static extern IDirect3D9 Direct3DCreate9(UInt32 SDKVersion);}
/* DECLARE_INTERFACE_(IDirect3D9, IUnknown)*/
[ComVisible(true), ComImport]
[Guid("81BDCBCA-64D4-426d-AE8D-AD0147F4275C"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDirect3D9 // http://msdn.microsoft.com/en-us/library/bb174300(VS.85).aspx
{
/* UINT GetAdapterCount(
);*/
[PreserveSig]
UInt32 GetAdapterCount();/\* HRESULT GetAdapterDisplayMode( UINT Adapter, D3DDISPLAYMODE\* pMode );\*/ \[PreserveSig\] int GetAdapterDisplayMode(UInt32 Adapter, \[In, Out\] D3DDISPLAYMODE pMode); ... }
-- Everything is possible, even the impossible! ^_^
The .NET devs said that the speed difference between native DirectX and Managed DirectX was about 6%, so I suspect the difference between a managed wrapper in C++/CLI and C# won't be very large. But as Simon said, only tests will show. But this can be a tedious task, since you have to create two wrappers with the same functionality. regards
-
Hello, I'm interested in writing a managed Direct3D 9/10 wrapper on my own for learning purpose. I pretty much aware about existing MDX (which is deprecate) and XNA (which is multiplatform, but less performant). While searching for something already existing, I found a MDX 9/10/10.1 wrapper written by Ralf Kornmann http://www.codeplex.com/MD3D10[^] This wrapper uses Managed C++ to wrap the D3D Api, but a lot of other OpenGL wrappers, uses a standard C# InterOp syntax, decorating and Marshalling. I have concern about performance issues so my main question is: what is actually faster, wrap Direct3D with Managed C++ or in C# using standard C# InterOp ? Is there some performance issues? What do you think? What I mean by C# Style wrapper:
public static class DirectX
{
/* IDirect3D9* Direct3DCreate9(
UINT SDKVersion
);*/
[DllImport("d3d9.dll", EntryPoint = "Direct3DCreate9", CallingConvention = CallingConvention.Winapi), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Interface)]
public static extern IDirect3D9 Direct3DCreate9(UInt32 SDKVersion);}
/* DECLARE_INTERFACE_(IDirect3D9, IUnknown)*/
[ComVisible(true), ComImport]
[Guid("81BDCBCA-64D4-426d-AE8D-AD0147F4275C"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDirect3D9 // http://msdn.microsoft.com/en-us/library/bb174300(VS.85).aspx
{
/* UINT GetAdapterCount(
);*/
[PreserveSig]
UInt32 GetAdapterCount();/\* HRESULT GetAdapterDisplayMode( UINT Adapter, D3DDISPLAYMODE\* pMode );\*/ \[PreserveSig\] int GetAdapterDisplayMode(UInt32 Adapter, \[In, Out\] D3DDISPLAYMODE pMode); ... }
-- Everything is possible, even the impossible! ^_^
Thank you for the answers. Yeah, so basically I think I'm just going to wrap the whole DirectX in C# and then test it against the managed C++ implementation. Think it will worth a good codeproject article :)
-- Everything is possible, even the impossible! ^_^
-
Hello, I'm interested in writing a managed Direct3D 9/10 wrapper on my own for learning purpose. I pretty much aware about existing MDX (which is deprecate) and XNA (which is multiplatform, but less performant). While searching for something already existing, I found a MDX 9/10/10.1 wrapper written by Ralf Kornmann http://www.codeplex.com/MD3D10[^] This wrapper uses Managed C++ to wrap the D3D Api, but a lot of other OpenGL wrappers, uses a standard C# InterOp syntax, decorating and Marshalling. I have concern about performance issues so my main question is: what is actually faster, wrap Direct3D with Managed C++ or in C# using standard C# InterOp ? Is there some performance issues? What do you think? What I mean by C# Style wrapper:
public static class DirectX
{
/* IDirect3D9* Direct3DCreate9(
UINT SDKVersion
);*/
[DllImport("d3d9.dll", EntryPoint = "Direct3DCreate9", CallingConvention = CallingConvention.Winapi), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Interface)]
public static extern IDirect3D9 Direct3DCreate9(UInt32 SDKVersion);}
/* DECLARE_INTERFACE_(IDirect3D9, IUnknown)*/
[ComVisible(true), ComImport]
[Guid("81BDCBCA-64D4-426d-AE8D-AD0147F4275C"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDirect3D9 // http://msdn.microsoft.com/en-us/library/bb174300(VS.85).aspx
{
/* UINT GetAdapterCount(
);*/
[PreserveSig]
UInt32 GetAdapterCount();/\* HRESULT GetAdapterDisplayMode( UINT Adapter, D3DDISPLAYMODE\* pMode );\*/ \[PreserveSig\] int GetAdapterDisplayMode(UInt32 Adapter, \[In, Out\] D3DDISPLAYMODE pMode); ... }
-- Everything is possible, even the impossible! ^_^
You may also want to take a look at SlimDX - an open source wrapper intended as a replacement for Managed DirectX. While C# is capable enough with unsafe code and interop, I'd suggest using C++/CLI if you are comfortable with it. Downside is of course more complexity.
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games. -
Hello, I'm interested in writing a managed Direct3D 9/10 wrapper on my own for learning purpose. I pretty much aware about existing MDX (which is deprecate) and XNA (which is multiplatform, but less performant). While searching for something already existing, I found a MDX 9/10/10.1 wrapper written by Ralf Kornmann http://www.codeplex.com/MD3D10[^] This wrapper uses Managed C++ to wrap the D3D Api, but a lot of other OpenGL wrappers, uses a standard C# InterOp syntax, decorating and Marshalling. I have concern about performance issues so my main question is: what is actually faster, wrap Direct3D with Managed C++ or in C# using standard C# InterOp ? Is there some performance issues? What do you think? What I mean by C# Style wrapper:
public static class DirectX
{
/* IDirect3D9* Direct3DCreate9(
UINT SDKVersion
);*/
[DllImport("d3d9.dll", EntryPoint = "Direct3DCreate9", CallingConvention = CallingConvention.Winapi), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Interface)]
public static extern IDirect3D9 Direct3DCreate9(UInt32 SDKVersion);}
/* DECLARE_INTERFACE_(IDirect3D9, IUnknown)*/
[ComVisible(true), ComImport]
[Guid("81BDCBCA-64D4-426d-AE8D-AD0147F4275C"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDirect3D9 // http://msdn.microsoft.com/en-us/library/bb174300(VS.85).aspx
{
/* UINT GetAdapterCount(
);*/
[PreserveSig]
UInt32 GetAdapterCount();/\* HRESULT GetAdapterDisplayMode( UINT Adapter, D3DDISPLAYMODE\* pMode );\*/ \[PreserveSig\] int GetAdapterDisplayMode(UInt32 Adapter, \[In, Out\] D3DDISPLAYMODE pMode); ... }
-- Everything is possible, even the impossible! ^_^
Thanks again. So basically I started the C# Wrapper to be able to compare it, after a few days and going to battle vs COM objets and performant InterOp, I finally managed to wrap a part of it. For now I'm doing just the D3DDevice creation and a simple window clear. I compared the exactly same function calls with Managed DirectX 1.1 ones (actually everything is the same, I compared every method parameter and call with PIX debugger). So creating D3D factory, creating a device and showing. (Just a first draft)
DirectX.Device.Clear(0, null, ClearFlags.D3DCLEAR_TARGET | ClearFlags.D3DCLEAR_ZBUFFER, 0, 1, 0);
DirectX.Device.BeginScene();
// todo
DirectX.Device.EndScene();
DirectX.Device.Present(null, null, IntPtr.Zero, null);And guess what, the performance are almost exactly the same between MDX and my C# Wrapper. I'm getting around 2180 FPS with both of them. I still would like to check IL code generated for both.
-- Everything is possible, even the impossible! ^_^