From a71c4db7dd21c137fce443eb70c40a505e7c2651 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 23 Sep 2023 16:46:06 +0200 Subject: [PATCH] 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. ///