Add internal helpers to insert resource barriers

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-28 13:30:48 +01:00
1 親 49475dcd77
コミット c50ab7954f
4ファイル変更
+132 -29

検索条件に一致するファイルはありません

+45 -1
ファイルの表示
@@ -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
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to change state for.</param>
/// <param name="d3D12ResourceStatesBefore">The starting <see cref="D3D12_RESOURCE_STATES"/> value for the transition.</param>
/// <param name="d3D12ResourceStatesAfter">The destnation <see cref="D3D12_RESOURCE_STATES"/> value for the transition.</param>
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);
}
/// <summary>
/// Inserts a UAV resource barrier for a specific resource.
/// </summary>
/// <param name="d3D12GraphicsCommandList">The <see cref="ID3D12GraphicsCommandList"/> instance in use.</param>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to insert the barrier for.</param>
public static void UAVBarrier(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ID3D12Resource* d3D12Resource)
{
D3D12_RESOURCE_BARRIER d3D12ResourceBarrier = D3D12_RESOURCE_BARRIER.InitUAV(d3D12Resource);
d3D12GraphicsCommandList.ResourceBarrier(1, &d3D12ResourceBarrier);
}
/// <summary>
/// Starts a UAV resource barrier for a specific resource.
/// </summary>
/// <param name="d3D12GraphicsCommandList">The <see cref="ID3D12GraphicsCommandList"/> instance in use.</param>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to start the barrier for.</param>
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);
}
/// <summary>
/// Ends a UAV resource barrier for a specific resource.
/// </summary>
/// <param name="d3D12GraphicsCommandList">The <see cref="ID3D12GraphicsCommandList"/> instance in use.</param>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to end the barrier for.</param>
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);
}
/// <summary>
/// Binds an input constant buffer to the first root parameter.
/// </summary>
+12 -12
ファイルの表示
@@ -207,7 +207,7 @@ public unsafe abstract class Texture2D<T> : 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<T> : 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<T> : 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<T> : 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<T> : 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<T> : 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<T> : 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<T> : 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<T> : 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<T> : 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();
+12 -12
ファイルの表示
@@ -223,7 +223,7 @@ public unsafe abstract class Texture3D<T> : 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<T> : 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<T> : 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<T> : 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<T> : 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<T> : 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<T> : 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<T> : 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<T> : 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<T> : 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();
+63 -4
ファイルの表示
@@ -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;
}
/// <summary>
/// Starts a resource barrier for a specific resource.
/// </summary>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to start the barrier for.</param>
internal readonly unsafe void StartBarrier(ID3D12Resource* d3D12Resource)
{
ThrowInvalidOperationExceptionIfDeviceIsNull();
ref CommandList commandList = ref GetCommandList(in this);
commandList.D3D12GraphicsCommandList->StartUAVBarrier(d3D12Resource);
}
/// <summary>
/// Ends a resource barrier for a specific resource.
/// </summary>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to end the barrier for.</param>
internal readonly unsafe void EndBarrier(ID3D12Resource* d3D12Resource)
{
ThrowInvalidOperationExceptionIfDeviceIsNull();
ref CommandList commandList = ref GetCommandList(in this);
commandList.D3D12GraphicsCommandList->EndUAVBarrier(d3D12Resource);
}
/// <summary>
/// Inserts a full a resource barrier for a specific resource.
/// </summary>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to insert the barrier for.</param>
internal readonly unsafe void FullBarrier(ID3D12Resource* d3D12Resource)
{
ThrowInvalidOperationExceptionIfDeviceIsNull();
ref CommandList commandList = ref GetCommandList(in this);
commandList.D3D12GraphicsCommandList->UAVBarrier(d3D12Resource);
}
/// <summary>
/// Runs the input shader with the specified parameters.
/// </summary>
@@ -196,6 +236,29 @@ public ref struct ComputeContext
this.commandList.ExecuteAndWaitForCompletion();
}
/// <summary>
/// Gets the current <see cref="CommandList"/> instance.
/// </summary>
/// <param name="this">The current <see cref="ComputeContext"/> instance.</param>
/// <returns>A reference to the <see cref="CommandList"/> instance to use.</returns>
/// <exception cref="InvalidOperationException">Thrown if the <see cref="CommandList"/> has not been initialized yet.</exception>
[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*<in ComputeContext, ref ComputeContext>)(delegate*<in byte, ref byte>)&Unsafe.AsRef<byte>)(in @this);
if (!context.commandList.IsAllocated)
{
ThrowHelper.ThrowInvalidOperationException("The current compute context has not yet been initialized.");
}
return ref context.commandList;
}
/// <summary>
/// Gets the current <see cref="CommandList"/> instance, and initializes it as needed.
/// </summary>
@@ -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*<in ComputeContext, ref ComputeContext>)(delegate*<in byte, ref byte>)&Unsafe.AsRef<byte>)(in @this);
if (context.commandList.IsAllocated)