diff --git a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs
index eab63a7d..b06a0b0f 100644
--- a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs
+++ b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs
@@ -12,6 +12,7 @@ using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.Metadata;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace ComputeSharp.Shaders.Translation
{
@@ -99,9 +100,14 @@ namespace ComputeSharp.Shaders.Translation
}
///
- /// A uused to preprocess the entry point declaration for both lambda expressions and local methods
+ /// A used to preprocess the closure type declarations for both lambda expressions and local methods
///
- private static readonly Regex LambdaMethodDeclarationRegex = new Regex(@"^(private|internal) void <\w+>[\w_|]+(?=\()", RegexOptions.Compiled);
+ private static readonly Regex ClosureTypeDeclarationRegex = new Regex(@"(?<=private sealed class )<\w*>[\w_]+", RegexOptions.Compiled);
+
+ ///
+ /// A used to preprocess the entry point declaration for both lambda expressions and local methods
+ ///
+ private static readonly Regex LambdaMethodDeclarationRegex = new Regex(@"(private|internal) void <\w+>[\w_|]+(?=\()", RegexOptions.Compiled);
///
/// Decompiles a target method and returns its and info
@@ -109,14 +115,15 @@ namespace ComputeSharp.Shaders.Translation
/// The input to inspect
/// The root node for the syntax tree of the input method
/// The semantic model for the input method
- public void GetSyntaxTree(MethodInfo methodInfo, out SyntaxNode rootNode, out SemanticModel semanticModel)
+ public void GetSyntaxTree(MethodInfo methodInfo, out MethodDeclarationSyntax rootNode, out SemanticModel semanticModel)
{
lock (Lock)
{
- // Get the handle of the input method
+ // Get the handle of the containing type method
string assemblyPath = methodInfo.DeclaringType?.Assembly.Location ?? throw new InvalidOperationException();
- EntityHandle methodHandle = MetadataTokenHelpers.TryAsEntityHandle(methodInfo.MetadataToken) ?? throw new InvalidOperationException();
- // Get or create a decompiler for the target assembly, and decompile the method
+ 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))
{
decompiler = CreateDecompiler(assemblyPath);
@@ -125,12 +132,15 @@ namespace ComputeSharp.Shaders.Translation
// 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");
+ sourceCode = decompiler.DecompileAsString(typeHandle),
+ typeFixedCode = ClosureTypeDeclarationRegex.Replace(sourceCode, "Shader"),
+ methodFixedCode = LambdaMethodDeclarationRegex.Replace(typeFixedCode, "internal void Main");
- // Load the method syntax tree
- SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(fixedCode);
- rootNode = syntaxTree.GetRoot();
+ // Load the type syntax tree
+ SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(methodFixedCode);
+
+ // Get the root node to return
+ rootNode = syntaxTree.GetRoot().DescendantNodes().OfType().First();
// Update the incremental compilation and retrieve the syntax tree for the method
_Compilation = _Compilation.AddSyntaxTrees(syntaxTree);
diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs
index 6d650909..feeba23f 100644
--- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs
+++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs
@@ -2,12 +2,10 @@
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
-using System.Reflection;
using System.Text.RegularExpressions;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Shaders.Mappings;
using ComputeSharp.Shaders.Renderer.Models.Fields;
-using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using SharpDX.Direct3D12;
@@ -161,7 +159,7 @@ namespace ComputeSharp.Shaders.Translation
string typeName = HlslKnownTypes.GetMappedName(fieldType);
processedFieldInfo = new ReadWriteBufferFieldInfo(typeName, fieldName, readWriteBuffersCount++);
}
- else if (HlslKnownTypes.IsKnownScalarType(fieldType))
+ else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType))
{
// Constant buffer
DescriptorRange range = new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, constantBuffersCount);
@@ -190,21 +188,20 @@ namespace ComputeSharp.Shaders.Translation
private void LoadMethodSource()
{
// Decompile the shader method
- MethodDecompiler.Instance.GetSyntaxTree(Action.Method, out SyntaxNode root, out SemanticModel semanticModel);
+ MethodDecompiler.Instance.GetSyntaxTree(Action.Method, out MethodDeclarationSyntax root, out SemanticModel semanticModel);
// Rewrite the shader method (eg. to fix the type declarations)
ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel);
- root = syntaxRewriter.Visit(root);
+ root = (MethodDeclarationSyntax)syntaxRewriter.Visit(root);
// Get the thread ids identifier name and shader method body
- MethodDeclarationSyntax methodNode = root.ChildNodes().OfType().First();
- ThreadsIdsVariableName = methodNode.ParameterList.Parameters.First().Identifier.Text;
- MethodBody = methodNode.Body.ToFullString();
+ ThreadsIdsVariableName = root.ParameterList.Parameters.First().Identifier.Text;
+ MethodBody = root.Body.ToFullString();
// Additional preprocessing
MethodBody = Regex.Replace(MethodBody, @"\d+[fFdD]", m => m.Value.Replace("f", ""));
MethodBody = MethodBody.TrimEnd('\n', '\r', ' ');
- MethodBody = $" {MethodBody.Replace(Environment.NewLine, $"{Environment.NewLine} ")}";
+ MethodBody = Regex.Replace(MethodBody, @"(?