diff --git a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs
index 59a3919c..9e225dee 100644
--- a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs
+++ b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs
@@ -4,7 +4,11 @@ using System.Runtime.InteropServices;
using TerraFX.Interop;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
-using TerraFX.Interop.WinRT;
+#if !WINDOWS_UWP
+using WinRT;
+using WinRT.Interop;
+#endif
+using IInspectable = TerraFX.Interop.WinRT.IInspectable;
#pragma warning disable CS0649, IDE1006
@@ -69,4 +73,80 @@ internal unsafe struct ICanvasEffectFactoryNative
dpi,
wrapper);
}
+
+ ///
+ /// The managed implementation of .
+ ///
+ [Guid("29BA1A1F-1CFE-44C3-984D-426D61B51427")]
+#if WINDOWS_UWP
+ [ComImport]
+ [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
+#else
+ [WindowsRuntimeType]
+ [WindowsRuntimeHelperType(typeof(Interface))]
+#endif
+ public interface Interface
+ {
+ ///
+#if WINDOWS_UWP
+ [PreserveSig]
+#endif
+ [return: NativeTypeName("HRESULT")]
+ int CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper);
+
+#if !WINDOWS_UWP
+ ///
+ /// The vtable type for .
+ ///
+ [Guid("29BA1A1F-1CFE-44C3-984D-426D61B51427")]
+ public struct Vftbl
+ {
+ ///
+ /// Allows CsWinRT to retrieve a pointer to the projection vtable (the name is hardcoded by convention).
+ ///
+ public static readonly IntPtr AbiToProjectionVftablePtr = InitVtbl();
+
+ ///
+ /// Builds the custom method table pointer for .
+ ///
+ /// The method table pointer for .
+ private static IntPtr InitVtbl()
+ {
+ Vftbl* lpVtbl = (Vftbl*)ComWrappersSupport.AllocateVtableMemory(typeof(Vftbl), sizeof(Vftbl));
+
+ lpVtbl->IUnknownVftbl = IUnknownVftbl.AbiToProjectionVftbl;
+ lpVtbl->CreateWrapper = &CreateWrapperFromAbi;
+
+ return (IntPtr)lpVtbl;
+ }
+
+ ///
+ /// The IUnknown vtable.
+ ///
+ internal IUnknownVftbl IUnknownVftbl;
+
+ ///
+ /// Function pointer for .
+ ///
+ internal delegate* unmanaged[Stdcall] CreateWrapper;
+
+ ///
+ [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
+ [return: NativeTypeName("HRESULT")]
+ private static int CreateWrapperFromAbi(IntPtr thisPtr, ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper)
+ {
+ try
+ {
+ return ComWrappersSupport.FindObject(thisPtr).CreateWrapper(device, resource, dpi, wrapper);
+ }
+ catch (Exception e)
+ {
+ ExceptionHelpers.SetErrorInfo(e);
+
+ return Marshal.GetHRForException(e);
+ }
+ }
+ }
+#endif
+ }
}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems b/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems
index 2bdd3aaf..73a88069 100644
--- a/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems
+++ b/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems
@@ -38,7 +38,7 @@
-
+
diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.EffectFactoryManager.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.EffectFactoryManager.cs
new file mode 100644
index 00000000..ac335191
--- /dev/null
+++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.EffectFactoryManager.cs
@@ -0,0 +1,85 @@
+using System;
+using ABI.Microsoft.Graphics.Canvas;
+using ComputeSharp.D2D1.Extensions;
+#if WINDOWS_UWP
+using ComputeSharp.D2D1.Uwp.Extensions;
+using ComputeSharp.D2D1.Uwp.Helpers;
+#else
+using ComputeSharp.D2D1.WinUI.Extensions;
+using ComputeSharp.D2D1.WinUI.Helpers;
+#endif
+using TerraFX.Interop.DirectX;
+using TerraFX.Interop.Windows;
+using TerraFX.Interop.WinRT;
+
+#if WINDOWS_UWP
+namespace ComputeSharp.D2D1.Uwp;
+#else
+namespace ComputeSharp.D2D1.WinUI;
+#endif
+
+///
+unsafe partial class PixelShaderEffect
+{
+ ///
+ /// A manager type to handle the effect factory logic and registration for .
+ ///
+ private sealed class EffectFactoryManager : ICanvasEffectFactoryNative.Interface
+ {
+ ///
+ /// Lock object used to synchronize the registration logic.
+ ///
+ private readonly object lockObject = new();
+
+ ///
+ /// Indicates whether an effect factory for has been registered.
+ ///
+ private bool isEffectFactoryRegistered;
+
+ ///
+ /// Creates a new instance.
+ ///
+ private EffectFactoryManager()
+ {
+ }
+
+ ///
+ /// Gets the shared instance.
+ ///
+ public static EffectFactoryManager Instance { get; } = new();
+
+ public void EnsureEffectFactoryIsRegistered()
+ {
+ lock (this.lockObject)
+ {
+ if (!this.isEffectFactoryRegistered)
+ {
+ // TODO
+
+ this.isEffectFactoryRegistered = true;
+ }
+ }
+ }
+
+ ///
+ int ICanvasEffectFactoryNative.Interface.CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper)
+ {
+ PixelShaderEffect @this = new();
+
+ using (ComPtr d2D1Device1 = default)
+ {
+ // Get the underlying ID2D1Device1 instance for the input device
+ device->GetD2DDevice(d2D1Device1.GetAddressOf()).Assert();
+
+ // Initialize the effect state
+ device->CopyTo(ref @this.canvasDevice).Assert();
+ d2D1Device1.CopyTo(ref @this.d2D1RealizationDevice).Assert();
+ resource->CopyTo(ref @this.d2D1Effect).Assert();
+ }
+
+ RcwMarshaller.GetNativeObject(@this, wrapper);
+
+ return S.S_OK;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs
deleted file mode 100644
index b80457a1..00000000
--- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs
+++ /dev/null
@@ -1,72 +0,0 @@
-using System;
-using System.Runtime.InteropServices;
-using ABI.Microsoft.Graphics.Canvas;
-using ComputeSharp.D2D1.Extensions;
-#if WINDOWS_UWP
-using ComputeSharp.D2D1.Uwp.Extensions;
-using ComputeSharp.D2D1.Uwp.Helpers;
-#else
-using ComputeSharp.D2D1.WinUI.Extensions;
-using ComputeSharp.D2D1.WinUI.Helpers;
-#endif
-using TerraFX.Interop;
-using TerraFX.Interop.DirectX;
-using TerraFX.Interop.Windows;
-using TerraFX.Interop.WinRT;
-
-#if WINDOWS_UWP
-namespace ComputeSharp.D2D1.Uwp;
-#else
-namespace ComputeSharp.D2D1.WinUI;
-#endif
-
-///
-unsafe partial class PixelShaderEffect
-{
- ///
- [UnmanagedFunctionPointer(CallingConvention.StdCall)]
- [return: NativeTypeName("HRESULT")]
- private delegate int CreateWrapperDelegate(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper);
-
- ///
- /// A cached instance wrapping .
- ///
- private static readonly CreateWrapperDelegate CreateWrapperWrapper = CreateWrapper;
-
- ///
- /// Creates a new instance for an input D2D effect and marshals it as an wrapper.
- ///
- ///
- ///
- ///
- ///
- /// This method always returns (any exceptions will be marshalled by the delegate stub).
- private static int CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper)
- {
- PixelShaderEffect @this = new();
-
- @this.InitializeFromNativeResource(device, resource);
-
- RcwMarshaller.GetNativeObject(@this, wrapper);
-
- return S.S_OK;
- }
-
- ///
- /// Initializes a instance from an external realized D2D1 effect.
- ///
- ///
- ///
- private void InitializeFromNativeResource(ICanvasDevice* device, ID2D1Effect* resource)
- {
- using ComPtr d2D1Device1 = default;
-
- // Get the underlying ID2D1Device1 instance for the input device
- device->GetD2DDevice(d2D1Device1.GetAddressOf()).Assert();
-
- // Initialize the effect state
- device->CopyTo(ref this.canvasDevice).Assert();
- d2D1Device1.CopyTo(ref this.d2D1RealizationDevice).Assert();
- resource->CopyTo(ref this.d2D1Effect).Assert();
- }
-}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs
index ddee4f0d..796bd28a 100644
--- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs
+++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs
@@ -347,6 +347,9 @@ unsafe partial class PixelShaderEffect
// Register the new realized effect
ResourceManager.RegisterWrapper((IUnknown*)this.d2D1Effect.Get(), this);
+ // Register the effect factory, if necessary
+ EffectFactoryManager.Instance.EnsureEffectFactoryIsRegistered();
+
return true;
}
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs
index 3e277c45..37204600 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs
@@ -154,7 +154,7 @@ internal unsafe partial struct PixelShaderEffect
}
///
- /// Gets the shader instance.
+ /// Gets the shared instance.
///
public static For Instance { get; } = CreateInstance();