feature/archetype-ecs #1

Merged
Misaki merged 23 commits from feature/archetype-ecs into develop 2025-12-17 08:27:32 +00:00
5 changed files with 44 additions and 37 deletions
Showing only changes of commit 7db4be1e6e - Show all commits

View File

@@ -20,9 +20,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Misaki.HighPerformance" Version="1.0.1" />
<PackageReference Include="Misaki.HighPerformance" Version="1.0.2" />
<PackageReference Include="Misaki.HighPerformance.Jobs" Version="1.2.1" />
<PackageReference Include="Misaki.HighPerformance.LowLevel" Version="1.3.0" />
<PackageReference Include="Misaki.HighPerformance.LowLevel" Version="1.3.2" />
<PackageReference Include="Misaki.HighPerformance.Mathematics" Version="1.2.6" />
<PackageReference Include="System.IO.Hashing" Version="10.0.0" />
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.26100.5" />

View File

@@ -1,7 +1,7 @@
using Ghost.Test.Core;
using Misaki.HighPerformance.Jobs;
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.Mathematics;
using System.Diagnostics;
namespace Ghost.Entities.Test;
@@ -9,17 +9,19 @@ internal struct TestChunkQueryJob : IJobChunk
{
public readonly void Execute(ChunkView view, int threadIndex)
{
var random = new random((uint)threadIndex + 1u);
var transforms = view.GetComponentDataRW<Transform>();
for (var i = 0; i < view.Count; i++)
{
transforms[i].position += new float3(10, 10, 10);
transforms[i].position += random.NextFloat3(-1f, 1f);
}
}
}
internal struct TestEntityQueryJob : IJobEntity<Transform>
{
public readonly void Execute(Entity entity, ref Transform transform, int index)
public readonly void Execute(Entity entity, ref Transform transform, int threadIndex)
{
transform.position += new float3(5, 5, 5);
}
@@ -38,23 +40,16 @@ public partial class EntityTest : ITest
public void Run()
{
var entity1 = _world.EntityManager.CreateEntity(ComponentTypeID<Transform>.value, ComponentTypeID<ManagedEntityRef>.value);
_world.EntityManager.AddComponent(entity1, new Mesh { index = 1 });
_world.EntityManager.SetComponent(entity1, new ManagedEntityRef
{
entity = _world.EntityManager.CreateManagedEntity()
});
var entities = (Span<Entity>)stackalloc Entity[1000];
_world.EntityManager.CreateEntities(entities, ComponentTypeID<Transform>.value);
var entity2 = _world.EntityManager.CreateEntity(ComponentTypeID<Transform>.value);
_world.EntityManager.SetComponent(entity2, new Transform { position = new float3(1, 2, 3) });
var queryID = new QueryBuilder().WithAllRW<Transform>().WithAbsent<Mesh>().Build(_world);
var queryID = new QueryBuilder().WithAllRW<Transform>().Build(_world);
ref var query = ref _world.GetEntityQueryReference(queryID);
_world.AdvanceVersion();
var testJob = new TestEntityQueryJob();
var handle = query.ScheduleEntityParallel<TestEntityQueryJob, Transform>(testJob, 64, JobHandle.Invalid);
var testJob = new TestChunkQueryJob();
var handle = query.ScheduleChunkParallel<TestChunkQueryJob>(testJob, 64, JobHandle.Invalid);
_jobScheduler.WaitComplete(handle);
// _world.EntityManager.AddScriptComponent<TestScriptComponent>(entity1);
@@ -73,22 +68,22 @@ public partial class EntityTest : ITest
foreach (var chunk in query.GetChunkIterator())
{
var transforms = chunk.GetComponentData<Transform>();
var entities = chunk.GetEntities();
var chunkEntities = chunk.GetEntities();
if (chunk.HasChanged<Transform>(0))
// if (chunk.HasChanged<Transform>(0))
{
var bits = chunk.GetEnableBits<Transform>();
// var bits = chunk.GetEnableBits<Transform>();
var it = bits.GetIterator();
while (it.Next(out var index) && index < chunk.Count)
// var it = bits.GetIterator();
// while (it.Next(out var index) && index < chunk.Count)
for (var index = 0; index < chunk.Count; index++)
{
Console.WriteLine($"Entity {entities[index]} Updated Position: {transforms[index].position}");
Console.WriteLine($"Entity {chunkEntities[index]} Updated Position: {transforms[index].position}");
}
}
}
Debug.Assert(_world.EntityManager.DestroyEntity(entity1) == Core.ErrorStatus.None);
Debug.Assert(_world.EntityManager.DestroyEntity(entity2) == Core.ErrorStatus.None);
_world.EntityManager.DestroyEntities(entities);
}
public void Cleanup()

View File

@@ -157,7 +157,9 @@ internal unsafe struct Chunk : IDisposable
public void Dispose()
{
_data.Dispose();
Console.WriteLine($"Disposing chunk data");
_versions.Dispose();
Console.WriteLine($"Disposing chunk versions");
}
}
@@ -587,9 +589,12 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
{
if (_chunks.IsCreated)
{
var i= 0;
foreach (ref var chunk in _chunks)
{
Console.WriteLine($"Disposing chunk {i} of archetype {_id}");
chunk.Dispose();
i++;
}
}

View File

@@ -68,7 +68,7 @@ public partial class EntityManager
/// <returns>True if the ManagedEntity exists, false otherwise.</returns>
public bool Exists(ManagedEntity managedEntity)
{
return _scriptComponents.Contain(managedEntity.id, managedEntity.generation);
return _scriptComponents.Contains(managedEntity.id, managedEntity.generation);
}
/// <summary>

View File

@@ -60,7 +60,7 @@ public unsafe partial class EntityManager : IDisposable
public Entity CreateEntity()
{
var entities = (Span<Entity>)stackalloc Entity[1];
CreateEntities(1, entities);
CreateEntities(entities);
return entities[0];
}
@@ -73,7 +73,7 @@ public unsafe partial class EntityManager : IDisposable
public Entity CreateEntity(params ReadOnlySpan<Identifier<IComponent>> componentTypeIDs)
{
var entities = (Span<Entity>)stackalloc Entity[1];
CreateEntities(1, entities, componentTypeIDs);
CreateEntities(entities, componentTypeIDs);
return entities[0];
}
@@ -81,14 +81,13 @@ public unsafe partial class EntityManager : IDisposable
/// <summary>
/// Create multiple entities with no components.
/// </summary>
/// <param name="count">The number of entities to create.</param>
/// <param name="entities">The span to store the created entities.</param>
public void CreateEntities(int count, Span<Entity> entities)
public void CreateEntities(Span<Entity> entities)
{
ref var emptyArchetype = ref _world.GetArchetypeReference(World.EmptyArchetypeID);
emptyArchetype.AllocateEntity(out var chunkIndex, out var rowIndex);
for (var i = 0; i < count; i++)
for (var i = 0; i < entities.Length; i++)
{
var id = _entityLocations.Add(new EntityLocation
{
@@ -130,11 +129,10 @@ public unsafe partial class EntityManager : IDisposable
/// <summary>
/// Create multiple entities with specified components.
/// </summary>
/// <param name="count">The number of entities to create.</param>
/// <param name="allocator">The allocator to use for the returned array.</param>
/// <param name="componentTypeIDs">The component type IDs to add to the entities. </param>
/// <returns>An array of the created entities.</returns>
public void CreateEntities(int count, Span<Entity> entities, params ReadOnlySpan<Identifier<IComponent>> componentTypeIDs)
public void CreateEntities(Span<Entity> entities, params ReadOnlySpan<Identifier<IComponent>> componentTypeIDs)
{
var signatureHash = ComponentRegister.GetHashCode(componentTypeIDs);
var arcID = _world.GetArchetypeIDBySignatureHash(signatureHash);
@@ -146,7 +144,7 @@ public unsafe partial class EntityManager : IDisposable
ref var archetype = ref _world.GetArchetypeReference(arcID);
for (var i = 0; i < count; i++)
for (var i = 0; i < entities.Length; i++)
{
archetype.AllocateEntity(out var chunkIndex, out var rowIndex);
@@ -230,6 +228,18 @@ public unsafe partial class EntityManager : IDisposable
return ErrorStatus.None;
}
/// <summary>
/// Destroy the specified entities.
/// </summary>
/// <param name="entities">The entities to destroy.</param>
public void DestroyEntities(ReadOnlySpan<Entity> entities)
{
for (var i = 0; i < entities.Length; i++)
{
DestroyEntity(entities[i]);
}
}
/// <summary>
/// Check if the specified entity exists.
/// </summary>
@@ -714,9 +724,6 @@ public unsafe partial class EntityManager : IDisposable
return;
}
Debug.Assert(_entityLocations.Count == 0, "There are still entities alive when disposing EntityManager.");
Debug.Assert(_scriptComponents.Count == 0, "There are still managed entities alive when disposing EntityManager.");
_entityLocations.Dispose();
_disposed = true;