Fix codegen for dynamic shaders

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-19 16:45:13 +01:00
1 親 16ef5f82e9
コミット 0675dd667c
2ファイル変更
+52 -76

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

+15 -7
ファイルの表示
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
@@ -15,16 +16,23 @@ public static class ShaderCompiler
/// <summary>
/// Compiles a new HLSL shader from the input source code.
/// </summary>
/// <param name="builder">The <see cref="ArrayPoolStringBuilder"/> instance with the HLSL source code to compile.</param>
/// <returns>The bytecode for the compiled shader.</returns>
public static unsafe IntPtr CompileShader(ref ArrayPoolStringBuilder builder)
/// <typeparam name="TBytecodeLoader">The type of bytecode loader being used.</typeparam>
/// <typeparam name="T">The type of shader being dispatched.</typeparam>
/// <param name="loader">The <typeparamref name="TBytecodeLoader"/> instance to use to load the bytecode.</param>
/// <param name="threadsX">The number of threads in each thread group for the X axis.</param>
/// <param name="threadsY">The number of threads in each thread group for the Y axis.</param>
/// <param name="threadsZ">The number of threads in each thread group for the Z axis.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the compute shader to run.</param>
public static unsafe void LoadDynamicBytecode<TBytecodeLoader, T>(ref TBytecodeLoader loader, int threadsX, int threadsY, int threadsZ, in T shader)
where TBytecodeLoader : struct, IBytecodeLoader
where T : struct, IShader
{
using ComPtr<IDxcBlob> shader = Shaders.Translation.ShaderCompiler.Instance.CompileShader(builder.WrittenSpan);
Unsafe.AsRef(in shader).BuildHlslString(out ArrayPoolStringBuilder builder, threadsX, threadsY, threadsZ);
using ComPtr<IDxcBlob> dxcBlobBytecode = Shaders.Translation.ShaderCompiler.Instance.CompileShader(builder.WrittenSpan);
builder.Dispose();
_ = shader.Get()->AddRef();
return (IntPtr)shader.Get();
loader.LoadDynamicBytecode((IntPtr)dxcBlobBytecode.Get());
}
}
+37 -69
ファイルの表示
@@ -1,7 +1,5 @@
using System;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Linq;
using ComputeSharp.__Internals;
using ComputeSharp.SourceGenerators.Models;
using Microsoft.CodeAnalysis;
@@ -79,7 +77,7 @@ public sealed partial class IShaderGenerator
if (supportsDynamicShaders)
{
// Return the dynamic compilation path
return Block(GetDynamicCompilationStatements());
return Block(GetDynamicCompilationStatement());
}
else
{
@@ -146,65 +144,40 @@ public sealed partial class IShaderGenerator
// If dynamic shaders are supported, return the combined block
if (supportsDynamicShaders)
{
return Block(embeddedBranch).AddStatements(GetDynamicCompilationStatements().ToArray());
return Block(embeddedBranch.WithElse(ElseClause(Block(GetDynamicCompilationStatement()))));
}
// If there is no dynamic shader support, add the faulting path after the branch
return Block(embeddedBranch).AddStatements(GetDynamicCompilationFaultingBlock());
return Block(embeddedBranch.WithElse(ElseClause(Block(GetDynamicCompilationFaultingBlock()))));
}
/// <summary>
/// The cached tree of dynamic statements.
/// </summary>
private static ImmutableArray<StatementSyntax> dynamicStatements;
private static StatementSyntax? dynamicStatement;
/// <summary>
/// Returns a sequence of <see cref="StatementSyntax"/> instances to create a new dynamic shader.
/// Returns the <see cref="StatementSyntax"/> instance to create a new dynamic shader.
/// </summary>
/// <returns>A sequence of <see cref="StatementSyntax"/> instances to create a new dynamic shader.</returns>
private static ImmutableArray<StatementSyntax> GetDynamicCompilationStatements()
/// <returns>The <see cref="StatementSyntax"/> instance to create a new dynamic shader.</returns>
private static StatementSyntax GetDynamicCompilationStatement()
{
if (dynamicStatements.IsDefault)
{
// This code produces a method declaration as follows:
//
// BuildHlslString(out global::ComputeSharp.__Internals.ArrayPoolStringBuilder builder, threadsX, threadsY, threadsZ);
// global::System.IntPtr bytecode = ShaderCompiler.CompileShader(ref builder);
// loader.LoadDynamicBytecode(bytecode);
dynamicStatements = ImmutableArray.Create<StatementSyntax>(
ExpressionStatement(
InvocationExpression(IdentifierName("BuildHlslString"))
.AddArgumentListArguments(
Argument(DeclarationExpression(
IdentifierName("global::ComputeSharp.__Internals.ArrayPoolStringBuilder"),
SingleVariableDesignation(Identifier("builder"))))
.WithRefOrOutKeyword(Token(SyntaxKind.OutKeyword)),
Argument(IdentifierName("threadsX")),
Argument(IdentifierName("threadsY")),
Argument(IdentifierName("threadsZ")))),
LocalDeclarationStatement(
VariableDeclaration(IdentifierName("global::System.IntPtr"))
.AddVariables(VariableDeclarator(Identifier("bytecode"))
.WithInitializer(
EqualsValueClause(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("ShaderCompiler"),
IdentifierName("CompileShader")))
.AddArgumentListArguments(
Argument(IdentifierName("builder"))
.WithRefKindKeyword(Token(SyntaxKind.RefKeyword))))))),
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("loader"),
IdentifierName("LoadDynamicBytecode")))
.AddArgumentListArguments(Argument(IdentifierName("bytecode")))));;
}
return dynamicStatements;
// 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")).WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)),
Argument(IdentifierName("threadsX")),
Argument(IdentifierName("threadsY")),
Argument(IdentifierName("threadsZ")),
Argument(ThisExpression()).WithRefOrOutKeyword(Token(SyntaxKind.InKeyword))));
}
/// <summary>
@@ -218,25 +191,20 @@ public sealed partial class IShaderGenerator
/// <returns>A <see cref="ExpressionStatementSyntax"/> instances to perform a faulting call to <c>LoadDynamicBytecode(IntPtr)</c>.</returns>
private static ExpressionStatementSyntax GetDynamicCompilationFaultingBlock()
{
if (dynamicFaultStatement is null)
{
// This code produces a faulting call as follows:
//
// loader.LoadDynamicBytecode(default);
return
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("loader"),
IdentifierName("LoadDynamicBytecode")))
.AddArgumentListArguments(
Argument(LiteralExpression(
SyntaxKind.DefaultLiteralExpression,
Token(SyntaxKind.DefaultKeyword)))));
}
return dynamicFaultStatement;
// 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)))));
}
/// <summary>