diff --git a/samples/ComputeSharp.Sample.Shared/Program.cs b/samples/ComputeSharp.Sample.Shared/Program.cs index db49f66a..78b04240 100644 --- a/samples/ComputeSharp.Sample.Shared/Program.cs +++ b/samples/ComputeSharp.Sample.Shared/Program.cs @@ -42,7 +42,7 @@ namespace ComputeSharp.Sample /// Kernel for . /// [AutoConstructor] - private readonly partial struct MainKernel : IComputeShader + internal readonly partial struct MainKernel : IComputeShader { private readonly ReadWriteBuffer buffer; diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs new file mode 100644 index 00000000..7cf539b8 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs @@ -0,0 +1,93 @@ +using System.Collections.Immutable; +using System.Linq; +using System.Text; +using ComputeSharp.SourceGenerators.Extensions; +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 IComputeShaderHashCodeGenerator : 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(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue; + + TypeSyntax shaderType = ParseTypeName(structDeclarationSymbol.ToDisplayString()); + BlockSyntax block = Block(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, ParseToken("0")))); + + // Create a static method to create the combined hashcode for a given shader type. + // This code takes a block syntax and produces a compilation unit as follows: + // + // using System; + // using System.ComponentModel; + // + // namespace ComputeSharp.__Internals + // { + // internal static partial class HashCodeProvider + // { + // [System.ComponentModel.EditorBrowsable(EditorBrowsableState.Never)] + // [System.Obsolete("This method is intended for internal usage only")] + // public static int CombineHashCode(int hash, in ShaderType shader) + // { + // return ...; + // } + // } + // } + var source = + CompilationUnit().AddUsings( + UsingDirective(IdentifierName("System")), + UsingDirective(IdentifierName("System.ComponentModel"))).AddMembers( + NamespaceDeclaration(IdentifierName("ComputeSharp.__Internals")).AddMembers( + ClassDeclaration("HashCodeProvider").AddModifiers( + Token(SyntaxKind.InternalKeyword), + Token(SyntaxKind.StaticKeyword), + Token(SyntaxKind.PartialKeyword)).AddMembers( + MethodDeclaration( + PredefinedType(Token(SyntaxKind.IntKeyword)), + Identifier("CombineHash")).AddAttributeLists( + AttributeList(SingletonSeparatedList( + Attribute(IdentifierName("EditorBrowsable")).AddArgumentListArguments( + AttributeArgument(ParseExpression("EditorBrowsableState.Never"))))), + AttributeList(SingletonSeparatedList( + Attribute(IdentifierName("Obsolete")).AddArgumentListArguments( + AttributeArgument(LiteralExpression( + SyntaxKind.StringLiteralExpression, + Literal("This method is intended for internal usage only"))))))).AddModifiers( + Token(SyntaxKind.PublicKeyword), + Token(SyntaxKind.StaticKeyword)).AddParameterListParameters( + Parameter(Identifier("hash")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))), + Parameter(Identifier("shader")).WithModifiers(TokenList(Token(SyntaxKind.InKeyword))).WithType(shaderType)) + .WithBody(block)))) + .NormalizeWhitespace() + .ToFullString(); + + // Add the method source attribute + context.AddSource(structDeclarationSymbol.GetGeneratedFileName(), SourceText.From(source, Encoding.UTF8)); + } + } + } +} +