Render extraction system & ECS/graphics refactor

Introduced RenderExtractionSystem for entity-based render data extraction. Added MeshInstance and MeshPalette components with shadow casting support. Refactored QueryBuilder API, SharedComponentStore, and component registration for clarity and flexibility. Updated SystemManager and SystemGroup to use SystemAPI. Replaced RenderingConfig with GraphicsEngineDesc/RenderSystemDesc. RenderFrame now uses CPU/GPU fence values for sync. Removed Camera.cs in favor of ECS-based rendering. Improved Material, RenderingLayerMask, Mesh, and RenderList APIs. Updated package references and fixed naming, error handling, and disposal issues.
This commit is contained in:
2026-03-08 22:51:03 +09:00
parent bfe8588d76
commit 619720feee
26 changed files with 493 additions and 269 deletions

View File

@@ -7,23 +7,20 @@ using Ghost.Graphics.Core;
using Ghost.Graphics.RHI;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Ghost.Graphics.D3D12;
public static class D3D12GraphicsEngineFactory
{
public static IGraphicsEngine Create(IFenceSynchronizer fenceSynchronizer)
public static IGraphicsEngine Create(GraphicsEngineDesc desc)
{
return new D3D12GraphicsEngine(fenceSynchronizer);
return new D3D12GraphicsEngine(desc);
}
}
internal class D3D12GraphicsEngine : IGraphicsEngine
{
private GCHandle _thisHandle;
private readonly IFenceSynchronizer _fenceSynchronizer;
private readonly GraphicsEngineDesc _desc;
#if ENABLE_DEBUG
private readonly D3D12DebugLayer _debugLayer;
@@ -45,9 +42,9 @@ internal class D3D12GraphicsEngine : IGraphicsEngine
public IResourceDatabase ResourceDatabase => _resourceDatabase;
public IResourceAllocator ResourceAllocator => _resourceAllocator;
public D3D12GraphicsEngine(IFenceSynchronizer fenceSynchronizer)
public D3D12GraphicsEngine(GraphicsEngineDesc desc)
{
_fenceSynchronizer = fenceSynchronizer;
_desc = desc;
#if ENABLE_DEBUG
_debugLayer = new D3D12DebugLayer();
@@ -56,7 +53,7 @@ internal class D3D12GraphicsEngine : IGraphicsEngine
_shaderCompiler = new DxcShaderCompiler();
_descriptorAllocator = new D3D12DescriptorAllocator(_device);
_resourceDatabase = new D3D12ResourceDatabase(_fenceSynchronizer, _descriptorAllocator);
_resourceDatabase = new D3D12ResourceDatabase(_descriptorAllocator);
_pipelineLibrary = new D3D12PipelineLibrary(_device, _resourceDatabase);
_resourceAllocator = new D3D12ResourceAllocator(_device, _descriptorAllocator, _resourceDatabase, _pipelineLibrary);
@@ -118,15 +115,19 @@ internal class D3D12GraphicsEngine : IGraphicsEngine
public ISwapChain CreateSwapChain(SwapChainDesc desc)
{
ThrowIfDisposed();
return new D3D12SwapChain(_resourceDatabase, _descriptorAllocator, _device, desc, _fenceSynchronizer.MaxFrameLatency);
return new D3D12SwapChain(_resourceDatabase, _descriptorAllocator, _device, desc, _desc.FrameBufferCount);
}
public Result RenderFrame(ICommandAllocator commandAllocator)
public Result RenderFrame(ICommandAllocator commandAllocator, uint cpuFenceValue, uint gpuFenceValue)
{
ThrowIfDisposed();
var r = Result.Success();
_resourceDatabase.BeginFrame(cpuFenceValue);
// TODO: We should not handle renderers in graphics engine since the purpose of graphics engine is to provide low-level graphics resource management and command buffer creation.
// We need to migrate this to IRenderPipeline instead when it's ready.
foreach (var renderer in _renderers)
{
r = renderer.Render(commandAllocator);
@@ -136,7 +137,7 @@ internal class D3D12GraphicsEngine : IGraphicsEngine
}
}
_resourceDatabase.EndFrame();
_resourceDatabase.EndFrame(gpuFenceValue);
return r;
}
@@ -166,8 +167,6 @@ internal class D3D12GraphicsEngine : IGraphicsEngine
_debugLayer.Dispose();
#endif
_thisHandle.Free();
_disposed = true;
GC.SuppressFinalize(this);
}

