testing building some core systems

- serialization
- chunks
- parralelfor test
This commit is contained in:
max
2026-04-24 19:21:03 +02:00
parent 5eaf3547dc
commit fec2cd8d24
15 changed files with 999 additions and 98 deletions
+38 -13
View File
@@ -1,6 +1,8 @@
using MoonTools.ECS;
using Nerfed.Runtime.Components;
using Nerfed.Runtime.Util;
using System;
using System.Collections.Generic;
using System.Numerics;
// TODO:
@@ -15,10 +17,19 @@ 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.
public override IReadOnlySet<Type> ReadsComponents { get; } = new HashSet<Type> { typeof(LocalTransform) };
public override IReadOnlySet<Type> WritesComponents { get; } = new HashSet<Type> { typeof(LocalToWorld) };
private readonly bool useParallelFor = true;
private const int ParallelForMinCount = 32; // Below this, parallel overhead costs more than it saves.
private static readonly System.Threading.Tasks.ParallelOptions ParallelOptions = new()
{
MaxDegreeOfParallelism = Environment.ProcessorCount
};
private readonly Filter rootEntitiesFilter;
private readonly Filter entitiesWithoutLocalToWorldFilter;
private readonly Action<int> updateWorldTransform;
private ParallelWriter<LocalToWorld> _parallelWriter;
public LocalToWorldSystem(World world) : base(world)
{
@@ -39,50 +50,64 @@ namespace Nerfed.Runtime.Systems
if (useParallelFor)
{
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.
Profiler.BeginSample("ParallelFor.LocalToWorldCheck");
foreach (Entity entity in entitiesWithoutLocalToWorldFilter.Entities) {
Set(entity, new LocalToWorld(Matrix4x4.Identity));
}
Profiler.EndSample();
// Acquire a ParallelWriter AFTER pre-allocation — all entities now have LocalToWorld.
// This writer only permits updating existing values; no structural mutations allowed.
_parallelWriter = World.GetParallelWriter<LocalToWorld>();
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);
if (rootEntitiesFilter.Count >= ParallelForMinCount)
{
Parallel.For(0, rootEntitiesFilter.Count, ParallelOptions, updateWorldTransform);
}
else
{
// Not enough work to justify thread overhead — run serially.
for (int i = 0; i < rootEntitiesFilter.Count; i++)
{
updateWorldTransform(i);
}
}
Profiler.EndSample();
}
else
{
foreach (Entity entity in rootEntitiesFilter.Entities)
{
Profiler.BeginSample("UpdateWorldTransform");
// Profiler.BeginSample("UpdateWorldTransform");
UpdateWorldTransform(entity, Matrix4x4.Identity);
Profiler.EndSample();
// Profiler.EndSample();
}
}
}
private void UpdateWorldTransformByIndex(int entityFilterIndex)
{
Profiler.BeginSample("UpdateWorldTransformByIndex");
// Profiler.BeginSample("UpdateWorldTransformByIndex");
Entity entity = rootEntitiesFilter.NthEntity(entityFilterIndex);
UpdateWorldTransform(entity, Matrix4x4.Identity);
Profiler.EndSample();
// 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);
Set(entity, localToWorld);
if (useParallelFor)
_parallelWriter.Set(entity, localToWorld); // thread-safe: direct write, no structural mutation
else
Set(entity, localToWorld);
}
ReverseSpanEnumerator<Entity> childEntities = World.InRelations<ChildParentRelation>(entity);