Update version support

This commit is contained in:
2025-12-11 21:25:32 +09:00
parent 856fa4f07d
commit a3863c1263
16 changed files with 1699 additions and 332 deletions

View File

@@ -287,6 +287,17 @@ public static class ResultExtensions
return result; return result;
} }
public static Result<T, E> OnSuccess<T, E>(this Result<T, E> result, Action<T> action)
where E : struct, Enum
{
if (result.IsSuccess)
{
action(result.Value);
}
return result;
}
public static Result OnFailed(this Result result, Action<string?> action) public static Result OnFailed(this Result result, Action<string?> action)
{ {
if (result.IsFailure) if (result.IsFailure)
@@ -306,4 +317,15 @@ public static class ResultExtensions
return result; return result;
} }
public static Result<T, E> OnFailed<T, E>(this Result<T, E> result, Action<E> action)
where E : struct, Enum
{
if (result.IsFailure)
{
action(result.Error);
}
return result;
}
} }

View File

@@ -1,14 +1,15 @@
using Ghost.Test.Core; using Ghost.Test.Core;
using Misaki.HighPerformance.Jobs; using Misaki.HighPerformance.Jobs;
using Misaki.HighPerformance.Mathematics; using Misaki.HighPerformance.Mathematics;
using System.Diagnostics;
namespace Ghost.Entities.Test; namespace Ghost.Entities.Test;
internal struct TestEntityQueryJob : IJobChunk internal struct TestChunkQueryJob : IJobChunk
{ {
public void Execute(ChunkView view, int threadIndex) public readonly void Execute(ChunkView view, int threadIndex)
{ {
var transforms = view.GetComponentData<Transform>(); var transforms = view.GetComponentDataRW<Transform>();
for (var i = 0; i < view.Count; i++) for (var i = 0; i < view.Count; i++)
{ {
transforms[i].position += new float3(10, 10, 10); transforms[i].position += new float3(10, 10, 10);
@@ -16,6 +17,14 @@ internal struct TestEntityQueryJob : IJobChunk
} }
} }
internal struct TestEntityQueryJob : IJobEntity<Transform>
{
public readonly void Execute(Entity entity, ref Transform transform, int index)
{
transform.position += new float3(5, 5, 5);
}
}
public partial class EntityTest : ITest public partial class EntityTest : ITest
{ {
private JobScheduler _jobScheduler = null!; private JobScheduler _jobScheduler = null!;
@@ -42,20 +51,20 @@ public partial class EntityTest : ITest
var queryID = new QueryBuilder().WithAllRW<Transform>().WithAbsent<Mesh>().Build(_world); var queryID = new QueryBuilder().WithAllRW<Transform>().WithAbsent<Mesh>().Build(_world);
ref var query = ref _world.GetEntityQueryReference(queryID); ref var query = ref _world.GetEntityQueryReference(queryID);
// var testJob = new TestEntityQueryJob();
// var handle = query.ScheduleChunkParallel(testJob, 64, JobHandle.Invalid);
// _jobScheduler.WaitComplete(handle);
_world.EntityManager.AddScriptComponent<TestScriptComponent>(entity1);
_world.EntityManager.RemoveComponent<ManagedEntityRef>(entity1); // This should destory the managed entity and call OnDestroy
_world.AdvanceVersion(); _world.AdvanceVersion();
query.ForEach<Transform>((e, ref t) => var testJob = new TestEntityQueryJob();
{ var handle = query.ScheduleEntityParallel<TestEntityQueryJob, Transform>(testJob, 64, JobHandle.Invalid);
Console.WriteLine($"Entity {e} Has Position: {t.position}"); _jobScheduler.WaitComplete(handle);
});
// _world.EntityManager.AddScriptComponent<TestScriptComponent>(entity1);
// _world.EntityManager.RemoveComponent<ManagedEntityRef>(entity1); // This should destory the managed entity and call OnDestroy
// query.ForEach<Transform>((e, ref t) =>
// {
// Console.WriteLine($"Entity {e} Has Position: {t.position}");
// });
//
// foreach (var (entity, transform) in query.GetEntityComponentIterator<Transform>()) // foreach (var (entity, transform) in query.GetEntityComponentIterator<Transform>())
// { // {
// Console.WriteLine($"Entity {entity} Updated Position: {transform.Get().position}"); // Console.WriteLine($"Entity {entity} Updated Position: {transform.Get().position}");
@@ -65,9 +74,10 @@ public partial class EntityTest : ITest
{ {
var transforms = chunk.GetComponentData<Transform>(); var transforms = chunk.GetComponentData<Transform>();
var entities = chunk.GetEntities(); var entities = chunk.GetEntities();
var bits = chunk.GetEnableBits<Transform>();
var changed = chunk.HasChanged<Transform>(0); if (chunk.HasChanged<Transform>(0))
{
var bits = chunk.GetEnableBits<Transform>();
var it = bits.GetIterator(); var it = bits.GetIterator();
while (it.Next(out var index) && index < chunk.Count) while (it.Next(out var index) && index < chunk.Count)
@@ -75,9 +85,10 @@ public partial class EntityTest : ITest
Console.WriteLine($"Entity {entities[index]} Updated Position: {transforms[index].position}"); Console.WriteLine($"Entity {entities[index]} Updated Position: {transforms[index].position}");
} }
} }
}
_world.EntityManager.DestroyEntity(entity1); Debug.Assert(_world.EntityManager.DestroyEntity(entity1) == Core.ErrorStatus.None);
_world.EntityManager.DestroyEntity(entity2); Debug.Assert(_world.EntityManager.DestroyEntity(entity2) == Core.ErrorStatus.None);
} }
public void Cleanup() public void Cleanup()

View File

