From 8943b93a27ebe4199d7a48eb32383d66eff080a5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Aug 2019 01:24:43 +0200 Subject: [PATCH] Added code to load a shader method body to the ShaderLoader class --- .../Assets/ShaderTemplate.mustache | 2 -- .../ShaderLoader.cs | 35 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/DirectX12GameEngine.Shaders/Assets/ShaderTemplate.mustache b/src/DirectX12GameEngine.Shaders/Assets/ShaderTemplate.mustache index f4a5d855..41207601 100644 --- a/src/DirectX12GameEngine.Shaders/Assets/ShaderTemplate.mustache +++ b/src/DirectX12GameEngine.Shaders/Assets/ShaderTemplate.mustache @@ -20,6 +20,4 @@ cbuffer {{FieldName}}Buffer : register(b{{ConstantBufferIndex}}) [Shader("compute")] [NumThreads({{ThreadsX}}, {{ThreadsY}}, {{ThreadsZ}})] void CSMain(uint3 {{ThreadsIdsVariableName}} : SV_DispatchThreadId) -{ {{ShaderBody}} -} diff --git a/src/DirectX12GameEngine.Shaders/ShaderLoader.cs b/src/DirectX12GameEngine.Shaders/ShaderLoader.cs index 2d6b130c..e9bf8c98 100644 --- a/src/DirectX12GameEngine.Shaders/ShaderLoader.cs +++ b/src/DirectX12GameEngine.Shaders/ShaderLoader.cs @@ -3,10 +3,13 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; using System.Reflection; +using System.Text.RegularExpressions; using DirectX12GameEngine.Shaders.Mappings; using DirectX12GameEngine.Shaders.Primitives; using DirectX12GameEngine.Shaders.Renderer.Models.Abstract; using DirectX12GameEngine.Shaders.Renderer.Models.Fields; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; namespace DirectX12GameEngine.Shaders { @@ -54,6 +57,16 @@ namespace DirectX12GameEngine.Shaders /// public IReadOnlyList FieldsInfo => _FieldsInfo; + /// + /// Gets the name of the variable used as input for the shader method + /// + public string ThreadIdsIdentifierName { get; private set; } + + /// + /// Gets the generated source code for the method in the current shader + /// + public string MethodBody { get; private set; } + /// /// Loads and processes an input /// @@ -65,6 +78,7 @@ namespace DirectX12GameEngine.Shaders ShaderLoader @this = new ShaderLoader(action); @this.LoadFieldsInfo(); + @this.LoadMethodSource(); return @this; } @@ -101,5 +115,26 @@ namespace DirectX12GameEngine.Shaders _FieldsInfo.Add(processedFieldInfo); } } + + /// + /// Loads the entry method for the current shader being loaded + /// + private void LoadMethodSource() + { + // Decompile the shader method + MethodDecompiler.Instance.GetSyntaxTree(Action.Method, out SyntaxNode root, out SemanticModel semanticModel); + + // Rewrite the shader method (eg. to fix the type declarations) + ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel, true); + root = syntaxRewriter.Visit(root); + + // Get the thread ids identifier name and shader method body + ConstructorDeclarationSyntax methodNode = root.ChildNodes().OfType().First(); + ThreadIdsIdentifierName = methodNode.ParameterList.Parameters.First().Identifier.Text; + MethodBody = methodNode.Body.ToFullString(); + + // Additional preprocessing + MethodBody = Regex.Replace(MethodBody, @"\d+[fFdD]", m => m.Value.Replace("f", "")); + } } }