From d677900c92d5f504a593010098f9b3efbaa342ba Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 29 Sep 2023 02:18:18 +0200 Subject: [PATCH] Fix generated Data type being generated multiple times --- ....CreateInputDescriptionsProperty.Syntax.cs | 33 ++++++++++----- ...ourceTextureDescriptionsProperty.Syntax.cs | 40 +++---------------- .../ID2D1ShaderGenerator.cs | 8 ++-- 3 files changed, 35 insertions(+), 46 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputDescriptionsProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputDescriptionsProperty.Syntax.cs index 77864746..d5597ef5 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputDescriptionsProperty.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputDescriptionsProperty.Syntax.cs @@ -21,9 +21,9 @@ partial class ID2D1ShaderGenerator /// Creates a instance for the InputDescriptions property. /// /// The input descriptions info gathered for the current shader. - /// Any additional instances needed by the generated code, if needed. + /// Any additional instances needed by the generated code, if needed. /// The resulting instance for the InputDescriptions property. - public static PropertyDeclarationSyntax GetSyntax(EquatableArray inputDescriptions, out TypeDeclarationSyntax[] additionalTypes) + public static PropertyDeclarationSyntax GetSyntax(EquatableArray inputDescriptions, out MemberDeclarationSyntax[] additionalDataMembers) { ExpressionSyntax memoryExpression; @@ -32,7 +32,7 @@ partial class ID2D1ShaderGenerator if (inputDescriptions.Length == 0) { memoryExpression = LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)); - additionalTypes = Array.Empty(); + additionalDataMembers = Array.Empty(); } else { @@ -41,7 +41,7 @@ partial class ID2D1ShaderGenerator IdentifierName("Data"), IdentifierName(nameof(InputDescriptions))); - additionalTypes = new[] { GetArrayDeclaration(inputDescriptions) }; + additionalDataMembers = new[] { GetArrayDeclaration(inputDescriptions) }; } // This code produces a method declaration as follows: @@ -63,7 +63,7 @@ partial class ID2D1ShaderGenerator /// /// The input descriptions info gathered for the current shader. /// The array declaration for the given input descriptions. - private static TypeDeclarationSyntax GetArrayDeclaration(EquatableArray inputDescriptions) + private static MemberDeclarationSyntax GetArrayDeclaration(EquatableArray inputDescriptions) { using ImmutableArrayBuilder inputDescriptionExpressions = ImmutableArrayBuilder.Rent(); @@ -109,7 +109,7 @@ partial class ID2D1ShaderGenerator // // /// The singleton array instance. // public static readonly global::ComputeSharp.D2D1.Interop.D2D1InputDescription[] InputDescriptions = { }; - FieldDeclarationSyntax fieldDeclaration = + return FieldDeclaration( VariableDeclaration( ArrayType(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1InputDescription")) @@ -124,11 +124,24 @@ partial class ID2D1ShaderGenerator Token(SyntaxKind.StaticKeyword), Token(SyntaxKind.ReadOnlyKeyword)) .WithLeadingTrivia(Comment("""/// The singleton array instance.""")); + } + + /// + /// Gets any type declarations for additional members. + /// + /// The additional members that are needed. + /// Any type declarations for additional members. + public static TypeDeclarationSyntax[] GetDataTypeDeclarations(MemberDeclarationSyntax[] memberDeclarations) + { + if (memberDeclarations.Length == 0) + { + return Array.Empty(); + } // Create the container type declaration: // // /// - // /// A container type for input descriptions. + // /// A container type for additional data needed by the shader. // /// // [global::System.CodeDom.Compiler.GeneratedCode("...", "...")] // [global::System.Diagnostics.DebuggerNonUserCode] @@ -137,7 +150,7 @@ partial class ID2D1ShaderGenerator // { // // } - return + TypeDeclarationSyntax dataTypeDeclaration = ClassDeclaration("Data") .AddModifiers(Token(SyntaxKind.FileKeyword), Token(SyntaxKind.StaticKeyword)) .AddAttributeLists( @@ -147,11 +160,13 @@ partial class ID2D1ShaderGenerator AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ID2D1ShaderGenerator).Assembly.GetName().Version.ToString())))))), AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))), AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage"))))) - .AddMembers(fieldDeclaration) + .AddMembers(memberDeclarations) .WithLeadingTrivia( Comment("/// "), Comment("/// A container type for input descriptions."), Comment("/// ")); + + return new[] { dataTypeDeclaration }; } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateResourceTextureDescriptionsProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateResourceTextureDescriptionsProperty.Syntax.cs index f41735b2..dc3fb916 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateResourceTextureDescriptionsProperty.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateResourceTextureDescriptionsProperty.Syntax.cs @@ -21,9 +21,9 @@ partial class ID2D1ShaderGenerator /// Creates a instance for the ResourceTextureDescriptions property. /// /// The resource texture descriptions info gathered for the current shader. - /// Any additional instances needed by the generated code, if needed. + /// Any additional instances needed by the generated code, if needed. /// The resulting instance for the ResourceTextureDescriptions property. - public static PropertyDeclarationSyntax GetSyntax(EquatableArray resourceTextureDescriptions, out TypeDeclarationSyntax[] additionalTypes) + public static PropertyDeclarationSyntax GetSyntax(EquatableArray resourceTextureDescriptions, out MemberDeclarationSyntax[] additionalDataMembers) { ExpressionSyntax memoryExpression; @@ -32,7 +32,7 @@ partial class ID2D1ShaderGenerator if (resourceTextureDescriptions.Length == 0) { memoryExpression = LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)); - additionalTypes = Array.Empty(); + additionalDataMembers = Array.Empty(); } else { @@ -41,7 +41,7 @@ partial class ID2D1ShaderGenerator IdentifierName("Data"), IdentifierName(nameof(ResourceTextureDescriptions))); - additionalTypes = new[] { GetArrayDeclaration(resourceTextureDescriptions) }; + additionalDataMembers = new[] { GetArrayDeclaration(resourceTextureDescriptions) }; } // This code produces a method declaration as follows: @@ -63,7 +63,7 @@ partial class ID2D1ShaderGenerator /// /// The resource texture descriptions info gathered for the current shader. /// The array declaration for the given resource texture descriptions. - private static TypeDeclarationSyntax GetArrayDeclaration(EquatableArray resourceTextureDescriptions) + private static MemberDeclarationSyntax GetArrayDeclaration(EquatableArray resourceTextureDescriptions) { using ImmutableArrayBuilder resourceTextureDescriptionExpressions = ImmutableArrayBuilder.Rent(); @@ -87,7 +87,7 @@ partial class ID2D1ShaderGenerator // // /// The singleton array instance. // public static readonly global::ComputeSharp.D2D1.Interop.D2D1ResourceTextureDescription[] ResourceTextureDescriptions = { }; - FieldDeclarationSyntax fieldDeclaration = + return FieldDeclaration( VariableDeclaration( ArrayType(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1ResourceTextureDescription")) @@ -102,34 +102,6 @@ partial class ID2D1ShaderGenerator Token(SyntaxKind.StaticKeyword), Token(SyntaxKind.ReadOnlyKeyword)) .WithLeadingTrivia(Comment("""/// The singleton array instance.""")); - - // Create the container type declaration: - // - // /// - // /// A container type for input descriptions. - // /// - // [global::System.CodeDom.Compiler.GeneratedCode("...", "...")] - // [global::System.Diagnostics.DebuggerNonUserCode] - // [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] - // file static class Data - // { - // - // } - return - ClassDeclaration("Data") - .AddModifiers(Token(SyntaxKind.FileKeyword), Token(SyntaxKind.StaticKeyword)) - .AddAttributeLists( - AttributeList(SingletonSeparatedList( - Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")).AddArgumentListArguments( - AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ID2D1ShaderGenerator).FullName))), - AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ID2D1ShaderGenerator).Assembly.GetName().Version.ToString())))))), - AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))), - AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage"))))) - .AddMembers(fieldDeclaration) - .WithLeadingTrivia( - Comment("/// "), - Comment("/// A container type for resource texture descriptions."), - Comment("/// ")); } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 603d8eff..f265fa36 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -204,8 +204,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator PropertyDeclarationSyntax effectAuthorProperty = EffectMetadata.GetEffectAuthorSyntax(item.EffectAuthor); PropertyDeclarationSyntax inputCountProperty = InputCount.GetSyntax(item.InputTypes.Length); PropertyDeclarationSyntax inputTypesProperty = InputTypes.GetSyntax(item.InputTypes, out TypeDeclarationSyntax[] inputTypesAdditionalTypes); - PropertyDeclarationSyntax inputDescriptionsProperty = InputDescriptions.GetSyntax(item.InputDescriptions, out TypeDeclarationSyntax[] inputDescriptionsAdditionalTypes); - PropertyDeclarationSyntax resourceTextureDescriptionsProperty = ResourceTextureDescriptions.GetSyntax(item.ResourceTextureDescriptions, out TypeDeclarationSyntax[] resourceTexturesAditionalTypes); + PropertyDeclarationSyntax inputDescriptionsProperty = InputDescriptions.GetSyntax(item.InputDescriptions, out MemberDeclarationSyntax[] inputDescriptionsAdditionalDataMembers); + PropertyDeclarationSyntax resourceTextureDescriptionsProperty = ResourceTextureDescriptions.GetSyntax(item.ResourceTextureDescriptions, out MemberDeclarationSyntax[] resourceTexturesAditionalDataMembers); PropertyDeclarationSyntax hlslStringProperty = HlslSource.GetSyntax(item.HlslInfoKey.HlslSource, item.Hierarchy.Hierarchy.Length); PropertyDeclarationSyntax hlslBytecodeProperty = LoadBytecode.GetHlslBytecodeSyntax(item.HlslInfo, out Func fixup, out TypeDeclarationSyntax[] hlslBytecodeAdditionalTypes); PropertyDeclarationSyntax shaderProfileProperty = LoadBytecode.GetShaderProfileSyntax(item.HlslInfoKey.EffectiveShaderProfile); @@ -228,7 +228,9 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator }, additionalMemberDeclarations: new TypeDeclarationSyntax[][] { - inputTypesAdditionalTypes, inputDescriptionsAdditionalTypes, resourceTexturesAditionalTypes, hlslBytecodeAdditionalTypes, loadDispatchDataAditionalTypes + loadDispatchDataAditionalTypes, + InputDescriptions.GetDataTypeDeclarations(new[] { inputDescriptionsAdditionalDataMembers, resourceTexturesAditionalDataMembers }.SelectMany(static members => members).ToArray()), + inputTypesAdditionalTypes, hlslBytecodeAdditionalTypes } .SelectMany(static types => types) .Cast()