Implement JobSystem for parallel task management and integrate with LocalToWorldSystem

This commit is contained in:
max
2026-08-05 14:04:58 +02:00
parent 6ad5aa3f2c
commit a0f2713fdc
3 changed files with 133 additions and 22 deletions
+21 -22
View File
@@ -15,19 +15,17 @@ namespace Nerfed.Runtime.Systems
{
public class LocalToWorldSystem : MoonTools.ECS.System
{
private readonly bool useParallelFor = true; // When having a low amount of transforms or when in debug mode this might be slower.
private readonly JobSystem jobs;
private readonly Filter rootEntitiesFilter;
private readonly Filter entitiesWithoutLocalToWorldFilter;
private readonly Action<int> updateWorldTransform;
private readonly Action<int> updateWorldTransformByIndex;
public LocalToWorldSystem(World world) : base(world)
public LocalToWorldSystem(World world, JobSystem jobs = null) : base(world)
{
this.jobs = jobs ?? JobSystem.Default;
rootEntitiesFilter = FilterBuilder.Include<LocalTransform>().Exclude<Child>().Build();
if (useParallelFor)
{
entitiesWithoutLocalToWorldFilter = FilterBuilder.Include<LocalTransform>().Exclude<LocalToWorld>().Build();
updateWorldTransform = UpdateWorldTransformByIndex;
}
entitiesWithoutLocalToWorldFilter = FilterBuilder.Include<LocalTransform>().Exclude<LocalToWorld>().Build();
updateWorldTransformByIndex = UpdateWorldTransformByIndex;
}
public override void Update(TimeSpan delta)
@@ -37,20 +35,18 @@ namespace Nerfed.Runtime.Systems
return;
}
if (useParallelFor)
if (this.jobs.WorkerCount > 0)
{
Profiler.BeginSample("ParallelFor.LocalToWorldCheck");
// This check is needed because some entities might not have a LocalToWorld component yet.
// Adding this during the loop will break.
foreach (Entity entity in entitiesWithoutLocalToWorldFilter.Entities) {
Profiler.BeginSample("LocalToWorldCheck");
// Structural pre-pass: ensure LocalToWorld exists on all entities before parallel writes.
foreach (Entity entity in entitiesWithoutLocalToWorldFilter.Entities)
{
Set(entity, new LocalToWorld(Matrix4x4.Identity));
}
Profiler.EndSample();
Profiler.BeginSample("ParallelFor.LocalToWorldUpdate");
// This should only be used when the filter doesn't change by executing these functions!
// So no entity deletion or setting/removing of components used by the filters in this loop.
Parallel.For(0, rootEntitiesFilter.Count, updateWorldTransform);
Profiler.BeginSample("LocalToWorldUpdate");
this.jobs.Dispatch(rootEntitiesFilter.Count, updateWorldTransformByIndex);
Profiler.EndSample();
}
else
@@ -66,22 +62,25 @@ namespace Nerfed.Runtime.Systems
private void UpdateWorldTransformByIndex(int entityFilterIndex)
{
Profiler.BeginSample("UpdateWorldTransformByIndex");
using ProfilerScope scope = new("UpdateWorldTransformByIndex");
Entity entity = rootEntitiesFilter.NthEntity(entityFilterIndex);
UpdateWorldTransform(entity, Matrix4x4.Identity);
Profiler.EndSample();
}
private void UpdateWorldTransform(in 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);
#if DEBUG
if (!Has<LocalToWorld>(entity))
{
throw new InvalidOperationException(
$"Entity {entity} is missing LocalToWorld. Ensure the structural pre-pass runs before parallel dispatch.");
}
#endif
Set(entity, localToWorld);
}