Merge pull request #566 from Sergio0694/dev/extended-effect-metadata
Add [D2DEffectAuthor], [D2DEffectCategory] and [D2DEffectDescription]
このコミットが含まれているのは:
20ファイル変更
+814
-219
検索条件に一致するファイルはありません
@@ -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)
|
||||
-62
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// A diagnostic analyzer that generates an error whenever [D2DEffectName] is used with an invalid value.
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public sealed class InvalidEffectDisplayNameValueAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DEffectDisplayNameAttributeValue);
|
||||
|
||||
/// <inheritdoc/>
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
+88
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// A diagnostic analyzer that generates an error whenever any shader metadata value attributes are used with an invalid value.
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public sealed class InvalidEffectMetadataValueAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
/// <summary>
|
||||
/// The set of type names for all metadata attributes.
|
||||
/// </summary>
|
||||
private static readonly ImmutableArray<string> MetadataAttributeTypeNames = ImmutableArray.Create(
|
||||
"ComputeSharp.D2D1.D2DEffectDisplayNameAttribute",
|
||||
"ComputeSharp.D2D1.D2DEffectDescriptionAttribute",
|
||||
"ComputeSharp.D2D1.D2DEffectCategoryAttribute",
|
||||
"ComputeSharp.D2D1.D2DEffectAuthorAttribute");
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of diagnostics to their attribute type names.
|
||||
/// </summary>
|
||||
private static readonly ImmutableDictionary<string, DiagnosticDescriptor> AttributeTypeNameToDiagnosticMapping = ImmutableDictionary.CreateRange(new[]
|
||||
{
|
||||
new KeyValuePair<string, DiagnosticDescriptor>("D2DEffectDisplayNameAttribute", InvalidD2DEffectDisplayNameAttributeValue),
|
||||
new KeyValuePair<string, DiagnosticDescriptor>("D2DEffectDescriptionAttribute", InvalidD2DEffectDescriptionAttributeValue),
|
||||
new KeyValuePair<string, DiagnosticDescriptor>("D2DEffectCategoryAttribute", InvalidD2DEffectCategoryAttributeValue),
|
||||
new KeyValuePair<string, DiagnosticDescriptor>("D2DEffectAuthorAttribute", InvalidD2DEffectAuthorAttributeValue)
|
||||
});
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
|
||||
InvalidD2DEffectDisplayNameAttributeValue,
|
||||
InvalidD2DEffectDescriptionAttributeValue,
|
||||
InvalidD2DEffectCategoryAttributeValue,
|
||||
InvalidD2DEffectAuthorAttributeValue);
|
||||
|
||||
/// <inheritdoc/>
|
||||
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<INamedTypeSymbol>? 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -877,7 +877,7 @@ partial class DiagnosticDescriptors
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>[D2DEffectDisplayName]</c> attribute is being used with an invalid value.
|
||||
/// <para>
|
||||
/// Format: <c>"The [D2DEffectDisplayName] attribute is being used with an invalid value (the input text must be a valid GUID)"</c>.
|
||||
/// Format: <c>"The [D2DEffectDisplayName] attribute is being used with an invalid value"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
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");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>[D2DEffectDescription]</c> attribute is being used with an invalid value.
|
||||
/// <para>
|
||||
/// Format: <c>"The [D2DEffectDescription] attribute is being used with an invalid value"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
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");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>[D2DEffectCategory]</c> attribute is being used with an invalid value.
|
||||
/// <para>
|
||||
/// Format: <c>"The [D2DEffectDescription] attribute is being used with an invalid value"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
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");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>[D2DEffectAuthor]</c> attribute is being used with an invalid value.
|
||||
/// <para>
|
||||
/// Format: <c>"The [D2DEffectAuthor] attribute is being used with an invalid value"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
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");
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a given <see cref="AttributeData"/> value contains a valid effect display name.
|
||||
/// Checks whether a given <see cref="AttributeData"/> value contains a valid effect metadata name.
|
||||
/// </summary>
|
||||
/// <param name="attributeData">The input <see cref="AttributeData"/> object to inspect.</param>
|
||||
/// <returns>Whether <paramref name="attributeData"/> contains a valid effect display name.</returns>
|
||||
public static bool IsValidEffectDisplayName(AttributeData attributeData)
|
||||
/// <returns>Whether <paramref name="attributeData"/> contains a valid effect metadata name.</returns>
|
||||
public static bool IsValidEffectMetadataName(AttributeData attributeData)
|
||||
{
|
||||
if (attributeData.ConstructorArguments is [{ Value: string { Length: > 0 } value }])
|
||||
{
|
||||
@@ -36,14 +37,50 @@ internal static class D2D1EffectMetadataParser
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
|
||||
/// <param name="typeSymbol">The input <see cref="INamedTypeSymbol"/> instance.</param>
|
||||
/// <param name="attributeFullyQualifiedMetadataName">The fully qualified metadata name of the target attribute.</param>
|
||||
/// <param name="isAssemblyLevelAttributeSupported">Whether the attribute can also be on the assembly.</param>
|
||||
/// <param name="effectMetadataName">The resulting defined effect metadata name, if found.</param>
|
||||
/// <returns>Whether or not a defined effect metadata name could be found.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the defined effect metadata name for a given attribute.
|
||||
/// </summary>
|
||||
/// <param name="attributeData">The input <see cref="AttributeData"/> object to inspect.</param>
|
||||
/// <param name="effectDisplayName">The resulting defined effect display name, if found.</param>
|
||||
/// <returns>Whether or not a defined effect display name could be found.</returns>
|
||||
public static bool TryGetEffectDisplayName(AttributeData attributeData, [NotNullWhen(true)] out string? effectDisplayName)
|
||||
/// <param name="effectMetadataName">The resulting defined effect metadata name, if found.</param>
|
||||
/// <returns>Whether or not a defined effect metadata name could be found.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
-34
@@ -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;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class ID2D1ShaderGenerator
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
partial class EffectDisplayName
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectDisplayName</c> property.
|
||||
/// </summary>
|
||||
/// <param name="effectDisplayName">The input effect display name value.</param>
|
||||
/// <returns>The resulting <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectDisplayName</c> property.</returns>
|
||||
public static PropertyDeclarationSyntax GetSyntax(string effectDisplayName)
|
||||
{
|
||||
// This code produces a method declaration as follows:
|
||||
//
|
||||
// readonly string global::ComputeSharp.D2D1.__Internals.ID2D1Shader.EffectDisplayName => "<EFFECT_DISPLAY_NAME>";
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
-57
@@ -1,57 +0,0 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using ComputeSharp.D2D1.SourceGenerators.Helpers;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.D2D1.SourceGenerators;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class ID2D1ShaderGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// A helper with all logic to generate the <c>EffectDisplayName</c> property.
|
||||
/// </summary>
|
||||
private static partial class EffectDisplayName
|
||||
{
|
||||
/// <summary>
|
||||
/// Extracts the effect display name info for the current shader.
|
||||
/// </summary>
|
||||
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
|
||||
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for the shader type in use.</param>
|
||||
/// <returns>The resulting effect display name.</returns>
|
||||
public static string GetInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol)
|
||||
{
|
||||
if (TryGetDefinedEffectDisplayName(compilation, structDeclarationSymbol, out string? effectDisplayName))
|
||||
{
|
||||
return effectDisplayName;
|
||||
}
|
||||
|
||||
return structDeclarationSymbol.GetFullyQualifiedMetadataName();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the defined effect display name for a given shader type.
|
||||
/// </summary>
|
||||
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
|
||||
/// <param name="typeSymbol">The input <see cref="INamedTypeSymbol"/> instance.</param>
|
||||
/// <param name="effectDisplayName">The resulting defined effect display name, if found.</param>
|
||||
/// <returns>Whether or not a defined effect display name could be found.</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
+82
@@ -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;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class ID2D1ShaderGenerator
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
partial class EffectMetadata
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectDisplayName</c> property.
|
||||
/// </summary>
|
||||
/// <param name="effectDisplayName">The input effect display name value, if available.</param>
|
||||
/// <returns>The resulting <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectDisplayName</c> property.</returns>
|
||||
public static PropertyDeclarationSyntax GetEffectDisplayNameSyntax(string? effectDisplayName)
|
||||
{
|
||||
return GetEffectMetadataSyntax("EffectDisplayName", effectDisplayName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectDescription</c> property.
|
||||
/// </summary>
|
||||
/// <param name="effectDescription">The input effect description value, if available.</param>
|
||||
/// <returns>The resulting <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectDescription</c> property.</returns>
|
||||
public static PropertyDeclarationSyntax GetEffectDescriptionSyntax(string? effectDescription)
|
||||
{
|
||||
return GetEffectMetadataSyntax("EffectDescription", effectDescription);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectCategory</c> property.
|
||||
/// </summary>
|
||||
/// <param name="effectCategory">The input effect category value, if available.</param>
|
||||
/// <returns>The resulting <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectCategory</c> property.</returns>
|
||||
public static PropertyDeclarationSyntax GetEffectCategorySyntax(string? effectCategory)
|
||||
{
|
||||
return GetEffectMetadataSyntax("EffectCategory", effectCategory);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectAuthor</c> property.
|
||||
/// </summary>
|
||||
/// <param name="effectAuthor">The input effect author value, if available.</param>
|
||||
/// <returns>The resulting <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectAuthor</c> property.</returns>
|
||||
public static PropertyDeclarationSyntax GetEffectAuthorSyntax(string? effectAuthor)
|
||||
{
|
||||
return GetEffectMetadataSyntax("EffectAuthor", effectAuthor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="PropertyDeclarationSyntax"/> instance for a given metadata property.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The property name to generate.</param>
|
||||
/// <param name="metadataValue">The input effect metadata value, if available.</param>
|
||||
/// <returns>The resulting <see cref="PropertyDeclarationSyntax"/> instance for the given metadata property.</returns>
|
||||
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.<PROPERTY_NAME> => <METADATA_VALUE>;
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
using ComputeSharp.D2D1.SourceGenerators.Helpers;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.D2D1.SourceGenerators;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class ID2D1ShaderGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// A helper with all logic to generate the effect metadata properties.
|
||||
/// </summary>
|
||||
private static partial class EffectMetadata
|
||||
{
|
||||
/// <summary>
|
||||
/// Extracts the effect display name info for the current shader.
|
||||
/// </summary>
|
||||
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
|
||||
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for the shader type in use.</param>
|
||||
/// <returns>The resulting effect display name, if available.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the effect description info for the current shader.
|
||||
/// </summary>
|
||||
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
|
||||
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for the shader type in use.</param>
|
||||
/// <returns>The resulting effect description, if available.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the effect category info for the current shader.
|
||||
/// </summary>
|
||||
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
|
||||
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for the shader type in use.</param>
|
||||
/// <returns>The resulting effect category, if available.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the effect author info for the current shader.
|
||||
/// </summary>
|
||||
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
|
||||
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for the shader type in use.</param>
|
||||
/// <returns>The resulting effect author, if available.</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,18 +50,33 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
return default;
|
||||
}
|
||||
|
||||
using ImmutableArrayBuilder<DiagnosticInfo> diagnostics = ImmutableArrayBuilder<DiagnosticInfo>.Rent();
|
||||
|
||||
// EffectId info
|
||||
ImmutableArray<byte> 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<DiagnosticInfo> diagnostics = ImmutableArrayBuilder<DiagnosticInfo>.Rent();
|
||||
|
||||
// LoadDispatchData() info
|
||||
ImmutableArray<FieldInfo> 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
|
||||
|
||||
@@ -8,7 +8,10 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models;
|
||||
/// </summary>
|
||||
/// <param name="Hierarchy">The hierarchy info for the shader type.</param>
|
||||
/// <param name="EffectId">The effect id info for the shader type.</param>
|
||||
/// <param name="EffectDisplayName">The effect display name for the shader type.</param>
|
||||
/// <param name="EffectDisplayName">The effect display name for the shader type, if available.</param>
|
||||
/// <param name="EffectDescription">The effect description for the shader type, if available.</param>
|
||||
/// <param name="EffectCategory">The effect category for the shader type, if available.</param>
|
||||
/// <param name="EffectAuthor">The effect author for the shader type, if available.</param>
|
||||
/// <param name="DispatchData">The gathered shader dispatch data.</param>
|
||||
/// <param name="InputTypes">The gathered input types for the shader.</param>
|
||||
/// <param name="ResourceTextureDescriptions">The gathered resource texture descriptions for the shader.</param>
|
||||
@@ -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,
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
|
||||
namespace ComputeSharp.D2D1;
|
||||
|
||||
/// <summary>
|
||||
/// An attribute for a D2D1 shader indicating the author of the shader effect to create.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This only applies to effects created from <see cref="Interop.D2D1PixelShaderEffect"/>.
|
||||
/// </remarks>
|
||||
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = false)]
|
||||
public sealed class D2DEffectAuthorAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="D2DEffectAuthorAttribute"/> type with the specified arguments.
|
||||
/// </summary>
|
||||
/// <param name="value">The value for the author.</param>
|
||||
public D2DEffectAuthorAttribute(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the author value.
|
||||
/// </summary>
|
||||
public string Value { get; }
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
|
||||
namespace ComputeSharp.D2D1;
|
||||
|
||||
/// <summary>
|
||||
/// An attribute for a D2D1 shader indicating the category of the shader effect to create.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This only applies to effects created from <see cref="Interop.D2D1PixelShaderEffect"/>.
|
||||
/// </remarks>
|
||||
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = false)]
|
||||
public sealed class D2DEffectCategoryAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="D2DEffectCategoryAttribute"/> type with the specified arguments.
|
||||
/// </summary>
|
||||
/// <param name="value">The value for the category.</param>
|
||||
public D2DEffectCategoryAttribute(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the category value.
|
||||
/// </summary>
|
||||
public string Value { get; }
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
|
||||
namespace ComputeSharp.D2D1;
|
||||
|
||||
/// <summary>
|
||||
/// An attribute for a D2D1 shader indicating the description of the shader effect to create.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This only applies to effects created from <see cref="Interop.D2D1PixelShaderEffect"/>.
|
||||
/// </remarks>
|
||||
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)]
|
||||
public sealed class D2DEffectDescriptionAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="D2DEffectDescriptionAttribute"/> type with the specified arguments.
|
||||
/// </summary>
|
||||
/// <param name="value">The value for the description.</param>
|
||||
public D2DEffectDescriptionAttribute(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the description value.
|
||||
/// </summary>
|
||||
public string Value { get; }
|
||||
}
|
||||
@@ -20,12 +20,36 @@ public interface ID2D1Shader
|
||||
ref readonly Guid EffectId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the display name of the D2D effect using this shader.
|
||||
/// Gets the display name of the D2D effect using this shader, if specified.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This only applies to effects created from <see cref="Interop.D2D1PixelShaderEffect"/>.
|
||||
/// </remarks>
|
||||
string EffectDisplayName { get; }
|
||||
string? EffectDisplayName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the description of the D2D effect using this shader, if specified.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This only applies to effects created from <see cref="Interop.D2D1PixelShaderEffect"/>.
|
||||
/// </remarks>
|
||||
string? EffectDescription { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the category of the D2D effect using this shader, if specified.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This only applies to effects created from <see cref="Interop.D2D1PixelShaderEffect"/>.
|
||||
/// </remarks>
|
||||
string? EffectCategory { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the author of the D2D effect using this shader, if specified.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This only applies to effects created from <see cref="Interop.D2D1PixelShaderEffect"/>.
|
||||
/// </remarks>
|
||||
string? EffectAuthor { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the current shader from a buffer with the serialized dispatch data.
|
||||
|
||||
@@ -32,11 +32,11 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to get the effect id for.</typeparam>
|
||||
/// <returns>The effect display name of the D2D effect using <typeparamref name="T"/> shaders.</returns>
|
||||
public static string GetEffectDisplayName<T>()
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to get the effect display name for.</typeparam>
|
||||
/// <returns>The effect display name of the D2D effect using <typeparamref name="T"/> shaders, if available.</returns>
|
||||
public static string? GetEffectDisplayName<T>()
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
Unsafe.SkipInit(out T shader);
|
||||
@@ -44,6 +44,45 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
return shader.EffectDisplayName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the effect description of the D2D effect using this shader, if available.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to get the effect description for.</typeparam>
|
||||
/// <returns>The effect description of the D2D effect using <typeparamref name="T"/> shaders, if available.</returns>
|
||||
public static string? GetEffectDescription<T>()
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
Unsafe.SkipInit(out T shader);
|
||||
|
||||
return shader.EffectDescription;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the effect category of the D2D effect using this shader, if available.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to get the effect category for.</typeparam>
|
||||
/// <returns>The effect category of the D2D effect using <typeparamref name="T"/> shaders, if available.</returns>
|
||||
public static string? GetEffectCategory<T>()
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
Unsafe.SkipInit(out T shader);
|
||||
|
||||
return shader.EffectCategory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the effect author of the D2D effect using this shader, if available.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to get the effect author for.</typeparam>
|
||||
/// <returns>The effect author of the D2D effect using <typeparamref name="T"/> shaders, if available.</returns>
|
||||
public static string? GetEffectAuthor<T>()
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
Unsafe.SkipInit(out T shader);
|
||||
|
||||
return shader.EffectAuthor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers an effect from an input D2D1 pixel shader, by calling <c>ID2D1Factory1::RegisterEffectFromString</c>.
|
||||
/// </summary>
|
||||
@@ -71,12 +110,24 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
<Effect>
|
||||
<Property name='DisplayName' type='string' value='
|
||||
""");
|
||||
writer.WriteRaw(GetEffectDisplayName<T>());
|
||||
writer.WriteRaw(GetEffectDisplayName<T>().AsSpan());
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Description' type='string' value='
|
||||
""");
|
||||
writer.WriteRaw(GetEffectDescription<T>().AsSpan());
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Category' type='string' value='
|
||||
""");
|
||||
writer.WriteRaw(GetEffectCategory<T>().AsSpan());
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Author' type='string' value='
|
||||
""");
|
||||
writer.WriteRaw(GetEffectAuthor<T>().AsSpan());
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Author' type='string' value='ComputeSharp.D2D1'/>
|
||||
<Property name='Category' type='string' value='Stylize'/>
|
||||
<Property name='Description' type='string' value='A custom D2D1 effect using a pixel shader'/>
|
||||
<Inputs>
|
||||
|
||||
""");
|
||||
@@ -311,12 +362,24 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
<Effect>
|
||||
<Property name='DisplayName' type='string' value='
|
||||
"""u8);
|
||||
writer.WriteAsUtf8(GetEffectDisplayName<T>());
|
||||
writer.WriteAsUtf8(GetEffectDisplayName<T>() ?? "");
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Description' type='string' value='
|
||||
"""u8);
|
||||
writer.WriteAsUtf8(GetEffectDescription<T>() ?? "");
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Category' type='string' value='
|
||||
"""u8);
|
||||
writer.WriteAsUtf8(GetEffectCategory<T>() ?? "");
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Author' type='string' value='
|
||||
"""u8);
|
||||
writer.WriteAsUtf8(GetEffectAuthor<T>() ?? "");
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Author' type='string' value='ComputeSharp.D2D1'/>
|
||||
<Property name='Category' type='string' value='Stylize'/>
|
||||
<Property name='Description' type='string' value='A custom D2D1 effect using a pixel shader'/>
|
||||
<Inputs>
|
||||
|
||||
"""u8);
|
||||
|
||||
@@ -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;
|
||||
/// </summary>
|
||||
internal static class CompilationExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to build a set of <see cref="INamedTypeSymbol"/> instances form the input mapping of names.
|
||||
/// </summary>
|
||||
/// <param name="compilation">The <see cref="Compilation"/> to consider for analysis.</param>
|
||||
/// <param name="fullyQualifiedTypeNames">The input fully qualified type names.</param>
|
||||
/// <param name="typeSymbols">The resulting mapping of resolved <see cref="INamedTypeSymbol"/> instances.</param>
|
||||
/// <returns>Whether all requested <see cref="INamedTypeSymbol"/> instances could be resolved.</returns>
|
||||
public static bool TryBuildNamedTypeSymbolSet(
|
||||
this Compilation compilation,
|
||||
IEnumerable<string> fullyQualifiedTypeNames,
|
||||
[NotNullWhen(true)] out ImmutableHashSet<INamedTypeSymbol>? typeSymbols)
|
||||
{
|
||||
ImmutableHashSet<INamedTypeSymbol>.Builder builder = ImmutableHashSet.CreateBuilder<INamedTypeSymbol>(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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Checks whether or not a type with a specified metadata name is accessible from a given <see cref="Compilation"/> instance.
|
||||
|
||||
@@ -27,6 +27,30 @@ internal static class ISymbolExtensions
|
||||
return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get an attribute with the specified type.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The input <see cref="ISymbol"/> instance to check.</param>
|
||||
/// <param name="typeSymbol">The <see cref="ITypeSymbol"/> instance for the attribute type to look for.</param>
|
||||
/// <param name="attributeData">The resulting attribute, if it was found.</param>
|
||||
/// <returns>Whether or not <paramref name="symbol"/> has an attribute with the specified name.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get an attribute with the specified fully qualified metadata name.
|
||||
/// </summary>
|
||||
|
||||
@@ -14,6 +14,7 @@ public partial class D2D1EffectRegistrationDataTests
|
||||
ReadOnlyMemory<byte> blob = D2D1PixelShaderEffect.GetRegistrationBlob<TestRegistrationBlobShader>(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("""
|
||||
<?xml version='1.0'?>
|
||||
<Effect>
|
||||
<Property name='DisplayName' type='string' value='ComputeSharp.D2D1.Tests.D2D1EffectRegistrationDataTests+TestRegistrationBlobShader'/>
|
||||
<Property name='Author' type='string' value='ComputeSharp.D2D1'/>
|
||||
<Property name='Category' type='string' value='Stylize'/>
|
||||
<Property name='Description' type='string' value='A custom D2D1 effect using a pixel shader'/>
|
||||
<Property name='DisplayName' type='string' value=''/>
|
||||
<Property name='Description' type='string' value=''/>
|
||||
<Property name='Category' type='string' value=''/>
|
||||
<Property name='Author' type='string' value=''/>
|
||||
<Inputs>
|
||||
<Input name='Source0'/>
|
||||
<Input name='Source1'/>
|
||||
@@ -124,4 +125,97 @@ public partial class D2D1EffectRegistrationDataTests
|
||||
return this.a;
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void EffectRegistrationData_WithCustomMetadata_Validate()
|
||||
{
|
||||
ReadOnlyMemory<byte> blob = D2D1PixelShaderEffect.GetRegistrationBlob<TestRegistrationBlobWithCustomMetadataShader>(out Guid effectId);
|
||||
D2D1EffectRegistrationData.V1 data = D2D1EffectRegistrationData.V1.Load(blob);
|
||||
|
||||
Assert.AreEqual(Guid.Parse("ABF58390-91D2-4F58-AA2B-22D0F6AC069C"), effectId);
|
||||
Assert.AreEqual("""
|
||||
<?xml version='1.0'?>
|
||||
<Effect>
|
||||
<Property name='DisplayName' type='string' value='EffectWithCustomMetadata'/>
|
||||
<Property name='Description' type='string' value='A test effect with some custom metadata'/>
|
||||
<Property name='Category' type='string' value='Test effects'/>
|
||||
<Property name='Author' type='string' value='Bob Ross'/>
|
||||
<Inputs>
|
||||
<Input name='Source0'/>
|
||||
<Input name='Source1'/>
|
||||
</Inputs>
|
||||
<Property name='ConstantBuffer' type='blob'>
|
||||
<Property name='DisplayName' type='string' value='ConstantBuffer'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager0' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager0'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager1' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager1'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager2' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager2'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager3' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager3'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager4' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager4'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager5' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager5'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager6' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager6'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager7' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager7'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager8' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager8'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager9' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager9'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager10' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager10'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager11' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager11'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager12' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager12'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager13' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager13'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager14' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager14'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager15' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager15'/>
|
||||
</Property>
|
||||
<Property name='TransformMapper' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='TransformMapper'/>
|
||||
</Property>
|
||||
</Effect>
|
||||
""", 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -226,9 +226,12 @@ public partial class D2D1PixelShaderEffectTests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void DefaultEffectDisplayName_MatchesValue()
|
||||
public unsafe void DefaultEffectMetadata_MatchesValue()
|
||||
{
|
||||
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName<ShaderWithDefaultEffectDisplayName>(), typeof(ShaderWithDefaultEffectDisplayName).FullName);
|
||||
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName<ShaderWithDefaultEffectDisplayName>(), null);
|
||||
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName<ShaderWithDefaultEffectDisplayName>(), null);
|
||||
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName<ShaderWithDefaultEffectDisplayName>(), null);
|
||||
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName<ShaderWithDefaultEffectDisplayName>(), 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<ShaderWithExplicitEffectDisplayName>(), "Fancy blur");
|
||||
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName<ShaderWithExplicitEffectDisplayName2>(), "Fancy"<");
|
||||
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName<ShaderWithExplicitEffectDisplayName3>(), "FancyBlurEffect");
|
||||
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName<ShaderWithExplicitEffectDisplayName1>(), "Fancy blur");
|
||||
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDescription<ShaderWithExplicitEffectDisplayName1>(), null);
|
||||
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectCategory<ShaderWithExplicitEffectDisplayName1>(), null);
|
||||
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectAuthor<ShaderWithExplicitEffectDisplayName1>(), 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<ShaderWithExplicitEffectDisplayName2>(), "Fancy"<");
|
||||
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDescription<ShaderWithExplicitEffectDisplayName2>(), "A test effect with some custom metadata");
|
||||
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectCategory<ShaderWithExplicitEffectDisplayName2>(), "Test effects!");
|
||||
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectAuthor<ShaderWithExplicitEffectDisplayName2>(), "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;
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする