This commit is contained in:
2025-12-12 17:22:41 +09:00
parent a3863c1263
commit 7db4be1e6e
5 changed files with 44 additions and 37 deletions

View File

@@ -20,9 +20,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <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.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="Misaki.HighPerformance.Mathematics" Version="1.2.6" />
<PackageReference Include="System.IO.Hashing" Version="10.0.0" /> <PackageReference Include="System.IO.Hashing" Version="10.0.0" />
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.26100.5" /> <PackageReference Include="TerraFX.Interop.Windows" Version="10.0.26100.5" />

View File

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

View File

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

View File

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

View File

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