diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Dispatching/D2D1ShaderBytecodeLoader.cs b/src/ComputeSharp.D2D1Interop/Shaders/Dispatching/D2D1ShaderBytecodeLoader.cs new file mode 100644 index 00000000..e18934db --- /dev/null +++ b/src/ComputeSharp.D2D1Interop/Shaders/Dispatching/D2D1ShaderBytecodeLoader.cs @@ -0,0 +1,78 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using CommunityToolkit.Diagnostics; +using ComputeSharp.__Internals; +using ComputeSharp.Core.Extensions; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; + +#pragma warning disable CS0618 + +namespace ComputeSharp.Shaders.Dispatching; + +/// +/// A bytecode loader for D2D1 shaders. +/// +internal unsafe struct D2D1ShaderBytecodeLoader : ID2D1BytecodeLoader +{ + /// + /// A pointer to the precompiled shader, if available. + /// + private byte* embeddedBytecodePtr; + + /// + /// The length of the precompiled shader buffer, if available. + /// + private int embeddedBytecodeSize; + + /// + /// The dynamically compiled shader bytecode blob, if available. + /// + private ComPtr d3DBlob; + + /// + /// Gets the resulting shader bytecode that was available. + /// + /// The precompiled bytecode, if available. + /// An with the shader bytecode, if dynamically compiled. + /// Either is empty, or the returned pointer is . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ComPtr GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode) + { + precompiledBytecode = new ReadOnlySpan(this.embeddedBytecodePtr, this.embeddedBytecodeSize); + + return this.d3DBlob.Move(); + } + + /// + public unsafe void LoadDynamicBytecode(IntPtr handle) + { + if (this.embeddedBytecodePtr is not null || + this.d3DBlob.Get() is not null) + { + 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.d3DBlob = new ComPtr((ID3DBlob*)handle); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void LoadEmbeddedBytecode(ReadOnlySpan bytecode) + { + if (this.embeddedBytecodePtr is not null || + this.d3DBlob.Get() is not null) + { + ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized."); + } + + this.embeddedBytecodePtr = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(bytecode)); + this.embeddedBytecodeSize = bytecode.Length; + } +} diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Interop/Buffers/PinnedBufferMemoryManager.cs b/src/ComputeSharp.D2D1Interop/Shaders/Interop/Buffers/PinnedBufferMemoryManager.cs new file mode 100644 index 00000000..788e6516 --- /dev/null +++ b/src/ComputeSharp.D2D1Interop/Shaders/Interop/Buffers/PinnedBufferMemoryManager.cs @@ -0,0 +1,57 @@ +using System; +using System.Buffers; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using CommunityToolkit.Diagnostics; + +namespace ComputeSharp.D2D1Interop.Shaders.Interop.Buffers; + +/// +/// A custom that wraps a pinned memory buffer. +/// +internal sealed unsafe class PinnedBufferMemoryManager : MemoryManager +{ + /// + /// A pointer to the buffer. + /// + private readonly byte* buffer; + + /// + /// The length of the buffer. + /// + private readonly int length; + + /// + /// Initializes a new instance of the class with the specified parameters. + /// + /// A wrapping the input pinned buffer. + public PinnedBufferMemoryManager(ReadOnlySpan span) + { + this.buffer = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span)); + this.length = span.Length; + } + + /// + public override Span GetSpan() + { + return new(this.buffer, this.length); + } + + /// + public override unsafe MemoryHandle Pin(int elementIndex = 0) + { + Guard.IsInRange(elementIndex, 0, this.length); + + return new(this.buffer + elementIndex); + } + + /// + public override void Unpin() + { + } + + /// + protected override void Dispose(bool disposing) + { + } +} diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Interop/InteropServices.cs b/src/ComputeSharp.D2D1Interop/Shaders/Interop/InteropServices.cs index f2f63b45..a93dfcc8 100644 --- a/src/ComputeSharp.D2D1Interop/Shaders/Interop/InteropServices.cs +++ b/src/ComputeSharp.D2D1Interop/Shaders/Interop/InteropServices.cs @@ -1,7 +1,11 @@ -using System.Runtime.CompilerServices; +using System; +using System.Runtime.CompilerServices; using ComputeSharp.D2D1Interop.__Internals; using ComputeSharp.D2D1Interop.Shaders.Dispatching; +using ComputeSharp.D2D1Interop.Shaders.Interop.Buffers; +using ComputeSharp.Shaders.Dispatching; using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; #pragma warning disable CS0618 @@ -12,11 +16,43 @@ namespace ComputeSharp.D2D1Interop.Interop; /// public static class InteropServices { + /// + /// Loads the bytecode from an input D2D1 pixel shader. + /// + /// The type of D2D1 pixel shader to load the bytecode for. + /// The input D2D1 pixel shader to load the bytecode for. + /// A instance with the resulting shader bytecode. + /// + /// If the input shader was precompiled, will wrap a pinned memory buffer (from the PE section). + /// If the shader was compiled at runtime, will wrap a array with the bytecode. + /// + public static unsafe void LoadShaderBytecode(in T shader, out ReadOnlyMemory bytecode) + where T : struct, ID2D1Shader + { + D2D1ShaderBytecodeLoader bytecodeLoader = default; + + Unsafe.AsRef(in shader).LoadBytecode(ref bytecodeLoader); + + using ComPtr dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode); + + // If a precompiled shader is available, return it + if (!precompiledBytecode.IsEmpty) + { + bytecode = new PinnedBufferMemoryManager(precompiledBytecode).Memory; + } + + // Otherwise, return the dynamic shader instead + byte* bytecodePtr = (byte*)dynamicBytecode.Get()->GetBufferPointer(); + int bytecodeSize = (int)dynamicBytecode.Get()->GetBufferSize(); + + bytecode = new ReadOnlySpan(bytecodePtr, bytecodeSize).ToArray(); + } + /// /// Sets the constant buffer from an input D2D1 pixel shader, by calling ID2D1DrawInfo::SetPixelShaderConstantBuffer. /// - /// The type of compute shader to retrieve info for. - /// The input compute shader to retrieve info for. + /// The type of D2D1 pixel shader to retrieve info for. + /// The input D2D1 pixel shader to retrieve info for. /// A pointer to the ID2D1DrawInfo instance to use. /// For more info, see . public static unsafe void SetPixelShaderConstantBufferForD2D1DrawInfo(in T shader, void* d2D1DrawInfo)