Added IDispatchDataLoader and compute shader loader

このコミットが含まれているのは:
Sergio Pedri committed 2021-07-12 15:58:51 +02:00
1 親 2d579758cd
コミット 8aa02ea0cc
4ファイル変更
+104 -18

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

+29
ファイルの表示
@@ -0,0 +1,29 @@
using System;
using System.ComponentModel;
namespace ComputeSharp.__Internals
{
/// <summary>
/// A base <see langword="interface"/> representing a data loader for a shader being dispatched.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This interface is not intended to be used directly by user code")]
public interface IDispatchDataLoader
{
/// <summary>
/// Loads the captured values used by the shader to be dispatched.
/// </summary>
/// <param name="data">The sequence of serialized captured values.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be called directly by user code")]
void LoadCapturedValues(ReadOnlySpan<uint> data);
/// <summary>
/// Loads the captured resources used by the shader to be dispatched.
/// </summary>
/// <param name="data">The sequence of serialized resources.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be called directly by user code")]
void LoadCapturedResources(ReadOnlySpan<ulong> data);
}
}
+14
ファイルの表示
@@ -19,5 +19,19 @@ namespace ComputeSharp.__Internals
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This interface is not intended to be used directly by user code")]
int GetDispatchId();
/// <summary>
/// Loads the dispatch data for the shader.
/// </summary>
/// <typeparam name="TDataLoader">The type of data loader being used.</typeparam>
/// <param name="dataLoader">The <typeparamref name="TDataLoader"/> instance to use to load the data.</param>
/// <param name="device">The device the shader is being dispatched on.</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>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be called directly by user code")]
void LoadDispatchData<TDataLoader>(in TDataLoader dataLoader, GraphicsDevice device, int x, int y, int z)
where TDataLoader : struct, IDispatchDataLoader;
}
}
+57
ファイルの表示
@@ -0,0 +1,57 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.__Internals;
using TerraFX.Interop;
using System.Runtime.InteropServices;
#pragma warning disable CS0618
namespace ComputeSharp.Shaders.Dispatching
{
/// <summary>
/// A data loader for compute shaders.
/// </summary>
internal readonly unsafe struct ComputeShaderDispatchDataLoader : IDispatchDataLoader
{
/// <summary>
/// The <see cref="ID3D12GraphicsCommandList"/> object in use.
/// </summary>
private readonly ID3D12GraphicsCommandList* d3D12GraphicsCommandList;
/// <summary>
/// Creates a new <see cref="ComputeShaderDispatchDataLoader"/> instance.
/// </summary>
/// <param name="d3D12GraphicsCommandList">The <see cref="ID3D12GraphicsCommandList"/> object to use.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ComputeShaderDispatchDataLoader(ID3D12GraphicsCommandList* d3D12GraphicsCommandList)
{
this.d3D12GraphicsCommandList = d3D12GraphicsCommandList;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void LoadCapturedValues(ReadOnlySpan<uint> data)
{
this.d3D12GraphicsCommandList->SetComputeRoot32BitConstants(data);
}
/// <inheritdoc/>
public void LoadCapturedResources(ReadOnlySpan<ulong> data)
{
ID3D12GraphicsCommandList* d3D12GraphicsCommandList = this.d3D12GraphicsCommandList;
ref ulong resourcesRef = ref MemoryMarshal.GetReference(data);
ref D3D12_GPU_DESCRIPTOR_HANDLE startRef = ref Unsafe.As<ulong, D3D12_GPU_DESCRIPTOR_HANDLE>(ref resourcesRef);
ref D3D12_GPU_DESCRIPTOR_HANDLE endRef = ref Unsafe.Add(ref startRef, data.Length);
uint offset = 1;
while (Unsafe.IsAddressLessThan(ref startRef, ref endRef))
{
d3D12GraphicsCommandList->SetComputeRootDescriptorTable(offset, startRef);
startRef = ref Unsafe.Add(ref startRef, 1);
offset++;
}
}
}
}
+4 -18
ファイルの表示
@@ -13,6 +13,7 @@ using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
using FX = TerraFX.Interop.Windows;
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
using ComputeSharp.Shaders.Dispatching;
#pragma warning disable CS0618
@@ -120,7 +121,6 @@ namespace ComputeSharp.Shaders
Guard.IsBetweenOrEqualTo(threadsZ, 1, 64, nameof(threadsZ));
Guard.IsLessThanOrEqualTo(threadsX * threadsY * threadsZ, 1024, "threadsXYZ");
// Calculate the dispatch values
int
groupsX = Math.DivRem(x, threadsX, out int modX) + (modX == 0 ? 0 : 1),
groupsY = Math.DivRem(y, threadsY, out int modY) + (modY == 0 ? 0 : 1),
@@ -130,15 +130,13 @@ namespace ComputeSharp.Shaders
Guard.IsBetweenOrEqualTo(groupsY, 1, FX.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
Guard.IsBetweenOrEqualTo(groupsZ, 1, FX.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX));
// Create the shader key
ShaderKey key = new(shader.GetDispatchId(), threadsX, threadsY, threadsZ);
CachedShader<T> shaderData;
PipelineData? pipelineData;
lock (ShadersCache)
{
// Get or preload the shader
if (!ShadersCache.TryGetValue(key, out shaderData))
if (!ShadersCache.TryGetValue(key, out CachedShader<T> shaderData))
{
LoadShader(threadsX, threadsY, threadsZ, in shader, out shaderData);
@@ -153,27 +151,15 @@ namespace ComputeSharp.Shaders
}
}
// Create the commands list and set the pipeline state
using CommandList commandList = new(device, D3D12_COMMAND_LIST_TYPE_COMPUTE);
commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature);
commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineData.D3D12PipelineState);
// Extract the dispatch data for the shader invocation
using DispatchData dispatchData = shaderData.Loader.GetDispatchData(device, in shader, x, y, z);
ComputeShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList);
// Initialize the loop targets and the captured values
commandList.D3D12GraphicsCommandList->SetComputeRoot32BitConstants(dispatchData.Variables);
shader.LoadDispatchData(in dataLoader, device, x, y, z);
ReadOnlySpan<D3D12_GPU_DESCRIPTOR_HANDLE> resources = dispatchData.Resources;
for (int i = 0; i < resources.Length; i++)
{
// Load the captured buffers
commandList.D3D12GraphicsCommandList->SetComputeRootDescriptorTable((uint)i + 1, resources[i]);
}
// Dispatch and wait for completion
commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, (uint)groupsZ);
commandList.ExecuteAndWaitForCompletion();
}