Search Unity

Bug static readonly - is reset without Domain Reload

Discussion in 'Burst' started by Dinamytes, Mar 16, 2021.

  1. Dinamytes

    Dinamytes

    Joined:
    Nov 3, 2016
    Posts:
    51
    A static readonly variable like Unity.Physics.Material returns default inside a Burst compiled job the next plays after the first.

    Reproduction:
    Code (CSharp):
    1. class T
    2. {
    3.         //Unity.Physics.Material copy
    4.         public struct TestStruct
    5.         {
    6.             public CombinePolicy FrictionCombinePolicy;
    7.             public CombinePolicy RestitutionCombinePolicy;
    8.             public byte CustomTags;
    9.             public float Friction;
    10.             public float Restitution;
    11.             public CollisionResponsePolicy CollisionResponse { get; set; }
    12.             public bool EnableMassFactors { get; set; }
    13.             public bool EnableSurfaceVelocity { get; set; }
    14.         }
    15.  
    16.         public static readonly TestStruct test = new TestStruct() { Friction = 1 };
    17.  
    18.         [BurstCompile]
    19.         public struct TestJob : IJob
    20.         {
    21.             public void Execute()
    22.             {
    23.                 Debug.Log($"{test.Friction}");
    24.             }
    25.         }
    26.  
    27.         [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
    28.         static void SubsystemRegistration()
    29.         {
    30.             // Just disabling Domain Reload (this part is not tested)
    31.             if (!EditorSettings.enterPlayModeOptionsEnabled)
    32.             {
    33.                 EditorSettings.enterPlayModeOptionsEnabled = true;
    34.                 EditorSettings.enterPlayModeOptions = EnterPlayModeOptions.DisableDomainReload | EnterPlayModeOptions.DisableSceneReload;
    35.             }
    36.  
    37.             var q = new TestJob().Schedule();
    38.             q.Complete();
    39.             EditorApplication.isPlaying = false;
    40.             return;
    41.         }
    42. }
    Problem found here.
    @ Burst 1.4.4
     
    Last edited: Mar 16, 2021
    apkdev likes this.
  2. sheredom

    sheredom

    Unity Technologies

    Joined:
    Jul 15, 2019
    Posts:
    300
    Just to rule it out - could you set
    [BurstCompile(CompileSynchronously =t rue)]
    on that job and tell us the result please?

    I'm suspicious that you are running the managed version of the job on the first iteration, and then the Burst job on the second iteration (thus the differing result!).
     
  3. Dinamytes

    Dinamytes

    Joined:
    Nov 3, 2016
    Posts:
    51
    Yes, that shows that the problem is not related to DomainReload.
    Code (CSharp):
    1.         public struct TestStruct
    2.         {
    3.             public float CustomTags;
    4.             public float Friction;
    5.         }
    6.  
    7.         public static readonly TestStruct test = new TestStruct() { Friction = 1 };
    8.  
    9.         [Unity.Burst.BurstCompile(CompileSynchronously = true)]
    10.         public struct TestJob : IJob
    11.         {
    12.             public void Execute()
    13.             {
    14.                 UnityEngine.Debug.Log($"{test.Friction}, {test.CustomTags}");
    15.             }
    16.         }
    Will always print "0, 1", but it should be "1, 0"
     
  4. sheredom

    sheredom

    Unity Technologies

    Joined:
    Jul 15, 2019
    Posts:
    300
    Ok cool, I can reproduce this. We've closed off 1.4 now, but I'll get a fix for this landed in 1.5!
     
  5. sheredom

    sheredom

    Unity Technologies

    Joined:
    Jul 15, 2019
    Posts:
    300
    Oh and as a workaround for now, if you explicitly set CustomTags = 0, it'll work as expected until we can get a bugfix landed!
     
    Dinamytes likes this.