Merge pull request #47 from Sergio0694/feature_static-methods
Added support for static methods
このコミットが含まれているのは:
11ファイル変更
+510
-80
検索条件に一致するファイルはありません
@@ -42,12 +42,14 @@ namespace ComputeSharp.Shaders.Extensions
|
||||
/// <param name="node">The input <see cref="MemberAccessExpressionSyntax"/> to check and modify if needed</param>
|
||||
/// <param name="semanticModel">The <see cref="SemanticModel"/> to use to load symbols for the input node</param>
|
||||
/// <param name="variable">The info on parsed static members, if any</param>
|
||||
/// <param name="method">The info on parsed static methods, if any</param>
|
||||
/// <returns>A <see cref="SyntaxNode"/> instance that is compatible with HLSL</returns>
|
||||
[Pure]
|
||||
public static SyntaxNode ReplaceMember(this MemberAccessExpressionSyntax node, SemanticModel semanticModel, out (string Name, ReadableMember MemberInfo)? variable)
|
||||
public static SyntaxNode ReplaceMember(this MemberAccessExpressionSyntax node, SemanticModel semanticModel, out (string Name, ReadableMember MemberInfo)? variable, out (string Name, MethodInfo MethodInfo)? method)
|
||||
{
|
||||
// Set the variable to null, replace it later on if needed
|
||||
// Set the out parameters to null, replace them later on if needed
|
||||
variable = null;
|
||||
method = null;
|
||||
|
||||
SymbolInfo containingMemberSymbolInfo;
|
||||
ISymbol? memberSymbol;
|
||||
@@ -115,37 +117,48 @@ namespace ComputeSharp.Shaders.Extensions
|
||||
return SyntaxFactory.IdentifierName(expression).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
|
||||
}
|
||||
|
||||
// Handle static fields as a special case
|
||||
// Handle static members as a special case
|
||||
if (memberSymbol.IsStatic && (
|
||||
memberSymbol.Kind == SymbolKind.Field ||
|
||||
memberSymbol.Kind == SymbolKind.Property))
|
||||
memberSymbol.Kind == SymbolKind.Property ||
|
||||
memberSymbol.Kind == SymbolKind.Method))
|
||||
{
|
||||
// Get the containing type
|
||||
string
|
||||
typeFullname = memberSymbol.ContainingType.ToString(),
|
||||
assemblyFullname = memberSymbol.ContainingAssembly.ToString();
|
||||
Type fieldDeclaringType = Type.GetType($"{typeFullname}, {assemblyFullname}");
|
||||
Type memberDeclaringType = Type.GetType($"{typeFullname}, {assemblyFullname}");
|
||||
|
||||
// Retrieve the field or property info
|
||||
bool isReadonly;
|
||||
ReadableMember memberInfo;
|
||||
switch (memberSymbol.Kind)
|
||||
// Static field or property
|
||||
if (memberSymbol.Kind == SymbolKind.Field || memberSymbol.Kind == SymbolKind.Property)
|
||||
{
|
||||
case SymbolKind.Field:
|
||||
FieldInfo fieldInfo = fieldDeclaringType.GetField(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
isReadonly = fieldInfo.IsInitOnly;
|
||||
memberInfo = fieldInfo;
|
||||
break;
|
||||
case SymbolKind.Property:
|
||||
PropertyInfo propertyInfo = fieldDeclaringType.GetProperty(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
isReadonly = !propertyInfo.CanWrite;
|
||||
memberInfo = propertyInfo;
|
||||
break;
|
||||
default: throw new InvalidOperationException($"Invalid symbol kind: {memberSymbol.Kind}");
|
||||
bool isReadonly;
|
||||
ReadableMember memberInfo;
|
||||
switch (memberSymbol.Kind)
|
||||
{
|
||||
case SymbolKind.Field:
|
||||
FieldInfo fieldInfo = memberDeclaringType.GetField(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
isReadonly = fieldInfo.IsInitOnly;
|
||||
memberInfo = fieldInfo;
|
||||
break;
|
||||
case SymbolKind.Property:
|
||||
PropertyInfo propertyInfo = memberDeclaringType.GetProperty(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
isReadonly = !propertyInfo.CanWrite;
|
||||
memberInfo = propertyInfo;
|
||||
break;
|
||||
default: throw new InvalidOperationException($"Invalid symbol kind: {memberSymbol.Kind}");
|
||||
}
|
||||
|
||||
// Handle the loaded info
|
||||
return ProcessStaticMember(node, memberInfo, isReadonly, ref variable);
|
||||
}
|
||||
|
||||
// Handle the loaded info
|
||||
return ProcessStaticMember(node, memberInfo, isReadonly, ref variable);
|
||||
// Static method
|
||||
MethodInfo methodInfo = memberDeclaringType.GetMethod(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
string name = $"{methodInfo.DeclaringType.Name}_{methodInfo.Name}";
|
||||
method = (name, methodInfo);
|
||||
|
||||
return SyntaxFactory.IdentifierName(name).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
|
||||
}
|
||||
|
||||
return node;
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ComputeSharp.Shaders.Extensions;
|
||||
|
||||
namespace ComputeSharp.Shaders.Renderer.Models.Functions
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains info on a shader function
|
||||
/// </summary>
|
||||
internal class FunctionInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the return type of the current function in the C# source
|
||||
/// </summary>
|
||||
public string FunctionCsharpReturnType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the fullname of the current function in the C# source
|
||||
/// </summary>
|
||||
public string FunctionFullName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameters of the function in the C# source
|
||||
/// </summary>
|
||||
public string FunctionCsharpParameters { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the return type of the current function
|
||||
/// </summary>
|
||||
public string ReturnType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the current function
|
||||
/// </summary>
|
||||
public string FunctionName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of parameters for the current function
|
||||
/// </summary>
|
||||
public IReadOnlyList<ParameterInfo> ParametersList { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the body of the current function
|
||||
/// </summary>
|
||||
public string FunctionBody { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="FunctionInfo"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="functionCsharpType">The concrete return type for the function</param>
|
||||
/// <param name="functionFullname">The fullname of the function in the C# source</param>
|
||||
/// <param name="functionCsharpParameters">The parameters of the function in the C# source</param>
|
||||
/// <param name="returnType">The return type of the current function</param>
|
||||
/// <param name="functionName">The name of the current function</param>
|
||||
/// <param name="parameters">The function parameters, if any</param>
|
||||
/// <param name="functionBody">The current function</param>
|
||||
public FunctionInfo(
|
||||
Type functionCsharpType,
|
||||
string functionFullname,
|
||||
string functionCsharpParameters,
|
||||
string returnType,
|
||||
string functionName,
|
||||
IReadOnlyList<ParameterInfo> parameters,
|
||||
string functionBody)
|
||||
{
|
||||
FunctionCsharpReturnType = functionCsharpType.ToFriendlyString();
|
||||
FunctionFullName = functionFullname;
|
||||
FunctionCsharpParameters = functionCsharpParameters;
|
||||
ReturnType = returnType;
|
||||
FunctionName = functionName;
|
||||
ParametersList = parameters;
|
||||
FunctionBody = functionBody;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
|
||||
namespace ComputeSharp.Shaders.Renderer.Models.Functions
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains info on a function parameter
|
||||
/// </summary>
|
||||
internal class ParameterInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the modifier to use for the current parameter
|
||||
/// </summary>
|
||||
public string ParameterModifier { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of the current parameter
|
||||
/// </summary>
|
||||
public string ParameterType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for the current parameter
|
||||
/// </summary>
|
||||
public string ParameterName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not the current parameter is the last one for the parent function
|
||||
/// </summary>
|
||||
public bool IsLastParameter { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ParameterInfo"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="parameterModifiers">The modifiers used in the current parameter</param>
|
||||
/// <param name="parameterType">The type of the current parameter</param>
|
||||
/// <param name="parameterName">The name of the current parameter</param>
|
||||
/// <param name="last">Indicates whether or not the current parameter is the last one</param>
|
||||
public ParameterInfo(IReadOnlyList<SyntaxToken> parameterModifiers, string parameterType, string parameterName, bool last)
|
||||
{
|
||||
if (parameterModifiers.Count == 0) ParameterModifier = "in";
|
||||
else if (parameterModifiers.First().IsKind(SyntaxKind.OutKeyword)) ParameterModifier = "out";
|
||||
else if (parameterModifiers.Any(m => m.IsKind(SyntaxKind.RefKeyword)) && !parameterModifiers.Any(m => m.IsKind(SyntaxKind.ReadOnlyKeyword))) ParameterModifier = "inout";
|
||||
else ParameterModifier = "in";
|
||||
|
||||
ParameterType = parameterType;
|
||||
ParameterName = parameterName;
|
||||
IsLastParameter = last;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Fields;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Functions;
|
||||
|
||||
#pragma warning disable CS8618 // Non-nullable field is uninitialized
|
||||
|
||||
@@ -12,12 +13,12 @@ namespace ComputeSharp.Shaders.Renderer.Models
|
||||
internal sealed class ShaderInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the list of captured buffers being present in the current shader
|
||||
/// Gets or sets the list of captured buffers being present in the current shader
|
||||
/// </summary>
|
||||
public IReadOnlyList<HlslBufferInfo> BuffersList { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of captured variables being present in the current shader
|
||||
/// Gets or sets the list of captured variables being present in the current shader
|
||||
/// </summary>
|
||||
public IReadOnlyList<CapturedFieldInfo> FieldsList { get; set; }
|
||||
|
||||
@@ -45,5 +46,10 @@ namespace ComputeSharp.Shaders.Renderer.Models
|
||||
/// Gets or sets the shader body to compile
|
||||
/// </summary>
|
||||
public string ShaderBody { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the list of static functions used by the shader
|
||||
/// </summary>
|
||||
public IReadOnlyList<FunctionInfo> FunctionsList { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,13 @@ cbuffer _{{FieldName}} : register(b{{BufferIndex}})
|
||||
{{/IsReadWriteBuffer}}
|
||||
{{/BuffersList}}
|
||||
|
||||
{{!Shader private functions}}
|
||||
{{#FunctionsList}}
|
||||
// {{FunctionCsharpReturnType}} {{FunctionFullName}}({{FunctionCsharpParameters}})
|
||||
{{ReturnType}} {{FunctionName}}({{#ParametersList}}{{ParameterModifier}} {{ParameterType}} {{ParameterName}}{{^IsLastParameter}}, {{/IsLastParameter}}{{/ParametersList}})
|
||||
{{FunctionBody}}
|
||||
{{/FunctionsList}}
|
||||
|
||||
{{!Shader entry point}}
|
||||
// Shader body
|
||||
[Shader("compute")]
|
||||
|
||||
@@ -76,7 +76,8 @@ namespace ComputeSharp.Shaders
|
||||
NumThreadsY = threadsY,
|
||||
NumThreadsZ = threadsZ,
|
||||
ThreadsIdsVariableName = shaderLoader.ThreadsIdsVariableName,
|
||||
ShaderBody = shaderLoader.MethodBody
|
||||
ShaderBody = shaderLoader.MethodBody,
|
||||
FunctionsList = shaderLoader.FunctionsList
|
||||
};
|
||||
string shaderSource = ShaderRenderer.Instance.Render(shaderInfo);
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace ComputeSharp.Shaders.Translation.Enums
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see langword="enum"/> that indicates a type of method to decompile
|
||||
/// </summary>
|
||||
internal enum MethodType
|
||||
{
|
||||
/// <summary>
|
||||
/// An instance method that belongs to a closure class
|
||||
/// </summary>
|
||||
Closure,
|
||||
|
||||
/// <summary>
|
||||
/// A standalone, static method
|
||||
/// </summary>
|
||||
Static
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ using System.Reflection.Metadata;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using ComputeSharp.Shaders.Mappings;
|
||||
using ComputeSharp.Shaders.Translation.Enums;
|
||||
using ICSharpCode.Decompiler;
|
||||
using ICSharpCode.Decompiler.CSharp;
|
||||
using ICSharpCode.Decompiler.Metadata;
|
||||
@@ -108,66 +109,49 @@ namespace ComputeSharp.Shaders.Translation
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A <see cref="Regex"/> used to preprocess the closure type declarations for both lambda expressions and local methods
|
||||
/// Decompiles a method or the whole declaring type
|
||||
/// </summary>
|
||||
private static readonly Regex ClosureTypeDeclarationRegex = new Regex(@"(?<=private sealed class )<\w*>[\w_]+", RegexOptions.Compiled);
|
||||
/// <param name="methodInfo">The target <see cref="MethodInfo"/> to decompile</param>
|
||||
/// <param name="methodOnly">Specifies whether or not to force the decompilation of just the given method, even if not static</param>
|
||||
/// <returns>The decompiled source code</returns>
|
||||
[Pure]
|
||||
private string DecompileMethodOrDeclaringType(MethodInfo methodInfo, bool methodOnly = false)
|
||||
{
|
||||
// Get the handle of the containing type method
|
||||
string assemblyPath = methodInfo.DeclaringType?.Assembly.Location ?? throw new InvalidOperationException();
|
||||
int metadataToken = methodInfo.IsStatic || methodOnly ? methodInfo.MetadataToken : methodInfo.DeclaringType.MetadataToken;
|
||||
EntityHandle typeHandle = MetadataTokenHelpers.TryAsEntityHandle(metadataToken) ?? throw new InvalidOperationException();
|
||||
|
||||
/// <summary>
|
||||
/// A <see cref="Regex"/> used to preprocess the entry point declaration for both lambda expressions and local methods
|
||||
/// </summary>
|
||||
private static readonly Regex LambdaMethodDeclarationRegex = new Regex(@"(?:private|internal) void <\w+>[\w_|]+(?=\()", RegexOptions.Compiled);
|
||||
// Get or create a decompiler for the target assembly, and decompile the type
|
||||
if (!Decompilers.TryGetValue(assemblyPath, out CSharpDecompiler decompiler))
|
||||
{
|
||||
decompiler = CreateDecompiler(assemblyPath);
|
||||
Decompilers.Add(assemblyPath, decompiler);
|
||||
}
|
||||
|
||||
return decompiler.DecompileAsString(typeHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decompiles a target method and returns its <see cref="SyntaxTree"/> and <see cref="SemanticModel"/> info
|
||||
/// </summary>
|
||||
/// <param name="methodInfo">The input <see cref="MethodInfo"/> to inspect</param>
|
||||
/// <param name="methodType">The type of method to decompile</param>
|
||||
/// <param name="rootNode">The root node for the syntax tree of the input method</param>
|
||||
/// <param name="semanticModel">The semantic model for the input method</param>
|
||||
public void GetSyntaxTree(MethodInfo methodInfo, out MethodDeclarationSyntax rootNode, out SemanticModel semanticModel)
|
||||
public void GetSyntaxTree(MethodInfo methodInfo, MethodType methodType, out MethodDeclarationSyntax rootNode, out SemanticModel semanticModel)
|
||||
{
|
||||
lock (Lock)
|
||||
{
|
||||
// Get the handle of the containing type method
|
||||
string assemblyPath = methodInfo.DeclaringType?.Assembly.Location ?? throw new InvalidOperationException();
|
||||
EntityHandle typeHandle = MetadataTokenHelpers.TryAsEntityHandle(methodInfo.DeclaringType.MetadataToken) ?? throw new InvalidOperationException();
|
||||
|
||||
// Get or create a decompiler for the target assembly, and decompile the type
|
||||
if (!Decompilers.TryGetValue(assemblyPath, out CSharpDecompiler decompiler))
|
||||
string sourceCode = methodType switch
|
||||
{
|
||||
decompiler = CreateDecompiler(assemblyPath);
|
||||
Decompilers.Add(assemblyPath, decompiler);
|
||||
}
|
||||
|
||||
// Decompile the method source and fix the method declaration for local methods converted to lambdas
|
||||
string
|
||||
sourceCode = decompiler.DecompileAsString(typeHandle),
|
||||
typeFixedCode = ClosureTypeDeclarationRegex.Replace(sourceCode, "Shader"),
|
||||
methodFixedCode = LambdaMethodDeclarationRegex.Replace(typeFixedCode, m => $"// {m.Value}{Environment.NewLine} internal void Main");
|
||||
|
||||
// Workaround for some local methods not being decompiled correctly
|
||||
if (!methodFixedCode.Contains("internal void Main"))
|
||||
{
|
||||
EntityHandle methodHandle = MetadataTokenHelpers.TryAsEntityHandle(methodInfo.MetadataToken) ?? throw new InvalidOperationException();
|
||||
string
|
||||
methodOnlySourceCode = decompiler.DecompileAsString(methodHandle),
|
||||
methodOnlyFixedSourceCode = LambdaMethodDeclarationRegex.Replace(methodOnlySourceCode, m => $"// {m.Value}{Environment.NewLine} internal void Main"),
|
||||
methodOnlyIndentedSourceCode = $" {methodOnlyFixedSourceCode.Replace(Environment.NewLine, $"{Environment.NewLine} ")}";
|
||||
|
||||
int lastClosedBracketsIndex = methodFixedCode.LastIndexOf('}');
|
||||
methodFixedCode = methodFixedCode.Insert(lastClosedBracketsIndex, methodOnlyIndentedSourceCode);
|
||||
}
|
||||
|
||||
// Unwrap the nested fields
|
||||
string unwrappedSourceCode = UnwrapSyntaxTree(methodFixedCode);
|
||||
|
||||
// Remove the in keyword from the source
|
||||
string inFixedSourceCode = Regex.Replace(unwrappedSourceCode, @"(?<!\w)in ", string.Empty);
|
||||
|
||||
// Tweak the out declarations
|
||||
string outFixedSourceCode = RefactorInlineOutDeclarations(inFixedSourceCode, methodInfo.Name);
|
||||
MethodType.Closure => GetSyntaxTreeForClosureMethod(methodInfo),
|
||||
MethodType.Static => GetSyntaxTreeForStaticMethod(methodInfo),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(methodType), $"Invalid method type: {methodType}")
|
||||
};
|
||||
|
||||
// Load the type syntax tree
|
||||
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(outFixedSourceCode);
|
||||
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sourceCode);
|
||||
|
||||
// Get the root node to return
|
||||
rootNode = syntaxTree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().First(node => node.GetLeadingTrivia().ToFullString().Contains(methodInfo.Name));
|
||||
@@ -178,6 +162,74 @@ namespace ComputeSharp.Shaders.Translation
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decompiles a target closure method
|
||||
/// </summary>
|
||||
/// <param name="methodInfo">The input <see cref="MethodInfo"/> to inspect</param>
|
||||
[Pure]
|
||||
private string GetSyntaxTreeForClosureMethod(MethodInfo methodInfo)
|
||||
{
|
||||
// Decompile the method source and fix the method declaration for local methods converted to lambdas
|
||||
string
|
||||
sourceCode = DecompileMethodOrDeclaringType(methodInfo),
|
||||
typeFixedCode = ClosureTypeDeclarationRegex.Replace(sourceCode, "Shader"),
|
||||
methodFixedCode = LambdaMethodDeclarationRegex.Replace(typeFixedCode, m => $"// {m.Value}{Environment.NewLine} internal void Main");
|
||||
|
||||
// Workaround for some local methods not being decompiled correctly
|
||||
if (!methodFixedCode.Contains("internal void Main"))
|
||||
{
|
||||
string
|
||||
methodOnlySourceCode = DecompileMethodOrDeclaringType(methodInfo, true),
|
||||
methodOnlyFixedSourceCode = LambdaMethodDeclarationRegex.Replace(methodOnlySourceCode, m => $"// {m.Value}{Environment.NewLine} internal void Main"),
|
||||
methodOnlyIndentedSourceCode = $" {methodOnlyFixedSourceCode.Replace(Environment.NewLine, $"{Environment.NewLine} ")}";
|
||||
|
||||
int lastClosedBracketsIndex = methodFixedCode.LastIndexOf('}');
|
||||
methodFixedCode = methodFixedCode.Insert(lastClosedBracketsIndex, methodOnlyIndentedSourceCode);
|
||||
}
|
||||
|
||||
// Unwrap the nested fields
|
||||
string unwrappedSourceCode = UnwrapSyntaxTree(methodFixedCode);
|
||||
|
||||
// Remove the in keyword from the source
|
||||
string inFixedSourceCode = Regex.Replace(unwrappedSourceCode, @"(?<!\w)in ", string.Empty);
|
||||
|
||||
// Tweak the out declarations
|
||||
string outFixedSourceCode = RefactorInlineOutDeclarations(inFixedSourceCode, methodInfo.Name);
|
||||
|
||||
return outFixedSourceCode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decompiles a target method and returns its <see cref="SyntaxTree"/> and <see cref="SemanticModel"/> info
|
||||
/// </summary>
|
||||
/// <param name="methodInfo">The input <see cref="MethodInfo"/> to inspect</param>
|
||||
[Pure]
|
||||
private string GetSyntaxTreeForStaticMethod(MethodInfo methodInfo)
|
||||
{
|
||||
string
|
||||
sourceCode = DecompileMethodOrDeclaringType(methodInfo),
|
||||
prototype = sourceCode.Split(Environment.NewLine)[0],
|
||||
commentedSourceCode = $"// {methodInfo.Name}{Environment.NewLine}{sourceCode}",
|
||||
inFixedSourceCode = Regex.Replace(commentedSourceCode, @"(?<!\w)in ", string.Empty),
|
||||
outFixedSourceCode = RefactorInlineOutDeclarations(inFixedSourceCode, methodInfo.Name);
|
||||
|
||||
// Restore the original prototype, as some out parameters might have been removed
|
||||
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(outFixedSourceCode);
|
||||
BlockSyntax block = syntaxTree.GetRoot().DescendantNodes().OfType<BlockSyntax>().First();
|
||||
|
||||
return $"// {methodInfo.Name}{Environment.NewLine}{prototype}{Environment.NewLine}{block.ToFullString()}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A <see cref="Regex"/> used to preprocess the closure type declarations for both lambda expressions and local methods
|
||||
/// </summary>
|
||||
private static readonly Regex ClosureTypeDeclarationRegex = new Regex(@"(?<=private sealed class )<\w*>[\w_]+", RegexOptions.Compiled);
|
||||
|
||||
/// <summary>
|
||||
/// A <see cref="Regex"/> used to preprocess the entry point declaration for both lambda expressions and local methods
|
||||
/// </summary>
|
||||
private static readonly Regex LambdaMethodDeclarationRegex = new Regex(@"(?:private|internal) void <\w+>[\w_|]+(?=\()", RegexOptions.Compiled);
|
||||
|
||||
/// <summary>
|
||||
/// A <see cref="Regex"/> used to find fields that represent nested closure types
|
||||
/// </summary>
|
||||
@@ -272,14 +324,14 @@ namespace ComputeSharp.Shaders.Translation
|
||||
argument.Expression.IsKind(SyntaxKind.DeclarationExpression)
|
||||
let match = Regex.Match(argument.Expression.ToFullString(), @"([\w.]+) ([\w_]+)")
|
||||
let mappedType = HlslKnownTypes.GetMappedName(match.Groups[1].Value)
|
||||
let declatation = $"{mappedType} {match.Groups[2].Value} = ({mappedType})0;"
|
||||
select (match.Groups[1].Value, match.Groups[2].Value, declatation)).ToArray();
|
||||
let declaration = $"{mappedType} {match.Groups[2].Value} = ({mappedType})0;"
|
||||
select declaration).ToArray();
|
||||
|
||||
// Insert the explicit declarations at the start of the method
|
||||
int start = rootNode.Body.ChildNodes().First().SpanStart;
|
||||
foreach (var item in outs.Reverse())
|
||||
foreach (var declaration in outs.Reverse())
|
||||
{
|
||||
source = source.Insert(start, $"{item.declatation}{Environment.NewLine} ");
|
||||
source = source.Insert(start, $"{declaration}{Environment.NewLine} ");
|
||||
}
|
||||
|
||||
// Remove the out keyword from the source
|
||||
|
||||
@@ -5,13 +5,18 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Shaders.Extensions;
|
||||
using ComputeSharp.Shaders.Mappings;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Fields;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Functions;
|
||||
using ComputeSharp.Shaders.Translation.Enums;
|
||||
using ComputeSharp.Shaders.Translation.Models;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Vortice.Direct3D12;
|
||||
using ParameterInfo = ComputeSharp.Shaders.Renderer.Models.Functions.ParameterInfo;
|
||||
|
||||
#pragma warning disable CS8618 // Non-nullable field is uninitialized
|
||||
|
||||
@@ -137,6 +142,13 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// </summary>
|
||||
public string MethodBody { get; private set; }
|
||||
|
||||
private readonly List<FunctionInfo> _FunctionsList = new List<FunctionInfo>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of <see cref="FunctionInfo"/> items for the shader
|
||||
/// </summary>
|
||||
public IReadOnlyList<FunctionInfo> FunctionsList => _FunctionsList;
|
||||
|
||||
/// <summary>
|
||||
/// Loads and processes an input <see cref="Action{T}"/>
|
||||
/// </summary>
|
||||
@@ -243,16 +255,22 @@ namespace ComputeSharp.Shaders.Translation
|
||||
private void LoadMethodSource()
|
||||
{
|
||||
// Decompile the shader method
|
||||
MethodDecompiler.Instance.GetSyntaxTree(Action.Method, out MethodDeclarationSyntax root, out SemanticModel semanticModel);
|
||||
MethodDecompiler.Instance.GetSyntaxTree(Action.Method, MethodType.Closure, out MethodDeclarationSyntax root, out SemanticModel semanticModel);
|
||||
|
||||
// Rewrite the shader method (eg. to fix the type declarations)
|
||||
ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel);
|
||||
root = (MethodDeclarationSyntax)syntaxRewriter.Visit(root);
|
||||
|
||||
// Register the captured static fields
|
||||
foreach (var item in syntaxRewriter.StaticMembers)
|
||||
// Register the captured static members
|
||||
foreach (var member in syntaxRewriter.StaticMembers)
|
||||
{
|
||||
LoadFieldInfo(item.Value, item.Key);
|
||||
LoadFieldInfo(member.Value, member.Key);
|
||||
}
|
||||
|
||||
// Register the captured static methods
|
||||
foreach (var method in syntaxRewriter.StaticMethods)
|
||||
{
|
||||
LoadStaticMethodSource(method.Key, method.Value);
|
||||
}
|
||||
|
||||
// Get the thread ids identifier name and shader method body
|
||||
@@ -264,5 +282,59 @@ namespace ComputeSharp.Shaders.Translation
|
||||
MethodBody = MethodBody.TrimEnd('\n', '\r', ' ');
|
||||
MethodBody = HlslKnownKeywords.GetMappedText(MethodBody);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads additional static methods used by the shader
|
||||
/// </summary>
|
||||
/// <param name="name">The HLSL name of the new method to load</param>
|
||||
/// <param name="methodInfo">The <see cref="MethodInfo"/> instance for the method to load</param>
|
||||
private void LoadStaticMethodSource(string name, MethodInfo methodInfo)
|
||||
{
|
||||
// Decompile the target method
|
||||
MethodDecompiler.Instance.GetSyntaxTree(methodInfo, MethodType.Static, out MethodDeclarationSyntax root, out SemanticModel semanticModel);
|
||||
|
||||
// Rewrite the method
|
||||
ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel);
|
||||
root = (MethodDeclarationSyntax)syntaxRewriter.Visit(root);
|
||||
|
||||
// Register the captured static members
|
||||
foreach (var member in syntaxRewriter.StaticMembers)
|
||||
{
|
||||
LoadFieldInfo(member.Value, member.Key);
|
||||
}
|
||||
|
||||
// Register the captured static methods
|
||||
foreach (var method in syntaxRewriter.StaticMethods)
|
||||
{
|
||||
LoadStaticMethodSource(method.Key, method.Value);
|
||||
}
|
||||
|
||||
// Get the function parameters
|
||||
IReadOnlyList<ParameterInfo> parameters = (
|
||||
from parameter in root.ParameterList.Parameters.Select((p, i) => (Node: p, Index: i))
|
||||
let modifiers = parameter.Node.Modifiers
|
||||
let type = parameter.Node.Type.ToFullString()
|
||||
let parameterName = parameter.Node.Identifier.ToFullString()
|
||||
let last = parameter.Index == root.ParameterList.Parameters.Count - 1
|
||||
select new ParameterInfo(modifiers, type, parameterName, last)).ToArray();
|
||||
|
||||
// Get the function body
|
||||
string body = root.Body.ToFullString();
|
||||
body = Regex.Replace(body, @"(?<=\W)(\d+)[fFdD]", m => m.Groups[1].Value);
|
||||
body = body.TrimEnd('\n', '\r', ' ');
|
||||
body = HlslKnownKeywords.GetMappedText(body);
|
||||
|
||||
// Get the final function info instance
|
||||
FunctionInfo functionInfo = new FunctionInfo(
|
||||
methodInfo.ReturnType,
|
||||
$"{methodInfo.DeclaringType.FullName}{Type.Delimiter}{methodInfo.Name}",
|
||||
string.Join(", ", methodInfo.GetParameters().Select(p => $"{p.ParameterType.ToFriendlyString()} {p.Name}")),
|
||||
root.ReturnType.ToFullString(),
|
||||
name,
|
||||
parameters,
|
||||
body);
|
||||
|
||||
_FunctionsList.Add(functionInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using ComputeSharp.Shaders.Extensions;
|
||||
using ComputeSharp.Shaders.Translation.Models;
|
||||
using Microsoft.CodeAnalysis;
|
||||
@@ -30,6 +31,13 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<string, ReadableMember> StaticMembers => _StaticMembers;
|
||||
|
||||
private readonly Dictionary<string, MethodInfo> _StaticMethods = new Dictionary<string, MethodInfo>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapping of captured static methods used by the target code
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<string, MethodInfo> StaticMethods => _StaticMethods;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitParameter(ParameterSyntax node)
|
||||
{
|
||||
@@ -40,6 +48,19 @@ namespace ComputeSharp.Shaders.Translation
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitArgument(ArgumentSyntax node)
|
||||
{
|
||||
node = (ArgumentSyntax)base.VisitArgument(node);
|
||||
|
||||
if (node.RefKindKeyword.IsKind(SyntaxKind.RefKeyword))
|
||||
{
|
||||
node = node.WithRefKindKeyword(SyntaxFactory.Token(SyntaxKind.None));
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitCastExpression(CastExpressionSyntax node)
|
||||
{
|
||||
@@ -83,15 +104,33 @@ namespace ComputeSharp.Shaders.Translation
|
||||
public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
|
||||
{
|
||||
node = (MemberAccessExpressionSyntax)base.VisitMemberAccessExpression(node);
|
||||
SyntaxNode syntaxNode = node.ReplaceMember(SemanticModel, out var variable);
|
||||
SyntaxNode syntaxNode = node.ReplaceMember(SemanticModel, out var variable, out var method);
|
||||
|
||||
// Register the captured member, if any
|
||||
// Register the captured members, if any
|
||||
if (variable.HasValue && !_StaticMembers.ContainsKey(variable.Value.Name))
|
||||
{
|
||||
_StaticMembers.Add(variable.Value.Name, variable.Value.MemberInfo);
|
||||
}
|
||||
if (method.HasValue && !_StaticMethods.ContainsKey(method.Value.Name))
|
||||
{
|
||||
_StaticMethods.Add(method.Value.Name, method.Value.MethodInfo);
|
||||
}
|
||||
|
||||
return syntaxNode;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
|
||||
{
|
||||
node = (MethodDeclarationSyntax)base.VisitMethodDeclaration(node);
|
||||
|
||||
// Replace the return type node, if needed
|
||||
if (!node.ReturnType.ToString().Equals("void"))
|
||||
{
|
||||
return node.ReplaceType(node.ReturnType);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace ComputeSharp.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// A container <see langword="class"/> for static methods to test
|
||||
/// </summary>
|
||||
public static class StaticMethodsContainer
|
||||
{
|
||||
[Pure]
|
||||
public static float Square(float x) => x * x;
|
||||
|
||||
[Pure]
|
||||
public static Float4 Range(float x) => new Float4(x, x + 1, x + 2, x + 3);
|
||||
|
||||
[Pure]
|
||||
public static int Sum(Float4 x) => (int)(x.X + x.Y + x.Z + x.W);
|
||||
|
||||
public static void Assign(int x, out int y) => y = x;
|
||||
|
||||
public static void ReadAndSquare(ref int x) => x *= x;
|
||||
}
|
||||
|
||||
[TestClass]
|
||||
[TestCategory("StaticMethodsTests")]
|
||||
public class StaticMethodsTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void FloatToFloatFunc()
|
||||
{
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
|
||||
|
||||
Gpu.Default.For(1, id => buffer[0] = StaticMethodsContainer.Square(3));
|
||||
|
||||
float[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FloatToFloat4Func()
|
||||
{
|
||||
using ReadWriteBuffer<Float4> buffer = Gpu.Default.AllocateReadWriteBuffer<Float4>(1);
|
||||
|
||||
Gpu.Default.For(1, id => buffer[0] = StaticMethodsContainer.Range(3));
|
||||
|
||||
Float4[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(MathF.Abs(result[0].X - 3) < 0.0001f);
|
||||
Assert.IsTrue(MathF.Abs(result[0].Y - 4) < 0.0001f);
|
||||
Assert.IsTrue(MathF.Abs(result[0].Z - 5) < 0.0001f);
|
||||
Assert.IsTrue(MathF.Abs(result[0].W - 6) < 0.0001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Float4ToIntFunc()
|
||||
{
|
||||
using ReadWriteBuffer<int> buffer = Gpu.Default.AllocateReadWriteBuffer<int>(1);
|
||||
|
||||
Gpu.Default.For(1, id => buffer[0] = StaticMethodsContainer.Sum(new Float4(1, 2, 3, 14)));
|
||||
|
||||
int[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(result[0] == 20);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IntToOutIntFunc()
|
||||
{
|
||||
using ReadWriteBuffer<int> buffer = Gpu.Default.AllocateReadWriteBuffer<int>(1);
|
||||
|
||||
Gpu.Default.For(1, id => StaticMethodsContainer.Assign(7, out buffer[0]));
|
||||
|
||||
int[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(result[0] == 7);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IntToRefIntFunc()
|
||||
{
|
||||
int[] data = { 3 };
|
||||
using ReadWriteBuffer<int> buffer = Gpu.Default.AllocateReadWriteBuffer(data);
|
||||
|
||||
Gpu.Default.For(1, id => StaticMethodsContainer.ReadAndSquare(ref buffer[0]));
|
||||
|
||||
buffer.GetData(data);
|
||||
|
||||
Assert.IsTrue(data[0] == 9);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする