Implement dynamic D2D1 compilation generation
このコミットが含まれているのは:
1 親
f95079ea51
コミット
c1e795129e
11ファイル変更
+126
-83
検索条件に一致するファイルはありません
+69
-66
@@ -39,7 +39,7 @@ partial class ID2D1ShaderGenerator
|
||||
|
||||
// This code produces a method declaration as follows:
|
||||
//
|
||||
// readonly void global::ComputeSharp.D2D1Interop.__Internals.ID2D1Shader.LoadBytecode<TLoader>(ref TLoader loader, global::ComputeSharp.D2D1Interop.D2D1ShaderProfile? shaderProfile, out string hlslSource)
|
||||
// readonly void global::ComputeSharp.D2D1Interop.__Internals.ID2D1Shader.LoadBytecode<TLoader>(ref TLoader loader, global::ComputeSharp.D2D1Interop.D2D1ShaderProfile? shaderProfile)
|
||||
// {
|
||||
// <BODY>
|
||||
// }
|
||||
@@ -52,8 +52,7 @@ partial class ID2D1ShaderGenerator
|
||||
.AddTypeParameterListParameters(TypeParameter(Identifier("TLoader")))
|
||||
.AddParameterListParameters(
|
||||
Parameter(Identifier("loader")).AddModifiers(Token(SyntaxKind.RefKeyword)).WithType(IdentifierName("TLoader")),
|
||||
Parameter(Identifier("shaderProfile")).WithType(NullableType(IdentifierName("global::ComputeSharp.D2D1Interop.D2D1ShaderProfile"))),
|
||||
Parameter(Identifier("hlslSource")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(PredefinedType(Token(SyntaxKind.StringKeyword))))
|
||||
Parameter(Identifier("shaderProfile")).WithType(NullableType(IdentifierName("global::ComputeSharp.D2D1Interop.D2D1ShaderProfile"))))
|
||||
.WithBody(block);
|
||||
}
|
||||
|
||||
@@ -65,79 +64,83 @@ partial class ID2D1ShaderGenerator
|
||||
/// <returns>The <see cref="BlockSyntax"/> instance trying to retrieve the precompiled shader.</returns>
|
||||
private static unsafe BlockSyntax GetShaderBytecodeBody(EmbeddedBytecodeInfo bytecodeInfo, out string? bytecodeLiterals)
|
||||
{
|
||||
ImmutableArray<StatementSyntax>.Builder statements = ImmutableArray.CreateBuilder<StatementSyntax>();
|
||||
|
||||
// This code produces a branch as follows:
|
||||
// This code produces a method declaration as follows:
|
||||
//
|
||||
// hlslSource = <HLSL_SOURCE>;
|
||||
statements.Add(
|
||||
// ComputeSharp.D2D1Interop.__Internals.D2D1ShaderCompiler.LoadDynamicBytecode(ref loader, in this, shaderProfile, <ENABLE_LINKING_SUPPORT>);
|
||||
ExpressionStatementSyntax dynamicLoadingStatement =
|
||||
ExpressionStatement(
|
||||
AssignmentExpression(
|
||||
SyntaxKind.SimpleAssignmentExpression,
|
||||
IdentifierName("hlslSource"),
|
||||
LiteralExpression(
|
||||
SyntaxKind.StringLiteralExpression,
|
||||
Literal(bytecodeInfo.HlslSource)))));
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("ComputeSharp.D2D1Interop.__Internals.D2D1ShaderCompiler"),
|
||||
IdentifierName("LoadDynamicBytecode")))
|
||||
.AddArgumentListArguments(
|
||||
Argument(IdentifierName("loader")).WithRefKindKeyword(Token(SyntaxKind.RefKeyword)),
|
||||
Argument(ThisExpression()).WithRefKindKeyword(Token(SyntaxKind.InKeyword)),
|
||||
Argument(IdentifierName("shaderProfile")),
|
||||
Argument(LiteralExpression(SyntaxKind.TrueLiteralExpression))));
|
||||
|
||||
// Load the bytecode literals, if available, otherwise emit the dynamic loading code
|
||||
if (bytecodeInfo.Bytecode.IsEmpty)
|
||||
// If there is no precompiled bytecode, just return the dynamic path
|
||||
if (bytecodeInfo.Bytecode.IsDefaultOrEmpty)
|
||||
{
|
||||
bytecodeLiterals = null;
|
||||
|
||||
// This code produces a method declaration as follows:
|
||||
//
|
||||
// ComputeSharp.D2D1Interop.__Internals.D2D1ShaderCompiler.LoadDynamicBytecode(ref loader, hlslSource);
|
||||
statements.Add(
|
||||
ExpressionStatement(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("ComputeSharp.D2D1Interop.__Internals.D2D1ShaderCompiler"),
|
||||
IdentifierName("LoadDynamicBytecode")))
|
||||
.AddArgumentListArguments(
|
||||
Argument(IdentifierName("loader")).WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)),
|
||||
Argument(IdentifierName("hlslSource")))));
|
||||
return Block(dynamicLoadingStatement);
|
||||
}
|
||||
else
|
||||
{
|
||||
bytecodeLiterals = BuildShaderBytecodeExpressionString(bytecodeInfo.Bytecode.AsSpan());
|
||||
|
||||
// This code produces the following statements:
|
||||
//
|
||||
// global::System.ReadOnlySpan<byte> bytecode = new byte[] { __EMBEDDED_SHADER_BYTECODE };
|
||||
//
|
||||
// Note that the __EMBEDDED_SHADER_BYTECODE identifier will be replaced after the string
|
||||
// is created by the generator. This greatly speeds up the code generation, as it avoids
|
||||
// having to create/parse tens of thousands of literal expression nodes for every shader.
|
||||
statements.Add(
|
||||
LocalDeclarationStatement(
|
||||
VariableDeclaration(
|
||||
GenericName(Identifier("global::System.ReadOnlySpan"))
|
||||
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))))
|
||||
.AddVariables(
|
||||
VariableDeclarator(Identifier("bytecode"))
|
||||
.WithInitializer(
|
||||
EqualsValueClause(
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
|
||||
ImmutableArray<StatementSyntax>.Builder statements = ImmutableArray.CreateBuilder<StatementSyntax>();
|
||||
|
||||
bytecodeLiterals = BuildShaderBytecodeExpressionString(bytecodeInfo.Bytecode.AsSpan());
|
||||
|
||||
// This code produces a branch as follows:
|
||||
//
|
||||
// if (shaderProfile is null or <SHADER_PROFILE>)
|
||||
// {
|
||||
// global::System.ReadOnlySpan<byte> bytecode = new byte[] { __EMBEDDED_SHADER_BYTECODE };
|
||||
// loader.LoadEmbeddedBytecode(bytecode);
|
||||
// }
|
||||
//
|
||||
// Note that the __EMBEDDED_SHADER_BYTECODE identifier will be replaced after the string
|
||||
// is created by the generator. This greatly speeds up the code generation, as it avoids
|
||||
// having to create/parse tens of thousands of literal expression nodes for every shader.
|
||||
IfStatementSyntax embeddedBranch =
|
||||
IfStatement(
|
||||
IsPatternExpression(
|
||||
IdentifierName("shaderProfile"),
|
||||
BinaryPattern(
|
||||
SyntaxKind.OrPattern,
|
||||
ConstantPattern(LiteralExpression(SyntaxKind.NullLiteralExpression)),
|
||||
ConstantPattern(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName(nameof(D2D1ShaderProfile)),
|
||||
IdentifierName(D2D1ShaderProfile.PixelShader50.ToString()))))),
|
||||
Block(
|
||||
LocalDeclarationStatement(
|
||||
VariableDeclaration(
|
||||
GenericName(Identifier("global::System.ReadOnlySpan"))
|
||||
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))))
|
||||
.AddVariables(
|
||||
VariableDeclarator(Identifier("bytecode"))
|
||||
.WithInitializer(
|
||||
InitializerExpression(
|
||||
SyntaxKind.ArrayInitializerExpression,
|
||||
SingletonSeparatedList<ExpressionSyntax>(IdentifierName("__EMBEDDED_SHADER_BYTECODE")))))))));
|
||||
EqualsValueClause(
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
|
||||
.WithInitializer(
|
||||
InitializerExpression(
|
||||
SyntaxKind.ArrayInitializerExpression,
|
||||
SingletonSeparatedList<ExpressionSyntax>(IdentifierName("__EMBEDDED_SHADER_BYTECODE")))))))),
|
||||
ExpressionStatement(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("loader"),
|
||||
IdentifierName("LoadEmbeddedBytecode")))
|
||||
.AddArgumentListArguments(Argument(IdentifierName("bytecode"))))));
|
||||
|
||||
// loader.LoadEmbeddedBytecode(bytecode);
|
||||
statements.Add(
|
||||
ExpressionStatement(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("loader"),
|
||||
IdentifierName("LoadEmbeddedBytecode")))
|
||||
.AddArgumentListArguments(Argument(IdentifierName("bytecode")))));
|
||||
}
|
||||
|
||||
return Block(statements);
|
||||
// Return the combined block
|
||||
return Block(embeddedBranch.WithElse(ElseClause(Block(dynamicLoadingStatement))));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace ComputeSharp.__Internals;
|
||||
namespace ComputeSharp.D2D1Interop.__Internals;
|
||||
|
||||
/// <summary>
|
||||
/// A base <see langword="interface"/> representing a bytecode loader for a D2D1 shader.
|
||||
@@ -14,7 +14,7 @@ public interface ID2D1BytecodeLoader
|
||||
/// Loads a dynamic shader bytecode.
|
||||
/// </summary>
|
||||
/// <param name="handle">An opaque handle to the shader bytecode.</param>
|
||||
/// <exception cref="InvalidOperationException">Thrown if the shader has already been initialized or a precompiled bytecode was requested when not available..</exception>
|
||||
/// <exception cref="InvalidOperationException">Thrown if the shader has already been initialized or a precompiled bytecode was requested when not available.</exception>
|
||||
void LoadDynamicBytecode(IntPtr handle);
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace ComputeSharp.__Internals;
|
||||
namespace ComputeSharp.D2D1Interop.__Internals;
|
||||
|
||||
/// <summary>
|
||||
/// A base <see langword="interface"/> representing a data loader for a D2D1 shader being dispatched.
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using ComputeSharp.__Internals;
|
||||
|
||||
namespace ComputeSharp.D2D1Interop.__Internals;
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.__Internals;
|
||||
using ComputeSharp.D2D1Interop.__Internals;
|
||||
using TerraFX.Interop.DirectX;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.__Internals;
|
||||
using ComputeSharp.D2D1Interop.__Internals;
|
||||
using TerraFX.Interop.DirectX;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.__Internals;
|
||||
using ComputeSharp.D2D1Interop.__Internals;
|
||||
using ComputeSharp.D2D1Interop.Extensions;
|
||||
using ComputeSharp.D2D1Interop.Helpers;
|
||||
using TerraFX.Interop.DirectX;
|
||||
@@ -9,7 +9,7 @@ using TerraFX.Interop.Windows;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.Shaders.Dispatching;
|
||||
namespace ComputeSharp.D2D1Interop.Shaders.Dispatching;
|
||||
|
||||
/// <summary>
|
||||
/// A bytecode loader for D2D1 shaders.
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.D2D1Interop.Shaders.Dispatching;
|
||||
using ComputeSharp.D2D1Interop.Shaders.Interop.Buffers;
|
||||
using ComputeSharp.Shaders.Dispatching;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
@@ -20,10 +19,12 @@ public static unsafe class D2D1InteropServices
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to load the bytecode for.</typeparam>
|
||||
/// <returns>A <see cref="ReadOnlyMemory{T}"/> instance with the resulting shader bytecode.</returns>
|
||||
/// <exception cref="InvalidOperationException">Thrown if the input shader has not been precompiled.</exception>
|
||||
/// <remarks>
|
||||
/// This method will only run correctly if the input shader has been precompiled, or it will throw an exception.
|
||||
/// The returned <see cref="ReadOnlyMemory{T}"/> instance will wrap a pinned memory buffer (from the PE section).
|
||||
/// This method will only compile the shader using <see cref="D2D1ShaderProfile.PixelShader50"/> if no precompiled shader is available.
|
||||
/// <para>
|
||||
/// If the input shader was precompiled, the returned <see cref="ReadOnlyMemory{T}"/> instance will wrap a pinned memory buffer (from the PE section).
|
||||
/// If the shader was compiled at runtime, the returned <see cref="ReadOnlyMemory{T}"/> instance will wrap a <see cref="byte"/> array with the bytecode.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static ReadOnlyMemory<byte> LoadShaderBytecode<T>()
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
@@ -59,7 +60,7 @@ public static unsafe class D2D1InteropServices
|
||||
{
|
||||
D2D1ShaderBytecodeLoader bytecodeLoader = default;
|
||||
|
||||
Unsafe.NullRef<T>().LoadBytecode(ref bytecodeLoader, shaderProfile, out _);
|
||||
Unsafe.NullRef<T>().LoadBytecode(ref bytecodeLoader, shaderProfile ?? D2D1ShaderProfile.PixelShader50);
|
||||
|
||||
using ComPtr<ID3DBlob> dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan<byte> precompiledBytecode);
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.D2D1Interop.Extensions;
|
||||
using ComputeSharp.Shaders.Dispatching;
|
||||
using ComputeSharp.D2D1Interop.Shaders.Dispatching;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
@@ -23,9 +23,11 @@ public static class D2D1ReflectionServices
|
||||
public static unsafe D2D1ShaderInfo GetShaderInfo<T>()
|
||||
where T : struct, ID2D1PixelShader
|
||||
{
|
||||
Unsafe.NullRef<T>().BuildHlslSource(out string hlslSource);
|
||||
|
||||
D2D1ShaderBytecodeLoader bytecodeLoader = default;
|
||||
|
||||
Unsafe.NullRef<T>().LoadBytecode(ref bytecodeLoader, D2D1ShaderProfile.PixelShader50, out string hlslSource);
|
||||
Unsafe.NullRef<T>().LoadBytecode(ref bytecodeLoader, D2D1ShaderProfile.PixelShader50);
|
||||
|
||||
using ComPtr<ID3DBlob> dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan<byte> precompiledBytecode);
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace ComputeSharp.D2D1Interop.__Internals;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that uses the DXC APIs to compile compute shaders.
|
||||
/// </summary>
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
[Obsolete("This type is not intended to be used directly by user code")]
|
||||
public static class D2D1ShaderCompiler
|
||||
{
|
||||
/// <summary>
|
||||
/// Compiles a new HLSL shader from the input source code.
|
||||
/// </summary>
|
||||
/// <typeparam name="TLoader">The type of bytecode loader being used.</typeparam>
|
||||
/// <typeparam name="T">The type of D2D1 shader being dispatched.</typeparam>
|
||||
/// <param name="loader">The <typeparamref name="TLoader"/> instance to use to load the bytecode.</param>
|
||||
/// <param name="shader">The input <typeparamref name="T"/> instance representing the D2D1 shader to run.</param>
|
||||
/// <param name="shaderProfile">The shader profile to use to compile the shader.</param>
|
||||
/// <param name="enableLinkingSupport">Whether to enable linking support for the shader.</param>
|
||||
public static unsafe void LoadDynamicBytecode<TLoader, T>(ref TLoader loader, in T shader, D2D1ShaderProfile? shaderProfile, bool enableLinkingSupport)
|
||||
where TLoader : struct, ID2D1BytecodeLoader
|
||||
where T : struct, ID2D1Shader
|
||||
{
|
||||
Unsafe.AsRef(in shader).BuildHlslSource(out string hlslSource);
|
||||
|
||||
using ComPtr<ID3DBlob> d3DBlobBytecode = Shaders.Translation.D2D1ShaderCompiler.Compile(
|
||||
hlslSource.AsSpan(),
|
||||
shaderProfile ?? D2D1ShaderProfile.PixelShader50,
|
||||
enableLinkingSupport);
|
||||
|
||||
loader.LoadDynamicBytecode((IntPtr)d3DBlobBytecode.Get());
|
||||
}
|
||||
}
|
||||
@@ -171,11 +171,11 @@ public sealed partial class IShaderGenerator
|
||||
IdentifierName("ComputeSharp.__Internals.ShaderCompiler"),
|
||||
IdentifierName("LoadDynamicBytecode")))
|
||||
.AddArgumentListArguments(
|
||||
Argument(IdentifierName("loader")).WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)),
|
||||
Argument(IdentifierName("loader")).WithRefKindKeyword(Token(SyntaxKind.RefKeyword)),
|
||||
Argument(IdentifierName("threadsX")),
|
||||
Argument(IdentifierName("threadsY")),
|
||||
Argument(IdentifierName("threadsZ")),
|
||||
Argument(ThisExpression()).WithRefOrOutKeyword(Token(SyntaxKind.InKeyword))));
|
||||
Argument(ThisExpression()).WithRefKindKeyword(Token(SyntaxKind.InKeyword))));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする