diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs index 5534928a..8193efc6 100644 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs @@ -81,4 +81,24 @@ public interface IShader #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) +#if NET6_0_OR_GREATER + { + throw new NotImplementedException(); + } +#else + ; +#endif } diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs index a86d812e..5cbc9b8a 100644 --- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs @@ -25,7 +25,7 @@ internal static class ShaderRunner /// /// The mapping used to cache and reuse compiled shaders. /// - private static readonly Dictionary ShadersCache = new(); + private static readonly Dictionary ShadersCache = new(); /// /// Compiles and runs the input shader on a target instance, with the specified parameters. @@ -129,7 +129,7 @@ internal static class ShaderRunner lock (ShadersCache) { // Get or preload the shader - if (!ShadersCache.TryGetValue(key, out CachedShader? shaderData)) + if (!ShadersCache.TryGetValue(key, out ICachedShader? shaderData)) { LoadShader(threadsX, threadsY, threadsZ, ref shader, out shaderData); @@ -183,7 +183,7 @@ internal static class ShaderRunner lock (ShadersCache) { // Get or preload the shader - if (!ShadersCache.TryGetValue(key, out CachedShader? shaderData)) + if (!ShadersCache.TryGetValue(key, out ICachedShader? shaderData)) { LoadShader(threadsX, threadsY, 1, ref shader, out shaderData); @@ -222,27 +222,34 @@ internal static class ShaderRunner /// 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 input instance representing the compute shader to run. - /// The instance to return with the cached shader data. + /// The instance to return with the cached shader data. [MethodImpl(MethodImplOptions.NoInlining)] - private static unsafe void LoadShader(int threadsX, int threadsY, int threadsZ, ref T shader, out CachedShader shaderData) + private static unsafe void LoadShader(int threadsX, int threadsY, int threadsZ, ref T shader, out ICachedShader shaderData) { - shader.BuildHlslString(out ArrayPoolStringBuilder builder, threadsX, threadsY, threadsZ); + if (shader.TryGetBytecode(threadsX, threadsY, threadsZ, out ReadOnlySpan bytecode)) + { + shaderData = new ICachedShader.Embedded(bytecode); + } + else + { + shader.BuildHlslString(out ArrayPoolStringBuilder builder, threadsX, threadsY, threadsZ); - using ComPtr dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(builder.WrittenSpan); + using ComPtr dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(builder.WrittenSpan); - builder.Dispose(); + builder.Dispose(); - shaderData = new CachedShader(dxcBlobBytecode.Get()); + shaderData = new ICachedShader.Dynamic(dxcBlobBytecode.Get()); + } } /// /// Creates and caches a instance for a given shader. /// /// The to use to run the shader. - /// The instance with the data on the loaded shader. + /// The instance with the data on the loaded shader. /// The resulting instance to use to run the shader. [MethodImpl(MethodImplOptions.NoInlining)] - private static unsafe void CreatePipelineData(GraphicsDevice device, CachedShader shaderData, out PipelineData pipelineData) + private static unsafe void CreatePipelineData(GraphicsDevice device, ICachedShader shaderData, out PipelineData pipelineData) { using ComPtr d3D12RootSignature = default; diff --git a/src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs b/src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs deleted file mode 100644 index 2afec884..00000000 --- a/src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Runtime.CompilerServices; -using ComputeSharp.Graphics.Commands; -using ComputeSharp.Interop; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; - -namespace ComputeSharp.Shaders.Translation.Models; - -/// -/// A class that contains info on a cached shader. -/// -internal sealed unsafe class CachedShader : NativeObject -{ - /// - /// The instance currently in use. - /// - private ComPtr dxcBlobBytecode; - - /// - /// Creates a new instance with the specified parameters. - /// - /// The bytecode instance to wrap. - public CachedShader(IDxcBlob* dxcBlobBytecode) - { - this.dxcBlobBytecode = new ComPtr(dxcBlobBytecode); - } - - /// - /// Gets the map of cached instances for each GPU in use. - /// - public ConditionalWeakTable CachedPipelines { get; } = new(); - - /// - /// Gets a raw pointer to the instance in use. - /// - public D3D12_SHADER_BYTECODE D3D12ShaderBytecode => new((ID3DBlob*)this.dxcBlobBytecode.Get()); - - /// - protected override void OnDispose() - { - this.dxcBlobBytecode.Dispose(); - } -} diff --git a/src/ComputeSharp/Shaders/Translation/Models/ICachedShader.cs b/src/ComputeSharp/Shaders/Translation/Models/ICachedShader.cs new file mode 100644 index 00000000..309f0de2 --- /dev/null +++ b/src/ComputeSharp/Shaders/Translation/Models/ICachedShader.cs @@ -0,0 +1,95 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.Graphics.Commands; +using ComputeSharp.Interop; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; + +namespace ComputeSharp.Shaders.Translation.Models; + +/// +/// An interface that contains info on a cached shader. +/// +internal interface ICachedShader +{ + /// + /// Gets the map of cached instances for each GPU in use. + /// + ConditionalWeakTable CachedPipelines { get; } + + /// + /// Gets a value pointing shader bytecode to use. + /// + D3D12_SHADER_BYTECODE D3D12ShaderBytecode { get; } + + /// + /// An implementation for an embedded shader. + /// + public sealed unsafe class Embedded : ICachedShader + { + /// + /// The bytecode buffer, if a precompiled shader is used. + /// + private readonly byte* precompiledBytecodeBuffer; + + /// + /// The buffer size for , if present. + /// + private readonly uint precompiledBytecodeSize; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The shader bytecode buffer to wrap. + /// + /// It is assumed that is a static binary block in the .text segment of the + /// PE file, which is what Roslyn generates when directly assigning a new array to a . As such, + /// the underlying buffer is assumed to be pinned, and it will not be properly tracked for GC compactions. If an invalid + /// buffer is passed and its contents are moved, the behavior is undefined (access violations will most definitely occurr). + /// + public Embedded(ReadOnlySpan precompiledBytecode) + { + this.precompiledBytecodeBuffer = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(precompiledBytecode)); + this.precompiledBytecodeSize = (uint)precompiledBytecode.Length; + } + + /// + public ConditionalWeakTable CachedPipelines { get; } = new(); + + /// + public D3D12_SHADER_BYTECODE D3D12ShaderBytecode => new(this.precompiledBytecodeBuffer, this.precompiledBytecodeSize); + } + + /// + /// An implementation for a dynamically compiled shader. + /// + public sealed unsafe class Dynamic : NativeObject, ICachedShader + { + /// + /// The instance currently in use. + /// + private ComPtr dxcBlobBytecode; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The bytecode instance to wrap. + public Dynamic(IDxcBlob* dxcBlobBytecode) + { + this.dxcBlobBytecode = new ComPtr(dxcBlobBytecode); + } + + /// + public ConditionalWeakTable CachedPipelines { get; } = new(); + + /// + public D3D12_SHADER_BYTECODE D3D12ShaderBytecode => new ((ID3DBlob*)this.dxcBlobBytecode.Get()); + + /// + protected override void OnDispose() + { + this.dxcBlobBytecode.Dispose(); + } + } +}