Skip to content

Commit

Permalink
Add ID2D1Shader.EffectDisplayName property
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Sep 14, 2023
1 parent 86dc363 commit 1feec3a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using ComputeSharp.D2D1.__Internals;
using ComputeSharp.SourceGeneration.Models;
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="info">The input <see cref="HierarchyInfo"/> instance.</param>
/// <returns>The resulting <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectDisplayName</c> property.</returns>
public static PropertyDeclarationSyntax GetSyntax(HierarchyInfo info)
{
// This code produces a method declaration as follows:
//
// readonly string global::ComputeSharp.D2D1.__Internals.ID2D1Shader.EffectDisplayName => "<FULLY_QUALIFIED_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(info.FullyQualifiedMetadataName))))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public static bool IsD2D1PixelShaderType(INamedTypeSymbol typeSymbol, Compilatio
/// Creates a <see cref="CompilationUnitSyntax"/> instance wrapping the given method.
/// </summary>
/// <param name="hierarchyInfo">The <see cref="HierarchyInfo"/> instance for the current type.</param>
/// <param name="methodDeclaration">The <see cref="MethodDeclarationSyntax"/> item to insert.</param>
/// <param name="memberDeclarationSyntax">The <see cref="MemberDeclarationSyntax"/> item to insert.</param>
/// <param name="canUseSkipLocalsInit">Whether <c>[SkipLocalsInit]</c> can be used.</param>
/// <returns>A <see cref="CompilationUnitSyntax"/> object wrapping <paramref name="methodDeclaration"/>.</returns>
private static CompilationUnitSyntax GetCompilationUnitFromMethod(
/// <returns>A <see cref="CompilationUnitSyntax"/> object wrapping <paramref name="memberDeclarationSyntax"/>.</returns>
private static CompilationUnitSyntax GetCompilationUnitFromMember(
HierarchyInfo hierarchyInfo,
MethodDeclarationSyntax methodDeclaration,
MemberDeclarationSyntax memberDeclarationSyntax,
bool canUseSkipLocalsInit)
{
// Method attributes
Expand Down Expand Up @@ -71,6 +71,6 @@ private static CompilationUnitSyntax GetCompilationUnitFromMethod(
attributes.Add(AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Runtime.CompilerServices.SkipLocalsInit")))));
}

return hierarchyInfo.GetSyntax(methodDeclaration.AddAttributeLists(attributes.ToArray()));
return hierarchyInfo.GetSyntax(memberDeclarationSyntax.AddAttributeLists(attributes.ToArray()));
}
}
34 changes: 24 additions & 10 deletions src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
shaderInfoWithErrors
.Select(static (item, _) => item.Diagnostcs));

// Get the EffectDisplayName info (hierarchy)
IncrementalValuesProvider<HierarchyInfo> hierarchyInfo =
shaderInfoWithErrors
.Select(static (item, _) => item.Hierarchy);

// Generate the EffectDisplayName properties
context.RegisterSourceOutput(hierarchyInfo, static (context, item) =>
{
PropertyDeclarationSyntax effectDisplayNameProperty = EffectDisplayName.GetSyntax(item);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item, effectDisplayNameProperty, canUseSkipLocalsInit: false);
context.AddSource($"{item.FullyQualifiedMetadataName}.{nameof(EffectDisplayName)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});

// Get the GetInputCount() info (hierarchy and input count)
IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo =
shaderInfoWithErrors
Expand All @@ -145,7 +159,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterSourceOutput(inputCountInfo, static (context, item) =>
{
MethodDeclarationSyntax getInputCountMethod = GetInputCount.GetSyntax(item.InputCount);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getInputCountMethod, canUseSkipLocalsInit: false);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, getInputCountMethod, canUseSkipLocalsInit: false);
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetInputCount)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
Expand All @@ -159,7 +173,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterSourceOutput(inputTypesInfo, static (context, item) =>
{
MethodDeclarationSyntax getInputTypeMethod = GetInputType.GetSyntax(item.InputTypes.InputTypes);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getInputTypeMethod, canUseSkipLocalsInit: false);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, getInputTypeMethod, canUseSkipLocalsInit: false);
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetInputType)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
Expand All @@ -173,7 +187,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterSourceOutput(resourceTextureDescriptionsInfo, static (context, item) =>
{
MethodDeclarationSyntax getInputTypeMethod = LoadResourceTextureDescriptions.GetSyntax(item.ResourceTextureDescriptions);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getInputTypeMethod, canUseSkipLocalsInit: false);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, getInputTypeMethod, canUseSkipLocalsInit: false);
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadResourceTextureDescriptions)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
Expand All @@ -187,7 +201,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterSourceOutput(initializeFromDispatchDataInfo, static (context, item) =>
{
MethodDeclarationSyntax loadDispatchDataMethod = InitializeFromDispatchData.GetSyntax(item.Dispatch);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, loadDispatchDataMethod, canUseSkipLocalsInit: false);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, loadDispatchDataMethod, canUseSkipLocalsInit: false);
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(InitializeFromDispatchData)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
Expand All @@ -201,7 +215,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterSourceOutput(dispatchDataInfo, static (context, item) =>
{
MethodDeclarationSyntax loadDispatchDataMethod = LoadDispatchData.GetSyntax(item.Info.Dispatch);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Info.Hierarchy, loadDispatchDataMethod, item.CanUseSkipLocalsInit);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Info.Hierarchy, loadDispatchDataMethod, item.CanUseSkipLocalsInit);
context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadDispatchData)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
Expand All @@ -221,7 +235,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterSourceOutput(hlslSourceInfo, static (context, item) =>
{
MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.Info.HlslSource, item.Info.Hierarchy.Hierarchy.Length, item.CanUseRawMultilineStringLiterals);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Info.Hierarchy, buildHlslStringMethod, canUseSkipLocalsInit: false);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Info.Hierarchy, buildHlslStringMethod, canUseSkipLocalsInit: false);
context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
Expand Down Expand Up @@ -279,7 +293,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterSourceOutput(embeddedBytecode, static (context, item) =>
{
MethodDeclarationSyntax loadBytecodeMethod = LoadBytecode.GetSyntax(item.BytecodeInfo, out Func<SyntaxNode, SourceText> fixup);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, loadBytecodeMethod, canUseSkipLocalsInit: false);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, loadBytecodeMethod, canUseSkipLocalsInit: false);
SourceText text = fixup(compilationUnit);
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadBytecode)}.g.cs", text);
Expand All @@ -294,7 +308,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterSourceOutput(outputBufferInfo, static (context, item) =>
{
MethodDeclarationSyntax getOutputBufferMethod = GetOutputBuffer.GetSyntax(item.OutputBuffer);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getOutputBufferMethod, canUseSkipLocalsInit: false);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, getOutputBufferMethod, canUseSkipLocalsInit: false);
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetOutputBuffer)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
Expand All @@ -309,7 +323,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterSourceOutput(inputDescriptionsInfo, static (context, item) =>
{
MethodDeclarationSyntax loadInputDescriptionsMethod = LoadInputDescriptions.GetSyntax(item.Info.InputDescriptions);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Info.Hierarchy, loadInputDescriptionsMethod, item.CanUseSkipLocalsInit);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Info.Hierarchy, loadInputDescriptionsMethod, item.CanUseSkipLocalsInit);
context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadInputDescriptions)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
Expand All @@ -323,7 +337,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterSourceOutput(pixelOptionsInfo, static (context, item) =>
{
MethodDeclarationSyntax getPixelOptionsMethod = GetPixelOptions.GetSyntax(item.PixelOptions);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getPixelOptionsMethod, canUseSkipLocalsInit: false);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, getPixelOptionsMethod, canUseSkipLocalsInit: false);
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetPixelOptions)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
Expand Down
8 changes: 8 additions & 0 deletions src/ComputeSharp.D2D1/Interfaces/__Internals/ID2D1Shader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ namespace ComputeSharp.D2D1.__Internals;
[Obsolete("This interface is not intended to be used directly by user code")]
public interface ID2D1Shader
{
/// <summary>
/// Gets the display name of the D2D effect using this shader.
/// </summary>
/// <remarks>
/// This only applies to effects created from <see cref="Interop.D2D1PixelShaderEffect"/>.
/// </remarks>
string EffectDisplayName { get; }

/// <summary>
/// Initializes the current shader from a buffer with the serialized dispatch data.
/// </summary>
Expand Down

0 comments on commit 1feec3a

Please sign in to comment.