Implemented SG-powered mapping of captured fields
このコミットが含まれているのは:
1 親
e711ef4c60
コミット
3d7c4c7cb6
3ファイル変更
+135
-67
検索条件に一致するファイルはありません
@@ -1,6 +1,9 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using ComputeSharp.Shaders.Mappings;
|
||||
using ComputeSharp.SourceGenerators.Extensions;
|
||||
using ComputeSharp.SourceGenerators.SyntaxRewriters;
|
||||
using Microsoft.CodeAnalysis;
|
||||
@@ -36,56 +39,104 @@ namespace ComputeSharp.SourceGenerators
|
||||
INamedTypeSymbol structDeclarationSymbol = semanticModel.GetDeclaredSymbol(structDeclaration)!;
|
||||
|
||||
// Only process compute shader types
|
||||
if (!structDeclarationSymbol.Interfaces.Any(interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue;
|
||||
if (!structDeclarationSymbol.Interfaces.Any(interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue;
|
||||
|
||||
var structFullName = structDeclarationSymbol.GetFullMetadataName();
|
||||
|
||||
// Find all declared methods in the type
|
||||
ImmutableArray<MethodDeclarationSyntax> methodDeclarations = (
|
||||
from syntaxNode in structDeclaration.DescendantNodes()
|
||||
where syntaxNode.IsKind(SyntaxKind.MethodDeclaration)
|
||||
select (MethodDeclarationSyntax)syntaxNode).ToImmutableArray();
|
||||
|
||||
foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations)
|
||||
// Helper that converts a sequence of string pairs into a nested array expression.
|
||||
// That is, this applies the following transformation:
|
||||
// - { ("K1", "V1"), ("K2", "V2") } => new object[] { new[] { "K1", "V1" }, new[] { "K2", "V2" } }
|
||||
static ArrayCreationExpressionSyntax NestedPairsArrayExpression(IEnumerable<(string Key, string Value)> pairs)
|
||||
{
|
||||
IMethodSymbol methodDeclarationSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration)!;
|
||||
|
||||
// Rewrite the method syntax tree
|
||||
var processedMethod = new ShaderSourceRewriter(semanticModel).Visit(methodDeclaration)!.WithoutTrivia();
|
||||
|
||||
// If the method is the shader entry point, do additional processing
|
||||
if (methodDeclarationSymbol.Name == nameof(IComputeShader.Execute) &&
|
||||
methodDeclarationSymbol.ReturnsVoid &&
|
||||
methodDeclarationSymbol.TypeParameters.Length == 0 &&
|
||||
methodDeclarationSymbol.Parameters.Length == 1 &&
|
||||
methodDeclarationSymbol.Parameters[0].Type.ToDisplayString() == typeof(ThreadIds).FullName)
|
||||
{
|
||||
var parameterName = methodDeclarationSymbol.Parameters[0].Name;
|
||||
|
||||
processedMethod = new ExecuteMethodRewriter(parameterName).Visit(processedMethod)!;
|
||||
}
|
||||
|
||||
// Produce the final method source
|
||||
var processedMethodSource = processedMethod.NormalizeWhitespace().ToFullString();
|
||||
|
||||
// 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(structFullName))),
|
||||
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(methodDeclarationSymbol.Name))),
|
||||
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(processedMethodSource))))))
|
||||
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
|
||||
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
|
||||
.NormalizeWhitespace()
|
||||
.ToFullString();
|
||||
|
||||
var generatedFileName = $"__ComputeSharp_{nameof(IComputeShaderSourceAttribute)}_{structDeclarationSymbol.Name}_{methodDeclarationSymbol.Name}";
|
||||
|
||||
// Add the method source attribute
|
||||
context.AddSource(generatedFileName, SourceText.From(source, Encoding.UTF8));
|
||||
return
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
|
||||
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
|
||||
.AddExpressions(pairs.Select(pair =>
|
||||
ImplicitArrayCreationExpression(InitializerExpression(SyntaxKind.ArrayInitializerExpression).AddExpressions(
|
||||
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(pair.Key)),
|
||||
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(pair.Value))))).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(NestedPairsArrayExpression(GetProcessedMembers(structDeclarationSymbol))),
|
||||
AttributeArgument(NestedPairsArrayExpression(GetProcessedMethods(structDeclaration, semanticModel))))))
|
||||
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
|
||||
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
|
||||
.NormalizeWhitespace()
|
||||
.ToFullString();
|
||||
|
||||
var generatedFileName = $"__ComputeSharp_{nameof(IComputeShaderSourceAttribute)}_{structDeclarationSymbol.Name}";
|
||||
|
||||
// Add the method source attribute
|
||||
context.AddSource(generatedFileName, SourceText.From(source, Encoding.UTF8));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a sequence of captured members and their mapped names.
|
||||
/// </summary>
|
||||
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
|
||||
/// <returns>A sequence of captured members in <paramref name="structDeclarationSymbol"/>.</returns>
|
||||
[Pure]
|
||||
private static IEnumerable<(string Key, string Value)> GetProcessedMembers(INamedTypeSymbol structDeclarationSymbol)
|
||||
{
|
||||
foreach (string memberName in (
|
||||
from member in structDeclarationSymbol.GetMembers()
|
||||
where member.Kind == SymbolKind.Field ||
|
||||
member.Kind == SymbolKind.Property
|
||||
select member.Name).Distinct())
|
||||
{
|
||||
_ = HlslKnownKeywords.TryGetMappedName(memberName, out string? mapping);
|
||||
|
||||
yield return (memberName, mapping ?? memberName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a sequence of processed methods declared within a given type.
|
||||
/// </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>
|
||||
/// <returns>A sequence of processed methods in <paramref name="structDeclaration"/>.</returns>
|
||||
[Pure]
|
||||
private static IEnumerable<(string Key, string Value)> GetProcessedMethods(
|
||||
StructDeclarationSyntax structDeclaration,
|
||||
SemanticModel semanticModel)
|
||||
{
|
||||
// Find all declared methods in the type
|
||||
ImmutableArray<MethodDeclarationSyntax> 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)!;
|
||||
|
||||
// Rewrite the method syntax tree
|
||||
var processedMethod = new ShaderSourceRewriter(semanticModel).Visit(methodDeclaration)!.WithoutTrivia();
|
||||
|
||||
// If the method is the shader entry point, do additional processing
|
||||
if (methodDeclarationSymbol.Name == nameof(IComputeShader.Execute) &&
|
||||
methodDeclarationSymbol.ReturnsVoid &&
|
||||
methodDeclarationSymbol.TypeParameters.Length == 0 &&
|
||||
methodDeclarationSymbol.Parameters.Length == 1 &&
|
||||
methodDeclarationSymbol.Parameters[0].Type.ToDisplayString() == typeof(ThreadIds).FullName)
|
||||
{
|
||||
var parameterName = methodDeclarationSymbol.Parameters[0].Name;
|
||||
|
||||
processedMethod = new ExecuteMethodRewriter(parameterName).Visit(processedMethod)!;
|
||||
}
|
||||
|
||||
// Produce the final method source
|
||||
var processedMethodSource = processedMethod.NormalizeWhitespace().ToFullString();
|
||||
|
||||
yield return (methodDeclarationSymbol.Name, processedMethodSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする