Login
 
Archive
Links
Search
Blog List
There are no categories in this blog.
 
Sep 15

Written by: Jeremiah Morrill
9/15/2008 8:20 AM 

So I've decided to drop the SlimDX dependency from the WPF MediaKit.  There's a couple reasons for this.  SlimDX is written in CLI/C++, so it has a hard dependency to 32bit or 64bit code.  This means that you cannot build your application as "ANY CPU" and have the JITter run automatically as 32 or 64bit code.  With SlimDX, you either have to install both 64bit and 32bit assemblies in the GAC, or swap out the 32bit assembly with the 64bit one (or vice-versa).  There's also a issue I was having with SlimDX requiring the latest [August] DX runtimes, which was about 30 megs.  This was just way too much inconvenience.

My solution is to write my own managed Direct3D, which is not really an wrapper, but more of an interop.  This will allow for compiling as "ANY CPU" and have it automatically run as 32/64bit, no CLI/C++ and no extra dependencies.

I am not going to convert all of Direct3D, but only the structures and interfaces I need specifically, which really isn't that much.  It's a pretty tedious task, but I'm about 70% done!  I'm not sure why SlimDX didn't go this route over the CLI/C++ (probably so they could give it a .NET feel), but I wish there was a library already like this so I wouldn't have had to go through the trouble.

If you are curious what the interop looks like, here's a taste:

public class DirectX
{
    [DllImport("d3d9.dll", EntryPoint = "Direct3DCreate9", CallingConvention = CallingConvention.StdCall),
     SuppressUnmanagedCodeSecurity]
    [return: MarshalAs(UnmanagedType.Interface)]
    public static extern IDirect3D9 Direct3DCreate9(ushort SDKVersion);

    [DllImport("d3d9.dll", EntryPoint = "Direct3DCreate9Ex", CallingConvention = CallingConvention.StdCall),
    SuppressUnmanagedCodeSecurity]
    [return: MarshalAs(UnmanagedType.Interface)]
    public static extern IDirect3D9Ex Direct3DCreate9Ex(ushort SDKVersion);
}
/// <summary>
/// CLSID_IDirect3D9
/// </summary>
[ComImport, Guid("81BDCBCA-64D4-426d-AE8D-AD0147F4275C"),
 InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDirect3D9
{
    [PreserveSig]
    int RegisterSoftwareDevice([In, Out]IntPtr pInitializeFunction);

    [PreserveSig]
    int GetAdapterCount();

    [PreserveSig]
    int GetAdapterIdentifier(uint Adapter, uint Flags, uint pIdentifier);

    [PreserveSig]
    uint GetAdapterModeCount(uint Adapter, D3DFORMAT Format);

    [PreserveSig]
    int EnumAdapterModes(uint Adapter, D3DFORMAT Format, uint Mode, [Out] out D3DDISPLAYMODE  pMode);

    [PreserveSig]
    int GetAdapterDisplayMode(ushort Adapter, [Out]out D3DFORMAT Format);

    #region Method Placeholders
    [PreserveSig]
    int CheckDeviceType();

    [PreserveSig]
    int CheckDeviceFormat();

    [PreserveSig]
    int CheckDeviceMultiSampleType();

    [PreserveSig]
    int CheckDepthStencilMatch();

    [PreserveSig]
    int CheckDeviceFormatConversion();

    [PreserveSig]
    int GetDeviceCaps();
    #endregion

    [PreserveSig]
    IntPtr GetAdapterMonitor(uint Adapter);

    [PreserveSig]
    int CreateDevice(int Adapter,
                      D3DDEVTYPE DeviceType, 
                      IntPtr hFocusWindow,
                      CreateFlags BehaviorFlags,
                      [In, Out]
                      ref D3DPRESENT_PARAMETERS pPresentationParameters, 
                      [Out]out IntPtr ppReturnedDeviceInterface);
}

Tags:

3 comment(s) so far...

Re: Dropping SlimDX - But why?

Your choice seems reasonable and very interesting. You are building it as an library which is not tightly coupled with your MediaKit? I have run into similar issues with SlimDX as you are describing. Too many big dependencies.

Maybe if you could publish your DirectX interop layer as an open source project. It does not matter even if you are building it for MediaKit when other people could extend it to their own needs. Maybe someday it could be almost full interop layer for DirectX.

By Pekka Heikura on   9/15/2008 9:56 AM

Re: Dropping SlimDX - But why?

This is going to be in the same assembly as all of the MediaKit. The only dependency that it will have beyond the BCL is the DirectShow.NET.

I think that would be rad if the D3D stuff grew into it's own project. I really only have plans to implement the interfaces/methods that I need specifically for MediaKit, which are relatively few. It really isn't a whole lot of fun translating the C++ COM stuff, but the end result is so worth it :)

By Jeremiah Morrill on   9/15/2008 10:02 AM

Re: Dropping SlimDX - But why?

Well, a couple comments.

* SlimDX does a lot of interop that is extremely difficult, and sometimes outright impossible, to do without C++/CLI. Native code generation is also a performance benefit in some places. Doing it in C# would have worked to a point, but it isn't ideal by any means.
* SlimDX apps don't require the DX redist to be installed, because the SlimDX installer (8MB download) already includes just the necessary bits. Of course this is only useful if the SlimDX installer is something appropriate for you in the first place, and our installer has some issues (no silent install support, for one). Trying to improve this, but building installers is a pain.
* The bitness thing is weird, but it's my understanding -- with the caveat that I haven't tested this -- that if you have the x64 version installed on a 64 bit system, Any CPU should work fine. But this is ONLY if you edit the csproj file by hand and remove the processorArchitecture attribute from the reference. The 32 bit shouldn't be required after you do that. I don't know why VS adds that tag, and it's something I'm trying to look into, but I probably have to go ask the VS team directly.

Anyway, I'm not trying to convert you or anything, just clarifying. It's quite important to me personally to see these kinds of issues that people are having with SlimDX and understand them in detail, to see if there's anything we can do better in the future. Please feel free to email me if you want to discuss further.

By Promit on   9/21/2008 4:15 PM

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 
  Minimize
Text/HTML Minimize
Copyright 2007
Downloaded from DNNSkins.com