feat(core,rendering)!: add cleanup component support, refactor render pipeline

Introduce ICleanupComponent and cleanup archetype logic in ECS. Refactor component versioning to uint. Update IResourceDatabase to use map/unmap pattern. Decouple per-frame render requests from RenderSystem via IRenderPayload. Update render pipeline and extraction system to new API.

BREAKING CHANGE: Entity destruction and render pipeline APIs have changed. IResourceDatabase.MapResource signature is updated; all callers must use map/memcpy/unmap. RenderSystem no longer manages per-frame render requests directly.
This commit is contained in:
2026-04-06 22:05:48 +09:00
parent c6bdbe0710
commit 6c96d4cf50
20 changed files with 399 additions and 200 deletions

View File

@@ -0,0 +1,8 @@
using Ghost.Entities;
namespace Ghost.Engine.Components;
public struct GPUInstanceRef : IComponent
{
public uint gpuSceneIndex;
}

View File

@@ -11,4 +11,4 @@ public struct MeshInstance : IComponent
public RenderingLayerMask renderingLayerMask;
public ShadowCastingMode shadowCastingMode;
public bool staticShadowCaster;
}
}

View File

@@ -1,145 +0,0 @@
using Ghost.Core;
using Ghost.Engine.Components;
using Ghost.Entities;
using Ghost.Graphics;
using Ghost.Graphics.Core;
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.Mathematics;
namespace Ghost.Engine.Systems;
public class RenderExtractionSystem : ISystem
{
private RenderSystem _renderSystem = null!;
private Identifier<EntityQuery> _cameraQueryID;
private Identifier<EntityQuery> _meshQueryID;
public void Initialize(ref readonly SystemAPI systemAPI)
{
_renderSystem = systemAPI.World.GetService<RenderSystem>();
var builder = new QueryBuilder();
_cameraQueryID = builder
.WithAll<Camera, LocalToWorld>()
.Build(systemAPI.World, false);
builder.Clear();
_meshQueryID = builder
.WithAll<MeshInstance, LocalToWorld>()
.Build(systemAPI.World, true);
}
public unsafe void Update(ref readonly SystemAPI systemAPI)
{
if (_meshQueryID.IsInvalid)
{
return;
}
ref var cameraQuery = ref systemAPI.World.ComponentManager.GetEntityQueryReference(_cameraQueryID);
ref var meshQuery = ref systemAPI.World.ComponentManager.GetEntityQueryReference(_meshQueryID);
foreach (var (cam, camLtw) in cameraQuery.GetComponentIterator<Camera, LocalToWorld>())
{
ref readonly var camRef = ref cam.Get();
ref readonly var camLtwRef = ref camLtw.Get();
// TODO: Classify transparent objects into a separate render list and render via oit.
var renderList = new RenderList(1, 64, Allocator.FreeList);
var transparentRenderList = new RenderList(1, 64, Allocator.FreeList);
var shadowCasterRenderList = new RenderList(1, 64, Allocator.FreeList);
// TODO: This chould be done in parallel jobs.
foreach (var chunk in meshQuery.GetChunkIterator())
{
var meshInstances = chunk.GetComponentData<MeshInstance>();
var localToWorlds = chunk.GetComponentData<LocalToWorld>();
for (var i = 0; i < chunk.EntityCount; i++)
{
ref readonly var meshInstance = ref meshInstances[i];
if ((meshInstance.renderingLayerMask & camRef.renderingLayerMask) == 0u)
{
// Not in the same rendering layer, skip.
continue;
}
ref readonly var meshLtw = ref localToWorlds[i];
var meshPosition = meshLtw.matrix.c3.xyz;
var camPosition = camLtwRef.matrix.c3.xyz;
var distance = math.distance(meshPosition, camPosition);
// TODO: Use bounding sphere or AABB for better culling. Currently it just uses the pivot point which can cause popping when the pivot is far from the actual geometry.
if (distance < camRef.nearClipPlane || distance > camRef.farClipPlane)
{
continue;
}
if (meshInstance.shadowCastingMode != ShadowCastingMode.ShadowsOnly)
{
renderList.Add(new RenderRecord
{
localToWorld = meshLtw.matrix,
mesh = meshInstance.mesh,
materialPalette = meshInstance.materialPalette,
renderingLayerMask = meshInstance.renderingLayerMask,
}, 0);
}
if (meshInstance.shadowCastingMode != ShadowCastingMode.Off)
{
shadowCasterRenderList.Add(new RenderRecord
{
localToWorld = meshLtw.matrix,
mesh = meshInstance.mesh,
materialPalette = meshInstance.materialPalette,
renderingLayerMask = meshInstance.renderingLayerMask,
}, 0);
}
}
}
var request = new RenderRequest
{
swapChainIndex = camRef.swapChainIndex,
colorTarget = camRef.colorTarget,
depthTarget = camRef.depthTarget,
opaqueRenderList = renderList,
shadowCasterRenderList = shadowCasterRenderList,
transparentRenderList = transparentRenderList,
renderFunc = camRef.renderFunc,
view = new RenderView
{
localToWorld = camLtwRef.matrix,
//viewMatrix = viewMatrix,
//projectionMatrix = projectionMatrix,
//position = camLtwRef.matrix.c3.xyz,
//frustum = frustum,
nearClipPlane = camRef.nearClipPlane,
farClipPlane = camRef.farClipPlane,
sensorSize = camRef.sensorSize,
gateFit = camRef.gateFit,
iso = camRef.iso,
shutterSpeed = camRef.shutterSpeed,
aperture = camRef.aperture,
focalLength = camRef.focalLength,
focusDistance = camRef.focusDistance,
renderingLayerMask = camRef.renderingLayerMask,
},
};
_renderSystem.AddRenderRequest(request);
}
}
public void Cleanup(ref readonly SystemAPI systemAPI)
{
}
}

