From fdb0f691a2fe6296ea1e6d3de3258bc79f466ef4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 10 Jul 2021 12:37:31 +0200 Subject: [PATCH] Added interface APIs to load shader metadata --- .../IComputeShaderDataLoaderGenerator.cs | 16 +++--- .../__Internals/IDispatchMetadataLoader.cs | 23 ++++++++ .../Interfaces/__Internals/IShader{TSelf}.cs | 15 +++++- .../Interop/__Internals/ResourceDescriptor.cs | 54 +++++++++++++++++++ src/ComputeSharp/Shaders/ShaderRunner{T}.cs | 4 +- 5 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 src/ComputeSharp/Core/Interfaces/__Internals/IDispatchMetadataLoader.cs create mode 100644 src/ComputeSharp/Graphics/Resources/Interop/__Internals/ResourceDescriptor.cs diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs index 998d1dba..12bd1543 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs @@ -98,7 +98,7 @@ namespace ComputeSharp.SourceGenerators // [EditorBrowsable(EditorBrowsableState.Never)] // [Obsolete("This method is not intended to be called directly by user code")] // [return: ComputeRoot32BitConstants(42)] - // public readonly void LoadDispatchData(in TDataLoader dataLoader, GraphicsDevice device, int x, int y, int z) + // public readonly void LoadDispatchData(ref TDataLoader loader, GraphicsDevice device, int x, int y, int z) // where TDataLoader : struct, IDispatchDataLoader // { // @@ -122,8 +122,8 @@ namespace ComputeSharp.SourceGenerators .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.ReadOnlyKeyword)) .AddTypeParameterListParameters(TypeParameter(Identifier("TDataLoader"))) .AddParameterListParameters( - Parameter(Identifier("dataLoader")) - .AddModifiers(Token(SyntaxKind.InKeyword)) + Parameter(Identifier("loader")) + .AddModifiers(Token(SyntaxKind.RefKeyword)) .WithType(IdentifierName("TDataLoader")), Parameter(Identifier("device")).WithType(IdentifierName("GraphicsDevice")), Parameter(Identifier("x")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))), @@ -288,13 +288,13 @@ namespace ComputeSharp.SourceGenerators SyntaxKind.NumericLiteralExpression, Literal(0)))))))))); - // dataLoader.LoadCapturedValues(span0); + // loader.LoadCapturedValues(span0); statements.Add( ExpressionStatement( InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - IdentifierName("dataLoader"), + IdentifierName("loader"), IdentifierName("LoadCapturedValues"))) .AddArgumentListArguments(Argument(IdentifierName("span0"))))); @@ -329,13 +329,13 @@ namespace ComputeSharp.SourceGenerators SyntaxKind.NumericLiteralExpression, Literal(0)))))))))); - // dataLoader.LoadCapturedResources(span1); + // loader.LoadCapturedResources(span1); statements.Add( ExpressionStatement( InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - IdentifierName("dataLoader"), + IdentifierName("loader"), IdentifierName("LoadCapturedResources"))) .AddArgumentListArguments(Argument(IdentifierName("span1"))))); } @@ -367,7 +367,7 @@ namespace ComputeSharp.SourceGenerators typeName = fieldSymbol.Type.GetFullMetadataName(); // Disambiguates the name of target fields against the current input parameters - if (fieldName is "dataLoader" or "device" or "x" or "y" or "z") + if (fieldName is "loader" or "device" or "x" or "y" or "z") { fieldName = $"this.{fieldName}"; } diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchMetadataLoader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchMetadataLoader.cs new file mode 100644 index 00000000..9095e60e --- /dev/null +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchMetadataLoader.cs @@ -0,0 +1,23 @@ +using System; +using System.ComponentModel; + +namespace ComputeSharp.__Internals +{ + /// + /// A base representing a metadatadata loader for a shader being dispatched. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This interface is not intended to be used directly by user code")] + public interface IDispatchMetadataLoader + { + /// + /// Loads an opaque metadata handle from the given shader metadata. + /// + /// The serialized metadata for the current shader. + /// The sequence of resource descriptors for the current shader. + /// The resulting opaque metadata handle. + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be called directly by user code")] + void LoadMetadataHandle(ReadOnlySpan serializedMetadata, ReadOnlySpan resourceDescriptors, out IntPtr result); + } +} diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs index b81e424c..e2bfda10 100644 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs @@ -24,16 +24,27 @@ namespace ComputeSharp.__Internals /// Loads the dispatch data for the shader. /// /// The type of data loader being used. - /// The instance to use to load the data. + /// The instance to use to load the data. /// The device the shader is being dispatched on. /// The number of iterations to run on the X axis. /// The number of iterations to run on the Y axis. /// The number of iterations to run on the Z axis. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be called directly by user code")] - void LoadDispatchData(in TDataLoader dataLoader, GraphicsDevice device, int x, int y, int z) + void LoadDispatchData(ref TDataLoader loader, GraphicsDevice device, int x, int y, int z) where TDataLoader : struct, IDispatchDataLoader; + /// + /// Loads an opaque metadata handle from the metadata of the current shader. + /// + /// The type of data loader being used. + /// The instance to use to load the data. + /// The resulting opaque metadata handle. + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be called directly by user code")] + void LoadDispatchMetadata(ref TMetadataLoader loader, out IntPtr result) + where TMetadataLoader : struct, IDispatchMetadataLoader; + /// /// Builds the HLSL source code for the current shader instance. /// diff --git a/src/ComputeSharp/Graphics/Resources/Interop/__Internals/ResourceDescriptor.cs b/src/ComputeSharp/Graphics/Resources/Interop/__Internals/ResourceDescriptor.cs new file mode 100644 index 00000000..e97fe2ab --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/Interop/__Internals/ResourceDescriptor.cs @@ -0,0 +1,54 @@ +using System; +using System.ComponentModel; +using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; +using static TerraFX.Interop.D3D12_DESCRIPTOR_RANGE_FLAGS; + +#pragma warning disable IDE0052 + +namespace ComputeSharp.__Internals +{ + /// + /// An opaque type describing a captured resource to be used in a shader. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This interface is not intended to be used directly by user code")] + public struct ResourceDescriptor + { + /// + private D3D12_DESCRIPTOR_RANGE_TYPE RangeType; + + /// + private uint NumDescriptors; + + /// + private uint BaseShaderRegister; + + /// + private uint RegisterSpace; + + /// + private D3D12_DESCRIPTOR_RANGE_FLAGS Flags; + + /// + private uint OffsetInDescriptorsFromTableStart; + + /// + /// Initializes a new instance. + /// + /// The type of resource to create the descriptor for. + /// The offset in the resource from the start of the shader mapping. + /// The resulting instance. + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be called directly by user code")] + public static void Create(int type, uint offset, out ResourceDescriptor descriptor) + { + descriptor.RangeType = (D3D12_DESCRIPTOR_RANGE_TYPE)type; + descriptor.NumDescriptors = 1; + descriptor.BaseShaderRegister = offset; + descriptor.RegisterSpace = 0; + descriptor.Flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE; + descriptor.OffsetInDescriptorsFromTableStart = FX.D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND; + } + } +} diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs index 511d5ea0..5651350e 100644 --- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs @@ -157,7 +157,7 @@ namespace ComputeSharp.Shaders ComputeShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList); - shader.LoadDispatchData(in dataLoader, device, x, y, z); + shader.LoadDispatchData(ref dataLoader, device, x, y, z); commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, (uint)groupsZ); commandList.ExecuteAndWaitForCompletion(); @@ -216,7 +216,7 @@ namespace ComputeSharp.Shaders PixelShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList); - shader.LoadDispatchData(in dataLoader, device, x, y, 1); + shader.LoadDispatchData(ref dataLoader, device, x, y, 1); // Load the implicit output texture commandList.D3D12GraphicsCommandList->SetComputeRootDescriptorTable(