diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IBytecodeLoader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IBytecodeLoader.cs new file mode 100644 index 00000000..943cef72 --- /dev/null +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IBytecodeLoader.cs @@ -0,0 +1,26 @@ +using System; +using System.ComponentModel; + +namespace ComputeSharp.__Internals; + +/// +/// A base representing a bytecode loader for a shader being dispatched. +/// +[EditorBrowsable(EditorBrowsableState.Never)] +[Obsolete("This interface is not intended to be used directly by user code")] +public interface IBytecodeLoader +{ + /// + /// Loads a dynamic shader bytecode. + /// + /// An opaque handle to the shader bytecode. + /// Thrown if the shader has already been initialized. + void LoadDynamicBytecode(IntPtr handle); + + /// + /// Loads the embedded shader bytecode for the shader being dispatched. + /// + /// The shader bytecode. + /// Thrown if the shader has already been initialized. + void LoadEmbeddedBytecode(ReadOnlySpan bytecode); +} diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs index 8193efc6..40989e59 100644 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs @@ -65,35 +65,17 @@ public interface IShader #endif /// - /// Builds the HLSL source code for the current shader instance. + /// Loads the bytecode for the current shader. /// - /// The resulting instance containing the HLSL source. + /// The type of bytecode loader being used. + /// The instance to use to load the bytecode. /// The number of threads in each thread group for the X axis. /// The number of threads in each thread group for the Y axis. /// The number of threads in each thread group for the Z axis. [EditorBrowsable(EditorBrowsableState.Never)] - [Obsolete("This method is not intended to be called directly by user code")] - void BuildHlslString(out ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ) -#if NET6_0_OR_GREATER - { - throw new NotImplementedException(); - } -#else - ; -#endif - - /// - /// Tries to retrieve the precompiled bytecode for the current shader. - /// - /// The number of threads in each thread group for the X axis. - /// The number of threads in each thread group for the Y axis. - /// The number of threads in each thread group for the Z axis. - /// The resulting bytecode, if found. - /// Whether or not a precompiled bytecode was found. - /// The underlying buffer for , if found, can be assumed to always be pinned. - [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be used directly by user code")] - bool TryGetBytecode(int threadsX, int threadsY, int threadsZ, out ReadOnlySpan bytecode) + void LoadBytecode(ref TBytecodeLoader loader, int threadsX, int threadsY, int threadsZ) + where TBytecodeLoader : struct, IBytecodeLoader #if NET6_0_OR_GREATER { throw new NotImplementedException(); diff --git a/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs b/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs new file mode 100644 index 00000000..68971f12 --- /dev/null +++ b/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs @@ -0,0 +1,60 @@ +using System; +using System.Runtime.CompilerServices; +using ComputeSharp.__Internals; +using Microsoft.Toolkit.Diagnostics; +using TerraFX.Interop.DirectX; +using ComputeSharp.Shaders.Translation.Models; + +#pragma warning disable CS0618 + +namespace ComputeSharp.Shaders.Dispatching; + +/// +/// A bytecode loader for compute shaders. +/// +internal struct ShaderBytecodeLoader : IBytecodeLoader +{ + /// + /// The current cached shader instance. + /// + private ICachedShader? cachedShader; + + /// + /// Gets the current cached shader instance. + /// + /// The current instance. + /// Thrown if the shader has not been initialized. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ICachedShader GetCachedShader() + { + if (this.cachedShader is not ICachedShader cachedShader) + { + return ThrowHelper.ThrowInvalidOperationException("The shader has not been initialized"); + } + + return cachedShader; + } + + /// + public unsafe void LoadDynamicBytecode(IntPtr handle) + { + if (this.cachedShader is not null) + { + ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized"); + } + + this.cachedShader = new ICachedShader.Dynamic((IDxcBlob*)handle); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void LoadEmbeddedBytecode(ReadOnlySpan bytecode) + { + if (this.cachedShader is not null) + { + ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized"); + } + + this.cachedShader = new ICachedShader.Embedded(bytecode); + } +} diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs index 0e926a5b..d7af2136 100644 --- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs @@ -4,9 +4,6 @@ using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Commands; using ComputeSharp.Shaders.Dispatching; using ComputeSharp.Shaders.Extensions; -#if !DISABLE_RUNTIME_SHADER_COMPILATION_SUPPORT -using ComputeSharp.Shaders.Translation; -#endif using ComputeSharp.Shaders.Translation.Models; using ComputeSharp.__Internals; using Microsoft.Toolkit.Diagnostics; @@ -228,26 +225,11 @@ internal static class ShaderRunner [MethodImpl(MethodImplOptions.NoInlining)] private static unsafe void LoadShader(int threadsX, int threadsY, int threadsZ, ref T shader, out ICachedShader shaderData) { - if (Unsafe.NullRef().TryGetBytecode(threadsX, threadsY, threadsZ, out ReadOnlySpan bytecode)) - { - shaderData = new ICachedShader.Embedded(bytecode); - } - else - { -#if DISABLE_RUNTIME_SHADER_COMPILATION_SUPPORT - ThrowHelper.ThrowNotSupportedException("Runtime shader compilation is not supported by the current configuration."); + ShaderBytecodeLoader bytecodeLoader = default; - shaderData = null!; -#else - shader.BuildHlslString(out ArrayPoolStringBuilder builder, threadsX, threadsY, threadsZ); + shader.LoadBytecode(ref bytecodeLoader, threadsX, threadsY, threadsZ); - using ComPtr dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(builder.WrittenSpan); - - builder.Dispose(); - - shaderData = new ICachedShader.Dynamic(dxcBlobBytecode.Get()); -#endif - } + shaderData = bytecodeLoader.GetCachedShader(); } ///