sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.69k stars 638 forks source link

SharpDX & Unity: Creating Device from cpp Pointer passed through callback function #899

Closed marwie closed 7 years ago

marwie commented 7 years ago

Hello,

I tried to pass a ID3D11Device from an unmanaged plugin callback function back into managed code in unity and use Direct3D11.Device.FromPointer() to get access to unitys graphics device in managed code.

In the editor I got it working so far, but as I try to build it I get error messages that I don't really understand. I hope maybe someone knows how to fix this or if this is just totally stupid :)

I'm using unity 2017.1 and testing the experimental feature NET 4.6 Equivalent. Maybe it's just a unity thing...

Here is the error message from my build:

System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> System.TypeLoadException: Could not load type 'SharpDX.ResultDescriptor' from assembly 'SharpDX, Version=3.1.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1'.
  at <Module>..cctor () [0x00000] in <7c7700b731ae45aa83a0028bfca190e7>:0 
   --- End of inner exception stack trace ---
  at UseRenderingPlugin+<CallPluginAtEndOfFrames>c__Iterator1.<>m__0 (System.IntPtr devicePtr) [0x00029] in D:\Projects\Git\unity-shared-texture\NativeRenderingPlugin\UnityProject\Assets\UseRenderingPlugin.cs:185 
UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:LogError(Object)
<CallPluginAtEndOfFrames>c__Iterator1:<>m__0(IntPtr) (at D:\Projects\Git\unity-shared-texture\NativeRenderingPlugin\UnityProject\Assets\UseRenderingPlugin.cs:190)
UseRenderingPlugin:GetDevice(Action`1)
<CallPluginAtEndOfFrames>c__Iterator1:MoveNext() (at D:\Projects\Git\unity-shared-texture\NativeRenderingPlugin\UnityProject\Assets\UseRenderingPlugin.cs:148)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) (at C:\buildslave\unity\build\Runtime\Export\Coroutines.cs:17)

(Filename: D:/Projects/Git/unity-shared-texture/NativeRenderingPlugin/UnityProject/Assets/UseRenderingPlugin.cs Line: 190)
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> System.BadImageFormatException: Could not resolve field token 0x04000017
  at SharpDX.Direct3D11.ModuleInit.Setup () [0x00000] in <9f3c1ead68e44b80a8bdf1830da90caa>:0 
  at <Module>..cctor () [0x00000] in <9f3c1ead68e44b80a8bdf1830da90caa>:0 
   --- End of inner exception stack trace ---
  at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
  at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:661 
   --- End of inner exception stack trace ---
  at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00013] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:667 
  at System.Reflection.MonoCMethod.DoInvoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00089] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:652 
  at System.Reflection.MonoCMethod.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:680 
  at System.RuntimeType.CreateInstanceImpl (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes, System.Threading.StackCrawlMark& stackMark) [0x00243] in /Users/builduser/buildslave/mono/build/mcs/class/referencesource/mscorlib/system/rttype.cs:5394 
UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:LogError(Object)
<CallPluginAtEndOfFrames>c__Iterator1:<>m__0(IntPtr) (at D:\Projects\Git\unity-shared-texture\NativeRenderingPlugin\UnityProject\Assets\UseRenderingPlugin.cs:190)
UseRenderingPlugin:GetDevice(Action`1)
<CallPluginAtEndOfFrames>c__Iterator1:MoveNext() (at D:\Projects\Git\unity-shared-texture\NativeRenderingPlugin\UnityProject\Assets\UseRenderingPlugin.cs:148)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) (at C:\buildslave\unity\build\Runtime\Export\Coroutines.cs:17)

(Filename: D:/Projects/Git/unity-shared-texture/NativeRenderingPlugin/UnityProject/Assets/UseRenderingPlugin.cs Line: 190)

here is what I use to pass the pointer from unmanaged to managed code:

/*
delegate to pass directx device/context back to managed code
see https://forum.unity3d.com/threads/communicating-c-with-c.89930/#post-586885
*/
typedef int(__stdcall *ANSWERCB)(ID3D11Device* ptr);
static ANSWERCB cb;

extern "C" __declspec(dllexport) int GetDevice(ANSWERCB fp)
{
    cb = fp;
    if (cb)
    {
        return cb(s_CurrentAPI->GetDevice());
    }
    return 0;
}

thanks in advance

best Marcel

marwie commented 7 years ago

copied wrong system dlls... sorry (32bit instead of 64 bit). Its building fine now!