diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs deleted file mode 100644 index 5fbbe563..00000000 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs +++ /dev/null @@ -1,47 +0,0 @@ -using ComputeSharp.D2D1.__Internals; -using ComputeSharp.SourceGeneration.Helpers; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -#pragma warning disable CS0618 - -namespace ComputeSharp.D2D1.SourceGenerators; - -/// -partial class ID2D1ShaderGenerator -{ - /// - partial class BuildHlslSource - { - /// - /// Creates a instance for the BuildHlslSource method. - /// - /// 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) - { - SyntaxToken hlslSourceLiteralExpression = SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth); - - // This code produces a method declaration as follows: - // - // readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.BuildHlslSource(out string hlslSource) - // { - // hlslSource = ; - // } - return - MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(BuildHlslSource))) - .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))))); - } - } -} \ 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.CreateGetOutputBufferMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs deleted file mode 100644 index 02b0cd88..00000000 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetOutputBufferMethod.Syntax.cs +++ /dev/null @@ -1,51 +0,0 @@ -using ComputeSharp.D2D1.__Internals; -using ComputeSharp.D2D1.SourceGenerators.Models; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -#pragma warning disable CS0618 - -namespace ComputeSharp.D2D1.SourceGenerators; - -/// -partial class ID2D1ShaderGenerator -{ - /// - partial class GetOutputBuffer - { - /// - /// 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.CreateGetPixelOptionsMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.Syntax.cs deleted file mode 100644 index e5d9b082..00000000 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetPixelOptionsMethod.Syntax.cs +++ /dev/null @@ -1,36 +0,0 @@ -using ComputeSharp.D2D1.__Internals; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -#pragma warning disable CS0618 - -namespace ComputeSharp.D2D1.SourceGenerators; - -/// -partial class ID2D1ShaderGenerator -{ - /// - partial class GetPixelOptions - { - /// - /// Creates a instance for the GetPixelOptions method. - /// - /// The pixel options for the shader. - /// The resulting instance for the GetPixelOptions method. - public static MethodDeclarationSyntax GetSyntax(D2D1PixelOptions pixelOptions) - { - // This code produces a method declaration as follows: - // - // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetPixelOptions() - // { - // return ; - // } - return - MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier(nameof(GetPixelOptions))) - .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) - .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) - .WithBody(Block(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)pixelOptions))))); - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateHlslSourceProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateHlslSourceProperty.Syntax.cs new file mode 100644 index 00000000..70c3c9e8 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateHlslSourceProperty.Syntax.cs @@ -0,0 +1,40 @@ +using ComputeSharp.D2D1.__Internals; +using ComputeSharp.SourceGeneration.Helpers; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +#pragma warning disable CS0618 + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +partial class ID2D1ShaderGenerator +{ + /// + partial class HlslSource + { + /// + /// 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 HlslSource property. + public static PropertyDeclarationSyntax GetSyntax(string hlslSource, int 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: + // + // readonly string global::ComputeSharp.D2D1.__Internals.ID2D1Shader.HlslSource => ; + return + 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))) + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); + } + } +} \ No newline at end of file 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.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.CreateOutputBufferProperties.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateOutputBufferProperties.Syntax.cs new file mode 100644 index 00000000..a7c73bf3 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateOutputBufferProperties.Syntax.cs @@ -0,0 +1,88 @@ +using System; +using ComputeSharp.D2D1.__Internals; +using ComputeSharp.D2D1.SourceGenerators.Models; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +#pragma warning disable CS0618 + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +partial class ID2D1ShaderGenerator +{ + /// + partial class OutputBuffer + { + /// + /// 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 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)); + } + } +} \ 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.CreatePixelOptionsProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreatePixelOptionsProperty.Syntax.cs new file mode 100644 index 00000000..7dac3342 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreatePixelOptionsProperty.Syntax.cs @@ -0,0 +1,51 @@ +using ComputeSharp.D2D1.__Internals; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +#pragma warning disable CS0618 + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +partial class ID2D1ShaderGenerator +{ + /// + partial class PixelOptions + { + /// + /// Creates a instance for the PixelOptions property. + /// + /// The pixel options for the shader. + /// The resulting instance for the PixelOptions property. + public static PropertyDeclarationSyntax GetSyntax(D2D1PixelOptions pixelOptions) + { + 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 ComputeSharp.D2D1.D2D1PixelOptions global::ComputeSharp.D2D1.__Internals.ID2D1Shader.PixelOptions => ; + return + PropertyDeclaration(IdentifierName("ComputeSharp.D2D1.D2D1PixelOptions"), Identifier(nameof(PixelOptions))) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) + .WithExpressionBody(ArrowExpressionClause(pixelOptionsExpression)) + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); + } + } +} \ No newline at end of file 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 a9b53915..5194aafa 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, @@ -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(); @@ -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(); @@ -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) @@ -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) => { - MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.HlslSource, item.Hierarchy.Hierarchy.Length); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, buildHlslStringMethod, canUseSkipLocalsInit: false); + PropertyDeclarationSyntax hlslStringProperty = HlslSource.GetSyntax(item.HlslSource, item.Hierarchy.Hierarchy.Length); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, hlslStringProperty, canUseSkipLocalsInit: false); - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.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 @@ -386,18 +386,23 @@ 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); + PropertyDeclarationSyntax bufferPrecisionProperty = OutputBuffer.GetBufferPrecisionSyntax(item.OutputBuffer); + CompilationUnitSyntax bufferPrecisionCompilationUnit = GetCompilationUnitFromMember(item.Hierarchy, bufferPrecisionProperty, canUseSkipLocalsInit: false); - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetOutputBuffer)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.BufferPrecision.g.cs", bufferPrecisionCompilationUnit.GetText(Encoding.UTF8)); + + 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)); }); // Get the InputDescriptions info (hierarchy and input descriptions) @@ -415,18 +420,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) => { - MethodDeclarationSyntax getPixelOptionsMethod = GetPixelOptions.GetSyntax(item.PixelOptions); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, getPixelOptionsMethod, canUseSkipLocalsInit: false); + 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 diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index ef33bcde..e3de74cf 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -64,18 +64,16 @@ 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. /// - /// 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. @@ -99,13 +97,18 @@ public interface ID2D1Shader ReadOnlyMemory ResourceTextureDescriptions { get; } /// - /// Gets the output buffer precision and depth for the shader. + /// Gets the buffer precision 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); + 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; } /// /// Loads the dispatch data for the shader. @@ -118,12 +121,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/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index c10cb2b1..60f63619 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; } /// @@ -255,7 +255,7 @@ public static class D2D1PixelShader { Unsafe.SkipInit(out T shader); - return (int)shader.GetInputCount(); + return shader.InputCount; } /// @@ -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; } /// 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(), 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(