From ad7d1bd706220fadbea38b578816fab1f241d7f9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 16 Apr 2022 12:56:19 +0200 Subject: [PATCH] Optimize D2D1InteropServices.SetConstantBufferForD2D1Effect --- .../D2D1EffectDispatchDataLoader.cs | 41 +++++++++++++++++++ .../Interop/D2D1InteropServices.Effects.cs | 22 +++------- 2 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 src/ComputeSharp.D2D1Interop/Shaders/Dispatching/D2D1EffectDispatchDataLoader.cs diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Dispatching/D2D1EffectDispatchDataLoader.cs b/src/ComputeSharp.D2D1Interop/Shaders/Dispatching/D2D1EffectDispatchDataLoader.cs new file mode 100644 index 00000000..45a70c6b --- /dev/null +++ b/src/ComputeSharp.D2D1Interop/Shaders/Dispatching/D2D1EffectDispatchDataLoader.cs @@ -0,0 +1,41 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.D2D1Interop.__Internals; +using ComputeSharp.D2D1Interop.Extensions; +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 D2D1EffectDispatchDataLoader : ID2D1DispatchDataLoader +{ + /// + /// The object in use. + /// + private readonly ID2D1Effect* d2D1Effect; + + /// + /// Creates a new instance. + /// + /// The object to use. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal D2D1EffectDispatchDataLoader(ID2D1Effect* d2D1Effect) + { + this.d2D1Effect = d2D1Effect; + } + + /// + public void LoadConstantBuffer(ReadOnlySpan data) + { + this.d2D1Effect->SetValue( + index: 0, + type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BLOB, + data: (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data)), + dataSize: (uint)data.Length * sizeof(uint)).Assert(); + } +} diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.Effects.cs b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.Effects.cs index 4062f71f..9a076ca1 100644 --- a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.Effects.cs +++ b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.Effects.cs @@ -1,13 +1,17 @@ using System; +using System.Runtime.CompilerServices; #if !NET6_0_OR_GREATER using System.Runtime.InteropServices; #endif using System.Text; using ComputeSharp.D2D1Interop.Extensions; using ComputeSharp.D2D1Interop.Interop.Effects; +using ComputeSharp.D2D1Interop.Shaders.Dispatching; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; +#pragma warning disable CS0618 + namespace ComputeSharp.D2D1Interop; /// @@ -115,22 +119,8 @@ unsafe partial class D2D1InteropServices public static unsafe void SetConstantBufferForD2D1Effect(in T shader, void* d2D1Effect) where T : unmanaged, ID2D1PixelShader { - // Get the shader state - ReadOnlyMemory buffer = D2D1InteropServices.GetPixelShaderConstantBufferForD2D1DrawInfo(in shader); + D2D1EffectDispatchDataLoader dataLoader = new((ID2D1Effect*)d2D1Effect); - if (buffer.Length > 0) - { - fixed (byte* p = buffer.Span) - { - // Load the effect buffer - ((ID2D1Effect*)d2D1Effect)->SetValue( - index: 0, - type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BLOB, - data: p, - dataSize: (uint)buffer.Length).Assert(); - } - } - - // TODO: optimize with a custom dispatch data loader + Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader); } }