Updated debug view for chunk

This commit is contained in:
2025-12-12 21:11:01 +09:00
parent 7db4be1e6e
commit 05843fd665
3 changed files with 64 additions and 76 deletions

View File

@@ -14,7 +14,7 @@ internal struct TestChunkQueryJob : IJobChunk
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 += random.NextFloat3(-1f, 1f); transforms[i].position += random.NextFloat3();
} }
} }
} }

View File

@@ -23,35 +23,42 @@ internal unsafe sealed class ChunkDebugView
} }
} }
public byte* pData; private Chunk _chunk;
public int count;
public int capacity;
public int worldID;
public int archetypeID;
public ChunkDebugView(Chunk chunk) public ChunkDebugView(Chunk chunk)
{ {
pData = chunk.GetUnsafePtr(); _chunk = chunk;
count = chunk._count;
capacity = chunk._capacity;
#if DEBUG || GHOST_EDITOR
worldID = chunk._worldID;
archetypeID = chunk._archetypeID;
#else
worldID = -1;
archetypeID = -1;
#endif
} }
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public object[] Items public object[] Items => GetItems(in _chunk);
private static T[] ReadComponentArray<T>(long pData, int offsetInChunk, int count)
where T : unmanaged
{ {
get var result = new T[count];
unsafe
{
var basePtr = (byte*)pData + offsetInChunk;
var span = new Span<T>(basePtr, count);
span.CopyTo(result);
}
return result;
}
private static object[] GetItems(ref readonly Chunk chunk)
{ {
#if !(DEBUG || GHOST_EDITOR) #if !(DEBUG || GHOST_EDITOR)
return [];
#else #else
var pData = chunk.GetUnsafePtr();
var count = chunk._count;
var capacity = chunk._capacity;
var worldID = chunk._worldID;
var archetypeID = chunk._archetypeID;
if (count == 0) if (count == 0)
#endif
{ {
return []; return [];
} }
@@ -67,19 +74,18 @@ internal unsafe sealed class ChunkDebugView
var it = archetype._signature.GetIterator(); var it = archetype._signature.GetIterator();
while (it.Next(out var index)) while (it.Next(out var index))
{ {
var type = Type.GetTypeFromHandle(RuntimeTypeHandle.FromIntPtr(ComponentRegister.s_runtimeIDToTypeHandle[index])); var type = ComponentRegister.s_runtimeIDToType[index];
if (type == null) if (type == null)
{ {
continue; continue;
} }
var layout = archetype.GetLayout(index).Value; var layout = archetype.GetLayout(index).Value;
var readMethod = typeof(ChunkDebugView) var readMethod = typeof(ChunkDebugView)
.GetMethod(nameof(ReadComponentArray), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)! .GetMethod(nameof(ReadComponentArray), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)!
.MakeGenericMethod(type); .MakeGenericMethod(type);
// 3. Invoke it to get a Position[] or Velocity[] // 3. Invoke it to get a Position[] or Velocity[]
var array = readMethod.Invoke(this, [layout.offset]); var array = readMethod.Invoke(null, [(long)pData, layout.offset, count]);
if (array == null) if (array == null)
{ {
continue; continue;
@@ -90,25 +96,7 @@ internal unsafe sealed class ChunkDebugView
} }
return [.. views]; return [.. views];
} #endif
}
private T[] ReadComponentArray<T>(int offsetInChunk)
where T : unmanaged
{
var result = new T[count];
unsafe
{
var basePtr = pData + offsetInChunk;
var sizeOfT = sizeof(T);
for (int i = 0; i < count; i++)
{
// Read directly from raw memory
result[i] = Unsafe.Read<T>(basePtr + (i * sizeOfT));
}
}
return result;
} }
} }
@@ -509,7 +497,7 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable
// Only operate the swap back after the update is succeed. // Only operate the swap back after the update is succeed.
MemoryUtility.MemCpy(pRowEntity, pLastEntity, (nuint)sizeof(Entity)); MemoryUtility.MemCpy(pRowEntity, pLastEntity, (nuint)sizeof(Entity));
for (var i = 0; i <= _layouts.Count; i++) for (var i = 0; i < _layouts.Count; i++)
{ {
var layout = _layouts[i]; var layout = _layouts[i];

View File

@@ -37,7 +37,7 @@ internal static class ComponentRegister
private static readonly Dictionary<IntPtr, int> s_typeHandleToID = new(); private static readonly Dictionary<IntPtr, int> s_typeHandleToID = new();
private static readonly Dictionary<string, int> s_nameToRuntimeID = new(); private static readonly Dictionary<string, int> s_nameToRuntimeID = new();
#if DEBUG || GHOST_EDITOR #if DEBUG || GHOST_EDITOR
internal static readonly Dictionary<int, IntPtr> s_runtimeIDToTypeHandle = new(); internal static readonly Dictionary<int, Type> s_runtimeIDToType = new();
#endif #endif
public static unsafe Identifier<IComponent> GetOrRegisterComponent<T>() public static unsafe Identifier<IComponent> GetOrRegisterComponent<T>()
@@ -72,7 +72,7 @@ internal static class ComponentRegister
s_typeHandleToID[typeHandle] = newID; s_typeHandleToID[typeHandle] = newID;
s_nameToRuntimeID[stableName] = newID; s_nameToRuntimeID[stableName] = newID;
#if DEBUG || GHOST_EDITOR #if DEBUG || GHOST_EDITOR
s_runtimeIDToTypeHandle[newID.value] = typeHandle; s_runtimeIDToType[newID.value] = typeof(T);
#endif #endif
return newID; return newID;