A game about forced loneliness, made by TACStudios
1#if UNITY_EDITOR
2using System.Diagnostics;
3using UnityEditor;
4
5namespace Unity.Burst.Editor
6{
7 /// <summary>
8 /// Responsible to synchronize <see cref="BurstCompiler.Options"/> with the menu
9 /// </summary>
10 internal static class BurstEditorOptions
11 {
12 // Properties stored in SessionState (survive between domain reloads, but stays alive only during the life of the editor)
13 private const string EnableBurstSafetyChecksText = "BurstSafetyChecks";
14
15 // Properties stored in EditorPrefs (survive between editor restart)
16 private const string EnableBurstCompilationText = "BurstCompilation";
17 private const string EnableBurstTimingsText = "BurstShowTimings";
18 private const string EnableBurstCompileSynchronouslyText = "BurstCompileSynchronously";
19 private const string EnableBurstDebugText = "BurstDebug";
20 private const string ForceEnableBurstSafetyChecksText = "BurstForceSafetyChecks";
21
22 /// <summary>
23 /// <c>true</c> if the menu options are synchronized with <see cref="BurstCompiler.Options"/>
24 /// </summary>
25 private static bool _isSynchronized;
26
27 public static void EnsureSynchronized()
28 {
29 GetGlobalOptions();
30 }
31
32 public static bool EnableBurstCompilation
33 {
34 get => GetGlobalOptions().EnableBurstCompilation;
35 set
36 {
37 var enabled = GetGlobalOptions().EnableBurstCompilation;
38 GetGlobalOptions().EnableBurstCompilation = value;
39 if (!enabled && value)
40 {
41 // Must be called AFTER we actually enable compilation
42 BurstCompiler.TriggerUnsafeStaticMethodRecompilation();
43 }
44 }
45 }
46
47 public static bool EnableBurstSafetyChecks
48 {
49 get => GetGlobalOptions().EnableBurstSafetyChecks;
50 set => GetGlobalOptions().EnableBurstSafetyChecks = value;
51 }
52
53 public static bool EnableBurstCompileSynchronously
54 {
55 get => GetGlobalOptions().EnableBurstCompileSynchronously;
56 set => GetGlobalOptions().EnableBurstCompileSynchronously = value;
57 }
58
59 public static bool EnableBurstTimings
60 {
61 get => GetGlobalOptions().EnableBurstTimings;
62 set => GetGlobalOptions().EnableBurstTimings = value;
63 }
64
65 public static bool EnableBurstDebug
66 {
67 get => GetGlobalOptions().EnableBurstDebug;
68 set => GetGlobalOptions().EnableBurstDebug = value;
69 }
70
71 public static bool ForceEnableBurstSafetyChecks
72 {
73 get => GetGlobalOptions().ForceEnableBurstSafetyChecks;
74 set => GetGlobalOptions().ForceEnableBurstSafetyChecks = value;
75 }
76
77 private static BurstCompilerOptions GetGlobalOptions()
78 {
79 var global = BurstCompiler.Options;
80 // If options are not synchronize with our global instance, setup the sync
81 if (!_isSynchronized)
82 {
83 global.IsInitializing = true;
84
85 try
86 {
87 // Setup the synchronization
88 global.EnableBurstCompilation = EditorPrefs.GetBool(EnableBurstCompilationText, true);
89 global.EnableBurstCompileSynchronously = EditorPrefs.GetBool(EnableBurstCompileSynchronouslyText, false);
90 global.EnableBurstTimings = EditorPrefs.GetBool(EnableBurstTimingsText, false);
91 global.EnableBurstDebug = EditorPrefs.GetBool(EnableBurstDebugText, false);
92 global.ForceEnableBurstSafetyChecks = EditorPrefs.GetBool(ForceEnableBurstSafetyChecksText, false);
93
94 // Session only properties
95 global.EnableBurstSafetyChecks = SessionState.GetBool(EnableBurstSafetyChecksText, true);
96 }
97 finally
98 {
99 global.IsInitializing = false;
100 }
101
102 global.OptionsChanged += GlobalOnOptionsChanged;
103 _isSynchronized = true;
104 }
105
106 return global;
107 }
108
109 private static void GlobalOnOptionsChanged()
110 {
111 var global = BurstCompiler.Options;
112 // We are not optimizing anything here, so whenever one option is set, we reset all of them
113 EditorPrefs.SetBool(EnableBurstCompilationText, global.EnableBurstCompilation);
114 EditorPrefs.SetBool(EnableBurstCompileSynchronouslyText, global.EnableBurstCompileSynchronously);
115 EditorPrefs.SetBool(EnableBurstTimingsText, global.EnableBurstTimings);
116 EditorPrefs.SetBool(EnableBurstDebugText, global.EnableBurstDebug);
117 EditorPrefs.SetBool(ForceEnableBurstSafetyChecksText, global.ForceEnableBurstSafetyChecks);
118
119 // Session only properties
120 SessionState.SetBool(EnableBurstSafetyChecksText, global.EnableBurstSafetyChecks);
121 }
122 }
123}
124#endif