Add ID2D1Shader.EffectId property
このコミットが含まれているのは:
1 親
97a2df23d5
コミット
c31572f68f
3ファイル変更
+132
-1
検索条件に一致するファイルはありません
+113
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using ComputeSharp.D2D1.__Internals;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using ComputeSharp.SourceGeneration.Models;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.D2D1.SourceGenerators;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class ID2D1ShaderGenerator
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
partial class EffectId
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectId</c> property.
|
||||
/// </summary>
|
||||
/// <param name="info">The input <see cref="HierarchyInfo"/> instance.</param>
|
||||
/// <param name="fixup">An opaque <see cref="Func{TResult}"/> instance to transform the final tree into text.</param>
|
||||
/// <returns>The resulting <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectId</c> property.</returns>
|
||||
public static PropertyDeclarationSyntax GetSyntax(HierarchyInfo info, out Func<SyntaxNode, SourceText> fixup)
|
||||
{
|
||||
// Create the local declaration:
|
||||
//
|
||||
// global::System.ReadOnlySpan<byte> bytes = new byte[] { __EMBEDDED_EFFECT_ID_BYTES };
|
||||
LocalDeclarationStatementSyntax guidBytesDeclaration =
|
||||
LocalDeclarationStatement(
|
||||
VariableDeclaration(
|
||||
GenericName(Identifier("global::System.ReadOnlySpan"))
|
||||
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))))
|
||||
.AddVariables(
|
||||
VariableDeclarator(Identifier("bytes"))
|
||||
.WithInitializer(
|
||||
EqualsValueClause(
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
|
||||
.WithInitializer(
|
||||
InitializerExpression(
|
||||
SyntaxKind.ArrayInitializerExpression,
|
||||
SingletonSeparatedList<ExpressionSyntax>(IdentifierName("__EMBEDDED_EFFECT_ID_BYTES"))))))));
|
||||
|
||||
// Prepare the initialization text
|
||||
string effectIdLiterals = SyntaxFormattingHelper.BuildByteArrayInitializationExpressionString(new byte[] { 1, 2, 3, 4 }.AsSpan());
|
||||
|
||||
// Set the fixup function
|
||||
fixup = tree => SourceText.From(tree.ToFullString().Replace("__EMBEDDED_EFFECT_ID_BYTES", effectIdLiterals), Encoding.UTF8);
|
||||
|
||||
// Prepare the return statement:
|
||||
//
|
||||
// return
|
||||
// ref global::System.Runtime.CompilerServices.Unsafe.As<byte, global::System.Guid>(
|
||||
// ref global::System.Runtime.InteropServices.MemoryMarshal.GetReference(bytes));
|
||||
ReturnStatementSyntax returnStatement =
|
||||
ReturnStatement(
|
||||
RefExpression(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("global::System.Runtime.CompilerServices.Unsafe"),
|
||||
GenericName(Identifier("As"))
|
||||
.AddTypeArgumentListArguments(
|
||||
PredefinedType(Token(SyntaxKind.ByteKeyword)),
|
||||
IdentifierName("global::System.Guid"))))
|
||||
.AddArgumentListArguments(
|
||||
Argument(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("global::System.Runtime.InteropServices.MemoryMarshal"),
|
||||
IdentifierName("GetReference")))
|
||||
.AddArgumentListArguments(Argument(IdentifierName("bytes"))))
|
||||
.WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)))));
|
||||
|
||||
// This code produces a method declaration as follows:
|
||||
//
|
||||
// readonly ref readonly global::System.Guid global::ComputeSharp.D2D1.__Internals.ID2D1Shader.EffectId
|
||||
// {
|
||||
// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
|
||||
// get
|
||||
// {
|
||||
// <BYTES_DECLARATION>
|
||||
// <RETURN_STATEMENT>
|
||||
// }
|
||||
// }
|
||||
return
|
||||
PropertyDeclaration(
|
||||
RefType(IdentifierName("global::System.Guid")).WithReadOnlyKeyword(Token(SyntaxKind.ReadOnlyKeyword)),
|
||||
Identifier(nameof(EffectId)))
|
||||
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
|
||||
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
|
||||
.AddAccessorListAccessors(
|
||||
AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
|
||||
.AddAttributeLists(
|
||||
AttributeList(SingletonSeparatedList(
|
||||
Attribute(IdentifierName("global::System.Runtime.CompilerServices.MethodImpl"))
|
||||
.AddArgumentListArguments(
|
||||
AttributeArgument(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("global::System.Runtime.CompilerServices.MethodImplOptions"),
|
||||
IdentifierName("AggressiveInlining")))))))
|
||||
.AddBodyStatements(guidBytesDeclaration, returnStatement));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,7 +136,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
shaderInfoWithErrors
|
||||
.Select(static (item, _) => item.Diagnostcs));
|
||||
|
||||
// Get the EffectDisplayName info (hierarchy)
|
||||
// Get the EffectDisplayName and EffectId info (hierarchy)
|
||||
IncrementalValuesProvider<HierarchyInfo> hierarchyInfo =
|
||||
shaderInfoWithErrors
|
||||
.Select(static (item, _) => item.Hierarchy);
|
||||
@@ -150,6 +150,16 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
context.AddSource($"{item.FullyQualifiedMetadataName}.{nameof(EffectDisplayName)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
|
||||
});
|
||||
|
||||
// Generate the EffectId properties
|
||||
context.RegisterSourceOutput(hierarchyInfo, static (context, item) =>
|
||||
{
|
||||
PropertyDeclarationSyntax effectDisplayNameProperty = EffectId.GetSyntax(item, out Func<SyntaxNode, SourceText> fixup);
|
||||
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item, effectDisplayNameProperty, canUseSkipLocalsInit: false);
|
||||
SourceText text = fixup(compilationUnit);
|
||||
|
||||
context.AddSource($"{item.FullyQualifiedMetadataName}.{nameof(EffectId)}.g.cs", text);
|
||||
});
|
||||
|
||||
// Get the GetInputCount() info (hierarchy and input count)
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo =
|
||||
shaderInfoWithErrors
|
||||
|
||||
@@ -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 effect id of the D2D effect using this shader.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This only applies to effects created from <see cref="Interop.D2D1PixelShaderEffect"/>.
|
||||
/// </remarks>
|
||||
ref readonly Guid EffectId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the display name of the D2D effect using this shader.
|
||||
/// </summary>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする