diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Functions/LocalFunctionInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Functions/LocalFunctionInfo.cs new file mode 100644 index 00000000..4dfd45f9 --- /dev/null +++ b/src/ComputeSharp.Shaders/Renderer/Models/Functions/LocalFunctionInfo.cs @@ -0,0 +1,29 @@ +using System; + +namespace ComputeSharp.Shaders.Renderer.Models.Functions +{ + /// + /// A that contains info on a local shader function + /// + internal sealed class LocalFunctionInfo + { + /// + /// Gets the prototype for the current function + /// + public string FunctionPrototype => FunctionDeclaration.Split(Environment.NewLine)[0]; + + /// + /// Gets the declaration of the current function + /// + public string FunctionDeclaration { get; } + + /// + /// Creates a new instance with the specified parameters + /// + /// The declaration of the current function + public LocalFunctionInfo(string functionDeclaration) + { + FunctionDeclaration = functionDeclaration; + } + } +} diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index 306dd3f7..fd6e92ed 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -51,5 +51,10 @@ namespace ComputeSharp.Shaders.Renderer.Models /// Gets or sets the list of static functions used by the shader /// public IReadOnlyList FunctionsList { get; set; } + + /// + /// Gets or sets the list of local (implicit) functions used by the shader + /// + public IReadOnlyList LocalFunctionsList { get; set; } } } diff --git a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache index 2be993e2..62515497 100644 --- a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache +++ b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache @@ -45,6 +45,12 @@ cbuffer _{{FieldName}} : register(b{{BufferIndex}}) {{ReturnType}} {{FunctionName}}({{#ParametersList}}{{ParameterModifier}} {{ParameterType}} {{ParameterName}}{{^IsLastParameter}}, {{/IsLastParameter}}{{/ParametersList}}) {{FunctionBody}} {{/FunctionsList}} +{{#LocalFunctionsList}} + +{{!Shader implicit functions}} +// {{FunctionPrototype}} +{{FunctionDeclaration}} +{{/LocalFunctionsList}} {{!Shader entry point}} // Shader body diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index c149e409..038d4b8a 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -76,7 +76,8 @@ namespace ComputeSharp.Shaders NumThreadsZ = threadsZ, ThreadsIdsVariableName = shaderLoader.ThreadsIdsVariableName, ShaderBody = shaderLoader.MethodBody, - FunctionsList = shaderLoader.FunctionsList + FunctionsList = shaderLoader.FunctionsList, + LocalFunctionsList = shaderLoader.LocalFunctionsList }; string shaderSource = ShaderRenderer.Instance.Render(shaderInfo); diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 30c5ddc4..13576969 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -148,6 +148,13 @@ namespace ComputeSharp.Shaders.Translation /// public IReadOnlyList FunctionsList => _FunctionsList; + private readonly List _LocalFunctionsList = new List(); + + /// + /// Gets the collection of items for the shader + /// + public IReadOnlyList LocalFunctionsList => _LocalFunctionsList; + /// /// Gets a unique hash code for a given /// @@ -295,6 +302,11 @@ namespace ComputeSharp.Shaders.Translation ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel); root = (MethodDeclarationSyntax)syntaxRewriter.Visit(root); + // Extract the implicit local functions + var locals = root.DescendantNodes().OfType().ToArray(); + root = root.RemoveNodes(locals, SyntaxRemoveOptions.KeepNoTrivia); + _LocalFunctionsList.AddRange(locals.Select(local => new LocalFunctionInfo(local.ToString()))); + // Register the captured static members foreach (var member in syntaxRewriter.StaticMembers) {