diff --git a/src/ComputeSharp.Core/Attributes/IComputeShaderSource.cs b/src/ComputeSharp.Core/Attributes/IComputeShaderSource.cs new file mode 100644 index 00000000..d76d9681 --- /dev/null +++ b/src/ComputeSharp.Core/Attributes/IComputeShaderSource.cs @@ -0,0 +1,43 @@ +using System; +using System.ComponentModel; + +namespace ComputeSharp +{ + /// + /// A shader that indicates that a target shader type should get an automatic constructor for all fields. + /// This attribute is not meant to be directly used by applications using ComputeSharp. + /// + [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This attribute is meant to be used from the source generator only", true)] + public sealed class IComputeShaderSourceAttribute : Attribute + { + /// + /// Creates a new instance with the specified parameters. + /// + /// The fully qualified name of the shader type. + /// The name of the current method. + /// The source code of the target method. + public IComputeShaderSourceAttribute(string shaderTypeName, string methodName, string source) + { + ShaderTypeName = shaderTypeName; + MethodName = methodName; + Source = source; + } + + /// + /// Gets the fully qualified name of the shader type. + /// + public string ShaderTypeName { get; } + + /// + /// Gets the name of the current method. + /// + public string MethodName { get; } + + /// + /// Gets the source code of the target method. + /// + public string Source { get; } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs new file mode 100644 index 00000000..ea02d431 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs @@ -0,0 +1,81 @@ +using System.Collections.Immutable; +using System.Linq; +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Text; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace ComputeSharp.SourceGenerators +{ + [Generator] + public class IComputeShaderSourceAttributeGenerator : ISourceGenerator + { + /// + public void Initialize(GeneratorInitializationContext context) + { + } + + /// + public void Execute(GeneratorExecutionContext context) + { + // Find all the struct declarations + ImmutableArray structDeclarations = ( + from tree in context.Compilation.SyntaxTrees + from structDeclaration in tree.GetRoot().DescendantNodes().OfType() + select structDeclaration).ToImmutableArray(); + + foreach (StructDeclarationSyntax structDeclaration in structDeclarations) + { + SemanticModel semanticModel = context.Compilation.GetSemanticModel(structDeclaration.SyntaxTree); + INamedTypeSymbol structDeclarationSymbol = semanticModel.GetDeclaredSymbol(structDeclaration)!; + + // Only process compute shader types + if (!structDeclarationSymbol.Interfaces.Any(interfaceSymbol => interfaceSymbol.Name == "IComputeShader")) continue; + + var structFullName = structDeclarationSymbol.ToDisplayString(new SymbolDisplayFormat( + SymbolDisplayGlobalNamespaceStyle.Omitted, + SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces, + SymbolDisplayGenericsOptions.IncludeTypeParameters)); + + // Find all declared methods in the type + ImmutableArray methodDeclarations = ( + from syntaxNode in structDeclaration.DescendantNodes() + where syntaxNode.IsKind(SyntaxKind.MethodDeclaration) + select (MethodDeclarationSyntax)syntaxNode).ToImmutableArray(); + + foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations) + { + IMethodSymbol methodDeclarationSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration)!; + + // Extract the source from either a block or an expression body + var methodSource = ((methodDeclaration.Body, methodDeclaration.ExpressionBody) switch + { + (BlockSyntax block, _) => block, + (_, ArrowExpressionClauseSyntax arrow) => Block(ExpressionStatement(arrow.Expression)), + _ => Block() + }).NormalizeWhitespace().ToFullString(); + + // Create the compilation unit with the source attribute + var source = + CompilationUnit().AddAttributeLists( + AttributeList(SingletonSeparatedList( + Attribute(IdentifierName("ComputeSharp.IComputeShaderSource")).AddArgumentListArguments( + AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(structFullName))), + AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(methodDeclarationSymbol.Name))), + AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(methodSource)))))) + .WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword)))) + .NormalizeWhitespace() + .ToFullString(); + + var generatedFileName = $"__ComputeSharp_IComputeShaderSourceAttribute_{structDeclarationSymbol.Name}_{methodDeclarationSymbol.Name}"; + + // Add the method source attribute + context.AddSource(generatedFileName, SourceText.From(source, Encoding.UTF8)); + } + } + } + } +} +