SamplingTime GetOutputPlayTime(DebugMemory debugMemory, SamplingTime samplingTime, SamplingTime closestMatch, DebugIdentifier outputTimeIdentifier)
        {
            if (!closestMatch.IsValid)
            {
                return(samplingTime);
            }

            int playAtTimeTypeCode = BurstRuntime.GetHashCode32 <PlayAtTimeDebug>();

            for (DebugReference reference = debugMemory.FirstOrDefault; reference.IsValid; reference = debugMemory.Next(reference))
            {
                if (reference.identifier.typeHashCode != playAtTimeTypeCode)
                {
                    continue;
                }

                PlayAtTimeDebug playAtTime = debugMemory.ReadObject <PlayAtTimeDebug>(reference);
                if (playAtTime.playTime.Equals(outputTimeIdentifier))
                {
                    return(closestMatch);
                }
            }

            return(samplingTime);
        }
        Nullable <TrajectoryHeuristicDebug> FindTrajectoryHeuristic(DebugMemory debugMemory)
        {
            if (!trajectory.IsValid)
            {
                return(null);
            }

            int heuristicTypeCode = BurstRuntime.GetHashCode32 <TrajectoryHeuristicDebug>();

            for (DebugReference reference = debugMemory.FirstOrDefault; reference.IsValid; reference = debugMemory.Next(reference))
            {
                if (reference.identifier.typeHashCode != heuristicTypeCode)
                {
                    continue;
                }

                TrajectoryHeuristicDebug heuristicDebug = debugMemory.ReadObject <TrajectoryHeuristicDebug>(reference);
                if (heuristicDebug.closestMatch.Equals(closestMatch))
                {
                    return(heuristicDebug);
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        static void AddNodeType(Type nodeType, Type runtimeType)
        {
            if (runtimeType != null)
            {
                if (runtimeType.GetCustomAttributes(typeof(DataAttribute), false).Length == 0)
                {
                    throw new InvalidOperationException(
                              $"Type {runtimeType.FullName} needs to have [Data] attribute to be instantiated as a node in the execution graph.");
                }

                bool isGraphNode      = nodeType == typeof(GraphNode);
                bool isGraphNodeChild = nodeType.IsSubclassOf(typeof(GraphNode));

                if (!isGraphNode && !isGraphNodeChild)
                {
                    throw new InvalidOperationException(
                              $"Type {nodeType.FullName} needs to inherit from {typeof(GraphNode).FullName}.");
                }

                int typeHashCode = BurstRuntime.GetHashCode32(runtimeType);

                if (!nodeTypes.ContainsKey(typeHashCode) || isGraphNodeChild)
                {
                    nodeTypes[typeHashCode] = nodeType;
                }
            }
        }
Exemplo n.º 4
0
        public void Execute()
        {
            Hashes[0] = BurstRuntime.GetHashCode32 <int>();
            Hashes[1] = TypeHashWrapper.GetIntHash();

            Hashes[2] = BurstRuntime.GetHashCode32 <TypeHashWrapper.SomeStruct <int> >();
            Hashes[3] = TypeHashWrapper.GetGenericHash <int>();
        }
            public RegisteredType(Type type, int nameIndex, int typeIndex)
            {
                this.type      = type;
                this.typeIndex = typeIndex;
                this.nameIndex = nameIndex;

                hashCode = BurstRuntime.GetHashCode32(type);

                numBytes = UnsafeUtility.SizeOf(type);

                fields = new List <RegisteredType>();
            }
Exemplo n.º 6
0
        DebugIdentifier CreateIdentifier <T>() where T : struct
        {
            int    typeHashCode = BurstRuntime.GetHashCode32 <T>();
            ushort currentIndex = 0;

            for (DebugReference debugRef = FirstOrDefault; debugRef.IsValid; debugRef = Next(debugRef))
            {
                if (debugRef.identifier.typeHashCode == typeHashCode)
                {
                    currentIndex = debugRef.identifier.index;
                    ++currentIndex;
                }
            }

            return(new DebugIdentifier()
            {
                index = currentIndex,
                version = version,
                typeHashCode = typeHashCode
            });
        }
Exemplo n.º 7
0
        TraitType(Type type)
        {
            Assert.IsTrue(HasTraitAttribute(type));

            if (!UnsafeUtility.IsUnmanaged(type) || !UnsafeUtility.IsBlittable(type))
            {
                throw new InvalidOperationException(
                          $"Trait type {type.FullName} must be unmanaged and blittable.");
            }

            if (ImplementsTraitInterface(type))
            {
                //
                // TODO: Find a way to call Trait.Execute directly
                //

                var executeSelf =
                    type.GetMethod(nameof(ExecuteSelf),
                                   BindingFlags.Static | BindingFlags.Public);

                if (executeSelf == null)
                {
                    throw new InvalidOperationException(
                              $"Trait type {type.FullName} must implement 'ExecuteSelf'.");
                }

                executeFunction = CompileFunction(type, executeSelf);

                Assert.IsTrue(executeFunction != IntPtr.Zero);
            }
            else
            {
                executeFunction = IntPtr.Zero;
            }

            hashCode = BurstRuntime.GetHashCode32(type);

            this.type = type;
        }
Exemplo n.º 8
0
        static BurstLoader()
        {
            if (BurstCompilerOptions.ForceDisableBurstCompilation)
            {
                UnityEngine.Debug.LogWarning("[com.unity.burst] Burst is disabled entirely from the command line");
                return;
            }

            // This can be setup to get more diagnostics
            var debuggingStr = Environment.GetEnvironmentVariable("UNITY_BURST_DEBUG");

            IsDebugging = debuggingStr != null;
            if (IsDebugging)
            {
                UnityEngine.Debug.LogWarning("[com.unity.burst] Extra debugging is turned on.");
                int debuggingLevel;
                int.TryParse(debuggingStr, out debuggingLevel);
                if (debuggingLevel <= 0)
                {
                    debuggingLevel = 1;
                }
                DebuggingLevel = debuggingLevel;
            }

            // Try to load the runtime through an environment variable
            RuntimePath = Environment.GetEnvironmentVariable("UNITY_BURST_RUNTIME_PATH");

            // Otherwise try to load it from the package itself
            if (!Directory.Exists(RuntimePath))
            {
                RuntimePath = Path.GetFullPath("Packages/com.unity.burst/.Runtime");
            }

            if (IsDebugging)
            {
                UnityEngine.Debug.LogWarning($"[com.unity.burst] Runtime directory set to {RuntimePath}");
            }

            BurstEditorOptions.EnsureSynchronized();

            BurstCompilerService.Initialize(RuntimePath, TryGetOptionsFromMemberDelegate);

            EditorApplication.quitting += BurstCompiler.Shutdown;

            CompilationPipeline.assemblyCompilationStarted  += OnAssemblyCompilationStarted;
            CompilationPipeline.assemblyCompilationFinished += OnAssemblyCompilationFinished;
            EditorApplication.playModeStateChanged          += EditorApplicationOnPlayModeStateChanged;

            VersionUpdateCheck();

            // Workaround to update the list of assembly folders as soon as possible
            // in order for the JitCompilerService to not fail with AssemblyResolveExceptions.
            try
            {
                var assemblyList    = BurstReflection.GetAssemblyList(AssembliesType.Editor);
                var assemblyFolders = new HashSet <string>();
                foreach (var assembly in assemblyList)
                {
                    try
                    {
                        var fullPath       = Path.GetFullPath(assembly.Location);
                        var assemblyFolder = Path.GetDirectoryName(fullPath);
                        if (!string.IsNullOrEmpty(assemblyFolder))
                        {
                            assemblyFolders.Add(assemblyFolder);
                        }
                    }
                    catch
                    {
                        // ignore
                    }
                }

                // Notify the compiler
                var assemblyFolderList = assemblyFolders.ToList();
                if (IsDebugging)
                {
                    UnityEngine.Debug.Log($"Burst - Change of list of assembly folders:\n{string.Join("\n", assemblyFolderList)}");
                }
                BurstCompiler.UpdateAssemblerFolders(assemblyFolderList);
            }
            catch
            {
                // ignore
            }

            // Notify the compiler about a domain reload
            if (IsDebugging)
            {
                UnityEngine.Debug.Log("Burst - Domain Reload");
            }

            // Notify the JitCompilerService about a domain reload
            BurstCompiler.DomainReload();

            // Make sure that the X86 CSR function pointers are compiled
            Intrinsics.X86.CompileManagedCsrAccessors();

            // Make sure BurstRuntime is initialized
            BurstRuntime.Initialize();
        }
Exemplo n.º 9
0
        DataType(Type type)
        {
            Assert.IsTrue(HasDataAttribute(type));

            var fields = type.GetFields(
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            var inputFields    = new List <Field>();
            var outputFields   = new List <Field>();
            var propertyFields = new List <Field>();

            foreach (var field in fields)
            {
                string name = field.Name;

                var inputAttribute = field.GetCustomAttribute <InputAttribute>();

                if (inputAttribute != null)
                {
                    if (field.FieldType != typeof(DebugIdentifier))
                    {
                        throw new ArgumentException($"Data type {type.FullName} has input {name} of type {field.FieldType.FullName}, all inputs should be of type {nameof(DebugIdentifier)}", "type");
                    }

                    var inputType = typeof(DebugIdentifier);

                    if (!string.IsNullOrEmpty(inputAttribute.name))
                    {
                        name = inputAttribute.name;
                    }

                    inputFields.Add(Field.Create(field, inputType, name, false));
                }

                var outputAttribute = field.GetCustomAttribute <OutputAttribute>();

                if (outputAttribute != null)
                {
                    if (field.FieldType != typeof(DebugIdentifier))
                    {
                        throw new ArgumentException($"Data type {type.FullName} has output {name} of type {field.FieldType.FullName}, all outputs should be of type {nameof(DebugIdentifier)}", "type");
                    }

                    var outputType = typeof(DebugIdentifier);

                    if (!string.IsNullOrEmpty(outputAttribute.name))
                    {
                        name = outputAttribute.name;
                    }

                    outputFields.Add(Field.Create(field, outputType, name, outputAttribute.AcceptOnlySelfNode));
                }

                var propertyAttribute = field.GetCustomAttribute <PropertyAttribute>();

                if (propertyAttribute != null)
                {
                    if (!string.IsNullOrEmpty(propertyAttribute.name))
                    {
                        name = propertyAttribute.name;
                    }

                    propertyFields.Add(Field.Create(field, field.FieldType, name, inputAttribute.AcceptOnlySelfNode));
                }
            }

            hashCode = BurstRuntime.GetHashCode32(type);

            this.type = type;

            this.inputFields    = inputFields.ToArray();
            this.outputFields   = outputFields.ToArray();
            this.propertyFields = propertyFields.ToArray();
        }
Exemplo n.º 10
0
        static BurstLoader()
        {
            if (BurstCompilerOptions.ForceDisableBurstCompilation)
            {
                UnityEngine.Debug.LogWarning("[com.unity.burst] Burst is disabled entirely from the command line");
                return;
            }

            // This can be setup to get more diagnostics
            var debuggingStr = Environment.GetEnvironmentVariable("UNITY_BURST_DEBUG");

            IsDebugging = debuggingStr != null;
            if (IsDebugging)
            {
                UnityEngine.Debug.LogWarning("[com.unity.burst] Extra debugging is turned on.");
                int debuggingLevel;
                int.TryParse(debuggingStr, out debuggingLevel);
                if (debuggingLevel <= 0)
                {
                    debuggingLevel = 1;
                }
                DebuggingLevel = debuggingLevel;
            }

            // Try to load the runtime through an environment variable
            if (!UnityBurstRuntimePathOverwritten(out var path))
            {
                // Otherwise try to load it from the package itself
                path = Path.GetFullPath("Packages/com.unity.burst/.Runtime");
            }

            RuntimePath = path;

            if (IsDebugging)
            {
                UnityEngine.Debug.LogWarning($"[com.unity.burst] Runtime directory set to {RuntimePath}");
            }

            BurstCompilerService.Initialize(RuntimePath, TryGetOptionsFromMemberDelegate);

            // It's important that this call comes *after* BurstCompilerService.Initialize,
            // otherwise any calls from within EnsureSynchronized to BurstCompilerService,
            // such as BurstCompiler.Disable(), will silently fail.
            BurstEditorOptions.EnsureSynchronized();

            EditorApplication.quitting += OnEditorApplicationQuitting;

#if UNITY_2019_1_OR_NEWER
            CompilationPipeline.compilationStarted += OnCompilationStarted;
#endif

            CompilationPipeline.assemblyCompilationStarted  += OnAssemblyCompilationStarted;
            CompilationPipeline.assemblyCompilationFinished += OnAssemblyCompilationFinished;
            EditorApplication.playModeStateChanged          += EditorApplicationOnPlayModeStateChanged;
            AppDomain.CurrentDomain.DomainUnload            += OnDomainUnload;

            VersionUpdateCheck();

            BurstReflection.EnsureInitialized();

#if !UNITY_2019_3_OR_NEWER
            // Workaround to update the list of assembly folders as soon as possible
            // in order for the JitCompilerService to not fail with AssemblyResolveExceptions.
            // This workaround is only necessary for editors prior to 2019.3 (i.e. 2018.4),
            // because 2019.3+ include a fix on the Unity side.
            try
            {
                var assemblyList    = BurstReflection.AllEditorAssemblies;
                var assemblyFolders = new HashSet <string>();
                foreach (var assembly in assemblyList)
                {
                    try
                    {
                        var fullPath       = Path.GetFullPath(assembly.Location);
                        var assemblyFolder = Path.GetDirectoryName(fullPath);
                        if (!string.IsNullOrEmpty(assemblyFolder))
                        {
                            assemblyFolders.Add(assemblyFolder);
                        }
                    }
                    catch
                    {
                        // ignore
                    }
                }

                // Notify the compiler
                var assemblyFolderList = assemblyFolders.ToList();
                if (IsDebugging)
                {
                    UnityEngine.Debug.Log($"Burst - Change of list of assembly folders:\n{string.Join("\n", assemblyFolderList)}");
                }
                BurstCompiler.UpdateAssemblerFolders(assemblyFolderList);
            }
            catch
            {
                // ignore
            }
#endif

            // Notify the compiler about a domain reload
            if (IsDebugging)
            {
                UnityEngine.Debug.Log("Burst - Domain Reload");
            }

            // Notify the JitCompilerService about a domain reload
            BurstCompiler.DomainReload();

#if UNITY_2020_1_OR_NEWER
            BurstCompiler.OnProgress += OnProgress;
            BurstCompiler.SetProgressCallback();
#endif

#if !BURST_INTERNAL && !UNITY_DOTSPLAYER
            // Make sure that the X86 CSR function pointers are compiled
            Intrinsics.X86.CompileManagedCsrAccessors();
#endif

            // Make sure BurstRuntime is initialized
            BurstRuntime.Initialize();

            // Schedule upfront compilation of all methods in all assemblies,
            // with the goal of having as many methods as possible Burst-compiled
            // by the time the user enters PlayMode.
            if (!EditorApplication.isPlayingOrWillChangePlaymode)
            {
                MaybeTriggerEagerCompilation();
            }

#if UNITY_2020_1_OR_NEWER
            // Can't call Menu.AddMenuItem immediately, presumably because the menu controller isn't initialized yet.
            EditorApplication.CallDelayed(() => CreateDynamicMenuItems());
#endif
        }
 public static int GetGenericHash <T>()
 {
     return(BurstRuntime.GetHashCode32 <SomeStruct <T> >());
 }
 public static int GetIntHash()
 {
     return(BurstRuntime.GetHashCode32 <int>());
 }
 /// <summary>
 /// Converts the generic type into a type index.
 /// </summary>
 /// <remarks>
 /// Given a type passed as generic argument, this method performs
 /// a lookup in the types stored in the runtime asset and returns
 /// the corresponding type index. This index can then be used to
 /// extract reflection information for the given type. The returned
 /// type index will be invalid if the generic type passed as argument
 /// can not be found in the runtime asset.
 /// </remarks>
 /// <returns>The type index that corresponds to the generic type.</returns>
 /// <seealso cref="GetType"/>
 public TypeIndex GetTypeIndex <T>() where T : struct
 {
     return(GetTypeIndex(
                BurstRuntime.GetHashCode32 <T>()));
 }
Exemplo n.º 14
0
 public static SharedStatic <T> GetOrCreate <TContext, TSubContext>(uint alignment = 0) =>
 new SharedStatic <T>(SharedStatic.GetOrCreateSharedStaticInternal(
                          BurstRuntime.GetHashCode64 <TContext>(),
                          BurstRuntime.GetHashCode64 <TSubContext>(),
                          (uint)UnsafeUtility.SizeOf <T>(),
                          alignment));