diff --git a/.gitignore b/.gitignore
index a49e5f52..3afc0186 100644
--- a/.gitignore
+++ b/.gitignore
@@ -341,4 +341,7 @@ ASALocalRun/
samples/**/city_*.jpg
# XML docs files
-src/**/ComputeSharp*.xml
\ No newline at end of file
+src/**/ComputeSharp*.xml
+
+# Preserve .rd.xml files
+!src/**/ComputeSharp*.rd.xml
\ No newline at end of file
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 69201dfb..95c3252b 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
@@ -47,7 +51,7 @@ internal unsafe struct ICanvasEffectFactoryNative
}
///
- /// Creates a new inspectable wrapper for an inpute D2D effect previosly registered through .
+ /// Creates a new inspectable wrapper for an input D2D effect previosly registered through .
///
/// The input canvas device.
/// The input native effect to create a wrapper for.
@@ -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.
+ ///
+ private IUnknownVftbl IUnknownVftbl;
+
+ ///
+ /// Function pointer for .
+ ///
+ private 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/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs
index 2fd2cf6d..a85463c6 100644
--- a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs
+++ b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs
@@ -128,8 +128,9 @@ internal unsafe struct ICanvasFactoryNative
[VtblIndex(9)]
public HRESULT RegisterEffectFactory([NativeTypeName("const IID &")] Guid* effectId, ICanvasEffectFactoryNative* factory)
{
- return ((delegate* unmanaged[Stdcall])this.lpVtbl[9])(
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[9])(
(ICanvasFactoryNative*)Unsafe.AsPointer(ref this),
+ effectId,
factory);
}
@@ -145,8 +146,9 @@ internal unsafe struct ICanvasFactoryNative
[VtblIndex(10)]
public HRESULT UnregisterEffectFactory([NativeTypeName("const IID &")] Guid* effectId)
{
- return ((delegate* unmanaged[Stdcall])this.lpVtbl[10])(
- (ICanvasFactoryNative*)Unsafe.AsPointer(ref this));
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[10])(
+ (ICanvasFactoryNative*)Unsafe.AsPointer(ref this),
+ effectId);
}
#if !WINDOWS_UWP
diff --git a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs
index b63aed7f..ac53ae5d 100644
--- a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs
+++ b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs
@@ -145,17 +145,17 @@ internal unsafe struct ICanvasImageInterop
///
/// The IUnknown vtable.
///
- internal IUnknownVftbl IUnknownVftbl;
+ private IUnknownVftbl IUnknownVftbl;
///
/// Function pointer for .
///
- internal delegate* unmanaged[Stdcall] GetDevice;
+ private delegate* unmanaged[Stdcall] GetDevice;
///
/// Function pointer for .
///
- internal delegate* unmanaged[Stdcall] GetD2DImage;
+ private delegate* unmanaged[Stdcall] GetD2DImage;
///
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
diff --git a/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems b/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems
index ebda8706..2bdd3aaf 100644
--- a/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems
+++ b/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems
@@ -38,6 +38,7 @@
+
diff --git a/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs b/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs
index 0a671c36..f1990f56 100644
--- a/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs
+++ b/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs
@@ -90,6 +90,25 @@ internal static unsafe class ResourceManager
canvasFactoryNative.Get()->UnregisterWrapper(resource).Assert();
}
+ ///
+ /// Registers a factory of wrappers for custom effect with a given effect id.
+ ///
+ /// The id of the effects to register a factory for.
+ /// The input factory to create wrappers of native effects.
+ public static void RegisterEffectFactory(Guid effectId, ICanvasEffectFactoryNative.Interface factory)
+ {
+ using ComPtr canvasFactoryNative = default;
+
+ GetActivationFactory(canvasFactoryNative.GetAddressOf());
+
+ using ComPtr canvasEffectFactoryNative = default;
+
+ RcwMarshaller.GetNativeInterface(factory, canvasEffectFactoryNative.GetAddressOf()).Assert();
+
+ // Register the factory for the current effect from the activation factory
+ canvasFactoryNative.Get()->RegisterEffectFactory(&effectId, canvasEffectFactoryNative.Get()).Assert();
+ }
+
///
/// Gets the activation factory.
///
diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs
new file mode 100644
index 00000000..ea1f07a6
--- /dev/null
+++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs
@@ -0,0 +1,112 @@
+using System;
+using System.Runtime.CompilerServices;
+using ABI.Microsoft.Graphics.Canvas;
+using ComputeSharp.D2D1.Extensions;
+using ComputeSharp.D2D1.Interop.Effects;
+#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 registration logic for .
+ ///
+ private sealed class EffectFactoryManager
+ {
+ ///
+ /// Lock object used to synchronize the registration logic.
+ ///
+ private readonly object lockObject = new();
+
+ ///
+ /// Indicates whether an effect factory for has been registered.
+ ///
+ private volatile bool isEffectFactoryRegistered;
+
+ ///
+ /// Creates a new instance.
+ ///
+ private EffectFactoryManager()
+ {
+ }
+
+ ///
+ /// Gets the shared instance.
+ ///
+ public static EffectFactoryManager Instance { get; } = new();
+
+ ///
+ /// Ensures an effect factory is registered in Win2D for the current effect type, or registers one otherwise.
+ ///
+ public void EnsureEffectFactoryIsRegistered()
+ {
+ if (this.isEffectFactoryRegistered)
+ {
+ return;
+ }
+
+ EnsureEffectFactoryIsRegisteredWithLock();
+ }
+
+ ///
+ /// Same as , but with a slower path using a lock.
+ ///
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ private void EnsureEffectFactoryIsRegisteredWithLock()
+ {
+ lock (this.lockObject)
+ {
+ if (!this.isEffectFactoryRegistered)
+ {
+ ResourceManager.RegisterEffectFactory(
+ effectId: PixelShaderEffect.For.Instance.Id,
+ factory: new EffectFactory());
+
+ this.isEffectFactoryRegistered = true;
+ }
+ }
+ }
+ }
+
+ ///
+ /// A managed implementation of for .
+ ///
+ private sealed class EffectFactory : ICanvasEffectFactoryNative.Interface
+ {
+ ///
+ 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}.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.Uwp/ComputeSharp.D2D1.Uwp.csproj b/src/ComputeSharp.D2D1.Uwp/ComputeSharp.D2D1.Uwp.csproj
index 1c67518c..e63d2ea1 100644
--- a/src/ComputeSharp.D2D1.Uwp/ComputeSharp.D2D1.Uwp.csproj
+++ b/src/ComputeSharp.D2D1.Uwp/ComputeSharp.D2D1.Uwp.csproj
@@ -15,6 +15,15 @@
$(NoWarn);NETSDK1023
+
+
+
+
+
+
diff --git a/src/ComputeSharp.D2D1.Uwp/Properties/ComputeSharp.D2D1.Uwp.rd.xml b/src/ComputeSharp.D2D1.Uwp/Properties/ComputeSharp.D2D1.Uwp.rd.xml
new file mode 100644
index 00000000..0ac421e8
--- /dev/null
+++ b/src/ComputeSharp.D2D1.Uwp/Properties/ComputeSharp.D2D1.Uwp.rd.xml
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
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();