Search Unity

Should MonoPInvokeCallback be required to all [BurstCompile] functions in an IL2CPP build?

Discussion in 'Burst' started by Rob-Fireproof, Jul 16, 2021.

  1. Rob-Fireproof

    Rob-Fireproof

    Joined:
    Dec 18, 2012
    Posts:
    56
    Hi all,

    Quick question about using the new DirectCall burst stuff. Here's a code snippet:

    Code (CSharp):
    1. public class BurstTest : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.     void Awake()
    5.     {
    6.         bool same = BurstUtils.SameSide(float3.zero, new float3(1.0f, 0.0f, 0.0f), new float3(1.0f, 1.0f, 0.0f), new float3(1.0f, -1.0f, 0.0f));
    7.         Debug.Log(same);
    8.     }
    9. }
    10.  
    11. [BurstCompile]
    12. public static class BurstUtils
    13. {
    14.     public delegate bool bool_float3x4(float3 a, float3 b, float3 c, float3 d);
    15.     public delegate bool float_float3x2(float3 a, float3 b);
    16.    
    17.     [BurstCompile]
    18.     [MonoPInvokeCallback(typeof(bool_float3x4))] // <- SHOULD I NEED THIS?!
    19.     public static bool SameSide(in float3 p1, in float3 p2, in float3 a, in float3 b)
    20.     {
    21.         float3 cp1 = math.cross(b - a, p1 - a);
    22.         float3 cp2 = math.cross(b - a, p2 - a);
    23.  
    24.         return math.dot(cp1, cp2) >= 0f;
    25.     }
    26. }
    So, we're calling into some BurstCompiled utility code from a mono-behaviour. Nothing is jobbified. In a Mono build, this all works fine *without* the MonoPInvokeCallback boilerplate. In an IL2CPP build though, without that attribute I get this error:

    The method `Boolean SameSide(Unity.Mathematics.float3 ByRef, Unity.Mathematics.float3 ByRef, Unity.Mathematics.float3 ByRef, Unity.Mathematics.float3 ByRef)` must have `MonoPInvokeCallback` attribute to be compatible with IL2CPP!
    UnityEngine.Logger:Log(LogType, Object)
    UnityEngine.Debug:Log(Object)
    Unity.Burst.BurstCompiler:Compile(Object, MethodInfo, Boolean, Object)
    Unity.Burst.BurstCompiler:CompileUnsafeStaticMethod(RuntimeMethodHandle)
    SameSide_00000005$BurstDirectCall:GetFunctionPointer()
    SameSide_00000005$BurstDirectCall:Constructor()
    BurstTest:Awake()

    Just wanted to check that's expected behaviour?

    Also, I'm wondering why it's trying to compile the code at run-time? My understanding was that all compilation should happen at build time.

    This is using Unity 2021.1.0f1, and Burst 1.5.0-pre.5.

    Apologies if I'm missing something obvious.

    Thanks,
    Rob.
     
  2. IgreygooI

    IgreygooI

    Joined:
    Mar 13, 2021
    Posts:
    48
  3. Lee_Hammerton

    Lee_Hammerton

    Unity Technologies

    Joined:
    Jul 26, 2018
    Posts:
    118
    Try updating to latest Burst 1.5 (1.5.4 currently), I believe our ILPP should be automatically inserting the needed attributes automatically for you now.