Enable generation for ID2D1Shader.GetOutputBuffer

このコミットが含まれているのは:
Sergio Pedri committed 2022-05-07 21:24:12 +02:00
1 親 c554f56bfa
コミット 80fea25b5a
15ファイル変更
+171 -19

検索条件に一致するファイルはありません

+2
ファイルの表示
@@ -65,6 +65,8 @@
<!-- ComputeSharp.D2D1 APIs -->
<Compile Include="..\ComputeSharp.D2D1\Attributes\Enums\D2D1ShaderProfile.cs" Link="ComputeSharp.D2D1\Attributes\Enums\D2D1ShaderProfile.cs" />
<Compile Include="..\ComputeSharp.D2D1\Attributes\Enums\D2D1BufferPrecision.cs" Link="ComputeSharp.D2D1\Attributes\Enums\D2D1BufferPrecision.cs" />
<Compile Include="..\ComputeSharp.D2D1\Attributes\Enums\D2D1ChannelDepth.cs" Link="ComputeSharp.D2D1\Attributes\Enums\D2D1ChannelDepth.cs" />
<Compile Include="..\ComputeSharp.D2D1\Attributes\D2DEmbeddedBytecodeAttribute.cs" Link="ComputeSharp.D2D1\Attributes\D2DEmbeddedBytecodeAttribute.cs" />
<Compile Include="..\ComputeSharp.D2D1\Attributes\D2DInputComplexAttribute.cs" Link="ComputeSharp.D2D1\Attributes\D2DInputComplexAttribute.cs" />
<Compile Include="..\ComputeSharp.D2D1\Attributes\D2DInputCountAttribute.cs" Link="ComputeSharp.D2D1\Attributes\D2DInputCountAttribute.cs" />
@@ -27,7 +27,7 @@ partial class ID2D1ShaderGenerator
// hlslSource = <HLSL_SOURCE>;
// }
return
MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("BuildHlslSource"))
MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(BuildHlslSource)))
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
.AddParameterListParameters(
@@ -400,15 +400,7 @@ partial class ID2D1ShaderGenerator
/// <remarks>Whether <paramref name="structDeclarationSymbol"/> requires the scene position.</remarks>
private static bool GetRequiresScenePositionInfo(INamedTypeSymbol structDeclarationSymbol)
{
foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes())
{
if (attributeData.AttributeClass?.GetFullMetadataName() is "ComputeSharp.D2D1.D2DRequiresScenePositionAttribute")
{
return true;
}
}
return false;
return structDeclarationSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DRequiresScenePositionAttribute", out _);
}
/// <summary>
@@ -29,7 +29,7 @@ partial class ID2D1ShaderGenerator
// return <INPUT_COUNT>;
// }
return
MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier("GetInputCount"))
MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier(nameof(GetInputCount)))
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
.WithBody(Block(ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputCount)))));
@@ -28,7 +28,7 @@ partial class ID2D1ShaderGenerator
// return <INPUT_TYPE>;
// }
return
MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier("GetInputType"))
MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier(nameof(GetInputType)))
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
.AddParameterListParameters(Parameter(Identifier("index")).WithType(PredefinedType(Token(SyntaxKind.UIntKeyword))))
@@ -12,7 +12,7 @@ namespace ComputeSharp.D2D1.SourceGenerators;
partial class ID2D1ShaderGenerator
{
/// <summary>
/// A helper with all logic to generate the <c>GetInputTypeMethod</c> method.
/// A helper with all logic to generate the <c>GetInputType</c> method.
/// </summary>
private static partial class GetInputType
{
@@ -0,0 +1,51 @@
using ComputeSharp.D2D1.__Internals;
using ComputeSharp.D2D1.SourceGenerators.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 GetOutputBuffer
{
/// <summary>
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>GetOutputBuffer</c> method.
/// </summary>
/// <param name="info">The input info for the shader output buffer.</param>
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>GetOutputBuffer</c> method.</returns>
public static MethodDeclarationSyntax GetSyntax(OutputBufferInfo info)
{
// This code produces a method declaration as follows:
//
// readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetOutputBuffer(out uint precision, out uint depth)
// {
// precision = <BUFFER_PRECISION>;
// depth = <CHANNEL_DEPTH>;
// }
return
MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(GetOutputBuffer)))
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
.AddParameterListParameters(
Parameter(Identifier("precision")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(PredefinedType(Token(SyntaxKind.UIntKeyword))),
Parameter(Identifier("depth")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(PredefinedType(Token(SyntaxKind.UIntKeyword))))
.WithBody(Block(
ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName("precision"),
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)info.BufferPrecision)))),
ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName("depth"),
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((int)info.ChannelDepth))))));
}
}
}
@@ -0,0 +1,62 @@
using ComputeSharp.SourceGeneration.Extensions;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.D2D1.SourceGenerators;
/// <inheritdoc/>
partial class ID2D1ShaderGenerator
{
/// <summary>
/// A helper with all logic to generate the <c>GetOutputBuffer</c> method.
/// </summary>
private static partial class GetOutputBuffer
{
/// <summary>
/// Extracts the output buffer info for the current shader.
/// </summary>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <param name="bufferPrecision">The output buffer precision for the shader.</param>
/// <param name="channelDepth">The output buffer channel depth for the shader.</param>
public static void GetInfo(
INamedTypeSymbol structDeclarationSymbol,
out D2D1BufferPrecision bufferPrecision,
out D2D1ChannelDepth channelDepth)
{
// Try to get the [D2DOutputBuffer] attribute
if (!structDeclarationSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DOutputBufferAttribute", out AttributeData? attributeData))
{
bufferPrecision = D2D1BufferPrecision.Unknown;
channelDepth = D2D1ChannelDepth.Default;
return;
}
// Extract the buffer precision and channel depth, if available
if (attributeData!.ConstructorArguments.Length == 1)
{
if (attributeData.AttributeConstructor is IMethodSymbol { Parameters.Length: 1 } constructorSymbol &&
constructorSymbol.Parameters[0].Type.HasFullyQualifiedName("ComputeSharp.D2D1.D2D1BufferPrecision"))
{
bufferPrecision = (D2D1BufferPrecision)attributeData.ConstructorArguments[0].Value!;
channelDepth = D2D1ChannelDepth.Default;
}
else
{
bufferPrecision = D2D1BufferPrecision.Unknown;
channelDepth = (D2D1ChannelDepth)attributeData.ConstructorArguments[0].Value!;
}
}
else if (attributeData.ConstructorArguments.Length == 2)
{
bufferPrecision = (D2D1BufferPrecision)attributeData.ConstructorArguments[0].Value!;
channelDepth = (D2D1ChannelDepth)attributeData.ConstructorArguments[1].Value!;
}
else
{
// The syntax tree is likely invalid, so just ignore it for now
bufferPrecision = D2D1BufferPrecision.Unknown;
channelDepth = D2D1ChannelDepth.Default;
}
}
}
}
@@ -29,7 +29,7 @@ partial class ID2D1ShaderGenerator
// <BODY>
// }
return
MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("InitializeFromDispatchData"))
MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(InitializeFromDispatchData)))
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
.AddParameterListParameters(Parameter(Identifier("data")).WithType(
GenericName(Identifier("global::System.ReadOnlySpan"))
@@ -44,9 +44,7 @@ partial class ID2D1ShaderGenerator
// <BODY>
// }
return
MethodDeclaration(
PredefinedType(Token(SyntaxKind.VoidKeyword)),
Identifier("LoadBytecode"))
MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(LoadBytecode)))
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
.AddTypeParameterListParameters(TypeParameter(Identifier("TLoader")))
@@ -21,7 +21,9 @@ namespace ComputeSharp.D2D1.SourceGenerators;
/// <inheritdoc/>
partial class ID2D1ShaderGenerator
{
/// <inheritodoc/>
/// <summary>
/// A helper with all logic to generate the <c>LoadBytecode</c> method.
/// </summary>
private static partial class LoadBytecode
{
/// <summary>
@@ -29,7 +29,7 @@ partial class ID2D1ShaderGenerator
// <BODY>
// }
return
MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("LoadDispatchData"))
MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(LoadDispatchData)))
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
.AddTypeParameterListParameters(TypeParameter(Identifier("TLoader")))
+26
ファイルの表示
@@ -243,5 +243,31 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(LoadBytecode)}", text);
});
// Get the output buffer data, which can be computed separately from all other generation steps.
// Caching at a fine grained level can be enabled immediately, as both models are comparable.
IncrementalValuesProvider<(HierarchyInfo Hierarchy, OutputBufferInfo Info)> outputBufferInfo =
shaderDeclarations
.Select(static (item, token) =>
{
// GetOutputBuffer() info
GetOutputBuffer.GetInfo(item.Symbol, out D2D1BufferPrecision bufferPrecision, out D2D1ChannelDepth channelDepth);
token.ThrowIfCancellationRequested();
OutputBufferInfo outputBufferInfo = new(bufferPrecision, channelDepth);
return (item.Hierarchy, outputBufferInfo);
})
.WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer<OutputBufferInfo>.Default);
// Generate the GetOutputBuffer() methods
context.RegisterSourceOutput(outputBufferInfo, static (context, item) =>
{
MethodDeclarationSyntax getOutputBufferMethod = GetOutputBuffer.GetSyntax(item.Info);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getOutputBufferMethod, false);
context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetOutputBuffer)}", compilationUnit.ToFullString());
});
}
}
+8
ファイルの表示
@@ -0,0 +1,8 @@
namespace ComputeSharp.D2D1.SourceGenerators.Models;
/// <summary>
/// A model representing the output buffer info for a shader.
/// </summary>
/// <param name="BufferPrecision">The buffer precision for the resulting output buffer.</param>
/// <param name="ChannelDepth">The channel depth for the resulting output buffer.</param>
internal sealed record OutputBufferInfo(D2D1BufferPrecision BufferPrecision, D2D1ChannelDepth ChannelDepth);
+11
ファイルの表示
@@ -40,6 +40,17 @@ internal static class ISymbolExtensions
return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat);
}
/// <summary>
/// Checks whether or not a given type symbol has a specified full name.
/// </summary>
/// <param name="symbol">The input <see cref="ISymbol"/> instance to check.</param>
/// <param name="name">The full name to check.</param>
/// <returns>Whether <paramref name="symbol"/> has a full name equals to <paramref name="name"/>.</returns>
public static bool HasFullyQualifiedName(this ISymbol symbol, string name)
{
return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat) == name;
}
/// <summary>
/// Gets a valid filename for a given <see cref="ITypeSymbol"/> instance.
/// </summary>