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 ec1cbabb..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.IsValidEffectDisplayName(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 79148ef2..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; @@ -17,11 +18,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 +37,50 @@ 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. + /// 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)!; + + // Always check the attributes on the type first + if (typeSymbol.TryGetAttributeWithType(metadataAttributeSymbol, out AttributeData? attributeData)) + { + 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; + + 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 +92,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.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs deleted file mode 100644 index 51b9de28..00000000 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs +++ /dev/null @@ -1,34 +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 EffectDisplayName - { - /// - /// Creates a instance for the EffectDisplayName property. - /// - /// The input effect display name value. - /// The resulting instance for the EffectDisplayName property. - public static PropertyDeclarationSyntax GetSyntax(string effectDisplayName) - { - // This code produces a method declaration as follows: - // - // readonly string global::ComputeSharp.D2D1.__Internals.ID2D1Shader.EffectDisplayName => ""; - return - PropertyDeclaration(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)))) - .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs deleted file mode 100644 index 0879e724..00000000 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using ComputeSharp.D2D1.SourceGenerators.Helpers; -using ComputeSharp.SourceGeneration.Extensions; -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. - public static string GetInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol) - { - if (TryGetDefinedEffectDisplayName(compilation, structDeclarationSymbol, out string? effectDisplayName)) - { - return effectDisplayName; - } - - return structDeclarationSymbol.GetFullyQualifiedMetadataName(); - } - - /// - /// 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.CreateEffectMetadataProperties.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.Syntax.cs new file mode 100644 index 00000000..fee46c11 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.Syntax.cs @@ -0,0 +1,82 @@ +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 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 GetEffectDisplayNameSyntax(string? effectDisplayName) + { + 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. + /// + /// 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 metadataValueExpression = metadataValue switch + { + { } => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(metadataValue)), + _ => LiteralExpression(SyntaxKind.NullLiteralExpression) + }; + + // This code produces a method declaration as follows: + // + // readonly string? global::ComputeSharp.D2D1.__Internals.ID2D1Shader. => ; + return + PropertyDeclaration(NullableType(PredefinedType(Token(SyntaxKind.StringKeyword))), Identifier(propertyName)) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) + .WithExpressionBody(ArrowExpressionClause(metadataValueExpression)) + .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs new file mode 100644 index 00000000..d2158878 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs @@ -0,0 +1,98 @@ +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", + isAssemblyLevelAttributeSupported: false, + out string? effectDisplayName)) + { + return effectDisplayName; + } + + 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", + isAssemblyLevelAttributeSupported: false, + 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", + isAssemblyLevelAttributeSupported: true, + 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", + isAssemblyLevelAttributeSupported: true, + 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 31767b11..d9f603b0 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -50,18 +50,33 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator return default; } - using ImmutableArrayBuilder diagnostics = ImmutableArrayBuilder.Rent(); - // EffectId info ImmutableArray effectId = EffectId.GetInfo(context.SemanticModel.Compilation, typeSymbol); token.ThrowIfCancellationRequested(); // EffectDisplayName info - string effectDisplayName = EffectDisplayName.GetInfo(context.SemanticModel.Compilation, typeSymbol); + string? effectDisplayName = EffectMetadata.GetEffectDisplayNameInfo(context.SemanticModel.Compilation, typeSymbol); 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,20 +166,6 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator shaderInfoWithErrors .Select(static (item, _) => item.Diagnostcs)); - // Get the EffectDisplayName info (hierarchy and effect display name) - IncrementalValuesProvider<(HierarchyInfo Hierarchy, string EffectDisplayName)> effectDisplayNameInfo = - shaderInfoWithErrors - .Select(static (item, _) => (item.Hierarchy, item.EffectDisplayName)); - - // Generate the EffectDisplayName properties - context.RegisterSourceOutput(effectDisplayNameInfo, static (context, item) => - { - PropertyDeclarationSyntax effectDisplayNameProperty = EffectDisplayName.GetSyntax(item.EffectDisplayName); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, effectDisplayNameProperty, canUseSkipLocalsInit: false); - - context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(EffectDisplayName)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); - }); - // Get the EffectId info (hierarchy and effect id info) IncrementalValuesProvider<(HierarchyInfo Hierarchy, EffectIdInfo EffectId)> effectIdInfo = shaderInfoWithErrors @@ -177,6 +181,62 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator 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 + .Select(static (item, _) => (item.Hierarchy, item.EffectDisplayName)); + + // Generate the EffectDisplayName properties + context.RegisterSourceOutput(effectDisplayNameInfo, static (context, item) => + { + PropertyDeclarationSyntax effectDisplayNameProperty = EffectMetadata.GetEffectDisplayNameSyntax(item.EffectDisplayName); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, effectDisplayNameProperty, canUseSkipLocalsInit: false); + + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.EffectDisplayName.g.cs", compilationUnit.GetText(Encoding.UTF8)); + }); + + // Get the EffectDescription info (hierarchy and effect description) + IncrementalValuesProvider<(HierarchyInfo Hierarchy, string? EffectDescription)> effectDescriptionInfo = + shaderInfoWithErrors + .Select(static (item, _) => (item.Hierarchy, item.EffectDescription)); + + // Generate the EffectDescription properties + context.RegisterSourceOutput(effectDescriptionInfo, static (context, item) => + { + PropertyDeclarationSyntax effectDescriptionProperty = EffectMetadata.GetEffectDescriptionSyntax(item.EffectDescription); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, effectDescriptionProperty, canUseSkipLocalsInit: false); + + 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) IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo = shaderInfoWithErrors diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs index 568ca2ef..5cd2c211 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs @@ -8,7 +8,10 @@ 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 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. @@ -20,7 +23,10 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; internal sealed record D2D1ShaderInfo( HierarchyInfo Hierarchy, EffectIdInfo EffectId, - string EffectDisplayName, + string? EffectDisplayName, + string? EffectDescription, + string? EffectCategory, + string? EffectAuthor, DispatchDataInfo DispatchData, InputTypesInfo InputTypes, ResourceTextureDescriptionsInfo ResourceTextureDescriptions, diff --git a/src/ComputeSharp.D2D1/Attributes/D2DEffectAuthorAttribute.cs b/src/ComputeSharp.D2D1/Attributes/D2DEffectAuthorAttribute.cs new file mode 100644 index 00000000..8e589eea --- /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 | AttributeTargets.Assembly, 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..53708f6c --- /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 | AttributeTargets.Assembly, 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 diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 3a6b4ce0..07f99fb6 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -20,12 +20,36 @@ 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; } + + /// + /// 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. diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index c6e2bd09..add62e86 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 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 { Unsafe.SkipInit(out T shader); @@ -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. /// @@ -71,12 +110,24 @@ public static unsafe class D2D1PixelShaderEffect + + + - - - """); @@ -311,12 +362,24 @@ public static unsafe class D2D1PixelShaderEffect + + + - - - """u8); 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. /// diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs index 712626f3..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); @@ -46,10 +47,10 @@ public partial class D2D1EffectRegistrationDataTests Assert.AreEqual(""" - - - - + + + + @@ -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 2e62f459..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(), typeof(ShaderWithDefaultEffectDisplayName).FullName); + 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