View File

@@ -109,10 +109,9 @@ internal unsafe struct Chunk : IDisposable
public const int BIT_ALIGNMENT_MINUS_ONE = BIT_ALIGNMENT - 1;
private UnsafeArray<byte> _data;
private UnsafeArray<int> _versions;
private UnsafeArray<uint> _versions;
// TODO: Add structual change versioning, similar to DidOrderChange in unity ecs.
internal int _structuralVersion;
internal uint _structuralVersion;
internal int _count;
internal readonly int _capacity;
@@ -123,10 +122,10 @@ internal unsafe struct Chunk : IDisposable
internal int _archetypeID;
#endif
public Chunk(int bufferSize, int capacity, int componentCount, int globalVersion)
public Chunk(int bufferSize, int capacity, int componentCount, uint globalVersion)
{
_data = new UnsafeArray<byte>(bufferSize, Allocator.Persistent, AllocationOption.Clear);
_versions = new UnsafeArray<int>(componentCount, Allocator.Persistent);
_versions = new UnsafeArray<uint>(componentCount, Allocator.Persistent);
_capacity = capacity;
_count = 0;
@@ -141,9 +140,9 @@ internal unsafe struct Chunk : IDisposable
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly int* GetVersionUnsafePtr()
public readonly uint* GetVersionUnsafePtr()
{
return (int*)_versions.GetUnsafePtr();
return (uint*)_versions.GetUnsafePtr();
}
public void Dispose()
@@ -175,7 +174,6 @@ internal unsafe struct Archetype : IDisposable
internal UnsafeArray<ComponentMemoryLayout> _layouts;
internal UnsafeArray<int> _componentIDToLayoutIndex;
// TODO: Is hash map better?
private UnsafeList<Edge> _edgesAdd;
private UnsafeList<Edge> _edgesRemove;
@@ -187,6 +185,9 @@ internal unsafe struct Archetype : IDisposable
private int _maxComponentID;
private int _entityIdsOffset;
// -1 means no cleanup component, 0 means haven't computed yet (since 0 is the empty archetype), positive value means the archetype id of the cleanup edge.
internal int _cleanupEdge;
public readonly Identifier<Archetype> ID => _id;
public readonly Identifier<World> WorldID => _worldID;
@@ -235,10 +236,21 @@ internal unsafe struct Archetype : IDisposable
var entityAlign = (int)MemoryUtility.AlignOf<Entity>();
var components = (Span<ComponentInfo>)stackalloc ComponentInfo[componentIds.Length];
var cleanupCount = 0;
for (var i = 0; i < componentIds.Length; i++)
{
_signature.SetBit(componentIds[i]);
components[i] = ComponentRegistry.GetComponentInfo(componentIds[i]);
if (components[i].isCleanup)
{
cleanupCount++;
}
}
if (cleanupCount == 0)
{
_cleanupEdge = -1;
}
// Calculate total size per entity to get an initial capacity estimate
@@ -456,7 +468,7 @@ internal unsafe struct Archetype : IDisposable
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Error MarkChanged(int chunkIndex, int componentTypeId, int globalVersion)
public readonly Error MarkChanged(int chunkIndex, int componentTypeId, uint globalVersion)
{
var layoutResult = GetLayout(componentTypeId);
if (layoutResult.IsFailure)
@@ -471,7 +483,7 @@ internal unsafe struct Archetype : IDisposable
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Result<int, Error> GetVersion(int chunkIndex, int componentTypeId)
public readonly Result<uint, Error> GetVersion(int chunkIndex, int componentTypeId)
{
var layoutResult = GetLayout(componentTypeId);
if (layoutResult.Error != Error.None)

View File

@@ -8,6 +8,7 @@ namespace Ghost.Entities;
public interface IComponent;
public interface IEnableableComponent : IComponent;
public interface ICleanupComponent : IComponent;
[AttributeUsage(AttributeTargets.Struct)]
public class RequireComponentAttribute<T> : Attribute
@@ -24,6 +25,7 @@ internal struct ComponentInfo
public int alignment;
public bool isEnableable;
public bool isSharedWarper;
public bool isCleanup;
}
/// <summary>
@@ -69,6 +71,7 @@ internal static class ComponentRegistry
alignment = (int)MemoryUtility.AlignOf<T>(),
isEnableable = typeof(IEnableableComponent).IsAssignableFrom(type),
isSharedWarper = typeof(ISharedWarper).IsAssignableFrom(type),
isCleanup = typeof(ICleanupComponent).IsAssignableFrom(type),
};
s_registeredComponents.Add(info);

View File

@@ -85,6 +85,29 @@ public unsafe partial class EntityManager : IDisposable
return Error.NotFound;
}
private static void CopyData(ref Archetype oldArch, int oldChunk, int oldRow,
ref Archetype newArch, int newChunk, int newRow)
{
// Iterate every component space in the OLD archetype
for (var i = 0; i < oldArch._layouts.Count; i++)
{
var layout = oldArch._layouts[i];
var src = oldArch._chunks[oldChunk].GetUnsafePtr() + layout.offset + (layout.size * oldRow);
var r = newArch.GetLayout(layout.componentID);
if (r.Error != Error.None)
{
// New archetype does not have this component, skip it.
// This can happen when removing components.
continue;
}
var dst = newArch._chunks[newChunk].GetUnsafePtr() + r.Value.offset + (layout.size * newRow);
MemoryUtility.MemCpy(dst, src, (nuint)layout.size);
}
}
/// <summary>
/// Create an entity with no components.
/// </summary>
@@ -227,29 +250,20 @@ public unsafe partial class EntityManager : IDisposable
}
}
private void DestoryManagedEntityIfExists(ref readonly Archetype archetype, EntityLocation location)
{
var pManagedRef = archetype.GetComponentData(location.chunkIndex, location.rowIndex, ComponentTypeID<ManagedEntityRef>.Value);
if (pManagedRef != null)
{
DestroyManagedEntity(((ManagedEntityRef*)pManagedRef)->entity);
}
}
// private void DestoryManagedEntityIfExists(ref readonly Archetype archetype, EntityLocation location)
// {
// var pManagedRef = archetype.GetComponentData(location.chunkIndex, location.rowIndex, ComponentTypeID<ManagedEntityRef>.Value);
// if (pManagedRef != null)
// {
// DestroyManagedEntity(((ManagedEntityRef*)pManagedRef)->entity);
// }
// }
/// <summary>
/// Destroy the specified entity.
/// </summary>
/// <returns>The result status of the operation.</returns>
public Error DestroyEntity(Entity entity)
private Error DestroyEntity_Internal(Entity entity, EntityLocation location)
{
if (!_entityLocations.TryGetElementAt(entity.ID, entity.Generation, out var location))
{
return Error.NotFound;
}
ref var archetype = ref _world.ComponentManager.GetArchetypeReference(location.archetypeID);
DestoryManagedEntityIfExists(in archetype, location);
// DestoryManagedEntityIfExists(in archetype, location);
var r = archetype.RemoveEntity(location.chunkIndex, location.rowIndex);
if (r != Error.None)
{
@@ -264,27 +278,100 @@ public unsafe partial class EntityManager : IDisposable
return Error.None;
}
/// <summary>
/// Destroy the specified entity.
/// </summary>
/// <returns>The result status of the operation.</returns>
public Error DestroyEntity(Entity entity)
{
if (!_entityLocations.TryGetElementAt(entity.ID, entity.Generation, out var location))
{
return Error.NotFound;
}
ref var archetype = ref _world.ComponentManager.GetArchetypeReference(location.archetypeID);
if (archetype._cleanupEdge < 0)
{
return DestroyEntity_Internal(entity, location);
}
else
{
Identifier<Archetype> newArcID = default;
if (archetype._cleanupEdge == 0)
{
ref var signature = ref archetype._signature;
using var scope = AllocationManager.CreateStackScope();
using var newSignature = new UnsafeBitSet(signature.Count, scope.AllocationHandle);
var compCount = 0;
var it = signature.GetIterator();
while (it.Next(out var componentID))
{
if (ComponentRegistry.GetComponentInfo(componentID).isCleanup)
{
newSignature.SetBit(componentID);
compCount++;
}
}
var newSignatureHash = newSignature.GetHashCode();
newArcID = _world.ComponentManager.GetArchetypeIDBySignatureHash(newSignatureHash);
if (newArcID.IsInvalid)
{
// Create new archetype
Span<Identifier<IComponent>> componentTypeIDs = stackalloc Identifier<IComponent>[compCount];
var newIt = newSignature.GetIterator();
var i = 0;
while (newIt.Next(out var index))
{
componentTypeIDs[i++] = index;
}
newArcID = _world.ComponentManager.CreateArchetype(componentTypeIDs, newSignatureHash);
}
archetype._cleanupEdge = newArcID;
}
else
{
newArcID = archetype._cleanupEdge;
}
ref var newArchetype = ref _world.ComponentManager.GetArchetypeReference(newArcID);
newArchetype.AllocateEntity(out var newChunkIndex, out var newRowIndex);
CopyData(ref archetype, location.chunkIndex, location.rowIndex,
ref newArchetype, newChunkIndex, newRowIndex);
newArchetype.SetEntity(newChunkIndex, newRowIndex, entity);
}
return Error.None;
}
/// <summary>
/// Destroy the specified entities.
/// </summary>
/// <param name="entities">The entities to destroy.</param>
public void DestroyEntities(ReadOnlySpan<Entity> entities)
{
void RemoveManagedEntity(ReadOnlySpan<int> rowIndicesCache, ref readonly Archetype archetype, int chunkIndex)
{
for (var j = 0; j < rowIndicesCache.Length; j++)
{
var rowIndex = rowIndicesCache[j];
var location = new EntityLocation
{
archetypeID = archetype.ID,
chunkIndex = chunkIndex,
rowIndex = rowIndex
};
DestoryManagedEntityIfExists(in archetype, location);
}
}
// void RemoveManagedEntity(ReadOnlySpan<int> rowIndicesCache, ref readonly Archetype archetype, int chunkIndex)
// {
// for (var j = 0; j < rowIndicesCache.Length; j++)
// {
// var rowIndex = rowIndicesCache[j];
// var location = new EntityLocation
// {
// archetypeID = archetype.ID,
// chunkIndex = chunkIndex,
// rowIndex = rowIndex
// };
//
// DestoryManagedEntityIfExists(in archetype, location);
// }
// }
if (entities.Length == 0)
{
@@ -292,8 +379,8 @@ public unsafe partial class EntityManager : IDisposable
}
using var scope = AllocationManager.CreateStackScope();
var batchDestroy = new UnsafeList<EntityLocation>(entities.Length, scope.AllocationHandle);
var rowIndicesCache = new UnsafeList<int>(32, scope.AllocationHandle);
using var batchDestroy = new UnsafeList<EntityLocation>(entities.Length, scope.AllocationHandle);
using var rowIndicesCache = new UnsafeList<int>(32, scope.AllocationHandle);
// 1. GATHER
// Resolve all entities to their locations
@@ -335,7 +422,9 @@ public unsafe partial class EntityManager : IDisposable
ref var prevArchetype = ref _world.ComponentManager.GetArchetypeReference(prevArchetypeID);
// Remove Managed Entities first
RemoveManagedEntity(rowIndicesCache.AsSpan(), in prevArchetype, prevChunkIndex);
// RemoveManagedEntity(rowIndicesCache.AsSpan(), in prevArchetype, prevChunkIndex);
// TODO: Handle ICleanupComponent here before we remove the entities from the archetype.
// Execute the hole-filling/swap logic
prevArchetype.RemoveEntities(prevChunkIndex, rowIndicesCache.AsSpan());
@@ -355,7 +444,7 @@ public unsafe partial class EntityManager : IDisposable
{
ref var lastArchetype = ref _world.ComponentManager.GetArchetypeReference(prevArchetypeID);
RemoveManagedEntity(rowIndicesCache.AsSpan(), in lastArchetype, prevChunkIndex);
// RemoveManagedEntity(rowIndicesCache.AsSpan(), in lastArchetype, prevChunkIndex);
lastArchetype.RemoveEntities(prevChunkIndex, rowIndicesCache.AsSpan());
}
@@ -470,29 +559,6 @@ public unsafe partial class EntityManager : IDisposable
return ref *(T*)ptr; // This will return null ref if ptr is null.
}
private static void CopyData(ref Archetype oldArch, int oldChunk, int oldRow,
ref Archetype newArch, int newChunk, int newRow)
{
// Iterate every component space in the OLD archetype
for (var i = 0; i < oldArch._layouts.Count; i++)
{
var layout = oldArch._layouts[i];
var src = oldArch._chunks[oldChunk].GetUnsafePtr() + layout.offset + (layout.size * oldRow);
var r = newArch.GetLayout(layout.componentID);
if (r.Error != Error.None)
{
// New archetype does not have this component, skip it.
// This can happen when removing components.
continue;
}
var dst = newArch._chunks[newChunk].GetUnsafePtr() + r.Value.offset + (layout.size * newRow);
MemoryUtility.MemCpy(dst, src, (nuint)layout.size);
}
}
/// <summary>
/// Add a component to the specified entity.
/// </summary>
@@ -640,6 +706,12 @@ public unsafe partial class EntityManager : IDisposable
}
}
if (compCount == 0)
{
// If there is no component left, we destroy the entity directly.
return DestroyEntity_Internal(entity, location);
}
// Find or create new archetype
var newSignatureHash = newSignature.GetHashCode();
newArcID = _world.ComponentManager.GetArchetypeIDBySignatureHash(newSignatureHash);
@@ -676,11 +748,11 @@ public unsafe partial class EntityManager : IDisposable
return r;
}
var pManagedRef = oldArchetype.GetComponentData(location.chunkIndex, location.rowIndex, ComponentTypeID<ManagedEntityRef>.Value);
if (pManagedRef != null)
{
DestroyManagedEntity(((ManagedEntityRef*)pManagedRef)->entity);
}
// var pManagedRef = oldArchetype.GetComponentData(location.chunkIndex, location.rowIndex, ComponentTypeID<ManagedEntityRef>.Value);
// if (pManagedRef != null)
// {
// DestroyManagedEntity(((ManagedEntityRef*)pManagedRef)->entity);
// }
// Update location
location.archetypeID = newArcID;
@@ -833,6 +905,8 @@ public unsafe partial class EntityManager : IDisposable
maskBase[byteIndex] &= (byte)~(1 << bitIndex);
}
chunk.GetVersionUnsafePtr()[layoutResult.Value.versionIndex] = _world.Version;
return Error.None;
}

