initial archetype storage structure

rev2
cosmonaut 2023-10-19 15:41:49 -07:00
parent 4ef7cb4302
commit 40d2bf87ba
12 changed files with 371 additions and 0 deletions

22
src/Rev2/Archetype.cs Normal file
View File

@ -0,0 +1,22 @@
using System.Collections.Generic;
namespace MoonTools.ECS.Rev2
{
public class Archetype
{
public ArchetypeSignature Signature;
public ArchetypeId Id { get; private set; }
/* FIXME: make this native memory too */
public List<Column> Components = new List<Column>();
public Dictionary<ComponentId, ArchetypeEdge> Edges = new Dictionary<ComponentId, ArchetypeEdge>();
public int Count;
public Archetype(ArchetypeId id, ArchetypeSignature signature)
{
Id = id;
Signature = signature;
Count = 0;
}
}
}

View File

@ -0,0 +1,4 @@
namespace MoonTools.ECS.Rev2
{
public readonly record struct ArchetypeEdge(Archetype Add, Archetype Remove);
}

4
src/Rev2/ArchetypeId.cs Normal file
View File

@ -0,0 +1,4 @@
namespace MoonTools.ECS.Rev2
{
public readonly record struct ArchetypeId(int Id) : IHasId;
}

View File

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

View File

@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
namespace MoonTools.ECS.Rev2
{
public class ArchetypeSignature : IEquatable<ArchetypeSignature>
{
public static ArchetypeSignature Empty = new ArchetypeSignature(0);
List<int> Ids;
public int Count => Ids.Count;
public ComponentId this[int i] => new ComponentId(Ids[i]);
public ArchetypeSignature()
{
Ids = new List<int>();
}
public ArchetypeSignature(int capacity)
{
Ids = new List<int>(capacity);
}
// Maintains sorted order
public void Insert(ComponentId componentId)
{
var index = Ids.BinarySearch(componentId.Id);
if (index < 0)
{
Ids.Insert(~index, componentId.Id);
}
}
public void CopyTo(ArchetypeSignature other)
{
other.Ids.AddRange(Ids);
}
public override bool Equals(object? obj)
{
return obj is ArchetypeSignature signature && Equals(signature);
}
public bool Equals(ArchetypeSignature? other)
{
if (other == null)
{
return false;
}
if (Ids.Count != other.Ids.Count)
{
return false;
}
for (int i = 0; i < Ids.Count; i += 1)
{
if (Ids[i] != other.Ids[i])
{
return false;
}
}
return true;
}
public override int GetHashCode()
{
var hashcode = 1;
foreach (var id in Ids)
{
hashcode = HashCode.Combine(hashcode, id);
}
return hashcode;
}
}
}

63
src/Rev2/Column.cs Normal file
View File

@ -0,0 +1,63 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace MoonTools.ECS.Rev2
{
public unsafe class Column
{
public nint Elements;
public int ElementSize;
public int Count;
public int Capacity;
public static Column Create<T>() where T : unmanaged
{
return new Column(Unsafe.SizeOf<T>());
}
private Column(int elementSize)
{
Capacity = 16;
Count = 0;
ElementSize = elementSize;
Elements = (nint) NativeMemory.Alloc((nuint) (ElementSize * Capacity));
}
private void Resize()
{
Capacity *= 2;
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
}
public void Delete(int index)
{
if (Count > 1)
{
NativeMemory.Copy(
(void*) (Elements + ((Count - 1) * ElementSize)),
(void*) (Elements + (index * ElementSize)),
(nuint) ElementSize
);
}
Count -= 1;
}
public void CopyToEnd(int index, Column other)
{
if (other.Count >= other.Capacity)
{
other.Resize();
}
NativeMemory.Copy(
(void*) (Elements + (index * ElementSize)),
(void*) (other.Elements + (other.Count * ElementSize)),
(nuint) ElementSize
);
other.Count += 1;
}
}
}

4
src/Rev2/ComponentId.cs Normal file
View File

@ -0,0 +1,4 @@
namespace MoonTools.ECS.Rev2
{
public readonly record struct ComponentId(int Id) : IHasId;
}

4
src/Rev2/EntityId.cs Normal file
View File

@ -0,0 +1,4 @@
namespace MoonTools.ECS.Rev2
{
public readonly record struct EntityId(int Id) : IHasId;
}

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

@ -0,0 +1,6 @@
namespace MoonTools.ECS.Rev2;
public interface IHasId
{
public int Id { get; init; }
}

26
src/Rev2/IdAssigner.cs Normal file
View File

