Refactoring rendering system.

Added new IRenderSystem and IFenceSynchronizer

Changed IRenderer managment from RenderSystem to IGraphicsEngine
This commit is contained in:
2025-11-07 16:46:21 +09:00
parent 15aca9aefb
commit 56f73e774b
8 changed files with 216 additions and 94 deletions

View File

@@ -1,3 +1,6 @@
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using Ghost.Graphics.RHI;
namespace Ghost.Graphics.D3D12;
@@ -11,18 +14,20 @@ internal unsafe class D3D12GraphicsEngine : IGraphicsEngine
private readonly D3D12RenderDevice _device;
private readonly D3D12PipelineLibrary _pipelineLibrary;
private readonly D3D12DescriptorAllocator _descriptorAllocator;
private readonly D3D12ResourceDatabase _resourceDatabase;
private readonly D3D12ResourceAllocator _resourceAllocator;
private readonly D3D12CommandBuffer _copyCommandBuffer;
private ImmutableArray<IRenderer> _renderers;
private bool _disposed;
public IRenderDevice Device => _device;
public IPipelineLibrary PipelineLibrary => _pipelineLibrary;
public IResourceDatabase ResourceDatabase => _resourceDatabase;
public IResourceAllocator ResourceAllocator => _resourceAllocator;
public D3D12GraphicsEngine(RenderSystem renderSystem)
public D3D12GraphicsEngine(IRenderSystem renderSystem)
{
#if DEBUG
_debugLayer = new();
@@ -41,6 +46,8 @@ internal unsafe class D3D12GraphicsEngine : IGraphicsEngine
_resourceAllocator,
_descriptorAllocator,
CommandBufferType.Copy);
_renderers = ImmutableArray<IRenderer>.Empty;
}
~D3D12GraphicsEngine()
@@ -48,13 +55,40 @@ internal unsafe class D3D12GraphicsEngine : IGraphicsEngine
Dispose();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ThrowIfDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(D3D12GraphicsEngine));
}
}
public IRenderer CreateRenderer()
{
return new D3D12Renderer(this, _resourceAllocator, _resourceDatabase);
ThrowIfDisposed();
var renderer = new D3D12Renderer(this, _resourceAllocator, _resourceDatabase);
ImmutableInterlocked.Update(ref _renderers, renderers => renderers.Add(renderer));
return renderer;
}
public void RemoveRenderer(IRenderer renderer)
{
ThrowIfDisposed();
ImmutableInterlocked.Update(ref _renderers, renderers => renderers.Remove(renderer));
}
public void ClearRenderers()
{
ThrowIfDisposed();
ImmutableInterlocked.Update(ref _renderers, renderers => renderers.Clear());
}
public ICommandBuffer CreateCommandBuffer(CommandBufferType type = CommandBufferType.Graphics)
{
ThrowIfDisposed();
return new D3D12CommandBuffer(
_device,
_pipelineLibrary,
@@ -66,21 +100,43 @@ internal unsafe class D3D12GraphicsEngine : IGraphicsEngine
public ISwapChain CreateSwapChain(SwapChainDesc desc)
{
ThrowIfDisposed();
return new D3D12SwapChain(_resourceDatabase, _device.DXGIFactory, ((D3D12CommandQueue)_device.ComputeQueue).NativeQueue, desc);
}
public void BeginFrame()
{
throw new NotImplementedException();
ThrowIfDisposed();
foreach (var renderer in _renderers)
{
renderer.ExecutePendingResize();
}
}
public void RenderFrame()
{
ThrowIfDisposed();
foreach (var renderer in _renderers)
{
renderer.Render();
}
}
public void EndFrame()
{
throw new NotImplementedException();
ThrowIfDisposed();
_resourceAllocator.ReleaseTempResources();
}
public void Dispose()
{
if (_disposed)
{
return;
}
_copyCommandBuffer.Dispose();
_pipelineLibrary.Dispose();
@@ -93,6 +149,7 @@ internal unsafe class D3D12GraphicsEngine : IGraphicsEngine
_debugLayer.Dispose();
#endif
_disposed = true;
GC.SuppressFinalize(this);
}
}