View File

@@ -90,11 +90,11 @@ public readonly unsafe ref struct ChunkView
private readonly ReadOnlyUnsafeCollection<Archetype.ComponentMemoryLayout> _layouts;
private readonly ReadOnlyUnsafeCollection<int> _layoutIndexLookup;
private readonly byte* _pChunkData;
private readonly int* _pVersion;
private readonly uint* _pVersion;
private readonly int _entityOffset;
private readonly int _entityCount;
private readonly int _structuralVersion;
private readonly int _currentVersion;
private readonly uint _structuralVersion;
private readonly uint _currentVersion;
public readonly int EntityCount => _entityCount;
@@ -168,7 +168,7 @@ public readonly unsafe ref struct ChunkView
/// <param name="id">The identifier of the component for which to retrieve the version number. Must reference a valid component.</param>
/// <returns>The version number of the specified component.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly int GetComponentVersion(Identifier<IComponent> id)
public readonly uint GetComponentVersion(Identifier<IComponent> id)
{
return _pVersion[id];
}
@@ -179,7 +179,7 @@ public readonly unsafe ref struct ChunkView
/// <typeparam name="T">The component space for which to retrieve the version. Must be an unmanaged space that implements <see cref="IComponent"/>.</typeparam>
/// <returns>The version number of the component space <typeparamref name="T"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly int GetComponentVersion<T>()
public readonly uint GetComponentVersion<T>()
where T : unmanaged, IComponent
{
return _pVersion[ComponentTypeID<T>.Value];

View File

@@ -88,7 +88,7 @@ public partial class World : IDisposable, IEquatable<World>
private readonly Dictionary<Type, object> _services;
private int _version;
private uint _version;
private bool _disposed = false;
/// <summary>
@@ -119,7 +119,7 @@ public partial class World : IDisposable, IEquatable<World>
/// <summary>
/// Gets the current version number of the world.
/// </summary>
public int Version => Interlocked.CompareExchange(ref _version, 0, 0);
public uint Version => Interlocked.CompareExchange(ref _version, 0, 0);
/// <summary>
/// Gets the main entity command buffer for this world.
@@ -172,7 +172,7 @@ public partial class World : IDisposable, IEquatable<World>
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal int AdvanceVersion()
internal uint AdvanceVersion()
{
return Interlocked.Increment(ref _version);
}

View File

@@ -401,7 +401,24 @@ internal unsafe class D3D12ResourceDatabase : IResourceDatabase
return Error.None;
}
public Error MapResource(Handle<GPUResource> handle, uint subResource, ResourceRange? readRange, ResourceRange? writeRange, void* pData, nuint size)
public void* MapResource(Handle<GPUResource> handle, uint subResource, ResourceRange? readRange)
{
var r = GetResourceRecord(handle);
if (r.IsFailure)
{
return null;
}
var resource = r.Value.ResourcePtr;
var rRange = readRange.HasValue ? new D3D12_RANGE { Begin = readRange.Value.Start, End = readRange.Value.End } : default;
void* mappedData = null;
resource.Get()->Map(subResource, readRange.HasValue ? &rRange : null, &mappedData);
return mappedData;
}
public Error UnmapResource(Handle<GPUResource> handle, uint subResource, ResourceRange? writtenRange)
{
var r = GetResourceRecord(handle);
if (r.IsFailure)
@@ -410,14 +427,9 @@ internal unsafe class D3D12ResourceDatabase : IResourceDatabase
}
var resource = r.Value.ResourcePtr;
var wRange = writtenRange.HasValue ? new D3D12_RANGE { Begin = writtenRange.Value.Start, End = writtenRange.Value.End } : default;
var rRange = readRange.HasValue ? new D3D12_RANGE { Begin = readRange.Value.Start, End = readRange.Value.End } : default;
var wRange = writeRange.HasValue ? new D3D12_RANGE { Begin = writeRange.Value.Start, End = writeRange.Value.End } : default;
void* mappedData = null;
resource.Get()->Map(subResource, readRange.HasValue ? &rRange : null, &mappedData);
MemoryUtility.MemCpy(mappedData, pData, size);
resource.Get()->Unmap(subResource, writeRange.HasValue ? &wRange : null);
resource.Get()->Unmap(subResource, writtenRange.HasValue ? &wRange : null);
return Error.None;
}

