From 32ceaf3def73e144e025af595ac72638762f9da3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 28 Sep 2023 00:55:28 +0200 Subject: [PATCH] Switch ID2D1Shader.InputCount to a property --- ...erator.CreateInputCountProperty.Syntax.cs} | 22 +++++++++---------- .../ID2D1ShaderGenerator.cs | 10 ++++----- .../Interfaces/__Internals/ID2D1Shader.cs | 3 +-- .../Shaders/Interop/D2D1PixelShader.cs | 2 +- 4 files changed, 17 insertions(+), 20 deletions(-) rename src/ComputeSharp.D2D1.SourceGenerators/{ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs => ID2D1ShaderGenerator.CreateInputCountProperty.Syntax.cs} (52%) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputCountProperty.Syntax.cs similarity index 52% rename from src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs rename to src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputCountProperty.Syntax.cs index 92326c75..e92a4b0e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputCountProperty.Syntax.cs @@ -11,28 +11,26 @@ namespace ComputeSharp.D2D1.SourceGenerators; partial class ID2D1ShaderGenerator { /// - /// A helper with all logic to generate the GetInputCount method. + /// A helper with all logic to generate the InputCount property. /// - private static partial class GetInputCount + private static partial class InputCount { /// - /// Creates a instance for the GetInputCount method. + /// Creates a instance for the InputCount property. /// /// The number of inputs for the shader. - /// The resulting instance for the GetInputCount method. - public static MethodDeclarationSyntax GetSyntax(int inputCount) + /// The resulting instance for the InputCount property. + public static PropertyDeclarationSyntax GetSyntax(int inputCount) { - // This code produces a method declaration as follows: + // This code produces a property declaration as follows: // - // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputCount() - // { - // return ; - // } + // readonly int global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputCount => ; return - MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier(nameof(GetInputCount))) + PropertyDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), Identifier(nameof(InputCount))) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) - .WithBody(Block(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputCount))))); + .WithExpressionBody(ArrowExpressionClause(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputCount)))) + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 7766cf98..5194aafa 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -243,18 +243,18 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.EffectAuthor.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Get the GetInputCount() info (hierarchy and input count) + // Get the InputCount info (hierarchy and input count) IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo = shaderInfoWithErrors .Select(static (item, _) => (item.Hierarchy, item.InputTypes.InputTypes.Length)); - // Generate the GetInputCount() methods + // Generate the InputCount properties context.RegisterSourceOutput(inputCountInfo, static (context, item) => { - MethodDeclarationSyntax getInputCountMethod = GetInputCount.GetSyntax(item.InputCount); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, getInputCountMethod, canUseSkipLocalsInit: false); + PropertyDeclarationSyntax inputCountProperty = InputCount.GetSyntax(item.InputCount); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, inputCountProperty, canUseSkipLocalsInit: false); - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetInputCount)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(InputCount)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); // Get the InputTypes info (hierarchy and input types) diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 236ab3b8..e3de74cf 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -71,10 +71,9 @@ public interface ID2D1Shader /// /// 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(); + int InputCount { get; } /// /// Gets the input types for the current shader. diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index e2cd3232..60f63619 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -255,7 +255,7 @@ public static class D2D1PixelShader { Unsafe.SkipInit(out T shader); - return (int)shader.GetInputCount(); + return shader.InputCount; } ///