Beispiel #1
0
        /// <summary>
        /// Begins profile.
        /// </summary>
        /// <param name="profileColor">The profile event color.</param>
        /// <param name="profilingKey">The <see cref="ProfilingKey"/></param>
        public Scope BeginProfile(Color4 profileColor, ProfilingKey profilingKey)
        {
            if (!Profiler.IsEnabled(profilingKey))
            {
                return(new Scope(this, profilingKey));
            }

            EnsureQueryPoolSize();

            // Push the current query range onto the stack
            var query = new QueryEvent
            {
                ProfilingKey = profilingKey,
                Pool         = currentQueryPool,
                Index        = currentQueryIndex++,
            };

            queries.Push(query);

            // Query the timestamp at the beginning of the range
            commandList.WriteTimestamp(currentQueryPool, query.Index);

            // Add the queries to the list of queries to proceess
            queryEvents.Enqueue(query);

            // Sets a debug marker if debug mode is enabled
            if (commandList.GraphicsDevice.IsDebugMode)
            {
                commandList.BeginProfile(profileColor, profilingKey.Name);
            }

            return(new Scope(this, profilingKey));
        }
Beispiel #2
0
        /// <summary>
        /// Ends profile.
        /// </summary>
        public void EndProfile(ProfilingKey profilingKey)
        {
            if (!Profiler.IsEnabled(profilingKey))
            {
                return;
            }

            if (queries.Count == 0)
            {
                throw new InvalidOperationException();
            }

            EnsureQueryPoolSize();

            // Get the current query
            var query = queries.Pop();

            query.Pool         = currentQueryPool;
            query.Index        = currentQueryIndex++;
            query.ProfilingKey = null;

            // Query the timestamp at the end of the range
            commandList.WriteTimestamp(query.Pool, query.Index);

            // Add the queries to the list of queries to proceess
            queryEvents.Enqueue(query);

            // End the debug marker
            if (commandList.GraphicsDevice.IsDebugMode)
            {
                commandList.EndProfile();
            }
        }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ComponentBase" /> class.
 /// </summary>
 /// <param name="name">The name attached to this component</param>
 protected RendererCoreBase(string name)
     : base(name)
 {
     Enabled = true;
     subRenderersToUnload = new List <IGraphicsRendererCore>();
     Profiling            = true;
     ProfilingKey         = new ProfilingKey(name ?? nameof(RendererCoreBase));
 }
Beispiel #4
0
        /// <summary>
        /// Starts this <see cref="MicroThread"/> with the specified function.
        /// </summary>
        /// <param name="microThreadFunction">The micro thread function.</param>
        /// <param name="flags">The flags.</param>
        /// <param name="scheduleMode">The schedule mode.</param>
        /// <exception cref="System.InvalidOperationException">MicroThread was already started before.</exception>
        public void Start(Func <Task> microThreadFunction, ScheduleMode scheduleMode = ScheduleMode.Last)
        {
            ProfilingKey = new ProfilingKey("MicroThread " + microThreadFunction.Target);

            // TODO: Interlocked compare exchange?
            if (Interlocked.CompareExchange(ref state, (int)MicroThreadState.Starting, (int)MicroThreadState.None) != (int)MicroThreadState.None)
            {
                throw new InvalidOperationException("MicroThread was already started before.");
            }

            Action wrappedMicroThreadFunction = async() =>
            {
                try
                {
                    State = MicroThreadState.Running;

                    await microThreadFunction();

                    if (State != MicroThreadState.Running)
                    {
                        throw new InvalidOperationException("MicroThread completed in an invalid state.");
                    }
                    State = MicroThreadState.Completed;
                }
                catch (Exception e)
                {
                    Scheduler.Log.Error("Unexpected exception while executing a micro-thread. Reason: {0}", new object[] { e });
                    SetException(e);
                }
                finally
                {
                    lock (Scheduler.allMicroThreads)
                    {
                        Scheduler.allMicroThreads.Remove(AllLinkedListNode);
                    }
                }
            };

            Action callback = () =>
            {
                SynchronizationContext = new MicroThreadSynchronizationContext(this);
                SynchronizationContext.SetSynchronizationContext(SynchronizationContext);

                wrappedMicroThreadFunction();
            };

            lock (Scheduler.allMicroThreads)
            {
                Scheduler.allMicroThreads.AddLast(AllLinkedListNode);
            }

            ScheduleContinuation(scheduleMode, callback);
        }
Beispiel #5
0
        /// <summary>
        /// Creates the game system key value from the GameSystems collection if it exists, or creates a game system.
        /// </summary>
        protected GameSystemKeyValue <T> CreateKeyValue <T>(Func <T> createGameSystem) where T : GameSystemBase
        {
            T gameSystem = null;

            for (int i = 0; i < GameSystems.Count; i++)
            {
                if (GameSystems[i] is T existingGameSystem)
                {
                    gameSystem = existingGameSystem;
                    break;
                }
            }
            gameSystem ??= createGameSystem();

            var gameSystemKey = new ProfilingKey(GameProfilingKeys.GameUpdate, gameSystem.GetType().Name);

            return(new GameSystemKeyValue <T>(gameSystemKey, gameSystem));
        }
