Refactor ECS framework and improve performance

Refactored `ArcEntityTest` to use updated `Transform` and `Mesh` components, improving query logic with `GetChunkIterator` and introducing `ForEach` methods for better modularity.

Enhanced `Chunk` and `Archetype` structs with `readonly` properties and memory optimizations. Fixed bugs in memory copy logic and entity relocation.

Improved `EntityManager` with proper disposal handling, added a destructor, and fixed pointer usage in `AddComponent` and `SetComponentData`.

Refactored `EntityQuery` to use `ChunkIterator` and `ChunkView` for better abstraction. Simplified `EntityQueryMask` logic for performance.

Introduced templated `ForEach` methods and `ForEachWithEntity` methods, dynamically generated using T4 templates for scalability.

Added disposal logic for archetypes and queries in `World`. Updated `Program.cs` to include memory debugging setup.

Integrated T4 templates for dynamic code generation and added helper functions for template generation. Updated project file to include templates and generated outputs.

General improvements include enforcing immutability, optimizing memory management, and adding debugging/logging for better traceability.
This commit is contained in:
2025-12-05 00:29:12 +09:00
parent f9db047a5f
commit 224b2b2dd5
15 changed files with 1424 additions and 135 deletions

View File

@@ -0,0 +1,127 @@
<#@ template language="C#" #>
<#@ output extension="gen.cs" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ include file="Helpers.ttinclude" #>
using Ghost.Core;
namespace Ghost.Entities;
public unsafe partial struct EntityQuery
{
<# for (var i = 1; i <= Amount; i++)
{
var generics = AppendGenerics(i);
var compGenerics = AppendGenericRefParameters(i);
var restrictions = AppendGenericRestrictionsMultiline(i, "unmanaged, IComponent", 2);
#>
public readonly void ForEach<<#= generics #>>(ForEach<<#= generics #>> action)
<#= restrictions #>
{
var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success);
var compTypeIDs = stackalloc int[] { <#= AppendGenerics(i, "ComponentTypeID<T{0}>.value") #> };
var offsets = stackalloc int[<#= i #>];
var basePtrs = stackalloc byte*[<#= i #>];
foreach (var archetypeID in _matchingArchetypes)
{
ref var archetype = ref world.GetArchetypeReference(archetypeID);
var hasAllComponents = true;
for (var index = 0; index < <#= i #>; index++)
{
offsets[index] = archetype.GetOffset(compTypeIDs[index]);
if (offsets[index] == -1)
{
hasAllComponents = false;
break;
}
}
if (!hasAllComponents)
{
continue;
}
for (var chunkIndex = 0; chunkIndex < archetype.ChunkCount; chunkIndex++)
{
ref var chunk = ref archetype.GetChunkReference(chunkIndex);
var count = chunk.Count;
for (var index = 0; index < <#= i #>; index++)
{
basePtrs[index] = chunk.GetUnsafePtr() + offsets[index];
}
for (var entityIndex = 0; entityIndex < count; entityIndex++)
{
<# for (var localIndex = 0; localIndex < i; localIndex++) { #>
var pComp<#= localIndex #> = (T<#= localIndex #>*)(basePtrs[<#= localIndex #>] + (sizeof(T<#= localIndex #>) * entityIndex));
<# } #>
action(<#= AppendRefParameters(i, "*pComp{0}") #>);
}
}
}
}
<# } #>
<# for (var i = 1; i <= Amount; i++)
{
var generics = AppendGenerics(i);
var compGenerics = AppendGenericRefParameters(i);
var restrictions = AppendGenericRestrictionsMultiline(i, "unmanaged, IComponent", 2);
#>
public readonly void ForEach<<#= generics #>>(ForEachWithEntity<<#= generics #>> action)
<#= restrictions #>
{
var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success);
var compTypeIDs = stackalloc int[] { <#= AppendGenerics(i, "ComponentTypeID<T{0}>.value") #> };
var offsets = stackalloc int[<#= i #>];
var basePtrs = stackalloc byte*[<#= i #>];
foreach (var archetypeID in _matchingArchetypes)
{
ref var archetype = ref world.GetArchetypeReference(archetypeID);
var hasAllComponents = true;
for (var index = 0; index < <#= i #>; index++)
{
offsets[index] = archetype.GetOffset(compTypeIDs[index]);
if (offsets[index] == -1)
{
hasAllComponents = false;
break;
}
}
if (!hasAllComponents)
{
continue;
}
for (var chunkIndex = 0; chunkIndex < archetype.ChunkCount; chunkIndex++)
{
ref var chunk = ref archetype.GetChunkReference(chunkIndex);
var count = chunk.Count;
for (var index = 0; index < <#= i #>; index++)
{
basePtrs[index] = chunk.GetUnsafePtr() + offsets[index];
}
for (var entityIndex = 0; entityIndex < count; entityIndex++)
{
var pEntity = (Entity*)(chunk.GetUnsafePtr() + archetype.EntityIDsOffset + (sizeof(Entity) * entityIndex));
<# for (var localIndex = 0; localIndex < i; localIndex++) { #>
var pComp<#= localIndex #> = (T<#= localIndex #>*)(basePtrs[<#= localIndex #>] + (sizeof(T<#= localIndex #>) * entityIndex));
<# } #>
action(*pEntity, <#= AppendRefParameters(i, "*pComp{0}") #>);
}
}
}
}
<# } #>
}