Refactor Render Graph: unified resources, benchmarking

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.
This commit is contained in:
2026-01-12 23:48:56 +09:00
parent 1fc9df1812
commit 954e3756aa
15 changed files with 940 additions and 776 deletions

View File

@@ -1,3 +1,6 @@
using Ghost.Core;
using Misaki.HighPerformance.Buffer;
namespace Ghost.RenderGraph.Concept;
/// <summary>
@@ -6,44 +9,76 @@ namespace Ghost.RenderGraph.Concept;
/// </summary>
internal sealed class RenderGraphObjectPool
{
private readonly Dictionary<Type, Stack<object>> _pools = new();
private static readonly List<SharedObjectPoolBase> s_allocatedPools = new();
public T Get<T>() where T : class, new()
private class SharedObjectPoolBase
{
var type = typeof(T);
if (_pools.TryGetValue(type, out var pool) && pool.Count > 0)
{
return (T)pool.Pop();
}
return new T();
public SharedObjectPoolBase() { }
public virtual void Clear() { }
}
public void Release<T>(T obj) where T : class
private class SharedObjectPool<T> : SharedObjectPoolBase where T : class, new()
{
if (obj == null) return;
private static readonly ObjectPool<T> s_pool = AllocatePool();
var type = typeof(T);
if (!_pools.TryGetValue(type, out var pool))
private static ObjectPool<T> AllocatePool()
{
pool = new Stack<object>(16);
_pools[type] = pool;
var newPool = new ObjectPool<T>(() => new T());
// Storing instance to clear the static pool of the same type if needed
s_allocatedPools.Add(new SharedObjectPool<T>());
return newPool;
}
pool.Push(obj);
/// <summary>
/// Clear the pool using SharedObjectPool instance.
/// </summary>
/// <returns></returns>
public override void Clear()
{
s_pool.Reset();
}
/// <summary>
/// Rent a new instance from the pool.
/// </summary>
/// <returns></returns>
public static T Rent() => s_pool.Rent();
/// <summary>
/// Return an object to the pool.
/// </summary>
/// <param name="toRelease">instance to release.</param>
public static void Return(T toRelease) => s_pool.Return(toRelease);
}
public T Rent<T>()
where T : class, new()
{
return SharedObjectPool<T>.Rent();
}
public void Return<T>(T obj)
where T : class, new()
{
SharedObjectPool<T>.Return(obj);
}
public void Clear()
{
_pools.Clear();
for (var i = 0; i < s_allocatedPools.Count; i++)
{
s_allocatedPools[i].Clear();
}
}
}
/// <summary>
/// Represents a texture resource in the render graph.
/// </summary>
internal sealed class TextureResource
internal sealed class RenderGraphResource
{
public RenderGraphResourceType type;
public int Index;
public int Version;
public TextureDescriptor Descriptor;
public bool IsImported;
public int FirstUsePass = -1;
@@ -55,7 +90,6 @@ internal sealed class TextureResource
public void Reset()
{
Index = -1;
Version = 0;
Descriptor = default;
IsImported = false;
FirstUsePass = -1;
@@ -72,7 +106,7 @@ internal sealed class TextureResource
/// </summary>
internal sealed class RenderGraphResourceRegistry
{
private readonly List<TextureResource> _textureResources = new(64);
private readonly List<RenderGraphResource> _resources = new(64);
private readonly RenderGraphObjectPool _pool = new();
private int _textureResourceCount;
@@ -85,76 +119,78 @@ internal sealed class RenderGraphResourceRegistry
_textureResourceCount = 0;
}
public RenderGraphTextureHandle ImportTexture(TextureDescriptor descriptor)
public Identifier<RGTexture> ImportTexture(TextureDescriptor descriptor)
{
var resource = GetOrCreateTextureResource();
resource.Index = _textureResourceCount - 1;
resource.Version = 0;
resource.Descriptor = descriptor;
resource.IsImported = true;
return new RenderGraphTextureHandle(resource.Index, resource.Version, descriptor.Name);
return new Identifier<RGTexture>(resource.Index);
}
public RenderGraphTextureHandle CreateTexture(TextureDescriptor descriptor)
public Identifier<RGTexture> CreateTexture(TextureDescriptor descriptor)
{
var resource = GetOrCreateTextureResource();
resource.Index = _textureResourceCount - 1;
resource.Version = 0;
resource.Descriptor = descriptor;
resource.IsImported = false;
return new RenderGraphTextureHandle(resource.Index, resource.Version, descriptor.Name);
return new Identifier<RGTexture>(resource.Index);
}
public TextureResource GetTextureResource(RenderGraphTextureHandle handle)
public RenderGraphResource GetResource(Identifier<RGResource> resource)
{
if (handle.Index < 0 || handle.Index >= _textureResourceCount)
throw new ArgumentException($"Invalid texture handle: {handle.Index}");
if (resource.Value < 0 || resource.Value >= _textureResourceCount)
throw new ArgumentException($"Invalid texture handle: {resource}");
return _textureResources[handle.Index];
return _resources[resource.Value];
}
public TextureResource GetTextureResourceByIndex(int index)
public RenderGraphResource GetTextureResourceByIndex(int index)
{
if (index < 0 || index >= _textureResourceCount)
throw new ArgumentException($"Invalid texture index: {index}");
return _textureResources[index];
return _resources[index];
}
public void SetProducer(RenderGraphTextureHandle handle, int passIndex)
public void SetProducer(Identifier<RGResource> resourceID, int passIndex)
{
var resource = GetTextureResource(handle);
var resource = GetResource(resourceID);
resource.ProducerPass = passIndex;
if (resource.FirstUsePass < 0)
{
resource.FirstUsePass = passIndex;
}
}
public void AddConsumer(RenderGraphTextureHandle handle, int passIndex)
public void AddConsumer(Identifier<RGResource> resourceID, int passIndex)
{
var resource = GetTextureResource(handle);
var resource = GetResource(resourceID);
resource.ConsumerPasses.Add(passIndex);
resource.LastUsePass = passIndex;
if (resource.FirstUsePass < 0)
{
resource.FirstUsePass = passIndex;
}
}
private TextureResource GetOrCreateTextureResource()
private RenderGraphResource GetOrCreateTextureResource()
{
TextureResource resource;
if (_textureResourceCount < _textureResources.Count)
RenderGraphResource resource;
if (_textureResourceCount < _resources.Count)
{
// Reuse existing slot
resource = _textureResources[_textureResourceCount];
resource = _resources[_textureResourceCount];
resource.Reset();
}
else
{
// Need to grow the list
resource = _pool.Get<TextureResource>();
resource = _pool.Rent<RenderGraphResource>();
resource.Reset();
_textureResources.Add(resource);
_resources.Add(resource);
}
_textureResourceCount++;
@@ -163,11 +199,11 @@ internal sealed class RenderGraphResourceRegistry
public void Clear()
{
for (int i = 0; i < _textureResources.Count; i++)
for (var i = 0; i < _resources.Count; i++)
{
_pool.Release(_textureResources[i]);
_pool.Return(_resources[i]);
}
_textureResources.Clear();
_resources.Clear();
_textureResourceCount = 0;
}
}