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