From 5ca2792892adc315d509aedf71e875018ba19464 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 22 Apr 2023 14:58:42 +0200 Subject: [PATCH 1/7] Add basic ICanvasEffectFactoryNative support code --- .../ICanvasEffectFactoryNative.cs | 2 +- .../ComputeSharp.D2D1.UI.projitems | 1 + ...derEffect{T}.ICanvasEffectFactoryNative.cs | 72 +++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs 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..59a3919c 100644 --- a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs +++ b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs @@ -47,7 +47,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. 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/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs new file mode 100644 index 00000000..b80457a1 --- /dev/null +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs @@ -0,0 +1,72 @@ +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 From 6c17745c5dc5ab2eaf9ff015c5ce74ad0a157b8f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 22 Apr 2023 15:19:20 +0200 Subject: [PATCH 2/7] Switch ICanvasEffectFactoryNative to COM interface --- .../ICanvasEffectFactoryNative.cs | 82 +++++++++++++++++- .../ComputeSharp.D2D1.UI.projitems | 2 +- ...xelShaderEffect{T}.EffectFactoryManager.cs | 85 +++++++++++++++++++ ...derEffect{T}.ICanvasEffectFactoryNative.cs | 72 ---------------- ...ixelShaderEffect{T}.ICanvasImageInterop.cs | 3 + .../Effects/PixelShaderEffect.For{T}.cs | 2 +- 6 files changed, 171 insertions(+), 75 deletions(-) create mode 100644 src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.EffectFactoryManager.cs delete mode 100644 src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs 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(); From 051e2536ef5b0787595ac708a19d8e4bc7a66170 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 22 Apr 2023 15:28:10 +0200 Subject: [PATCH 3/7] Wire up effect factory registration on first realization --- .../Helpers/ResourceManager.cs | 19 +++++++++++++++++++ ...xelShaderEffect{T}.EffectFactoryManager.cs | 6 +++++- 2 files changed, 24 insertions(+), 1 deletion(-) 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}.EffectFactoryManager.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.EffectFactoryManager.cs index ac335191..73d2d2ba 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.EffectFactoryManager.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.EffectFactoryManager.cs @@ -1,6 +1,7 @@ using System; 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; @@ -48,13 +49,16 @@ unsafe partial class PixelShaderEffect /// 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() { lock (this.lockObject) { if (!this.isEffectFactoryRegistered) { - // TODO + ResourceManager.RegisterEffectFactory(PixelShaderEffect.For.Instance.Id, this); this.isEffectFactoryRegistered = true; } From af7bd0a8b2f5365beb12c43c9dede61fd8c4c458 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 22 Apr 2023 15:32:06 +0200 Subject: [PATCH 4/7] Fix signatures in ICanvasFactoryNative effect factory APIs --- .../ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 From 39e67a3ba08d51b038f71094150d17ecf46e31b5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 22 Apr 2023 18:02:57 +0200 Subject: [PATCH 5/7] Split EffectFactoryManager and EffectFactory classes --- .../ComputeSharp.D2D1.UI.projitems | 2 +- ...lShaderEffect{T}.ICanvasEffectFactoryNative.cs} | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) rename src/ComputeSharp.D2D1.UI/{PixelShaderEffect{T}.EffectFactoryManager.cs => PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs} (81%) diff --git a/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems b/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems index 73a88069..2bdd3aaf 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}.ICanvasEffectFactoryNative.cs similarity index 81% rename from src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.EffectFactoryManager.cs rename to src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs index 73d2d2ba..ab3ccb30 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.EffectFactoryManager.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs @@ -23,9 +23,9 @@ namespace ComputeSharp.D2D1.WinUI; unsafe partial class PixelShaderEffect { /// - /// A manager type to handle the effect factory logic and registration for . + /// A manager type to handle the effect factory registration logic for . /// - private sealed class EffectFactoryManager : ICanvasEffectFactoryNative.Interface + private sealed class EffectFactoryManager { /// /// Lock object used to synchronize the registration logic. @@ -58,13 +58,21 @@ unsafe partial class PixelShaderEffect { if (!this.isEffectFactoryRegistered) { - ResourceManager.RegisterEffectFactory(PixelShaderEffect.For.Instance.Id, this); + 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) { From cba80806d67332b6ce5114f3f720d8846e902f23 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Apr 2023 13:04:23 +0200 Subject: [PATCH 6/7] Fix EffectFactory marshalling on .NET Native --- .gitignore | 5 ++++- .../ICanvasEffectFactoryNative.cs | 4 ++-- .../ICanvasImageInterop.cs | 6 +++--- .../ComputeSharp.D2D1.Uwp.csproj | 9 +++++++++ .../Properties/ComputeSharp.D2D1.Uwp.rd.xml | 18 ++++++++++++++++++ 5 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 src/ComputeSharp.D2D1.Uwp/Properties/ComputeSharp.D2D1.Uwp.rd.xml 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 9e225dee..95c3252b 100644 --- a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs +++ b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs @@ -123,12 +123,12 @@ internal unsafe struct ICanvasEffectFactoryNative /// /// The IUnknown vtable. /// - internal IUnknownVftbl IUnknownVftbl; + private IUnknownVftbl IUnknownVftbl; /// /// Function pointer for . /// - internal delegate* unmanaged[Stdcall] CreateWrapper; + private delegate* unmanaged[Stdcall] CreateWrapper; /// [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] 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.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 From 4d825d969182d6b19aba5b8cc55a6cd5740d39b3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Apr 2023 13:11:14 +0200 Subject: [PATCH 7/7] Add fast path in EffectFactoryManager.EnsureEffectFactoryIsRegistered --- ...haderEffect{T}.ICanvasEffectFactoryNative.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs index ab3ccb30..ea1f07a6 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Interop.Effects; @@ -35,7 +36,7 @@ unsafe partial class PixelShaderEffect /// /// Indicates whether an effect factory for has been registered. /// - private bool isEffectFactoryRegistered; + private volatile bool isEffectFactoryRegistered; /// /// Creates a new instance. @@ -53,6 +54,20 @@ unsafe partial class PixelShaderEffect /// 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) {