diff --git a/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs b/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs index 4cdeb8c5..f9cbd999 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs @@ -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 dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(shaderSource.WrittenSpan); + using ComPtr dxcBlobBytecode = Shaders.Translation.ShaderCompiler.Instance.CompileShader(shaderSource.WrittenSpan); shaderSource.Dispose(); diff --git a/src/ComputeSharp.Dynamic/Shaders/Translation/__Internals/ShaderCompiler.cs b/src/ComputeSharp.Dynamic/Shaders/Translation/__Internals/ShaderCompiler.cs index fc51c97c..6ff54932 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Translation/__Internals/ShaderCompiler.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Translation/__Internals/ShaderCompiler.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -7,16 +8,20 @@ namespace ComputeSharp.__Internals; /// /// A that uses the DXC APIs to compile compute shaders. /// +[EditorBrowsable(EditorBrowsableState.Never)] +[Obsolete("This type is not intended to be used directly by user code")] public static class ShaderCompiler { /// /// Compiles a new HLSL shader from the input source code. /// - /// The HLSL source code to compile. + /// The instance with the HLSL source code to compile. /// The bytecode for the compiled shader. - public static unsafe IntPtr CompileShader(ReadOnlySpan source) + public static unsafe IntPtr CompileShader(ref ArrayPoolStringBuilder builder) { - using ComPtr shader = Shaders.Translation.ShaderCompiler.Instance.CompileShader(source); + using ComPtr shader = Shaders.Translation.ShaderCompiler.Instance.CompileShader(builder.WrittenSpan); + + builder.Dispose(); _ = shader.Get()->AddRef(); diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateGetBytecodeMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateGetBytecodeMethod.Syntax.cs index bb0a41ce..4b41b8ec 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateGetBytecodeMethod.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateGetBytecodeMethod.Syntax.cs @@ -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( 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; diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 3a92a278..35b829a8 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -227,7 +227,8 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator // Check whether or not dynamic shaders are supported IncrementalValueProvider 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) => diff --git a/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs b/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs index 68971f12..4f26a41f 100644 --- a/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs +++ b/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs @@ -29,7 +29,7 @@ internal struct ShaderBytecodeLoader : IBytecodeLoader { if (this.cachedShader is not ICachedShader cachedShader) { - return ThrowHelper.ThrowInvalidOperationException("The shader has not been initialized"); + return ThrowHelper.ThrowInvalidOperationException("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);