Finally managed to create a generic solution for direct function calling. This will even work under Burst, because it allows to use calli unmanaged cdecl calls, and .NET runtime can provide us this with
Tho this call creates some wrapper and reverse P/Invoke call, on IL2CPP it's absolutely useless, so we can call this function directly.
For some reason IL2CPP doesn't compile the code where TReturn is returned by ref with Fully Shared Generics enabled, this is why I'm using RefReturn struct to overcome this behavior.
P.S. Different platforms using different function pointer alignment, WebGL doesn't restricts function to be aligned by 1 byte, which means I can't use first bit to store
Marshal.GetFunctionPointerForDelegate.Tho this call creates some wrapper and reverse P/Invoke call, on IL2CPP it's absolutely useless, so we can call this function directly.
FuncIL2CPP/FuncIL2CPPStatic provides us this ability and we can safely use it within burst and call managed land functions without any conversion.GetUnityCaller() returns a special call which can directly call IL2CPP methods without jumping to RuntimeMethodHandle->methodPtr again in again. This caller works in cross-runtime mode, so you can use it without unity without any limitations, tho it will give very little performance penalty.For some reason IL2CPP doesn't compile the code where TReturn is returned by ref with Fully Shared Generics enabled, this is why I'm using RefReturn struct to overcome this behavior.
P.S. Different platforms using different function pointer alignment, WebGL doesn't restricts function to be aligned by 1 byte, which means I can't use first bit to store
isIL2CPP flag, while Android ARMv7 (32bit) allows functions to store last bit, so I can't use it either. So for Unity IL2CPP direct calls I've made 64-bit field for funcPtr (even on 32-bit platforms) and use last (0x8000000000000000) bit as indicator for IL2CPP direct call. To make direct il2cpp calls faster it's possible to reinterpret to AsIL2CPP() if you are sure that you are running on IL2CPP.#if ENABLE_IL2CPP
FuncIL2CPP func = unityCall.AsIL2CPP();
#else
FuncManaged func = unityCall.AsManaged();
#endif
👏3🔥2
tgoop.com/madcsharp/10
Create:
Last Update:
Last Update:
Finally managed to create a generic solution for direct function calling. This will even work under Burst, because it allows to use calli unmanaged cdecl calls, and .NET runtime can provide us this with
Tho this call creates some wrapper and reverse P/Invoke call, on IL2CPP it's absolutely useless, so we can call this function directly.
For some reason IL2CPP doesn't compile the code where TReturn is returned by ref with Fully Shared Generics enabled, this is why I'm using RefReturn struct to overcome this behavior.
P.S. Different platforms using different function pointer alignment, WebGL doesn't restricts function to be aligned by 1 byte, which means I can't use first bit to store
Marshal.GetFunctionPointerForDelegate.Tho this call creates some wrapper and reverse P/Invoke call, on IL2CPP it's absolutely useless, so we can call this function directly.
FuncIL2CPP/FuncIL2CPPStatic provides us this ability and we can safely use it within burst and call managed land functions without any conversion.GetUnityCaller() returns a special call which can directly call IL2CPP methods without jumping to RuntimeMethodHandle->methodPtr again in again. This caller works in cross-runtime mode, so you can use it without unity without any limitations, tho it will give very little performance penalty.For some reason IL2CPP doesn't compile the code where TReturn is returned by ref with Fully Shared Generics enabled, this is why I'm using RefReturn struct to overcome this behavior.
P.S. Different platforms using different function pointer alignment, WebGL doesn't restricts function to be aligned by 1 byte, which means I can't use first bit to store
isIL2CPP flag, while Android ARMv7 (32bit) allows functions to store last bit, so I can't use it either. So for Unity IL2CPP direct calls I've made 64-bit field for funcPtr (even on 32-bit platforms) and use last (0x8000000000000000) bit as indicator for IL2CPP direct call. To make direct il2cpp calls faster it's possible to reinterpret to AsIL2CPP() if you are sure that you are running on IL2CPP.#if ENABLE_IL2CPP
FuncIL2CPP func = unityCall.AsIL2CPP();
#else
FuncManaged func = unityCall.AsManaged();
#endif
BY MadSharp: Unsafe


Share with your friend now:
tgoop.com/madcsharp/10
