forked from Misaki/GhostEngine
refactor IRenderer
This commit is contained in:
@@ -28,6 +28,11 @@ public readonly struct Result
|
|||||||
return new Result(false, message);
|
return new Result(false, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Result Failure(ErrorStatus status)
|
||||||
|
{
|
||||||
|
return new Result(false, status.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
public static Result<T> Success<T>(T value)
|
public static Result<T> Success<T>(T value)
|
||||||
{
|
{
|
||||||
return Result<T>.Success(value);
|
return Result<T>.Success(value);
|
||||||
@@ -38,6 +43,11 @@ public readonly struct Result
|
|||||||
return Result<T>.Failure(message);
|
return Result<T>.Failure(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Result<T> Failure<T>(ErrorStatus status)
|
||||||
|
{
|
||||||
|
return Result<T>.Failure(status.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
public void Deconstruct(out bool success, out string? message)
|
public void Deconstruct(out bool success, out string? message)
|
||||||
{
|
{
|
||||||
success = IsSuccess;
|
success = IsSuccess;
|
||||||
@@ -116,7 +126,9 @@ public enum ErrorStatus : byte
|
|||||||
OutOfMemory,
|
OutOfMemory,
|
||||||
Timeout,
|
Timeout,
|
||||||
Cancelled,
|
Cancelled,
|
||||||
UnknownError
|
UnknownError,
|
||||||
|
|
||||||
|
Success = None,
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly struct Result<T, E>
|
public readonly struct Result<T, E>
|
||||||
|
|||||||
5
Ghost.Editor.Core/SceneGraph/SceneNode.cs
Normal file
5
Ghost.Editor.Core/SceneGraph/SceneNode.cs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
namespace Ghost.Editor.Core.SceneGraph;
|
||||||
|
|
||||||
|
public class SceneNode : SceneGraphNode
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -48,7 +48,6 @@ public sealed partial class GraphicsTestWindow : Window
|
|||||||
GraphicsAPI = GraphicsAPI.Direct3D12
|
GraphicsAPI = GraphicsAPI.Direct3D12
|
||||||
});
|
});
|
||||||
_renderer = _renderSystem.GraphicsEngine.CreateRenderer();
|
_renderer = _renderSystem.GraphicsEngine.CreateRenderer();
|
||||||
|
|
||||||
_swapChain = _renderSystem.GraphicsEngine.CreateSwapChain(new SwapChainDesc
|
_swapChain = _renderSystem.GraphicsEngine.CreateSwapChain(new SwapChainDesc
|
||||||
{
|
{
|
||||||
Width = (uint)AppWindow.Size.Width,
|
Width = (uint)AppWindow.Size.Width,
|
||||||
@@ -57,7 +56,7 @@ public sealed partial class GraphicsTestWindow : Window
|
|||||||
Target = SwapChainTarget.FromCompositionSurface(Panel)
|
Target = SwapChainTarget.FromCompositionSurface(Panel)
|
||||||
});
|
});
|
||||||
|
|
||||||
_renderer.SetSwapChain(_swapChain);
|
_renderer.SetRenderTarget(_swapChain.GetCurrentBackBuffer());
|
||||||
|
|
||||||
_renderSystem.Start();
|
_renderSystem.Start();
|
||||||
CompositionTarget.Rendering += OnRendering;
|
CompositionTarget.Rendering += OnRendering;
|
||||||
@@ -106,7 +105,8 @@ public sealed partial class GraphicsTestWindow : Window
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_renderer?.RequestResize(new uint2(newWidth, newHeight));
|
_renderSystem?.WaitIdle();
|
||||||
|
_swapChain?.Resize(newWidth, newHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnRendering(object? sender, object e)
|
private void OnRendering(object? sender, object e)
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ internal class D3D12GraphicsEngine : IGraphicsEngine
|
|||||||
{
|
{
|
||||||
ThrowIfDisposed();
|
ThrowIfDisposed();
|
||||||
|
|
||||||
var renderer = new D3D12Renderer(this, _resourceAllocator, _resourceDatabase);
|
var renderer = new D3D12Renderer(this, _resourceDatabase);
|
||||||
ImmutableInterlocked.Update(ref _renderers, renderers => renderers.Add(renderer));
|
ImmutableInterlocked.Update(ref _renderers, renderers => renderers.Add(renderer));
|
||||||
return renderer;
|
return renderer;
|
||||||
}
|
}
|
||||||
@@ -113,20 +113,15 @@ internal class D3D12GraphicsEngine : IGraphicsEngine
|
|||||||
return new D3D12SwapChain(_resourceDatabase, _descriptorAllocator, _device, desc, _renderSystem.MaxFrameLatency);
|
return new D3D12SwapChain(_resourceDatabase, _descriptorAllocator, _device, desc, _renderSystem.MaxFrameLatency);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RenderFrame()
|
public void RenderFrame(ICommandBuffer commandBuffer)
|
||||||
{
|
{
|
||||||
ThrowIfDisposed();
|
ThrowIfDisposed();
|
||||||
|
|
||||||
foreach (var renderer in _renderers)
|
|
||||||
{
|
|
||||||
renderer.ExecutePendingResize();
|
|
||||||
}
|
|
||||||
|
|
||||||
_copyCommandBuffer.Begin();
|
_copyCommandBuffer.Begin();
|
||||||
|
|
||||||
foreach (var renderer in _renderers)
|
foreach (var renderer in _renderers)
|
||||||
{
|
{
|
||||||
renderer.Render();
|
renderer.Render(commandBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
_copyCommandBuffer.End().ThrowIfFailed();
|
_copyCommandBuffer.End().ThrowIfFailed();
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
using Ghost.Core;
|
using Ghost.Core;
|
||||||
using Ghost.Graphics.D3D12.Utilities;
|
|
||||||
using Ghost.Graphics.RHI;
|
using Ghost.Graphics.RHI;
|
||||||
using Misaki.HighPerformance.Mathematics;
|
|
||||||
using Ghost.Graphics.Core;
|
using Ghost.Graphics.Core;
|
||||||
using Ghost.Graphics.RenderPasses;
|
using Ghost.Graphics.RenderPasses;
|
||||||
|
|
||||||
@@ -31,44 +29,27 @@ internal class D3D12Renderer : IRenderer
|
|||||||
}
|
}
|
||||||
|
|
||||||
private readonly D3D12GraphicsEngine _graphicsEngine;
|
private readonly D3D12GraphicsEngine _graphicsEngine;
|
||||||
private readonly FrameResource[] _frameResources;
|
|
||||||
private uint _frameIndex;
|
private uint _frameIndex;
|
||||||
|
|
||||||
private readonly D3D12ResourceAllocator _resourceAllocator;
|
|
||||||
private readonly D3D12ResourceDatabase _resourceDatabase;
|
private readonly D3D12ResourceDatabase _resourceDatabase;
|
||||||
|
|
||||||
private Handle<Texture> _renderTarget;
|
private Handle<Texture> _renderTarget;
|
||||||
private ISwapChain? _swapChain;
|
|
||||||
|
|
||||||
private readonly Lock _lock = new();
|
|
||||||
|
|
||||||
private uint2 _currentSize;
|
|
||||||
private uint2 _pendingSize;
|
|
||||||
private bool _resizeRequested;
|
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
|
|
||||||
|
|
||||||
// NOTE: Testing only.
|
// NOTE: Testing only.
|
||||||
private readonly MeshRenderPass _pass;
|
private readonly MeshRenderPass _pass;
|
||||||
|
|
||||||
public uint2 Size => _currentSize;
|
public Handle<Texture> RenderTarget => _renderTarget;
|
||||||
|
|
||||||
// TODO: Add render passes support
|
// TODO: Add render passes support
|
||||||
// private ImmutableArray<IRenderPass> _renderPasses;
|
// private ImmutableArray<IRenderPass> _renderPasses;
|
||||||
|
|
||||||
public D3D12Renderer(D3D12GraphicsEngine graphicsEngine, D3D12ResourceAllocator resourceAllocator, D3D12ResourceDatabase resourceDatabase)
|
public D3D12Renderer(D3D12GraphicsEngine graphicsEngine, D3D12ResourceDatabase resourceDatabase)
|
||||||
{
|
{
|
||||||
_graphicsEngine = graphicsEngine;
|
_graphicsEngine = graphicsEngine;
|
||||||
_resourceAllocator = resourceAllocator;
|
|
||||||
_resourceDatabase = resourceDatabase;
|
_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;
|
_renderTarget = Handle<Texture>.Invalid;
|
||||||
|
|
||||||
|
|
||||||
@@ -81,136 +62,36 @@ internal class D3D12Renderer : IRenderer
|
|||||||
Dispose();
|
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)
|
public void SetRenderTarget(Handle<Texture> renderTarget)
|
||||||
{
|
{
|
||||||
_swapChain = null;
|
|
||||||
|
|
||||||
_resourceDatabase.ReleaseResource(_renderTarget.AsResource());
|
_resourceDatabase.ReleaseResource(_renderTarget.AsResource());
|
||||||
_renderTarget = renderTarget;
|
_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;
|
return commandBuffer.End();
|
||||||
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++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: A proper render graph integration.
|
// 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 };
|
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);
|
_pass.Initialize(ref ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
var viewport = new ViewportDesc { Width = _currentSize.x, Height = _currentSize.y, MinDepth = 0, MaxDepth = 1 };
|
var result = _resourceDatabase.GetResourceDescription(target.AsResource());
|
||||||
var scissor = new RectDesc { Right = _currentSize.x, Bottom = _currentSize.y };
|
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.BeginRenderPass(rtDesc, depthDesc, false);
|
||||||
cmd.SetViewport(viewport);
|
cmd.SetViewport(viewport);
|
||||||
@@ -248,43 +136,8 @@ internal class D3D12Renderer : IRenderer
|
|||||||
_pass.Execute(ref ctx);
|
_pass.Execute(ref ctx);
|
||||||
|
|
||||||
cmd.EndRenderPass();
|
cmd.EndRenderPass();
|
||||||
}
|
|
||||||
|
|
||||||
private void BlitToSwapChain(Handle<Texture> source, Handle<Texture> destination, ICommandBuffer cmd)
|
return ErrorStatus.None;
|
||||||
{
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
@@ -294,23 +147,9 @@ internal class D3D12Renderer : IRenderer
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
WaitIdle();
|
|
||||||
|
|
||||||
// NOTE: Testing only.
|
// NOTE: Testing only.
|
||||||
_pass.Cleanup(_resourceDatabase);
|
_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;
|
_disposed = true;
|
||||||
|
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ namespace Ghost.Graphics.D3D12.Utilities;
|
|||||||
|
|
||||||
internal unsafe static class D3D12PipelineResource
|
internal unsafe static class D3D12PipelineResource
|
||||||
{
|
{
|
||||||
public const int BACK_BUFFER_COUNT = 2;
|
|
||||||
|
|
||||||
private readonly static D3D12_INPUT_ELEMENT_DESC[] s_inputElementDescs = [
|
private readonly static D3D12_INPUT_ELEMENT_DESC[] s_inputElementDescs = [
|
||||||
new D3D12_INPUT_ELEMENT_DESC{ SemanticName = (sbyte*)Vertex.Semantic.position.GetUnsafePointer(), SemanticIndex = 0u, Format = Vertex.Semantic.ALIGNED_FORMAT, InputSlot = 0u, AlignedByteOffset = 0u, InputSlotClass = D3D12_INPUT_CLASSIFICATION.D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, InstanceDataStepRate = 0 },
|
new D3D12_INPUT_ELEMENT_DESC{ SemanticName = (sbyte*)Vertex.Semantic.position.GetUnsafePointer(), SemanticIndex = 0u, Format = Vertex.Semantic.ALIGNED_FORMAT, InputSlot = 0u, AlignedByteOffset = 0u, InputSlotClass = D3D12_INPUT_CLASSIFICATION.D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, InstanceDataStepRate = 0 },
|
||||||
new D3D12_INPUT_ELEMENT_DESC{ SemanticName = (sbyte*)Vertex.Semantic.normal.GetUnsafePointer(), SemanticIndex = 0u, Format = Vertex.Semantic.ALIGNED_FORMAT, InputSlot = 0u, AlignedByteOffset = 16u, InputSlotClass = D3D12_INPUT_CLASSIFICATION.D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, InstanceDataStepRate = 0 },
|
new D3D12_INPUT_ELEMENT_DESC{ SemanticName = (sbyte*)Vertex.Semantic.normal.GetUnsafePointer(), SemanticIndex = 0u, Format = Vertex.Semantic.ALIGNED_FORMAT, InputSlot = 0u, AlignedByteOffset = 16u, InputSlotClass = D3D12_INPUT_CLASSIFICATION.D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, InstanceDataStepRate = 0 },
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ using Ghost.Graphics.Core;
|
|||||||
|
|
||||||
namespace Ghost.Graphics.RHI;
|
namespace Ghost.Graphics.RHI;
|
||||||
|
|
||||||
|
// TODO: Add ICommandAllocator support for thread local command buffers. We often use one allocator for multiple command buffers in a single frame.
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// D3D12-style command buffer interface for recording rendering commands
|
/// D3D12-style command buffer interface for recording rendering commands
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -51,5 +51,5 @@ public interface IGraphicsEngine : IDisposable
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Renders the current frame.
|
/// Renders the current frame.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void RenderFrame();
|
void RenderFrame(ICommandBuffer commandBuffer);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using Ghost.Core;
|
using Ghost.Core;
|
||||||
using Misaki.HighPerformance.Mathematics;
|
|
||||||
using Ghost.Graphics.Core;
|
using Ghost.Graphics.Core;
|
||||||
|
|
||||||
namespace Ghost.Graphics.RHI;
|
namespace Ghost.Graphics.RHI;
|
||||||
@@ -9,7 +8,7 @@ namespace Ghost.Graphics.RHI;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IRenderer : IDisposable
|
public interface IRenderer : IDisposable
|
||||||
{
|
{
|
||||||
public uint2 Size
|
Handle<Texture> RenderTarget
|
||||||
{
|
{
|
||||||
get;
|
get;
|
||||||
}
|
}
|
||||||
@@ -20,30 +19,10 @@ public interface IRenderer : IDisposable
|
|||||||
/// <param name="renderTarget">Render Target to render into</param>
|
/// <param name="renderTarget">Render Target to render into</param>
|
||||||
public void SetRenderTarget(Handle<Texture> renderTarget);
|
public void SetRenderTarget(Handle<Texture> renderTarget);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sets the swap chain for this renderer
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="swapChain">Swap chain for presentation</param>
|
|
||||||
public void SetSwapChain(ISwapChain? swapChain);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Executes any pending resize operations
|
|
||||||
/// </summary>
|
|
||||||
public void ExecutePendingResize();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Renders a frame
|
/// Renders a frame
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Render();
|
/// <param name="commandBuffer">Command buffer to record rendering commands into</param>
|
||||||
|
/// <returns>Result of the rendering operation</returns>
|
||||||
/// <summary>
|
public Result Render(ICommandBuffer commandBuffer);
|
||||||
/// Requests a resize operation
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="newSize">New size</param>
|
|
||||||
public void RequestResize(uint2 newSize);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Waits for the GPU to complete all work
|
|
||||||
/// </summary>
|
|
||||||
public void WaitIdle();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ public interface IFenceSynchronizer
|
|||||||
|
|
||||||
bool WaitForGPUReady(int timeOut = -1);
|
bool WaitForGPUReady(int timeOut = -1);
|
||||||
void SignalCPUReady();
|
void SignalCPUReady();
|
||||||
|
void WaitIdle();
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IRenderSystem : IFenceSynchronizer, IDisposable
|
public interface IRenderSystem : IFenceSynchronizer, IDisposable
|
||||||
@@ -68,21 +69,34 @@ public interface IRenderSystem : IFenceSynchronizer, IDisposable
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
internal class RenderSystem : IRenderSystem
|
internal class RenderSystem : IRenderSystem
|
||||||
{
|
{
|
||||||
|
// TODO: Thread local command buffers.
|
||||||
private struct FrameResource : IDisposable
|
private struct FrameResource : IDisposable
|
||||||
{
|
{
|
||||||
public readonly AutoResetEvent cpuReadyEvent;
|
public required AutoResetEvent CpuReadyEvent
|
||||||
public readonly AutoResetEvent gpuReadyEvent;
|
|
||||||
|
|
||||||
public FrameResource()
|
|
||||||
{
|
{
|
||||||
cpuReadyEvent = new AutoResetEvent(false);
|
get; init;
|
||||||
gpuReadyEvent = new AutoResetEvent(true);
|
}
|
||||||
|
|
||||||
|
public required AutoResetEvent GpuReadyEvent
|
||||||
|
{
|
||||||
|
get; init;
|
||||||
|
}
|
||||||
|
|
||||||
|
public required ICommandBuffer CommandBuffer
|
||||||
|
{
|
||||||
|
get; init;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ulong FenceValue
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly void Dispose()
|
public readonly void Dispose()
|
||||||
{
|
{
|
||||||
cpuReadyEvent.Dispose();
|
CpuReadyEvent.Dispose();
|
||||||
gpuReadyEvent.Dispose();
|
GpuReadyEvent.Dispose();
|
||||||
|
CommandBuffer.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,7 +137,12 @@ internal class RenderSystem : IRenderSystem
|
|||||||
_frameResources = new FrameResource[config.FrameBufferCount];
|
_frameResources = new FrameResource[config.FrameBufferCount];
|
||||||
for (var i = 0; i < config.FrameBufferCount; i++)
|
for (var i = 0; i < config.FrameBufferCount; i++)
|
||||||
{
|
{
|
||||||
_frameResources[i] = new FrameResource();
|
_frameResources[i] = new FrameResource
|
||||||
|
{
|
||||||
|
CpuReadyEvent = new AutoResetEvent(false),
|
||||||
|
GpuReadyEvent = new AutoResetEvent(true),
|
||||||
|
CommandBuffer = _graphicsEngine.CreateCommandBuffer(CommandBufferType.Graphics),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
_renderThread = new Thread(RenderLoop)
|
_renderThread = new Thread(RenderLoop)
|
||||||
@@ -174,7 +193,7 @@ internal class RenderSystem : IRenderSystem
|
|||||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||||
|
|
||||||
var eventIndex = (int)(_cpuFenceValue % _config.FrameBufferCount);
|
var eventIndex = (int)(_cpuFenceValue % _config.FrameBufferCount);
|
||||||
return _frameResources[eventIndex].gpuReadyEvent.WaitOne(timeOut);
|
return _frameResources[eventIndex].GpuReadyEvent.WaitOne(timeOut);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SignalCPUReady()
|
public void SignalCPUReady()
|
||||||
@@ -182,10 +201,21 @@ internal class RenderSystem : IRenderSystem
|
|||||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||||
|
|
||||||
var eventIndex = (int)(_cpuFenceValue % _config.FrameBufferCount);
|
var eventIndex = (int)(_cpuFenceValue % _config.FrameBufferCount);
|
||||||
_frameResources[eventIndex].cpuReadyEvent.Set();
|
_frameResources[eventIndex].CpuReadyEvent.Set();
|
||||||
_cpuFenceValue++;
|
_cpuFenceValue++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void WaitIdle()
|
||||||
|
{
|
||||||
|
foreach (var frameResource in _frameResources)
|
||||||
|
{
|
||||||
|
if (frameResource.FenceValue > 0)
|
||||||
|
{
|
||||||
|
_graphicsEngine.Device.GraphicsQueue.WaitForValue(frameResource.FenceValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void RenderLoop()
|
private void RenderLoop()
|
||||||
{
|
{
|
||||||
var waitHandles = new WaitHandle[] { null!, _shutdownEvent };
|
var waitHandles = new WaitHandle[] { null!, _shutdownEvent };
|
||||||
@@ -193,10 +223,10 @@ internal class RenderSystem : IRenderSystem
|
|||||||
while (_isRunning)
|
while (_isRunning)
|
||||||
{
|
{
|
||||||
_frameIndex = _gpuFenceValue % _config.FrameBufferCount;
|
_frameIndex = _gpuFenceValue % _config.FrameBufferCount;
|
||||||
var frameResource = _frameResources[_frameIndex];
|
ref var frameResource = ref _frameResources[_frameIndex];
|
||||||
|
|
||||||
// Wait for either CPU ready signal or shutdown signal
|
// Wait for either CPU ready signal or shutdown signal
|
||||||
waitHandles[0] = frameResource.cpuReadyEvent;
|
waitHandles[0] = frameResource.CpuReadyEvent;
|
||||||
var waitResult = WaitHandle.WaitAny(waitHandles);
|
var waitResult = WaitHandle.WaitAny(waitHandles);
|
||||||
|
|
||||||
// If shutdown was signaled or timeout occurred, exit the loop
|
// If shutdown was signaled or timeout occurred, exit the loop
|
||||||
@@ -208,21 +238,18 @@ internal class RenderSystem : IRenderSystem
|
|||||||
// Only proceed if CPU ready event was signaled
|
// Only proceed if CPU ready event was signaled
|
||||||
if (waitResult == 0)
|
if (waitResult == 0)
|
||||||
{
|
{
|
||||||
_graphicsEngine.RenderFrame();
|
if (frameResource.FenceValue > 0)
|
||||||
// if (result.IsFailure)
|
{
|
||||||
// {
|
_graphicsEngine.Device.GraphicsQueue.WaitForValue(frameResource.FenceValue);
|
||||||
// // Terminate the render loop on failure
|
}
|
||||||
// _isRunning = false;
|
|
||||||
//#if DEBUG
|
_graphicsEngine.RenderFrame(frameResource.CommandBuffer);
|
||||||
// throw new InvalidOperationException($"RenderFrame failed: {result.Message}");
|
|
||||||
//#else
|
|
||||||
// Logger.LogError($"RenderFrame failed: {result.Message}");
|
|
||||||
// break;
|
|
||||||
//#endif
|
|
||||||
// }
|
|
||||||
|
|
||||||
_gpuFenceValue++;
|
_gpuFenceValue++;
|
||||||
frameResource.gpuReadyEvent.Set();
|
frameResource.GpuReadyEvent.Set();
|
||||||
|
|
||||||
|
frameResource.FenceValue = _graphicsEngine.Device.GraphicsQueue.Signal(_frameIndex);
|
||||||
|
_frameIndex++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user