From f4bd3f87b9df6f0229436c4094d343dd87067bcf Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 1 Dec 2020 14:12:13 +0100 Subject: [PATCH] Minor code refactoring --- .../Attributes/ShaderMethodSourceAttribute.cs | 29 +++++++++++++++---- .../Translation/ShaderLoader{T}.cs | 2 +- .../ShaderMethodSourceGenerator.cs | 2 +- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs b/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs index db6ce1d1..508758f1 100644 --- a/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs +++ b/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs @@ -19,6 +19,16 @@ namespace ComputeSharp [Obsolete("This attribute is meant to be used from the source generator only")] public sealed class ShaderMethodSourceAttribute : Attribute { + /// + /// The identifier for the invoke method, for late binding. + /// + internal const string InvokeMethodIdentifier = "____"; + + /// + /// The source code for the target entry point method. + /// + private readonly string invokeMethod; + /// /// Creates a new instance with the specified parameters. /// @@ -28,9 +38,10 @@ namespace ComputeSharp /// The collection of processed methods. public ShaderMethodSourceAttribute(string methodName, string[] types, string invokeMethod, string[] methods) { + this.invokeMethod = invokeMethod; + MethodName = methodName; Types = types; - InvokeMethod = invokeMethod; Methods = methods; } @@ -44,16 +55,22 @@ namespace ComputeSharp /// internal IReadOnlyCollection Types { get; } - /// - /// Gets the source code for the target entry point method. - /// - internal string InvokeMethod { get; } - /// /// Gets the collection of processed methods. /// internal IReadOnlyCollection Methods { get; } + /// + /// Gets the mapped source code for the current method. + /// + /// The name to bind the method to. + /// The mapped source code for the current mehtod. + [Pure] + internal string GetMappedInvokeMethod(string name) + { + return this.invokeMethod.Replace(InvokeMethodIdentifier, name); + } + /// /// Gets the associated instance for a specified type. /// diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs index 97987103..51ad4b88 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs @@ -194,7 +194,7 @@ namespace ComputeSharp.Shaders.Translation var methodSource = ShaderMethodSourceAttribute.GetForDelegate(func); this.declaredTypes.AddRange(methodSource.Types); - this.methodsInfo.Add(methodSource.InvokeMethod.Replace("____", hlslName)); + this.methodsInfo.Add(methodSource.GetMappedInvokeMethod(hlslName)); this.methodsInfo.AddRange(methodSource.Methods); } else ThrowHelper.ThrowArgumentException("Invalid captured variable"); diff --git a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs index 7e6dae24..277ec2c5 100644 --- a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs @@ -100,7 +100,7 @@ namespace ComputeSharp.SourceGenerators // Rewrite the method syntax tree var processedMethod = shaderSourceRewriter .Visit(methodDeclaration)! - .WithIdentifier(Identifier("____")) + .WithIdentifier(Identifier(ShaderMethodSourceAttribute.InvokeMethodIdentifier)) .WithoutTrivia() .NormalizeWhitespace() .ToFullString();