Added code to load static methods in the shader source

このコミットが含まれているのは:
Sergio Pedri committed 2019-09-04 12:19:04 +02:00
1 親 817436f88c
コミット ae5386cce8
2ファイル変更
+74 -4

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

+2 -1
ファイルの表示
@@ -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);
+72 -3
ファイルの表示
@@ -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
/// </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>
@@ -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);
}
/// <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 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);
}
}
}