Major overhaul of resource barrier and state tracking in RenderGraph: - Introduce ResourceBarrierData for explicit (layout, access, sync) tracking. - Separate aliasing and transition barriers; explicit aliasing support. - Remove BufferHint; infer buffer usage from BufferUsage flags. - Update TextureAccess/BufferAccess to include usage requirements. - Improve enums (BarrierSync, BarrierAccess, BarrierLayout) for D3D12 alignment. - Update D3D12CommandBuffer to use new barrier data and error handling. - Make D3D12DescriptorHeap a class; add ReleaseSampler to IResourceDatabase. - Reset resource pools and aliasing managers each frame. - Batch and flush barriers efficiently per pass. - Update HLSL mesh shader macros to [NumThreads]. - Remove obsolete code and improve documentation. This refactor improves correctness, extensibility, and prepares for advanced features.
74 lines
2.2 KiB
C#
74 lines
2.2 KiB
C#
using Ghost.Core;
|
|
using Ghost.Graphics.RHI;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace Ghost.Graphics.RenderGraphModule;
|
|
|
|
[Flags]
|
|
internal enum BarrierFlags
|
|
{
|
|
None = 0,
|
|
FirstUsage = 1 << 0,
|
|
Discard = 1 << 1
|
|
}
|
|
|
|
/// <summary>
|
|
/// Represents a resource barrier requirement that needs to be resolved at runtime.
|
|
/// </summary>
|
|
internal struct ResourceBarrier
|
|
{
|
|
public int PassIndex;
|
|
public Identifier<RGResource> Resource;
|
|
public ResourceBarrierData TargetState;
|
|
public Identifier<RGResource> AliasingPredecessor; // Invalid if not aliasing
|
|
public BarrierFlags Flags;
|
|
|
|
public static ResourceBarrier CreateTransition(int passIndex, Identifier<RGResource> resource, ResourceBarrierData targetState, BarrierFlags flags = BarrierFlags.None)
|
|
{
|
|
return new ResourceBarrier
|
|
{
|
|
PassIndex = passIndex,
|
|
Resource = resource,
|
|
TargetState = targetState,
|
|
AliasingPredecessor = Identifier<RGResource>.Invalid,
|
|
Flags = flags
|
|
};
|
|
}
|
|
|
|
public static ResourceBarrier CreateAliasing(int passIndex, Identifier<RGResource> resource, Identifier<RGResource> predecessor, ResourceBarrierData targetState)
|
|
{
|
|
return new ResourceBarrier
|
|
{
|
|
PassIndex = passIndex,
|
|
Resource = resource,
|
|
TargetState = targetState,
|
|
AliasingPredecessor = predecessor,
|
|
Flags = BarrierFlags.FirstUsage | BarrierFlags.Discard // Aliasing implies starting fresh
|
|
};
|
|
}
|
|
|
|
public override readonly string ToString()
|
|
{
|
|
return AliasingPredecessor.IsValid
|
|
? $"[Pass {PassIndex}] Aliasing Barrier: {AliasingPredecessor.Value}->{Resource.Value} Target: {TargetState.Layout}"
|
|
: $"[Pass {PassIndex}] Barrier: {Resource.Value} Target: {TargetState.Layout}";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tracks the current state of a resource across passes during compilation.
|
|
/// </summary>
|
|
internal sealed class ResourceStateTracker
|
|
{
|
|
public int resourceIndex;
|
|
public ResourceBarrierData currentState;
|
|
public int lastAccessPass = -1;
|
|
|
|
public void Reset()
|
|
{
|
|
resourceIndex = -1;
|
|
currentState = default;
|
|
lastAccessPass = -1;
|
|
}
|
|
}
|