Improve source generator support for dynamic shaders

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-19 16:45:12 +01:00
1 親 80044d69fd
コミット 23b500fe81
5ファイル変更
+27 -32

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

+1 -2
ファイルの表示
@@ -2,7 +2,6 @@
using System.Runtime.CompilerServices;
using ComputeSharp.__Internals;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Shaders.Translation;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
#if !NET6_0_OR_GREATER
@@ -107,7 +106,7 @@ public static class ReflectionServices
{
Unsafe.AsRef(in shader).BuildHlslString(out ArrayPoolStringBuilder shaderSource, 1, 1, 1);
using ComPtr<IDxcBlob> dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(shaderSource.WrittenSpan);
using ComPtr<IDxcBlob> dxcBlobBytecode = Shaders.Translation.ShaderCompiler.Instance.CompileShader(shaderSource.WrittenSpan);
shaderSource.Dispose();
+8 -3
ファイルの表示
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
@@ -7,16 +8,20 @@ namespace ComputeSharp.__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 ShaderCompiler
{
/// <summary>
/// Compiles a new HLSL shader from the input source code.
/// </summary>
/// <param name="source">The HLSL source code to compile.</param>
/// <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(ReadOnlySpan<char> source)
public static unsafe IntPtr CompileShader(ref ArrayPoolStringBuilder builder)
{
using ComPtr<IDxcBlob> shader = Shaders.Translation.ShaderCompiler.Instance.CompileShader(source);
using ComPtr<IDxcBlob> shader = Shaders.Translation.ShaderCompiler.Instance.CompileShader(builder.WrittenSpan);
builder.Dispose();
_ = shader.Get()->AddRef();
+8 -23
ファイルの表示
@@ -169,8 +169,7 @@ public sealed partial class IShaderGenerator
// This code produces a method declaration as follows:
//
// BuildHlslString(out global::ComputeSharp.__Internals.ArrayPoolStringBuilder builder, threadsX, threadsY, threadsZ);
// global::System.IntPtr bytecode = ShaderCompiler.CompileShader(builder.WrittenSpan);
// builder.Dispose();
// global::System.IntPtr bytecode = ShaderCompiler.CompileShader(ref builder);
// loader.LoadDynamicBytecode(bytecode);
dynamicStatements = ImmutableArray.Create<StatementSyntax>(
ExpressionStatement(
@@ -194,24 +193,15 @@ public sealed partial class IShaderGenerator
IdentifierName("ShaderCompiler"),
IdentifierName("CompileShader")))
.AddArgumentListArguments(
Argument(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("builder"),
IdentifierName("WrittenSpan")))))))),
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("builder"),
IdentifierName("Dispose")))),
Argument(IdentifierName("builder"))
.WithRefKindKeyword(Token(SyntaxKind.RefKeyword))))))),
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("loader"),
IdentifierName("LoadDynamicBytecode")))
.AddArgumentListArguments(Argument(IdentifierName("bytecode")))));
.AddArgumentListArguments(Argument(IdentifierName("bytecode")))));;
}
return dynamicStatements;
@@ -232,7 +222,7 @@ public sealed partial class IShaderGenerator
{
// This code produces a faulting call as follows:
//
// loader.LoadDynamicBytecode(global::System.IntPtr.Zero);
// loader.LoadDynamicBytecode(default);
return
ExpressionStatement(
InvocationExpression(
@@ -241,14 +231,9 @@ public sealed partial class IShaderGenerator
IdentifierName("loader"),
IdentifierName("LoadDynamicBytecode")))
.AddArgumentListArguments(
Argument(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("global::System.IntPtr"),
IdentifierName("IntPtr")),
IdentifierName("Zero")))));
Argument(LiteralExpression(
SyntaxKind.DefaultLiteralExpression,
Token(SyntaxKind.DefaultKeyword)))));
}
return dynamicFaultStatement;
+2 -1
ファイルの表示
@@ -227,7 +227,8 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
// Check whether or not dynamic shaders are supported
IncrementalValueProvider<bool> supportsDynamicShaders =
context.CompilationProvider
.Select(static (compilation, token) => compilation.GetTypeByMetadataName("ComputeSharp.__Internals.ShaderCompiler") is not null);
.Select(static (compilation, token) => compilation.ReferencedAssemblyNames.Any(
static identity => identity.Name is "ComputeSharp.Dynamic"));
// Generate the TryGetBytecode() methods
context.RegisterSourceOutput(embeddedBytecode.Combine(supportsDynamicShaders), static (context, item) =>
+8 -3
ファイルの表示
@@ -29,7 +29,7 @@ internal struct ShaderBytecodeLoader : IBytecodeLoader
{
if (this.cachedShader is not ICachedShader cachedShader)
{
return ThrowHelper.ThrowInvalidOperationException<ICachedShader>("The shader has not been initialized");
return ThrowHelper.ThrowInvalidOperationException<ICachedShader>("The shader has not been initialized.");
}
return cachedShader;
@@ -40,7 +40,12 @@ internal struct ShaderBytecodeLoader : IBytecodeLoader
{
if (this.cachedShader is not null)
{
ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized");
ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized.");
}
if (handle == IntPtr.Zero)
{
ThrowHelper.ThrowNotSupportedException("Runtime shader compilation is not supported by the current configuration.");
}
this.cachedShader = new ICachedShader.Dynamic((IDxcBlob*)handle);
@@ -52,7 +57,7 @@ internal struct ShaderBytecodeLoader : IBytecodeLoader
{
if (this.cachedShader is not null)
{
ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized");
ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized.");
}
this.cachedShader = new ICachedShader.Embedded(bytecode);