Add IConstantBufferLoader and associated loader

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-15 20:12:23 +02:00
1 親 342cb64ad7
コミット 2ff7def00a
9ファイル変更
+72 -154

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

+16
ファイルの表示
@@ -0,0 +1,16 @@
using System;
namespace ComputeSharp.__Internals;
/// <summary>
/// A type representing a data loader for the constant buffer of a compute shader.
/// </summary>
public interface IConstantBufferLoader
{
/// <summary>
/// Loads the constant buffer of a compute shader.
/// </summary>
/// <param name="data">The constant buffer for the compute shader (the size must be a multiple of the size of a DWORD value).</param>
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="data"/> is not a multiple of the size of a DWORD value).</exception>
void LoadConstantBuffer(ReadOnlySpan<byte> data);
}
-24
ファイルの表示
@@ -1,24 +0,0 @@
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>
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>
void LoadCapturedResources(ReadOnlySpan<ulong> data);
}
+3 -6
ファイルの表示
@@ -60,18 +60,15 @@ public interface IShader
ReadOnlyMemory<byte> HlslBytecode { get; }
/// <summary>
/// Loads the dispatch data for the shader.
/// Loads the constant buffer of a given input shader.
/// </summary>
/// <typeparam name="TLoader">The type of data loader being used.</typeparam>
/// <param name="loader">The <typeparamref name="TLoader"/> 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<TLoader>(ref TLoader loader, GraphicsDevice device, int x, int y, int z)
where TLoader : struct, IDispatchDataLoader;
void LoadConstantBuffer<TLoader>(ref TLoader loader, int x, int y, int z)
where TLoader : struct, IConstantBufferLoader;
/// <summary>
/// Loads the graphics resources for the shader.
+3 -3
ファイルの表示
@@ -288,11 +288,11 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions
/// </summary>
/// <param name="d3D12GraphicsCommandList">The <see cref="ID3D12GraphicsCommandList"/> instance in use.</param>
/// <param name="data">The input buffer with the constant data to bind.</param>
public static void SetComputeRoot32BitConstants(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ReadOnlySpan<uint> data)
public static void SetComputeRoot32BitConstants(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ReadOnlySpan<byte> data)
{
fixed (uint* p = data)
fixed (byte* p = data)
{
d3D12GraphicsCommandList.SetComputeRoot32BitConstants(0, (uint)data.Length, p, 0);
d3D12GraphicsCommandList.SetComputeRoot32BitConstants(0, (uint)data.Length / sizeof(uint), p, 0);
}
}
}
+12 -4
ファイルの表示
@@ -172,9 +172,13 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature);
ComputeShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList);
D3D12GraphicsCommandListConstantBufferLoader dataLoader = new(commandList.D3D12GraphicsCommandList);
shader.LoadDispatchData(ref dataLoader, this.device, x, y, z);
shader.LoadConstantBuffer(ref dataLoader, x, y, z);
D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device, rootParameterOffset: 1);
shader.LoadGraphicsResources(ref graphicsResourceLoader);
commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, (uint)groupsZ);
}
@@ -206,9 +210,13 @@ public struct ComputeContext : IDisposable, IAsyncDisposable
commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature);
PixelShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList);
D3D12GraphicsCommandListConstantBufferLoader constantBufferLoader = new(commandList.D3D12GraphicsCommandList);
shader.LoadDispatchData(ref dataLoader, this.device, x, y, 1);
shader.LoadConstantBuffer(ref constantBufferLoader, x, y, 1);
D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device, rootParameterOffset: 2);
shader.LoadGraphicsResources(ref graphicsResourceLoader);
// Load the implicit output texture
commandList.D3D12GraphicsCommandList->SetComputeRootDescriptorTable(
-56
ファイルの表示
@@ -1,56 +0,0 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.__Internals;
using TerraFX.Interop.DirectX;
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++;
}
}
}
@@ -0,0 +1,37 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.__Internals;
using TerraFX.Interop.DirectX;
namespace ComputeSharp.Shaders.Dispatching;
/// <summary>
/// An <see cref="IConstantBufferLoader"/> implementation for compute shaders dispatched via <see cref="ID3D12GraphicsCommandList"/>.
/// </summary>
internal readonly unsafe struct D3D12GraphicsCommandListConstantBufferLoader : IConstantBufferLoader
{
/// <summary>
/// The <see cref="ID3D12GraphicsCommandList"/> object in use.
/// </summary>
private readonly ID3D12GraphicsCommandList* d3D12GraphicsCommandList;
/// <summary>
/// Creates a new <see cref="D3D12GraphicsCommandListConstantBufferLoader"/> instance.
/// </summary>
/// <param name="d3D12GraphicsCommandList">The <see cref="ID3D12GraphicsCommandList"/> object to use.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal D3D12GraphicsCommandListConstantBufferLoader(ID3D12GraphicsCommandList* d3D12GraphicsCommandList)
{
this.d3D12GraphicsCommandList = d3D12GraphicsCommandList;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void LoadConstantBuffer(ReadOnlySpan<byte> data)
{
default(ArgumentException).ThrowIf((data.Length & 3) != 0, nameof(data));
this.d3D12GraphicsCommandList->SetComputeRoot32BitConstants(data);
}
}
-60
ファイルの表示
@@ -1,60 +0,0 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.__Internals;
using TerraFX.Interop.DirectX;
#pragma warning disable CS0618
namespace ComputeSharp.Shaders.Dispatching;
/// <summary>
/// A data loader for pixel shaders.
/// </summary>
internal readonly unsafe struct PixelShaderDispatchDataLoader : IDispatchDataLoader
{
/// <summary>
/// The <see cref="ID3D12GraphicsCommandList"/> object in use.
/// </summary>
private readonly ID3D12GraphicsCommandList* d3D12GraphicsCommandList;
/// <summary>
/// Creates a new <see cref="PixelShaderDispatchDataLoader"/> instance.
/// </summary>
/// <param name="d3D12GraphicsCommandList">The <see cref="ID3D12GraphicsCommandList"/> object to use.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal PixelShaderDispatchDataLoader(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 = 2;
// Skip the first target resource, as that is loaded explicitly by the shader runner using the
// target texture passed in by the user, which is not actually captured by the shader itself.
// The target root descriptor table index is therefore 2 in this case, as it needs to skip not
// only the this implicit texture to draw on, but also the constant buffer with capture values.
while (Unsafe.IsAddressLessThan(ref startRef, ref endRef))
{
d3D12GraphicsCommandList->SetComputeRootDescriptorTable(offset, startRef);
startRef = ref Unsafe.Add(ref startRef, 1);
offset++;
}
}
}