private unsafe static void WriteFunctionPointer <TDelegate>(IntPtr ptr, TypeDecomposer.FieldData fieldData, MethodInfo method) where TDelegate : Delegate { var func = (TDelegate)method.CreateDelegate(typeof(TDelegate)); var compiled = BurstCompiler.CompileFunctionPointer <TDelegate>(func); UnsafeUtility.CopyStructureToPtr(ref compiled, (ptr + fieldData.offset).ToPointer()); }
internal static void Initialize() { #if !UNITY_IOS if (Managed._initialized) { return; } Managed._initialized = true; Managed._bfp_AddComponentEntitiesBatch = BurstCompiler.CompileFunctionPointer <Managed._dlg_AddComponentEntitiesBatch>(_mono_to_burst_AddComponentEntitiesBatch).Invoke; Managed._bfp_AddComponentsEntitiesBatch = BurstCompiler.CompileFunctionPointer <Managed._dlg_AddComponentsEntitiesBatch>(_mono_to_burst_AddComponentsEntitiesBatch).Invoke; Managed._bfp_AddComponentEntity = BurstCompiler.CompileFunctionPointer <Managed._dlg_AddComponentEntity>(_mono_to_burst_AddComponentEntity).Invoke; Managed._bfp_AddComponentsEntity = BurstCompiler.CompileFunctionPointer <Managed._dlg_AddComponentsEntity>(_mono_to_burst_AddComponentsEntity).Invoke; Managed._bfp_AddComponentChunks = BurstCompiler.CompileFunctionPointer <Managed._dlg_AddComponentChunks>(_mono_to_burst_AddComponentChunks).Invoke; Managed._bfp_AddComponentsChunks = BurstCompiler.CompileFunctionPointer <Managed._dlg_AddComponentsChunks>(_mono_to_burst_AddComponentsChunks).Invoke; Managed._bfp_RemoveComponentEntity = BurstCompiler.CompileFunctionPointer <Managed._dlg_RemoveComponentEntity>(_mono_to_burst_RemoveComponentEntity).Invoke; Managed._bfp_RemoveComponentsEntity = BurstCompiler.CompileFunctionPointer <Managed._dlg_RemoveComponentsEntity>(_mono_to_burst_RemoveComponentsEntity).Invoke; Managed._bfp_RemoveComponentEntitiesBatch = BurstCompiler.CompileFunctionPointer <Managed._dlg_RemoveComponentEntitiesBatch>(_mono_to_burst_RemoveComponentEntitiesBatch).Invoke; Managed._bfp_RemoveComponentsEntitiesBatch = BurstCompiler.CompileFunctionPointer <Managed._dlg_RemoveComponentsEntitiesBatch>(_mono_to_burst_RemoveComponentsEntitiesBatch).Invoke; Managed._bfp_RemoveComponentChunks = BurstCompiler.CompileFunctionPointer <Managed._dlg_RemoveComponentChunks>(_mono_to_burst_RemoveComponentChunks).Invoke; Managed._bfp_RemoveComponentsChunks = BurstCompiler.CompileFunctionPointer <Managed._dlg_RemoveComponentsChunks>(_mono_to_burst_RemoveComponentsChunks).Invoke; Managed._bfp_AddSharedComponentChunks = BurstCompiler.CompileFunctionPointer <Managed._dlg_AddSharedComponentChunks>(_mono_to_burst_AddSharedComponentChunks).Invoke; Managed._bfp_MoveEntityArchetype = BurstCompiler.CompileFunctionPointer <Managed._dlg_MoveEntityArchetype>(_mono_to_burst_MoveEntityArchetype).Invoke; Managed._bfp_SetChunkComponent = BurstCompiler.CompileFunctionPointer <Managed._dlg_SetChunkComponent>(_mono_to_burst_SetChunkComponent).Invoke; Managed._bfp_CreateEntity = BurstCompiler.CompileFunctionPointer <Managed._dlg_CreateEntity>(_mono_to_burst_CreateEntity).Invoke; Managed._bfp_DestroyEntity = BurstCompiler.CompileFunctionPointer <Managed._dlg_DestroyEntity>(_mono_to_burst_DestroyEntity).Invoke; Managed._bfp_InstantiateEntities = BurstCompiler.CompileFunctionPointer <Managed._dlg_InstantiateEntities>(_mono_to_burst_InstantiateEntities).Invoke; #endif }
static string[] GetBurstCommandLineArgs(BurstCompiler compiler, NPath outputPrefixForObjectFile, NPath outputDirForPatchedAssemblies, string pinvokeName, DotNetAssembly[] inputAssemblies) { var commandLineArguments = new[] { $"--platform={compiler.TargetPlatform}", $"--target={compiler.TargetArchitecture}", $"--format={compiler.ObjectFormat}", compiler.SafetyChecks ? "--safety-checks" : "", $"--dump=\"None\"", compiler.DisableVectors ? "--disable-vectors" : "", compiler.Link ? "" : "--nolink", $"--float-precision={compiler.FloatPrecision}", $"--keep-intermediate-files", compiler.Verbose ? "--verbose" : "", $"--patch-assemblies-into={outputDirForPatchedAssemblies}", $"--output={outputPrefixForObjectFile}", compiler.OnlyStaticMethods ? "--only-static-methods": "", "--method-prefix=burstedmethod_", $"--pinvoke-name={pinvokeName}" }.Concat(inputAssemblies.Select(asm => $"--root-assembly={asm.Path}")); if (!compiler.UseOwnToolchain) { commandLineArguments = commandLineArguments.Concat(new[] { "--no-native-toolchain" }); } if (!HostPlatform.IsWindows) { commandLineArguments = new[] { BurstExecutable.ToString(SlashMode.Native) } }
private static void VersionUpdateCheck() { var seek = "com.unity.burst@"; var first = RuntimePath.LastIndexOf(seek); var last = RuntimePath.LastIndexOf(".Runtime"); string version; if (first == -1 || last == -1 || last <= first) { version = "Unknown"; } else { first += seek.Length; last -= 1; version = RuntimePath.Substring(first, last - first); } var result = BurstCompiler.VersionNotify(version); // result will be empty if we are shutting down, and thus we shouldn't popup a dialog if (!String.IsNullOrEmpty(result) && result != version) { if (IsDebugging) { UnityEngine.Debug.LogWarning($"[com.unity.burst] - '{result}' != '{version}'"); } OnVersionChangeDetected(); } }
public FunctionPointer <TDelegate> Get() { if (!m_IsCompiled) { m_IsCompiled = true; if (!BurstCompiler.Options.IsEnabled || !BurstCompiler.Options.EnableBurstCompilation) { return(new FunctionPointer <TDelegate>(Marshal.GetFunctionPointerForDelegate(Origin))); } try { m_BurstResult = BurstCompiler.CompileFunctionPointer(Origin); WasBurstCompiled = true; } catch { m_BurstResult = default; WasBurstCompiled = false; } } if (WasBurstCompiled) { return(m_BurstResult); } return(new FunctionPointer <TDelegate>(Marshal.GetFunctionPointerForDelegate(Origin))); }
public static FunctionPointer <TDelegate> CompileStaticMemberFunction <TDelegate>(Type type, string methodName) where TDelegate : class { if (type.GetCustomAttribute <BurstCompileAttribute>() == null) { throw new ArgumentException($"Compilation of function {methodName} from {type.Name} failed : class is missing [BurstCompile] attribute."); } MethodInfo method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static); if (method == null) { throw new ArgumentException($"Compilation of function {methodName} from {type.Name} failed : method not found."); } if (method.GetCustomAttribute <BurstCompileAttribute>() == null) { throw new ArgumentException($"Compilation of function {methodName} from {type.Name} failed : method is missing [BurstCompile] attribute."); } TDelegate functionDelegate = (TDelegate)(object)method.CreateDelegate(typeof(TDelegate)); try { FunctionPointer <TDelegate> functionPointer = BurstCompiler.CompileFunctionPointer <TDelegate>(functionDelegate); return(functionPointer); } catch (Exception e) { throw new Exception($"Compilation of function {methodName} from {type.Name} failed : {e}"); } }
private static void VersionUpdateCheck() { var seek = "com.unity.burst@"; var first = RuntimePath.LastIndexOf(seek); var last = RuntimePath.LastIndexOf(".Runtime"); string version; if (first == -1 || last == -1 || last <= first) { version = "Unknown"; } else { first += seek.Length; last -= 1; version = RuntimePath.Substring(first, last - first); } var result = BurstCompiler.VersionNotify(version); if (result != version) { if (IsDebugging) { UnityEngine.Debug.LogWarning($"[com.unity.burst] - '{result}' != '{version}'"); } OnVersionChangeDetected(); } }
private static void CompileFunctions() { _1ByteEffectorFunction = BurstCompiler.CompileFunctionPointer <EffectorDelegate>(Effector1Byte); _2ByteEffectorFunction = BurstCompiler.CompileFunctionPointer <EffectorDelegate>(Effector2Byte); _4ByteEffectorFunction = BurstCompiler.CompileFunctionPointer <EffectorDelegate>(Effector4Byte); _8ByteEffectorFunction = BurstCompiler.CompileFunctionPointer <EffectorDelegate>(Effector8Byte); }
private static void OnDomainUnload(object sender, EventArgs e) { if (DebuggingLevel > 2) { UnityEngine.Debug.Log($"Burst - OnDomainUnload"); } lock (EagerCompilationLockObject) { EagerCompilationTokenSource.Cancel(); } BurstCompiler.Cancel(); #if UNITY_2020_1_OR_NEWER // Because of a check in Unity (specifically SCRIPTINGAPI_THREAD_AND_SERIALIZATION_CHECK), // we are not allowed to call thread-unsafe methods (like Progress.Exists) after the // kApplicationTerminating bit has been set. And because the domain is unloaded // (thus triggering AppDomain.DomainUnload) *after* that bit is set, we can't call Progress.Exists // during shutdown. So we check _isQuitting here. When quitting, it's fine for the progress item // not to be removed since it's all being torn down anyway. if (!_isQuitting && Progress.Exists(BurstProgressId)) { Progress.Remove(BurstProgressId); BurstProgressId = -1; } #endif }
protected override TestResult HandleCompilerException(ITestExecutionContext context, MethodInfo methodInfo) { var arguments = GetArgumentsArray(_originalMethod); Type[] nativeArgTypes = new Type[arguments.Length]; for (var i = 0; i < arguments.Length; ++i) { nativeArgTypes[i] = arguments[i].GetType(); } bool isInRegistry; Func <object, object[], object> caller; var delegateType = CreateNativeDelegateType(methodInfo.ReturnType, nativeArgTypes, out isInRegistry, out caller); var functionDelegate = Delegate.CreateDelegate(delegateType, methodInfo); Delegate compiledFunction = BurstCompiler.CompileDelegate(functionDelegate); if (functionDelegate == compiledFunction) { context.CurrentResult.SetResult(ResultState.Success); } else { context.CurrentResult.SetResult(ResultState.Failure, $"The function have been compiled successfully, but an error was expected."); } return(context.CurrentResult); }
private static void OnEditorApplicationQuitting() { #if UNITY_2020_1_OR_NEWER _isQuitting = true; #endif BurstCompiler.Shutdown(); }
public static BurstAction Compile(Action action) { var functionPointer = BurstCompiler.CompileFunctionPointer(action); DelegateCache <Action> .SetDelegate(functionPointer.Value, action); return(new BurstAction(functionPointer)); }
public void StressTestFromBurst() { fixed(World.StateAllocator *p = &alloc) fixed(byte *s = systems.Bytes) { BurstCompiler.CompileFunctionPointer <RunBurstTest>(RunStressTest).Invoke((IntPtr)p, (IntPtr)s); } }
public static BurstFunc <TResult> Compile(Func <TResult> func) { var functionPointer = BurstCompiler.CompileFunctionPointer(func); DelegateCache <Func <TResult> > .SetDelegate(functionPointer.Value, func); return(new BurstFunc <TResult>(functionPointer)); }
protected override Delegate CompileDelegate(ITestExecutionContext context, MethodInfo methodInfo, Type delegateType) { var functionDelegate = Delegate.CreateDelegate(delegateType, methodInfo); var compiledFunction = BurstCompiler.CompileDelegate(functionDelegate); return(compiledFunction); }
public PortableFunctionPointer(T executeDelegate) { #if !UNITY_DOTSPLAYER Ptr = BurstCompiler.CompileFunctionPointer(executeDelegate); #else Ptr = executeDelegate; #endif }
void Start() { m_Texture = new Texture2D(k_Width, k_Height, TextureFormat.RFloat, true, true); TargetMaterial.mainTexture = m_Texture; m_RenderMandelbrot = BurstCompiler.CompileFunctionPointer <Render>(RenderMandelbrot).Invoke; m_RenderMandelbrotSimd = BurstCompiler.CompileFunctionPointer <Render>(RenderMandelbrotSimd).Invoke; }
private static void EditorApplicationOnPlayModeStateChanged(PlayModeStateChange state) { if (DebuggingLevel > 2) { UnityEngine.Debug.Log($"Burst - Change of Editor State: {state}"); } switch (state) { case PlayModeStateChange.ExitingEditMode: if (BurstCompiler.Options.RequiresSynchronousCompilation) { if (DebuggingLevel > 2) { UnityEngine.Debug.Log("Burst - Exiting EditMode - waiting for any pending synchronous jobs"); } EditorUtility.DisplayProgressBar("Burst", "Waiting for synchronous compilation to finish", -1); try { BurstCompiler.WaitUntilCompilationFinished(); } finally { EditorUtility.ClearProgressBar(); } if (DebuggingLevel > 2) { UnityEngine.Debug.Log("Burst - Exiting EditMode - finished waiting for any pending synchronous jobs"); } } else { BurstCompiler.ClearEagerCompilationQueues(); if (DebuggingLevel > 2) { UnityEngine.Debug.Log("Burst - Exiting EditMode - cleared eager-compilation queues"); } } break; case PlayModeStateChange.ExitingPlayMode: // If Synchronous Compilation is checked, then we will already have waited for eager-compilation to finish // before entering playmode. But if it was unchecked, we may have cancelled in-progress eager-compilation. // We start it again here. if (!BurstCompiler.Options.RequiresSynchronousCompilation) { if (DebuggingLevel > 2) { UnityEngine.Debug.Log("Burst - Exiting PlayMode - triggering eager-compilation"); } MaybeTriggerEagerCompilation(); } break; } }
/// <summary> /// don't ask me why it's so complicated, I am hacking here. Use at your risk. /// </summary> /// <param name="action"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static T ConvertBurstMethodToDelegate <T>(T action) where T : MulticastDelegate { var delegateType = DelegateHelper.NewDelegateType(action.Method.ReturnType, typeof(T).GetGenericArguments()); var functionDelegate = Delegate.CreateDelegate(delegateType, action.Method); var o = BurstCompiler.CompileDelegate(functionDelegate); var compile = DelegateCompiler.Compile <T>(o.Method); return(compile); }
public float CheckFunctionPointer() { var functionPointer1 = BurstCompiler.CompileFunctionPointer <Add2NumbersDelegate>(Add2Numbers); var result = functionPointer1.Invoke(1.0f, 2.0f); var functionPointer2 = BurstCompiler.CompileFunctionPointer <Add2NumbersDelegate>(Add2NumbersThrows); return(functionPointer2.Invoke(1.0f, 2.0f)); }
private static void CancelEagerCompilationPriorToAssemblyCompilation() { BurstCompiler.CancelEagerCompilation(); if (DebuggingLevel > 2) { UnityEngine.Debug.Log("Burst - Cancelled eager-compilation prior to assembly compilation"); } }
/// <summary> /// Requests previously-compiled functions to be cleared from the cache during the next domain reload. /// Note that this method does not trigger a domain reload itself, so it should be paired with /// <see cref="EditorUtility.RequestScriptReload()"/> to force a domain reload. /// </summary> /// <remarks> /// During the next domain reload, previously-compiled functions are unloaded from memory, /// and the corresponding libraries in the on-disk cache are deleted. /// /// This method cannot be called while the Editor is in play mode. /// </remarks> /// <example> /// The following example shows calling this method in a test, then triggering a domain reload, /// and then waiting for the domain reload to finish: /// <code> /// BurstEditorUtility.RequestClearJitCache(); /// EditorUtility.RequestScriptReload(); /// yield return new WaitForDomainReload(); /// </code> /// </example> public static void RequestClearJitCache() { if (EditorApplication.isPlayingOrWillChangePlaymode) { throw new InvalidOperationException("This method cannot be called while the Editor is in play mode"); } BurstCompiler.RequestClearJitCache(); }
private static void OnAssemblyCompilationFinished(string arg1, CompilerMessage[] arg2) { // On assembly compilation finished, we cancel all pending compilation if (DebuggingLevel > 2) { UnityEngine.Debug.Log("Burst - Assembly compilation finished - cancelling any pending jobs"); } BurstCompiler.Cancel(); }
static unsafe xxHash3() { if (_initialized) { return; } _initialized = true; _bfp_Hash64Long = BurstCompiler.CompileFunctionPointer <_dlg_Hash64Long>(_mono_to_burst_Hash64Long).Invoke; _bfp_Hash128Long = BurstCompiler.CompileFunctionPointer <_dlg_Hash128Long>(_mono_to_burst_Hash128Long).Invoke; }
protected unsafe override Delegate CompileDelegate(ITestExecutionContext context, MethodInfo methodInfo, Type delegateType, byte *returnBox, out Type returnBoxType) { returnBoxType = null; var functionDelegate = Delegate.CreateDelegate(delegateType, methodInfo); var compiledFunction = BurstCompiler.CompileDelegate(functionDelegate); return(compiledFunction); }
private static void CompileFunctions() { _1ByteDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(Decorator1Byte); _2ByteDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(Decorator2Byte); _4ByteDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(Decorator4Byte); _8ByteDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(Decorator8Byte); _1ByteInvertedDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(InvertedDecorator1Byte); _2ByteInvertedDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(InvertedDecorator2Byte); _4ByteInvertedDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(InvertedDecorator4Byte); _8ByteInvertedDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(InvertedDecorator8Byte); }
internal static void Initialize() { #if !UNITY_IOS if (Managed._initialized) { return; } Managed._initialized = true; Managed._bfp_PlaybackChainChunk = BurstCompiler.CompileFunctionPointer <Managed._dlg_PlaybackChainChunk>(_mono_to_burst_PlaybackChainChunk).Invoke; #endif }
public static void Initialize() { _getCharsDelegate = BurstCompiler. CompileFunctionPointer <GetCharsDelegate>(GetCharsBurst).Invoke; _terminateDelegate = BurstCompiler. CompileFunctionPointer <TerminateDelegate>(TerminateBurst).Invoke; _getBytesDelegate = BurstCompiler. CompileFunctionPointer <GetBytesDelegate>(GetBytesBurst).Invoke; }
void Start() { m_CountSmallNumbers = BurstCompiler.CompileFunctionPointer <F>(CountSmallNumbers).Invoke; m_CountSmallNumbersSimd = BurstCompiler.CompileFunctionPointer <F>(CountSmallNumbersSimd).Invoke; m_Data = new float[1024 * 1024 * 16]; for (int i = 0; i < m_Data.Length; i++) { m_Data[i] = Random.value; } }
private static void CompileFunctions() { _1ByteEqualsDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(EqualsDecorator1Byte); _2ByteEqualsDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(EqualsDecorator2Byte); _4ByteEqualsDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(EqualsDecorator4Byte); _8ByteEqualsDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(EqualsDecorator8Byte); _1ByteNotEqualsDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(NotEqualsDecorator1Byte); _2ByteNotEqualsDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(NotEqualsDecorator2Byte); _4ByteNotEqualsDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(NotEqualsDecorator4Byte); _8ByteNotEqualsDecoratorFunction = BurstCompiler.CompileFunctionPointer <DecoratorDelegate>(NotEqualsDecorator8Byte); }