Enhanced barrier

This commit is contained in:
2026-01-22 12:33:23 +09:00
parent 92b966fe0d
commit 139312d73b
35 changed files with 653 additions and 9544 deletions

View File

@@ -294,35 +294,122 @@ public struct PassDepthStencilDesc
}
[StructLayout(LayoutKind.Explicit)]
public struct BarrierSubresourceRange
{
public uint IndexOrFirstMipLevel
{
get; set;
}
public uint NumMipLevels
{
get; set;
}
public uint FirstArraySlice
{
get; set;
}
public uint NumArraySlices
{
get; set;
}
}
public struct BarrierDesc
{
public struct barrierdesc_transition
public BarrierType Type
{
public Handle<GPUResource> resource;
public ResourceState stateBefore;
public ResourceState stateAfter;
get; set;
}
public struct barrierdesc_aliasing
public BarrierSync SyncBefore
{
public Handle<GPUResource> resourceBefore;
public Handle<GPUResource> resourceAfter;
get; set;
}
public struct barrierdesc_uav
public BarrierSync SyncAfter
{
public Handle<GPUResource> resource;
get; set;
}
[FieldOffset(0)]
public BarrierType type;
[FieldOffset(4)]
public barrierdesc_transition transition;
[FieldOffset(4)]
public barrierdesc_aliasing aliasing;
[FieldOffset(4)]
public barrierdesc_uav uav;
public BarrierAccess AccessBefore
{
get; set;
}
public BarrierAccess AccessAfter
{
get; set;
}
public BarrierLayout LayoutBefore
{
get; set;
}
public BarrierLayout LayoutAfter
{
get; set;
}
public Handle<GPUResource> Resource
{
get; set;
}
public BarrierSubresourceRange Subresources
{
get; set;
}
public bool Discard
{
get; set;
}
public static BarrierDesc Global(BarrierSync syncBefore, BarrierSync syncAfter, BarrierAccess accessBefore, BarrierAccess accessAfter)
{
return new BarrierDesc
{
Type = BarrierType.Global,
SyncBefore = syncBefore,
SyncAfter = syncAfter,
AccessBefore = accessBefore,
AccessAfter = accessAfter
};
}
public static BarrierDesc Buffer(Handle<GPUResource> resource, BarrierSync syncBefore, BarrierSync syncAfter, BarrierAccess accessBefore, BarrierAccess accessAfter)
{
return new BarrierDesc
{
Type = BarrierType.Buffer,
Resource = resource,
SyncBefore = syncBefore,
SyncAfter = syncAfter,
AccessBefore = accessBefore,
AccessAfter = accessAfter
};
}
public static BarrierDesc Texture(Handle<GPUResource> resource, BarrierSync syncBefore, BarrierSync syncAfter, BarrierAccess accessBefore, BarrierAccess accessAfter, BarrierLayout layoutBefore, BarrierLayout layoutAfter, BarrierSubresourceRange subresources = default, bool discard = false)
{
return new BarrierDesc
{
Type = BarrierType.Texture,
Resource = resource,
SyncBefore = syncBefore,
SyncAfter = syncAfter,
AccessBefore = accessBefore,
AccessAfter = accessAfter,
LayoutBefore = layoutBefore,
LayoutAfter = layoutAfter,
Subresources = subresources,
Discard = discard
};
}
}
public struct ResourceDesc
@@ -574,9 +661,6 @@ public struct TextureDesc
}
}
/// <summary>
/// Describes the parameters used to configure a texture sampler for graphics rendering operations.
/// </summary>
public record struct SamplerDesc
{
public TextureFilterMode FilterMode
@@ -625,14 +709,8 @@ public record struct SamplerDesc
}
}
/// <summary>
/// Buffer description
/// </summary>
public struct BufferDesc
{
/// <summary>
/// Size of the buffer in bytes
/// </summary>
public ulong Size
{
get; set;
@@ -643,17 +721,11 @@ public struct BufferDesc
get; set;
}
/// <summary>
/// Buffer usage flags
/// </summary>
public BufferUsage Usage
{
get; set;
}
/// <summary>
/// Memory space for the buffer
/// </summary>
public ResourceMemoryType MemoryType
{
get; set;
@@ -678,22 +750,13 @@ public struct CommandError
}
}
/// <summary>
/// Swap chain description
/// </summary>
public struct SwapChainDesc
{
/// <summary>
/// Width of the swap chain
/// </summary>
public uint Width
{
get; set;
}
/// <summary>
/// Height of the swap chain
/// </summary>
public uint Height
{
get; set;
@@ -709,50 +772,29 @@ public struct SwapChainDesc
get; set;
}
/// <summary>
/// Back buffer Format
/// </summary>
public TextureFormat Format
{
get; set;
}
/// <summary>
/// Target for presentation (window handle or composition Target)
/// </summary>
public SwapChainTarget Target
{
get; set;
}
}
/// <summary>
/// Swap chain Target (window handle or composition surface)
/// </summary>
public struct SwapChainTarget
{
/// <summary>
/// Target space
/// </summary>
public SwapChainTargetType Type
{
get; set;
}
/// <summary>
/// Window handle for HWND targets
/// </summary>
public nint WindowHandle
{
get; set;
}
/// <summary>
/// Composition surface for UWP/WinUI targets
/// </summary>
public object? CompositionSurface
{
get; set;
@@ -787,11 +829,97 @@ public enum SwapChainTargetType
}
public enum BarrierType : int
public enum BarrierType
{
Transition,
Aliasing,
UAV
Global,
Texture,
Buffer
}
[Flags]
public enum BarrierSync : uint
{
None = 0x0,
All = 0x0,
Draw = 0x1,
IndexInput = 0x2,
VertexShading = 0x4,
PixelShading = 0x8,
DepthStencil = 0x10,
RenderTarget = 0x20,
ComputeShading = 0x40,
Raytracing = 0x80,
Copy = 0x100,
Resolve = 0x200,
ExecuteIndirect = 0x400,
Predication = 0x800,
AllShading = VertexShading | PixelShading | ComputeShading | Raytracing,
NonPixelShading = VertexShading | ComputeShading | Raytracing,
EmitRaytracingAccelerationStructurePostbuildInfo = 0x1000,
ClearUnorderedAccessView = 0x2000,
VideoDecode = 0x40000,
VideoProcess = 0x80000,
VideoEncode = 0x100000,
BuildRaytracingAccelerationStructure = 0x200000,
CopyRaytracingAccelerationStructure = 0x400000,
Split = 0x800000
}
[Flags]
public enum BarrierAccess : uint
{
Common = 0,
VertexBuffer = 0x1,
ConstantBuffer = 0x2,
IndexBuffer = 0x4,
RenderTarget = 0x8,
UnorderedAccess = 0x10,
DepthStencilWrite = 0x20,
DepthStencilRead = 0x40,
ShaderResource = 0x80,
StreamOutput = 0x100,
IndirectArgument = 0x200,
CopyDest = 0x400,
CopySource = 0x800,
ResolveDest = 0x1000,
ResolveSource = 0x2000,
RaytracingAccelerationStructureRead = 0x4000,
RaytracingAccelerationStructureWrite = 0x8000,
ShadingRateSource = 0x10000,
VideoDecodeRead = 0x20000,
VideoDecodeWrite = 0x40000,
VideoProcessRead = 0x80000,
VideoProcessWrite = 0x100000,
VideoEncodeRead = 0x200000,
VideoEncodeWrite = 0x400000,
NoAccess = 0x80000000
}
public enum BarrierLayout
{
Undefined = -1,
Common = 0,
Present = 0,
GenericRead = 1,
RenderTarget = 2,
UnorderedAccess = 3,
DepthStencilWrite = 4,
DepthStencilRead = 5,
ShaderResource = 6,
CopyDest = 7,
CopySource = 8,
ResolveDest = 9,
ResolveSource = 10,
ShadingRateSource = 11,
VideoDecodeRead = 12,
VideoDecodeWrite = 13,
VideoProcessRead = 14,
VideoProcessWrite = 15,
VideoEncodeRead = 16,
VideoEncodeWrite = 17,
DirectQueueCommon = 18,
ComputeQueueCommon = 19,
VideoQueueCommon = 20
}
[Flags]
@@ -948,41 +1076,15 @@ public enum ComparisonFunction
Always
}
/// <summary>
/// Specifies how to load attachment contents at the start of a render pass.
/// </summary>
public enum AttachmentLoadOp
{
/// <summary>
/// Load existing contents from memory. Use when you need to preserve previous data.
/// </summary>
Load,
/// <summary>
/// Clear the attachment to a specified value. Use when you want to start with a clean slate.
/// </summary>
Clear,
/// <summary>
/// Don't care about previous contents. Use when you'll overwrite all pixels (fullscreen pass).
/// On tile-based deferred renderers (TBDR), this can save significant memory bandwidth.
/// </summary>
DontCare
}
/// <summary>
/// Specifies how to store attachment contents at the end of a render pass.
/// </summary>
public enum AttachmentStoreOp
{
/// <summary>
/// Store the contents to memory for later use.
/// </summary>
Store,
/// <summary>
/// Discard the contents (not needed after this pass).
/// On tile-based deferred renderers (TBDR), this can save significant memory bandwidth.
/// </summary>
DontCare
}

