From 70b9aa9588a5f1f3bc2e75b1b14a3daba75d20a2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 24 Sep 2023 16:57:03 +0200 Subject: [PATCH] Add and generate ID2D1Shader.ShaderProfile --- ...nerator.CreateLoadBytecodeMethod.Syntax.cs | 22 +++++++++++++++++++ .../ID2D1ShaderGenerator.cs | 9 ++++++-- .../Interfaces/__Internals/ID2D1Shader.cs | 10 +++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs index 3dd66174..ac0ca80d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs @@ -21,6 +21,28 @@ partial class ID2D1ShaderGenerator /// partial class LoadBytecode { + /// + /// Creates a instance for the ShaderProfile property. + /// + /// The input bytecode info. + /// The resulting instance for the ShaderProfile property. + public static PropertyDeclarationSyntax GetShaderProfileSyntax(EmbeddedBytecodeInfo bytecodeInfo) + { + // This code produces a method declaration as follows: + // + // readonly ComputeSharp.D2D1.D2D1ShaderProfile global::ComputeSharp.D2D1.__Internals.ID2D1Shader.ShaderProfile => ; + return + PropertyDeclaration(IdentifierName("ComputeSharp.D2D1.D2D1ShaderProfile"), Identifier(nameof(ID2D1Shader.ShaderProfile))) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) + .WithExpressionBody(ArrowExpressionClause( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("global::ComputeSharp.D2D1.D2D1ShaderProfile"), + IdentifierName(bytecodeInfo.ShaderProfile.ToString())))) + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); + } + /// /// Creates a instance for the TryGetBytecode method. /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 9957023c..3368e1d1 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -363,10 +363,15 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.RegisterSourceOutput(embeddedBytecode, static (context, item) => { MethodDeclarationSyntax loadBytecodeMethod = LoadBytecode.GetSyntax(item.BytecodeInfo, out Func fixup); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, loadBytecodeMethod, skipLocalsInit: false); - SourceText text = fixup(compilationUnit); + CompilationUnitSyntax loadBytecodeCompilationUnit = GetCompilationUnitFromMember(item.Hierarchy, loadBytecodeMethod, skipLocalsInit: false); + SourceText text = fixup(loadBytecodeCompilationUnit); context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadBytecode)}.g.cs", text); + + PropertyDeclarationSyntax shaderProfileProperty = LoadBytecode.GetShaderProfileSyntax(item.BytecodeInfo); + CompilationUnitSyntax shaderProfileCompilationUnit = GetCompilationUnitFromMember(item.Hierarchy, shaderProfileProperty, skipLocalsInit: false); + + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.ShaderProfile.g.cs", shaderProfileCompilationUnit.GetText(Encoding.UTF8)); }); // Get the output buffer info (hierarchy and output buffers) diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index e3de74cf..be881457 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -127,6 +127,16 @@ public interface ID2D1Shader [Obsolete("This method is not intended to be called directly by user code")] string HlslSource { get; } + /// + /// Gets the shader profile for the current shader. + /// + /// + /// This shader profile is either explicitly set by users, or the default one for D2D1 shaders. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be used directly by user code")] + D2D1ShaderProfile ShaderProfile { get; } + /// /// Loads the bytecode for the current shader. ///