Added support for static methods in shaders
このコミットが含まれているのは:
1 親
44d854117d
コミット
e57d7a9354
3ファイル変更
+68
-9
検索条件に一致するファイルはありません
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using ComputeSharp.SourceGenerators.Mappings;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
@@ -144,5 +145,23 @@ namespace ComputeSharp.SourceGenerators.Extensions
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="MethodDeclarationSyntax"/> instance with no accessibility modifiers.
|
||||
/// </summary>
|
||||
/// <param name="node">The input <see cref="MethodDeclarationSyntax"/> node.</param>
|
||||
/// <returns>A node just like <paramref name="node"/> but with no accessibility modifiers.</returns>
|
||||
[Pure]
|
||||
public static MethodDeclarationSyntax WithoutAccessibilityModifiers(this MethodDeclarationSyntax node)
|
||||
{
|
||||
return node.WithModifiers(TokenList(node.Modifiers.Where(static modifier => modifier.Kind() switch
|
||||
{
|
||||
SyntaxKind.PublicKeyword => false,
|
||||
SyntaxKind.PrivateKeyword => false,
|
||||
SyntaxKind.ProtectedKeyword => false,
|
||||
SyntaxKind.InternalKeyword => false,
|
||||
_ => true
|
||||
}).ToArray()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,8 +41,9 @@ namespace ComputeSharp.SourceGenerators
|
||||
// Only process compute shader types
|
||||
if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue;
|
||||
|
||||
// The list to use to track all discovered custom types
|
||||
// We need to sets to track all discovered custom types and static methods
|
||||
HashSet<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
|
||||
Dictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods = new(SymbolEqualityComparer.Default);
|
||||
|
||||
// Helper that converts a sequence of string sequences into a nested array expression.
|
||||
// That is, this applies the following transformation:
|
||||
@@ -72,15 +73,23 @@ namespace ComputeSharp.SourceGenerators
|
||||
.AddExpressions(values.Select(static value => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(value))).ToArray()));
|
||||
}
|
||||
|
||||
// Explore the syntax tree and extract the processed info
|
||||
var processedMembers = GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray();
|
||||
var processedMethods = GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes, staticMethods).ToArray();
|
||||
var processedTypes = GetProcessedTypes(discoveredTypes).ToArray();
|
||||
|
||||
// Merge the static methods as well
|
||||
processedMethods = processedMethods.Concat(staticMethods.Select(foo => new[] { "", foo.Value.ToFullString() })).ToArray();
|
||||
|
||||
// Create the compilation unit with the source attribute
|
||||
var source =
|
||||
CompilationUnit().AddAttributeLists(
|
||||
AttributeList(SingletonSeparatedList(
|
||||
Attribute(IdentifierName(typeof(IComputeShaderSourceAttribute).FullName)).AddArgumentListArguments(
|
||||
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(structDeclarationSymbol.GetFullMetadataName()))),
|
||||
AttributeArgument(NestedArrayExpression(GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray())),
|
||||
AttributeArgument(NestedArrayExpression(GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes).ToArray())),
|
||||
AttributeArgument(ArrayExpression(GetProcessedTypes(discoveredTypes))))))
|
||||
AttributeArgument(NestedArrayExpression(processedMembers)),
|
||||
AttributeArgument(NestedArrayExpression(processedMethods)),
|
||||
AttributeArgument(ArrayExpression(processedTypes)))))
|
||||
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
|
||||
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
|
||||
.NormalizeWhitespace()
|
||||
@@ -123,13 +132,15 @@ namespace ComputeSharp.SourceGenerators
|
||||
/// </summary>
|
||||
/// <param name="structDeclaration">The <see cref="StructDeclarationSyntax"/> instance for the current type.</param>
|
||||
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance for the type to process.</param>
|
||||
/// <param name="types">The collection of currently discovered types.</param>
|
||||
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
|
||||
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
|
||||
/// <returns>A sequence of processed methods in <paramref name="structDeclaration"/>.</returns>
|
||||
[Pure]
|
||||
private static IEnumerable<IEnumerable<string>> GetProcessedMethods(
|
||||
StructDeclarationSyntax structDeclaration,
|
||||
SemanticModel semanticModel,
|
||||
ICollection<INamedTypeSymbol> types)
|
||||
ICollection<INamedTypeSymbol> discoveredTypes,
|
||||
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods)
|
||||
{
|
||||
// Find all declared methods in the type
|
||||
ImmutableArray<MethodDeclarationSyntax> methodDeclarations = (
|
||||
@@ -140,7 +151,7 @@ namespace ComputeSharp.SourceGenerators
|
||||
foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations)
|
||||
{
|
||||
IMethodSymbol methodDeclarationSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration)!;
|
||||
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, types);
|
||||
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods);
|
||||
|
||||
// Rewrite the method syntax tree
|
||||
var processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia();
|
||||
|
||||
@@ -26,6 +26,11 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
|
||||
/// </summary>
|
||||
private readonly ICollection<INamedTypeSymbol> discoveredTypes;
|
||||
|
||||
/// <summary>
|
||||
/// The collection of discovered static methods.
|
||||
/// </summary>
|
||||
private readonly IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods;
|
||||
|
||||
/// <summary>
|
||||
/// The collection of processed local functions in the current tree.
|
||||
/// </summary>
|
||||
@@ -41,10 +46,15 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
|
||||
/// </summary>
|
||||
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance for the target syntax tree.</param>
|
||||
/// <param name="discoveredTypes">The set of discovered custom types.</param>
|
||||
public ShaderSourceRewriter(SemanticModel semanticModel, ICollection<INamedTypeSymbol> discoveredTypes)
|
||||
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
|
||||
public ShaderSourceRewriter(
|
||||
SemanticModel semanticModel,
|
||||
ICollection<INamedTypeSymbol> discoveredTypes,
|
||||
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods)
|
||||
{
|
||||
this.semanticModel = semanticModel;
|
||||
this.discoveredTypes = discoveredTypes;
|
||||
this.staticMethods = staticMethods;
|
||||
this.localFunctions = new();
|
||||
}
|
||||
|
||||
@@ -149,7 +159,10 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node)
|
||||
{
|
||||
return ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!).WithBlockBody();
|
||||
return
|
||||
((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!)
|
||||
.WithBlockBody()
|
||||
.WithoutAccessibilityModifiers();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -232,6 +245,22 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
|
||||
|
||||
return updatedNode.WithExpression(ParseExpression(functionIdentifier));
|
||||
}
|
||||
|
||||
// If the method is an external static method, import and rewrite it as well.
|
||||
// This assumes that the target method is actually part of the same compilation.
|
||||
var methodIdentifier = method.GetFullMetadataName().Replace(".", "__");
|
||||
|
||||
if (!this.staticMethods.ContainsKey(method))
|
||||
{
|
||||
ShaderSourceRewriter shaderSourceRewriter = new(this.semanticModel, this.discoveredTypes, this.staticMethods);
|
||||
MethodDeclarationSyntax
|
||||
methodNode = (MethodDeclarationSyntax)method.DeclaringSyntaxReferences[0].GetSyntax(),
|
||||
processedMethod = shaderSourceRewriter.Visit(methodNode)!.NormalizeWhitespace().WithoutTrivia();
|
||||
|
||||
this.staticMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier)));
|
||||
}
|
||||
|
||||
return updatedNode.WithExpression(ParseExpression(methodIdentifier));
|
||||
}
|
||||
|
||||
return updatedNode;
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする