From ae5386cce847035bd842a8bf831e78c0465268ff Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 4 Sep 2019 12:19:04 +0200 Subject: [PATCH] Added code to load static methods in the shader source --- src/ComputeSharp.Shaders/ShaderRunner.cs | 3 +- .../Translation/ShaderLoader.cs | 75 ++++++++++++++++++- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index a2a31153..2815ce8b 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -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); diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index b4654506..641dde83 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -5,14 +5,17 @@ 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.Syntax; using Vortice.Direct3D12; +using ParameterInfo = ComputeSharp.Shaders.Renderer.Models.Functions.ParameterInfo; #pragma warning disable CS8618 // Non-nullable field is uninitialized @@ -138,6 +141,13 @@ namespace ComputeSharp.Shaders.Translation /// public string MethodBody { get; private set; } + private readonly List _FunctionsList = new List(); + + /// + /// Gets the collection of items for the shader + /// + public IReadOnlyList FunctionsList => _FunctionsList; + /// /// Loads and processes an input /// @@ -250,10 +260,16 @@ namespace ComputeSharp.Shaders.Translation 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 @@ -265,5 +281,58 @@ namespace ComputeSharp.Shaders.Translation MethodBody = MethodBody.TrimEnd('\n', '\r', ' '); MethodBody = HlslKnownKeywords.GetMappedText(MethodBody); } + + /// + /// Loads additional static methods used by the shader + /// + /// The HLSL name of the new method to load + /// The instance for the method to load + 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 parameters = ( + from parameter in root.ParameterList.Parameters.Select((p, i) => (Node: p, Index: i)) + let type = parameter.Node.Type.ToFullString() + let parameterName = parameter.Node.Identifier.ToFullString() + let last = parameter.Index == root.ParameterList.Parameters.Count - 1 + select new ParameterInfo(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, + name, + string.Join(", ", methodInfo.GetParameters().Select(p => $"{p.ParameterType.ToFriendlyString()} {p.Name}")), + root.ReturnType.ToFullString(), + name, + parameters, + body); + + _FunctionsList.Add(functionInfo); + } } }