@@ -57,18 +57,33 @@ internal unsafe sealed class ChunkDebugView
} }
var views = new List<object>(); var views = new List<object>();
ref var archetype = ref World.GetWorld(worldID).GetValueOrThrow() var r = World.GetWorld(worldID);
.GetArchetypeReference(archetypeID); if (!r)
foreach (var layout in archetype._layouts)
{ {
var type = Type.GetTypeFromHandle(RuntimeTypeHandle.FromIntPtr(ComponentRegister.s_runtimeIDToTypeHandle[layout.componentID])); return [];
}
ref var archetype = ref r.Value.GetArchetypeReference(archetypeID);
var it = archetype._signature.GetIterator();
while (it.Next(out var index))
{
var type = Type.GetTypeFromHandle(RuntimeTypeHandle.FromIntPtr(ComponentRegister.s_runtimeIDToTypeHandle[index]));
if (type == null)
{
continue;
}
var layout = archetype.GetLayout(index).Value;
var readMethod = typeof(ChunkDebugView) var readMethod = typeof(ChunkDebugView)
.GetMethod(nameof(ReadComponentArray), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) .GetMethod(nameof(ReadComponentArray), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
.MakeGenericMethod(type); .MakeGenericMethod(type);
// 3. Invoke it to get a Position[] or Velocity[] // 3. Invoke it to get a Position[] or Velocity[]
var array = readMethod.Invoke(this, [layout.offset]); var array = readMethod.Invoke(this, [layout.offset]);
if (array == null)
{
continue;
}
// 4. Wrap it in a nice label so the debugger shows "Position[]" // 4. Wrap it in a nice label so the debugger shows "Position[]"
views.Add(new ComponentArrayView(type.Name, array)); views.Add(new ComponentArrayView(type.Name, array));
@@ -127,18 +142,6 @@ internal unsafe struct Chunk : IDisposable
_versions.AsSpan().Fill(globalVersion); _versions.AsSpan().Fill(globalVersion);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void MarkChanged(int componentTypeId, int globalVersion)
{
_versions[componentTypeId] = globalVersion;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly int GetVersion(int componentTypeId)
{
return _versions[componentTypeId];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly byte* GetUnsafePtr() public readonly byte* GetUnsafePtr()
{ {
@@ -165,7 +168,8 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
public int componentID; public int componentID;
public int size; public int size;
public int offset; public int offset;
public int enableBitsOffset; // TODO: Support enableable component public int enableBitsOffset;
public int versionIndex;
} }
private struct Edge private struct Edge
@@ -192,6 +196,7 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
private int _entityIdsOffset; private int _entityIdsOffset;
public readonly Identifier<Archetype> ID => _id; public readonly Identifier<Archetype> ID => _id;
public readonly Identifier<World> WorldID => _worldID;
public readonly int EntityCapacity => _entityCapacity; public readonly int EntityCapacity => _entityCapacity;
public readonly int ChunkCount => _chunks.Count; public readonly int ChunkCount => _chunks.Count;
@@ -313,10 +318,11 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
{ {
_layouts[i] = new ComponentMemoryLayout _layouts[i] = new ComponentMemoryLayout
{ {
componentID = components[i].id,
offset = tempOffsets[i], offset = tempOffsets[i],
size = components[i].size, size = components[i].size,
componentID = components[i].id,
enableBitsOffset = tempBitmaskOffsets[i], enableBitsOffset = tempBitmaskOffsets[i],
versionIndex = i
}; };
_componentIDToLayoutIndex[components[i].id] = i; _componentIDToLayoutIndex[components[i].id] = i;
@@ -401,7 +407,7 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
MemoryUtility.MemCpy(dst, pComponent, (nuint)size); MemoryUtility.MemCpy(dst, pComponent, (nuint)size);
var world = World.GetWorldUncheck(_worldID); var world = World.GetWorldUncheck(_worldID);
chunk.MarkChanged(componentID, world.Version); MarkChanged(chunkIndex, componentID, world.Version);
return ErrorStatus.None; return ErrorStatus.None;
} }
@@ -446,6 +452,34 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
return _layouts[layoutIndex]; return _layouts[layoutIndex];
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ErrorStatus MarkChanged(int chunkIndex, int componentTypeId, int globalVersion)
{
var layoutResult = GetLayout(componentTypeId);
if (layoutResult.IsFailure)
{
return layoutResult.Error;
}
ref var chunk = ref _chunks[chunkIndex];
chunk.GetVersionUnsafePtr()[layoutResult.Value.versionIndex] = globalVersion;
return ErrorStatus.None;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Result<int, ErrorStatus> GetVersion(int chunkIndex, int componentTypeId)
{
var layoutResult = GetLayout(componentTypeId);
if (layoutResult.Error != ErrorStatus.None)
{
return layoutResult.Error;
}
ref var chunk = ref _chunks[chunkIndex];
return chunk.GetVersionUnsafePtr()[layoutResult.Value.versionIndex];
}
public ErrorStatus RemoveEntity(int chunkIndex, int rowIndex) public ErrorStatus RemoveEntity(int chunkIndex, int rowIndex)
{ {
if (chunkIndex < 0 || chunkIndex >= _chunks.Count) if (chunkIndex < 0 || chunkIndex >= _chunks.Count)

View File

@@ -103,17 +103,6 @@ internal static class ComponentRegister
} }
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetComponentLastWrite(Identifier<IComponent> typeId, int version)
{
lock (s_registeredComponents)
{
var info = s_registeredComponents[typeId];
info.lastWriteVersion = version;
s_registeredComponents[typeId] = info;
}
}
public static int GetHashCode(params ReadOnlySpan<Identifier<IComponent>> componentTypeIDs) public static int GetHashCode(params ReadOnlySpan<Identifier<IComponent>> componentTypeIDs)
{ {
var largestID = 0; var largestID = 0;

View File

@@ -8,8 +8,8 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<IsAotCompatible>True</IsAotCompatible> <IsAotCompatible>False</IsAotCompatible>
<IsTrimmable>True</IsTrimmable> <IsTrimmable>False</IsTrimmable>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">

View File

@@ -91,6 +91,7 @@ public readonly unsafe ref struct ChunkView
private readonly int* _pVersion; private readonly int* _pVersion;
private readonly int _entityOffset; private readonly int _entityOffset;
private readonly int _entityCount; private readonly int _entityCount;
private readonly int _currentVersion;
public readonly int Count => _entityCount; public readonly int Count => _entityCount;
@@ -101,6 +102,20 @@ public readonly unsafe ref struct ChunkView
_entityOffset = archetype.EntityIDsOffset; _entityOffset = archetype.EntityIDsOffset;
_entityCount = chunk._count; _entityCount = chunk._count;
_pVersion = chunk.GetVersionUnsafePtr(); _pVersion = chunk.GetVersionUnsafePtr();
_currentVersion = World.GetWorldUncheck(archetype.WorldID).Version;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Archetype.ComponentMemoryLayout GetLayout(Identifier<IComponent> id)
{
var layout = _layouts[id.value];
if (layout.enableBitsOffset == -1)
{
throw new InvalidOperationException($"Component {id} is not exist in the archetype.");
}
return layout;
} }
/// <summary> /// <summary>
@@ -112,7 +127,8 @@ public readonly unsafe ref struct ChunkView
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasChanged(Identifier<IComponent> id, int version) public bool HasChanged(Identifier<IComponent> id, int version)
{ {
return version < _pVersion[id]; var layout = GetLayout(id);
return version < _pVersion[layout.versionIndex];
} }
/// <summary> /// <summary>
@@ -126,7 +142,8 @@ public readonly unsafe ref struct ChunkView
public readonly bool HasChanged<T>(int version) public readonly bool HasChanged<T>(int version)
where T : unmanaged, IComponent where T : unmanaged, IComponent
{ {
return version < _pVersion[ComponentTypeID<T>.value]; var layout = GetLayout(ComponentTypeID<T>.value);
return version < _pVersion[layout.versionIndex];
} }
/// <summary> /// <summary>
@@ -163,6 +180,21 @@ public readonly unsafe ref struct ChunkView
return new ReadOnlySpan<Entity>(pEntity, _entityCount); return new ReadOnlySpan<Entity>(pEntity, _entityCount);
} }
/// <summary>
/// Gets a readonly span providing direct access to the component data of type T0 for structuralAll entities in the chunk.
/// </summary>
/// <typeparam name="T">The type of component to access. Must be an unmanaged type that implements <see cref="Component"/>.</typeparam>
/// <returns>A readonly span of type <see cref="{T}"/> containing the component data for each entity in the chunk.</returns>
/// <exception cref="InvalidOperationException">Thrown if the specified component type is not present in the archetype.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<T> GetComponentData<T>()
where T : unmanaged, IComponent
{
var layout = GetLayout(ComponentTypeID<T>.value);
var pComponentData = _pChunkData + layout.offset;
return new ReadOnlySpan<T>(pComponentData, _entityCount);
}
/// <summary> /// <summary>
/// Gets a span providing direct access to the component data of type T0 for structuralAll entities in the chunk. /// Gets a span providing direct access to the component data of type T0 for structuralAll entities in the chunk.
/// </summary> /// </summary>
@@ -170,10 +202,14 @@ public readonly unsafe ref struct ChunkView
/// <returns>A span of type <see cref="{T}"/> containing the component data for each entity in the chunk.</returns> /// <returns>A span of type <see cref="{T}"/> containing the component data for each entity in the chunk.</returns>
/// <exception cref="InvalidOperationException">Thrown if the specified component type is not present in the archetype.</exception> /// <exception cref="InvalidOperationException">Thrown if the specified component type is not present in the archetype.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<T> GetComponentData<T>() public Span<T> GetComponentDataRW<T>()
where T : unmanaged, IComponent where T : unmanaged, IComponent
{ {
var layout = _layouts[ComponentTypeID<T>.value]; var compId = ComponentTypeID<T>.value;
var layout = GetLayout(compId);
_pVersion[layout.versionIndex] = _currentVersion;
var pComponentData = _pChunkData + layout.offset; var pComponentData = _pChunkData + layout.offset;
return new Span<T>(pComponentData, _entityCount); return new Span<T>(pComponentData, _entityCount);
} }
@@ -190,11 +226,6 @@ public readonly unsafe ref struct ChunkView
where T : unmanaged, IEnableableComponent where T : unmanaged, IEnableableComponent
{ {
var layout = _layouts[ComponentTypeID<T>.value]; var layout = _layouts[ComponentTypeID<T>.value];
if (layout.enableBitsOffset == -1)
{
throw new InvalidOperationException($"Component {typeof(T).FullName} is not enableable.");
}
var maskBase = _pChunkData + layout.enableBitsOffset; var maskBase = _pChunkData + layout.enableBitsOffset;
return new SpanBitSet(new Span<uint>(maskBase, (_entityCount + 31) / 32)); return new SpanBitSet(new Span<uint>(maskBase, (_entityCount + 31) / 32));
} }
@@ -210,12 +241,7 @@ public readonly unsafe ref struct ChunkView
public bool IsComponentEnabled<T>(int index) public bool IsComponentEnabled<T>(int index)
where T : unmanaged, IEnableableComponent where T : unmanaged, IEnableableComponent
{ {
var layout = _layouts[ComponentTypeID<T>.value]; var layout = GetLayout(ComponentTypeID<T>.value);
if (layout.enableBitsOffset == -1)
{
throw new InvalidOperationException($"Component {typeof(T).FullName} is not enableable.");
}
var pMask = _pChunkData + layout.enableBitsOffset; var pMask = _pChunkData + layout.enableBitsOffset;
return EntityQuery.CheckBit(pMask, index); return EntityQuery.CheckBit(pMask, index);
} }

View File

@@ -1,6 +1,3 @@
using Ghost.Core;
using Misaki.HighPerformance.Jobs;
namespace Ghost.Entities; namespace Ghost.Entities;
public readonly ref struct SystemAPI public readonly ref struct SystemAPI
@@ -235,7 +232,6 @@ public class SystemManager
private readonly World _world; private readonly World _world;
private readonly List<ISystem> _systems = new (); private readonly List<ISystem> _systems = new ();
private readonly Dictionary<Type, int> _systemTypeMap = new ();
internal SystemManager(World world) internal SystemManager(World world)
{ {
@@ -247,15 +243,17 @@ public class SystemManager
{ {
var system = new T(); var system = new T();
_systems.Add(system); _systems.Add(system);
_systemTypeMap[typeof(T)] = _systems.Count - 1;
} }
public T GetSystem<T>() public T GetSystem<T>()
where T : ISystem where T : ISystem
{ {
if (_systemTypeMap.TryGetValue(typeof(T), out var index)) foreach (var system in _systems)
{ {
return (T)_systems[index]; if (system is T typedSystem)
{
return typedSystem;
}
} }
throw new InvalidOperationException($"System of type {typeof(T).FullName} not found in SystemManager."); throw new InvalidOperationException($"System of type {typeof(T).FullName} not found in SystemManager.");

View File

@@ -1,5 +1,6 @@
using Ghost.Core; using Ghost.Core;
using Misaki.HighPerformance.LowLevel; using Misaki.HighPerformance.LowLevel;
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections; using Misaki.HighPerformance.LowLevel.Collections;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@@ -10,7 +11,7 @@ public unsafe partial struct EntityQuery
public readonly ref struct ComponentIterator<T0> public readonly ref struct ComponentIterator<T0>
where T0 : unmanaged, IComponent where T0 : unmanaged, IComponent
{ {
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[1]; private fixed int _compTypeIDs[1];
private fixed int _offsets[1]; private fixed int _offsets[1];
@@ -19,6 +20,10 @@ public unsafe partial struct EntityQuery
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly int _currentVersion;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
@@ -39,6 +44,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(1, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 1; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -58,6 +79,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -124,6 +150,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
@@ -170,7 +201,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[2]; private fixed int _compTypeIDs[2];
private fixed int _offsets[2]; private fixed int _offsets[2];
@@ -179,6 +210,10 @@ public unsafe partial struct EntityQuery
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly int _currentVersion;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
@@ -203,6 +238,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(2, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 2; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -225,6 +276,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -291,6 +347,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
@@ -342,7 +403,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[3]; private fixed int _compTypeIDs[3];
private fixed int _offsets[3]; private fixed int _offsets[3];
@@ -351,6 +412,10 @@ public unsafe partial struct EntityQuery
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly int _currentVersion;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
@@ -379,6 +444,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(3, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 3; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -402,6 +483,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -468,6 +554,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
@@ -524,7 +615,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[4]; private fixed int _compTypeIDs[4];
private fixed int _offsets[4]; private fixed int _offsets[4];
@@ -533,6 +624,10 @@ public unsafe partial struct EntityQuery
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly int _currentVersion;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
@@ -565,6 +660,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(4, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 4; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -589,6 +700,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -655,6 +771,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
@@ -716,7 +837,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[5]; private fixed int _compTypeIDs[5];
private fixed int _offsets[5]; private fixed int _offsets[5];
@@ -725,6 +846,10 @@ public unsafe partial struct EntityQuery
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly int _currentVersion;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
@@ -761,6 +886,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(5, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 5; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -786,6 +927,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -852,6 +998,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
@@ -918,7 +1069,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[6]; private fixed int _compTypeIDs[6];
private fixed int _offsets[6]; private fixed int _offsets[6];
@@ -927,6 +1078,10 @@ public unsafe partial struct EntityQuery
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly int _currentVersion;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
@@ -967,6 +1122,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(6, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 6; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -993,6 +1164,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -1059,6 +1235,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
@@ -1130,7 +1311,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[7]; private fixed int _compTypeIDs[7];
private fixed int _offsets[7]; private fixed int _offsets[7];
@@ -1139,6 +1320,10 @@ public unsafe partial struct EntityQuery
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly int _currentVersion;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
@@ -1183,6 +1368,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(7, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 7; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -1210,6 +1411,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -1276,6 +1482,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
@@ -1352,7 +1563,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[8]; private fixed int _compTypeIDs[8];
private fixed int _offsets[8]; private fixed int _offsets[8];
@@ -1361,6 +1572,10 @@ public unsafe partial struct EntityQuery
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly int _currentVersion;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
@@ -1409,6 +1624,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(8, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 8; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -1437,6 +1668,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -1503,6 +1739,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;

View File

@@ -6,6 +6,7 @@
<#@ include file="Helpers.ttinclude" #> <#@ include file="Helpers.ttinclude" #>
using Ghost.Core; using Ghost.Core;
using Misaki.HighPerformance.LowLevel; using Misaki.HighPerformance.LowLevel;
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections; using Misaki.HighPerformance.LowLevel.Collections;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@@ -45,7 +46,7 @@ public unsafe partial struct EntityQuery
} }
<# } #> <# } #>
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[<#= i #>]; private fixed int _compTypeIDs[<#= i #>];
private fixed int _offsets[<#= i #>]; private fixed int _offsets[<#= i #>];
@@ -54,6 +55,10 @@ public unsafe partial struct EntityQuery
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly int _currentVersion;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
@@ -76,6 +81,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(<#= i #>, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < <#= i #>; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -103,6 +124,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -169,6 +195,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;

View File

@@ -1,5 +1,6 @@
using Ghost.Core; using Ghost.Core;
using Misaki.HighPerformance.LowLevel; using Misaki.HighPerformance.LowLevel;
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections; using Misaki.HighPerformance.LowLevel.Collections;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@@ -30,7 +31,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[1]; private fixed int _compTypeIDs[1];
private fixed int _offsets[1]; private fixed int _offsets[1];
@@ -40,6 +41,9 @@ public unsafe partial struct EntityQuery
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
private byte* _chunkBasePtr; private byte* _chunkBasePtr;
@@ -59,6 +63,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(1, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 1; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -81,6 +101,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -147,6 +172,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
@@ -199,7 +229,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[2]; private fixed int _compTypeIDs[2];
private fixed int _offsets[2]; private fixed int _offsets[2];
@@ -209,6 +239,9 @@ public unsafe partial struct EntityQuery
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
private byte* _chunkBasePtr; private byte* _chunkBasePtr;
@@ -232,6 +265,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(2, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 2; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -255,6 +304,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -321,6 +375,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
@@ -378,7 +437,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[3]; private fixed int _compTypeIDs[3];
private fixed int _offsets[3]; private fixed int _offsets[3];
@@ -388,6 +447,9 @@ public unsafe partial struct EntityQuery
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
private byte* _chunkBasePtr; private byte* _chunkBasePtr;
@@ -415,6 +477,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(3, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 3; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -439,6 +517,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -505,6 +588,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
@@ -567,7 +655,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[4]; private fixed int _compTypeIDs[4];
private fixed int _offsets[4]; private fixed int _offsets[4];
@@ -577,6 +665,9 @@ public unsafe partial struct EntityQuery
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
private byte* _chunkBasePtr; private byte* _chunkBasePtr;
@@ -608,6 +699,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(4, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 4; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -633,6 +740,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -699,6 +811,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
@@ -766,7 +883,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[5]; private fixed int _compTypeIDs[5];
private fixed int _offsets[5]; private fixed int _offsets[5];
@@ -776,6 +893,9 @@ public unsafe partial struct EntityQuery
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
private byte* _chunkBasePtr; private byte* _chunkBasePtr;
@@ -811,6 +931,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(5, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 5; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -837,6 +973,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -903,6 +1044,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
@@ -975,7 +1121,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[6]; private fixed int _compTypeIDs[6];
private fixed int _offsets[6]; private fixed int _offsets[6];
@@ -985,6 +1131,9 @@ public unsafe partial struct EntityQuery
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
private byte* _chunkBasePtr; private byte* _chunkBasePtr;
@@ -1024,6 +1173,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(6, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 6; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -1051,6 +1216,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -1117,6 +1287,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
@@ -1194,7 +1369,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[7]; private fixed int _compTypeIDs[7];
private fixed int _offsets[7]; private fixed int _offsets[7];
@@ -1204,6 +1379,9 @@ public unsafe partial struct EntityQuery
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
private byte* _chunkBasePtr; private byte* _chunkBasePtr;
@@ -1247,6 +1425,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(7, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 7; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -1275,6 +1469,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -1341,6 +1540,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;
@@ -1423,7 +1627,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[8]; private fixed int _compTypeIDs[8];
private fixed int _offsets[8]; private fixed int _offsets[8];
@@ -1433,6 +1637,9 @@ public unsafe partial struct EntityQuery
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
private byte* _chunkBasePtr; private byte* _chunkBasePtr;
@@ -1480,6 +1687,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(8, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < 8; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -1509,6 +1732,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -1575,6 +1803,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;

View File

@@ -6,6 +6,7 @@
<#@ include file="Helpers.ttinclude" #> <#@ include file="Helpers.ttinclude" #>
using Ghost.Core; using Ghost.Core;
using Misaki.HighPerformance.LowLevel; using Misaki.HighPerformance.LowLevel;
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections; using Misaki.HighPerformance.LowLevel.Collections;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@@ -49,7 +50,7 @@ public unsafe partial struct EntityQuery
} }
} }
public ref struct Enumerator public ref struct Enumerator : IDisposable
{ {
private fixed int _compTypeIDs[<#= i #>]; private fixed int _compTypeIDs[<#= i #>];
private fixed int _offsets[<#= i #>]; private fixed int _offsets[<#= i #>];
@@ -59,6 +60,9 @@ public unsafe partial struct EntityQuery
private readonly EntityQueryMask _mask; private readonly EntityQueryMask _mask;
private readonly World _world; private readonly World _world;
private readonly Stack.Scope _scope;
private UnsafeList<int> _changedComponentIDs;
private ref Archetype _currentArchetype; private ref Archetype _currentArchetype;
private ref Chunk _currentChunk; private ref Chunk _currentChunk;
private byte* _chunkBasePtr; private byte* _chunkBasePtr;
@@ -80,6 +84,22 @@ public unsafe partial struct EntityQuery
_mask = mask; _mask = mask;
_world = world; _world = world;
_scope = AllocationManager.CreateStackScope();
_changedComponentIDs = new UnsafeList<int>(<#= i #>, _scope.AllocationHandle);
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i = 0; i < <#= i #>; i++)
{
if (id == _compTypeIDs[i])
{
_changedComponentIDs.Add(id);
break;
}
}
}
Reset(); Reset();
} }
@@ -104,6 +124,11 @@ public unsafe partial struct EntityQuery
_offsets[index] = layout.offset; _offsets[index] = layout.offset;
_compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]);
} }
for (var i = 0; i < _changedComponentIDs.Count; i++)
{
_currentArchetype.MarkChanged(_currentChunkIndex, _changedComponentIDs[i], _world.Version);
}
} }
public bool MoveNext() public bool MoveNext()
@@ -170,6 +195,11 @@ public unsafe partial struct EntityQuery
} }
} }
} }
public readonly void Dispose()
{
_scope.Dispose();
}
} }
private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes; private readonly ReadOnlyUnsafeCollection<Identifier<Archetype>> _matchingArchetypes;

View File

@@ -1,6 +1,3 @@
using Ghost.Core;
namespace Ghost.Entities; namespace Ghost.Entities;
public unsafe partial struct EntityQuery public unsafe partial struct EntityQuery
@@ -31,7 +28,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -67,7 +63,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 1; index++) for (var index = 0; index < 1; index++)
@@ -119,7 +115,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -155,7 +150,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 2; index++) for (var index = 0; index < 2; index++)
@@ -211,7 +206,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -247,7 +241,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 3; index++) for (var index = 0; index < 3; index++)
@@ -307,7 +301,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -343,7 +336,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 4; index++) for (var index = 0; index < 4; index++)
@@ -407,7 +400,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -443,7 +435,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 5; index++) for (var index = 0; index < 5; index++)
@@ -511,7 +503,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -547,7 +538,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 6; index++) for (var index = 0; index < 6; index++)
@@ -619,7 +610,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -655,7 +645,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 7; index++) for (var index = 0; index < 7; index++)
@@ -731,7 +721,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -767,7 +756,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 8; index++) for (var index = 0; index < 8; index++)
@@ -823,7 +812,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -859,7 +847,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 1; index++) for (var index = 0; index < 1; index++)
@@ -912,7 +900,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -948,7 +935,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 2; index++) for (var index = 0; index < 2; index++)
@@ -1005,7 +992,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -1041,7 +1027,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 3; index++) for (var index = 0; index < 3; index++)
@@ -1102,7 +1088,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -1138,7 +1123,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 4; index++) for (var index = 0; index < 4; index++)
@@ -1203,7 +1188,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -1239,7 +1223,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 5; index++) for (var index = 0; index < 5; index++)
@@ -1308,7 +1292,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -1344,7 +1327,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 6; index++) for (var index = 0; index < 6; index++)
@@ -1417,7 +1400,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -1453,7 +1435,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 7; index++) for (var index = 0; index < 7; index++)
@@ -1530,7 +1512,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -1566,7 +1547,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < 8; index++) for (var index = 0; index < 8; index++)

