From 01b32fd0ad8c6c5c28cce0f57be98e596f073401 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 17 Sep 2023 02:12:55 +0200 Subject: [PATCH 1/7] Add new metadata attributes --- .../Attributes/D2DEffectAuthorAttribute.cs | 27 +++++++++++++++++++ .../Attributes/D2DEffectCategoryAttribute.cs | 27 +++++++++++++++++++ .../D2DEffectDescriptionAttribute.cs | 27 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 src/ComputeSharp.D2D1/Attributes/D2DEffectAuthorAttribute.cs create mode 100644 src/ComputeSharp.D2D1/Attributes/D2DEffectCategoryAttribute.cs create mode 100644 src/ComputeSharp.D2D1/Attributes/D2DEffectDescriptionAttribute.cs diff --git a/src/ComputeSharp.D2D1/Attributes/D2DEffectAuthorAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DEffectAuthorAttribute.cs new file mode 100644 index 00000000..2b0def4a --- /dev/null +++ b/src/ComputeSharp.D2D1/Attributes/D2DEffectAuthorAttribute.cs @@ -0,0 +1,27 @@ +using System; + +namespace ComputeSharp.D2D1; + +/// +/// An attribute for a D2D1 shader indicating the author of the shader effect to create. +/// +/// +/// This only applies to effects created from . +/// +[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] +public sealed class D2DEffectAuthorAttribute : Attribute +{ + /// + /// Creates a new instance of the type with the specified arguments. + /// + /// The value for the author. + public D2DEffectAuthorAttribute(string value) + { + Value = value; + } + + /// + /// Gets the author value. + /// + public string Value { get; } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Attributes/D2DEffectCategoryAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DEffectCategoryAttribute.cs new file mode 100644 index 00000000..bb77860d --- /dev/null +++ b/src/ComputeSharp.D2D1/Attributes/D2DEffectCategoryAttribute.cs @@ -0,0 +1,27 @@ +using System; + +namespace ComputeSharp.D2D1; + +/// +/// An attribute for a D2D1 shader indicating the category of the shader effect to create. +/// +/// +/// This only applies to effects created from . +/// +[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] +public sealed class D2DEffectCategoryAttribute : Attribute +{ + /// + /// Creates a new instance of the type with the specified arguments. + /// + /// The value for the category. + public D2DEffectCategoryAttribute(string value) + { + Value = value; + } + + /// + /// Gets the category value. + /// + public string Value { get; } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Attributes/D2DEffectDescriptionAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DEffectDescriptionAttribute.cs new file mode 100644 index 00000000..7bbb2c1d --- /dev/null +++ b/src/ComputeSharp.D2D1/Attributes/D2DEffectDescriptionAttribute.cs @@ -0,0 +1,27 @@ +using System; + +namespace ComputeSharp.D2D1; + +/// +/// An attribute for a D2D1 shader indicating the description of the shader effect to create. +/// +/// +/// This only applies to effects created from . +/// +[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] +public sealed class D2DEffectDescriptionAttribute : Attribute +{ + /// + /// Creates a new instance of the type with the specified arguments. + /// + /// The value for the description. + public D2DEffectDescriptionAttribute(string value) + { + Value = value; + } + + /// + /// Gets the description value. + /// + public string Value { get; } +} \ No newline at end of file From 8edc54748f505bce889cbda35fa2c187eda75c44 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 17 Sep 2023 02:22:11 +0200 Subject: [PATCH 2/7] Update ID2D1Shader interface and generator --- ...or.CreateEffectDisplayNameProperty.Syntax.cs | 17 ++++++++++++----- ...Generator.CreateEffectDisplayNameProperty.cs | 7 +++---- .../ID2D1ShaderGenerator.cs | 4 ++-- .../Models/D2D1ShaderInfo.cs | 4 ++-- .../Interfaces/__Internals/ID2D1Shader.cs | 4 ++-- .../Shaders/Interop/D2D1PixelShaderEffect.cs | 10 +++++----- .../D2D1EffectRegistrationDataTests.cs | 2 +- .../D2D1PixelShaderEffectTests.cs | 2 +- 8 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs index 51b9de28..0dd5f13c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs @@ -16,18 +16,25 @@ partial class ID2D1ShaderGenerator /// /// Creates a instance for the EffectDisplayName property. /// - /// The input effect display name value. + /// The input effect display name value, if available. /// The resulting instance for the EffectDisplayName property. - public static PropertyDeclarationSyntax GetSyntax(string effectDisplayName) + public static PropertyDeclarationSyntax GetSyntax(string? effectDisplayName) { + // Get the expression: either a string literal, or just null + ExpressionSyntax displayNameExpression = effectDisplayName switch + { + { } => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(effectDisplayName)), + _ => LiteralExpression(SyntaxKind.NullLiteralExpression) + }; + // This code produces a method declaration as follows: // - // readonly string global::ComputeSharp.D2D1.__Internals.ID2D1Shader.EffectDisplayName => ""; + // readonly string? global::ComputeSharp.D2D1.__Internals.ID2D1Shader.EffectDisplayName => ; return - PropertyDeclaration(PredefinedType(Token(SyntaxKind.StringKeyword)), Identifier(nameof(EffectDisplayName))) + PropertyDeclaration(NullableType(PredefinedType(Token(SyntaxKind.StringKeyword))), Identifier(nameof(EffectDisplayName))) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) - .WithExpressionBody(ArrowExpressionClause(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(effectDisplayName)))) + .WithExpressionBody(ArrowExpressionClause(displayNameExpression)) .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); } } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs index 0879e724..556add8c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs @@ -1,6 +1,5 @@ using System.Diagnostics.CodeAnalysis; using ComputeSharp.D2D1.SourceGenerators.Helpers; -using ComputeSharp.SourceGeneration.Extensions; using Microsoft.CodeAnalysis; namespace ComputeSharp.D2D1.SourceGenerators; @@ -18,15 +17,15 @@ partial class ID2D1ShaderGenerator /// /// The input object currently in use. /// The for the shader type in use. - /// The resulting effect display name. - public static string GetInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol) + /// The resulting effect display name, if available. + public static string? GetInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol) { if (TryGetDefinedEffectDisplayName(compilation, structDeclarationSymbol, out string? effectDisplayName)) { return effectDisplayName; } - return structDeclarationSymbol.GetFullyQualifiedMetadataName(); + return null; } /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 31767b11..3a4812e0 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -58,7 +58,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator token.ThrowIfCancellationRequested(); // EffectDisplayName info - string effectDisplayName = EffectDisplayName.GetInfo(context.SemanticModel.Compilation, typeSymbol); + string? effectDisplayName = EffectDisplayName.GetInfo(context.SemanticModel.Compilation, typeSymbol); token.ThrowIfCancellationRequested(); @@ -149,7 +149,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator .Select(static (item, _) => item.Diagnostcs)); // Get the EffectDisplayName info (hierarchy and effect display name) - IncrementalValuesProvider<(HierarchyInfo Hierarchy, string EffectDisplayName)> effectDisplayNameInfo = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, string? EffectDisplayName)> effectDisplayNameInfo = shaderInfoWithErrors .Select(static (item, _) => (item.Hierarchy, item.EffectDisplayName)); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs index 568ca2ef..36f9836d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs @@ -8,7 +8,7 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; /// /// The hierarchy info for the shader type. /// The effect id info for the shader type. -/// The effect display name for the shader type. +/// The effect display name for the shader type, if available. /// The gathered shader dispatch data. /// The gathered input types for the shader. /// The gathered resource texture descriptions for the shader. @@ -20,7 +20,7 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; internal sealed record D2D1ShaderInfo( HierarchyInfo Hierarchy, EffectIdInfo EffectId, - string EffectDisplayName, + string? EffectDisplayName, DispatchDataInfo DispatchData, InputTypesInfo InputTypes, ResourceTextureDescriptionsInfo ResourceTextureDescriptions, diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 3a6b4ce0..716c22d0 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -20,12 +20,12 @@ public interface ID2D1Shader ref readonly Guid EffectId { get; } /// - /// Gets the display name of the D2D effect using this shader. + /// Gets the display name of the D2D effect using this shader, if specified. /// /// /// This only applies to effects created from . /// - string EffectDisplayName { get; } + string? EffectDisplayName { get; } /// /// Initializes the current shader from a buffer with the serialized dispatch data. diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index c6e2bd09..66758318 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -32,11 +32,11 @@ public static unsafe class D2D1PixelShaderEffect } /// - /// Gets the effect display name of the D2D effect using this shader. + /// Gets the effect display name of the D2D effect using this shader, if available. /// /// The type of D2D1 pixel shader to get the effect id for. - /// The effect display name of the D2D effect using shaders. - public static string GetEffectDisplayName() + /// The effect display name of the D2D effect using shaders, if available. + public static string? GetEffectDisplayName() where T : unmanaged, ID2D1PixelShader { Unsafe.SkipInit(out T shader); @@ -71,7 +71,7 @@ public static unsafe class D2D1PixelShaderEffect @@ -311,7 +311,7 @@ public static unsafe class D2D1PixelShaderEffect diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs index 712626f3..19069545 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs @@ -46,7 +46,7 @@ public partial class D2D1EffectRegistrationDataTests Assert.AreEqual(""" - + diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs index 2e62f459..8a9ea8a1 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -228,7 +228,7 @@ public partial class D2D1PixelShaderEffectTests [TestMethod] public unsafe void DefaultEffectDisplayName_MatchesValue() { - Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName(), typeof(ShaderWithDefaultEffectDisplayName).FullName); + Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName(), null); } [D2DInputCount(0)] From 2f0d5695b3b9f5a2278ae0c917f1781d13befed7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 17 Sep 2023 02:35:56 +0200 Subject: [PATCH 3/7] Unify metadata info and syntax code in generator --- .../InvalidEffectDisplayNameValueAnalyzer.cs | 2 +- .../Helpers/D2D1EffectMetadataParser.cs | 50 +++++++++++++---- ...nerator.CreateEffectDisplayNameProperty.cs | 56 ------------------- ....CreateEffectMetadataProperties.Syntax.cs} | 25 ++++++--- ...enerator.CreateEffectMetadataProperties.cs | 34 +++++++++++ .../ID2D1ShaderGenerator.cs | 6 +- 6 files changed, 96 insertions(+), 77 deletions(-) delete mode 100644 src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs rename src/ComputeSharp.D2D1.SourceGenerators/{ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs => ID2D1ShaderGenerator.CreateEffectMetadataProperties.Syntax.cs} (57%) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs index ec1cbabb..9fc3bd75 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs @@ -47,7 +47,7 @@ public sealed class InvalidEffectDisplayNameValueAnalyzer : DiagnosticAnalyzer } // Validate the effect display name - if (!D2D1EffectMetadataParser.IsValidEffectDisplayName(attributeData)) + if (!D2D1EffectMetadataParser.IsValidEffectMetadataName(attributeData)) { context.ReportDiagnostic(Diagnostic.Create( InvalidD2DEffectDisplayNameAttributeValue, diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs b/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs index 79148ef2..fcb4cba9 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs @@ -17,11 +17,11 @@ internal static class D2D1EffectMetadataParser private static readonly Regex NewLinesRegex = new("[\r\n\v]", RegexOptions.Compiled); /// - /// Checks whether a given value contains a valid effect display name. + /// Checks whether a given value contains a valid effect metadata name. /// /// The input object to inspect. - /// Whether contains a valid effect display name. - public static bool IsValidEffectDisplayName(AttributeData attributeData) + /// Whether contains a valid effect metadata name. + public static bool IsValidEffectMetadataName(AttributeData attributeData) { if (attributeData.ConstructorArguments is [{ Value: string { Length: > 0 } value }]) { @@ -36,14 +36,44 @@ internal static class D2D1EffectMetadataParser } /// - /// Tries to get the defined effect display name for a given attribute. + /// Tries to get the defined effect metadata name for a given shader type. + /// + /// The input object currently in use. + /// The input instance. + /// The fully qualified metadata name of the target attribute. + /// The resulting defined effect metadata name, if found. + /// Whether or not a defined effect metadata name could be found. + public static bool TryGetEffectMetadataName( + Compilation compilation, + INamedTypeSymbol typeSymbol, + string attributeFullyQualifiedMetadataName, + [NotNullWhen(true)] out string? effectMetadataName) + { + INamedTypeSymbol metadataAttributeSymbol = compilation.GetTypeByMetadataName(attributeFullyQualifiedMetadataName)!; + + foreach (AttributeData attributeData in typeSymbol.GetAttributes()) + { + // Check that the attribute is the one we're looking for + if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, metadataAttributeSymbol)) + { + return TryGetEffectMetadataName(attributeData, out effectMetadataName); + } + } + + effectMetadataName = default; + + return false; + } + + /// + /// Tries to get the defined effect metadata name for a given attribute. /// /// The input object to inspect. - /// The resulting defined effect display name, if found. - /// Whether or not a defined effect display name could be found. - public static bool TryGetEffectDisplayName(AttributeData attributeData, [NotNullWhen(true)] out string? effectDisplayName) + /// The resulting defined effect metadata name, if found. + /// Whether or not a defined effect metadata name could be found. + public static bool TryGetEffectMetadataName(AttributeData attributeData, [NotNullWhen(true)] out string? effectMetadataName) { - // Check that the attribute is [D2DEffectDisplayName] and with a valid parameter + // Check that the attribute has a valid parameter if (attributeData.ConstructorArguments is [{ Value: string { Length: > 0 } value }]) { // Remove new lines (the values cannot span multiple lines in XML) @@ -55,13 +85,13 @@ internal static class D2D1EffectMetadataParser // Trim the display name as well if (escapedValue.AsSpan().Trim() is { Length: > 0 } trimmedValue) { - effectDisplayName = trimmedValue.ToString(); + effectMetadataName = trimmedValue.ToString(); return true; } } - effectDisplayName = default; + effectMetadataName = default; return false; } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs deleted file mode 100644 index 556add8c..00000000 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using ComputeSharp.D2D1.SourceGenerators.Helpers; -using Microsoft.CodeAnalysis; - -namespace ComputeSharp.D2D1.SourceGenerators; - -/// -partial class ID2D1ShaderGenerator -{ - /// - /// A helper with all logic to generate the EffectDisplayName property. - /// - private static partial class EffectDisplayName - { - /// - /// Extracts the effect display name info for the current shader. - /// - /// The input object currently in use. - /// The for the shader type in use. - /// The resulting effect display name, if available. - public static string? GetInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol) - { - if (TryGetDefinedEffectDisplayName(compilation, structDeclarationSymbol, out string? effectDisplayName)) - { - return effectDisplayName; - } - - return null; - } - - /// - /// Tries to get the defined effect display name for a given shader type. - /// - /// The input object currently in use. - /// The input instance. - /// The resulting defined effect display name, if found. - /// Whether or not a defined effect display name could be found. - private static bool TryGetDefinedEffectDisplayName(Compilation compilation, INamedTypeSymbol typeSymbol, [NotNullWhen(true)] out string? effectDisplayName) - { - INamedTypeSymbol effectIdAttributeSymbol = compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DEffectDisplayNameAttribute")!; - - foreach (AttributeData attributeData in typeSymbol.GetAttributes()) - { - // Check that the attribute is [D2DEffectDisplayName] - if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, effectIdAttributeSymbol)) - { - return D2D1EffectMetadataParser.TryGetEffectDisplayName(attributeData, out effectDisplayName); - } - } - - effectDisplayName = default; - - return false; - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.Syntax.cs similarity index 57% rename from src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs rename to src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.Syntax.cs index 0dd5f13c..30fa468d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.Syntax.cs @@ -11,30 +11,41 @@ namespace ComputeSharp.D2D1.SourceGenerators; partial class ID2D1ShaderGenerator { /// - partial class EffectDisplayName + partial class EffectMetadata { /// /// Creates a instance for the EffectDisplayName property. /// /// The input effect display name value, if available. /// The resulting instance for the EffectDisplayName property. - public static PropertyDeclarationSyntax GetSyntax(string? effectDisplayName) + public static PropertyDeclarationSyntax GetEffectDisplayNameSyntax(string? effectDisplayName) + { + return GetEffectMetadataSyntax("EffectDisplayName", effectDisplayName); + } + + /// + /// Creates a instance for a given metadata property. + /// + /// The property name to generate. + /// The input effect metadata value, if available. + /// The resulting instance for the given metadata property. + private static PropertyDeclarationSyntax GetEffectMetadataSyntax(string propertyName, string? metadataValue) { // Get the expression: either a string literal, or just null - ExpressionSyntax displayNameExpression = effectDisplayName switch + ExpressionSyntax metadataValueExpression = metadataValue switch { - { } => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(effectDisplayName)), + { } => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(metadataValue)), _ => LiteralExpression(SyntaxKind.NullLiteralExpression) }; // This code produces a method declaration as follows: // - // readonly string? global::ComputeSharp.D2D1.__Internals.ID2D1Shader.EffectDisplayName => ; + // readonly string? global::ComputeSharp.D2D1.__Internals.ID2D1Shader. => ; return - PropertyDeclaration(NullableType(PredefinedType(Token(SyntaxKind.StringKeyword))), Identifier(nameof(EffectDisplayName))) + PropertyDeclaration(NullableType(PredefinedType(Token(SyntaxKind.StringKeyword))), Identifier(propertyName)) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) - .WithExpressionBody(ArrowExpressionClause(displayNameExpression)) + .WithExpressionBody(ArrowExpressionClause(metadataValueExpression)) .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); } } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs new file mode 100644 index 00000000..61efa038 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs @@ -0,0 +1,34 @@ +using ComputeSharp.D2D1.SourceGenerators.Helpers; +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +partial class ID2D1ShaderGenerator +{ + /// + /// A helper with all logic to generate the effect metadata properties. + /// + private static partial class EffectMetadata + { + /// + /// Extracts the effect display name info for the current shader. + /// + /// The input object currently in use. + /// The for the shader type in use. + /// The resulting effect display name, if available. + public static string? GetEffectDisplayNameInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol) + { + if (D2D1EffectMetadataParser.TryGetEffectMetadataName( + compilation, + structDeclarationSymbol, + "ComputeSharp.D2D1.D2DEffectDisplayNameAttribute", + out string? effectDisplayName)) + { + return effectDisplayName; + } + + return null; + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 3a4812e0..069525f1 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -58,7 +58,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator token.ThrowIfCancellationRequested(); // EffectDisplayName info - string? effectDisplayName = EffectDisplayName.GetInfo(context.SemanticModel.Compilation, typeSymbol); + string? effectDisplayName = EffectMetadata.GetEffectDisplayNameInfo(context.SemanticModel.Compilation, typeSymbol); token.ThrowIfCancellationRequested(); @@ -156,10 +156,10 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator // Generate the EffectDisplayName properties context.RegisterSourceOutput(effectDisplayNameInfo, static (context, item) => { - PropertyDeclarationSyntax effectDisplayNameProperty = EffectDisplayName.GetSyntax(item.EffectDisplayName); + PropertyDeclarationSyntax effectDisplayNameProperty = EffectMetadata.GetEffectDisplayNameSyntax(item.EffectDisplayName); CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, effectDisplayNameProperty, canUseSkipLocalsInit: false); - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(EffectDisplayName)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.EffectDisplayName.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); // Get the EffectId info (hierarchy and effect id info) From dd8f2219056ebdbb78c93ffa3952a0cbbdf9a539 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 17 Sep 2023 13:25:04 +0200 Subject: [PATCH 4/7] Add new effect metadata properties and generation --- ...r.CreateEffectMetadataProperties.Syntax.cs | 30 +++++++ ...enerator.CreateEffectMetadataProperties.cs | 60 ++++++++++++++ .../ID2D1ShaderGenerator.cs | 82 ++++++++++++++++--- .../Models/D2D1ShaderInfo.cs | 6 ++ .../Interfaces/__Internals/ID2D1Shader.cs | 24 ++++++ 5 files changed, 191 insertions(+), 11 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.Syntax.cs index 30fa468d..fee46c11 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.Syntax.cs @@ -23,6 +23,36 @@ partial class ID2D1ShaderGenerator return GetEffectMetadataSyntax("EffectDisplayName", effectDisplayName); } + /// + /// Creates a instance for the EffectDescription property. + /// + /// The input effect description value, if available. + /// The resulting instance for the EffectDescription property. + public static PropertyDeclarationSyntax GetEffectDescriptionSyntax(string? effectDescription) + { + return GetEffectMetadataSyntax("EffectDescription", effectDescription); + } + + /// + /// Creates a instance for the EffectCategory property. + /// + /// The input effect category value, if available. + /// The resulting instance for the EffectCategory property. + public static PropertyDeclarationSyntax GetEffectCategorySyntax(string? effectCategory) + { + return GetEffectMetadataSyntax("EffectCategory", effectCategory); + } + + /// + /// Creates a instance for the EffectAuthor property. + /// + /// The input effect author value, if available. + /// The resulting instance for the EffectAuthor property. + public static PropertyDeclarationSyntax GetEffectAuthorSyntax(string? effectAuthor) + { + return GetEffectMetadataSyntax("EffectAuthor", effectAuthor); + } + /// /// Creates a instance for a given metadata property. /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs index 61efa038..9e574e31 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs @@ -30,5 +30,65 @@ partial class ID2D1ShaderGenerator return null; } + + /// + /// Extracts the effect description info for the current shader. + /// + /// The input object currently in use. + /// The for the shader type in use. + /// The resulting effect description, if available. + public static string? GetEffectDescriptionInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol) + { + if (D2D1EffectMetadataParser.TryGetEffectMetadataName( + compilation, + structDeclarationSymbol, + "ComputeSharp.D2D1.D2DEffectDescriptionAttribute", + out string? effectDescription)) + { + return effectDescription; + } + + return null; + } + + /// + /// Extracts the effect category info for the current shader. + /// + /// The input object currently in use. + /// The for the shader type in use. + /// The resulting effect category, if available. + public static string? GetEffectCategoryInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol) + { + if (D2D1EffectMetadataParser.TryGetEffectMetadataName( + compilation, + structDeclarationSymbol, + "ComputeSharp.D2D1.D2DEffectCategoryAttribute", + out string? effectCategory)) + { + return effectCategory; + } + + return null; + } + + /// + /// Extracts the effect author info for the current shader. + /// + /// The input object currently in use. + /// The for the shader type in use. + /// The resulting effect author, if available. + public static string? GetEffectAuthorInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol) + { + if (D2D1EffectMetadataParser.TryGetEffectMetadataName( + compilation, + structDeclarationSymbol, + "ComputeSharp.D2D1.D2DEffectAuthorAttribute", + out string? effectAuthor)) + { + return effectAuthor; + } + + return null; + } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 069525f1..d9f603b0 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -50,8 +50,6 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator return default; } - using ImmutableArrayBuilder diagnostics = ImmutableArrayBuilder.Rent(); - // EffectId info ImmutableArray effectId = EffectId.GetInfo(context.SemanticModel.Compilation, typeSymbol); @@ -62,6 +60,23 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator token.ThrowIfCancellationRequested(); + // EffectDescription info + string? effectDescription = EffectMetadata.GetEffectDescriptionInfo(context.SemanticModel.Compilation, typeSymbol); + + token.ThrowIfCancellationRequested(); + + // EffectCategory info + string? effectCategory = EffectMetadata.GetEffectCategoryInfo(context.SemanticModel.Compilation, typeSymbol); + + token.ThrowIfCancellationRequested(); + + // EffectAuthor info + string? effectAuthor = EffectMetadata.GetEffectAuthorInfo(context.SemanticModel.Compilation, typeSymbol); + + token.ThrowIfCancellationRequested(); + + using ImmutableArrayBuilder diagnostics = ImmutableArrayBuilder.Rent(); + // LoadDispatchData() info ImmutableArray fieldInfos = LoadDispatchData.GetInfo( diagnostics, @@ -127,6 +142,9 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator Hierarchy: HierarchyInfo.From(typeSymbol), EffectId: new EffectIdInfo(effectId), EffectDisplayName: effectDisplayName, + EffectDescription: effectDescription, + EffectCategory: effectCategory, + EffectAuthor: effectAuthor, DispatchData: new DispatchDataInfo(fieldInfos, constantBufferSizeInBytes), InputTypes: new InputTypesInfo(inputTypes), ResourceTextureDescriptions: new ResourceTextureDescriptionsInfo(resourceTextureDescriptions), @@ -148,6 +166,21 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator shaderInfoWithErrors .Select(static (item, _) => item.Diagnostcs)); + // Get the EffectId info (hierarchy and effect id info) + IncrementalValuesProvider<(HierarchyInfo Hierarchy, EffectIdInfo EffectId)> effectIdInfo = + shaderInfoWithErrors + .Select(static (item, _) => (item.Hierarchy, item.EffectId)); + + // Generate the EffectId properties + context.RegisterSourceOutput(effectIdInfo, static (context, item) => + { + PropertyDeclarationSyntax effectDisplayNameProperty = EffectId.GetSyntax(item.EffectId, out Func fixup); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, effectDisplayNameProperty, canUseSkipLocalsInit: false); + SourceText text = fixup(compilationUnit); + + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(EffectId)}.g.cs", text); + }); + // Get the EffectDisplayName info (hierarchy and effect display name) IncrementalValuesProvider<(HierarchyInfo Hierarchy, string? EffectDisplayName)> effectDisplayNameInfo = shaderInfoWithErrors @@ -162,19 +195,46 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.EffectDisplayName.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Get the EffectId info (hierarchy and effect id info) - IncrementalValuesProvider<(HierarchyInfo Hierarchy, EffectIdInfo EffectId)> effectIdInfo = + // Get the EffectDescription info (hierarchy and effect description) + IncrementalValuesProvider<(HierarchyInfo Hierarchy, string? EffectDescription)> effectDescriptionInfo = shaderInfoWithErrors - .Select(static (item, _) => (item.Hierarchy, item.EffectId)); + .Select(static (item, _) => (item.Hierarchy, item.EffectDescription)); - // Generate the EffectId properties - context.RegisterSourceOutput(effectIdInfo, static (context, item) => + // Generate the EffectDescription properties + context.RegisterSourceOutput(effectDescriptionInfo, static (context, item) => { - PropertyDeclarationSyntax effectDisplayNameProperty = EffectId.GetSyntax(item.EffectId, out Func fixup); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, effectDisplayNameProperty, canUseSkipLocalsInit: false); - SourceText text = fixup(compilationUnit); + PropertyDeclarationSyntax effectDescriptionProperty = EffectMetadata.GetEffectDescriptionSyntax(item.EffectDescription); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, effectDescriptionProperty, canUseSkipLocalsInit: false); - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(EffectId)}.g.cs", text); + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.EffectDescription.g.cs", compilationUnit.GetText(Encoding.UTF8)); + }); + + // Get the EffectCategory info (hierarchy and effect description) + IncrementalValuesProvider<(HierarchyInfo Hierarchy, string? EffectCategory)> effectCategoryInfo = + shaderInfoWithErrors + .Select(static (item, _) => (item.Hierarchy, item.EffectCategory)); + + // Generate the EffectCategory properties + context.RegisterSourceOutput(effectCategoryInfo, static (context, item) => + { + PropertyDeclarationSyntax effectCategoryProperty = EffectMetadata.GetEffectCategorySyntax(item.EffectCategory); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, effectCategoryProperty, canUseSkipLocalsInit: false); + + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.EffectCategory.g.cs", compilationUnit.GetText(Encoding.UTF8)); + }); + + // Get the EffectAuthor info (hierarchy and effect description) + IncrementalValuesProvider<(HierarchyInfo Hierarchy, string? EffectAuthor)> effectAuthorInfo = + shaderInfoWithErrors + .Select(static (item, _) => (item.Hierarchy, item.EffectAuthor)); + + // Generate the EffectAuthor properties + context.RegisterSourceOutput(effectAuthorInfo, static (context, item) => + { + PropertyDeclarationSyntax effectCategoryProperty = EffectMetadata.GetEffectAuthorSyntax(item.EffectAuthor); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, effectCategoryProperty, canUseSkipLocalsInit: false); + + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.EffectAuthor.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); // Get the GetInputCount() info (hierarchy and input count) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs index 36f9836d..5cd2c211 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs @@ -9,6 +9,9 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; /// The hierarchy info for the shader type. /// The effect id info for the shader type. /// The effect display name for the shader type, if available. +/// The effect description for the shader type, if available. +/// The effect category for the shader type, if available. +/// The effect author for the shader type, if available. /// The gathered shader dispatch data. /// The gathered input types for the shader. /// The gathered resource texture descriptions for the shader. @@ -21,6 +24,9 @@ internal sealed record D2D1ShaderInfo( HierarchyInfo Hierarchy, EffectIdInfo EffectId, string? EffectDisplayName, + string? EffectDescription, + string? EffectCategory, + string? EffectAuthor, DispatchDataInfo DispatchData, InputTypesInfo InputTypes, ResourceTextureDescriptionsInfo ResourceTextureDescriptions, diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 716c22d0..07f99fb6 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -27,6 +27,30 @@ public interface ID2D1Shader /// string? EffectDisplayName { get; } + /// + /// Gets the description of the D2D effect using this shader, if specified. + /// + /// + /// This only applies to effects created from . + /// + string? EffectDescription { get; } + + /// + /// Gets the category of the D2D effect using this shader, if specified. + /// + /// + /// This only applies to effects created from . + /// + string? EffectCategory { get; } + + /// + /// Gets the author of the D2D effect using this shader, if specified. + /// + /// + /// This only applies to effects created from . + /// + string? EffectAuthor { get; } + /// /// Initializes the current shader from a buffer with the serialized dispatch data. /// From e5daf558ecf3bea02d43a3c66d7b1ce81d9c9b9b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 17 Sep 2023 13:54:37 +0200 Subject: [PATCH 5/7] Enable assembly defaults, update analyzer --- .../AnalyzerReleases.Shipped.md | 3 + .../InvalidEffectDisplayNameValueAnalyzer.cs | 62 ------------- .../InvalidEffectMetadataValueAnalyzer.cs | 88 +++++++++++++++++++ .../Diagnostics/DiagnosticDescriptors.cs | 50 ++++++++++- .../Helpers/D2D1EffectMetadataParser.cs | 19 ++-- ...enerator.CreateEffectMetadataProperties.cs | 4 + .../Attributes/D2DEffectAuthorAttribute.cs | 2 +- .../Attributes/D2DEffectCategoryAttribute.cs | 2 +- .../Extensions/CompilationExtensions.cs | 34 +++++++ .../Extensions/ISymbolExtensions.cs | 24 +++++ 10 files changed, 217 insertions(+), 71 deletions(-) delete mode 100644 src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectMetadataValueAnalyzer.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md index b3e8a176..60e42424 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.SourceGenerators/AnalyzerReleases.Shipped.md @@ -67,3 +67,6 @@ CMPSD2D0057 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github CMPSD2D0058 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0059 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2D0060 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0061 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0062 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2D0063 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs deleted file mode 100644 index 9fc3bd75..00000000 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System.Collections.Immutable; -using ComputeSharp.D2D1.SourceGenerators.Helpers; -using ComputeSharp.SourceGeneration.Extensions; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Diagnostics; -using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; - -namespace ComputeSharp.D2D1.SourceGenerators; - -/// -/// A diagnostic analyzer that generates an error whenever [D2DEffectName] is used with an invalid value. -/// -[DiagnosticAnalyzer(LanguageNames.CSharp)] -public sealed class InvalidEffectDisplayNameValueAnalyzer : DiagnosticAnalyzer -{ - /// - public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DEffectDisplayNameAttributeValue); - - /// - public override void Initialize(AnalysisContext context) - { - context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); - context.EnableConcurrentExecution(); - - context.RegisterCompilationStartAction(static context => - { - // Get the [D2DEffectDisplayName] symbol - if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DEffectDisplayNameAttribute") is not { } d2DEffectIdAttributeSymbol) - { - return; - } - - context.RegisterSymbolAction(context => - { - // Only struct types are possible targets - if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol) - { - return; - } - - foreach (AttributeData attributeData in typeSymbol.GetAttributes()) - { - // Look for the [D2DEffectDisplayName] use - if (!SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, d2DEffectIdAttributeSymbol)) - { - continue; - } - - // Validate the effect display name - if (!D2D1EffectMetadataParser.IsValidEffectMetadataName(attributeData)) - { - context.ReportDiagnostic(Diagnostic.Create( - InvalidD2DEffectDisplayNameAttributeValue, - attributeData.GetLocation())); - } - - return; - } - }, SymbolKind.NamedType); - }); - } -} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectMetadataValueAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectMetadataValueAnalyzer.cs new file mode 100644 index 00000000..085603ed --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectMetadataValueAnalyzer.cs @@ -0,0 +1,88 @@ +using System.Collections.Generic; +using System.Collections.Immutable; +using ComputeSharp.D2D1.SourceGenerators.Helpers; +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +/// A diagnostic analyzer that generates an error whenever any shader metadata value attributes are used with an invalid value. +/// +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class InvalidEffectMetadataValueAnalyzer : DiagnosticAnalyzer +{ + /// + /// The set of type names for all metadata attributes. + /// + private static readonly ImmutableArray MetadataAttributeTypeNames = ImmutableArray.Create( + "ComputeSharp.D2D1.D2DEffectDisplayNameAttribute", + "ComputeSharp.D2D1.D2DEffectDescriptionAttribute", + "ComputeSharp.D2D1.D2DEffectCategoryAttribute", + "ComputeSharp.D2D1.D2DEffectAuthorAttribute"); + + /// + /// The mapping of diagnostics to their attribute type names. + /// + private static readonly ImmutableDictionary AttributeTypeNameToDiagnosticMapping = ImmutableDictionary.CreateRange(new[] + { + new KeyValuePair("D2DEffectDisplayNameAttribute", InvalidD2DEffectDisplayNameAttributeValue), + new KeyValuePair("D2DEffectDescriptionAttribute", InvalidD2DEffectDescriptionAttributeValue), + new KeyValuePair("D2DEffectCategoryAttribute", InvalidD2DEffectCategoryAttributeValue), + new KeyValuePair("D2DEffectAuthorAttribute", InvalidD2DEffectAuthorAttributeValue) + }); + + /// + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create( + InvalidD2DEffectDisplayNameAttributeValue, + InvalidD2DEffectDescriptionAttributeValue, + InvalidD2DEffectCategoryAttributeValue, + InvalidD2DEffectAuthorAttributeValue); + + /// + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); + context.EnableConcurrentExecution(); + + context.RegisterCompilationStartAction(static context => + { + // Get the metadata attribute symbols + if (!context.Compilation.TryBuildNamedTypeSymbolSet(MetadataAttributeTypeNames, out ImmutableHashSet? typeSymbols)) + { + return; + } + + context.RegisterSymbolAction(context => + { + // Only struct types and assemblies are possible targets + if (context.Symbol is not (INamedTypeSymbol { TypeKind: TypeKind.Struct } or IAssemblySymbol)) + { + return; + } + + foreach (AttributeData attributeData in context.Symbol.GetAttributes()) + { + // Only check attributes we care about + if (attributeData.AttributeClass is not INamedTypeSymbol attributeSymbol || + !typeSymbols.Contains(attributeSymbol)) + { + continue; + } + + // Validate the effect display name + if (!D2D1EffectMetadataParser.IsValidEffectMetadataName(attributeData)) + { + context.ReportDiagnostic(Diagnostic.Create( + AttributeTypeNameToDiagnosticMapping[attributeSymbol.MetadataName], + attributeData.GetLocation())); + } + + return; + } + }, SymbolKind.NamedType | SymbolKind.Assembly); + }); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 3f1debd2..0cb58f8e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -877,7 +877,7 @@ partial class DiagnosticDescriptors /// /// Gets a for when the [D2DEffectDisplayName] attribute is being used with an invalid value. /// - /// Format: "The [D2DEffectDisplayName] attribute is being used with an invalid value (the input text must be a valid GUID)". + /// Format: "The [D2DEffectDisplayName] attribute is being used with an invalid value". /// /// public static readonly DiagnosticDescriptor InvalidD2DEffectDisplayNameAttributeValue = new DiagnosticDescriptor( @@ -889,4 +889,52 @@ partial class DiagnosticDescriptors isEnabledByDefault: true, description: "The [D2DEffectDisplayName] attribute must contain valid text.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when the [D2DEffectDescription] attribute is being used with an invalid value. + /// + /// Format: "The [D2DEffectDescription] attribute is being used with an invalid value". + /// + /// + public static readonly DiagnosticDescriptor InvalidD2DEffectDescriptionAttributeValue = new DiagnosticDescriptor( + id: "CMPSD2D0061", + title: "Invalid [D2DEffectDescription] attribute value", + messageFormat: "The [D2DEffectDescription] attribute is being used with an invalid value", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The [D2DEffectDescription] attribute must contain valid text.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when the [D2DEffectCategory] attribute is being used with an invalid value. + /// + /// Format: "The [D2DEffectDescription] attribute is being used with an invalid value". + /// + /// + public static readonly DiagnosticDescriptor InvalidD2DEffectCategoryAttributeValue = new DiagnosticDescriptor( + id: "CMPSD2D0062", + title: "Invalid [D2DEffectCategory] attribute value", + messageFormat: "The [D2DEffectCategory] attribute is being used with an invalid value", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The [D2DEffectCategory] attribute must contain valid text.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for when the [D2DEffectAuthor] attribute is being used with an invalid value. + /// + /// Format: "The [D2DEffectAuthor] attribute is being used with an invalid value". + /// + /// + public static readonly DiagnosticDescriptor InvalidD2DEffectAuthorAttributeValue = new DiagnosticDescriptor( + id: "CMPSD2D0063", + title: "Invalid [D2DEffectAuthor] attribute value", + messageFormat: "The [D2DEffectAuthor] attribute is being used with an invalid value", + category: "ComputeSharp.D2D1.Shaders", + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The [D2DEffectAuthor] attribute must contain valid text.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs b/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs index fcb4cba9..018abee1 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs @@ -2,6 +2,7 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Security; using System.Text.RegularExpressions; +using ComputeSharp.SourceGeneration.Extensions; using Microsoft.CodeAnalysis; namespace ComputeSharp.D2D1.SourceGenerators.Helpers; @@ -41,23 +42,29 @@ internal static class D2D1EffectMetadataParser /// The input object currently in use. /// The input instance. /// The fully qualified metadata name of the target attribute. + /// Whether the attribute can also be on the assembly. /// The resulting defined effect metadata name, if found. /// Whether or not a defined effect metadata name could be found. public static bool TryGetEffectMetadataName( Compilation compilation, INamedTypeSymbol typeSymbol, string attributeFullyQualifiedMetadataName, + bool isAssemblyLevelAttributeSupported, [NotNullWhen(true)] out string? effectMetadataName) { INamedTypeSymbol metadataAttributeSymbol = compilation.GetTypeByMetadataName(attributeFullyQualifiedMetadataName)!; - foreach (AttributeData attributeData in typeSymbol.GetAttributes()) + // Always check the attributes on the type first + if (typeSymbol.TryGetAttributeWithType(metadataAttributeSymbol, out AttributeData? attributeData)) { - // Check that the attribute is the one we're looking for - if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, metadataAttributeSymbol)) - { - return TryGetEffectMetadataName(attributeData, out effectMetadataName); - } + return TryGetEffectMetadataName(attributeData, out effectMetadataName); + } + + // Check assembly-level attributes next, if needed + if (isAssemblyLevelAttributeSupported && + typeSymbol.ContainingAssembly!.TryGetAttributeWithType(metadataAttributeSymbol, out attributeData)) + { + return TryGetEffectMetadataName(attributeData, out effectMetadataName); } effectMetadataName = default; diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs index 9e574e31..d2158878 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs @@ -23,6 +23,7 @@ partial class ID2D1ShaderGenerator compilation, structDeclarationSymbol, "ComputeSharp.D2D1.D2DEffectDisplayNameAttribute", + isAssemblyLevelAttributeSupported: false, out string? effectDisplayName)) { return effectDisplayName; @@ -43,6 +44,7 @@ partial class ID2D1ShaderGenerator compilation, structDeclarationSymbol, "ComputeSharp.D2D1.D2DEffectDescriptionAttribute", + isAssemblyLevelAttributeSupported: false, out string? effectDescription)) { return effectDescription; @@ -63,6 +65,7 @@ partial class ID2D1ShaderGenerator compilation, structDeclarationSymbol, "ComputeSharp.D2D1.D2DEffectCategoryAttribute", + isAssemblyLevelAttributeSupported: true, out string? effectCategory)) { return effectCategory; @@ -83,6 +86,7 @@ partial class ID2D1ShaderGenerator compilation, structDeclarationSymbol, "ComputeSharp.D2D1.D2DEffectAuthorAttribute", + isAssemblyLevelAttributeSupported: true, out string? effectAuthor)) { return effectAuthor; diff --git a/src/ComputeSharp.D2D1/Attributes/D2DEffectAuthorAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DEffectAuthorAttribute.cs index 2b0def4a..8e589eea 100644 --- a/src/ComputeSharp.D2D1/Attributes/D2DEffectAuthorAttribute.cs +++ b/src/ComputeSharp.D2D1/Attributes/D2DEffectAuthorAttribute.cs @@ -8,7 +8,7 @@ namespace ComputeSharp.D2D1; /// /// This only applies to effects created from . /// -[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] +[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = false)] public sealed class D2DEffectAuthorAttribute : Attribute { /// diff --git a/src/ComputeSharp.D2D1/Attributes/D2DEffectCategoryAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DEffectCategoryAttribute.cs index bb77860d..53708f6c 100644 --- a/src/ComputeSharp.D2D1/Attributes/D2DEffectCategoryAttribute.cs +++ b/src/ComputeSharp.D2D1/Attributes/D2DEffectCategoryAttribute.cs @@ -8,7 +8,7 @@ namespace ComputeSharp.D2D1; /// /// This only applies to effects created from . /// -[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] +[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = false)] public sealed class D2DEffectCategoryAttribute : Attribute { /// diff --git a/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs index 01f78575..b937a26b 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs @@ -2,6 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Diagnostics.CodeAnalysis; using Microsoft.CodeAnalysis; namespace ComputeSharp.SourceGeneration.Extensions; @@ -11,6 +14,37 @@ namespace ComputeSharp.SourceGeneration.Extensions; /// internal static class CompilationExtensions { + /// + /// Tries to build a set of instances form the input mapping of names. + /// + /// The to consider for analysis. + /// The input fully qualified type names. + /// The resulting mapping of resolved instances. + /// Whether all requested instances could be resolved. + public static bool TryBuildNamedTypeSymbolSet( + this Compilation compilation, + IEnumerable fullyQualifiedTypeNames, + [NotNullWhen(true)] out ImmutableHashSet? typeSymbols) + { + ImmutableHashSet.Builder builder = ImmutableHashSet.CreateBuilder(SymbolEqualityComparer.Default); + + foreach (string fullyQualifiedTypeName in fullyQualifiedTypeNames) + { + if (compilation.GetTypeByMetadataName(fullyQualifiedTypeName) is not INamedTypeSymbol typeSymbol) + { + typeSymbols = null; + + return false; + } + + _ = builder.Add(typeSymbol); + } + + typeSymbols = builder.ToImmutable(); + + return true; + } + /// /// /// Checks whether or not a type with a specified metadata name is accessible from a given instance. diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs index fa276e2b..a69c739e 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -27,6 +27,30 @@ internal static class ISymbolExtensions return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat); } + /// + /// Tries to get an attribute with the specified type. + /// + /// The input instance to check. + /// The instance for the attribute type to look for. + /// The resulting attribute, if it was found. + /// Whether or not has an attribute with the specified name. + public static bool TryGetAttributeWithType(this ISymbol symbol, ITypeSymbol typeSymbol, [NotNullWhen(true)] out AttributeData? attributeData) + { + foreach (AttributeData attribute in symbol.GetAttributes()) + { + if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, typeSymbol)) + { + attributeData = attribute; + + return true; + } + } + + attributeData = null; + + return false; + } + /// /// Tries to get an attribute with the specified fully qualified metadata name. /// From 308eab5fbcdee58aafd8316c167e6e623cdf1d6f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 20 Sep 2023 14:12:35 +0200 Subject: [PATCH 6/7] Add APIs to get other metadata, update effect XML --- .../Shaders/Interop/D2D1PixelShaderEffect.cs | 77 +++++++++++++++++-- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index 66758318..add62e86 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -34,7 +34,7 @@ public static unsafe class D2D1PixelShaderEffect /// /// Gets the effect display name of the D2D effect using this shader, if available. /// - /// The type of D2D1 pixel shader to get the effect id for. + /// The type of D2D1 pixel shader to get the effect display name for. /// The effect display name of the D2D effect using shaders, if available. public static string? GetEffectDisplayName() where T : unmanaged, ID2D1PixelShader @@ -44,6 +44,45 @@ public static unsafe class D2D1PixelShaderEffect return shader.EffectDisplayName; } + /// + /// Gets the effect description of the D2D effect using this shader, if available. + /// + /// The type of D2D1 pixel shader to get the effect description for. + /// The effect description of the D2D effect using shaders, if available. + public static string? GetEffectDescription() + where T : unmanaged, ID2D1PixelShader + { + Unsafe.SkipInit(out T shader); + + return shader.EffectDescription; + } + + /// + /// Gets the effect category of the D2D effect using this shader, if available. + /// + /// The type of D2D1 pixel shader to get the effect category for. + /// The effect category of the D2D effect using shaders, if available. + public static string? GetEffectCategory() + where T : unmanaged, ID2D1PixelShader + { + Unsafe.SkipInit(out T shader); + + return shader.EffectCategory; + } + + /// + /// Gets the effect author of the D2D effect using this shader, if available. + /// + /// The type of D2D1 pixel shader to get the effect author for. + /// The effect author of the D2D effect using shaders, if available. + public static string? GetEffectAuthor() + where T : unmanaged, ID2D1PixelShader + { + Unsafe.SkipInit(out T shader); + + return shader.EffectAuthor; + } + /// /// Registers an effect from an input D2D1 pixel shader, by calling ID2D1Factory1::RegisterEffectFromString. /// @@ -74,9 +113,21 @@ public static unsafe class D2D1PixelShaderEffect writer.WriteRaw(GetEffectDisplayName().AsSpan()); writer.WriteRaw(""" '/> - - - + + + """); @@ -314,9 +365,21 @@ public static unsafe class D2D1PixelShaderEffect writer.WriteAsUtf8(GetEffectDisplayName() ?? ""); writer.WriteRaw(""" '/> - - - + + + """u8); From 4407d78c91e6d9407a328c2aff6cf342dfa79aa4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 20 Sep 2023 14:12:43 +0200 Subject: [PATCH 7/7] Update unit tests for new custom metadata --- .../D2D1EffectRegistrationDataTests.cs | 100 +++++++++++++++++- .../D2D1PixelShaderEffectTests.cs | 40 ++++--- 2 files changed, 120 insertions(+), 20 deletions(-) diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs index 19069545..ef833251 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs @@ -14,6 +14,7 @@ public partial class D2D1EffectRegistrationDataTests ReadOnlyMemory blob = D2D1PixelShaderEffect.GetRegistrationBlob(out Guid effectId); D2D1EffectRegistrationData.V1 data = D2D1EffectRegistrationData.V1.Load(blob); + Assert.AreEqual(Guid.Parse("8AA3BDF3-5EC8-34D9-CFBC-97A2B4068FFD"), effectId); Assert.AreEqual(effectId, data.ClassId); Assert.AreEqual(2, data.NumberOfInputs); Assert.IsTrue(data.EffectFactory is not null); @@ -47,9 +48,9 @@ public partial class D2D1EffectRegistrationDataTests - - - + + + @@ -124,4 +125,97 @@ public partial class D2D1EffectRegistrationDataTests return this.a; } } + + [TestMethod] + public unsafe void EffectRegistrationData_WithCustomMetadata_Validate() + { + ReadOnlyMemory blob = D2D1PixelShaderEffect.GetRegistrationBlob(out Guid effectId); + D2D1EffectRegistrationData.V1 data = D2D1EffectRegistrationData.V1.Load(blob); + + Assert.AreEqual(Guid.Parse("ABF58390-91D2-4F58-AA2B-22D0F6AC069C"), effectId); + Assert.AreEqual(""" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + """, data.PropertyXml); + } + + [D2DInputCount(2)] + [D2DEffectId("ABF58390-91D2-4F58-AA2B-22D0F6AC069C")] + [D2DEffectDisplayName("EffectWithCustomMetadata")] + [D2DEffectDescription("A test effect with some custom metadata")] + [D2DEffectCategory("Test effects")] + [D2DEffectAuthor("Bob Ross")] + [AutoConstructor] + private readonly partial struct TestRegistrationBlobWithCustomMetadataShader : ID2D1PixelShader + { + private readonly float a; + + public float4 Execute() + { + return this.a; + } + } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs index 8a9ea8a1..7dd2fe16 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -226,9 +226,12 @@ public partial class D2D1PixelShaderEffectTests } [TestMethod] - public unsafe void DefaultEffectDisplayName_MatchesValue() + public unsafe void DefaultEffectMetadata_MatchesValue() { Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName(), null); + Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName(), null); + Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName(), null); + Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName(), null); } [D2DInputCount(0)] @@ -241,16 +244,17 @@ public partial class D2D1PixelShaderEffectTests } [TestMethod] - public unsafe void ExplicitEffectDisplayName_MatchesValue() + public unsafe void ExplicitEffectMetadata1_MatchesValue() { - Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName(), "Fancy blur"); - Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName(), "Fancy"<"); - Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName(), "FancyBlurEffect"); + Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName(), "Fancy blur"); + Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDescription(), null); + Assert.AreEqual(D2D1PixelShaderEffect.GetEffectCategory(), null); + Assert.AreEqual(D2D1PixelShaderEffect.GetEffectAuthor(), null); } [D2DInputCount(0)] [D2DEffectDisplayName("Fancy blur")] - private partial struct ShaderWithExplicitEffectDisplayName : ID2D1PixelShader + private partial struct ShaderWithExplicitEffectDisplayName1 : ID2D1PixelShader { public float4 Execute() { @@ -258,8 +262,20 @@ public partial class D2D1PixelShaderEffectTests } } + [TestMethod] + public unsafe void ExplicitEffectMetadata2_MatchesValue() + { + Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName(), "Fancy"<"); + Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDescription(), "A test effect with some custom metadata"); + Assert.AreEqual(D2D1PixelShaderEffect.GetEffectCategory(), "Test effects!"); + Assert.AreEqual(D2D1PixelShaderEffect.GetEffectAuthor(), "Bob Ross"); + } + [D2DInputCount(0)] - [D2DEffectDisplayName("Fancy\"<")] + [D2DEffectDisplayName("F\r\na\nncy\"<")] + [D2DEffectDescription("A test effect with \nsome custom metadata")] + [D2DEffectCategory("Test effects!")] + [D2DEffectAuthor("Bob \r\nRoss")] private partial struct ShaderWithExplicitEffectDisplayName2 : ID2D1PixelShader { public float4 Execute() @@ -267,14 +283,4 @@ public partial class D2D1PixelShaderEffectTests return 0; } } - - [D2DInputCount(0)] - [D2DEffectDisplayName("Fancy\r\nBlur\nEffect")] - private partial struct ShaderWithExplicitEffectDisplayName3 : ID2D1PixelShader - { - public float4 Execute() - { - return 0; - } - } } \ No newline at end of file