diff --git a/src/ComputeSharp.Core/Attributes/IComputeShaderSourceAttribute.cs b/src/ComputeSharp.Core/Attributes/IComputeShaderSourceAttribute.cs index a6752761..78a86340 100644 --- a/src/ComputeSharp.Core/Attributes/IComputeShaderSourceAttribute.cs +++ b/src/ComputeSharp.Core/Attributes/IComputeShaderSourceAttribute.cs @@ -20,15 +20,17 @@ namespace ComputeSharp /// Creates a new instance with the specified parameters. /// /// The fully qualified name of the shader type. - /// The mapped collection of shader fields. - /// The mapped collection of shader methods. /// The collection of custom types. - public IComputeShaderSourceAttribute(string shaderTypeName, object[] args, object[] methods, string[] types) + /// The mapped collection of shader fields. + /// The source code for the method. + /// The collection of processed methods. + public IComputeShaderSourceAttribute(string shaderTypeName, string[] types, object[] args, string executeMethod, string[] methods) { ShaderTypeName = shaderTypeName; - Fields = args.Cast().ToDictionary(static arg => arg[0], static arg => (arg[1], arg[2])); - Methods = methods.Cast().ToDictionary(static method => method[0], static method => method[1]); Types = types; + Fields = args.Cast().ToDictionary(static arg => arg[0], static arg => (arg[1], arg[2])); + ExecuteMethod = executeMethod; + Methods = methods; } /// @@ -36,20 +38,25 @@ namespace ComputeSharp /// internal string ShaderTypeName { get; } + /// + /// Gets the collection of processed custom types. + /// + internal IReadOnlyCollection Types { get; } + /// /// Gets the mapping of field names. /// internal IReadOnlyDictionary Fields { get; } /// - /// Gets the mapping of methods and their source code. + /// Gets the source code for the method. /// - internal IReadOnlyDictionary Methods { get; } + internal string ExecuteMethod { get; } /// - /// Gets the collection of processed custom types. + /// Gets the collection of processed methods. /// - internal IReadOnlyCollection Types { get; } + internal IReadOnlyCollection Methods { get; } /// /// Gets the associated instance for a specified type. diff --git a/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs b/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs index d411ba63..0bdd616b 100644 --- a/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs +++ b/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs @@ -100,8 +100,8 @@ namespace ComputeSharp.Shaders.Renderer } } - // Local functions - foreach (var function in info.LocalFunctionsInfo) + // Captured methods + foreach (var function in info.MethodsInfo) { builder.AppendLine(); builder.AppendLine(function); diff --git a/src/ComputeSharp.Shaders/Translation/IShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/IShaderLoader.cs index 08f75f95..9a4724a7 100644 --- a/src/ComputeSharp.Shaders/Translation/IShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/IShaderLoader.cs @@ -24,9 +24,9 @@ namespace ComputeSharp.Shaders.Translation public IReadOnlyList FieldsInfo { get; } /// - /// Gets the collection of items for the shader. + /// Gets the collection of methods to be used in the shader. /// - public IReadOnlyCollection LocalFunctionsInfo { get; } + public IReadOnlyCollection MethodsInfo { get; } /// /// Gets the collection of declared types for the shader. diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs index 08ea6d57..347bbcfc 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs @@ -56,11 +56,6 @@ namespace ComputeSharp.Shaders.Translation /// private readonly List fieldsInfo = new(); - /// - /// The with the items for the shader local functions. - /// - private readonly List localFunctionsInfo = new(); - /// /// Creates a new instance. /// @@ -86,7 +81,7 @@ namespace ComputeSharp.Shaders.Translation public IReadOnlyList FieldsInfo => this.fieldsInfo; /// - public IReadOnlyCollection LocalFunctionsInfo => this.localFunctionsInfo; + public IReadOnlyCollection MethodsInfo { get; private set; } /// public IReadOnlyCollection DeclaredTypes { get; private set; } @@ -201,18 +196,9 @@ namespace ComputeSharp.Shaders.Translation /// private void LoadMethodMetadata() { - // Extract the method sources - foreach (var method in Attribute.Methods) - { - switch (method.Key) - { - case nameof(IComputeShader.Execute): EntryPoint = method.Value; break; - default: this.localFunctionsInfo.Add(method.Value); break; - } - } - - // Extract the dependent types DeclaredTypes = Attribute.Types; + EntryPoint = Attribute.ExecuteMethod; + MethodsInfo = Attribute.Methods; } } } diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs index d127b6de..ea60def7 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs @@ -75,11 +75,9 @@ namespace ComputeSharp.SourceGenerators // Explore the syntax tree and extract the processed info var processedMembers = GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray(); - var processedMethods = GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes, staticMethods).ToArray(); + var (entryPoint, localFunctions) = GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes, staticMethods); var processedTypes = GetProcessedTypes(discoveredTypes).ToArray(); - - // Merge the static methods as well - processedMethods = processedMethods.Concat(staticMethods.Select(foo => new[] { "", foo.Value.ToFullString() })).ToArray(); + var processedMethods = localFunctions.Concat(staticMethods.Values.Select(static method => method.ToFullString())).ToArray(); // Create the compilation unit with the source attribute var source = @@ -87,9 +85,10 @@ namespace ComputeSharp.SourceGenerators AttributeList(SingletonSeparatedList( Attribute(IdentifierName(typeof(IComputeShaderSourceAttribute).FullName)).AddArgumentListArguments( AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(structDeclarationSymbol.GetFullMetadataName()))), + AttributeArgument(ArrayExpression(processedTypes)), AttributeArgument(NestedArrayExpression(processedMembers)), - AttributeArgument(NestedArrayExpression(processedMethods)), - AttributeArgument(ArrayExpression(processedTypes))))) + AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(entryPoint))), + AttributeArgument(ArrayExpression(processedMethods))))) .WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList())) .WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword)))) .NormalizeWhitespace() @@ -134,9 +133,9 @@ namespace ComputeSharp.SourceGenerators /// The instance for the type to process. /// The collection of currently discovered types. /// The set of discovered and processed static methods. - /// A sequence of processed methods in . + /// A sequence of processed methods in , and the entry point. [Pure] - private static IEnumerable> GetProcessedMethods( + private static (string EntryPoint, IEnumerable Methods) GetProcessedMethods( StructDeclarationSyntax structDeclaration, SemanticModel semanticModel, ICollection discoveredTypes, @@ -148,6 +147,9 @@ namespace ComputeSharp.SourceGenerators where syntaxNode.IsKind(SyntaxKind.MethodDeclaration) select (MethodDeclarationSyntax)syntaxNode).ToImmutableArray(); + string? entryPoint = null; + List methods = new(); + foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations) { IMethodSymbol methodDeclarationSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration)!; @@ -166,19 +168,19 @@ namespace ComputeSharp.SourceGenerators var parameterName = methodDeclarationSymbol.Parameters[0].Name; processedMethod = new ExecuteMethodRewriter(parameterName).Visit(processedMethod)!; + + entryPoint = processedMethod.NormalizeWhitespace().ToFullString(); } - - // Produce the final method source - var processedMethodSource = processedMethod.NormalizeWhitespace().ToFullString(); - - yield return new[] { methodDeclarationSymbol.Name, processedMethodSource }; + else methods.Add(processedMethod.NormalizeWhitespace().ToFullString()); // Emit the extracted local functions foreach (var localFunction in shaderSourceRewriter.LocalFunctions) { - yield return new[] { localFunction.Key, localFunction.Value.NormalizeWhitespace().ToFullString() }; + methods.Add(localFunction.Value.NormalizeWhitespace().ToFullString()); } } + + return (entryPoint!, methods); } ///