Profiler and LocalToWorldThreadedSystem
Added a simple profiler Testing LocalToWorldSystem with Parallel execution for root nodes
This commit is contained in:
@ -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;
|
||||
//}
|
Reference in New Issue
Block a user