Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

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

Discussion in 'Burst' started by lberte, Jan 17, 2024.

  1. lberte

    lberte

    Joined:
    Mar 19, 2016
    Posts:
    3
    With the code :
    Code (CSharp):
    1.  
    2. [BurstCompile]
    3. public static class TestUtil
    4. {
    5.     [BurstCompile]
    6.     public static int3 ItDoesntWork()
    7.     {
    8.         return int3.zero;
    9.     }
    10.  
    11.     [BurstCompile]
    12.     public static int3 ItWorksWithGeneric<T>()
    13.     {
    14.         return int3.zero;
    15.     }
    16. }

    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 ?
     
    Last edited: Jan 17, 2024
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    850
    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
     
    lberte likes this.
  3. lberte

    lberte

    Joined:
    Mar 19, 2016
    Posts:
    3
    Thank you for your detailed response. :)

    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.