more rev2 implementation

rev2
cosmonaut 2023-10-20 17:24:35 -07:00
parent 5a670fa36c
commit 272fd6b492
6 changed files with 192 additions and 72 deletions

View File

@ -8,6 +8,9 @@ namespace MoonTools.ECS.Rev2
public ArchetypeId Id { get; private set; }
public List<Column> Components = new List<Column>();
public List<EntityId> RowToEntity = new List<EntityId>();
public Dictionary<ComponentId, int> ComponentToColumnIndex =
new Dictionary<ComponentId, int>();
public SortedDictionary<ComponentId, ArchetypeEdge> Edges = new SortedDictionary<ComponentId, ArchetypeEdge>();
public int Count;

View File

@ -1,4 +0,0 @@
namespace MoonTools.ECS.Rev2
{
public readonly record struct ArchetypeRecord(int ColumnIndex);
}

View File

@ -27,6 +27,7 @@ namespace MoonTools.ECS.Rev2
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
}
// Fills gap by copying final element to the deleted index
public void Delete(int index)
{
if (Count > 1)

76
src/Rev2/Filter.cs Normal file
View File

@ -0,0 +1,76 @@
using System;
using System.Runtime.InteropServices;
namespace MoonTools.ECS.Rev2
{
// TODO: do we want to get fancy with queries beyond Include and Exclude?
// TODO: need an edge iterator as part of this nested horseshit
public class Filter
{
private Archetype Start;
public EntityEnumerator Entities => new EntityEnumerator(Start);
private ref struct FilterEnumerator
{
private Archetype CurrentArchetype;
private bool Active;
public FilterEnumerator(Archetype start)
{
CurrentArchetype = start;
Active = false;
}
public bool MoveNext()
{
if (!Active)
{
Active = true;
return true;
}
// TODO: go to next available edge
}
public Archetype Current => CurrentArchetype;
}
public ref struct EntityEnumerator
{
private FilterEnumerator FilterEnumerator;
private ReverseSpanEnumerator<EntityId> EntityListEnumerator;
private bool EntityListEnumeratorActive;
public EntityEnumerator GetEnumerator() => this;
public EntityEnumerator(Archetype start)
{
FilterEnumerator = new FilterEnumerator(start);
}
public bool MoveNext()
{
if (!EntityListEnumeratorActive || !EntityListEnumerator.MoveNext())
{
if (!FilterEnumerator.MoveNext())
{
return false;
}
if (FilterEnumerator.Current.RowToEntity.Count != 0)
{
EntityListEnumerator = new ReverseSpanEnumerator<EntityId>(CollectionsMarshal.AsSpan(FilterEnumerator.Current.RowToEntity));
EntityListEnumeratorActive = true;
}
return MoveNext();
}
return true;
}
public EntityId Current => EntityListEnumerator.Current;
}
}
}

6
src/Rev2/IForEach.cs Normal file
View File

@ -0,0 +1,6 @@
namespace MoonTools.ECS.Rev2;
public interface IForEach<T1, T2> where T1 : unmanaged where T2 : unmanaged
{
public void Update(ref T1 t1, ref T2 t2);
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace MoonTools.ECS.Rev2
{
@ -12,8 +13,8 @@ namespace MoonTools.ECS.Rev2
// Going from EntityId to Archetype and storage row
Dictionary<EntityId, Record> EntityIndex = new Dictionary<EntityId, Record>();
// Going from ComponentId to an Archetype storage column index
Dictionary<ComponentId, Dictionary<ArchetypeId, ArchetypeRecord>> ComponentIndex = new Dictionary<ComponentId, Dictionary<ArchetypeId, ArchetypeRecord>>();
// Going from ComponentId to Archetype list
Dictionary<ComponentId, List<Archetype>> ComponentIndex = new Dictionary<ComponentId, List<Archetype>>();
// Get ComponentId from a Type
Dictionary<Type, ComponentId> TypeToComponentId = new Dictionary<Type, ComponentId>();
@ -49,7 +50,8 @@ namespace MoonTools.ECS.Rev2
for (int i = 0; i < signature.Count; i += 1)
{
var componentId = signature[i];
ComponentIndex[componentId].Add(archetypeId, new ArchetypeRecord(i));
ComponentIndex[componentId].Add(archetype); //, new ArchetypeRecord(i));
archetype.ComponentToColumnIndex.Add(componentId, archetype.Components.Count);
archetype.Components.Add(new Column(ElementSizes[componentId]));
}
@ -70,7 +72,7 @@ namespace MoonTools.ECS.Rev2
{
var componentId = ComponentIdAssigner.Assign();
TypeToComponentId.Add(typeof(T), componentId);
ComponentIndex.Add(componentId, new Dictionary<ArchetypeId, ArchetypeRecord>());
ComponentIndex.Add(componentId, new List<Archetype>());
ElementSizes.Add(componentId, Unsafe.SizeOf<T>());
}
@ -82,67 +84,51 @@ namespace MoonTools.ECS.Rev2
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ComponentId GetComponentId<T>() where T : unmanaged
{
return TypeToComponentId[typeof(T)];
}
internal ArchetypeRecord GetArchetypeRecord<T>(Archetype archetype) where T : unmanaged
public bool Has<T>(EntityId entityId) where T : unmanaged
{
var componentId = GetComponentId<T>();
return ComponentIndex[componentId][archetype.Id];
var record = EntityIndex[entityId];
return record.Archetype.ComponentToColumnIndex.ContainsKey(componentId);
}
public bool HasComponent<T>(EntityId entityId) where T : unmanaged
// will throw if non-existent
public unsafe ref T Get<T>(EntityId entityId) where T : unmanaged
{
var componentId = GetComponentId<T>();
var record = EntityIndex[entityId];
var archetypes = ComponentIndex[componentId];
return archetypes.ContainsKey(record.Archetype.Id);
var columnIndex = record.Archetype.ComponentToColumnIndex[componentId];
var column = record.Archetype.Components[columnIndex];
return ref ((T*) column.Elements)[record.Row];
}
public unsafe T GetComponent<T>(EntityId entityId) where T : unmanaged
{
var componentId = GetComponentId<T>();
var record = EntityIndex[entityId];
var archetype = record.Archetype;
var archetypes = ComponentIndex[componentId];
if (!archetypes.ContainsKey(archetype.Id))
{
return default; // FIXME: maybe throw in debug mode?
}
var archetypeRecord = archetypes[archetype.Id];
var column = archetype.Components[archetypeRecord.ColumnIndex];
return ((T*) column.Elements)[record.Row];
}
public unsafe void SetComponent<T>(EntityId entityId, T component) where T : unmanaged
public unsafe void Set<T>(in EntityId entityId, in T component) where T : unmanaged
{
TryRegisterComponentId<T>();
var componentId = GetComponentId<T>();
if (HasComponent<T>(entityId))
if (Has<T>(entityId))
{
var record = EntityIndex[entityId];
var archetype = record.Archetype;
var archetypes = ComponentIndex[componentId];
var archetypeRecord = archetypes[archetype.Id];
var column = archetype.Components[archetypeRecord.ColumnIndex];
var columnIndex = record.Archetype.ComponentToColumnIndex[componentId];
var column = record.Archetype.Components[columnIndex];
((T*) column.Elements)[record.Row] = component;
}
else
{
AddComponent(entityId, component);
Add(entityId, component);
}
}
private void AddComponent<T>(EntityId entityId, T component) where T : unmanaged
private void Add<T>(EntityId entityId, in T component) where T : unmanaged
{
Archetype? nextArchetype;
@ -176,20 +162,18 @@ namespace MoonTools.ECS.Rev2
MoveEntityToHigherArchetype(entityId, record.Row, archetype, nextArchetype);
// add the new component to the new archetype
var archetypes = ComponentIndex[componentId];
var archetypeRecord = archetypes[nextArchetype.Id];
var column = nextArchetype.Components[archetypeRecord.ColumnIndex];
var columnIndex = nextArchetype.ComponentToColumnIndex[componentId];
var column = nextArchetype.Components[columnIndex];
column.Append(component);
}
public void RemoveComponent<T>(EntityId entityId) where T : unmanaged
public void Remove<T>(EntityId entityId) where T : unmanaged
{
Archetype? nextArchetype;
var componentId = GetComponentId<T>();
var record = EntityIndex[entityId];
var archetype = record.Archetype;
var (archetype, row) = EntityIndex[entityId];
if (archetype.Edges.TryGetValue(componentId, out var edge))
{
@ -212,7 +196,30 @@ namespace MoonTools.ECS.Rev2
nextArchetype.Edges.Add(componentId, newEdge);
}
MoveEntityToLowerArchetype(entityId, record.Row, archetype, nextArchetype, componentId);
MoveEntityToLowerArchetype(entityId, row, archetype, nextArchetype, componentId);
}
public void Destroy(EntityId entityId)
{
var record = EntityIndex[entityId];
var archetype = record.Archetype;
var row = record.Row;
for (int i = 0; i < archetype.Components.Count; i += 1)
{
archetype.Components[i].Delete(row);
}
if (archetype.Count > 1)
{
// update row to entity lookup on archetype
archetype.RowToEntity[row] = archetype.RowToEntity[archetype.Count - 1];
archetype.RowToEntity.RemoveAt(archetype.Count - 1);
}
archetype.Count -= 1;
EntityIndex.Remove(entityId);
EntityIdAssigner.Unassign(entityId);
}
private void MoveEntityToHigherArchetype(EntityId entityId, int row, Archetype from, Archetype to)
@ -220,13 +227,14 @@ namespace MoonTools.ECS.Rev2
for (int i = 0; i < from.Components.Count; i += 1)
{
var componentId = from.Signature[i];
var destinationColumnIndex = ComponentIndex[componentId][to.Id].ColumnIndex;
var destinationColumnIndex = to.ComponentToColumnIndex[componentId];
// copy all components to higher archetype
from.Components[i].CopyToEnd(row, to.Components[destinationColumnIndex]);
// delete row on from archetype
from.Components[i].Delete(row);
}
if (from.Count > 1)
{
@ -235,7 +243,6 @@ namespace MoonTools.ECS.Rev2
from.RowToEntity.RemoveAt(from.Count - 1);
EntityIndex[from.RowToEntity[row]] = new Record(from, row);
}
}
// update row to entity lookup on to archetype
EntityIndex[entityId] = new Record(to, to.Count);
@ -257,18 +264,18 @@ namespace MoonTools.ECS.Rev2
// if this isn't the removed component, copy to the lower archetype
if (componentId != removed)
{
var destinationColumnIndex = ComponentIndex[componentId][to.Id].ColumnIndex;
var destinationColumnIndex = to.ComponentToColumnIndex[componentId];
from.Components[i].CopyToEnd(row, to.Components[destinationColumnIndex]);
}
}
if (from.Count > 0)
if (from.Count > 1)
{
// update row to entity lookup on from archetype
from.RowToEntity[row] = from.RowToEntity[from.Count - 1];
from.RowToEntity.RemoveAt(from.Count - 1);
EntityIndex[from.RowToEntity[row]] = new Record(from, row);
}
}
}
// update row to entity lookup on to archetype
EntityIndex[entityId] = new Record(to, to.Count);
@ -278,19 +285,46 @@ namespace MoonTools.ECS.Rev2
from.Count -= 1;
}
public unsafe void ForEachEntity<T, T1, T2>(ArchetypeSignature signature,
T rowForEachContainer) where T : IForEach<T1, T2> where T1 : unmanaged where T2 : unmanaged
{
var archetype = ArchetypeIndex[signature];
var componentIdOne = signature[0];
var columnIndexOne = archetype.ComponentToColumnIndex[componentIdOne];
var columnOneElements = archetype.Components[columnIndexOne].Elements;
var componentIdTwo = signature[1];
var columnIndexTwo = archetype.ComponentToColumnIndex[componentIdTwo];
var columnTwoElements = archetype.Components[columnIndexTwo].Elements;
for (int i = archetype.Count - 1; i >= 0; i -= 1)
{
rowForEachContainer.Update(ref ((T1*) columnOneElements)[i], ref ((T2*) columnTwoElements)[i]);
}
foreach (var edge in archetype.Edges.Values)
{
if (edge.Add != archetype)
{
ForEachEntity<T, T1, T2>(edge.Add.Signature, rowForEachContainer);
}
}
}
public unsafe void ForEachEntity<T1, T2>(ArchetypeSignature signature, RefAction<T1, T2> rowAction) where T1 : unmanaged where T2 : unmanaged
{
var archetype = ArchetypeIndex[signature];
var componentIdOne = signature[0];
var columnIndexOne = ComponentIndex[componentIdOne][archetype.Id].ColumnIndex;
var columnIndexOne = archetype.ComponentToColumnIndex[componentIdOne];
var columnOneElements = archetype.Components[columnIndexOne].Elements;
var componentIdTwo = signature[1];
var columnIndexTwo = ComponentIndex[componentIdTwo][archetype.Id].ColumnIndex;
var columnIndexTwo = archetype.ComponentToColumnIndex[componentIdTwo];
var columnTwoElements = archetype.Components[columnIndexTwo].Elements;
for (int i = 0; i < archetype.Count; i += 1)
for (int i = archetype.Count - 1; i >= 0; i -= 1)
{
rowAction(ref ((T1*) columnOneElements)[i], ref ((T2*) columnTwoElements)[i]);
}
@ -304,15 +338,13 @@ namespace MoonTools.ECS.Rev2
}
}
/*
public void ForEachEntity(ArchetypeSignature signature, Action<Entity> rowAction)
public void ForEachEntity(ArchetypeSignature signature, Action<EntityId> rowAction)
{
var archetype = ArchetypeIndex[signature];
for (int i = 0; i < archetype.Count; i += 1)
{
var entity = new Entity(this, archetype, i, archetype.RowToEntity[i]);
rowAction(entity);
rowAction(archetype.RowToEntity[i]);
}
// recursion might get too hairy here
@ -324,7 +356,13 @@ namespace MoonTools.ECS.Rev2
}
}
}
*/
public ReverseSpanEnumerator<EntityId> Entities(ArchetypeSignature signature)
{
var archetype = ArchetypeIndex[signature];
return new ReverseSpanEnumerator<EntityId>(
CollectionsMarshal.AsSpan(archetype.RowToEntity));
}
protected virtual void Dispose(bool disposing)
{