Add ComputeContext type

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-27 20:44:04 +01:00
1 親 ca9bfa700d
コミット c43ed97f72
6ファイル変更
+352 -9

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

+10
ファイルの表示
@@ -78,6 +78,16 @@ internal unsafe ref struct CommandList
device.SetDescriptorHeapForCommandList(this.d3D12GraphicsCommandList);
}
/// <summary>
/// Gets whether or not the curreent <see cref="CommandList"/> instance is allocated.
/// </summary>
public readonly bool IsAllocated => this.device is not null;
/// <summary>
/// Gets the <see cref="GraphicsDevice"/> instance associated with the current command list.
/// </summary>
public GraphicsDevice GraphicsDevice => this.device;
/// <summary>
/// Gets the command list type being used by the current instance.
/// </summary>
+28 -6
ファイルの表示
@@ -9,7 +9,7 @@ namespace ComputeSharp;
public static partial class GraphicsDeviceExtensions
{
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// Runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of compute shader to run.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader.</param>
@@ -22,7 +22,7 @@ public static partial class GraphicsDeviceExtensions
}
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// Runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of compute shader to run.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader.</param>
@@ -36,7 +36,7 @@ public static partial class GraphicsDeviceExtensions
}
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// Runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of compute shader to run.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader.</param>
@@ -51,7 +51,7 @@ public static partial class GraphicsDeviceExtensions
}
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// Runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of compute shader to run.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader.</param>
@@ -69,7 +69,7 @@ public static partial class GraphicsDeviceExtensions
}
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// Runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of pixel shader to run.</typeparam>
/// <typeparam name="TPixel">The type of pixels being processed by the shader.</typeparam>
@@ -83,7 +83,7 @@ public static partial class GraphicsDeviceExtensions
}
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// Runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of pixel shader to run.</typeparam>
/// <typeparam name="TPixel">The type of pixels being processed by the shader.</typeparam>
@@ -96,4 +96,26 @@ public static partial class GraphicsDeviceExtensions
{
ShaderRunner<T>.Run(device, texture, ref Unsafe.AsRef(in shader));
}
/// <summary>
/// Creates a new <see cref="ComputeContext"/> instance to batch compute operations together.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run shaders.</param>
/// <returns>A new <see cref="ComputeContext"/> instance to batch compute operations together.</returns>
/// <remarks>
/// The returned <see cref="ComputeContext"/> instance should be used in a <see langword="using"/> block or declaration:
/// <code>
/// using (var context = device.CreateComputeContext())
/// {
/// // Dispatch shaders here...
/// }
/// </code>
/// All dispatched shaders will be executed as soon as the context goes out of scope.
/// </remarks>
public static ComputeContext CreateComputeContext(this GraphicsDevice device)
{
device.ThrowIfDisposed();
return new(device);
}
}
+213
ファイルの表示
@@ -0,0 +1,213 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.__Internals;
using ComputeSharp.Graphics.Commands;
using ComputeSharp.Shaders;
using ComputeSharp.Shaders.Dispatching;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop.DirectX;
#pragma warning disable CS0618
namespace ComputeSharp;
/// <summary>
/// A context to batch compute operations in a single invocation, minimizing GPU overhead.
/// </summary>
public ref struct ComputeContext
{
/// <summary>
/// The <see cref="GraphicsDevice"/> instance owning the current context.
/// </summary>
private readonly GraphicsDevice device;
/// <summary>
/// The current <see cref="CommandList"/> instance used to dispatch shaders.
/// </summary>
private CommandList commandList;
/// <summary>
/// Creates a new <see cref="ComputeContext"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> instance owning the current context.</param>
internal ComputeContext(GraphicsDevice device)
{
this.device = device;
this.commandList = default;
}
/// <summary>
/// Runs the input shader with the specified parameters.
/// </summary>
/// <param name="x">The number of iterations to run on the X axis.</param>
/// <param name="y">The number of iterations to run on the Y axis.</param>
/// <param name="z">The number of iterations to run on the Z axis.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the compute shader to run.</param>
internal unsafe void Run<T>(int x, int y, int z, ref T shader)
where T : struct, IComputeShader
{
bool xIs1 = x == 1;
bool yIs1 = y == 1;
bool zIs1 = z == 1;
int mask = *(byte*)&xIs1 << 2 | *(byte*)&yIs1 << 1 | *(byte*)&zIs1;
int threadsX;
int threadsY;
int threadsZ;
switch (mask - 1)
{
case 0: // (_, _, 1)
threadsX = threadsY = 8;
threadsZ = 1;
break;
case 1: // (_, 1, _)
threadsX = threadsZ = 8;
threadsY = 1;
break;
case 2: // (_, 1, 1)
threadsX = (int)this.device.WavefrontSize;
threadsY = threadsZ = 1;
break;
case 3: // (1, _, _)
threadsX = 1;
threadsY = threadsZ = 8;
break;
case 4: // (1, _, 1)
threadsX = threadsZ = 1;
threadsY = (int)this.device.WavefrontSize;
break;
case 5: // (1, 1, _)
threadsX = threadsY = 1;
threadsZ = (int)this.device.WavefrontSize;
break;
default: // (_, _, _)
threadsX = threadsY = threadsZ = 4;
break;
}
Run(x, y, z, threadsX, threadsY, threadsZ, ref shader);
}
/// <summary>
/// Runs the input shader with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of compute shader to run.</typeparam>
/// <param name="x">The number of iterations to run on the X axis.</param>
/// <param name="y">The number of iterations to run on the Y axis.</param>
/// <param name="z">The number of iterations to run on the Z axis.</param>
/// <param name="threadsX">The number of threads in each thread group for the X axis.</param>
/// <param name="threadsY">The number of threads in each thread group for the Y axis.</param>
/// <param name="threadsZ">The number of threads in each thread group for the Z axis.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the compute shader to run.</param>
internal unsafe void Run<T>(
int x,
int y,
int z,
int threadsX,
int threadsY,
int threadsZ,
ref T shader)
where T : struct, IComputeShader
{
Guard.IsGreaterThan(x, 0, nameof(x));
Guard.IsGreaterThan(y, 0, nameof(y));
Guard.IsGreaterThan(z, 0, nameof(z));
Guard.IsBetweenOrEqualTo(threadsX, 1, 1024, nameof(threadsX));
Guard.IsBetweenOrEqualTo(threadsY, 1, 1024, nameof(threadsY));
Guard.IsBetweenOrEqualTo(threadsZ, 1, 64, nameof(threadsZ));
Guard.IsLessThanOrEqualTo(threadsX * threadsY * threadsZ, 1024, "threadsXYZ");
int groupsX = Math.DivRem(x, threadsX, out int modX) + (modX == 0 ? 0 : 1);
int groupsY = Math.DivRem(y, threadsY, out int modY) + (modY == 0 ? 0 : 1);
int groupsZ = Math.DivRem(z, threadsZ, out int modZ) + (modZ == 0 ? 0 : 1);
Guard.IsBetweenOrEqualTo(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
Guard.IsBetweenOrEqualTo(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
Guard.IsBetweenOrEqualTo(groupsZ, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
PipelineData pipelineData = ShaderRunner<T>.GetPipelineData(this.device, threadsX, threadsY, threadsZ, ref shader);
ref CommandList commandList = ref GetCommandList(ref this, this.device, pipelineData.D3D12PipelineState);
commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature);
ComputeShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList);
shader.LoadDispatchData(ref dataLoader, this.device, x, y, z);
commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, (uint)groupsZ);
}
/// <summary>
/// Runs the input shader with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of pixel shader to run.</typeparam>
/// <typeparam name="TPixel">The type of pixel to work on.</typeparam>
/// <param name="texture">The target texture to invoke the pixel shader upon.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the pixel shader to run.</param>
internal unsafe void Run<T, TPixel>(IReadWriteTexture2D<TPixel> texture, ref T shader)
where T : struct, IPixelShader<TPixel>
where TPixel : unmanaged
{
int x = texture.Width;
int y = texture.Height;
const int threadsX = 8;
const int threadsY = 8;
const int threadsZ = 1;
int groupsX = Math.DivRem(x, threadsX, out int modX) + (modX == 0 ? 0 : 1);
int groupsY = Math.DivRem(y, threadsY, out int modY) + (modY == 0 ? 0 : 1);
Guard.IsBetweenOrEqualTo(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
Guard.IsBetweenOrEqualTo(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
PipelineData pipelineData = ShaderRunner<T>.GetPipelineData(this.device, threadsX, threadsY, threadsZ, ref shader);
ref CommandList commandList = ref GetCommandList(ref this, this.device, pipelineData.D3D12PipelineState);
commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature);
PixelShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList);
shader.LoadDispatchData(ref dataLoader, this.device, x, y, 1);
// Load the implicit output texture
commandList.D3D12GraphicsCommandList->SetComputeRootDescriptorTable(
1,
((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuDescriptorHandle(this.device));
commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, 1);
}
/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose()
{
if (!this.commandList.IsAllocated)
{
return;
}
this.commandList.ExecuteAndWaitForCompletion();
}
/// <summary>
/// Gets the current <see cref="CommandList"/> instance, and initializes it as needed.
/// </summary>
/// <param name="this">The current <see cref="ComputeContext"/> instance.</param>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader.</param>
/// <param name="pipelineState">The input <see cref="ID3D12PipelineState"/> to load.</param>
/// <returns>A reference to the <see cref="CommandList"/> instance to use.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe ref CommandList GetCommandList(ref ComputeContext @this, GraphicsDevice device, ID3D12PipelineState* pipelineState)
{
if (@this.commandList.IsAllocated)
{
@this.commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineState);
}
else
{
@this.commandList = new CommandList(device, pipelineState);
}
return ref @this.commandList;
}
}
+98
ファイルの表示
@@ -0,0 +1,98 @@
using System.Runtime.CompilerServices;
namespace ComputeSharp;
/// <summary>
/// A <see langword="class"/> that contains extension methods for the <see cref="ComputeContext"/> type, used to run compute shaders.
/// </summary>
public static partial class ComputeContextExtensions
{
/// <summary>
/// Runs the input shader on a target <see cref="ComputeContext"/> instance, with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of compute shader to run.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to run the shader.</param>
/// <param name="x">The number of iterations to run on the X axis.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the compute shader to run.</param>
public static void For<T>(this in ComputeContext context, int x, in T shader)
where T : struct, IComputeShader
{
context.Run(x, 1, 1, ref Unsafe.AsRef(in shader));
}
/// <summary>
/// Runs the input shader on a target <see cref="ComputeContext"/> instance, with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of compute shader to run.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to run the shader.</param>
/// <param name="x">The number of iterations to run on the X axis.</param>
/// <param name="y">The number of iterations to run on the Y axis.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the compute shader to run.</param>
public static void For<T>(this ComputeContext context, int x, int y, in T shader)
where T : struct, IComputeShader
{
context.Run(x, y, 1, ref Unsafe.AsRef(in shader));
}
/// <summary>
/// Runs the input shader on a target <see cref="ComputeContext"/> instance, with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of compute shader to run.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to run the shader.</param>
/// <param name="x">The number of iterations to run on the X axis.</param>
/// <param name="y">The number of iterations to run on the Y axis.</param>
/// <param name="z">The number of iterations to run on the Z axis.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the compute shader to run.</param>
public static void For<T>(this ComputeContext context, int x, int y, int z, in T shader)
where T : struct, IComputeShader
{
context.Run(x, y, z, ref Unsafe.AsRef(in shader));
}
/// <summary>
/// Runs the input shader on a target <see cref="ComputeContext"/> instance, with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of compute shader to run.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to run the shader.</param>
/// <param name="x">The number of iterations to run on the X axis.</param>
/// <param name="y">The number of iterations to run on the Y axis.</param>
/// <param name="z">The number of iterations to run on the Z axis.</param>
/// <param name="threadsX">The number of threads in each thread group for the X axis.</param>
/// <param name="threadsY">The number of threads in each thread group for the Y axis.</param>
/// <param name="threadsZ">The number of threads in each thread group for the Z axis.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the compute shader to run.</param>
public static void For<T>(this ComputeContext context, int x, int y, int z, int threadsX, int threadsY, int threadsZ, in T shader)
where T : struct, IComputeShader
{
context.Run(x, y, z, threadsX, threadsY, threadsZ, ref Unsafe.AsRef(in shader));
}
/// <summary>
/// Runs the input shader on a target <see cref="ComputeContext"/> instance, with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of pixel shader to run.</typeparam>
/// <typeparam name="TPixel">The type of pixels being processed by the shader.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to run the shader.</param>
/// <param name="texture">The target texture to apply the pixel shader to.</param>
public static void ForEach<T, TPixel>(this ComputeContext context, IReadWriteTexture2D<TPixel> texture)
where T : struct, IPixelShader<TPixel>
where TPixel : unmanaged
{
context.Run(texture, ref Unsafe.AsRef(default(T)));
}
/// <summary>
/// Runs the input shader on a target <see cref="ComputeContext"/> instance, with the specified parameters.
/// </summary>
/// <typeparam name="T">The type of pixel shader to run.</typeparam>
/// <typeparam name="TPixel">The type of pixels being processed by the shader.</typeparam>
/// <param name="context">The <see cref="ComputeContext"/> to use to run the shader.</param>
/// <param name="texture">The target texture to apply the pixel shader to.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the pixel shader to run.</param>
public static void ForEach<T, TPixel>(this ComputeContext context, IReadWriteTexture2D<TPixel> texture, in T shader)
where T : struct, IPixelShader<TPixel>
where TPixel : unmanaged
{
context.Run(texture, ref Unsafe.AsRef(in shader));
}
}
差分なしでファイル名を変更。
+3 -3
ファイルの表示
@@ -27,7 +27,7 @@ internal static class ShaderRunner<T>
private static readonly Dictionary<ShaderKey, ICachedShader> ShadersCache = new();
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// Runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader.</param>
/// <param name="x">The number of iterations to run on the X axis.</param>
@@ -84,7 +84,7 @@ internal static class ShaderRunner<T>
}
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// Runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader.</param>
/// <param name="x">The number of iterations to run on the X axis.</param>
@@ -137,7 +137,7 @@ internal static class ShaderRunner<T>
}
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// Runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader.</param>
/// <param name="texture">The target texture to invoke the pixel shader upon.</param>