From 0bccf6ec57c8fb770977ed5ea7789bb217043fac Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 23 Sep 2023 16:27:50 +0200 Subject: [PATCH 1/9] Switch ID2D1Shader.PixelOptions to a property --- ...ator.CreateGetPixelOptionsMethod.Syntax.cs | 35 +++++++++++++------ .../ID2D1ShaderGenerator.cs | 4 +-- .../Interfaces/__Internals/ID2D1Shader.cs | 3 +- .../Shaders/Interop/D2D1PixelShader.cs | 2 +- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.Syntax.cs index e5d9b082..e6338f55 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.Syntax.cs @@ -14,23 +14,38 @@ partial class ID2D1ShaderGenerator partial class GetPixelOptions { /// - /// Creates a instance for the GetPixelOptions method. + /// Creates a instance for the PixelOptions property. /// /// The pixel options for the shader. - /// The resulting instance for the GetPixelOptions method. - public static MethodDeclarationSyntax GetSyntax(D2D1PixelOptions pixelOptions) + /// The resulting instance for the PixelOptions property. + public static PropertyDeclarationSyntax GetSyntax(D2D1PixelOptions pixelOptions) { - // This code produces a method declaration as follows: + ExpressionSyntax pixelOptionsExpression; + + // Set the right expression if the pixel options are valid + if (pixelOptions is D2D1PixelOptions.None or D2D1PixelOptions.TrivialSampling) + { + pixelOptionsExpression = + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("ComputeSharp.D2D1.D2D1PixelOptions"), + IdentifierName(pixelOptions.ToString())); + } + else + { + // Otherwise just return default (the analyzer will emit a diagnostic) + pixelOptionsExpression = LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)); + } + + // This code produces a property declaration as follows: // - // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetPixelOptions() - // { - // return ; - // } + // readonly ComputeSharp.D2D1.D2D1PixelOptions global::ComputeSharp.D2D1.__Internals.ID2D1Shader.PixelOptions => ; return - MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier(nameof(GetPixelOptions))) + PropertyDeclaration(IdentifierName("ComputeSharp.D2D1.D2D1PixelOptions"), Identifier("PixelOptions")) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) - .WithBody(Block(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)pixelOptions))))); + .WithExpressionBody(ArrowExpressionClause(pixelOptionsExpression)) + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index a9b53915..5f779817 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -423,8 +423,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Generate the GetPixelOptions() methods context.RegisterSourceOutput(pixelOptionsInfo, static (context, item) => { - MethodDeclarationSyntax getPixelOptionsMethod = GetPixelOptions.GetSyntax(item.PixelOptions); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, getPixelOptionsMethod, canUseSkipLocalsInit: false); + PropertyDeclarationSyntax pixelOptionsProperty = GetPixelOptions.GetSyntax(item.PixelOptions); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, pixelOptionsProperty, canUseSkipLocalsInit: false); context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetPixelOptions)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index ef33bcde..0dc9676b 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -64,10 +64,9 @@ public interface ID2D1Shader /// /// Gets the pixel options for the current shader. /// - /// The pixel options for the current shader. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be used directly by user code")] - uint GetPixelOptions(); + D2D1PixelOptions PixelOptions { get; } /// /// Gets the number of inputs for the current shader. diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index c10cb2b1..5bd65dff 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -242,7 +242,7 @@ public static class D2D1PixelShader { Unsafe.SkipInit(out T shader); - return (D2D1PixelOptions)shader.GetPixelOptions(); + return shader.PixelOptions; } /// From d5c00e048a871283b6f2649a6bd39575c5b950cf Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 23 Sep 2023 16:33:26 +0200 Subject: [PATCH 2/9] Rename PixelOptions helper class in generator --- ...erGenerator.CreatePixelOptionsProperty.Syntax.cs} | 4 ++-- ...2D1ShaderGenerator.CreatePixelOptionsProperty.cs} | 4 ++-- .../ID2D1ShaderGenerator.cs | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) rename src/ComputeSharp.D2D1.SourceGenerators/{ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.Syntax.cs => ID2D1ShaderGenerator.CreatePixelOptionsProperty.Syntax.cs} (96%) rename src/ComputeSharp.D2D1.SourceGenerators/{ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.cs => ID2D1ShaderGenerator.CreatePixelOptionsProperty.cs} (89%) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreatePixelOptionsProperty.Syntax.cs similarity index 96% rename from src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.Syntax.cs rename to src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreatePixelOptionsProperty.Syntax.cs index e6338f55..7dac3342 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreatePixelOptionsProperty.Syntax.cs @@ -11,7 +11,7 @@ namespace ComputeSharp.D2D1.SourceGenerators; partial class ID2D1ShaderGenerator { /// - partial class GetPixelOptions + partial class PixelOptions { /// /// Creates a instance for the PixelOptions property. @@ -41,7 +41,7 @@ partial class ID2D1ShaderGenerator // // readonly ComputeSharp.D2D1.D2D1PixelOptions global::ComputeSharp.D2D1.__Internals.ID2D1Shader.PixelOptions => ; return - PropertyDeclaration(IdentifierName("ComputeSharp.D2D1.D2D1PixelOptions"), Identifier("PixelOptions")) + PropertyDeclaration(IdentifierName("ComputeSharp.D2D1.D2D1PixelOptions"), Identifier(nameof(PixelOptions))) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .WithExpressionBody(ArrowExpressionClause(pixelOptionsExpression)) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreatePixelOptionsProperty.cs similarity index 89% rename from src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.cs rename to src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreatePixelOptionsProperty.cs index 428cbe61..08907815 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreatePixelOptionsProperty.cs @@ -7,9 +7,9 @@ namespace ComputeSharp.D2D1.SourceGenerators; partial class ID2D1ShaderGenerator { /// - /// A helper with all logic to generate the GetPixelOptions method. + /// A helper with all logic to generate the PixelOptions property. /// - private static partial class GetPixelOptions + private static partial class PixelOptions { /// /// Extracts the pixel options for the current shader. diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 5f779817..220d70e1 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -139,8 +139,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator token.ThrowIfCancellationRequested(); - // Get the info for GetPixelOptions() - GetPixelOptions.GetInfo(typeSymbol, out D2D1PixelOptions pixelOptions); + // Get the info for PixelOptions + PixelOptions.GetInfo(typeSymbol, out D2D1PixelOptions pixelOptions); token.ThrowIfCancellationRequested(); @@ -415,18 +415,18 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.{nameof(InputDescriptions)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Get the GetPixelOptions() info (hierarchy and pixel options) + // Get the PixelOptions info (hierarchy and pixel options) IncrementalValuesProvider<(HierarchyInfo Hierarchy, D2D1PixelOptions PixelOptions)> pixelOptionsInfo = shaderInfoWithErrors .Select(static (item, _) => (item.Hierarchy, item.PixelOptions)); - // Generate the GetPixelOptions() methods + // Generate the PixelOptions properties context.RegisterSourceOutput(pixelOptionsInfo, static (context, item) => { - PropertyDeclarationSyntax pixelOptionsProperty = GetPixelOptions.GetSyntax(item.PixelOptions); + PropertyDeclarationSyntax pixelOptionsProperty = PixelOptions.GetSyntax(item.PixelOptions); CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, pixelOptionsProperty, canUseSkipLocalsInit: false); - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetPixelOptions)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(PixelOptions)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); } } \ No newline at end of file From 81dd2e2b7e99ffddcd2b86d79f44a7e70b48974f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 23 Sep 2023 16:42:15 +0200 Subject: [PATCH 3/9] Add ID2D1Shader.BufferPrecision property and generate it --- ...ator.CreateGetOutputBufferMethod.Syntax.cs | 36 +++++++++++++++++++ .../ID2D1ShaderGenerator.cs | 5 +++ .../Interfaces/__Internals/ID2D1Shader.cs | 7 ++++ 3 files changed, 48 insertions(+) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs index 02b0cd88..cf5326e8 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs @@ -1,3 +1,4 @@ +using System; using ComputeSharp.D2D1.__Internals; using ComputeSharp.D2D1.SourceGenerators.Models; using Microsoft.CodeAnalysis.CSharp; @@ -14,6 +15,41 @@ partial class ID2D1ShaderGenerator /// partial class GetOutputBuffer { + /// + /// Creates a instance for the BufferPrecision property. + /// + /// The input info for the shader output buffer. + /// The resulting instance for the BufferPrecision property. + public static PropertyDeclarationSyntax GetBufferPrecisionSyntax(OutputBufferInfo info) + { + ExpressionSyntax bufferPrecisionExpression; + + // Set the right expression if the buffer options are valid + if (Enum.IsDefined(typeof(D2D1BufferPrecision), info.BufferPrecision)) + { + bufferPrecisionExpression = + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("ComputeSharp.D2D1.D2D1BufferPrecision"), + IdentifierName(info.BufferPrecision.ToString())); + } + else + { + // Otherwise just return default (the analyzer will emit a diagnostic) + bufferPrecisionExpression = LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)); + } + + // This code produces a property declaration as follows: + // + // readonly ComputeSharp.D2D1.D2D1BufferPrecision global::ComputeSharp.D2D1.__Internals.ID2D1Shader.BufferPrecision => ; + return + PropertyDeclaration(IdentifierName("ComputeSharp.D2D1.D2D1BufferPrecision"), Identifier("BufferPrecision")) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) + .WithExpressionBody(ArrowExpressionClause(bufferPrecisionExpression)) + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); + } + /// /// Creates a instance for the GetOutputBuffer method. /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 220d70e1..0f5cfc9a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -398,6 +398,11 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, getOutputBufferMethod, canUseSkipLocalsInit: false); context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetOutputBuffer)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); + + PropertyDeclarationSyntax bufferPrecisionProperty = GetOutputBuffer.GetBufferPrecisionSyntax(item.OutputBuffer); + CompilationUnitSyntax bufferPrecisionCompilationUnit = GetCompilationUnitFromMember(item.Hierarchy, bufferPrecisionProperty, canUseSkipLocalsInit: false); + + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.BufferPrecision.g.cs", bufferPrecisionCompilationUnit.GetText(Encoding.UTF8)); }); // Get the InputDescriptions info (hierarchy and input descriptions) diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 0dc9676b..1da64a8d 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -97,6 +97,13 @@ public interface ID2D1Shader [Obsolete("This method is not intended to be used directly by user code")] ReadOnlyMemory ResourceTextureDescriptions { get; } + /// + /// Gets the buffer precision for the shader. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be used directly by user code")] + D2D1BufferPrecision BufferPrecision { get; } + /// /// Gets the output buffer precision and depth for the shader. /// From a71c4db7dd21c137fce443eb70c40a505e7c2651 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 23 Sep 2023 16:46:06 +0200 Subject: [PATCH 4/9] Add ID2D1Shader.ChannelDepth property and generate it --- ...ator.CreateGetOutputBufferMethod.Syntax.cs | 35 +++++++++++++++++++ .../ID2D1ShaderGenerator.cs | 5 +++ .../Interfaces/__Internals/ID2D1Shader.cs | 7 ++++ 3 files changed, 47 insertions(+) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs index cf5326e8..6f001a18 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs @@ -50,6 +50,41 @@ partial class ID2D1ShaderGenerator .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); } + /// + /// Creates a instance for the ChannelDepth property. + /// + /// The input info for the shader output buffer. + /// The resulting instance for the ChannelDepth property. + public static PropertyDeclarationSyntax GetChannelDepthSyntax(OutputBufferInfo info) + { + ExpressionSyntax channelDepthExpression; + + // Set the right expression if the buffer options are valid + if (Enum.IsDefined(typeof(D2D1ChannelDepth), info.ChannelDepth)) + { + channelDepthExpression = + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("ComputeSharp.D2D1.D2D1ChannelDepth"), + IdentifierName(info.ChannelDepth.ToString())); + } + else + { + // Otherwise just return default (the analyzer will emit a diagnostic) + channelDepthExpression = LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)); + } + + // This code produces a property declaration as follows: + // + // readonly ComputeSharp.D2D1.D2D1ChannelDepth global::ComputeSharp.D2D1.__Internals.ID2D1Shader.ChannelDepth => ; + return + PropertyDeclaration(IdentifierName("ComputeSharp.D2D1.D2D1ChannelDepth"), Identifier("ChannelDepth")) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) + .WithExpressionBody(ArrowExpressionClause(channelDepthExpression)) + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); + } + /// /// Creates a instance for the GetOutputBuffer method. /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 0f5cfc9a..dc033c56 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -403,6 +403,11 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator CompilationUnitSyntax bufferPrecisionCompilationUnit = GetCompilationUnitFromMember(item.Hierarchy, bufferPrecisionProperty, canUseSkipLocalsInit: false); context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.BufferPrecision.g.cs", bufferPrecisionCompilationUnit.GetText(Encoding.UTF8)); + + PropertyDeclarationSyntax channelDepthProperty = GetOutputBuffer.GetChannelDepthSyntax(item.OutputBuffer); + CompilationUnitSyntax channelDepthCompilationUnit = GetCompilationUnitFromMember(item.Hierarchy, channelDepthProperty, canUseSkipLocalsInit: false); + + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.ChannelDepth.g.cs", channelDepthCompilationUnit.GetText(Encoding.UTF8)); }); // Get the InputDescriptions info (hierarchy and input descriptions) diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 1da64a8d..1ae9c1f6 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -104,6 +104,13 @@ public interface ID2D1Shader [Obsolete("This method is not intended to be used directly by user code")] D2D1BufferPrecision BufferPrecision { get; } + /// + /// Gets the channel depth for the shader. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be used directly by user code")] + D2D1ChannelDepth ChannelDepth { get; } + /// /// Gets the output buffer precision and depth for the shader. /// From ca77bab1f2cb0b508b0f3050bc39e04a1d8df83e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 23 Sep 2023 16:53:17 +0200 Subject: [PATCH 5/9] Remove ID2D1Shader.GetOutputBuffer method and generation --- ...or.CreateOutputBufferProperties.Syntax.cs} | 36 +------------------ ...Generator.CreateOutputBufferProperties.cs} | 4 +-- .../ID2D1ShaderGenerator.cs | 17 ++++----- .../Interfaces/__Internals/ID2D1Shader.cs | 9 ----- .../Shaders/Interop/D2D1PixelShader.cs | 8 ++--- 5 files changed, 11 insertions(+), 63 deletions(-) rename src/ComputeSharp.D2D1.SourceGenerators/{ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs => ID2D1ShaderGenerator.CreateOutputBufferProperties.Syntax.cs} (66%) rename src/ComputeSharp.D2D1.SourceGenerators/{ID2D1ShaderGenerator.CreateGetOutputBufferMethod.cs => ID2D1ShaderGenerator.CreateOutputBufferProperties.cs} (95%) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateOutputBufferProperties.Syntax.cs similarity index 66% rename from src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs rename to src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateOutputBufferProperties.Syntax.cs index 6f001a18..a7c73bf3 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateOutputBufferProperties.Syntax.cs @@ -13,7 +13,7 @@ namespace ComputeSharp.D2D1.SourceGenerators; partial class ID2D1ShaderGenerator { /// - partial class GetOutputBuffer + partial class OutputBuffer { /// /// Creates a instance for the BufferPrecision property. @@ -84,39 +84,5 @@ partial class ID2D1ShaderGenerator .WithExpressionBody(ArrowExpressionClause(channelDepthExpression)) .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); } - - /// - /// Creates a instance for the GetOutputBuffer method. - /// - /// The input info for the shader output buffer. - /// The resulting instance for the GetOutputBuffer method. - public static MethodDeclarationSyntax GetSyntax(OutputBufferInfo info) - { - // This code produces a method declaration as follows: - // - // readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetOutputBuffer(out uint precision, out uint depth) - // { - // precision = ; - // depth = ; - // } - return - MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(GetOutputBuffer))) - .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) - .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) - .AddParameterListParameters( - Parameter(Identifier("precision")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(PredefinedType(Token(SyntaxKind.UIntKeyword))), - Parameter(Identifier("depth")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(PredefinedType(Token(SyntaxKind.UIntKeyword)))) - .WithBody(Block( - ExpressionStatement( - AssignmentExpression( - SyntaxKind.SimpleAssignmentExpression, - IdentifierName("precision"), - LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)info.BufferPrecision)))), - ExpressionStatement( - AssignmentExpression( - SyntaxKind.SimpleAssignmentExpression, - IdentifierName("depth"), - LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)info.ChannelDepth)))))); - } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateOutputBufferProperties.cs similarity index 95% rename from src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.cs rename to src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateOutputBufferProperties.cs index e2f0b848..9c01e195 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateOutputBufferProperties.cs @@ -7,9 +7,9 @@ namespace ComputeSharp.D2D1.SourceGenerators; partial class ID2D1ShaderGenerator { /// - /// A helper with all logic to generate the GetOutputBuffer method. + /// A helper with all logic to generate the output buffer properties. /// - private static partial class GetOutputBuffer + private static partial class OutputBuffer { /// /// Extracts the output buffer info for the current shader. diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index dc033c56..034ec0d2 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -126,8 +126,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator token.ThrowIfCancellationRequested(); - // Get the info for GetOutputBuffer() - GetOutputBuffer.GetInfo(typeSymbol, out D2D1BufferPrecision bufferPrecision, out D2D1ChannelDepth channelDepth); + // Get the info for the output buffer properties + OutputBuffer.GetInfo(typeSymbol, out D2D1BufferPrecision bufferPrecision, out D2D1ChannelDepth channelDepth); token.ThrowIfCancellationRequested(); @@ -386,25 +386,20 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadBytecode)}.g.cs", text); }); - // Get the GetOutputBuffer() info (hierarchy and output buffers) + // Get the output buffer info (hierarchy and output buffers) IncrementalValuesProvider<(HierarchyInfo Hierarchy, OutputBufferInfo OutputBuffer)> outputBufferInfo = shaderInfoWithErrors .Select(static (item, _) => (item.Hierarchy, item.OutputBuffer)); - // Generate the GetOutputBuffer() methods + // Generate the output buffer properties context.RegisterSourceOutput(outputBufferInfo, static (context, item) => { - MethodDeclarationSyntax getOutputBufferMethod = GetOutputBuffer.GetSyntax(item.OutputBuffer); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, getOutputBufferMethod, canUseSkipLocalsInit: false); - - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetOutputBuffer)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); - - PropertyDeclarationSyntax bufferPrecisionProperty = GetOutputBuffer.GetBufferPrecisionSyntax(item.OutputBuffer); + PropertyDeclarationSyntax bufferPrecisionProperty = OutputBuffer.GetBufferPrecisionSyntax(item.OutputBuffer); CompilationUnitSyntax bufferPrecisionCompilationUnit = GetCompilationUnitFromMember(item.Hierarchy, bufferPrecisionProperty, canUseSkipLocalsInit: false); context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.BufferPrecision.g.cs", bufferPrecisionCompilationUnit.GetText(Encoding.UTF8)); - PropertyDeclarationSyntax channelDepthProperty = GetOutputBuffer.GetChannelDepthSyntax(item.OutputBuffer); + PropertyDeclarationSyntax channelDepthProperty = OutputBuffer.GetChannelDepthSyntax(item.OutputBuffer); CompilationUnitSyntax channelDepthCompilationUnit = GetCompilationUnitFromMember(item.Hierarchy, channelDepthProperty, canUseSkipLocalsInit: false); context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.ChannelDepth.g.cs", channelDepthCompilationUnit.GetText(Encoding.UTF8)); diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 1ae9c1f6..c900b46b 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -111,15 +111,6 @@ public interface ID2D1Shader [Obsolete("This method is not intended to be used directly by user code")] D2D1ChannelDepth ChannelDepth { get; } - /// - /// Gets the output buffer precision and depth for the shader. - /// - /// The output buffer precision. - /// The output buffer channel depth. - [EditorBrowsable(EditorBrowsableState.Never)] - [Obsolete("This method is not intended to be used directly by user code")] - void GetOutputBuffer(out uint precision, out uint depth); - /// /// Loads the dispatch data for the shader. /// diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index 5bd65dff..e2cd3232 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -307,9 +307,7 @@ public static class D2D1PixelShader { Unsafe.SkipInit(out T shader); - shader.GetOutputBuffer(out uint precision, out _); - - return (D2D1BufferPrecision)precision; + return shader.BufferPrecision; } /// @@ -322,9 +320,7 @@ public static class D2D1PixelShader { Unsafe.SkipInit(out T shader); - shader.GetOutputBuffer(out _, out uint depth); - - return (D2D1ChannelDepth)depth; + return shader.ChannelDepth; } /// From 60028ce00ca560ac09badf5366caf7c158b18506 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 23 Sep 2023 16:59:34 +0200 Subject: [PATCH 6/9] 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(), From e69f083b299430dd978718162ad651d1ce9c4c22 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 23 Sep 2023 17:08:59 +0200 Subject: [PATCH 7/9] Update and rename source generator files --- ...erGenerator.CreateHlslSourceProperty.Syntax.cs} | 4 ++-- ...2D1ShaderGenerator.CreateHlslSourceProperty.cs} | 6 ++---- .../ID2D1ShaderGenerator.cs | 14 +++++++------- 3 files changed, 11 insertions(+), 13 deletions(-) rename src/ComputeSharp.D2D1.SourceGenerators/{ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs => ID2D1ShaderGenerator.CreateHlslSourceProperty.Syntax.cs} (95%) rename src/ComputeSharp.D2D1.SourceGenerators/{ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs => ID2D1ShaderGenerator.CreateHlslSourceProperty.cs} (99%) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateHlslSourceProperty.Syntax.cs similarity index 95% rename from src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs rename to src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateHlslSourceProperty.Syntax.cs index 34363d08..21ff370c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateHlslSourceProperty.Syntax.cs @@ -13,7 +13,7 @@ namespace ComputeSharp.D2D1.SourceGenerators; partial class ID2D1ShaderGenerator { /// - partial class BuildHlslSource + partial class HlslSource { /// /// Creates a instance for the HlslSource property. @@ -29,7 +29,7 @@ partial class ID2D1ShaderGenerator // // readonly string global::ComputeSharp.D2D1.__Internals.ID2D1Shader.HlslSource => ; return - PropertyDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("HlslSource")) + PropertyDeclaration(PredefinedType(Token(SyntaxKind.StringKeyword)), Identifier("HlslSource")) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .WithExpressionBody(ArrowExpressionClause(LiteralExpression(SyntaxKind.StringLiteralExpression, hlslSourceLiteralExpression))) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateHlslSourceProperty.cs similarity index 99% rename from src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs rename to src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateHlslSourceProperty.cs index 41b86846..73ece91a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateHlslSourceProperty.cs @@ -14,17 +14,15 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -#pragma warning disable CS0618 - namespace ComputeSharp.D2D1.SourceGenerators; /// partial class ID2D1ShaderGenerator { /// - /// A helper with all logic to generate the BuildHlslSource method. + /// A helper with all logic to generate the HlslSource property. /// - private static partial class BuildHlslSource + private static partial class HlslSource { /// /// Gathers all necessary information on a transpiled HLSL source for a given shader type. diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 10e014e4..7766cf98 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -107,8 +107,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator inputCount, out ImmutableArray resourceTextureDescriptions); - // Get HLSL source for BuildHlslSource() - string hlslSource = BuildHlslSource.GetHlslSource( + // Get HLSL source for HlslSource + string hlslSource = HlslSource.GetHlslSource( diagnostics, context.SemanticModel.Compilation, typeDeclaration, @@ -313,18 +313,18 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadDispatchData)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Get the BuildHlslSource() info (hierarchy and HLSL source) + // Get the HlslSource info (hierarchy, HLSL source and parsing options) IncrementalValuesProvider<(HierarchyInfo Hierarchy, string HlslSource)> hlslSourceInfo = shaderInfoWithErrors .Select(static (item, _) => (item.Hierarchy, item.HlslShaderSource.HlslSource)); - // Generate the BuildHlslSource() methods + // Generate the HlslSource properties context.RegisterSourceOutput(hlslSourceInfo, static (context, item) => { - PropertyDeclarationSyntax hlslStringProperty = BuildHlslSource.GetSyntax(item.Info.HlslSource, item.Info.Hierarchy.Hierarchy.Length); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Info.Hierarchy, hlslStringProperty, canUseSkipLocalsInit: false); + PropertyDeclarationSyntax hlslStringProperty = HlslSource.GetSyntax(item.HlslSource, item.Hierarchy.Hierarchy.Length); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, hlslStringProperty, canUseSkipLocalsInit: false); - context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.HlslSource.g.cs", compilationUnit.GetText(Encoding.UTF8)); + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(HlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); // Get a filtered sequence to enable caching for the HLSL source info, before the shader compilation step From 0780345ef9067f7c29d88efac6710ad6a69bad57 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 23 Sep 2023 18:12:53 +0200 Subject: [PATCH 8/9] Fix indentation of HLSL source expression --- ...haderGenerator.CreateHlslSourceProperty.Syntax.cs | 3 ++- .../Helpers/SyntaxTokenHelper.cs | 12 +++++------- ...erGenerator.CreateBuildHlslSourceMethod.Syntax.cs | 3 ++- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateHlslSourceProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateHlslSourceProperty.Syntax.cs index 21ff370c..70c3c9e8 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateHlslSourceProperty.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateHlslSourceProperty.Syntax.cs @@ -23,7 +23,8 @@ partial class ID2D1ShaderGenerator /// The resulting instance for the HlslSource property. public static PropertyDeclarationSyntax GetSyntax(string hlslSource, int hierarchyDepth) { - SyntaxToken hlslSourceLiteralExpression = SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth); + // The indentation level is the current depth + 1 (for the property) + SyntaxToken hlslSourceLiteralExpression = SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth + 1); // This code produces a property declaration as follows: // diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SyntaxTokenHelper.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SyntaxTokenHelper.cs index f537568f..8210504c 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SyntaxTokenHelper.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SyntaxTokenHelper.cs @@ -17,13 +17,11 @@ internal static class SyntaxTokenHelper /// A representing a raw multiline string literal with the value of . public static SyntaxToken CreateRawMultilineStringLiteral(string text, int indentationLevel) { - // Create a token to represent the raw multiline string literal expression. Here some spaces are - // also added to properly align the resulting text with one indentation below the declaring string constant. - // The spaces are: 4 for each containing type, 4 for the containing method, and 4 for the one additional indentation. - // An extra newline and indentation has to be added to the raw text when there is no trailing newline, as not doing - // so would otherwise case the terminating """ token to fall on the same line, which is invalid syntax. This is only - // needed to make the code valid, as the actual literal context of the string is not affected and remains the same. - string indentation = new(' ', (4 * indentationLevel) + 4 + 4); + // Create a token to represent the raw multiline string literal expression. An extra newline and indentation + // has to be added to the raw text when there is no trailing newline, as not doing so would otherwise case + // the terminating """ token to fall on the same line, which is invalid syntax. This is only needed to make + // the code valid, as the actual literal context of the string is not affected and remains the same. + string indentation = new(' ', 4 * indentationLevel); return Token( TriviaList(), diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs index c0484d51..3b693127 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs @@ -119,7 +119,8 @@ partial class IShaderGenerator sizeHint += textBuilder.Length; - SyntaxToken hlslSourceLiteralExpression = SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth); + // The indentation level is the current depth + 2 (the method, and the return expression) + SyntaxToken hlslSourceLiteralExpression = SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth + 2); statements.Add( ExpressionStatement( From 32ceaf3def73e144e025af595ac72638762f9da3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 28 Sep 2023 00:55:28 +0200 Subject: [PATCH 9/9] Switch ID2D1Shader.InputCount to a property --- ...erator.CreateInputCountProperty.Syntax.cs} | 22 +++++++++---------- .../ID2D1ShaderGenerator.cs | 10 ++++----- .../Interfaces/__Internals/ID2D1Shader.cs | 3 +-- .../Shaders/Interop/D2D1PixelShader.cs | 2 +- 4 files changed, 17 insertions(+), 20 deletions(-) rename src/ComputeSharp.D2D1.SourceGenerators/{ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs => ID2D1ShaderGenerator.CreateInputCountProperty.Syntax.cs} (52%) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputCountProperty.Syntax.cs similarity index 52% rename from src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs rename to src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputCountProperty.Syntax.cs index 92326c75..e92a4b0e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputCountProperty.Syntax.cs @@ -11,28 +11,26 @@ namespace ComputeSharp.D2D1.SourceGenerators; partial class ID2D1ShaderGenerator { /// - /// A helper with all logic to generate the GetInputCount method. + /// A helper with all logic to generate the InputCount property. /// - private static partial class GetInputCount + private static partial class InputCount { /// - /// Creates a instance for the GetInputCount method. + /// Creates a instance for the InputCount property. /// /// The number of inputs for the shader. - /// The resulting instance for the GetInputCount method. - public static MethodDeclarationSyntax GetSyntax(int inputCount) + /// The resulting instance for the InputCount property. + public static PropertyDeclarationSyntax GetSyntax(int inputCount) { - // This code produces a method declaration as follows: + // This code produces a property declaration as follows: // - // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputCount() - // { - // return ; - // } + // readonly int global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputCount => ; return - MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier(nameof(GetInputCount))) + PropertyDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), Identifier(nameof(InputCount))) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) - .WithBody(Block(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputCount))))); + .WithExpressionBody(ArrowExpressionClause(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputCount)))) + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 7766cf98..5194aafa 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -243,18 +243,18 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.EffectAuthor.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Get the GetInputCount() info (hierarchy and input count) + // Get the InputCount info (hierarchy and input count) IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo = shaderInfoWithErrors .Select(static (item, _) => (item.Hierarchy, item.InputTypes.InputTypes.Length)); - // Generate the GetInputCount() methods + // Generate the InputCount properties context.RegisterSourceOutput(inputCountInfo, static (context, item) => { - MethodDeclarationSyntax getInputCountMethod = GetInputCount.GetSyntax(item.InputCount); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, getInputCountMethod, canUseSkipLocalsInit: false); + PropertyDeclarationSyntax inputCountProperty = InputCount.GetSyntax(item.InputCount); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, inputCountProperty, canUseSkipLocalsInit: false); - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetInputCount)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(InputCount)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); // Get the InputTypes info (hierarchy and input types) diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 236ab3b8..e3de74cf 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -71,10 +71,9 @@ public interface ID2D1Shader /// /// Gets the number of inputs for the current shader. /// - /// The number of inputs for the current shader. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be used directly by user code")] - uint GetInputCount(); + int InputCount { get; } /// /// Gets the input types for the current shader. diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index e2cd3232..60f63619 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -255,7 +255,7 @@ public static class D2D1PixelShader { Unsafe.SkipInit(out T shader); - return (int)shader.GetInputCount(); + return shader.InputCount; } ///