start rewriting component storage
parent
2adecf771a
commit
cbdcca5bbf
|
@ -7,7 +7,7 @@ namespace MoonTools.ECS.Collections;
|
||||||
public unsafe class NativeArray<T> : IDisposable where T : unmanaged
|
public unsafe class NativeArray<T> : IDisposable where T : unmanaged
|
||||||
{
|
{
|
||||||
private T* Elements;
|
private T* Elements;
|
||||||
public int Count { get; private set;}
|
public int Count { get; private set; }
|
||||||
private int Capacity;
|
private int Capacity;
|
||||||
private int ElementSize;
|
private int ElementSize;
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ public unsafe class NativeArray<T> : IDisposable where T : unmanaged
|
||||||
Count = 0;
|
Count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ResizeTo(int size)
|
public void ResizeTo(int size)
|
||||||
{
|
{
|
||||||
Capacity = size;
|
Capacity = size;
|
||||||
Elements = (T*) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
|
Elements = (T*) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
|
||||||
|
|
|
@ -7,8 +7,8 @@ namespace MoonTools.ECS.Collections;
|
||||||
internal unsafe class NativeArray : IDisposable
|
internal unsafe class NativeArray : IDisposable
|
||||||
{
|
{
|
||||||
private nint Elements;
|
private nint Elements;
|
||||||
public int Count { get; private set;}
|
public int Count { get; private set; }
|
||||||
private int Capacity;
|
public int Capacity { get; private set; }
|
||||||
public readonly int ElementSize;
|
public readonly int ElementSize;
|
||||||
|
|
||||||
private bool IsDisposed;
|
private bool IsDisposed;
|
||||||
|
@ -44,7 +44,7 @@ internal unsafe class NativeArray : IDisposable
|
||||||
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
|
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ResizeTo(int capacity)
|
public void ResizeTo(int capacity)
|
||||||
{
|
{
|
||||||
Capacity = capacity;
|
Capacity = capacity;
|
||||||
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
|
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
|
||||||
|
@ -65,6 +65,11 @@ internal unsafe class NativeArray : IDisposable
|
||||||
Count -= 1;
|
Count -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void RemoveLastElement()
|
||||||
|
{
|
||||||
|
Count -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
public void Append<T>(T component) where T : unmanaged
|
public void Append<T>(T component) where T : unmanaged
|
||||||
{
|
{
|
||||||
if (Count >= Capacity)
|
if (Count >= Capacity)
|
||||||
|
|
|
@ -1,90 +1,98 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using MoonTools.ECS.Collections;
|
using MoonTools.ECS.Collections;
|
||||||
|
|
||||||
namespace MoonTools.ECS;
|
namespace MoonTools.ECS;
|
||||||
|
|
||||||
internal class ComponentStorage : IDisposable
|
public class ComponentStorage : IDisposable
|
||||||
{
|
{
|
||||||
internal readonly Dictionary<Entity, int> EntityIDToStorageIndex = new Dictionary<Entity, int>(16);
|
internal readonly NativeArray<Entity> DenseArray = new NativeArray<Entity>();
|
||||||
internal readonly NativeArray Components;
|
internal readonly NativeArray<Entity> SparseArray = new NativeArray<Entity>();
|
||||||
internal readonly NativeArray<Entity> EntityIDs;
|
internal readonly NativeArray ElementArray;
|
||||||
internal readonly TypeId TypeId;
|
|
||||||
internal readonly int ElementSize;
|
|
||||||
|
|
||||||
private bool IsDisposed;
|
private bool IsDisposed;
|
||||||
|
|
||||||
public ComponentStorage(TypeId typeId, int elementSize)
|
public int ElementSize { get; private set; }
|
||||||
|
public int Count => DenseArray.Count;
|
||||||
|
|
||||||
|
public ComponentStorage(int elementSize)
|
||||||
{
|
{
|
||||||
|
for (var i = 0; i < 16; i += 1)
|
||||||
|
{
|
||||||
|
SparseArray.Append(Entity.Null); // sentinel value
|
||||||
|
}
|
||||||
|
|
||||||
|
ElementArray = new NativeArray(elementSize);
|
||||||
ElementSize = elementSize;
|
ElementSize = elementSize;
|
||||||
Components = new NativeArray(elementSize);
|
|
||||||
EntityIDs = new NativeArray<Entity>();
|
|
||||||
TypeId = typeId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Any()
|
public bool Any() => DenseArray.Count > 0;
|
||||||
{
|
|
||||||
return Components.Count > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Has(Entity entity)
|
public ref T Get<T>(Entity entity) where T : unmanaged
|
||||||
{
|
{
|
||||||
return EntityIDToStorageIndex.ContainsKey(entity);
|
if (entity.ID >= ElementArray.Capacity)
|
||||||
|
{
|
||||||
|
throw new Exception("oh noes");
|
||||||
}
|
}
|
||||||
|
return ref ElementArray.Get<T>(entity.ID);
|
||||||
public ref T Get<T>(in Entity entity) where T : unmanaged
|
|
||||||
{
|
|
||||||
return ref Components.Get<T>(EntityIDToStorageIndex[entity]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ref T GetFirst<T>() where T : unmanaged
|
public ref T GetFirst<T>() where T : unmanaged
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
if (Components.Count == 0)
|
if (DenseArray.Count == 0)
|
||||||
{
|
{
|
||||||
throw new IndexOutOfRangeException("Component storage is empty!");
|
throw new IndexOutOfRangeException("Component storage is empty!");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return ref Components.Get<T>(0);
|
return ref ElementArray.Get<T>(DenseArray[0].ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the entity had this component.
|
public bool Set<T>(Entity entity, T component) where T : unmanaged
|
||||||
public bool Set<T>(in Entity entity, in T component) where T : unmanaged
|
|
||||||
{
|
{
|
||||||
if (EntityIDToStorageIndex.TryGetValue(entity, out var index))
|
var newEntity = SparseArray[entity.ID] == Entity.Null;
|
||||||
|
if (newEntity)
|
||||||
{
|
{
|
||||||
Components.Set(index, component);
|
// the entity is being added! let's do some record keeping
|
||||||
return true;
|
var index = DenseArray.Count;
|
||||||
|
DenseArray.Append(entity);
|
||||||
|
if (entity.ID >= SparseArray.Count)
|
||||||
|
{
|
||||||
|
var oldCount = SparseArray.Count;
|
||||||
|
SparseArray.ResizeTo(entity.ID + 1);
|
||||||
|
for (var i = oldCount; i < SparseArray.Count; i += 1)
|
||||||
|
{
|
||||||
|
SparseArray.Append(Entity.Null); // sentinel value
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
|
SparseArray[entity.ID] = entity;
|
||||||
|
|
||||||
|
// FIXME: something is not right here
|
||||||
|
if (entity.ID >= ElementArray.Count)
|
||||||
{
|
{
|
||||||
EntityIDToStorageIndex[entity] = Components.Count;
|
ElementArray.ResizeTo(entity.ID + 1);
|
||||||
EntityIDs.Append(entity);
|
|
||||||
Components.Append(component);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the entity had this component.
|
ElementArray.Set(entity.ID, component);
|
||||||
public bool Remove(in Entity entity)
|
return !newEntity;
|
||||||
{
|
|
||||||
if (EntityIDToStorageIndex.TryGetValue(entity, out int index))
|
|
||||||
{
|
|
||||||
var lastElementIndex = Components.Count - 1;
|
|
||||||
|
|
||||||
var lastEntity = EntityIDs[lastElementIndex];
|
|
||||||
|
|
||||||
// move a component into the hole to maintain contiguous memory
|
|
||||||
Components.Delete(index);
|
|
||||||
EntityIDs.Delete(index);
|
|
||||||
EntityIDToStorageIndex.Remove(entity);
|
|
||||||
|
|
||||||
// update the index if it changed
|
|
||||||
if (lastElementIndex != index)
|
|
||||||
{
|
|
||||||
EntityIDToStorageIndex[lastEntity] = index;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool Has(Entity entity)
|
||||||
|
{
|
||||||
|
return SparseArray[entity.ID] != Entity.Null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Remove(Entity entity)
|
||||||
|
{
|
||||||
|
if (Has(entity))
|
||||||
|
{
|
||||||
|
var denseIndex = SparseArray[entity.ID];
|
||||||
|
var lastItem = DenseArray[DenseArray.Count - 1];
|
||||||
|
DenseArray[denseIndex.ID] = lastItem;
|
||||||
|
SparseArray[lastItem.ID] = denseIndex;
|
||||||
|
SparseArray[entity.ID] = Entity.Null; // sentinel value
|
||||||
|
DenseArray.RemoveLastElement();
|
||||||
|
ElementArray.RemoveLastElement();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,26 +101,29 @@ internal class ComponentStorage : IDisposable
|
||||||
|
|
||||||
public void Clear()
|
public void Clear()
|
||||||
{
|
{
|
||||||
Components.Clear();
|
DenseArray.Clear();
|
||||||
EntityIDs.Clear();
|
ElementArray.Clear();
|
||||||
EntityIDToStorageIndex.Clear();
|
for (var i = 0; i < SparseArray.Count; i += 1)
|
||||||
|
{
|
||||||
|
SparseArray[i] = Entity.Null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entity FirstEntity()
|
public Entity FirstEntity()
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
if (EntityIDs.Count == 0)
|
if (DenseArray.Count == 0)
|
||||||
{
|
{
|
||||||
throw new IndexOutOfRangeException("Component storage is empty!");
|
throw new IndexOutOfRangeException("Component storage is empty!");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return EntityIDs[0];
|
return DenseArray[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
internal IEnumerable<Entity> Debug_GetEntities()
|
internal Span<Entity> Debug_GetEntities()
|
||||||
{
|
{
|
||||||
return EntityIDToStorageIndex.Keys;
|
return DenseArray.ToSpan();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -120,19 +131,17 @@ internal class ComponentStorage : IDisposable
|
||||||
{
|
{
|
||||||
if (!IsDisposed)
|
if (!IsDisposed)
|
||||||
{
|
{
|
||||||
Components.Dispose();
|
if (disposing)
|
||||||
EntityIDs.Dispose();
|
{
|
||||||
|
DenseArray.Dispose();
|
||||||
|
SparseArray.Dispose();
|
||||||
|
ElementArray.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
IsDisposed = true;
|
IsDisposed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ~ComponentStorage()
|
|
||||||
// {
|
|
||||||
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
||||||
// Dispose(disposing: false);
|
|
||||||
// }
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||||
|
|
|
@ -11,7 +11,7 @@ public abstract class DebugSystem : System
|
||||||
protected DebugSystem(World world) : base(world) { }
|
protected DebugSystem(World world) : base(world) { }
|
||||||
|
|
||||||
protected World.ComponentTypeEnumerator Debug_GetAllComponentTypes(Entity entity) => World.Debug_GetAllComponentTypes(entity);
|
protected World.ComponentTypeEnumerator Debug_GetAllComponentTypes(Entity entity) => World.Debug_GetAllComponentTypes(entity);
|
||||||
protected IEnumerable<Entity> Debug_GetEntities(Type componentType) => World.Debug_GetEntities(componentType);
|
protected Span<Entity> Debug_GetEntities(Type componentType) => World.Debug_GetEntities(componentType);
|
||||||
protected IEnumerable<Type> Debug_SearchComponentType(string typeString) => World.Debug_SearchComponentType(typeString);
|
protected IEnumerable<Type> Debug_SearchComponentType(string typeString) => World.Debug_SearchComponentType(typeString);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
namespace MoonTools.ECS;
|
namespace MoonTools.ECS;
|
||||||
|
|
||||||
public readonly record struct Entity(uint ID);
|
public readonly record struct Entity(int ID)
|
||||||
|
{
|
||||||
|
public static readonly Entity Null = new Entity(int.MaxValue);
|
||||||
|
}
|
||||||
|
|
|
@ -5,12 +5,12 @@ namespace MoonTools.ECS;
|
||||||
|
|
||||||
internal class IdAssigner : IDisposable
|
internal class IdAssigner : IDisposable
|
||||||
{
|
{
|
||||||
uint Next;
|
int Next = 0;
|
||||||
NativeArray<uint> AvailableIds = new NativeArray<uint>();
|
NativeArray<int> AvailableIds = new NativeArray<int>();
|
||||||
|
|
||||||
private bool IsDisposed;
|
private bool IsDisposed;
|
||||||
|
|
||||||
public uint Assign()
|
public int Assign()
|
||||||
{
|
{
|
||||||
if (AvailableIds.TryPop(out var id))
|
if (AvailableIds.TryPop(out var id))
|
||||||
{
|
{
|
||||||
|
@ -22,7 +22,7 @@ internal class IdAssigner : IDisposable
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Unassign(uint id)
|
public void Unassign(int id)
|
||||||
{
|
{
|
||||||
AvailableIds.Append(id);
|
AvailableIds.Append(id);
|
||||||
}
|
}
|
||||||
|
@ -46,13 +46,6 @@ internal class IdAssigner : IDisposable
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
|
||||||
// ~IdAssigner()
|
|
||||||
// {
|
|
||||||
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
||||||
// Dispose(disposing: false);
|
|
||||||
// }
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||||
|
|
|
@ -206,34 +206,31 @@ public class Snapshot : IDisposable
|
||||||
|
|
||||||
private class ComponentSnapshot : IDisposable
|
private class ComponentSnapshot : IDisposable
|
||||||
{
|
{
|
||||||
private readonly NativeArray Components;
|
private readonly NativeArray<Entity> DenseArray = new NativeArray<Entity>();
|
||||||
private readonly NativeArray<Entity> EntityIDs;
|
private readonly NativeArray<Entity> SparseArray = new NativeArray<Entity>();
|
||||||
|
private readonly NativeArray ElementArray;
|
||||||
|
|
||||||
private bool IsDisposed;
|
private bool IsDisposed;
|
||||||
|
|
||||||
public ComponentSnapshot(int elementSize)
|
public ComponentSnapshot(int elementSize)
|
||||||
{
|
{
|
||||||
Components = new NativeArray(elementSize);
|
ElementArray = new NativeArray(elementSize);
|
||||||
EntityIDs = new NativeArray<Entity>();
|
DenseArray = new NativeArray<Entity>(elementSize);
|
||||||
|
SparseArray = new NativeArray<Entity>(elementSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Take(ComponentStorage componentStorage)
|
public void Take(ComponentStorage componentStorage)
|
||||||
{
|
{
|
||||||
componentStorage.Components.CopyAllTo(Components);
|
componentStorage.DenseArray.CopyTo(DenseArray);
|
||||||
componentStorage.EntityIDs.CopyTo(EntityIDs);
|
componentStorage.SparseArray.CopyTo(SparseArray);
|
||||||
|
componentStorage.ElementArray.CopyAllTo(ElementArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Restore(ComponentStorage componentStorage)
|
public void Restore(ComponentStorage componentStorage)
|
||||||
{
|
{
|
||||||
Components.CopyAllTo(componentStorage.Components);
|
DenseArray.CopyTo(componentStorage.DenseArray);
|
||||||
EntityIDs.CopyTo(componentStorage.EntityIDs);
|
SparseArray.CopyTo(componentStorage.SparseArray);
|
||||||
|
ElementArray.CopyAllTo(componentStorage.ElementArray);
|
||||||
componentStorage.EntityIDToStorageIndex.Clear();
|
|
||||||
for (int i = 0; i < EntityIDs.Count; i += 1)
|
|
||||||
{
|
|
||||||
var entityID = EntityIDs[i];
|
|
||||||
componentStorage.EntityIDToStorageIndex[entityID] = i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
protected virtual void Dispose(bool disposing)
|
||||||
|
@ -242,8 +239,9 @@ public class Snapshot : IDisposable
|
||||||
{
|
{
|
||||||
if (disposing)
|
if (disposing)
|
||||||
{
|
{
|
||||||
Components.Dispose();
|
DenseArray.Dispose();
|
||||||
EntityIDs.Dispose();
|
SparseArray.Dispose();
|
||||||
|
ElementArray.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
IsDisposed = true;
|
IsDisposed = true;
|
||||||
|
|
18
src/World.cs
18
src/World.cs
|
@ -51,8 +51,7 @@ public class World : IDisposable
|
||||||
// add missing storages, it's possible for there to be multiples in multi-world scenarios
|
// add missing storages, it's possible for there to be multiples in multi-world scenarios
|
||||||
for (var i = ComponentIndex.Count; i <= typeId; i += 1)
|
for (var i = ComponentIndex.Count; i <= typeId; i += 1)
|
||||||
{
|
{
|
||||||
var missingTypeId = new TypeId((uint) i);
|
var componentStorage = new ComponentStorage(ComponentTypeElementSizes[i]);
|
||||||
var componentStorage = new ComponentStorage(missingTypeId, ComponentTypeElementSizes[i]);
|
|
||||||
ComponentIndex.Add(componentStorage);
|
ComponentIndex.Add(componentStorage);
|
||||||
ComponentTypeToFilter.Add(new List<Filter>());
|
ComponentTypeToFilter.Add(new List<Filter>());
|
||||||
}
|
}
|
||||||
|
@ -149,8 +148,7 @@ public class World : IDisposable
|
||||||
|
|
||||||
public bool Has<T>(in Entity entity) where T : unmanaged
|
public bool Has<T>(in Entity entity) where T : unmanaged
|
||||||
{
|
{
|
||||||
var storage = GetComponentStorage<T>();
|
return EntityComponentIndex[entity].Contains(new TypeId(ComponentTypeIdAssigner<T>.Id));
|
||||||
return storage.Has(entity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool Has(in Entity entity, in TypeId typeId)
|
internal bool Has(in Entity entity, in TypeId typeId)
|
||||||
|
@ -185,12 +183,13 @@ public class World : IDisposable
|
||||||
public void Set<T>(in Entity entity, in T component) where T : unmanaged
|
public void Set<T>(in Entity entity, in T component) where T : unmanaged
|
||||||
{
|
{
|
||||||
var componentStorage = GetComponentStorage<T>();
|
var componentStorage = GetComponentStorage<T>();
|
||||||
|
var typeId = new TypeId(ComponentTypeIdAssigner<T>.Id);
|
||||||
|
|
||||||
if (!componentStorage.Set(entity, component))
|
if (!componentStorage.Set(entity, component))
|
||||||
{
|
{
|
||||||
EntityComponentIndex[entity].Add(componentStorage.TypeId);
|
EntityComponentIndex[entity].Add(typeId);
|
||||||
|
|
||||||
foreach (var filter in ComponentTypeToFilter[componentStorage.TypeId])
|
foreach (var filter in ComponentTypeToFilter[typeId])
|
||||||
{
|
{
|
||||||
filter.Check(entity);
|
filter.Check(entity);
|
||||||
}
|
}
|
||||||
|
@ -200,12 +199,13 @@ public class World : IDisposable
|
||||||
public void Remove<T>(in Entity entity) where T : unmanaged
|
public void Remove<T>(in Entity entity) where T : unmanaged
|
||||||
{
|
{
|
||||||
var componentStorage = GetComponentStorage<T>();
|
var componentStorage = GetComponentStorage<T>();
|
||||||
|
var typeId = new TypeId(ComponentTypeIdAssigner<T>.Id);
|
||||||
|
|
||||||
if (componentStorage.Remove(entity))
|
if (componentStorage.Remove(entity))
|
||||||
{
|
{
|
||||||
EntityComponentIndex[entity].Remove(componentStorage.TypeId);
|
EntityComponentIndex[entity].Remove(typeId);
|
||||||
|
|
||||||
foreach (var filter in ComponentTypeToFilter[componentStorage.TypeId])
|
foreach (var filter in ComponentTypeToFilter[typeId])
|
||||||
{
|
{
|
||||||
filter.Check(entity);
|
filter.Check(entity);
|
||||||
}
|
}
|
||||||
|
@ -391,7 +391,7 @@ public class World : IDisposable
|
||||||
return new ComponentTypeEnumerator(this, EntityComponentIndex[entity]);
|
return new ComponentTypeEnumerator(this, EntityComponentIndex[entity]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Entity> Debug_GetEntities(Type componentType)
|
public Span<Entity> Debug_GetEntities(Type componentType)
|
||||||
{
|
{
|
||||||
var storage = ComponentIndex[ComponentTypeToId[componentType]];
|
var storage = ComponentIndex[ComponentTypeToId[componentType]];
|
||||||
return storage.Debug_GetEntities();
|
return storage.Debug_GetEntities();
|
||||||
|
|
Loading…
Reference in New Issue