Add IGraphicsResourceLoader and associated loader

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

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

+19
ファイルの表示
@@ -0,0 +1,19 @@
using System;
namespace ComputeSharp.__Internals;
/// <summary>
/// A type representing a loader object for <see cref="IGraphicsResource"/> values used in compute shaders.
/// </summary>
public interface IGraphicsResourceLoader
{
/// <summary>
/// Loads a resource used by the shader to be dispatched.
/// </summary>
/// <param name="resource">The input resource to be loaded.</param>
/// <param name="index">The index to use to bind the resource to the shader.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="resource"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="resource"/> is not a valid resource object.</exception>
/// <exception cref="GraphicsDeviceMismatchException">Thrown if the resource isn't associated with the same device used to dispatch the shader.</exception>
void LoadGraphicsResource(IGraphicsResource resource, uint index);
}
+8
ファイルの表示
@@ -72,4 +72,12 @@ public interface IShader
[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;
/// <summary>
/// Loads the graphics resources for the shader.
/// </summary>
/// <typeparam name="TLoader">The type of graphics resource loader being used.</typeparam>
/// <param name="loader">The <typeparamref name="TLoader"/> instance to use to load the data.</param>
void LoadGraphicsResources<TLoader>(ref TLoader loader)
where TLoader : struct, IGraphicsResourceLoader;
}
@@ -0,0 +1,75 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.__Internals;
using TerraFX.Interop.DirectX;
using ComputeSharp.Resources.Interop;
namespace ComputeSharp.Shaders.Dispatching;
/// <summary>
/// An <see cref="IGraphicsResource"/> loader for compute shaders dispatched via <see cref="ID3D12GraphicsCommandList"/>.
/// </summary>
internal readonly unsafe struct D3D12GraphicsCommandListGraphicsResourceLoader : IGraphicsResourceLoader
{
/// <summary>
/// The <see cref="ID3D12GraphicsCommandList"/> object in use.
/// </summary>
private readonly ID3D12GraphicsCommandList* d3D12GraphicsCommandList;
/// <summary>
/// The <see cref="GraphicsDevice"/> object in use.
/// </summary>
private readonly GraphicsDevice graphicsDevice;
/// <summary>
/// The offset into the compute root descriptor table for loaded resources.
/// </summary>
/// <remarks>
/// <para>
/// This is used to correctly align the explicit resources captured by shaders, with the implicit ones
/// that are injected by ComputeSharp. For instance, the constant buffer is always added automatically.
/// </para>
/// <para>
/// For compute shaders capturing an implicit texture, this is used to 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.
/// </para>
/// </remarks>
private readonly uint rootParameterOffset;
/// <summary>
/// Creates a new <see cref="D3D12GraphicsCommandListGraphicsResourceLoader"/> instance.
/// </summary>
/// <param name="d3D12GraphicsCommandList">The <see cref="ID3D12GraphicsCommandList"/> object to use.</param>
/// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> instance that <paramref name="d3D12GraphicsCommandList"/> is tied to.</param>
/// <param name="rootParameterOffset">The offset into the compute root descriptor table for loaded resources.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal D3D12GraphicsCommandListGraphicsResourceLoader(
ID3D12GraphicsCommandList* d3D12GraphicsCommandList,
GraphicsDevice graphicsDevice,
uint rootParameterOffset)
{
this.d3D12GraphicsCommandList = d3D12GraphicsCommandList;
this.graphicsDevice = graphicsDevice;
this.rootParameterOffset = rootParameterOffset;
}
/// <inheritdoc/>
void IGraphicsResourceLoader.LoadGraphicsResource(IGraphicsResource resource, uint index)
{
default(ArgumentNullException).ThrowIfNull(resource);
if (resource is not ID3D12ReadOnlyResource readOnlyResource)
{
default(ArgumentException).Throw(nameof(resource));
return;
}
this.d3D12GraphicsCommandList->SetComputeRootDescriptorTable(
RootParameterIndex: this.rootParameterOffset + index,
BaseDescriptor: readOnlyResource.ValidateAndGetGpuDescriptorHandle(this.graphicsDevice));
}
}