Beispiel #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EntityProcessor"/> class.
        /// </summary>
        /// <param name="mainComponentType">Type of the main component.</param>
        /// <param name="additionalTypes">The additional types required by this processor.</param>
        /// <exception cref="System.ArgumentNullException">If parameteters are null</exception>
        /// <exception cref="System.ArgumentException">If a type does not inherit from EntityComponent</exception>
        protected EntityProcessor(Type mainComponentType, Type[] additionalTypes)
        {
            if (mainComponentType == null)
            {
                throw new ArgumentNullException(nameof(mainComponentType));
            }
            if (additionalTypes == null)
            {
                throw new ArgumentNullException(nameof(additionalTypes));
            }

            MainComponentType = mainComponentType;
            mainTypeInfo      = MainComponentType.GetTypeInfo();
            Enabled           = true;

            RequiredTypes = new TypeInfo[additionalTypes.Length];

            // Check that types are valid
            for (int i = 0; i < additionalTypes.Length; i++)
            {
                var requiredType = additionalTypes[i];
                if (!typeof(EntityComponent).GetTypeInfo().IsAssignableFrom(requiredType.GetTypeInfo()))
                {
                    throw new ArgumentException($"Invalid required type [{requiredType}]. Expecting only an EntityComponent type");
                }

                RequiredTypes[i] = requiredType.GetTypeInfo();
            }

            if (RequiredTypes.Length > 0)
            {
                componentTypesSupportedAsRequired = new Dictionary <TypeInfo, bool>();
            }

            UpdateProfilingKey = new ProfilingKey(GameProfilingKeys.GameUpdate, this.GetType().Name);
            DrawProfilingKey   = new ProfilingKey(GameProfilingKeys.GameDraw, this.GetType().Name);
        }
Beispiel #7
0
        private static bool AddGameSystem <T>(T gameSystem, List <KeyValuePair <T, ProfilingKey> > gameSystems, IComparer <KeyValuePair <T, ProfilingKey> > comparer, ProfilingKey parentProfilingKey, bool removePreviousSystem = false)
            where T : class
        {
            lock (gameSystems)
            {
                var gameSystemKey = new KeyValuePair <T, ProfilingKey>(gameSystem, null);

                // Find this gameSystem
                int index = -1;
                for (int i = 0; i < gameSystems.Count; ++i)
                {
                    if (gameSystem == gameSystems[i].Key)
                    {
                        index = i;
                    }
                }

                // If we are updating the order
                if (index >= 0)
                {
                    if (removePreviousSystem)
                    {
                        gameSystemKey = gameSystems[index];
                        gameSystems.RemoveAt(index);
                        index = -1;
                    }
                }
                else
                {
                    gameSystemKey = new KeyValuePair <T, ProfilingKey>(gameSystemKey.Key, new ProfilingKey(parentProfilingKey, gameSystem.GetType().Name));
                }

                if (index == -1)
                {
                    // we want to insert right after all other systems with same draw/update order
                    index = gameSystems.UpperBound(gameSystemKey, comparer, 0, gameSystems.Count);

                    gameSystems.Insert(index, gameSystemKey);

                    // True, the system was inserted
                    return(true);
                }
            }

            // False, it is already in the list
            return(false);
        }
Beispiel #8
0
 protected EntityProcessor(PropertyKey[] requiredKeys)
 {
     UpdateProfilingKey = new ProfilingKey(GameProfilingKeys.GameUpdate, this.GetType().Name);
     DrawProfilingKey   = new ProfilingKey(GameProfilingKeys.GameDraw, this.GetType().Name);
     this.requiredKeys  = requiredKeys;
 }
Beispiel #9
0
        // TODO: We will need a better API than exposing PriorityQueueNode<SchedulerEntry> before we can make this public.
        internal PriorityQueueNode <SchedulerEntry> Add(Action simpleAction, int priority = 0, object token = null, ProfilingKey profilingKey = null)
        {
            var schedulerEntryNode = new PriorityQueueNode <SchedulerEntry>(new SchedulerEntry(simpleAction, priority)
            {
                Token = token, ProfilingKey = profilingKey
            });

            Schedule(schedulerEntryNode, ScheduleMode.Last);
            return(schedulerEntryNode);
        }
Beispiel #10
0
 public Scope(QueryManager queryManager, ProfilingKey profilingKey)
 {
     this.queryManager = queryManager;
     this.profilingKey = profilingKey;
 }
Beispiel #11
0
        /// <summary>
        /// Creates the game system key value from the supplied game system..
        /// </summary>
        protected static GameSystemKeyValue <T> CreateKeyValue <T>(T gameSystem) where T : GameSystemBase
        {
            var gameSystemKey = new ProfilingKey(GameProfilingKeys.GameUpdate, gameSystem.GetType().Name);

            return(new GameSystemKeyValue <T>(gameSystemKey, gameSystem));
        }
Beispiel #12
0
 protected EntityProcessor()
 {
     UpdateProfilingKey = new ProfilingKey(GameProfilingKeys.GameUpdate, this.GetType().Name);
     DrawProfilingKey   = new ProfilingKey(GameProfilingKeys.GameDraw, this.GetType().Name);
 }
Beispiel #13
0
 public GameSystemKeyValue(ProfilingKey profilingKey, T system)
 {
     ProfilingKey = profilingKey;
     System       = system;
 }