diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs
index 9e52abec..d79cf59a 100644
--- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs
+++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs
@@ -1,5 +1,7 @@
using System;
using TerraFX.Interop.DirectX;
+using static TerraFX.Interop.DirectX.D3D12_RESOURCE_BARRIER_FLAGS;
+using static TerraFX.Interop.DirectX.D3D12_RESOURCE_BARRIER_TYPE;
namespace ComputeSharp.Graphics.Extensions;
@@ -195,7 +197,7 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions
/// The to change state for.
/// The starting value for the transition.
/// The destnation value for the transition.
- public static void ResourceBarrier(
+ public static void TransitioneBarrier(
this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList,
ID3D12Resource* d3D12Resource,
D3D12_RESOURCE_STATES d3D12ResourceStatesBefore,
@@ -206,6 +208,48 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions
d3D12GraphicsCommandList.ResourceBarrier(1, &d3D12ResourceBarrier);
}
+ ///
+ /// Inserts a UAV resource barrier for a specific resource.
+ ///
+ /// The instance in use.
+ /// The to insert the barrier for.
+ public static void UAVBarrier(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ID3D12Resource* d3D12Resource)
+ {
+ D3D12_RESOURCE_BARRIER d3D12ResourceBarrier = D3D12_RESOURCE_BARRIER.InitUAV(d3D12Resource);
+
+ d3D12GraphicsCommandList.ResourceBarrier(1, &d3D12ResourceBarrier);
+ }
+
+ ///
+ /// Starts a UAV resource barrier for a specific resource.
+ ///
+ /// The instance in use.
+ /// The to start the barrier for.
+ public static void StartUAVBarrier(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ID3D12Resource* d3D12Resource)
+ {
+ D3D12_RESOURCE_BARRIER d3D12ResourceBarrier = default;
+ d3D12ResourceBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
+ d3D12ResourceBarrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_BEGIN_ONLY;
+ d3D12ResourceBarrier.Anonymous.UAV.pResource = d3D12Resource;
+
+ d3D12GraphicsCommandList.ResourceBarrier(1, &d3D12ResourceBarrier);
+ }
+
+ ///
+ /// Ends a UAV resource barrier for a specific resource.
+ ///
+ /// The instance in use.
+ /// The to end the barrier for.
+ public static void EndUAVBarrier(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ID3D12Resource* d3D12Resource)
+ {
+ D3D12_RESOURCE_BARRIER d3D12ResourceBarrier = default;
+ d3D12ResourceBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
+ d3D12ResourceBarrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_END_ONLY;
+ d3D12ResourceBarrier.Anonymous.UAV.pResource = d3D12Resource;
+
+ d3D12GraphicsCommandList.ResourceBarrier(1, &d3D12ResourceBarrier);
+ }
+
///
/// Binds an input constant buffer to the first root parameter.
///
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs
index 2619cc2b..af1cf799 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs
@@ -207,7 +207,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper
{
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
}
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
@@ -226,7 +226,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
@@ -287,8 +287,8 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
}
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
@@ -306,8 +306,8 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
@@ -349,7 +349,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
}
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination = &destination.D3D12PlacedSubresourceFootprint)
@@ -371,7 +371,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
@@ -439,7 +439,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
}
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
@@ -458,7 +458,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
@@ -500,7 +500,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
}
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource = &source.D3D12PlacedSubresourceFootprint)
@@ -522,7 +522,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs
index d49a14b3..1120489a 100644
--- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs
+++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs
@@ -223,7 +223,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper
{
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
}
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
@@ -242,7 +242,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
@@ -314,8 +314,8 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
}
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
@@ -333,8 +333,8 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
@@ -385,7 +385,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
}
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintDestination = &destination.D3D12PlacedSubresourceFootprint)
@@ -407,7 +407,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
@@ -483,7 +483,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
}
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
@@ -502,7 +502,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
@@ -553,7 +553,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
}
fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* d3D12PlacedSubresourceFootprintSource = &source.D3D12PlacedSubresourceFootprint)
@@ -575,7 +575,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper
if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
{
- copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
+ copyCommandList.D3D12GraphicsCommandList->TransitioneBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
}
copyCommandList.ExecuteAndWaitForCompletion();
diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs
index 133b34fb..d4e5f85e 100644
--- a/src/ComputeSharp/Shaders/ComputeContext.cs
+++ b/src/ComputeSharp/Shaders/ComputeContext.cs
@@ -2,6 +2,7 @@
using System.Runtime.CompilerServices;
using ComputeSharp.__Internals;
using ComputeSharp.Graphics.Commands;
+using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Shaders;
using ComputeSharp.Shaders.Dispatching;
using Microsoft.Toolkit.Diagnostics;
@@ -36,6 +37,45 @@ public ref struct ComputeContext
this.commandList = default;
}
+ ///
+ /// Starts a resource barrier for a specific resource.
+ ///
+ /// The to start the barrier for.
+ internal readonly unsafe void StartBarrier(ID3D12Resource* d3D12Resource)
+ {
+ ThrowInvalidOperationExceptionIfDeviceIsNull();
+
+ ref CommandList commandList = ref GetCommandList(in this);
+
+ commandList.D3D12GraphicsCommandList->StartUAVBarrier(d3D12Resource);
+ }
+
+ ///
+ /// Ends a resource barrier for a specific resource.
+ ///
+ /// The to end the barrier for.
+ internal readonly unsafe void EndBarrier(ID3D12Resource* d3D12Resource)
+ {
+ ThrowInvalidOperationExceptionIfDeviceIsNull();
+
+ ref CommandList commandList = ref GetCommandList(in this);
+
+ commandList.D3D12GraphicsCommandList->EndUAVBarrier(d3D12Resource);
+ }
+
+ ///
+ /// Inserts a full a resource barrier for a specific resource.
+ ///
+ /// The to insert the barrier for.
+ internal readonly unsafe void FullBarrier(ID3D12Resource* d3D12Resource)
+ {
+ ThrowInvalidOperationExceptionIfDeviceIsNull();
+
+ ref CommandList commandList = ref GetCommandList(in this);
+
+ commandList.D3D12GraphicsCommandList->UAVBarrier(d3D12Resource);
+ }
+
///
/// Runs the input shader with the specified parameters.
///
@@ -196,6 +236,29 @@ public ref struct ComputeContext
this.commandList.ExecuteAndWaitForCompletion();
}
+ ///
+ /// Gets the current instance.
+ ///
+ /// The current instance.
+ /// A reference to the instance to use.
+ /// Thrown if the has not been initialized yet.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private static unsafe ref CommandList GetCommandList(in ComputeContext @this)
+ {
+ // This method has to take the context by readonly reference to allow callers to be marked as readonly.
+ // This is needed to skip the hidden copies done by Roslyn, which would break the dispatching, as the
+ // original context would not see the changes done by the following queued dispatches. This delegate
+ // cast is necessary to work around the fact that ref structs cannot currently be used as type arguments.
+ ref ComputeContext context = ref ((delegate*)(delegate*)&Unsafe.AsRef)(in @this);
+
+ if (!context.commandList.IsAllocated)
+ {
+ ThrowHelper.ThrowInvalidOperationException("The current compute context has not yet been initialized.");
+ }
+
+ return ref context.commandList;
+ }
+
///
/// Gets the current instance, and initializes it as needed.
///
@@ -205,10 +268,6 @@ public ref struct ComputeContext
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe ref CommandList GetCommandList(in ComputeContext @this, ID3D12PipelineState* pipelineState)
{
- // This method has to take the context by readonly reference to allow callers to be marked as readonly.
- // This is needed to skip the hidden copies done by Roslyn, which would break the dispatching, as the
- // original context would not see the changes done by the following queued dispatches. This delegate
- // cast is necessary to work around the fact that ref structs cannot currently be used as type arguments.
ref ComputeContext context = ref ((delegate*)(delegate*)&Unsafe.AsRef)(in @this);
if (context.commandList.IsAllocated)