@ -0,0 +1,26 @@
using System.Collections.Generic;
namespace MoonTools.ECS.Rev2
{
public class IdAssigner<T> where T : struct, IHasId
{
int Next;
Queue<int> AvailableIds = new Queue<int>();
public T Assign()
{
if (!AvailableIds.TryDequeue(out var id))
{
id = Next;
Next += 1;
}
return new T { Id = id };
}
public void Unassign(T idHaver)
{
AvailableIds.Enqueue(idHaver.Id);
}
}
}

4
src/Rev2/Record.cs Normal file
View File

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

148
src/Rev2/World.cs Normal file
View File

@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
namespace MoonTools.ECS.Rev2
{
public class World
{
// Lookup from ArchetypeSignature to Archetype
Dictionary<ArchetypeSignature, Archetype> ArchetypeIndex = new Dictionary<ArchetypeSignature, Archetype>();
// Going from EntityId to Archetype and storage row
Dictionary<EntityId, Record> EntityIndex = new Dictionary<EntityId, Record>();
// Going from ComponentId to an Archetype storage column
Dictionary<ComponentId, Dictionary<ArchetypeId, ArchetypeRecord>> ComponentIndex = new Dictionary<ComponentId, Dictionary<ArchetypeId, ArchetypeRecord>>();
// Get ComponentId from a Type
Dictionary<Type, ComponentId> TypeToComponentId = new Dictionary<Type, ComponentId>();
// ID Management
IdAssigner<ArchetypeId> ArchetypeIdAssigner = new IdAssigner<ArchetypeId>();
IdAssigner<EntityId> EntityIdAssigner = new IdAssigner<EntityId>();
IdAssigner<ComponentId> ComponentIdAssigner = new IdAssigner<ComponentId>();
public World()
{
// Create the Empty Archetype
CreateArchetype(ArchetypeSignature.Empty);
}
private Archetype CreateArchetype(ArchetypeSignature signature)
{
var archetypeId = ArchetypeIdAssigner.Assign();
var archetype = new Archetype(archetypeId, signature);
ArchetypeIndex.Add(signature, archetype);
for (int i = 0; i < signature.Count; i += 1)
{
var componentId = signature[i];
ComponentIndex[componentId].Add(archetypeId, new ArchetypeRecord(i));
}
return archetype;
}
public EntityId CreateEntity()
{
var entityId = EntityIdAssigner.Assign();
var emptyArchetype = ArchetypeIndex[ArchetypeSignature.Empty];
EntityIndex.Add(entityId, new Record(emptyArchetype, 0));
return entityId;
}
// FIXME: would be much more efficient to do all this at load time somehow
private void RegisterComponentId(ComponentId componentId)
{
ComponentIndex.Add(componentId, new Dictionary<ArchetypeId, ArchetypeRecord>());
}
public ComponentId GetComponentId<T>() where T : unmanaged
{
if (!TypeToComponentId.TryGetValue(typeof(T), out var componentId))
{
componentId = ComponentIdAssigner.Assign();
}
return componentId;
}
public bool HasComponent<T>(EntityId entityId) where T : unmanaged
{
var componentId = GetComponentId<T>();
var record = EntityIndex[entityId];
var archetypes = ComponentIndex[componentId];
return archetypes.ContainsKey(record.Archetype.Id);
}
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 void AddComponent<T>(EntityId entityId, T component) where T : unmanaged
{
Archetype? nextArchetype;
var componentId = GetComponentId<T>();
var record = EntityIndex[entityId];
var archetype = record.Archetype;
if (archetype.Edges.TryGetValue(componentId, out var edge))
{
nextArchetype = edge.Add;
}
else
{
// FIXME: pool the signatures
var nextSignature = new ArchetypeSignature(archetype.Signature.Count + 1);
archetype.Signature.CopyTo(nextSignature);
nextSignature.Insert(componentId);
if (!ArchetypeIndex.TryGetValue(nextSignature, out nextArchetype))
{
nextArchetype = CreateArchetype(nextSignature);
}
var newEdge = new ArchetypeEdge(archetype, nextArchetype);
archetype.Edges.Add(componentId, newEdge);
nextArchetype.Edges.Add(componentId, newEdge);
}
MoveEntity(entityId, record.Row, archetype, nextArchetype);
}
private void MoveEntity(EntityId entityId, int row, Archetype from, Archetype to)
{
for (int i = 0; i < from.Components.Count; i += 1)
{
var componentId = from.Signature[i];
var destinationColumnIndex = ComponentIndex[componentId][to.Id].ColumnIndex;
from.Components[i].CopyToEnd(row, to.Components[destinationColumnIndex]);
from.Components[i].Delete(row);
}
EntityIndex[entityId] = new Record(to, to.Count);
to.Count += 1;
from.Count -= 1;
}
}
}