From 60028ce00ca560ac09badf5366caf7c158b18506 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 23 Sep 2023 16:59:34 +0200 Subject: [PATCH] Switch ID2D1Shader.HlslSource to a property --- ...ator.CreateBuildHlslSourceMethod.Syntax.cs | 24 +++++++------------ ...Generator.CreateEffectIdProperty.Syntax.cs | 2 +- .../ID2D1ShaderGenerator.cs | 6 ++--- .../Interfaces/__Internals/ID2D1Shader.cs | 5 ++-- .../Shaders/Interop/D2D1ReflectionServices.cs | 2 +- .../__Internals/D2D1ShaderCompiler.cs | 2 +- 6 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs index 5fbbe563..34363d08 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs @@ -16,32 +16,24 @@ partial class ID2D1ShaderGenerator partial class BuildHlslSource { /// - /// Creates a instance for the BuildHlslSource method. + /// Creates a instance for the HlslSource property. /// /// The input HLSL source. /// The depth of the hierarchy for this type (used to calculate the right indentation). - /// The resulting instance for the BuildHlslSource method. - public static MethodDeclarationSyntax GetSyntax(string hlslSource, int hierarchyDepth) + /// The resulting instance for the HlslSource property. + public static PropertyDeclarationSyntax GetSyntax(string hlslSource, int hierarchyDepth) { SyntaxToken hlslSourceLiteralExpression = SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth); - // This code produces a method declaration as follows: + // This code produces a property declaration as follows: // - // readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.BuildHlslSource(out string hlslSource) - // { - // hlslSource = ; - // } + // readonly string global::ComputeSharp.D2D1.__Internals.ID2D1Shader.HlslSource => ; return - MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(BuildHlslSource))) + PropertyDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("HlslSource")) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) - .AddParameterListParameters( - Parameter(Identifier("hlslSource")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(PredefinedType(Token(SyntaxKind.StringKeyword)))) - .WithBody(Block(ExpressionStatement( - AssignmentExpression( - SyntaxKind.SimpleAssignmentExpression, - IdentifierName("hlslSource"), - LiteralExpression(SyntaxKind.StringLiteralExpression, hlslSourceLiteralExpression))))); + .WithExpressionBody(ArrowExpressionClause(LiteralExpression(SyntaxKind.StringLiteralExpression, hlslSourceLiteralExpression))) + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectIdProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectIdProperty.Syntax.cs index 04e1e4bc..40a3f440 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectIdProperty.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectIdProperty.Syntax.cs @@ -79,7 +79,7 @@ partial class ID2D1ShaderGenerator .AddArgumentListArguments(Argument(IdentifierName("bytes")))) .WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword))))); - // This code produces a method declaration as follows: + // This code produces a property declaration as follows: // // readonly ref readonly global::System.Guid global::ComputeSharp.D2D1.__Internals.ID2D1Shader.EffectId // { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 034ec0d2..10e014e4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -321,10 +321,10 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Generate the BuildHlslSource() methods context.RegisterSourceOutput(hlslSourceInfo, static (context, item) => { - MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.HlslSource, item.Hierarchy.Hierarchy.Length); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, buildHlslStringMethod, canUseSkipLocalsInit: false); + PropertyDeclarationSyntax hlslStringProperty = BuildHlslSource.GetSyntax(item.Info.HlslSource, item.Info.Hierarchy.Hierarchy.Length); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Info.Hierarchy, hlslStringProperty, canUseSkipLocalsInit: false); - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); + context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.HlslSource.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); // Get a filtered sequence to enable caching for the HLSL source info, before the shader compilation step diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index c900b46b..236ab3b8 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -122,12 +122,11 @@ public interface ID2D1Shader where TLoader : struct, ID2D1DispatchDataLoader; /// - /// Builds the HLSL source code for the current shader instance. + /// Gets the HLSL source code for the current shader instance. /// - /// The resulting HLSL source. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be called directly by user code")] - void BuildHlslSource(out string hlslSource); + string HlslSource { get; } /// /// Loads the bytecode for the current shader. diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs index 958e4c5d..3ad440b7 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs @@ -33,7 +33,7 @@ public static class D2D1ReflectionServices { Unsafe.SkipInit(out T shader); - shader.BuildHlslSource(out string hlslSource); + string hlslSource = shader.HlslSource; D2D1ShaderBytecodeLoader bytecodeLoader = default; diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs index cda09946..95414a68 100644 --- a/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs +++ b/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs @@ -26,7 +26,7 @@ public static class D2D1ShaderCompiler where TLoader : struct, ID2D1BytecodeLoader where T : struct, ID2D1Shader { - Unsafe.AsRef(in shader).BuildHlslSource(out string hlslSource); + string hlslSource = Unsafe.AsRef(in shader).HlslSource; using ComPtr d3DBlobBytecode = Shaders.Translation.D3DCompiler.Compile( hlslSource.AsSpan(),