View File

@@ -33,8 +33,14 @@ public enum BindlessAccess
public unsafe interface IResourceDatabase : IDisposable
{
/// <summary>
/// Enters a parallel read section, allowing multiple threads to read from the resource database concurrently and block any write operations until all readers have exited.
/// </summary>
void EnterParallelRead();
/// <summary>
/// Exits a parallel read section, allowing write operations to proceed once all readers have exited.
/// </summary>
void ExitParallelRead();
/// <summary>
@@ -126,7 +132,30 @@ public unsafe interface IResourceDatabase : IDisposable
/// <returns>An Error indicating the success or failure of the swap operation.</returns>
Error Swap(Handle<GPUResource> handleA, Handle<GPUResource> handleB);
Error MapResource(Handle<GPUResource> handle, uint subResource, ResourceRange? readRange, ResourceRange? writeRange, void* pData, nuint size);
/// <summary>
/// Maps a subresource of a GPU resource for CPU access, specifying read and write ranges.
/// </summary>
/// <param name="handle">A handle to the GPU resource to be mapped.</param>
/// <param name="subResource">The zero-based index of the subresource to map.</param>
/// <param name="readRange">The range of the resource to be read by the CPU. Specify null to indicate read access to the entire resource.</param>
/// <returns>A pointer to the mapped subresource data, or null if the mapping operation fails.</returns>
void* MapResource(Handle<GPUResource> handle, uint subResource, ResourceRange? readRange);
/// <summary>
/// Unmaps a previously mapped subresource of a GPU resource, optionally specifying the range of data that was written by the CPU.
/// </summary>
/// <param name="handle">A handle to the GPU resource to unmap. Must reference a resource that was previously mapped.</param>
/// <param name="subResource">The zero-based index of the subresource to unmap.</param>
/// <param name="writtenRange">The range within the resource that was written to by the CPU. Specify null if no data was written or if the entire resource was modified.</param>
/// <returns>An Error value indicating the result of the operation. Returns Error.None if the resource was successfully unmapped.</returns>
Error UnmapResource(Handle<GPUResource> handle, uint subResource, ResourceRange? writtenRange);
/// <summary>
/// Gets the total size in bytes of the specified GPU resource, including all its subresources. This method is useful for determining the memory footprint of a resource and can be used for memory management and optimization purposes.
/// </summary>
/// <param name="resource">The handle to the GPU resource.</param>
/// <param name="firstSubResource">The index of the first subresource to include in the size calculation.</param>
/// <param name="numSubResources">The number of subresources to include in the size calculation.</param>
/// <returns>The total size in bytes of the specified GPU resource and its subresources.</returns>
ulong GetIntermediateResourceSize(Handle<GPUResource> resource, uint firstSubResource, uint numSubResources);
}

