Singularity/Library/PackageCache/com.unity.burst@1.8.4/Documentation~/csharp-calling-burst-code.md
2024-05-06 11:45:45 -07:00

1.9 KiB

Calling Burst-compiled code

You can call Burst-compiled methods direct from managed code. Calling generic methods or methods whose declaring type is generic isn't supported, otherwise the rules as for function pointers apply. However, you don't need to worry about the extra boiler plate needed for function pointers.

The following example shows a Burst-compiled utility class. Because it uses structs, it passes by reference per the function pointer rules.

[BurstCompile]
public static class MyBurstUtilityClass
{
    [BurstCompile]
    public static void BurstCompiled_MultiplyAdd(in float4 mula, in float4 mulb, in float4 add, out float4 result)
    {
        result = mula * mulb + add;
    }
}

Use this method from managed code like so:

public class MyMonoBehaviour : MonoBehaviour
{
    void Start()
    {
        var mula = new float4(1, 2, 3, 4);
        var mulb = new float4(-1,1,-1,1);
        var add = new float4(99,0,0,0);
        MyBurstUtilityClass.BurstCompiled_MultiplyAdd(mula, mulb, add, out var result);
        Debug.Log(result);
    }
}

If you attach this script to an object and run it, float4(98f, 2f, -3f, 4f) is printed to the log.

Code transformation

Burst uses IL Post Processing to automatically transform the code into a function pointer and call. For more information, see the documentation on Function pointers.

To disable the direct call transformation, addDisableDirectCall = true to the BurstCompile options. This prevents the Post Processor from running on the code:

[BurstCompile]
public static class MyBurstUtilityClass
{
    [BurstCompile(DisableDirectCall = true)]
    public static void BurstCompiled_MultiplyAdd(in float4 mula, in float4 mulb, in float4 add, out float4 result)
    {
        result = mula * mulb + add;
    }
}