diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Dispatching/D2D1ByteArrayDispatchDataLoader.cs b/src/ComputeSharp.D2D1Interop/Shaders/Dispatching/D2D1ByteArrayDispatchDataLoader.cs new file mode 100644 index 00000000..539ba916 --- /dev/null +++ b/src/ComputeSharp.D2D1Interop/Shaders/Dispatching/D2D1ByteArrayDispatchDataLoader.cs @@ -0,0 +1,36 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.__Internals; +using TerraFX.Interop.DirectX; + +#pragma warning disable CS0618 + +namespace ComputeSharp.D2D1Interop.Shaders.Dispatching; + +/// +/// A data loader for D2D1 pixel shaders dispatched via . +/// +internal struct D2D1ByteArrayDispatchDataLoader : ID2D1DispatchDataLoader +{ + /// + /// The object in use. + /// + private byte[]? data; + + /// + /// Gets the resulting pixel shader constant buffer. + /// + /// A array with the constant buffer for the current shader. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public byte[] GetResultingDispatchData() + { + return this.data!; + } + + /// + public void LoadConstantBuffer(ReadOnlySpan data) + { + this.data = MemoryMarshal.AsBytes(data).ToArray(); + } +} diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs index 2bc6eba7..f50cd61a 100644 --- a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs +++ b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs @@ -13,7 +13,7 @@ namespace ComputeSharp.D2D1Interop.Interop; /// /// Provides methods to extract reflection info on D2D1 shaders generated using this library. /// -public static class D2D1InteropServices +public static unsafe class D2D1InteropServices { /// /// Loads the bytecode from an input D2D1 pixel shader. @@ -25,7 +25,7 @@ public static class D2D1InteropServices /// 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) + public static void LoadShaderBytecode(in T shader, out ReadOnlyMemory bytecode) where T : unmanaged, ID2D1PixelShader { D2D1ShaderBytecodeLoader bytecodeLoader = default; @@ -47,6 +47,25 @@ public static class D2D1InteropServices bytecode = new ReadOnlySpan(bytecodePtr, bytecodeSize).ToArray(); } + /// + /// Gets the constant buffer from an input D2D1 pixel shader. + /// + /// The type of D2D1 pixel shader to retrieve info for. + /// The input D2D1 pixel shader to retrieve info for. + /// A instance with the pixel shader constant buffer. + /// + /// This method will allocate a buffer every time it is invoked. + /// For a zero-allocation alternative, use . + public static void GetPixelShaderConstantBufferForD2D1DrawInfo(in T shader, out ReadOnlyMemory buffer) + where T : unmanaged, ID2D1PixelShader + { + D2D1ByteArrayDispatchDataLoader dataLoader = default; + + Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader); + + buffer = dataLoader.GetResultingDispatchData(); + } + /// /// Sets the constant buffer from an input D2D1 pixel shader, by calling ID2D1DrawInfo::SetPixelShaderConstantBuffer. /// @@ -54,7 +73,7 @@ public static class D2D1InteropServices /// 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) + public static void SetPixelShaderConstantBufferForD2D1DrawInfo(in T shader, void* d2D1DrawInfo) where T : unmanaged, ID2D1PixelShader { D2D1DrawInfoDispatchDataLoader dataLoader = new((ID2D1DrawInfo*)d2D1DrawInfo);