View File

@@ -68,7 +68,7 @@ public interface ICommandBuffer : IDisposable
void ClearRenderTargetView(Handle<Texture> renderTarget, Color128 clearColor);
void ClearDepthStencilView(Handle<Texture> depthStencil, bool inlcudeDepth, bool includeStencil, float clearDepth = 1.0f, byte clearStencil = 0);
void ClearDepthStencilView(Handle<Texture> depthStencil, bool inlcludeDepth, bool includeStencil, float clearDepth = 1.0f, byte clearStencil = 0);
/// <summary>
/// Begins a render pass with the specified render Target
@@ -89,29 +89,7 @@ public interface ICommandBuffer : IDisposable
/// Inserts multiple resource barriers.
/// </summary>
/// <param name="barrierDescs">Resource barrier descriptions</param>
void ResourceBarrier(ReadOnlySpan<BarrierDesc> barrierDescs);
/// <summary>
/// Inserts a resource barrier for state transitions.
/// </summary>
/// <param name="resource">A handle to the GPU resource to transition.</param>
/// <param name="stateBefore">The current state of the resource before the transition.</param>
/// <param name="stateAfter">The desired state of the resource after the transition.</param>
void TransitionBarrier(Handle<GPUResource> resource, ResourceState stateBefore, ResourceState stateAfter);
/// <summary>
/// Inserts a resource barrier for state transitions. The current state is tracked internally.
/// </summary>
/// <param name="resource">A handle to the GPU resource to transition.</param>
/// <param name="stateAfter">The desired state of the resource after the transition.</param>
void TransitionBarrier(Handle<GPUResource> resource, ResourceState stateAfter);
/// <summary>
/// Inserts a barrier to ensure correct aliasing transitions between two GPU resources.
/// </summary>
/// <param name="resourceBefore">A handle to the GPU resource representing the state before the aliasing transition</param>
/// <param name="resourceAfter">A handle to the GPU resource representing the state after the aliasing transition</param>
void AliasBarrier(Handle<GPUResource> resourceBefore, Handle<GPUResource> resourceAfter);
void ResourceBarrier(params ReadOnlySpan<BarrierDesc> barrierDescs);
/// <summary>
/// Sets the pipeline state object
@@ -204,18 +182,16 @@ public interface ICommandBuffer : IDisposable
/// <param name="buffer">A handle to the buffer that will receive the uploaded data.</param>
/// <param name="data">A read-only span containing the data to upload to the buffer. The span must contain elements of space
/// <typeparamref name="T"/>.</param>
void UploadBuffer<T>(Handle<GraphicsBuffer> buffer, ReadOnlySpan<T> data)
void UploadBuffer<T>(Handle<GraphicsBuffer> buffer, params ReadOnlySpan<T> data)
where T : unmanaged;
/// <summary>
/// Uploads texture data to the specified texture resource starting at the given subresource index.
/// </summary>
/// <param name="texture">The texture resource to which the subresource data will be uploaded. Must be a valid, initialized texture handle.</param>
/// <param name="firstSubresource">The index of the first subresource in the texture to receive data. Must be less than the total number of subresources in the texture.</param>
/// <param name="subresources">A reference to the structure containing the subresource data to upload. The data must match the Format and layout expected by the texture.</param>
/// <param name="numSubresources">The number of subresources to upload, starting from <paramref name="firstSubresource"/>.
/// Must be greater than zero and not exceed the remaining subresources in the texture.</param>
void UploadTexture(Handle<Texture> texture, ReadOnlySpan<SubResourceData> subresources);
void UploadTexture(Handle<Texture> texture, params ReadOnlySpan<SubResourceData> subresources);
/// <summary>
/// Copies a specified number of bytes from the source graphics buffer to the destination graphics buffer.

