refactor IRenderer

This commit is contained in:
2025-12-22 15:59:02 +09:00
parent 2881fda112
commit d23e701f0a
11 changed files with 112 additions and 255 deletions

View File

@@ -1,7 +1,5 @@
using Ghost.Core;
using Ghost.Graphics.D3D12.Utilities;
using Ghost.Graphics.RHI;
using Misaki.HighPerformance.Mathematics;
using Ghost.Graphics.Core;
using Ghost.Graphics.RenderPasses;
@@ -31,44 +29,27 @@ internal class D3D12Renderer : IRenderer
}
private readonly D3D12GraphicsEngine _graphicsEngine;
private readonly FrameResource[] _frameResources;
private uint _frameIndex;
private readonly D3D12ResourceAllocator _resourceAllocator;
private readonly D3D12ResourceDatabase _resourceDatabase;
private Handle<Texture> _renderTarget;
private ISwapChain? _swapChain;
private readonly Lock _lock = new();
private uint2 _currentSize;
private uint2 _pendingSize;
private bool _resizeRequested;
private bool _disposed;
// NOTE: Testing only.
private readonly MeshRenderPass _pass;
public uint2 Size => _currentSize;
public Handle<Texture> RenderTarget => _renderTarget;
// TODO: Add render passes support
// private ImmutableArray<IRenderPass> _renderPasses;
public D3D12Renderer(D3D12GraphicsEngine graphicsEngine, D3D12ResourceAllocator resourceAllocator, D3D12ResourceDatabase resourceDatabase)
public D3D12Renderer(D3D12GraphicsEngine graphicsEngine, D3D12ResourceDatabase resourceDatabase)
{
_graphicsEngine = graphicsEngine;
_resourceAllocator = resourceAllocator;
_resourceDatabase = resourceDatabase;
// Create frame resources for double buffering
_frameResources = new FrameResource[D3D12PipelineResource.BACK_BUFFER_COUNT];
for (var i = 0; i < _frameResources.Length; i++)
{
_frameResources[i] = new FrameResource(graphicsEngine, i);
}
_renderTarget = Handle<Texture>.Invalid;
@@ -81,136 +62,36 @@ internal class D3D12Renderer : IRenderer
Dispose();
}
private void CreateOffScreenRenderTarget(uint width, uint height)
{
var desc = RenderTargetDesc.Color(width, height, 1, TextureFormat.R8G8B8A8_UNorm);
_renderTarget = _resourceAllocator.CreateRenderTarget(in desc);
}
public void SetRenderTarget(Handle<Texture> renderTarget)
{
_swapChain = null;
_resourceDatabase.ReleaseResource(_renderTarget.AsResource());
_renderTarget = renderTarget;
}
public void SetSwapChain(ISwapChain? swapChain)
public Result Render(ICommandBuffer commandBuffer)
{
if (_swapChain != null)
if (!_renderTarget.IsValid)
{
_resourceDatabase.ReleaseResource(_renderTarget.AsResource());
return Result.Failure("Render target is not set.");
}
if (swapChain != null)
commandBuffer.Begin();
// NOTE: Temperary solution: render directly to the swap chain back buffer if available.
// HACK: This is hard coded for testing purposes only.
var error = RenderScene(_renderTarget, commandBuffer);
if (error != ErrorStatus.None)
{
CreateOffScreenRenderTarget(swapChain.Width, swapChain.Height);
commandBuffer.End();
return Result.Failure(error);
}
var newSize = swapChain != null ? new uint2(swapChain.Width, swapChain.Height) : _currentSize;
if (!math.all(newSize == _currentSize))
{
RequestResize(newSize);
}
_swapChain = swapChain;
}
public void RequestResize(uint2 newSize )
{
lock (_lock)
{
if (math.all(_pendingSize == newSize))
{
return;
}
_resizeRequested = true;
_pendingSize = newSize;
}
}
public void ExecutePendingResize()
{
if (!_resizeRequested)
{
return;
}
uint2 newSize;
lock (_lock)
{
newSize = _pendingSize;
_resizeRequested = false;
}
// Wait for GPU to complete
WaitIdle();
// Resize swap chain if present
_swapChain?.Resize(newSize.x, newSize.y);
_currentSize = newSize;
// Update off-screen render Target size
if (_swapChain != null)
{
_resourceDatabase.ReleaseResource(_renderTarget.AsResource());
CreateOffScreenRenderTarget(newSize.x, newSize.y);
}
}
public void Render()
{
ExecutePendingResize();
var frameIndex = _frameIndex % (uint)_frameResources.Length;
ref var frame = ref _frameResources[frameIndex];
if (frame.fenceValue > 0)
{
_graphicsEngine.Device.GraphicsQueue.WaitForValue(frame.fenceValue);
}
if (_renderTarget.IsValid)
{
frame.commandBuffer.Begin();
// NOTE: Temperary solution: render directly to the swap chain back buffer if available.
// HACK: This is hard coded for testing purposes only.
var rt = _swapChain?.GetCurrentBackBuffer() ?? _renderTarget;
if(_swapChain != null)
{
frame.commandBuffer.ResourceBarrier(rt.AsResource(), ResourceState.Present, ResourceState.RenderTarget);
}
RenderScene(rt, frame.commandBuffer);
if (_swapChain != null)
{
frame.commandBuffer.ResourceBarrier(rt.AsResource(), ResourceState.RenderTarget, ResourceState.Present);
}
// if (_swapChain != null)
// {
// var backBufferRT = _swapChain.GetCurrentBackBuffer();
// BlitToDestination(_renderTarget, backBufferRT, frame.commandBuffer);
// }
frame.commandBuffer.End().ThrowIfFailed();
_graphicsEngine.Device.GraphicsQueue.Submit(frame.commandBuffer);
_swapChain?.Present();
}
frame.fenceValue = _graphicsEngine.Device.GraphicsQueue.Signal(_frameIndex);
_frameIndex++;
return commandBuffer.End();
}
// TODO: A proper render graph integration.
private void RenderScene(Handle<Texture> target, ICommandBuffer cmd)
private ErrorStatus RenderScene(Handle<Texture> target, ICommandBuffer cmd)
{
var clearColor = new Color128 { r = 1.0f, g = 0.0f, b = 1.0f, a = 1.0f };
@@ -237,8 +118,15 @@ internal class D3D12Renderer : IRenderer
_pass.Initialize(ref ctx);
}
var viewport = new ViewportDesc { Width = _currentSize.x, Height = _currentSize.y, MinDepth = 0, MaxDepth = 1 };
var scissor = new RectDesc { Right = _currentSize.x, Bottom = _currentSize.y };
var result = _resourceDatabase.GetResourceDescription(target.AsResource());
if (result.IsFailure)
{
return result.Error;
}
var texDesc = result.Value.TextureDescription;
var viewport = new ViewportDesc { Width = texDesc.Width, Height = texDesc.Height, MinDepth = 0, MaxDepth = 1 };
var scissor = new RectDesc { Right = texDesc.Width, Bottom = texDesc.Height };
cmd.BeginRenderPass(rtDesc, depthDesc, false);
cmd.SetViewport(viewport);
@@ -248,43 +136,8 @@ internal class D3D12Renderer : IRenderer
_pass.Execute(ref ctx);
cmd.EndRenderPass();
}
private void BlitToSwapChain(Handle<Texture> source, Handle<Texture> destination, ICommandBuffer cmd)
{
// Handle swap chain back buffer transitions if needed
if (_swapChain != null)
{
// Transition back buffer to render Target
cmd.ResourceBarrier(destination.AsResource(), ResourceState.Present, ResourceState.RenderTarget);
}
// For now, we'll do a simple copy operation
// In a real implementation, you would use a blit shader for post-processing
// FIX: Implement proper blit operation with shader
// This is a placeholder - in D3D12, you would typically:
// 1. Set render Target to the destination
// 2. Use a full-screen quad/triangle with a shader that samples from the source
// Handle swap chain back buffer transitions if needed
if (_swapChain != null)
{
// Transition back buffer to present
cmd.ResourceBarrier(destination.AsResource(), ResourceState.RenderTarget, ResourceState.Present);
}
}
public void WaitIdle()
{
// Wait for all frame resources to complete
foreach (ref var frame in _frameResources.AsSpan())
{
if (frame.fenceValue > 0)
{
_graphicsEngine.Device.GraphicsQueue.WaitForValue(frame.fenceValue);
}
}
return ErrorStatus.None;
}
public void Dispose()
@@ -294,23 +147,9 @@ internal class D3D12Renderer : IRenderer
return;
}
WaitIdle();
// NOTE: Testing only.
_pass.Cleanup(_resourceDatabase);
// If using a swap chain, release the off-screen render Target.
// Otherwise, the render Target is managed externally.
if (_swapChain != null)
{
_resourceDatabase.ReleaseResource(_renderTarget.AsResource());
}
foreach (ref var frame in _frameResources.AsSpan())
{
frame.Dispose();
}
_disposed = true;
GC.SuppressFinalize(this);