View File

@@ -4,8 +4,6 @@
<#@ import namespace="System.Linq" #> <#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #> <#@ import namespace="System.Text" #>
<#@ include file="Helpers.ttinclude" #> <#@ include file="Helpers.ttinclude" #>
using Ghost.Core;
namespace Ghost.Entities; namespace Ghost.Entities;
public unsafe partial struct EntityQuery public unsafe partial struct EntityQuery
@@ -51,7 +49,6 @@ public unsafe partial struct EntityQuery
{ {
if (id == compTypeIDs[i]) if (id == compTypeIDs[i])
{ {
ComponentRegister.SetComponentLastWrite(id, globalVersion);
changedCompIDs[changedCompCount] = id; changedCompIDs[changedCompCount] = id;
changedCompCount++; changedCompCount++;
break; break;
@@ -87,7 +84,7 @@ public unsafe partial struct EntityQuery
for (var j = 0; j < changedCompCount; j++) for (var j = 0; j < changedCompCount; j++)
{ {
chunk.MarkChanged(changedCompIDs[i], globalVersion); archetype.MarkChanged(chunkIndex, changedCompIDs[j], globalVersion);
} }
for (var index = 0; index < <#= i #>; index++) for (var index = 0; index < <#= i #>; index++)

File diff suppressed because it is too large Load Diff

View File

@@ -6,7 +6,6 @@
<#@ include file="Helpers.ttinclude" #> <#@ include file="Helpers.ttinclude" #>
using Ghost.Core; using Ghost.Core;
using Misaki.HighPerformance.Jobs; using Misaki.HighPerformance.Jobs;
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections; using Misaki.HighPerformance.LowLevel.Collections;
namespace Ghost.Entities; namespace Ghost.Entities;
@@ -26,26 +25,35 @@ internal unsafe struct JobEntityBatch<TJob, <#= generics #>> : IJobParallelFor
where TJob : unmanaged, IJobEntity<<#= generics #>> where TJob : unmanaged, IJobEntity<<#= generics #>>
<#= restrictions #> <#= restrictions #>
{ {
public fixed int componentIDs[<#= i #>];
public fixed bool componentRW[<#= i #>];
public TJob userJob; public TJob userJob;
public UnsafeList<IntPtr> chunks; public UnsafeList<IntPtr> chunks;
public UnsafeList<IntPtr> chunkVersions;
public UnsafeList<int> chunkCount; public UnsafeList<int> chunkCount;
public UnsafeList<int> entityOffset; public UnsafeList<int> entityOffset;
<# for (var j = 0; j < i; j++){ #> <# for (var j = 0; j < i; j++){ #>
public UnsafeList<int> offsets<#= j #>; public UnsafeList<int> offsets<#= j #>;
public UnsafeList<int> bitsOffsets<#= j #>; public UnsafeList<int> bitsOffsets<#= j #>;
public UnsafeList<int> versionindices<#= j #>;
<# } #> <# } #>
public int version;
public void Execute(int loopIndex, int threadIndex) public void Execute(int loopIndex, int threadIndex)
{ {
// 1. Get the specific pChunk for this thread // 1. Get the specific pChunk for this thread
var pChunk = (byte*)chunks[loopIndex]; var pChunk = (byte*)chunks[loopIndex];
var pVersions = (int*)chunkVersions[loopIndex];
var count = chunkCount[loopIndex]; var count = chunkCount[loopIndex];
<# for (var j = 0; j < i; j++){ #> <# for (var j = 0; j < i; j++){ #>
var off<#= j #> = offsets<#= j #>[loopIndex]; var off<#= j #> = offsets<#= j #>[loopIndex];
var enableOff<#= j #> = bitsOffsets<#= j #>[loopIndex]; var enableOff<#= j #> = bitsOffsets<#= j #>[loopIndex];
var versionIndex<#= j #> = versionindices<#= j #>[loopIndex];
<# } #> <# } #>
var pEntity = (Entity*)(pChunk + entityOffset[loopIndex]); var pEntity = (Entity*)(pChunk + entityOffset[loopIndex]);
@@ -53,6 +61,15 @@ internal unsafe struct JobEntityBatch<TJob, <#= generics #>> : IJobParallelFor
var ptr<#= j #> = (<#= "T" + j #>*)(pChunk + off<#= j #>); var ptr<#= j #> = (<#= "T" + j #>*)(pChunk + off<#= j #>);
<# } #> <# } #>
// 2. Update versions for RW components
<# for (var j = 0; j < i; j++){ #>
if (componentRW[<#= j #>])
{
pVersions[versionIndex<#= j #>] = version;
}
<# } #>
// 3. Iterate all entities in this chunk
for (var i = 0; i < count; i++) for (var i = 0; i < count; i++)
{ {
<# for (var j = 0; j < i; j++){ #> <# for (var j = 0; j < i; j++){ #>
@@ -77,43 +94,49 @@ public unsafe partial struct EntityQuery
#> #>
private struct DisposeJobEntity<#= i #> : IJob private struct DisposeJobEntity<#= i #> : IJob
{ {
public UnsafeList<IntPtr> chunkList; public UnsafeList<IntPtr> chunks;
public UnsafeList<IntPtr> chunkVersions;
public UnsafeList<int> chunkEntityCounts; public UnsafeList<int> chunkEntityCounts;
public UnsafeList<int> entityOffsets; public UnsafeList<int> entityOffsets;
<# for (var j = 0; j < i; j++){ #> <# for (var j = 0; j < i; j++){ #>
public UnsafeList<int> offsets<#= j #>; public UnsafeList<int> offsets<#= j #>;
public UnsafeList<int> bitsOffsets<#= j #>; public UnsafeList<int> bitsOffsets<#= j #>;
public UnsafeList<int> versionindices<#= j #>;
<# } #> <# } #>
public void Execute(int threadIndex) public void Execute(int threadIndex)
{ {
chunkList.Dispose(); chunks.Dispose();
chunkVersions.Dispose();
chunkEntityCounts.Dispose(); chunkEntityCounts.Dispose();
entityOffsets.Dispose(); entityOffsets.Dispose();
<# for (var j = 0; j < i; j++){ #> <# for (var j = 0; j < i; j++){ #>
offsets<#= j #>.Dispose(); offsets<#= j #>.Dispose();
bitsOffsets<#= j #>.Dispose(); bitsOffsets<#= j #>.Dispose();
versionindices<#= j #>.Dispose();
<# } #> <# } #>
} }
} }
public JobHandle ScheduleEntityParallel<TJob, <#= generics #>>(TJob jobData, Allocator allocator, int batchSize, JobHandle dependency) public JobHandle ScheduleEntityParallel<TJob, <#= generics #>>(TJob jobData, int batchSize, JobHandle dependency)
where TJob : unmanaged, IJobEntity<<#= generics #>> where TJob : unmanaged, IJobEntity<<#= generics #>>
<#= restrictions #> <#= restrictions #>
{ {
var world = World.GetWorld(_worldID).GetValueOrThrow(); var world = World.GetWorld(_worldID).GetValueOrThrow();
// 1. Flatten the World // 1. Flatten the World
var chunkList = new UnsafeList<IntPtr>(128, allocator); var chunks = new UnsafeList<IntPtr>(128, JobScheduler.TempAllocatorHandle);
var chunkEntityCounts = new UnsafeList<int>(128, allocator); var chunkVersions = new UnsafeList<IntPtr>(128, JobScheduler.TempAllocatorHandle);
var entityOffsets = new UnsafeList<int>(128, allocator); var chunkEntityCounts = new UnsafeList<int>(128, JobScheduler.TempAllocatorHandle);
var entityOffsets = new UnsafeList<int>(128, JobScheduler.TempAllocatorHandle);
<# for (var j = 0; j < i; j++){ #> <# for (var j = 0; j < i; j++){ #>
var offsets<#= j #> = new UnsafeList<int>(128, allocator); var offsets<#= j #> = new UnsafeList<int>(128, JobScheduler.TempAllocatorHandle);
var bitsOffsets<#= j #> = new UnsafeList<int>(128, allocator); var bitsOffsets<#= j #> = new UnsafeList<int>(128, JobScheduler.TempAllocatorHandle);
var versionIndices<#= j #> = new UnsafeList<int>(128, JobScheduler.TempAllocatorHandle);
<# } #> <# } #>
// Iterate the Query's matching archetypes // Iterate the Query's matching archetypes
@@ -137,13 +160,15 @@ public unsafe partial struct EntityQuery
{ {
ref var chunkRef = ref arch.GetChunkReference(i); ref var chunkRef = ref arch.GetChunkReference(i);
chunkList.Add((IntPtr)chunkRef.GetUnsafePtr()); chunks.Add((IntPtr)chunkRef.GetUnsafePtr());
chunkVersions.Add((IntPtr)chunkRef.GetVersionUnsafePtr());
chunkEntityCounts.Add(chunkRef._count); chunkEntityCounts.Add(chunkRef._count);
entityOffsets.Add(arch.EntityIDsOffset); entityOffsets.Add(arch.EntityIDsOffset);
<# for (var j = 0; j < i; j++){ #> <# for (var j = 0; j < i; j++){ #>
offsets<#= j #>.Add(layout<#= j #>.offset); offsets<#= j #>.Add(layout<#= j #>.offset);
bitsOffsets<#= j #>.Add(layout<#= j #>.enableBitsOffset); bitsOffsets<#= j #>.Add(layout<#= j #>.enableBitsOffset);
versionIndices<#= j #>.Add(layout<#= j #>.versionIndex);
<# } #> <# } #>
} }
@@ -153,29 +178,49 @@ public unsafe partial struct EntityQuery
var runner = new JobEntityBatch<TJob, <#= generics #>> var runner = new JobEntityBatch<TJob, <#= generics #>>
{ {
userJob = jobData, userJob = jobData,
chunks = chunkList, chunks = chunks,
chunkVersions = chunkVersions,
chunkCount = chunkEntityCounts, chunkCount = chunkEntityCounts,
entityOffset = entityOffsets, entityOffset = entityOffsets,
<# for (var j = 0; j < i; j++){ #> <# for (var j = 0; j < i; j++){ #>
offsets<#= j #> = offsets<#= j #>, offsets<#= j #> = offsets<#= j #>,
bitsOffsets<#= j #> = bitsOffsets<#= j #>, bitsOffsets<#= j #> = bitsOffsets<#= j #>,
versionindices<#= j #> = versionIndices<#= j #>,
<# } #> <# } #>
version = world.Version,
}; };
var jobHandle = world.JobScheduler.ScheduleParallel(ref runner, chunkList.Count, batchSize, dependency); runner.componentIDs[0] = ComponentTypeID<T0>.value;
var it = _mask.writeAccess.GetIterator();
while (it.Next(out var id))
{
for (var i =0; i < 1; i++)
{
if (id == runner.componentIDs[i])
{
runner.componentRW[i] = true;
break;
}
}
}
var jobHandle = world.JobScheduler.ScheduleParallel(ref runner, chunks.Count, batchSize, dependency);
// 3. Dispose the temp lists // 3. Dispose the temp lists
var disposeJob = new DisposeJobEntity<#= i #> var disposeJob = new DisposeJobEntity<#= i #>
{ {
chunkList = chunkList, chunks = chunks,
chunkVersions = chunkVersions,
chunkEntityCounts = chunkEntityCounts, chunkEntityCounts = chunkEntityCounts,
entityOffsets = entityOffsets, entityOffsets = entityOffsets,
<# for (var j = 0; j < i; j++){ #> <# for (var j = 0; j < i; j++){ #>
offsets<#= j #> = offsets<#= j #>, offsets<#= j #> = offsets<#= j #>,
bitsOffsets<#= j #> = bitsOffsets<#= j #>, bitsOffsets<#= j #> = bitsOffsets<#= j #>,
versionindices<#= j #> = versionIndices<#= j #>,
<# } #> <# } #>
}; };

View File

@@ -50,7 +50,17 @@ public partial class World
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static World GetWorldUncheck(Identifier<World> id) internal static World GetWorldUncheck(Identifier<World> id)
{ {
#if DEBUG || GHOST_EDITOR
if (id.value < 0 || id.value >= s_worlds.Count)
{
throw new ArgumentOutOfRangeException(nameof(id), "World ID is out of range.");
}
var world = s_worlds[id.value];
return world is null ? throw new InvalidOperationException("World not found.") : world;
#else
return s_worlds[id.value]!; return s_worlds[id.value]!;
#endif
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -62,12 +72,7 @@ public partial class World
} }
var world = s_worlds[id.value]; var world = s_worlds[id.value];
if (world is null) return world is null ? ErrorStatus.NotFound : world;
{
return ErrorStatus.NotFound;
}
return world;
} }
} }
@@ -80,6 +85,8 @@ public partial class World : IIdentifierType, IDisposable, IEquatable<World>
private readonly EntityCommandBuffer _entityCommandBuffer; private readonly EntityCommandBuffer _entityCommandBuffer;
private readonly EntityCommandBuffer[] _threadLocalECBs; private readonly EntityCommandBuffer[] _threadLocalECBs;
private readonly SystemManager _systemManager;
private UnsafeList<Archetype> _archetypes; private UnsafeList<Archetype> _archetypes;
private UnsafeList<EntityQuery> _entityQueries; private UnsafeList<EntityQuery> _entityQueries;
@@ -106,6 +113,11 @@ public partial class World : IIdentifierType, IDisposable, IEquatable<World>
/// </summary> /// </summary>
public EntityManager EntityManager => _entityManager; public EntityManager EntityManager => _entityManager;
/// <summary>
/// Gets the system manager for this world.
/// </summary>
public SystemManager SystemManager => _systemManager;
/// <summary> /// <summary>
/// Gets the current version number of the world. /// Gets the current version number of the world.
/// </summary> /// </summary>
@@ -124,16 +136,18 @@ public partial class World : IIdentifierType, IDisposable, IEquatable<World>
_id = id; _id = id;
_jobScheduler = jobScheduler; _jobScheduler = jobScheduler;
_entityManager = new EntityManager(this, entityCapacity);
_entityCommandBuffer = new EntityCommandBuffer(_entityManager);
_threadLocalECBs = new EntityCommandBuffer[jobScheduler.WorkerCount];
_systemManager = new SystemManager(this);
_archetypes = new UnsafeList<Archetype>(16, Allocator.Persistent); _archetypes = new UnsafeList<Archetype>(16, Allocator.Persistent);
_entityQueries = new UnsafeList<EntityQuery>(16, Allocator.Persistent); _entityQueries = new UnsafeList<EntityQuery>(16, Allocator.Persistent);
_archetypeLookup = new UnsafeHashMap<int, Identifier<Archetype>>(16, Allocator.Persistent); _archetypeLookup = new UnsafeHashMap<int, Identifier<Archetype>>(16, Allocator.Persistent);
_querieLookup = new UnsafeHashMap<int, Identifier<EntityQuery>>(16, Allocator.Persistent); _querieLookup = new UnsafeHashMap<int, Identifier<EntityQuery>>(16, Allocator.Persistent);
_entityManager = new EntityManager(this, entityCapacity);
_entityCommandBuffer = new EntityCommandBuffer(_entityManager);
_threadLocalECBs = new EntityCommandBuffer[jobScheduler.WorkerCount];
for (var i = 0; i < jobScheduler.WorkerCount; i++) for (var i = 0; i < jobScheduler.WorkerCount; i++)
{ {
_threadLocalECBs[i] = new EntityCommandBuffer(_entityManager); _threadLocalECBs[i] = new EntityCommandBuffer(_entityManager);