Implemented working version of metaprogramming

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-01 14:07:31 +01:00
1 親 df02a534ea
コミット c231e8506a
7ファイル変更
+74 -17

検索条件に一致するファイルはありません

+10 -3
ファイルの表示
@@ -4,6 +4,7 @@ using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
using ComputeSharp.Core.Extensions;
using Microsoft.Toolkit.Diagnostics;
namespace ComputeSharp
@@ -23,11 +24,13 @@ namespace ComputeSharp
/// </summary>
/// <param name="methodName">The fully qualified name of the current method.</param>
/// <param name="types">The collection of custom types.</param>
/// <param name="invokeMethod">The source code for the target entry point method.</param>
/// <param name="methods">The collection of processed methods.</param>
public ShaderMethodSourceAttribute(string methodName, string[] types, string[] methods)
public ShaderMethodSourceAttribute(string methodName, string[] types, string invokeMethod, string[] methods)
{
MethodName = methodName;
Types = types;
InvokeMethod = invokeMethod;
Methods = methods;
}
@@ -41,6 +44,11 @@ namespace ComputeSharp
/// </summary>
internal IReadOnlyCollection<string> Types { get; }
/// <summary>
/// Gets the source code for the target entry point method.
/// </summary>
internal string InvokeMethod { get; }
/// <summary>
/// Gets the collection of processed methods.
/// </summary>
@@ -56,8 +64,7 @@ namespace ComputeSharp
{
Guard.IsTrue(function.Method.IsStatic, "Captured delegates need to wrap static methods");
Type declaringType = function.Method.DeclaringType;
string methodName = $"{declaringType.FullName}.{function.Method.Name}";
string methodName = function.Method.GetFullName();
return (
from attribute in function.Method.DeclaringType.Assembly.GetCustomAttributes<ShaderMethodSourceAttribute>()
+25
ファイルの表示
@@ -0,0 +1,25 @@
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
namespace ComputeSharp.Core.Extensions
{
/// <summary>
/// A <see langword="class"/> that provides extension methods for the <see cref="MethodInfo"/> type.
/// </summary>
internal static class MethodInfoExtensions
{
/// <summary>
/// Gets a fully qualified name for the input method, including parameters.
/// </summary>
/// <param name="method">The input <see cref="MethodInfo"/> instance.</param>
/// <returns>A fully qualified name for <paramref name="method"/>, including parameters.</returns>
[Pure]
public static string GetFullName(this MethodInfo method)
{
var parameters = string.Join(", ", method.GetParameters().Select(static p => p.ParameterType.FullName));
return $"{method.DeclaringType!.FullName}.{method.Name}({parameters})";
}
}
}
+20 -5
ファイルの表示
@@ -56,6 +56,16 @@ namespace ComputeSharp.Shaders.Translation
/// </summary>
private readonly List<CapturedFieldInfo> fieldsInfo = new();
/// <summary>
/// The <see cref="List{T}"/> with the collected methods for the shader.
/// </summary>
private readonly List<string> methodsInfo = new();
/// <summary>
/// The <see cref="List{T}"/> with the collected declared types for the shader.
/// </summary>
private readonly List<string> declaredTypes = new();
/// <summary>
/// Creates a new <see cref="ShaderLoader{T}"/> instance.
/// </summary>
@@ -81,10 +91,10 @@ namespace ComputeSharp.Shaders.Translation
public IReadOnlyList<CapturedFieldInfo> FieldsInfo => this.fieldsInfo;
/// <inheritdoc/>
public IReadOnlyCollection<string> MethodsInfo { get; private set; }
public IReadOnlyCollection<string> MethodsInfo => this.methodsInfo;
/// <inheritdoc/>
public IReadOnlyCollection<string> DeclaredTypes { get; private set; }
public IReadOnlyCollection<string> DeclaredTypes => this.declaredTypes;
/// <summary>
/// Loads and processes an input<typeparamref name="T"/> shadeer
@@ -181,7 +191,11 @@ namespace ComputeSharp.Shaders.Translation
else if (fieldInfo.GetValue(shader) is Delegate { Method: { IsStatic: true } } func)
{
// Captured static delegates with a return type
throw new NotImplementedException();
var methodSource = ShaderMethodSourceAttribute.GetForDelegate(func);
this.declaredTypes.AddRange(methodSource.Types);
this.methodsInfo.Add(methodSource.InvokeMethod.Replace("__<NAME>__", hlslName));
this.methodsInfo.AddRange(methodSource.Methods);
}
else ThrowHelper.ThrowArgumentException("Invalid captured variable");
}
@@ -191,9 +205,10 @@ namespace ComputeSharp.Shaders.Translation
/// </summary>
private void LoadMethodMetadata()
{
DeclaredTypes = Attribute.Types;
EntryPoint = Attribute.ExecuteMethod;
MethodsInfo = Attribute.Methods;
this.methodsInfo.AddRange(Attribute.Methods);
this.declaredTypes.AddRange(Attribute.Types);
}
}
}
+1 -1
ファイルの表示
@@ -56,7 +56,7 @@ namespace ComputeSharp.SourceGenerators.Extensions
{
if (includeParameters)
{
var parameters = $"{string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName()))}";
var parameters = string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName()));
return $"{symbol.ToDisplayString(FullyQualifiedWithoutGlobalAndParametersFormat)}({parameters})";
}
-3
ファイルの表示
@@ -112,9 +112,6 @@ namespace ComputeSharp.SourceGenerators
{
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type;
// Delegate types are not included and are processed separately
if (typeSymbol.TypeKind == TypeKind.Delegate) continue;
string typeName = HlslKnownTypes.GetMappedName(typeSymbol);
_ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping);
+5
ファイルの表示
@@ -73,8 +73,13 @@ namespace ComputeSharp.SourceGenerators.Mappings
/// </summary>
/// <param name="typeSymbol">The input type to map.</param>
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader.</returns>
[Pure]
public static string GetMappedName(INamedTypeSymbol typeSymbol)
{
// Delegate types just return an empty string, as they're not actually
// used in the generated shaders, but just mapped to a function at runtime.
if (typeSymbol.TypeKind == TypeKind.Delegate) return "";
string typeName = typeSymbol.GetFullMetadataName();
// Special case for the structured buffer types
+13 -5
ファイルの表示
@@ -45,7 +45,7 @@ namespace ComputeSharp.SourceGenerators
Dictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods = new(SymbolEqualityComparer.Default);
// Explore the syntax tree and extract the processed info
var processedMethods = GetProcessedMethods(methodDeclaration, semanticModel, discoveredTypes, staticMethods).ToArray();
var (invokeMethod, processedMethods) = GetProcessedMethods(methodDeclaration, semanticModel, discoveredTypes, staticMethods);
var processedTypes = IComputeShaderSourceGenerator.GetProcessedTypes(discoveredTypes).ToArray();
// Helper that converts a sequence of strings into an array expression.
@@ -68,6 +68,7 @@ namespace ComputeSharp.SourceGenerators
Attribute(IdentifierName(typeof(ShaderMethodSourceAttribute).FullName)).AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(methodDeclarationSymbol.GetFullMetadataName(true)))),
AttributeArgument(ArrayExpression(processedTypes)),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(invokeMethod))),
AttributeArgument(ArrayExpression(processedMethods)))))
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
@@ -88,7 +89,7 @@ namespace ComputeSharp.SourceGenerators
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
/// <returns>A sequence of processed methods in <paramref name="methodDeclaration"/>.</returns>
[Pure]
private static IEnumerable<string> GetProcessedMethods(
private static (string InvokeMethod, IEnumerable<string> Methods) GetProcessedMethods(
MethodDeclarationSyntax methodDeclaration,
SemanticModel semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
@@ -97,15 +98,22 @@ namespace ComputeSharp.SourceGenerators
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods);
// Rewrite the method syntax tree
var processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia();
var processedMethod = shaderSourceRewriter
.Visit(methodDeclaration)!
.WithIdentifier(Identifier("__<NAME>__"))
.WithoutTrivia()
.NormalizeWhitespace()
.ToFullString();
yield return processedMethod.NormalizeWhitespace().ToFullString();
List<string> methods = new(shaderSourceRewriter.LocalFunctions.Count);
// Emit the extracted local functions
foreach (var localFunction in shaderSourceRewriter.LocalFunctions)
{
yield return localFunction.Value.NormalizeWhitespace().ToFullString();
methods.Add(localFunction.Value.NormalizeWhitespace().ToFullString());
}
return (processedMethod, methods);
}
}
}