Search Unity

Bug (Case 1428139) Static Constructor not excecuting code correctly

Discussion in 'Burst' started by Luxxuor, Apr 30, 2022.

  1. Luxxuor

    Luxxuor

    Joined:
    Jul 18, 2019
    Posts:
    89
    While updating some older code to a newer version of Burst (1.7.1, current latest), I encountered a problem when using a static constructor in a generic class for initializing a static member based on some type information.

    The constructor is used to get an an index into an array - based on a generic type T - from Mono and in bursted jobs; in Mono it returns the correct index but under burst the constructor seems to run but always returns the default value of -1 (weirdly enough it does run the Log statements and prints the correct TypeIndices but the if statements do not seem to be excuted).
    The breaking change seemed to happen somewhere between 1.4.3 (where it still works and returns the correct index) and 1.4.11 where it does not work anymore.

    I managed to create a minimal repro, the bug happens in the TraitArrayIndex<T> constructor (requires the latest entities due to the TypeManger):

    Code (CSharp):
    1. using Unity.Burst;
    2. using Unity.Entities;
    3. using Unity.Jobs;
    4. using UnityEngine;
    5.  
    6. public static class TraitArrayIndex<T> where T : unmanaged, ITrait
    7. {
    8.     public static readonly int Index = -1;
    9.  
    10.     static TraitArrayIndex()
    11.     {
    12.         var typeIndex = TypeManager.GetTypeIndex<T>();
    13.         var robotTypeIndex = TypeManager.GetTypeIndex<Robot>();
    14.         var locationTypeIndex = TypeManager.GetTypeIndex<Location>();
    15.         var dirtTypeIndex = TypeManager.GetTypeIndex<Dirt>();
    16.         var moveableTypeIndex = TypeManager.GetTypeIndex<Moveable>();
    17.         var planningAgentTypeIndex = TypeManager.GetTypeIndex<PlanningAgent>();
    18.  
    19.         if (typeIndex == robotTypeIndex)
    20.             Index = 0;
    21.         else if (typeIndex == locationTypeIndex)
    22.             Index = 1;
    23.         else if (typeIndex == dirtTypeIndex)
    24.             Index = 2;
    25.         else if (typeIndex == moveableTypeIndex)
    26.             Index = 3;
    27.         else if (typeIndex == planningAgentTypeIndex)
    28.             Index = 4;
    29.  
    30.         Debug.Log($"Trait Index Constructor: {Index}");
    31.         Debug.Log($"TypeManager TypeIndex: {typeIndex}");
    32.  
    33.         Debug.Log($"Robot TypeIndex: {robotTypeIndex}");
    34.         Debug.Log($"Location TypeIndex: {locationTypeIndex}");
    35.         Debug.Log($"Dirt TypeIndex: {dirtTypeIndex}");
    36.         Debug.Log($"Moveable TypeIndex: {moveableTypeIndex}");
    37.         Debug.Log($"PlanningAgent TypeIndex: {planningAgentTypeIndex}");
    38.     }
    39. }
    40.  
    41. public struct PlanningAgent : ITrait
    42. {
    43. }
    44.  
    45. public struct Moveable : ITrait
    46. {
    47. }
    48.  
    49. public struct Dirt : ITrait
    50. {
    51. }
    52.  
    53. public struct Location : ITrait
    54. {
    55. }
    56.  
    57. public struct Robot : ITrait
    58. {
    59. }
    60.  
    61. public interface ITrait : IComponentData
    62. {
    63. }
    64.  
    65. public class Test : MonoBehaviour
    66. {
    67.     private void Start()
    68.     {
    69.         var index = TraitArrayIndex<Robot>.Index;
    70.         Debug.Log($"Index from Mono: {index}");
    71.  
    72.         var testJob = new TestJob();
    73.         testJob.Run();
    74.     }
    75. }
    76.  
    77. [BurstCompile]
    78. public struct TestJob : IJob
    79. {
    80.     public void Execute()
    81.     {
    82.         var index = TraitArrayIndex<Robot>.Index;
    83.         Debug.Log($"index from Burst: {index}");
    84.         // index return is -1, should be == 0 (works in Burst 1.4.3)
    85.     }
    86. }
    Is this excpected and if yes is there a way to convert this idiom to something else (like SharedStatic?) or is this a bug?
     
  2. Luxxuor

    Luxxuor

    Joined:
    Jul 18, 2019
    Posts:
    89
    Still happens with Burst 1.8.0-pre.1, reported as Case 1428139.

    In the meantime you can workaround it using a SharedStatic instead.