View File

@@ -11,6 +11,20 @@ public interface IResourceReleasable
void ReleaseResource(IResourceDatabase database);
}
public struct ResourceBarrierData
{
public BarrierLayout Layout;
public BarrierAccess Access;
public BarrierSync Sync;
public ResourceBarrierData(BarrierLayout layout, BarrierAccess access, BarrierSync sync)
{
Layout = layout;
Access = access;
Sync = sync;
}
}
// TODO: Consider adding methods for resource enumeration, statistics, and bulk operations.
// TODO: Consider adding async resource loading and streaming support.
// TODO: Mesh, Material, Shader management could be separated into their own interfaces for better modularity because they are not bound to specific graphics API.
@@ -35,19 +49,19 @@ public interface IResourceDatabase : IDisposable
bool HasResource(Handle<GPUResource> handle);
/// <summary>
/// Retrieves the current state of the specified resource.
/// Retrieves the current barrier data of the specified resource.
/// </summary>
/// <param name="handle">The handle that uniquely identifies the resource whose state is to be retrieved.</param>
/// <returns>A ResourceState Value representing the current state of the resource associated with the specified handle.</returns>
Result<ResourceState, ErrorStatus> GetResourceState(Handle<GPUResource> handle);
/// <param name="handle">The handle that uniquely identifies the resource.</param>
/// <returns>A ResourceBarrierData value representing the current barrier state.</returns>
Result<ResourceBarrierData, ErrorStatus> GetResourceBarrierData(Handle<GPUResource> handle);
/// <summary>
/// Sets the state of the specified resource handle to the given Value.
/// Sets the barrier data of the specified resource handle.
/// </summary>
/// <param name="handle">The handle that identifies the resource whose state will be updated.</param>
/// <param name="state">The new state to assign to the resource represented by <paramref name="handle"/>.</param>
/// <param name="handle">The handle that identifies the resource.</param>
/// <param name="data">The new barrier data.</param>
/// <returns>An ErrorStatus indicating the success or failure of the operation.</returns>
ErrorStatus SetResourceState(Handle<GPUResource> handle, ResourceState state);
ErrorStatus SetResourceBarrierData(Handle<GPUResource> handle, ResourceBarrierData data);
/// <summary>
/// Retrieves the description of a GPU resource associated with the specified handle.