From 97bfa49d4736d7842675e4731602a43040b66120 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 2 Apr 2022 13:42:36 +0200 Subject: [PATCH] Add SetPixelShaderConstantBufferForD2D1DrawInfo API --- .../D2D1DrawInfoDispatchDataLoader.cs | 38 +++++++++++++++++++ .../Shaders/Interop/InteropServices.cs | 29 ++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/ComputeSharp.D2D1Interop/Shaders/Dispatching/D2D1DrawInfoDispatchDataLoader.cs create mode 100644 src/ComputeSharp.D2D1Interop/Shaders/Interop/InteropServices.cs diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Dispatching/D2D1DrawInfoDispatchDataLoader.cs b/src/ComputeSharp.D2D1Interop/Shaders/Dispatching/D2D1DrawInfoDispatchDataLoader.cs new file mode 100644 index 00000000..b7152cd3 --- /dev/null +++ b/src/ComputeSharp.D2D1Interop/Shaders/Dispatching/D2D1DrawInfoDispatchDataLoader.cs @@ -0,0 +1,38 @@ +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 readonly unsafe struct D2D1DrawInfoDispatchDataLoader : ID2D1DispatchDataLoader +{ + /// + /// The object in use. + /// + private readonly ID2D1DrawInfo* d2D1DrawInfo; + + /// + /// Creates a new instance. + /// + /// The object to use. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal D2D1DrawInfoDispatchDataLoader(ID2D1DrawInfo* d2D1DrawInfo) + { + this.d2D1DrawInfo = d2D1DrawInfo; + } + + /// + public void LoadConstantBuffer(ReadOnlySpan data) + { + this.d2D1DrawInfo->SetPixelShaderConstantBuffer( + buffer: (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data)), + bufferCount: (uint)data.Length * sizeof(uint)); + } +} diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Interop/InteropServices.cs b/src/ComputeSharp.D2D1Interop/Shaders/Interop/InteropServices.cs new file mode 100644 index 00000000..f2f63b45 --- /dev/null +++ b/src/ComputeSharp.D2D1Interop/Shaders/Interop/InteropServices.cs @@ -0,0 +1,29 @@ +using System.Runtime.CompilerServices; +using ComputeSharp.D2D1Interop.__Internals; +using ComputeSharp.D2D1Interop.Shaders.Dispatching; +using TerraFX.Interop.DirectX; + +#pragma warning disable CS0618 + +namespace ComputeSharp.D2D1Interop.Interop; + +/// +/// Provides methods to extract reflection info on compute shaders generated using this library. +/// +public static class InteropServices +{ + /// + /// 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. + /// A pointer to the ID2D1DrawInfo instance to use. + /// For more info, see . + public static unsafe void SetPixelShaderConstantBufferForD2D1DrawInfo(in T shader, void* d2D1DrawInfo) + where T : struct, ID2D1Shader + { + D2D1DrawInfoDispatchDataLoader dataLoader = new((ID2D1DrawInfo*)d2D1DrawInfo); + + Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader); + } +}