From 5a4b7a8a8649de994f2e77a748cbf0ea88237c82 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 27 Apr 2022 23:50:26 +0200 Subject: [PATCH] Add ID2D1Shader.GetInputCount() method generation --- ...erator.CreateGetInputCountMethod.Syntax.cs | 38 +++++++++++++++++++ .../ID2D1ShaderGenerator.cs | 22 +++++++++-- .../Interfaces/__Internals/ID2D1Shader.cs | 8 ++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs new file mode 100644 index 00000000..d0496ef4 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs @@ -0,0 +1,38 @@ +using ComputeSharp.D2D1.__Internals; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +#pragma warning disable CS0618 + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +partial class ID2D1ShaderGenerator +{ + /// + /// A helper with all logic to generate the GetInputCount method. + /// + partial class GetInputCount + { + /// + /// Creates a instance for the GetInputCount method. + /// + /// The number of inputs for the shader. + /// The resulting instance for the GetInputCount method. + public static MethodDeclarationSyntax GetSyntax(int inputCount) + { + // This code produces a method declaration as follows: + // + // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputCount() + // { + // return ; + // } + return + MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier("GetInputCount")) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) + .WithBody(Block(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputCount))))); + } + } +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 62b802d5..5851d946 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -51,7 +51,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Get the dispatch data, HLSL source and embedded bytecode info. This info is computed on the // same step as parts are shared in following sub-branches in the incremental generator pipeline. - IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result Dispatch, Result Source)> shaderInfoWithErrors = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result Dispatch, Result Source, int InputCount)> shaderInfoWithErrors = shaderDeclarations .Combine(context.CompilationProvider) .Select(static (item, token) => @@ -92,13 +92,29 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator return ( item.Left.Hierarchy, new Result(dispatchDataInfo, dispatchDataDiagnostics), - new Result(sourceInfo, hlslSourceDiagnostics)); + new Result(sourceInfo, hlslSourceDiagnostics), + inputCount); }); // Output the diagnostics context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Dispatch.Errors)); context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Source.Errors)); + // Filter items to enable caching for the input count methods + IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo = + shaderInfoWithErrors + .Select(static (item, token) => (item.Hierarchy, item.InputCount)) + .WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer.Default); + + // Generate the GetInputCount() methods + context.RegisterSourceOutput(inputCountInfo, static (context, item) => + { + MethodDeclarationSyntax getInputCountMethod = GetInputCount.GetSyntax(item.InputCount); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getInputCountMethod, false); + + context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetInputCount)}", compilationUnit.ToFullString()); + }); + // Filter all items to enable caching at a coarse level, and remove diagnostics IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, HlslShaderSourceInfo Source)> shaderInfo = shaderInfoWithErrors @@ -106,7 +122,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator .WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, EqualityComparer.Default); // Get a filtered sequence to enable caching - IncrementalValuesProvider< (HierarchyInfo Hierarchy, DispatchDataInfo Dispatch)> dispatchDataInfo = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch)> dispatchDataInfo = shaderInfo .Select(static (item, token) => (item.Hierarchy, item.Dispatch)) .WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default); diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 8159e2ef..7d503b40 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -19,6 +19,14 @@ public interface ID2D1Shader [Obsolete("This method is not intended to be called directly by user code")] void InitializeFromDispatchData(ReadOnlySpan data); + /// + /// Gets the number of inputs for the current shader. + /// + /// The number of inputs for the current shader. + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be used directly by user code")] + uint GetInputCount(); + /// /// Loads the dispatch data for the shader. ///