View File

@@ -2,6 +2,7 @@ using Ghost.Core;
using Ghost.Graphics.RHI;
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Diagnostics;
namespace Ghost.Graphics.Core;
@@ -61,7 +62,9 @@ public readonly unsafe ref struct RenderContext
{
fixed (T* pData = data)
{
ResourceDatabase.MapResource(buffer.AsResource(), 0, null, null, pData, sizeInBytes);
var mappedData = _engine.ResourceDatabase.MapResource(buffer.AsResource(), 0, null);
MemoryUtility.MemCpy(mappedData, pData, sizeInBytes);
_engine.ResourceDatabase.UnmapResource(buffer.AsResource(), 0, null);
}
}
else
@@ -81,7 +84,9 @@ public readonly unsafe ref struct RenderContext
fixed (T* pData = data)
{
ResourceDatabase.MapResource(uploadHandle.AsResource(), 0, null, null, pData, sizeInBytes);
var mappedData = _engine.ResourceDatabase.MapResource(uploadHandle.AsResource(), 0, null);
MemoryUtility.MemCpy(mappedData, pData, sizeInBytes);
_engine.ResourceDatabase.UnmapResource(uploadHandle.AsResource(), 0, null);
}
_cmd.CopyBuffer(buffer, uploadHandle, 0, 0, sizeInBytes);

