Skip to content

Commit

Permalink
Update ID2D1Shader interface and generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Sep 17, 2023
1 parent e6fc5d3 commit cdb826a
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,25 @@ 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>
/// <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 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 => "<EFFECT_DISPLAY_NAME>";
// readonly string? global::ComputeSharp.D2D1.__Internals.ID2D1Shader.EffectDisplayName => <EFFECT_DISPLAY_NAME>;
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));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using ComputeSharp.D2D1.SourceGenerators.Helpers;
using ComputeSharp.SourceGeneration.Extensions;
using Microsoft.CodeAnalysis;

namespace ComputeSharp.D2D1.SourceGenerators;
Expand All @@ -18,15 +17,15 @@ private static partial class EffectDisplayName
/// </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)
/// <returns>The resulting effect display name, if available.</returns>
public static string? GetInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol)
{
if (TryGetDefinedEffectDisplayName(compilation, structDeclarationSymbol, out string? effectDisplayName))
{
return effectDisplayName;
}

return structDeclarationSymbol.GetFullyQualifiedMetadataName();
return null;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
token.ThrowIfCancellationRequested();
// EffectDisplayName info
string effectDisplayName = EffectDisplayName.GetInfo(context.SemanticModel.Compilation, typeSymbol);
string? effectDisplayName = EffectDisplayName.GetInfo(context.SemanticModel.Compilation, typeSymbol);
token.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -149,7 +149,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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="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>
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ 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>
/// Initializes the current shader from a buffer with the serialized dispatch data.
Expand Down
10 changes: 5 additions & 5 deletions src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public static ref readonly Guid GetEffectId<T>()
}

/// <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>()
/// <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);
Expand Down Expand Up @@ -71,7 +71,7 @@ public static void RegisterForD2D1Factory1<T>(void* d2D1Factory1, out Guid effec
<Effect>
<Property name='DisplayName' type='string' value='
""");
writer.WriteRaw(GetEffectDisplayName<T>());
writer.WriteRaw(GetEffectDisplayName<T>().AsSpan());
writer.WriteRaw("""
'/>
<Property name='Author' type='string' value='ComputeSharp.D2D1'/>
Expand Down Expand Up @@ -311,7 +311,7 @@ public static ReadOnlyMemory<byte> GetRegistrationBlob<T>(out Guid effectId)
<Effect>
<Property name='DisplayName' type='string' value='
"""u8);
writer.WriteAsUtf8(GetEffectDisplayName<T>());
writer.WriteAsUtf8(GetEffectDisplayName<T>() ?? "");
writer.WriteRaw("""
'/>
<Property name='Author' type='string' value='ComputeSharp.D2D1'/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public unsafe void EffectRegistrationData_Validate()
Assert.AreEqual("""
<?xml version='1.0'?>
<Effect>
<Property name='DisplayName' type='string' value='ComputeSharp.D2D1.Tests.D2D1EffectRegistrationDataTests+TestRegistrationBlobShader'/>
<Property name='DisplayName' type='string' value=''/>
<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'/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public float4 Execute()
[TestMethod]
public unsafe void DefaultEffectDisplayName_MatchesValue()
{
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName<ShaderWithDefaultEffectDisplayName>(), typeof(ShaderWithDefaultEffectDisplayName).FullName);
Assert.AreEqual(D2D1PixelShaderEffect.GetEffectDisplayName<ShaderWithDefaultEffectDisplayName>(), null);
}

[D2DInputCount(0)]
Expand Down

0 comments on commit cdb826a

Please sign in to comment.