forked from Misaki/GhostEngine
Update EntityManager and Archetype
This commit is contained in:
@@ -43,18 +43,19 @@ internal unsafe struct Chuck : IDisposable
|
||||
|
||||
internal struct Edge
|
||||
{
|
||||
public int componentID;
|
||||
public Identifier<IComponent> componentID;
|
||||
public Identifier<Archetype> targetArchetype;
|
||||
}
|
||||
|
||||
internal struct ComponentMemoryLayout
|
||||
{
|
||||
public int offset;
|
||||
public int size;
|
||||
public Identifier<IComponent> componentID;
|
||||
}
|
||||
|
||||
internal unsafe struct Archetype : IIdentifierType, IDisposable
|
||||
{
|
||||
private struct ComponentMemoryLayout
|
||||
{
|
||||
public int offset;
|
||||
public int size;
|
||||
}
|
||||
|
||||
private readonly Identifier<Archetype> _id;
|
||||
private readonly Identifier<World> _worldID;
|
||||
|
||||
@@ -63,18 +64,25 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
|
||||
private UnsafeArray<ComponentMemoryLayout> _layouts;
|
||||
private UnsafeArray<int> _componentIDToOffset;
|
||||
|
||||
// TODO: Is hash map better?
|
||||
private UnsafeList<Edge> _edgesAdd;
|
||||
private UnsafeList<Edge> _edgesRemove;
|
||||
|
||||
private int _hash;
|
||||
private int _entityCapacity;
|
||||
private int _maxComponentID;
|
||||
private int _entityIdsOffset;
|
||||
|
||||
public Identifier<Archetype> ID => _id;
|
||||
|
||||
public UnsafeBitSet Signature => _signature;
|
||||
public UnsafeList<Chuck> Chunks => _chunks;
|
||||
public UnsafeArray<ComponentMemoryLayout> Layouts => _layouts;
|
||||
|
||||
public int EntityCapacity => _entityCapacity;
|
||||
public int ChunkCount => _chunks.Count;
|
||||
|
||||
public Archetype(Identifier<Archetype> id, Identifier<World> worldID, ReadOnlySpan<int> componentIds)
|
||||
public Archetype(Identifier<Archetype> id, Identifier<World> worldID, ReadOnlySpan<Identifier<IComponent>> componentIds)
|
||||
{
|
||||
_id = id;
|
||||
_worldID = worldID;
|
||||
@@ -109,11 +117,13 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
|
||||
_edgesAdd = new UnsafeList<Edge>(4, Allocator.Persistent);
|
||||
_edgesRemove = new UnsafeList<Edge>(4, Allocator.Persistent);
|
||||
|
||||
_hash = _signature.GetHashCode();
|
||||
|
||||
var pComponents = stackalloc ComponentInfo[componentIds.Length];
|
||||
for (var i = 0; i < componentIds.Length; i++)
|
||||
{
|
||||
_signature.SetBit(componentIds[i]);
|
||||
pComponents[i] = ComponentRegister.s_registeredComponents[componentIds[i]];
|
||||
pComponents[i] = ComponentRegister.GetComponentInfo(componentIds[i]);
|
||||
}
|
||||
|
||||
CalculateLayout(new Span<ComponentInfo>(pComponents, componentIds.Length));
|
||||
@@ -129,10 +139,11 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
|
||||
var maxComponentID = 0;
|
||||
for (var i = 0; i < components.Length; i++)
|
||||
{
|
||||
bytesPerEntity += components[i].size;
|
||||
if (components[i].id > maxComponentID)
|
||||
var comp = components[i];
|
||||
bytesPerEntity += comp.size;
|
||||
if (comp.id > maxComponentID)
|
||||
{
|
||||
maxComponentID = components[i].id;
|
||||
maxComponentID = comp.id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,6 +155,7 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
|
||||
_componentIDToOffset.AsSpan().Fill(-1);
|
||||
|
||||
components.Sort((a, b) => b.alignment.CompareTo(a.alignment));
|
||||
var tempOffsets = stackalloc int[components.Length];
|
||||
|
||||
while (_entityCapacity > 0)
|
||||
{
|
||||
@@ -155,8 +167,6 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
|
||||
_entityIdsOffset = currentOffset;
|
||||
currentOffset += _entityCapacity * entitySize;
|
||||
|
||||
var tempOffsets = stackalloc int[components.Length];
|
||||
|
||||
for (var i = 0; i < components.Length; i++)
|
||||
{
|
||||
var size = components[i].size;
|
||||
@@ -180,7 +190,8 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
|
||||
_layouts[i] = new ComponentMemoryLayout
|
||||
{
|
||||
offset = tempOffsets[i],
|
||||
size = components[i].size
|
||||
size = components[i].size,
|
||||
componentID = components[i].id
|
||||
};
|
||||
|
||||
_componentIDToOffset[components[i].id] = tempOffsets[i];
|
||||
@@ -227,6 +238,19 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
|
||||
MemoryUtility.MemCpy(&entity, pEntity, (nuint)sizeof(Entity));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetComponentData(int chunkIndex, int rowIndex, Identifier<IComponent> componentID, void* pComponent)
|
||||
{
|
||||
var offset = _componentIDToOffset[componentID];
|
||||
var chunk = _chunks[chunkIndex];
|
||||
|
||||
var chunkBase = chunk.GetUnsafePtr();
|
||||
var size = ComponentRegister.GetComponentInfo(componentID).size;
|
||||
var dst = chunkBase + offset + (size * rowIndex);
|
||||
|
||||
MemoryUtility.MemCpy(pComponent, dst, (nuint)size);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ref Chuck GetChunkReference(int index)
|
||||
{
|
||||
@@ -292,7 +316,13 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AddEdgeAdd(int componentID, Identifier<Archetype> targetArchetype)
|
||||
public bool HasComponent(Identifier<IComponent> componentID)
|
||||
{
|
||||
return _signature.IsSet(componentID);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AddEdgeAdd(Identifier<IComponent> componentID, Identifier<Archetype> targetArchetype)
|
||||
{
|
||||
_edgesAdd.Add(new Edge
|
||||
{
|
||||
@@ -302,21 +332,22 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Result<int, ResultStatus> GetEdgeAdd(int componentID)
|
||||
public Identifier<Archetype> GetEdgeAdd(Identifier<IComponent> componentID)
|
||||
{
|
||||
for (var i = 0; i < _edgesAdd.Count; i++)
|
||||
{
|
||||
if (_edgesAdd[i].componentID == componentID)
|
||||
var edge = _edgesAdd[i];
|
||||
if (edge.componentID == componentID)
|
||||
{
|
||||
return Result.Create(_edgesAdd[i].targetArchetype.value, ResultStatus.Success);
|
||||
return edge.targetArchetype;
|
||||
}
|
||||
}
|
||||
|
||||
return Result.Create(-1, ResultStatus.NotFound);
|
||||
return Identifier<Archetype>.Invalid;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AddEdgeRemove(int componentID, Identifier<Archetype> targetArchetype)
|
||||
public void AddEdgeRemove(Identifier<IComponent> componentID, Identifier<Archetype> targetArchetype)
|
||||
{
|
||||
_edgesRemove.Add(new Edge
|
||||
{
|
||||
@@ -326,22 +357,23 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Result<int, ResultStatus> GetEdgeRemove(int componentID)
|
||||
public Identifier<Archetype> GetEdgeRemove(Identifier<IComponent> componentID)
|
||||
{
|
||||
for (var i = 0; i < _edgesRemove.Count; i++)
|
||||
{
|
||||
if (_edgesRemove[i].componentID == componentID)
|
||||
var edge = _edgesRemove[i];
|
||||
if (edge.componentID == componentID)
|
||||
{
|
||||
return Result.Create(_edgesRemove[i].targetArchetype.value, ResultStatus.Success);
|
||||
return edge.targetArchetype;
|
||||
}
|
||||
}
|
||||
|
||||
return Result.Create(-1, ResultStatus.NotFound);
|
||||
return Identifier<Archetype>.Invalid;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public UnsafeArray<T> GetComponentArray<T>(int chunkIndex)
|
||||
where T : unmanaged
|
||||
public Span<T> GetComponentArray<T>(int chunkIndex)
|
||||
where T : unmanaged, IComponent
|
||||
{
|
||||
var id = ComponentTypeID<T>.value;
|
||||
if (id >= _componentIDToOffset.Count)
|
||||
@@ -356,7 +388,12 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
|
||||
}
|
||||
|
||||
var chunk = _chunks[chunkIndex];
|
||||
return new UnsafeArray<T>((T*)((byte*)chunk.GetUnsafePtr() + offset), _entityCapacity);
|
||||
return new Span<T>((T*)((byte*)chunk.GetUnsafePtr() + offset), chunk.Count);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _hash;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
@@ -1,43 +1,51 @@
|
||||
using Ghost.Core;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.LowLevel.Utilities;
|
||||
|
||||
namespace Ghost.ArcEntities;
|
||||
|
||||
public interface IComponent : IIdentifierType
|
||||
{
|
||||
}
|
||||
|
||||
public struct ComponentInfo
|
||||
{
|
||||
// public FixedText64 stableName; // Do we actually need this?
|
||||
public int size;
|
||||
public int alignment;
|
||||
public int id;
|
||||
public Identifier<IComponent> id;
|
||||
}
|
||||
|
||||
internal static unsafe class ComponentTypeID<T>
|
||||
where T : unmanaged
|
||||
public static unsafe class ComponentTypeID<T>
|
||||
where T : unmanaged, IComponent
|
||||
{
|
||||
public static readonly int value = ComponentRegister.GetOrRegisterComponent<T>();
|
||||
public static readonly Identifier<IComponent> value = ComponentRegister.GetOrRegisterComponent<T>();
|
||||
}
|
||||
|
||||
internal static class ComponentRegister
|
||||
{
|
||||
private static int s_nextComponentTypeID = 0;
|
||||
private static Dictionary<IntPtr, int> s_typeHandleToID = new();
|
||||
private static Dictionary<IntPtr, Identifier<IComponent>> s_typeHandleToID = new();
|
||||
|
||||
internal static List<ComponentInfo> s_registeredComponents = new();
|
||||
internal static Dictionary<string, int> s_nameToRuntimeID = new();
|
||||
private static List<ComponentInfo> s_registeredComponents = new();
|
||||
private static Dictionary<string, Identifier<IComponent>> s_nameToRuntimeID = new();
|
||||
|
||||
internal unsafe static int GetOrRegisterComponent<T>()
|
||||
where T : unmanaged
|
||||
public unsafe static Identifier<IComponent> GetOrRegisterComponent<T>()
|
||||
where T : unmanaged, IComponent
|
||||
{
|
||||
var typeHandle = typeof(T).TypeHandle.Value;
|
||||
|
||||
lock (s_registeredComponents)
|
||||
{
|
||||
if (s_typeHandleToID.TryGetValue(typeHandle, out int existingID))
|
||||
if (s_typeHandleToID.TryGetValue(typeHandle, out var existingID))
|
||||
{
|
||||
return existingID;
|
||||
}
|
||||
|
||||
int newID = s_nextComponentTypeID++;
|
||||
string stableName = typeof(T).FullName ?? typeof(T).Name;
|
||||
var newID = new Identifier<IComponent>(s_nextComponentTypeID);
|
||||
s_nextComponentTypeID++;
|
||||
|
||||
var stableName = typeof(T).FullName ?? typeof(T).Name;
|
||||
|
||||
var info = new ComponentInfo
|
||||
{
|
||||
@@ -47,8 +55,8 @@ internal static class ComponentRegister
|
||||
id = newID,
|
||||
};
|
||||
|
||||
while (s_registeredComponents.Count <= newID) s_registeredComponents.Add(default);
|
||||
s_registeredComponents[newID] = info;
|
||||
while (s_registeredComponents.Count <= newID.value) s_registeredComponents.Add(default);
|
||||
s_registeredComponents[newID.value] = info;
|
||||
|
||||
s_typeHandleToID[typeHandle] = newID;
|
||||
s_nameToRuntimeID[stableName] = newID;
|
||||
@@ -57,8 +65,32 @@ internal static class ComponentRegister
|
||||
}
|
||||
}
|
||||
|
||||
internal static ComponentInfo GetComponentInfo(int typeId)
|
||||
public static ComponentInfo GetComponentInfo(Identifier<IComponent> typeId)
|
||||
{
|
||||
return s_registeredComponents[typeId];
|
||||
}
|
||||
|
||||
public static int GetHashCode(ReadOnlySpan<Identifier<IComponent>> componentTypeIDs)
|
||||
{
|
||||
var largestID = 0;
|
||||
foreach (var id in componentTypeIDs)
|
||||
{
|
||||
if (id.value > largestID)
|
||||
{
|
||||
largestID = id.value;
|
||||
}
|
||||
}
|
||||
|
||||
var length = UnsafeBitSet.RequiredLength(largestID + 1);
|
||||
var bits = (Span<uint>)stackalloc uint[length];
|
||||
bits.Clear();
|
||||
|
||||
var bitSet = new SpanBitSet(bits);
|
||||
foreach (var id in componentTypeIDs)
|
||||
{
|
||||
bitSet.SetBit(id.value);
|
||||
}
|
||||
|
||||
return bitSet.GetHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,26 +4,26 @@ using System.Runtime.InteropServices;
|
||||
namespace Ghost.ArcEntities;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, Size = 8)]
|
||||
public struct Entity : IEquatable<Entity>, IComparable<Entity>
|
||||
public readonly struct Entity : IEquatable<Entity>, IComparable<Entity>
|
||||
{
|
||||
public const EntityID INVALID_ID = -1;
|
||||
|
||||
private EntityID _id;
|
||||
private GenerationID _generation;
|
||||
private readonly EntityID _id;
|
||||
private readonly GenerationID _generation;
|
||||
|
||||
public readonly EntityID ID
|
||||
public EntityID ID
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _id;
|
||||
}
|
||||
|
||||
public readonly GenerationID Generation
|
||||
public GenerationID Generation
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _generation;
|
||||
}
|
||||
|
||||
public readonly bool IsValid
|
||||
public bool IsValid
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ID != INVALID_ID;
|
||||
@@ -41,27 +41,24 @@ public struct Entity : IEquatable<Entity>, IComparable<Entity>
|
||||
_generation = generation;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void IncrementGeneration() => _generation++;
|
||||
|
||||
public readonly bool Equals(Entity other)
|
||||
public bool Equals(Entity other)
|
||||
{
|
||||
return _id == other._id && _generation == other._generation;
|
||||
}
|
||||
|
||||
public readonly int CompareTo(Entity other)
|
||||
public int CompareTo(Entity other)
|
||||
{
|
||||
return _id.CompareTo(other._id);
|
||||
}
|
||||
|
||||
public override readonly bool Equals(object? obj)
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is Entity other && Equals(other);
|
||||
}
|
||||
|
||||
public override readonly int GetHashCode()
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _id.GetHashCode();
|
||||
return _id ^ _generation << 16;
|
||||
}
|
||||
|
||||
public static bool operator ==(Entity left, Entity right)
|
||||
@@ -74,7 +71,7 @@ public struct Entity : IEquatable<Entity>, IComparable<Entity>
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
public override readonly string ToString()
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Entity {{ Index: {ID}, Generation: {Generation} }}";
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public unsafe class EntityCommandBuffer : IDisposable
|
||||
}
|
||||
|
||||
private readonly EntityManager _entityManager;
|
||||
private UnsafeList<Command> _commands;
|
||||
private UnsafeList<Command> _commands; // TODO: Maybe use UnsafeArray<byte> directly?
|
||||
|
||||
public EntityCommandBuffer(EntityManager entityManager)
|
||||
{
|
||||
@@ -58,7 +58,7 @@ public unsafe class EntityCommandBuffer : IDisposable
|
||||
}
|
||||
|
||||
public void AddComponent<T>(Entity entity, T component)
|
||||
where T : unmanaged
|
||||
where T : unmanaged, IComponent
|
||||
{
|
||||
var data = new UnsafeArray<byte>(sizeof(T), Misaki.HighPerformance.LowLevel.Buffer.Allocator.Persistent);
|
||||
MemoryUtility.MemCpy(&component, data.GetUnsafePtr(), (nuint)sizeof(T));
|
||||
@@ -75,7 +75,7 @@ public unsafe class EntityCommandBuffer : IDisposable
|
||||
}
|
||||
|
||||
public void RemoveComponent<T>(Entity entity)
|
||||
where T : unmanaged
|
||||
where T : unmanaged, IComponent
|
||||
{
|
||||
var command = new Command
|
||||
{
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
using Ghost.Core;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.LowLevel.Utilities;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Ghost.ArcEntities;
|
||||
|
||||
public class EntityManager : IDisposable
|
||||
public unsafe class EntityManager : IDisposable
|
||||
{
|
||||
private struct EntityLocation
|
||||
{
|
||||
@@ -16,10 +18,51 @@ public class EntityManager : IDisposable
|
||||
private World _world;
|
||||
private UnsafeSlotMap<EntityLocation> _entityLocations;
|
||||
|
||||
internal EntityManager(World world)
|
||||
internal EntityManager(World world, int initialCapacity)
|
||||
{
|
||||
_world = world;
|
||||
_entityLocations = new UnsafeSlotMap<EntityLocation>(16, Allocator.Persistent);
|
||||
_entityLocations = new UnsafeSlotMap<EntityLocation>(initialCapacity, Allocator.Persistent);
|
||||
}
|
||||
|
||||
internal ResultStatus UpdateEntityLocation(Entity entity, Identifier<Archetype> newArchetypeID, int newChunkIndex, int newRowIndex)
|
||||
{
|
||||
ref var location = ref _entityLocations.GetElementReferenceAt(entity.ID, entity.Generation, out var exist);
|
||||
if (!exist)
|
||||
{
|
||||
return ResultStatus.NotFound;
|
||||
}
|
||||
|
||||
location.archetypeID = newArchetypeID;
|
||||
location.chunkIndex = newChunkIndex;
|
||||
location.rowIndex = newRowIndex;
|
||||
|
||||
return ResultStatus.Success;
|
||||
}
|
||||
|
||||
public Entity CreateEntity(params ReadOnlySpan<Identifier<IComponent>> componentTypeIDs)
|
||||
{
|
||||
var signatureHash = ComponentRegister.GetHashCode(componentTypeIDs);
|
||||
var arcID = _world.GetArchetypeIDBySignatureHash(signatureHash);
|
||||
|
||||
if (arcID.IsNotValid)
|
||||
{
|
||||
arcID = _world.CreateArchetype(componentTypeIDs, signatureHash);
|
||||
}
|
||||
|
||||
ref var archetype = ref _world.GetArchetypeReference(arcID);
|
||||
archetype.AllocateEntity(out var chunkIndex, out var rowIndex);
|
||||
|
||||
var id = _entityLocations.Add(new EntityLocation
|
||||
{
|
||||
archetypeID = arcID,
|
||||
chunkIndex = chunkIndex,
|
||||
rowIndex = rowIndex
|
||||
}, out var generation);
|
||||
|
||||
var entity = new Entity(id, generation);
|
||||
archetype.SetEntity(chunkIndex, rowIndex, entity);
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
public Entity CreateEntity()
|
||||
@@ -41,7 +84,7 @@ public class EntityManager : IDisposable
|
||||
return entity;
|
||||
}
|
||||
|
||||
public ResultStatus RemoveEntity(Entity entity)
|
||||
public ResultStatus DestoryEntity(Entity entity)
|
||||
{
|
||||
if (!_entityLocations.TryGetElementAt(entity.ID, entity.Generation, out var location))
|
||||
{
|
||||
@@ -63,23 +106,23 @@ public class EntityManager : IDisposable
|
||||
return ResultStatus.Success;
|
||||
}
|
||||
|
||||
internal ResultStatus UpdateEntityLocation(Entity entity, Identifier<Archetype> newArchetypeID, int newChunkIndex, int newRowIndex)
|
||||
private static void CopyData(ref Archetype oldArch, int oldChunk, int oldRow,
|
||||
ref Archetype newArch, int newChunk, int newRow)
|
||||
{
|
||||
ref var location = ref _entityLocations.GetElementReferenceAt(entity.ID, entity.Generation, out var exist);
|
||||
if (!exist)
|
||||
// Iterate every component type in the OLD archetype
|
||||
for (int i = 0; i < oldArch.Layouts.Count; i++)
|
||||
{
|
||||
return ResultStatus.NotFound;
|
||||
var layout = oldArch.Layouts[i];
|
||||
|
||||
var src = oldArch.Chunks[oldChunk].GetUnsafePtr() + layout.offset + (layout.size * oldRow);
|
||||
var newOffset = newArch.GetOffset(layout.componentID); // O(1) Lookup
|
||||
var dst = oldArch.Chunks[oldChunk].GetUnsafePtr() + newOffset + (layout.size * newRow);
|
||||
|
||||
MemoryUtility.MemCpy(src, dst, (nuint)layout.size);
|
||||
}
|
||||
}
|
||||
|
||||
location.archetypeID = newArchetypeID;
|
||||
location.chunkIndex = newChunkIndex;
|
||||
location.rowIndex = newRowIndex;
|
||||
|
||||
return ResultStatus.Success;
|
||||
}
|
||||
|
||||
public ResultStatus AddComponent<T>(Entity entity)
|
||||
where T : unmanaged
|
||||
public ResultStatus AddComponent(Entity entity, Identifier<IComponent> componentID, void* component)
|
||||
{
|
||||
// Find current location
|
||||
ref var location = ref _entityLocations.GetElementReferenceAt(entity.ID, entity.Generation, out var exist);
|
||||
@@ -89,12 +132,14 @@ public class EntityManager : IDisposable
|
||||
}
|
||||
|
||||
// Build new archetype signature
|
||||
ref var archetype = ref _world.GetArchetypeReference(location.archetypeID);
|
||||
var currentSignature = archetype.Signature;
|
||||
ref var oldArchetype = ref _world.GetArchetypeReference(location.archetypeID);
|
||||
var oldSignature = oldArchetype.Signature;
|
||||
|
||||
var compID = ComponentTypeID<T>.value;
|
||||
|
||||
var largestComponentID = Math.Max(currentSignature.Count, compID);
|
||||
// TODO: Check edge cache first.
|
||||
var newArcID = oldArchetype.GetEdgeAdd(componentID);
|
||||
if (newArcID.IsNotValid)
|
||||
{
|
||||
var largestComponentID = Math.Max(oldSignature.Count, componentID);
|
||||
var length = UnsafeBitSet.RequiredLength(largestComponentID + 1);
|
||||
|
||||
Span<uint> bits = stackalloc uint[length];
|
||||
@@ -102,49 +147,67 @@ public class EntityManager : IDisposable
|
||||
|
||||
var newSignature = new SpanBitSet(bits);
|
||||
|
||||
var i = 0;
|
||||
var iterator = 0;
|
||||
var compCount = 0;
|
||||
do
|
||||
while (true)
|
||||
{
|
||||
i = currentSignature.NextSetBit(i);
|
||||
if (i == -1)
|
||||
int bit = oldSignature.NextSetBit(iterator);
|
||||
if (bit == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
newSignature.SetBit(i);
|
||||
newSignature.SetBit(bit);
|
||||
iterator = bit + 1;
|
||||
compCount++;
|
||||
} while (true);
|
||||
}
|
||||
|
||||
compCount++;
|
||||
newSignature.SetBit(compID);
|
||||
newSignature.SetBit(componentID);
|
||||
|
||||
// Find or create new archetype
|
||||
var newSignatureHash = newSignature.GetHashCode();
|
||||
var newArcID = _world.GetArchetypeIDBySignatureHash(newSignatureHash);
|
||||
newArcID = _world.GetArchetypeIDBySignatureHash(newSignatureHash);
|
||||
if (newArcID.IsNotValid)
|
||||
{
|
||||
// Create new archetype
|
||||
Span<int> componentTypeIDs = stackalloc int[compCount];
|
||||
componentTypeIDs[0] = compID;
|
||||
Span<Identifier<IComponent>> componentTypeIDs = stackalloc Identifier<IComponent>[compCount];
|
||||
componentTypeIDs[0] = componentID;
|
||||
|
||||
do
|
||||
iterator = 0;
|
||||
while (true)
|
||||
{
|
||||
i = currentSignature.NextSetBit(i);
|
||||
if (i == -1)
|
||||
int bit = oldSignature.NextSetBit(iterator);
|
||||
if (bit == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
componentTypeIDs[--compCount] = i;
|
||||
} while (true);
|
||||
componentTypeIDs[--compCount] = bit;
|
||||
iterator = bit + 1;
|
||||
}
|
||||
|
||||
_world.CreateArchetype(componentTypeIDs, newSignatureHash);
|
||||
}
|
||||
|
||||
oldArchetype.AddEdgeAdd(componentID, newArcID);
|
||||
}
|
||||
|
||||
// Move entity data
|
||||
ref var newArchetype = ref _world.GetArchetypeReference(newArcID);
|
||||
newArchetype.AllocateEntity(out var newChunkIndex, out var newRowIndex);
|
||||
CopyData(ref oldArchetype, location.chunkIndex, location.rowIndex,
|
||||
ref newArchetype, newChunkIndex, newRowIndex);
|
||||
|
||||
newArchetype.SetEntity(newChunkIndex, newRowIndex, entity);
|
||||
newArchetype.SetComponentData(newChunkIndex, newRowIndex, componentID, component);
|
||||
|
||||
var r = oldArchetype.RemoveEntity(location.chunkIndex, location.rowIndex);
|
||||
Debug.Assert(r == ResultStatus.Success); // We assert it because the entity should exist if the whole system is consistent.
|
||||
// if (r != ResultStatus.Success)
|
||||
// {
|
||||
// return r;
|
||||
// }
|
||||
|
||||
// Update location
|
||||
location.archetypeID = newArcID;
|
||||
@@ -154,6 +217,48 @@ public class EntityManager : IDisposable
|
||||
return ResultStatus.Success;
|
||||
}
|
||||
|
||||
public ResultStatus AddComponent<T>(Entity entity, ref T component)
|
||||
where T : unmanaged, IComponent
|
||||
{
|
||||
return AddComponent(entity, ComponentTypeID<T>.value, UnsafeUtility.AddressOf(ref component));
|
||||
}
|
||||
|
||||
public ResultStatus SetComponentData(Entity entity, Identifier<IComponent> componentID, void* pComponent)
|
||||
{
|
||||
if (!_entityLocations.TryGetElementAt(entity.ID, entity.Generation, out var location))
|
||||
{
|
||||
return ResultStatus.NotFound;
|
||||
}
|
||||
|
||||
ref var archetype = ref _world.GetArchetypeReference(location.archetypeID);
|
||||
archetype.SetComponentData(location.chunkIndex, location.rowIndex, componentID, pComponent);
|
||||
|
||||
return ResultStatus.Success;
|
||||
}
|
||||
|
||||
public ResultStatus SetComponentData<T>(Entity entity, ref T component)
|
||||
where T : unmanaged, IComponent
|
||||
{
|
||||
return SetComponentData(entity, ComponentTypeID<T>.value, UnsafeUtility.AddressOf(ref component));
|
||||
}
|
||||
|
||||
public bool HasComponent(Entity entity, Identifier<IComponent> componentID)
|
||||
{
|
||||
if (!_entityLocations.TryGetElementAt(entity.ID, entity.Generation, out var location))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
ref var archetype = ref _world.GetArchetypeReference(location.archetypeID);
|
||||
return archetype.HasComponent(componentID);
|
||||
}
|
||||
|
||||
public bool HasComponent<T>(Entity entity)
|
||||
where T : unmanaged, IComponent
|
||||
{
|
||||
return HasComponent(entity, ComponentTypeID<T>.value);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_entityLocations.Dispose();
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
namespace Ghost.ArcEntities;
|
||||
|
||||
public unsafe class EntityQuery<T1, T2>
|
||||
where T1 : unmanaged
|
||||
where T2 : unmanaged
|
||||
where T1 : unmanaged, IComponent
|
||||
where T2 : unmanaged, IComponent
|
||||
{
|
||||
// The Cache Struct
|
||||
struct ArchetypeCache
|
||||
|
||||
5
Ghost.ArcEntities/Utility.cs
Normal file
5
Ghost.ArcEntities/Utility.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace Ghost.ArcEntities;
|
||||
|
||||
public static class Utility
|
||||
{
|
||||
}
|
||||
@@ -57,11 +57,13 @@ public partial class World : IIdentifierType, IDisposable, IEquatable<World>
|
||||
private UnsafeList<Archetype> _archetypes;
|
||||
private UnsafeHashMap<int, Identifier<Archetype>> _archetypeLookup; // Signature Hash to Archetype ID
|
||||
private EntityManager _entityManager;
|
||||
private EntityCommandBuffer _entityCommandBuffer;
|
||||
|
||||
private bool _disposed = false;
|
||||
|
||||
public Identifier<World> ID => _id;
|
||||
public EntityManager EntityManager => _entityManager;
|
||||
public EntityCommandBuffer EntityCommandBuffer => _entityCommandBuffer;
|
||||
|
||||
private World(Identifier<World> id, int entityCapacity)
|
||||
{
|
||||
@@ -70,10 +72,11 @@ public partial class World : IIdentifierType, IDisposable, IEquatable<World>
|
||||
_archetypes = new UnsafeList<Archetype>(16, Allocator.Persistent);
|
||||
_archetypeLookup = new UnsafeHashMap<int, Identifier<Archetype>>(16, Allocator.Persistent);
|
||||
|
||||
_entityManager = new EntityManager(this);
|
||||
_entityManager = new EntityManager(this, entityCapacity);
|
||||
_entityCommandBuffer = new EntityCommandBuffer(_entityManager);
|
||||
|
||||
// Create the empty archetype
|
||||
CreateArchetype(ReadOnlySpan<int>.Empty, 0);
|
||||
CreateArchetype(ReadOnlySpan<Identifier<IComponent>>.Empty, 0);
|
||||
}
|
||||
|
||||
~World()
|
||||
@@ -81,11 +84,12 @@ public partial class World : IIdentifierType, IDisposable, IEquatable<World>
|
||||
Dispose();
|
||||
}
|
||||
|
||||
internal Identifier<Archetype> CreateArchetype(ReadOnlySpan<int> componentTypeIDs, int signatureHash)
|
||||
internal Identifier<Archetype> CreateArchetype(ReadOnlySpan<Identifier<IComponent>> componentTypeIDs, int signatureHash)
|
||||
{
|
||||
var arcID = new Identifier<Archetype>(_archetypes.Count);
|
||||
_archetypes.Add(new Archetype(arcID, _id, componentTypeIDs));
|
||||
_archetypeLookup.Add(signatureHash, arcID);
|
||||
|
||||
return arcID;
|
||||
}
|
||||
|
||||
@@ -136,6 +140,8 @@ public partial class World : IIdentifierType, IDisposable, IEquatable<World>
|
||||
return;
|
||||
}
|
||||
|
||||
_entityManager.Dispose();
|
||||
|
||||
_archetypes.Dispose();
|
||||
_archetypeLookup.Dispose();
|
||||
|
||||
|
||||
@@ -106,6 +106,29 @@ public readonly struct Identifier<T>
|
||||
{
|
||||
return !a.Equals(b);
|
||||
}
|
||||
|
||||
public static bool operator <(Identifier<T> a, Identifier<T> b)
|
||||
{
|
||||
return a.value < b.value;
|
||||
}
|
||||
|
||||
public static bool operator >(Identifier<T> a, Identifier<T> b)
|
||||
{
|
||||
return a.value > b.value;
|
||||
}
|
||||
|
||||
public static bool operator <=(Identifier<T> a, Identifier<T> b)
|
||||
{
|
||||
return a.value <= b.value;
|
||||
}
|
||||
|
||||
public static bool operator >=(Identifier<T> a, Identifier<T> b)
|
||||
{
|
||||
return a.value >= b.value;
|
||||
}
|
||||
|
||||
public static implicit operator int(Identifier<T> id) => id.value;
|
||||
public static implicit operator Identifier<T>(int value) => new Identifier<T>(value);
|
||||
}
|
||||
|
||||
public readonly struct Key<T>
|
||||
|
||||
@@ -152,6 +152,14 @@ public readonly ref struct RefResult<T, S>
|
||||
|
||||
public static class ResultExtensions
|
||||
{
|
||||
public static void ThrowIfFailed(this ResultStatus result)
|
||||
{
|
||||
if (result != ResultStatus.Success)
|
||||
{
|
||||
throw new InvalidOperationException($"Operation failed: {result}");
|
||||
}
|
||||
}
|
||||
|
||||
public static void ThrowIfFailed(this Result result)
|
||||
{
|
||||
if (!result.IsSuccess)
|
||||
@@ -170,11 +178,6 @@ public static class ResultExtensions
|
||||
return result.Value;
|
||||
}
|
||||
|
||||
public static T? GetValueOrDefault<T>(this Result<T> result, T? defaultValue = default)
|
||||
{
|
||||
return result.IsSuccess ? result.Value : defaultValue;
|
||||
}
|
||||
|
||||
public static T GetValueOrThrow<T, S>(this Result<T, S> result, S expect)
|
||||
where S : Enum
|
||||
{
|
||||
@@ -186,12 +189,29 @@ public static class ResultExtensions
|
||||
return result.Value;
|
||||
}
|
||||
|
||||
public static T? GetValueOrDefault<T>(this Result<T> result, T? defaultValue = default)
|
||||
{
|
||||
return result.IsSuccess ? result.Value : defaultValue;
|
||||
}
|
||||
|
||||
public static T? GetValueOrDefault<T, S>(this Result<T, S> result, S expect, T? defaultValue = default)
|
||||
where S : Enum
|
||||
{
|
||||
return (result.Status?.Equals(expect) ?? false) ? defaultValue : result.Value;
|
||||
}
|
||||
|
||||
public static bool TryGetValue<T>(this Result<T> result, out T value)
|
||||
{
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
value = result.Value;
|
||||
return true;
|
||||
}
|
||||
|
||||
value = default!;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Result OnSuccess(this Result result, Action action)
|
||||
{
|
||||
if (result.IsSuccess)
|
||||
|
||||
42
Ghost.Entities.Test/ArcEntityTest.cs
Normal file
42
Ghost.Entities.Test/ArcEntityTest.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using Ghost.Test.Core;
|
||||
using Ghost.ArcEntities;
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
|
||||
namespace Ghost.Entities.Test;
|
||||
|
||||
public partial class ArcEntityTest : ITest
|
||||
{
|
||||
private Ghost.ArcEntities.World _world = null!;
|
||||
|
||||
public void Setup()
|
||||
{
|
||||
_world = Ghost.ArcEntities.World.Create();
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
var entity1 = _world.EntityManager.CreateEntity(ComponentTypeID<TransformData>.value);
|
||||
Console.WriteLine($"{entity1} Has Transform: {_world.EntityManager.HasComponent<TransformData>(entity1)}");
|
||||
var mesh = new MeshData { index = 1 };
|
||||
_world.EntityManager.AddComponent<MeshData>(entity1, ref mesh);
|
||||
Console.WriteLine($"{entity1} Has Mesh: {_world.EntityManager.HasComponent<MeshData>(entity1)}");
|
||||
|
||||
_world.EntityManager.DestoryEntity(entity1);
|
||||
Console.WriteLine($"{entity1} Has Transform: {_world.EntityManager.HasComponent<TransformData>(entity1)}");
|
||||
}
|
||||
|
||||
public void Cleanup()
|
||||
{
|
||||
_world.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public struct TransformData : IComponent
|
||||
{
|
||||
public float3 position;
|
||||
}
|
||||
|
||||
public struct MeshData : IComponent
|
||||
{
|
||||
public int index;
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Ghost.Entities\Ghost.Entities.csproj" />
|
||||
<ProjectReference Include="..\Ghost.ArcEntities\Ghost.ArcEntities.csproj" />
|
||||
<ProjectReference Include="..\Ghost.Test.Core\Ghost.Test.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
|
||||
using Ghost.Entities.Test;
|
||||
using Ghost.Test.Core;
|
||||
|
||||
TestRunner.Run<EntityTest>();
|
||||
TestRunner.Run<Ghost.Entities.Test.ArcEntityTest>();
|
||||
|
||||
Reference in New Issue
Block a user