Why when the method use a generic I can return structs?

With the code :

[BurstCompile]
public static class TestUtil
{
    [BurstCompile]
    public static int3 ItDoesntWork()
    {
        return int3.zero;
    }

    [BurstCompile]
    public static int3 ItWorksWithGeneric<T>()
    {
        return int3.zero;
    }
}

Only the first method generates an error while compile the job : “Burst error BC1064: Unsupported return type Unity.Mathematics.int3”.

When my method use a generic, I don’t have an error. Why ?
In my code, I have a lot of static methods that return structs. Should I avoid this ? What are the consequences ?

The second one doesn’t “work.” Each entry point is compiled to one method, so what type argument should it use? The answer is “you can’t choose,” so it shouldn’t be compiled as a Burst entry point. You can check it yourself in the Burst Inspector, the method shouldn’t be there. Calling that method from other Bursted code will be Burst-compiled since you have a known type argument, and managed code calling the method will only be calling the original, managed method.

Not being able to pass structs (besides single field handle structs) is a known constraint on Burst entry points.
https://docs.unity3d.com/Packages/com.unity.burst@1.8/manual/csharp-burst-intrinsics-dllimport.html
You can just add an out parameter for the return value instead.

Other pages:
https://docs.unity3d.com/Packages/com.unity.burst@1.8/manual/csharp-calling-burst-code.html
https://docs.unity3d.com/Packages/com.unity.burst@1.8/manual/csharp-function-pointers.html

1 Like

Thank you for your detailed response. :slight_smile:

I didn’t understant that there was a difference if the method was called from other Bursted code or from managed code.
I’m still surprised that the method with the generic doesn’t cause at least a warning.