Profiler and LocalToWorldThreadedSystem
Added a simple profiler Testing LocalToWorldSystem with Parallel execution for root nodes
This commit is contained in:
@ -111,10 +111,14 @@ public static class Engine
|
||||
|
||||
private static void Tick()
|
||||
{
|
||||
Profiler.BeginFrame();
|
||||
|
||||
AdvanceElapsedTime();
|
||||
|
||||
if (framerateCapped)
|
||||
{
|
||||
Profiler.BeginSample("framerateCapped");
|
||||
|
||||
/* We want to wait until the framerate cap,
|
||||
* but we don't want to oversleep. Requesting repeated 1ms sleeps and
|
||||
* seeing how long we actually slept for lets us estimate the worst case
|
||||
@ -137,6 +141,8 @@ public static class Engine
|
||||
Thread.SpinWait(1);
|
||||
AdvanceElapsedTime();
|
||||
}
|
||||
|
||||
Profiler.EndSample();
|
||||
}
|
||||
|
||||
// Do not let any step take longer than our maximum.
|
||||
@ -149,6 +155,7 @@ public static class Engine
|
||||
{
|
||||
while (accumulatedUpdateTime >= Timestep)
|
||||
{
|
||||
Profiler.BeginSample("Update");
|
||||
Keyboard.Update();
|
||||
Mouse.Update();
|
||||
GamePad.Update();
|
||||
@ -156,19 +163,26 @@ public static class Engine
|
||||
ProcessSDLEvents();
|
||||
|
||||
// Tick game here...
|
||||
Profiler.BeginSample("OnUpdate");
|
||||
OnUpdate?.Invoke();
|
||||
Profiler.EndSample();
|
||||
|
||||
AudioDevice.WakeThread();
|
||||
accumulatedUpdateTime -= Timestep;
|
||||
Profiler.EndSample();
|
||||
}
|
||||
|
||||
double alpha = accumulatedUpdateTime / Timestep;
|
||||
|
||||
// Render here..
|
||||
Profiler.BeginSample("OnRender");
|
||||
OnRender?.Invoke();
|
||||
Profiler.EndSample();
|
||||
|
||||
accumulatedDrawTime -= framerateCapTimeSpan;
|
||||
}
|
||||
|
||||
Profiler.EndFrame();
|
||||
}
|
||||
|
||||
private static TimeSpan AdvanceElapsedTime()
|
||||
|
@ -1,6 +1,4 @@
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Nerfed.Runtime;
|
||||
|
||||
@ -17,13 +15,120 @@ public struct ProfilerScope : IDisposable
|
||||
|
||||
public static class Profiler
|
||||
{
|
||||
[Conditional("PROFILING")]
|
||||
public static void BeginSample(string label) {
|
||||
public struct ProfileRecord
|
||||
{
|
||||
public string label;
|
||||
public long startTime;
|
||||
public long endTime;
|
||||
public int depth;
|
||||
|
||||
public readonly double ElapsedMilliseconds()
|
||||
{
|
||||
long elapsedTicks = endTime - startTime;
|
||||
return ((double)(elapsedTicks * 1000)) / Stopwatch.Frequency;
|
||||
}
|
||||
}
|
||||
|
||||
public class FrameData
|
||||
{
|
||||
public uint frame;
|
||||
public readonly List<ProfileRecord> records = new List<ProfileRecord>();
|
||||
public long startTime;
|
||||
public long endTime;
|
||||
|
||||
public FrameData(uint frame, long startTime)
|
||||
{
|
||||
this.frame = frame;
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
public double ElapsedMilliseconds()
|
||||
{
|
||||
long elapsedTicks = endTime - startTime;
|
||||
return ((double)(elapsedTicks * 1000)) / Stopwatch.Frequency;
|
||||
}
|
||||
}
|
||||
|
||||
private const int maxFrames = 128;
|
||||
|
||||
public static readonly BoundedQueue<FrameData> frames = new(maxFrames);
|
||||
public static bool recording = true;
|
||||
|
||||
private static readonly Stopwatch stopwatch = new Stopwatch();
|
||||
private static FrameData currentFrame = null;
|
||||
private static uint currentFrameIndex = 0;
|
||||
private static int currentDepth = 0;
|
||||
|
||||
static Profiler()
|
||||
{
|
||||
stopwatch.Start();
|
||||
}
|
||||
|
||||
[Conditional("PROFILING")]
|
||||
public static void EndSample() {
|
||||
public static void BeginFrame()
|
||||
{
|
||||
if (!recording)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
currentFrame = new FrameData(currentFrameIndex, stopwatch.ElapsedTicks);
|
||||
currentDepth = 0;
|
||||
currentFrameIndex++;
|
||||
}
|
||||
|
||||
[Conditional("PROFILING")]
|
||||
public static void EndFrame()
|
||||
{
|
||||
if (!recording)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
currentFrame.endTime = stopwatch.ElapsedTicks;
|
||||
frames.Enqueue(currentFrame);
|
||||
}
|
||||
|
||||
[Conditional("PROFILING")]
|
||||
public static void BeginSample(string label)
|
||||
{
|
||||
if (!recording)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ProfileRecord record = new ProfileRecord
|
||||
{
|
||||
label = label,
|
||||
startTime = stopwatch.ElapsedTicks,
|
||||
depth = currentDepth,
|
||||
};
|
||||
currentFrame.records.Add(record);
|
||||
//Log.Info($"{record.label} {record.depth} | {record.startTime}");
|
||||
currentDepth++; // Increase depth for nested scopes
|
||||
}
|
||||
|
||||
[Conditional("PROFILING")]
|
||||
public static void EndSample()
|
||||
{
|
||||
if (!recording)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
currentDepth--; // Decrease depth when exiting a scope
|
||||
|
||||
// Find the last uncompleted record at the current depth and set the end time
|
||||
for (int i = currentFrame.records.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (currentFrame.records[i].endTime == 0)
|
||||
{
|
||||
ProfileRecord record = currentFrame.records[i];
|
||||
record.endTime = stopwatch.ElapsedTicks;
|
||||
currentFrame.records[i] = record; // Assign back to the list
|
||||
//Log.Info($"{record.label} | {record.depth} | {record.endTime}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -24,11 +24,9 @@ namespace Nerfed.Runtime.Systems
|
||||
|
||||
public override void Update(TimeSpan delta)
|
||||
{
|
||||
Matrix4x4 rootMatrix = Matrix4x4.Identity;
|
||||
|
||||
foreach (Entity entity in rootEntitiesFilter.Entities)
|
||||
{
|
||||
UpdateWorldTransform(entity, rootMatrix);
|
||||
UpdateWorldTransform(entity, Matrix4x4.Identity);
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,18 +35,17 @@ namespace Nerfed.Runtime.Systems
|
||||
// TODO: Only update dirty transforms.
|
||||
// If a parent is dirty all the children need to update their localToWorld matrix.
|
||||
// How do we check if something is dirty? How do we know if a LocalTransform has been changed?
|
||||
|
||||
if (Has<LocalTransform>(entity))
|
||||
{
|
||||
LocalTransform localTransform = Get<LocalTransform>(entity);
|
||||
localToWorldMatrix = Matrix4x4.Multiply(localToWorldMatrix, localTransform.TRS());
|
||||
LocalToWorld localToWorld = new(localToWorldMatrix);
|
||||
Set(entity, localToWorld);
|
||||
|
||||
//Task.Delay(10).Wait();
|
||||
//Log.Info($"Entity {entity} | local position {localTransform.position} | world position {localToWorldMatrix.Translation}");
|
||||
}
|
||||
|
||||
ReverseSpanEnumerator<Entity> childEntities = World.OutRelations<ChildParentRelation>(entity);
|
||||
ReverseSpanEnumerator<Entity> childEntities = World.InRelations<ChildParentRelation>(entity);
|
||||
foreach (Entity childEntity in childEntities)
|
||||
{
|
||||
UpdateWorldTransform(childEntity, localToWorldMatrix);
|
||||
|
81
Nerfed.Runtime/Systems/LocalToWorldThreadedSystem.cs
Normal file
81
Nerfed.Runtime/Systems/LocalToWorldThreadedSystem.cs
Normal file
@ -0,0 +1,81 @@
|
||||
using MoonTools.ECS;
|
||||
using Nerfed.Runtime.Components;
|
||||
using Nerfed.Runtime.Util;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Nerfed.Runtime.Systems
|
||||
{
|
||||
public class LocalToWorldThreadedSystem : MoonTools.ECS.System
|
||||
{
|
||||
private readonly Filter rootEntitiesFilter;
|
||||
private readonly Action<int> forEntity;
|
||||
|
||||
public LocalToWorldThreadedSystem(World world) : base(world)
|
||||
{
|
||||
rootEntitiesFilter = FilterBuilder.Include<LocalTransform>().Exclude<Child>().Build();
|
||||
forEntity = UpdateEntity;
|
||||
}
|
||||
|
||||
public override void Update(TimeSpan delta)
|
||||
{
|
||||
Parallel.For(0, rootEntitiesFilter.Count, forEntity);
|
||||
}
|
||||
|
||||
private void UpdateEntity(int entityFilterIndex)
|
||||
{
|
||||
Entity entity = rootEntitiesFilter.NthEntity(entityFilterIndex);
|
||||
UpdateWorldTransform(entity, Matrix4x4.Identity);
|
||||
}
|
||||
|
||||
private void UpdateWorldTransform(Entity entity, Matrix4x4 localToWorldMatrix)
|
||||
{
|
||||
// TODO: Only update dirty transforms.
|
||||
// If a parent is dirty all the children need to update their localToWorld matrix.
|
||||
// How do we check if something is dirty? How do we know if a LocalTransform has been changed?
|
||||
|
||||
if (Has<LocalTransform>(entity))
|
||||
{
|
||||
LocalTransform localTransform = Get<LocalTransform>(entity);
|
||||
localToWorldMatrix = Matrix4x4.Multiply(localToWorldMatrix, localTransform.TRS());
|
||||
LocalToWorld localToWorld = new(localToWorldMatrix);
|
||||
Set(entity, localToWorld);
|
||||
//Task.Delay(10).Wait();
|
||||
//Log.Info($"Entity {entity} | local position {localTransform.position} | world position {localToWorldMatrix.Translation}");
|
||||
}
|
||||
|
||||
ReverseSpanEnumerator<Entity> childEntities = World.InRelations<ChildParentRelation>(entity);
|
||||
foreach (Entity childEntity in childEntities)
|
||||
{
|
||||
UpdateWorldTransform(childEntity, localToWorldMatrix);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//System.Random rnd = new System.Random();
|
||||
|
||||
//World world = new World();
|
||||
//Filter filter = world.FilterBuilder.Include<Test>().Build();
|
||||
|
||||
//for (int i = 0; i < 100; i++)
|
||||
//{
|
||||
// Entity e = world.CreateEntity(i.ToString());
|
||||
// world.Set(e, new Test());
|
||||
//}
|
||||
|
||||
//Action<int> forEntityIndex = ForEntityIndex;
|
||||
//ParallelLoopResult result = Parallel.For(0, filter.Count, forEntityIndex);
|
||||
//Console.WriteLine(result.IsCompleted);
|
||||
|
||||
//void ForEntityIndex(int entity)
|
||||
//{
|
||||
// int delay = rnd.Next(1, 1000);
|
||||
// Task.Delay(delay).Wait();
|
||||
// Console.WriteLine($"ForEntityIndex | {filter.NthEntity(entity).ID}");
|
||||
//}
|
||||
|
||||
//namespace Hoi
|
||||
//{
|
||||
// public readonly record struct Test;
|
||||
//}
|
73
Nerfed.Runtime/Util/BoundedQueue.cs
Normal file
73
Nerfed.Runtime/Util/BoundedQueue.cs
Normal file
@ -0,0 +1,73 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Nerfed.Runtime;
|
||||
|
||||
public class BoundedQueue<T> : IEnumerable<T>, ICollection, IReadOnlyCollection<T>
|
||||
{
|
||||
private readonly Queue<T> queue = null;
|
||||
private readonly int maxSize = 10;
|
||||
private T lastAddedElement;
|
||||
|
||||
public BoundedQueue(int maxSize)
|
||||
{
|
||||
this.maxSize = maxSize;
|
||||
queue = new Queue<T>(maxSize);
|
||||
}
|
||||
|
||||
public void Enqueue(T item)
|
||||
{
|
||||
queue.Enqueue(item);
|
||||
if (queue.Count > maxSize)
|
||||
{
|
||||
queue.Dequeue(); // Remove the oldest element
|
||||
}
|
||||
|
||||
lastAddedElement = item;
|
||||
}
|
||||
|
||||
public T Dequeue()
|
||||
{
|
||||
return queue.Dequeue();
|
||||
}
|
||||
|
||||
public T Peek()
|
||||
{
|
||||
return queue.Peek();
|
||||
}
|
||||
|
||||
public T LastAddedElement()
|
||||
{
|
||||
return lastAddedElement;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
queue.Clear();
|
||||
}
|
||||
|
||||
public bool Contains(T item)
|
||||
{
|
||||
return queue.Contains(item);
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return queue.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return queue.GetEnumerator();
|
||||
}
|
||||
|
||||
public void CopyTo(Array array, int index)
|
||||
{
|
||||
((ICollection)queue).CopyTo(array, index);
|
||||
}
|
||||
|
||||
public int Count => queue.Count;
|
||||
public int Capacity => maxSize;
|
||||
public bool IsSynchronized => ((ICollection)queue).IsSynchronized;
|
||||
public object SyncRoot => ((ICollection)queue).SyncRoot;
|
||||
int IReadOnlyCollection<T>.Count => queue.Count;
|
||||
}
|
Reference in New Issue
Block a user