View File

@@ -864,7 +864,7 @@ internal sealed unsafe partial class D3D12ResourceAllocator : IResourceAllocator
_device.NativeDevice.Get()->CreateSampler(&samplerDesc, cpuHandle);
_descriptorAllocator.CopyToShaderVisible(samplerDescriptor);
return _resourceDatabase.CreateSampler(in desc, samplerDescriptor.Value);
return _resourceDatabase.AddSampler(in desc, samplerDescriptor.Value);
}
public void Dispose()

View File

@@ -96,9 +96,9 @@ internal class D3D12ResourceDatabase : IResourceDatabase
}
}
private readonly IFenceSynchronizer _fenceSynchronizer;
private readonly D3D12DescriptorAllocator _descriptorAllocator;
// TODO: Change AOS to SOA? Does it even matter since we mostly access resources by handle which is essentially random access?
private UnsafeSlotMap<ResourceRecord> _resources;
private UnsafeHashMap<SamplerDesc, Identifier<Sampler>> _samplers;
#if DEBUG || GHOST_EDITOR
@@ -107,11 +107,11 @@ internal class D3D12ResourceDatabase : IResourceDatabase
private UnsafeQueue<ReleaseEntry> _releaseQueue;
private uint _currentFrameFenceValue;
private bool _disposed;
public D3D12ResourceDatabase(IFenceSynchronizer fenceSynchronizer, D3D12DescriptorAllocator descriptorAllocator)
public D3D12ResourceDatabase(D3D12DescriptorAllocator descriptorAllocator)
{
_fenceSynchronizer = fenceSynchronizer;
_descriptorAllocator = descriptorAllocator;
_resources = new UnsafeSlotMap<ResourceRecord>(64, Allocator.Persistent, AllocationOption.Clear);
@@ -287,7 +287,7 @@ internal class D3D12ResourceDatabase : IResourceDatabase
return;
}
var entry = new ReleaseEntry(record, _fenceSynchronizer.CPUFenceValue);
var entry = new ReleaseEntry(record, _currentFrameFenceValue);
_releaseQueue.Enqueue(entry);
_resources.Remove(handle.ID, handle.Generation);
@@ -311,7 +311,7 @@ internal class D3D12ResourceDatabase : IResourceDatabase
_resources.Remove(handle.ID, handle.Generation);
}
public Identifier<Sampler> CreateSampler(ref readonly SamplerDesc desc, int id)
public Identifier<Sampler> AddSampler(ref readonly SamplerDesc desc, int id)
{
ObjectDisposedException.ThrowIf(_disposed, this);
@@ -341,14 +341,20 @@ internal class D3D12ResourceDatabase : IResourceDatabase
_descriptorAllocator.Release(new Identifier<SamplerDescriptor>(id.Value));
}
public void EndFrame()
public void BeginFrame(uint currentFrameFenceValue)
{
ObjectDisposedException.ThrowIf(_disposed, this);
_currentFrameFenceValue = currentFrameFenceValue;
}
public void EndFrame(uint completedFenceValue)
{
ObjectDisposedException.ThrowIf(_disposed, this);
while (_releaseQueue.Count > 0)
{
var toRelease = _releaseQueue.Peek();
if (toRelease.fenceValue > _fenceSynchronizer.GPUFenceValue)
if (toRelease.fenceValue > completedFenceValue)
{
break;
}