From 67d1d92cae3abf9c06cbcf4aa480a0a934c4fd2d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 12 Jan 2021 16:22:23 +0100 Subject: [PATCH] Added source generator support for shared buffers --- .../IComputeShaderSourceAttribute.cs | 10 +++++- .../AutoConstructorGenerator.cs | 2 +- .../Helpers/SyntaxFactoryHelper.cs | 32 +++++++++++++++++++ .../IComputeShaderDataLoaderGenerator.cs | 2 ++ .../IComputeShaderHashCodeGenerator.cs | 2 ++ .../IComputeShaderSourceGenerator.cs | 15 ++++----- 6 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/ComputeSharp.Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs b/src/ComputeSharp.Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs index 91c92c52..7cd9407c 100644 --- a/src/ComputeSharp.Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs +++ b/src/ComputeSharp.Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs @@ -25,13 +25,15 @@ namespace ComputeSharp.__Internals /// The source code for the method. /// The collection of processed methods. /// The collection of discovered constants. + /// The collection of group shared buffers public IComputeShaderSourceAttribute( string shaderTypeName, string[] types, object[] args, string executeMethod, string[] methods, - object[] constants) + object[] constants, + object[] sharedBuffers) { ShaderTypeName = shaderTypeName; Types = types; @@ -39,6 +41,7 @@ namespace ComputeSharp.__Internals ExecuteMethod = executeMethod; Methods = methods; Constants = constants.Cast().ToDictionary(static c => c[0], static c => c[1]); + SharedBuffers = sharedBuffers.Cast().ToDictionary(static t => (string)t[0], static t => ((string)t[1], (int?)t[2])); } /// @@ -71,6 +74,11 @@ namespace ComputeSharp.__Internals /// internal IReadOnlyDictionary Constants { get; } + /// + /// Gets the collection of discovered constants. + /// + internal IReadOnlyDictionary SharedBuffers { get; } + /// /// Gets the associated instance for a specified type. /// diff --git a/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs index 40ebf95d..7111ff4b 100644 --- a/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/AutoConstructorGenerator.cs @@ -43,7 +43,7 @@ namespace ComputeSharp.SourceGenerators var structModifiers = structDeclaration.Modifiers; var fields = ( from fieldSymbol in structDeclarationSymbol.GetMembers().OfType() - where !fieldSymbol.IsConst + where !fieldSymbol.IsConst && !fieldSymbol.IsStatic let typeName = fieldSymbol.Type!.ToDisplayString() let fieldFullType = ParseTypeName(typeName) select (Type: fieldFullType, Identifier: Identifier(fieldSymbol.Name))).ToImmutableArray(); diff --git a/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs b/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs index 5fb2c00a..894f6630 100644 --- a/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs +++ b/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs @@ -57,5 +57,37 @@ namespace ComputeSharp.SourceGenerators.Helpers ImplicitArrayCreationExpression(InitializerExpression(SyntaxKind.ArrayInitializerExpression).AddExpressions( group.Select(static item => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(item))).ToArray()))).ToArray())); } + + /// + /// Creates an [] array expression with the nested groups in the input sequence. + /// + /// That it, it applies the following transformation: + /// + /// { ("K1", "V1", I1), ("K2", "V2", null) } => new object[] { new object[] { "K1", "V1", I1 }, new object[] { "K2", "V2", null } } + /// + /// + /// + /// The input sequence of groups. + /// An instance with the described contents. + [Pure] + public static ArrayCreationExpressionSyntax NestedArrayExpression(IEnumerable<(string A, string B, int? C)> groups) + { + return + ArrayCreationExpression( + ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword))) + .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression())))) + .WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression) + .AddExpressions(groups.Select(static group => + ArrayCreationExpression( + ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword))) + .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression())))) + .WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression) + .AddExpressions( + LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(group.A)), + LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(group.B)), + group.C is int i + ? LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(i)) + : LiteralExpression(SyntaxKind.NullLiteralExpression)))).ToArray())); + } } } diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs index 2922aa8a..834fb2d9 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs @@ -128,6 +128,8 @@ namespace ComputeSharp.SourceGenerators foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { + if (fieldSymbol.IsStatic) continue; + INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type; string typeName = typeSymbol.GetFullMetadataName(); diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs index ae194820..127677c7 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs @@ -120,6 +120,8 @@ namespace ComputeSharp.SourceGenerators { foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { + if (fieldSymbol.IsStatic) continue; + INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type; if (typeSymbol.TypeKind != TypeKind.Delegate) continue; diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs index fd39af43..93f2d6a7 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs @@ -50,6 +50,7 @@ namespace ComputeSharp.SourceGenerators // Explore the syntax tree and extract the processed info var processedMembers = GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray(); + var sharedBuffers = GetGroupSharedMembers(structDeclarationSymbol, discoveredTypes).ToArray(); var (entryPoint, localFunctions) = GetProcessedMethods(structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions); var processedTypes = GetProcessedTypes(discoveredTypes).ToArray(); var processedMethods = localFunctions.Concat(staticMethods.Values).Select(static method => method.NormalizeWhitespace().ToFullString()).ToArray(); @@ -66,7 +67,8 @@ namespace ComputeSharp.SourceGenerators AttributeArgument(NestedArrayExpression(processedMembers)), AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(entryPoint))), AttributeArgument(ArrayExpression(processedMethods)), - AttributeArgument(NestedArrayExpression(processedConstants))))) + AttributeArgument(NestedArrayExpression(processedConstants)), + AttributeArgument(NestedArrayExpression(sharedBuffers))))) .WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList())) .WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword)))) .NormalizeWhitespace() @@ -115,7 +117,7 @@ namespace ComputeSharp.SourceGenerators /// The collection of currently discovered types. /// A sequence of captured members in . [Pure] - private static IEnumerable> GetGroupSharedMembers(INamedTypeSymbol structDeclarationSymbol, ICollection types) + private static IEnumerable<(string Name, string Type, int? Count)> GetGroupSharedMembers(INamedTypeSymbol structDeclarationSymbol, ICollection types) { foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { @@ -129,12 +131,9 @@ namespace ComputeSharp.SourceGenerators _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping); - yield return new[] - { - mapping ?? fieldSymbol.Name, - typeName, - bufferSize?.ToString() ?? "-1" - }; + yield return (mapping ?? fieldSymbol.Name, typeName, bufferSize); + + types.Add((INamedTypeSymbol)typeSymbol.ElementType); } }