View File

@@ -179,7 +179,7 @@ public struct RenderView
public RenderingLayerMask renderingLayerMask;
}
public unsafe struct RenderRequest: IDisposable
public struct RenderRequest: IDisposable
{
public RenderView view;
@@ -191,8 +191,6 @@ public unsafe struct RenderRequest: IDisposable
public RenderList transparentRenderList;
public RenderList shadowCasterRenderList;
public delegate*<ref readonly RenderContext, ref readonly RenderRequest, void> renderFunc;
public void Dispose()
{
opaqueRenderList.Dispose();

View File

@@ -0,0 +1,51 @@
using Ghost.Core;
using Ghost.Graphics.RHI;
using System.Diagnostics;
namespace Ghost.Graphics;
public unsafe class GPUScene : IDisposable
{
private readonly IResourceAllocator _resourceAllocator;
private readonly IResourceDatabase _resourceDatabase;
private Handle<GPUBuffer> _sceneBuffer;
private bool _disposed;
internal GPUScene(IResourceAllocator resourceAllocator, IResourceDatabase resourceDatabase, ulong initialCount)
{
_resourceAllocator = resourceAllocator;
_resourceDatabase = resourceDatabase;
var bufferDesc = new BufferDesc
{
Size = initialCount * (ulong)sizeof(InstanceData),
Stride = (uint)sizeof(InstanceData),
Usage = BufferUsage.Structured | BufferUsage.UnorderedAccess | BufferUsage.ShaderResource,
HeapType = HeapType.Default,
};
_sceneBuffer = _resourceAllocator.CreateBuffer(in bufferDesc, "SceneBuffer");
Debug.Assert(_sceneBuffer.IsValid, "Failed to create GPUScene buffer.");
}
~GPUScene()
{
Dispose();
}
public void Dispose()
{
if (_disposed)
{
return;
}
_resourceDatabase.ReleaseResource(_sceneBuffer.AsResource());
_disposed = true;
GC.SuppressFinalize(this);
}
}

View File

@@ -1,16 +1,15 @@
using Ghost.Core;
using Ghost.Graphics.Core;
using Ghost.Graphics.RHI;
using System.Diagnostics;
namespace Ghost.Graphics.RenderPipeline;
public interface IRenderPayload : IDisposable;
public interface IRenderPipelineSettings
{
IRenderPipeline CreatePipeline(RenderSystem renderSystem);
void CreatePipeline(RenderSystem renderSystem, out IRenderPipeline renderPipeline, out IRenderPayload renderPayload);
}
public interface IRenderPipeline : IDisposable
{
void Render(RenderContext ctx, ReadOnlySpan<RenderRequest> requests);
void Render(RenderContext ctx, int frameIndex, IRenderPayload payload);
}

View File

@@ -43,8 +43,6 @@ public class RenderSystem : IDisposable
{
private struct FrameResource : IDisposable
{
private UnsafeList<RenderRequest> _renderRequests;
public required AutoResetEvent CpuReadyEvent
{
get; init;
@@ -65,26 +63,11 @@ public class RenderSystem : IDisposable
get; set;
}
public bool CpuWriteOpen
{
get; set;
}
[UnscopedRef]
public ref UnsafeList<RenderRequest> RenderRequests => ref _renderRequests;
public void Dispose()
public readonly void Dispose()
{
CpuReadyEvent.Dispose();
GpuReadyEvent.Dispose();
CommandAllocator.Dispose();
for (var i = 0; i < _renderRequests.Count; i++)
{
_renderRequests[i].Dispose();
}
_renderRequests.Dispose();
}
}
@@ -102,8 +85,8 @@ public class RenderSystem : IDisposable
private IRenderPipelineSettings _renderPipelineSettings;
private IRenderPipeline _renderPipeline;
private IRenderPayload _renderPayload;
private uint _frameIndex;
private ulong _cpuFenceValue;
private ulong _submittedFenceValue;
@@ -118,9 +101,10 @@ public class RenderSystem : IDisposable
public ulong CPUFenceValue => _cpuFenceValue;
public ulong SubmittedFenceValue => _submittedFenceValue;
public uint FrameIndex => _frameIndex;
public uint MaxFrameLatency => _config.FrameBufferCount;
public IRenderPayload RenderPayload => _renderPayload;
public IRenderPipelineSettings RenderPipelineSettings
{
get => _renderPipelineSettings;
@@ -135,8 +119,10 @@ public class RenderSystem : IDisposable
}
_renderPipeline?.Dispose();
_renderPayload?.Dispose();
_renderPipelineSettings = value;
_renderPipeline = _renderPipelineSettings.CreatePipeline(this);
_renderPipelineSettings.CreatePipeline(this, out _renderPipeline, out _renderPayload);
}
}
@@ -180,7 +166,6 @@ public class RenderSystem : IDisposable
CpuReadyEvent = new AutoResetEvent(false),
GpuReadyEvent = new AutoResetEvent(true),
CommandAllocator = _graphicsEngine.CreateCommandAllocator(CommandBufferType.Graphics),
RenderRequests = new UnsafeList<RenderRequest>(2, Allocator.Persistent)
};
}
@@ -195,7 +180,7 @@ public class RenderSystem : IDisposable
_resizeRequest = new ConcurrentDictionary<ISwapChain, uint2>();
_renderPipelineSettings = _config.InitialRenderPipelineSettings ?? new GhostRenderPipelineSettings();
_renderPipeline = _renderPipelineSettings.CreatePipeline(this);
_renderPipelineSettings.CreatePipeline(this, out _renderPipeline, out _renderPayload);
_isRunning = false;
_disposed = false;
@@ -225,8 +210,8 @@ public class RenderSystem : IDisposable
{
try
{
_frameIndex = (uint)(_submittedFenceValue % _config.FrameBufferCount);
ref var frameResource = ref _frameResources[_frameIndex];
var frameIndex = (int)(_submittedFenceValue % _config.FrameBufferCount);
ref var frameResource = ref _frameResources[frameIndex];
// Wait for either CPU ready signal or shutdown signal
waitHandles[0] = frameResource.CpuReadyEvent;
@@ -276,15 +261,14 @@ public class RenderSystem : IDisposable
// TODO: How can we support async compute and async copy?
var cmd = _graphicsEngine.GetPooledCommandBuffer(CommandBufferType.Graphics);
ref var renderRequests = ref frameResource.RenderRequests;
try
{
cmd.Begin(frameResource.CommandAllocator);
var renderCtx = new RenderContext(_graphicsEngine, _resourceManager, cmd);
var ctx = new RenderContext(_graphicsEngine, _resourceManager, cmd);
_renderPipeline.Render(renderCtx, renderRequests.AsSpan());
_renderPipeline.Render(ctx, frameIndex, _renderPayload);
_swapChainManager.TransitionToPresent(cmd);
// End recording commands and submit
@@ -301,13 +285,6 @@ public class RenderSystem : IDisposable
finally
{
_graphicsEngine.ReturnPooledCommandBuffer(cmd);
for (var i = 0; i < renderRequests.Count; i++)
{
renderRequests[i].Dispose();
}
renderRequests.Clear();
}
_submittedFenceValue++;
@@ -361,9 +338,6 @@ public class RenderSystem : IDisposable
var eventIndex = (int)(_cpuFenceValue % _config.FrameBufferCount);
ref var frameResource = ref _frameResources[eventIndex];
Debug.Assert(frameResource.CpuWriteOpen, "SignalCPUReady called without a matching successful TryAcquireCPUFrame.");
frameResource.CpuWriteOpen = false;
frameResource.CpuReadyEvent.Set();
_cpuFenceValue++;
}
@@ -388,25 +362,9 @@ public class RenderSystem : IDisposable
var eventIndex = (int)(_cpuFenceValue % _config.FrameBufferCount);
ref var frameResource = ref _frameResources[eventIndex];
Debug.Assert(!frameResource.CpuWriteOpen, "TryAcquireCPUFrame called while the previous CPU frame is still open. Call SignalCPUReady first.");
frameResource.CpuWriteOpen = true;
frameResource.RenderRequests.Clear();
return true;
}
public void AddRenderRequest(in RenderRequest request)
{
Debug.Assert(!_disposed, "Cannot add render request to a disposed RenderSystem.");
var frameIndex = (int)(_cpuFenceValue % _config.FrameBufferCount);
ref var frameResource = ref _frameResources[frameIndex];
Debug.Assert(frameResource.CpuWriteOpen, "AddRenderRequest requires a successful TryAcquireCPUFrame and must happen before SignalCPUReady.");
frameResource.RenderRequests.Add(request);
}
public bool WaitForGPUReady(int timeOut = -1)
{
Debug.Assert(!_disposed, "Cannot wait for GPU ready on a disposed RenderSystem.");

View File

@@ -1,5 +1,6 @@
using Ghost.Core;
using Ghost.Graphics.RHI;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Diagnostics;
namespace Ghost.Graphics.Utilities;
@@ -24,7 +25,9 @@ public static unsafe class RenderingUtility
{
fixed (T* pData = data)
{
resourceDatabase.MapResource(buffer.AsResource(), 0, null, null, pData, sizeInBytes);
var mappedData = resourceDatabase.MapResource(buffer.AsResource(), 0, null);
MemoryUtility.MemCpy(mappedData, pData, sizeInBytes);
resourceDatabase.UnmapResource(buffer.AsResource(), 0, null);
}
}
else
@@ -44,7 +47,9 @@ public static unsafe class RenderingUtility
fixed (T* pData = data)
{
resourceDatabase.MapResource(uploadHandle.AsResource(), 0, null, null, pData, sizeInBytes);
var mappedData = resourceDatabase.MapResource(uploadHandle.AsResource(), 0, null);
MemoryUtility.MemCpy(mappedData, pData, sizeInBytes);
resourceDatabase.UnmapResource(uploadHandle.AsResource(), 0, null);
}
cmd.CopyBuffer(buffer, uploadHandle, 0, 0, sizeInBytes);