Skip to content

Commit

Permalink
Rename and update source generator files
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Sep 20, 2023
1 parent 2145a92 commit 93be034
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,26 @@ namespace ComputeSharp.D2D1.SourceGenerators;
partial class ID2D1ShaderGenerator
{
/// <inheritdoc/>
partial class GetInputType
partial class InputTypes
{
/// <summary>
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>GetInputTypeMethod</c> method.
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>InputTypes</c> property.
/// </summary>
/// <param name="inputTypes">The input types for the shader.</param>
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c
/// >GetInputTypeMethod</c> method.</returns>
public static (MemberDeclarationSyntax Member, MemberDeclarationSyntax Type) GetSyntax(ImmutableArray<uint> inputTypes)
/// <param name="additionalType">An additional <see cref="TypeDeclarationSyntax"/> instance needed by the generated code.</param>
/// <returns>The resulting <see cref="MemberDeclarationSyntax"/> instance for the <c>InputTypes</c> property.</returns>
public static PropertyDeclarationSyntax GetSyntax(ImmutableArray<uint> inputTypes, out TypeDeclarationSyntax additionalType)
{
additionalType = GetMemoryManagerDeclaration(inputTypes);

// This code produces a method declaration as follows:
//
// readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputTypes => InputTypesMemoryManager.Memory;
MemberDeclarationSyntax member =
return
PropertyDeclaration(
GenericName(Identifier("global::System.ReadOnlyMemory"))
.AddTypeArgumentListArguments(IdentifierName("global::ComputeSharp.D2D1.Interop.D2D1PixelShaderInputType")),
Identifier("InputTypes")) // TODO: use nameof()
Identifier(nameof(InputTypes)))
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
.WithExpressionBody(ArrowExpressionClause(
Expand All @@ -40,8 +42,6 @@ public static (MemberDeclarationSyntax Member, MemberDeclarationSyntax Type) Get
IdentifierName("InputTypesMemoryManager"),
IdentifierName("Memory"))))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));

return (member, GetMemoryManagerDeclaration(inputTypes));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace ComputeSharp.D2D1.SourceGenerators;
partial class ID2D1ShaderGenerator
{
/// <summary>
/// A helper with all logic to generate the <c>GetInputType</c> method.
/// A helper with all logic to generate the <c>InputTypes</c> properties.
/// </summary>
private static partial class GetInputType
private static partial class InputTypes
{
/// <summary>
/// Extracts the input info for the current shader.
Expand Down
14 changes: 7 additions & 7 deletions src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
token.ThrowIfCancellationRequested();
// Get the input info for GetInputInfo()
GetInputType.GetInfo(
// Get the input info for InputTypes
InputTypes.GetInfo(
diagnostics,
typeSymbol,
out int inputCount,
Expand Down Expand Up @@ -150,18 +150,18 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetInputCount)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});

// Get the GetInputType() info (hierarchy and input types)
// Get the InputTypes info (hierarchy and input types)
IncrementalValuesProvider<(HierarchyInfo Hierarchy, InputTypesInfo InputTypes)> inputTypesInfo =
shaderInfoWithErrors
.Select(static (item, _) => (item.Hierarchy, item.InputTypes));

// Generate the GetInputType() methods
// Generate the InputTypes properties
context.RegisterSourceOutput(inputTypesInfo, static (context, item) =>
{
(MemberDeclarationSyntax property, MemberDeclarationSyntax type) = GetInputType.GetSyntax(item.InputTypes.InputTypes);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, property, canUseSkipLocalsInit: false, additionalMemberDeclarations: type);
MemberDeclarationSyntax inputTypesProperty = InputTypes.GetSyntax(item.InputTypes.InputTypes, out TypeDeclarationSyntax additionalType);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, inputTypesProperty, canUseSkipLocalsInit: false, additionalMemberDeclarations: additionalType);
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(GetInputType)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(InputTypes)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});

// Get the LoadResourceTextureDescriptions() info (hierarchy and resource texture descriptions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public interface ID2D1Shader
/// <summary>
/// Gets the input types for the current shader.
/// </summary>
/// <returns>The input types for the current shader.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be used directly by user code")]
ReadOnlyMemory<D2D1PixelShaderInputType> InputTypes { get; }
Expand Down

0 comments on commit 93be034

Please sign in to comment.