forked from Misaki/GhostEngine
Major overhaul of Render Graph system: - Replaced texture handles with generic Identifier<T> for unified, type-safe resource management (textures, buffers, etc.) - Refactored resource registry and pooling for performance and extensibility - Added AccessFlags and TextureAccess for precise resource usage tracking - Split passes into Raster and Compute types; introduced builder interfaces for safer pass construction - Modernized pass setup API (SetColorAttachment, UseTexture, etc.) - Updated command buffer and context structs to use new resource system - Refactored barrier and aliasing logic for improved correctness - Integrated BenchmarkDotNet for performance/memory benchmarking - Improved blackboard type safety and removed obsolete code/extensions - Added BenchmarkDotNet NuGet package These changes make the Render Graph more extensible, efficient, and ready for future resource types and advanced features.
137 lines
4.1 KiB
C#
137 lines
4.1 KiB
C#
using Ghost.Core;
|
|
|
|
namespace Ghost.RenderGraph.Concept;
|
|
|
|
/// <summary>
|
|
/// Mock command buffer for recording GPU commands.
|
|
/// In a real implementation, this would wrap D3D12 command lists.
|
|
/// </summary>
|
|
internal sealed class MockCommandBuffer
|
|
{
|
|
public void SetRenderTarget(Identifier<RGTexture> texture)
|
|
{
|
|
#if DEBUG
|
|
Console.WriteLine(nameof(SetRenderTarget) + ": " + texture);
|
|
#endif
|
|
}
|
|
|
|
public void SetDepthStencil(Identifier<RGTexture> texture)
|
|
{
|
|
#if DEBUG
|
|
Console.WriteLine(nameof(SetDepthStencil) + ": " + texture);
|
|
#endif
|
|
}
|
|
|
|
public void ClearRenderTarget(Identifier<RGTexture> texture, float r, float g, float b, float a)
|
|
{
|
|
#if DEBUG
|
|
Console.WriteLine(nameof(ClearRenderTarget) + ": " + texture);
|
|
#endif
|
|
}
|
|
|
|
public void ClearDepth(Identifier<RGTexture> texture, float depth)
|
|
{
|
|
#if DEBUG
|
|
Console.WriteLine(nameof(ClearDepth) + ": " + texture);
|
|
#endif
|
|
}
|
|
|
|
public void Draw(int vertexCount)
|
|
{
|
|
#if DEBUG
|
|
Console.WriteLine(nameof(Draw) + ": " + vertexCount);
|
|
#endif
|
|
}
|
|
|
|
public void BindShaderResource(Identifier<RGResource> resource, int slot)
|
|
{
|
|
#if DEBUG
|
|
Console.WriteLine(nameof(BindShaderResource) + ": " + resource + ", slot " + slot);
|
|
#endif
|
|
}
|
|
|
|
public void BindUnorderedAccess(Identifier<RGResource> resource, int slot)
|
|
{
|
|
#if DEBUG
|
|
Console.WriteLine(nameof(BindUnorderedAccess) + ": " + resource + ", slot " + slot);
|
|
#endif
|
|
}
|
|
|
|
public void Dispatch(int x, int y, int z)
|
|
{
|
|
#if DEBUG
|
|
Console.WriteLine(nameof(Dispatch) + ": " + x + ", " + y + ", " + z);
|
|
#endif
|
|
}
|
|
|
|
public void ResourceBarrier(Identifier<RGResource> resource, ResourceState stateBefore, ResourceState stateAfter)
|
|
{
|
|
#if DEBUG
|
|
Console.WriteLine(nameof(ResourceBarrier) + ": " + resource + " from " + stateBefore + " to " + stateAfter);
|
|
#endif
|
|
}
|
|
|
|
public void AliasBarrier(Identifier<RGResource> resourceBefore, Identifier<RGResource> resourceAfter)
|
|
{
|
|
#if DEBUG
|
|
Console.WriteLine(nameof(AliasBarrier) + ": " + resourceBefore + " to " + resourceAfter);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Context for raster rendering passes.
|
|
/// Directly exposes command buffer methods.
|
|
/// </summary>
|
|
public readonly struct RasterRenderContext
|
|
{
|
|
private readonly MockCommandBuffer _cmd;
|
|
|
|
internal RasterRenderContext(MockCommandBuffer cmd)
|
|
{
|
|
_cmd = cmd;
|
|
}
|
|
|
|
// Expose command buffer methods directly
|
|
public void SetRenderTarget(Identifier<RGTexture> texture) => _cmd.SetRenderTarget(texture);
|
|
public void SetDepthStencil(Identifier<RGTexture> texture) => _cmd.SetDepthStencil(texture);
|
|
public void ClearRenderTarget(Identifier<RGTexture> texture, float r, float g, float b, float a) => _cmd.ClearRenderTarget(texture, r, g, b, a);
|
|
public void ClearDepth(Identifier<RGTexture> texture, float depth) => _cmd.ClearDepth(texture, depth);
|
|
public void Draw(int vertexCount) => _cmd.Draw(vertexCount);
|
|
public void BindShaderResource(Identifier<RGResource> resource, int slot) => _cmd.BindShaderResource(resource, slot);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Context for compute rendering passes.
|
|
/// Directly exposes command buffer methods.
|
|
/// </summary>
|
|
public readonly struct ComputeRenderContext
|
|
{
|
|
private readonly MockCommandBuffer _cmd;
|
|
|
|
internal ComputeRenderContext(MockCommandBuffer cmd)
|
|
{
|
|
_cmd = cmd;
|
|
}
|
|
|
|
// Expose command buffer methods directly
|
|
public void BindShaderResource(Identifier<RGResource> resource, int slot) => _cmd.BindShaderResource(resource, slot);
|
|
public void BindUnorderedAccess(Identifier<RGResource> resource, int slot) => _cmd.BindUnorderedAccess(resource, slot);
|
|
public void Dispatch(int x, int y, int z) => _cmd.Dispatch(x, y, z);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unified render context containing both raster and compute contexts.
|
|
/// </summary>
|
|
internal readonly struct RenderContext
|
|
{
|
|
public readonly RasterRenderContext RasterContext;
|
|
public readonly ComputeRenderContext ComputeContext;
|
|
|
|
public RenderContext(MockCommandBuffer cmd)
|
|
{
|
|
RasterContext = new RasterRenderContext(cmd);
|
|
ComputeContext = new ComputeRenderContext(cmd);
|
|
}
|
|
}
|