Remove checks for dynamic shader in source generator

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-13 12:11:55 +02:00
1 親 490226aa4d
コミット 2e020a1524
6ファイル変更
+19 -155

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

@@ -22,10 +22,9 @@ partial class IShaderGenerator
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>BuildHlslSource</c> method.
/// </summary>
/// <param name="hlslSourceInfo">The input <see cref="HlslShaderSourceInfo"/> instance to use.</param>
/// <param name="supportsDynamicShaders">Indicates whether or not dynamic shaders are supported.</param>
/// <param name="hierarchyDepth">The depth of the hierarchy for this type (used to calculate the right indentation).</param>
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>BuildHlslSource</c> method.</returns>
public static MethodDeclarationSyntax GetSyntax(HlslShaderSourceInfo hlslSourceInfo, bool supportsDynamicShaders, int hierarchyDepth)
public static MethodDeclarationSyntax GetSyntax(HlslShaderSourceInfo hlslSourceInfo, int hierarchyDepth)
{
// Generate the necessary body statements depending on whether dynamic shaders are supported
ImmutableArray<StatementSyntax> bodyStatements = GenerateRenderMethodBody(hlslSourceInfo, hierarchyDepth);
+6 -78
ファイルの表示
@@ -23,12 +23,11 @@ partial class IShaderGenerator
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>TryGetBytecode</c> method.
/// </summary>
/// <param name="bytecodeInfo">The input <see cref="EmbeddedBytecodeInfo"/> instance.</param>
/// <param name="supportsDynamicShaders">Indicates whether or not dynamic shaders are supported.</param>
/// <param name="fixup">An opaque <see cref="Func{TResult}"/> instance to transform the final tree into text.</param>
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>BuildHlslSource</c> method.</returns>
public static MethodDeclarationSyntax GetSyntax(EmbeddedBytecodeInfo bytecodeInfo, bool supportsDynamicShaders, out Func<SyntaxNode, SourceText> fixup)
public static MethodDeclarationSyntax GetSyntax(EmbeddedBytecodeInfo bytecodeInfo, out Func<SyntaxNode, SourceText> fixup)
{
BlockSyntax block = GetShaderBytecodeBody(bytecodeInfo, supportsDynamicShaders, out string? bytecodeLiterals);
BlockSyntax block = GetShaderBytecodeBody(bytecodeInfo, out string? bytecodeLiterals);
if (bytecodeLiterals is not null)
{
@@ -66,25 +65,16 @@ partial class IShaderGenerator
/// Gets a <see cref="BlockSyntax"/> instance with the logic to try to get a compiled shader bytecode.
/// </summary>
/// <param name="bytecodeInfo">The input <see cref="EmbeddedBytecodeInfo"/> instance.</param>
/// <param name="supportsDynamicShaders">Indicates whether or not dynamic shaders are supported.</param>
/// <param name="bytecodeLiterals">The resulting bytecode literals to insert into the final source code.</param>
/// <returns>The <see cref="BlockSyntax"/> instance trying to retrieve the precompiled shader.</returns>
private static unsafe BlockSyntax GetShaderBytecodeBody(EmbeddedBytecodeInfo bytecodeInfo, bool supportsDynamicShaders, out string? bytecodeLiterals)
private static unsafe BlockSyntax GetShaderBytecodeBody(EmbeddedBytecodeInfo bytecodeInfo, out string? bytecodeLiterals)
{
if (bytecodeInfo.Bytecode.IsEmpty)
{
bytecodeLiterals = null;
if (supportsDynamicShaders)
{
// Return the dynamic compilation path
return Block(GetDynamicCompilationStatement());
}
else
{
// Faulting path
return Block(GetDynamicCompilationFaultingBlock());
}
// Faulting path
return Block();
}
bytecodeLiterals = SyntaxFormattingHelper.BuildByteArrayInitializationExpressionString(bytecodeInfo.Bytecode.AsSpan());
@@ -142,70 +132,8 @@ partial class IShaderGenerator
IdentifierName("LoadEmbeddedBytecode")))
.AddArgumentListArguments(Argument(IdentifierName("bytecode"))))));
// If dynamic shaders are supported, return the combined block
if (supportsDynamicShaders)
{
return Block(embeddedBranch.WithElse(ElseClause(Block(GetDynamicCompilationStatement()))));
}
// If there is no dynamic shader support, add the faulting path after the branch
return Block(embeddedBranch.WithElse(ElseClause(Block(GetDynamicCompilationFaultingBlock()))));
}
/// <summary>
/// The cached tree of dynamic statements.
/// </summary>
private static StatementSyntax? dynamicStatement;
/// <summary>
/// Returns the <see cref="StatementSyntax"/> instance to create a new dynamic shader.
/// </summary>
/// <returns>The <see cref="StatementSyntax"/> instance to create a new dynamic shader.</returns>
private static StatementSyntax GetDynamicCompilationStatement()
{
// This code produces a method declaration as follows:
//
// ComputeSharp.__Internals.ShaderCompiler.LoadDynamicBytecode(ref loader, threadsX, threadsY, threadsZ, in this);
return dynamicStatement ??=
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("ComputeSharp.__Internals.ShaderCompiler"),
IdentifierName("LoadDynamicBytecode")))
.AddArgumentListArguments(
Argument(IdentifierName("loader")).WithRefKindKeyword(Token(SyntaxKind.RefKeyword)),
Argument(IdentifierName("threadsX")),
Argument(IdentifierName("threadsY")),
Argument(IdentifierName("threadsZ")),
Argument(ThisExpression()).WithRefKindKeyword(Token(SyntaxKind.InKeyword))));
}
/// <summary>
/// The cached tree of dynamic fault statement.
/// </summary>
private static ExpressionStatementSyntax? dynamicFaultStatement;
/// <summary>
/// Returns a <see cref="ExpressionStatementSyntax"/> instances to perform a faulting call to <c>LoadDynamicBytecode(IntPtr)</c>.
/// </summary>
/// <returns>A <see cref="ExpressionStatementSyntax"/> instances to perform a faulting call to <c>LoadDynamicBytecode(IntPtr)</c>.</returns>
private static ExpressionStatementSyntax GetDynamicCompilationFaultingBlock()
{
// This code produces a faulting call as follows:
//
// loader.LoadDynamicBytecode(default);
return dynamicFaultStatement ??=
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("loader"),
IdentifierName("LoadDynamicBytecode")))
.AddArgumentListArguments(
Argument(LiteralExpression(
SyntaxKind.DefaultLiteralExpression,
Token(SyntaxKind.DefaultKeyword)))));
return Block(embeddedBranch.WithElse(ElseClause(Block())));
}
}
}
+10 -17
ファイルの表示
@@ -22,11 +22,6 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
/// <inheritdoc/>
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// Check whether or not dynamic shaders are supported
IncrementalValueProvider<bool> supportsDynamicShaders =
context.CompilationProvider
.Select(static (compilation, _) => false);
// Discover all shader types and extract all the necessary info from each of them
IncrementalValuesProvider<ShaderInfo> shaderInfoWithErrors =
context.SyntaxProvider
@@ -130,18 +125,17 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
});
// Get the BuildHlslSource info (hierarchy, HLSL source and parsing options)
IncrementalValuesProvider<((HierarchyInfo Hierarchy, HlslShaderSourceInfo HlslShaderSource) Left, bool SupportsDynamicShaders)> hlslSourceInfo =
IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderSourceInfo HlslShaderSource)> hlslSourceInfo =
shaderInfoWithErrors
.Select(static (item, _) => (item.Hierarchy, item.HlslShaderSource))
.Combine(supportsDynamicShaders);
.Select(static (item, _) => (item.Hierarchy, item.HlslShaderSource));
// Generate the BuildHlslSource() methods
context.RegisterSourceOutput(hlslSourceInfo, static (context, item) =>
{
MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.Left.HlslShaderSource, item.SupportsDynamicShaders, item.Left.Hierarchy.Hierarchy.Length);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Left.Hierarchy, buildHlslStringMethod, addSkipLocalsInitAttribute: true);
MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.HlslShaderSource, item.Hierarchy.Hierarchy.Length);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, buildHlslStringMethod, addSkipLocalsInitAttribute: true);
context.AddSource($"{item.Left.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
// Get the LoadDispatchMetadata() info (hierarchy and dispatch metadata info)
@@ -198,19 +192,18 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
context.ReportDiagnostics(embeddedBytecodeDiagnostics);
// Get the TryGetBytecode() info (hierarchy and compiled bytecode)
IncrementalValuesProvider<((HierarchyInfo Hierarchy, EmbeddedBytecodeInfo EmbeddedBytecode) Info, bool SupportsDynamicShaders)> embeddedBytecodeInfo =
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo EmbeddedBytecode)> embeddedBytecodeInfo =
embeddedBytecodeWithErrors
.Select(static (item, token) => (item.Hierarchy, item.EmbeddedBytecode))
.Combine(supportsDynamicShaders);
.Select(static (item, token) => (item.Hierarchy, item.EmbeddedBytecode));
// Generate the TryGetBytecode() methods
context.RegisterSourceOutput(embeddedBytecodeInfo, static (context, item) =>
{
MethodDeclarationSyntax tryGetBytecodeMethod = LoadBytecode.GetSyntax(item.Info.EmbeddedBytecode, item.SupportsDynamicShaders, out Func<SyntaxNode, SourceText> fixup);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Info.Hierarchy, tryGetBytecodeMethod, addSkipLocalsInitAttribute: false);
MethodDeclarationSyntax tryGetBytecodeMethod = LoadBytecode.GetSyntax(item.EmbeddedBytecode, out Func<SyntaxNode, SourceText> fixup);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, tryGetBytecodeMethod, addSkipLocalsInitAttribute: false);
SourceText text = fixup(compilationUnit);
context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadBytecode)}.g.cs", text);
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadBytecode)}.g.cs", text);
});
}
}