Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Direct3D Wrapper - C# or Managed C++ ?

Direct3D Wrapper - C# or Managed C++ ?

Scheduled Pinned Locked Moved C#
questioncomgraphicsgame-devcsharp
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kel_
    wrote on last edited by
    #1

    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! ^_^

    S L K M 5 Replies Last reply
    0
    • K Kel_

      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! ^_^

      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • K Kel_

        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! ^_^

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • K Kel_

          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! ^_^

          K Offline
          K Offline
          Kel_
          wrote on last edited by
          #4

          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! ^_^

          1 Reply Last reply
          0
          • K Kel_

            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! ^_^

            M Offline
            M Offline
            Mark Churchill
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • K Kel_

              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! ^_^

              K Offline
              K Offline
              Kel_
              wrote on last edited by
              #6

              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! ^_^

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups