From 33c87516f8ae25f16e944b89d87a256c5e77171c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 14 Apr 2022 02:27:40 +0200 Subject: [PATCH] Extract APIs to register and create pixel shader effects --- .../Shaders/Interop/D2D1InteropServices.cs | 4 +- .../Helpers/D2D1InteropServices2.cs | 126 ++++++++++++++++++ .../Helpers/D2D1ShaderTestHelper.cs | 67 +--------- 3 files changed, 134 insertions(+), 63 deletions(-) create mode 100644 tests/ComputeSharp.D2D1Interop.Tests/Helpers/D2D1InteropServices2.cs diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs index 965663c5..9b26ca18 100644 --- a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs +++ b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1InteropServices.cs @@ -99,8 +99,8 @@ public static unsafe class D2D1InteropServices /// /// Sets the constant buffer from an input D2D1 pixel shader, by calling ID2D1DrawInfo::SetPixelShaderConstantBuffer. /// - /// The type of D2D1 pixel shader to retrieve info for. - /// The input D2D1 pixel shader to retrieve info for. + /// The type of D2D1 pixel shader to set the constant buffer for. + /// The input D2D1 pixel shader to set the contant buffer for. /// A pointer to the ID2D1DrawInfo instance to use. /// For more info, see . public static void SetPixelShaderConstantBufferForD2D1DrawInfo(in T shader, void* d2D1DrawInfo) diff --git a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/D2D1InteropServices2.cs b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/D2D1InteropServices2.cs new file mode 100644 index 00000000..29852a12 --- /dev/null +++ b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/D2D1InteropServices2.cs @@ -0,0 +1,126 @@ +using System; +using System.Text; +using ComputeSharp.D2D1Interop.Tests.Extensions; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; + +namespace ComputeSharp.D2D1Interop.Tests.Helpers; + +/// +/// Provides methods to extract reflection info on D2D1 shaders generated using this library. +/// +internal static class D2D1InteropServices2 +{ + /// + /// Registers an effect from an input D2D1 pixel shader, by calling ID2D1Factory1::RegisterEffectFromString. + /// + /// The type of D2D1 pixel shader to register. + /// A pointer to the ID2D1Factory1 instance to use. + /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. + /// For more info, see . + public static unsafe void RegisterPixelShaderEffectForD2D1Factory1(void* d2D1Factory1, out Guid effectId) + where T : unmanaged, ID2D1PixelShader + { + effectId = default; + + // Setup the input string + StringBuilder effectInputsBuilder = new(); + + for (int i = 0; i < PixelShaderEffect.For.NumberOfInputs; i++) + { + effectInputsBuilder.Append($""); + } + + // Prepare the XML with a variable number of inputs + string xml = @$" + + + + + + + + {effectInputsBuilder} + + + + +"; + + fixed (char* pXml = xml) + fixed (char* pPropertyName = "Buffer") + { + // Prepare the effect binding functions + D2D1_PROPERTY_BINDING d2D1PropertyBinding; + d2D1PropertyBinding.propertyName = (ushort*)pPropertyName; + d2D1PropertyBinding.getFunction = + (delegate* unmanaged) + (delegate* unmanaged) + &PixelShaderEffect.GetConstantBuffer; + d2D1PropertyBinding.setFunction = + (delegate* unmanaged) + (delegate* unmanaged) + &PixelShaderEffect.SetConstantBuffer; + + Guid shaderId = typeof(T).GUID; + + // Register the effect + ((ID2D1Factory1*)d2D1Factory1)->RegisterEffectFromString( + classId: &shaderId, + propertyXml: (ushort*)pXml, + bindings: &d2D1PropertyBinding, + bindingsCount: 1, + effectFactory: PixelShaderEffect.For.Factory).Assert(); + + effectId = shaderId; + } + } + + /// + /// Creates an effect wrapping an input D2D1 pixel shader, by calling ID2D1DeviceContext::CreateEffect. + /// + /// The type of D2D1 pixel shader to create an effect for. + /// A pointer to the ID2D1DeviceContext instance to use. + /// A pointer to the resulting ID2D1Effect* pointer to produce. + /// For more info, see . + public static unsafe void CreatePixelShaderEffectFromD2D1DeviceContext(void* d2D1DeviceContext, void** d2D1Effect) + where T : unmanaged, ID2D1PixelShader + { + Guid shaderId = typeof(T).GUID; + + ((ID2D1DeviceContext*)d2D1DeviceContext)->CreateEffect( + effectId: &shaderId, + effect: (ID2D1Effect**)d2D1Effect).Assert(); + } + + /// + /// Sets the constant buffer from an input D2D1 effect, by calling ID2D1Effect::SetValue. + /// + /// The type of D2D1 pixel shader to set the constant buffer for. + /// The input D2D1 pixel shader to set the contant buffer for. + /// A pointer to the ID2D1Effect instance to use. + /// For more info, see . + public static unsafe void SetConstantBufferForD2D1Effect(in T shader, void* d2D1Effect) + where T : unmanaged, ID2D1PixelShader + { + // Get the shader state + ReadOnlyMemory buffer = D2D1InteropServices.GetPixelShaderConstantBufferForD2D1DrawInfo(in shader); + + 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 + } +} diff --git a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/D2D1ShaderTestHelper.cs b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/D2D1ShaderTestHelper.cs index 7e67b4d0..d9e75a4b 100644 --- a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/D2D1ShaderTestHelper.cs +++ b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/D2D1ShaderTestHelper.cs @@ -1,6 +1,4 @@ -using System; -using System.Linq; -using ComputeSharp.D2D1Interop.Tests.Extensions; +using ComputeSharp.D2D1Interop.Tests.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -24,8 +22,6 @@ internal static class D2D1ShaderTestHelper in T shader) where T : unmanaged, ID2D1PixelShader { - Guid runId = Guid.NewGuid(); - using ComPtr d2D1Factory2 = default; D2D1_FACTORY_OPTIONS d2D1FactoryOptions = default; @@ -116,66 +112,15 @@ internal static class D2D1ShaderTestHelper wicBitmapSource: (IWICBitmapSource*)wicBitmap.Get(), bitmap: d2D1BitmapSource.GetAddressOf()).Assert(); - // Prepare the XML with a variable number of inputs - string xml = @$" - - - - - - - - {string.Join("", Enumerable.Range(0, PixelShaderEffect.For.NumberOfInputs).Select(static i => $""))} - - - - -"; - - fixed (char* pXml = xml) - fixed (char* pPropertyName = "Buffer") - { - // Prepare the effect binding functions - D2D1_PROPERTY_BINDING d2D1PropertyBinding; - d2D1PropertyBinding.propertyName = (ushort*)pPropertyName; - d2D1PropertyBinding.getFunction = - (delegate* unmanaged) - (delegate* unmanaged)&PixelShaderEffect.GetConstantBuffer; - d2D1PropertyBinding.setFunction = - (delegate* unmanaged) - (delegate* unmanaged)&PixelShaderEffect.SetConstantBuffer; - - // Register the effect - d2D1Factory2.Get()->RegisterEffectFromString( - classId: &runId, - propertyXml: (ushort*)pXml, - bindings: &d2D1PropertyBinding, - bindingsCount: 1, - effectFactory: PixelShaderEffect.For.Factory).Assert(); - } + D2D1InteropServices2.RegisterPixelShaderEffectForD2D1Factory1(d2D1Factory2.Get(), out _); using ComPtr d2D1Effect = default; - // Create a the pixel shader effect - d2D1DeviceContext.Get()->CreateEffect( - effectId: &runId, - effect: d2D1Effect.GetAddressOf()).Assert(); + D2D1InteropServices2.CreatePixelShaderEffectFromD2D1DeviceContext( + d2D1DeviceContext.Get(), + (void**)d2D1Effect.GetAddressOf()); - // Get the shader state - ReadOnlyMemory buffer = D2D1InteropServices.GetPixelShaderConstantBufferForD2D1DrawInfo(in shader); - - if (buffer.Length > 0) - { - fixed (byte* p = buffer.Span) - { - // Load the effect buffer - d2D1Effect.Get()->SetValue( - index: 0, - type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BLOB, - data: p, - dataSize: (uint)buffer.Length).Assert(); - } - } + D2D1InteropServices2.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get()); d2D1Effect.Get()->SetInput(0, (ID2D1Image*)d2D1BitmapSource.Get());