Implemented SG-powered mapping of captured fields
このコミットが含まれているのは:
1 親
e711ef4c60
コミット
3d7c4c7cb6
3ファイル変更
+135
-67
検索条件に一致するファイルはありません
@@ -1,5 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
@@ -18,26 +22,40 @@ namespace ComputeSharp
|
||||
/// <param name="shaderTypeName">The fully qualified name of the shader type.</param>
|
||||
/// <param name="methodName">The name of the current method.</param>
|
||||
/// <param name="source">The source code of the target method.</param>
|
||||
public IComputeShaderSourceAttribute(string shaderTypeName, string methodName, string source)
|
||||
public IComputeShaderSourceAttribute(string shaderTypeName, object[] args, object[] methods)
|
||||
{
|
||||
ShaderTypeName = shaderTypeName;
|
||||
MethodName = methodName;
|
||||
Source = source;
|
||||
Fields = args.Cast<string[]>().ToDictionary(static arg => arg[0], static arg => arg[1]);
|
||||
Methods = methods.Cast<string[]>().ToDictionary(static method => method[0], static method => method[1]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the fully qualified name of the shader type.
|
||||
/// </summary>
|
||||
public string ShaderTypeName { get; }
|
||||
internal string ShaderTypeName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the current method.
|
||||
/// Gets the mapping of field names.
|
||||
/// </summary>
|
||||
public string MethodName { get; }
|
||||
internal IReadOnlyDictionary<string, string> Fields { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the source code of the target method.
|
||||
/// Gets the mapping of methods and their source code.
|
||||
/// </summary>
|
||||
public string Source { get; }
|
||||
internal IReadOnlyDictionary<string, string> Methods { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the associated <see cref="IComputeShaderSourceAttribute"/> instance for a specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The shader type to get the attribute for.</typeparam>
|
||||
/// <returns>The associated <see cref="IComputeShaderSourceAttribute"/> instance for type <typeparamref name="T"/>.</returns>
|
||||
[Pure]
|
||||
internal static IComputeShaderSourceAttribute GetForType<T>()
|
||||
{
|
||||
return (
|
||||
from attribute in typeof(T).Assembly.GetCustomAttributes<IComputeShaderSourceAttribute>()
|
||||
where attribute.ShaderTypeName == typeof(T).FullName
|
||||
select attribute).Single();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,11 @@ namespace ComputeSharp.Shaders.Translation
|
||||
internal sealed partial class ShaderLoader<T>
|
||||
where T : struct, IComputeShader
|
||||
{
|
||||
/// <summary>
|
||||
/// The associated <see cref="IComputeShaderSourceAttribute"/> instance for the current shader type.
|
||||
/// </summary>
|
||||
private static IComputeShaderSourceAttribute Attribute = IComputeShaderSourceAttribute.GetForType<T>();
|
||||
|
||||
/// <summary>
|
||||
/// The number of constant buffers to define in the shader.
|
||||
/// </summary>
|
||||
@@ -126,7 +131,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
object box = shader;
|
||||
|
||||
@this.LoadFieldsInfo(box);
|
||||
@this.LoadMethodSource(box);
|
||||
@this.LoadMethodSource();
|
||||
@this.BuildDispatchDataLoader();
|
||||
|
||||
return @this;
|
||||
@@ -166,11 +171,10 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// </summary>
|
||||
/// <param name="shader">The boxed <typeparamref name="T"/> instance to use to build the shader</param>
|
||||
/// <param name="memberInfo">The target <see cref="ReadableMember"/> to load</param>
|
||||
/// <param name="name">The optional explicit name to use for the field</param>
|
||||
private void LoadFieldInfo(object shader, ReadableMember memberInfo, string? name = null)
|
||||
private void LoadFieldInfo(object shader, ReadableMember memberInfo)
|
||||
{
|
||||
Type fieldType = memberInfo.MemberType;
|
||||
string fieldName = name; // TODO: implement this for SG
|
||||
string fieldName = Attribute.Fields[memberInfo.Name];
|
||||
|
||||
// Constant buffer
|
||||
if (HlslKnownTypes.IsConstantBufferType(fieldType))
|
||||
@@ -235,14 +239,9 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// <summary>
|
||||
/// Loads the entry method for the current shader being loaded
|
||||
/// </summary>
|
||||
/// <param name="shader">The boxed <typeparamref name="T"/> instance to use to build the shader</param>
|
||||
private void LoadMethodSource(object shader)
|
||||
private void LoadMethodSource()
|
||||
{
|
||||
EntryPoint = (
|
||||
from attribute in typeof(T).Assembly.GetCustomAttributes<IComputeShaderSourceAttribute>()
|
||||
where attribute.ShaderTypeName == typeof(T).FullName &&
|
||||
attribute.MethodName == nameof(IComputeShader.Execute)
|
||||
select attribute.Source).First();
|
||||
EntryPoint = Attribute.Methods[nameof(IComputeShader.Execute)];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする