diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs index 51b9de28..0dd5f13c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs @@ -16,18 +16,25 @@ partial class ID2D1ShaderGenerator /// /// Creates a instance for the EffectDisplayName property. /// - /// The input effect display name value. + /// The input effect display name value, if available. /// The resulting instance for the EffectDisplayName property. - public static PropertyDeclarationSyntax GetSyntax(string effectDisplayName) + public static PropertyDeclarationSyntax GetSyntax(string? effectDisplayName) { + // Get the expression: either a string literal, or just null + ExpressionSyntax displayNameExpression = effectDisplayName switch + { + { } => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(effectDisplayName)), + _ => LiteralExpression(SyntaxKind.NullLiteralExpression) + }; + // This code produces a method declaration as follows: // - // readonly string global::ComputeSharp.D2D1.__Internals.ID2D1Shader.EffectDisplayName => ""; + // readonly string? global::ComputeSharp.D2D1.__Internals.ID2D1Shader.EffectDisplayName => ; return - PropertyDeclaration(PredefinedType(Token(SyntaxKind.StringKeyword)), Identifier(nameof(EffectDisplayName))) + PropertyDeclaration(NullableType(PredefinedType(Token(SyntaxKind.StringKeyword))), Identifier(nameof(EffectDisplayName))) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) - .WithExpressionBody(ArrowExpressionClause(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(effectDisplayName)))) + .WithExpressionBody(ArrowExpressionClause(displayNameExpression)) .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)); } } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs index 0879e724..556add8c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs @@ -1,6 +1,5 @@ using System.Diagnostics.CodeAnalysis; using ComputeSharp.D2D1.SourceGenerators.Helpers; -using ComputeSharp.SourceGeneration.Extensions; using Microsoft.CodeAnalysis; namespace ComputeSharp.D2D1.SourceGenerators; @@ -18,15 +17,15 @@ partial class ID2D1ShaderGenerator /// /// The input object currently in use. /// The for the shader type in use. - /// The resulting effect display name. - public static string GetInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol) + /// The resulting effect display name, if available. + public static string? GetInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol) { if (TryGetDefinedEffectDisplayName(compilation, structDeclarationSymbol, out string? effectDisplayName)) { return effectDisplayName; } - return structDeclarationSymbol.GetFullyQualifiedMetadataName(); + return null; } /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 31767b11..3a4812e0 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -58,7 +58,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator token.ThrowIfCancellationRequested(); // EffectDisplayName info - string effectDisplayName = EffectDisplayName.GetInfo(context.SemanticModel.Compilation, typeSymbol); + string? effectDisplayName = EffectDisplayName.GetInfo(context.SemanticModel.Compilation, typeSymbol); token.ThrowIfCancellationRequested(); @@ -149,7 +149,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator .Select(static (item, _) => item.Diagnostcs)); // Get the EffectDisplayName info (hierarchy and effect display name) - IncrementalValuesProvider<(HierarchyInfo Hierarchy, string EffectDisplayName)> effectDisplayNameInfo = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, string? EffectDisplayName)> effectDisplayNameInfo = shaderInfoWithErrors .Select(static (item, _) => (item.Hierarchy, item.EffectDisplayName)); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs index 568ca2ef..36f9836d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs @@ -8,7 +8,7 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; /// /// The hierarchy info for the shader type. /// The effect id info for the shader type. -/// The effect display name for the shader type. +/// The effect display name for the shader type, if available. /// The gathered shader dispatch data. /// The gathered input types for the shader. /// The gathered resource texture descriptions for the shader. @@ -20,7 +20,7 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; internal sealed record D2D1ShaderInfo( HierarchyInfo Hierarchy, EffectIdInfo EffectId, - string EffectDisplayName, + string? EffectDisplayName, DispatchDataInfo DispatchData, InputTypesInfo InputTypes, ResourceTextureDescriptionsInfo ResourceTextureDescriptions, diff --git a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs index 3a6b4ce0..716c22d0 100644 --- a/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs +++ b/src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs @@ -20,12 +20,12 @@ public interface ID2D1Shader ref readonly Guid EffectId { get; } /// - /// Gets the display name of the D2D effect using this shader. + /// Gets the display name of the D2D effect using this shader, if specified. /// /// /// This only applies to effects created from . /// - string EffectDisplayName { get; } + string? EffectDisplayName { get; } /// /// Initializes the current shader from a buffer with the serialized dispatch data. diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index c6e2bd09..66758318 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -32,11 +32,11 @@ public static unsafe class D2D1PixelShaderEffect } /// - /// Gets the effect display name of the D2D effect using this shader. + /// Gets the effect display name of the D2D effect using this shader, if available. /// /// The type of D2D1 pixel shader to get the effect id for. - /// The effect display name of the D2D effect using shaders. - public static string GetEffectDisplayName() + /// The effect display name of the D2D effect using shaders, if available. + public static string? GetEffectDisplayName() where T : unmanaged, ID2D1PixelShader { Unsafe.SkipInit(out T shader); @@ -71,7 +71,7 @@ public static unsafe class D2D1PixelShaderEffect @@ -311,7 +311,7 @@ public static unsafe class D2D1PixelShaderEffect diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs index 712626f3..19069545 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1EffectRegistrationDataTests.cs @@ -46,7 +46,7 @@ public partial class D2D1EffectRegistrationDataTests Assert.AreEqual(""" - + diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs index 2e62f459..8a9ea8a1 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -228,7 +228,7 @@ public partial class D2D1PixelShaderEffectTests [TestMethod] public unsafe void DefaultEffectDisplayName_MatchesValue() { - Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName(), typeof(ShaderWithDefaultEffectDisplayName).FullName); + Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName(), null); } [D2DInputCount(0)]