Added support for local methods to define shaders

このコミットが含まれているのは:
Sergio Pedri committed 2019-08-13 15:40:08 +02:00
1 親 348cadd622
コミット 8783035ad5
3ファイル変更
+27 -3

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

+11 -2
ファイルの表示
@@ -6,6 +6,7 @@ using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Text.RegularExpressions;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.Metadata;
@@ -97,6 +98,11 @@ namespace ComputeSharp.Shaders.Translation
}
}
/// <summary>
/// A <see cref="Regex"/> uused 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>
/// Decompiles a target method and returns its <see cref="SyntaxTree"/> and <see cref="SemanticModel"/> info
/// </summary>
@@ -117,10 +123,13 @@ namespace ComputeSharp.Shaders.Translation
Decompilers.Add(assemblyPath, decompiler);
}
string sourceCode = decompiler.DecompileAsString(methodHandle);
// Decompile the method source and fix the method declaration for local methods converted to lambdas
string
sourceCode = decompiler.DecompileAsString(methodHandle),
fixedCode = LambdaMethodDeclarationRegex.Replace(sourceCode, "internal void Main");
// Load the method syntax tree
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sourceCode);
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(fixedCode);
rootNode = syntaxTree.GetRoot();
// Update the incremental compilation and retrieve the syntax tree for the method
+1 -1
ファイルの表示
@@ -157,7 +157,7 @@ namespace ComputeSharp.Shaders.Translation
root = syntaxRewriter.Visit(root);
// Get the thread ids identifier name and shader method body
ConstructorDeclarationSyntax methodNode = root.ChildNodes().OfType<ConstructorDeclarationSyntax>().First();
MethodDeclarationSyntax methodNode = root.ChildNodes().OfType<MethodDeclarationSyntax>().First();
ThreadsIdsVariableName = methodNode.ParameterList.Parameters.First().Identifier.Text;
MethodBody = methodNode.Body.ToFullString();
+15
ファイルの表示
@@ -26,6 +26,21 @@ namespace ComputeSharp.NetCore.Tests
Assert.IsTrue((int)result[0] == (int)value);
}
[TestMethod]
public void LocalScalarAssignToBufferWithinLocalMethod()
{
float value = 10;
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
void Foo(ThreadIds id) => buffer[0] = value;
Gpu.Default.For(1, Foo);
float[] result = buffer.GetData();
Assert.IsTrue((int)result[0] == (int)value);
}
[TestMethod]
public void LocalKnownVectorAssignToBuffer()
{