From da98a03317885018068ee2cdf6cb0459affe154f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 29 Sep 2024 12:36:19 -0700 Subject: [PATCH 1/2] Fix IID for 'ISwapChainPanelNative' on UWP --- src/ComputeSharp.UI/ComputeSharp.UI.projitems | 1 + .../Helpers/SwapChainManager.IID.cs | 54 +++++++++++++++++++ .../Helpers/SwapChainManager.cs | 4 +- 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 src/ComputeSharp.UI/Helpers/SwapChainManager.IID.cs diff --git a/src/ComputeSharp.UI/ComputeSharp.UI.projitems b/src/ComputeSharp.UI/ComputeSharp.UI.projitems index 56d28d6b2..aa0cebec5 100644 --- a/src/ComputeSharp.UI/ComputeSharp.UI.projitems +++ b/src/ComputeSharp.UI/ComputeSharp.UI.projitems @@ -17,6 +17,7 @@ + diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.IID.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.IID.cs new file mode 100644 index 000000000..b9e9fdf8f --- /dev/null +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.IID.cs @@ -0,0 +1,54 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +#if WINDOWS_UWP +namespace ComputeSharp.Uwp.Helpers; +#else +namespace ComputeSharp.WinUI.Helpers; +#endif + +/// +partial class SwapChainManager +{ + /// + /// Gets the IID of . + /// + private static unsafe Guid* IID_ISwapChainPanelNative + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ReadOnlySpan data = + [ +#if WINDOWS_UWP + 0xD2, 0x19, 0x2F, 0xF9, + 0xDE, 0x3A, + 0xA6, 0x45, + 0xA2, + 0x0C, + 0xF6, + 0xF1, + 0xEA, + 0x90, + 0x55, + 0x4B +#else + 0xB8, 0xD0, 0xAA, 0x63, + 0x24, 0x7C, + 0xFF, 0x40, + 0x85, + 0xA8, + 0x64, + 0x0D, + 0x94, + 0x4C, + 0xC3, + 0x25 +#endif + ]; + + return (Guid*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data)); + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs index e5322bf4d..c160c7e8c 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs @@ -233,9 +233,7 @@ public SwapChainManager(TOwner owner, GraphicsDevice device) // IDXGISwapChain reference just created and set that as the swap chain panel to use. fixed (ISwapChainPanelNative** swapChainPanelNative = this.swapChainPanelNative) { - Guid guid = new(0x63AAD0B8, 0x7C24, 0x40FF, 0x85, 0xA8, 0x64, 0x0D, 0x94, 0x4C, 0xC3, 0x25); - - ((IWinRTObject)owner).NativeObject.TryAs(guid, out *(nint*)swapChainPanelNative).Assert(); + ((IWinRTObject)owner).NativeObject.TryAs(*IID_ISwapChainPanelNative, out *(nint*)swapChainPanelNative).Assert(); } // Get the underlying ID3D12Device in use From 277638379ed735c5d6bbae9ab351dc383734d77f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 29 Sep 2024 13:29:09 -0700 Subject: [PATCH 2/2] Refactor IID logic into marshaller type --- src/ComputeSharp.UI/ComputeSharp.UI.projitems | 2 +- .../Helpers/SwapChainManager.IID.cs | 54 --------------- .../Helpers/SwapChainManager.cs | 3 +- .../Helpers/SwapChainPanelNativeMarshaller.cs | 68 +++++++++++++++++++ 4 files changed, 70 insertions(+), 57 deletions(-) delete mode 100644 src/ComputeSharp.UI/Helpers/SwapChainManager.IID.cs create mode 100644 src/ComputeSharp.UI/Helpers/SwapChainPanelNativeMarshaller.cs diff --git a/src/ComputeSharp.UI/ComputeSharp.UI.projitems b/src/ComputeSharp.UI/ComputeSharp.UI.projitems index aa0cebec5..5d500adc7 100644 --- a/src/ComputeSharp.UI/ComputeSharp.UI.projitems +++ b/src/ComputeSharp.UI/ComputeSharp.UI.projitems @@ -17,7 +17,7 @@ - + diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.IID.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.IID.cs deleted file mode 100644 index b9e9fdf8f..000000000 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.IID.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -#if WINDOWS_UWP -namespace ComputeSharp.Uwp.Helpers; -#else -namespace ComputeSharp.WinUI.Helpers; -#endif - -/// -partial class SwapChainManager -{ - /// - /// Gets the IID of . - /// - private static unsafe Guid* IID_ISwapChainPanelNative - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - ReadOnlySpan data = - [ -#if WINDOWS_UWP - 0xD2, 0x19, 0x2F, 0xF9, - 0xDE, 0x3A, - 0xA6, 0x45, - 0xA2, - 0x0C, - 0xF6, - 0xF1, - 0xEA, - 0x90, - 0x55, - 0x4B -#else - 0xB8, 0xD0, 0xAA, 0x63, - 0x24, 0x7C, - 0xFF, 0x40, - 0x85, - 0xA8, - 0x64, - 0x0D, - 0x94, - 0x4C, - 0xC3, - 0x25 -#endif - ]; - - return (Guid*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data)); - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs index c160c7e8c..c999eebd1 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs @@ -20,7 +20,6 @@ using Windows.System; using Windows.UI.Xaml.Controls; #endif -using WinRT; #if WINDOWS_UWP namespace ComputeSharp.Uwp.Helpers; @@ -233,7 +232,7 @@ public SwapChainManager(TOwner owner, GraphicsDevice device) // IDXGISwapChain reference just created and set that as the swap chain panel to use. fixed (ISwapChainPanelNative** swapChainPanelNative = this.swapChainPanelNative) { - ((IWinRTObject)owner).NativeObject.TryAs(*IID_ISwapChainPanelNative, out *(nint*)swapChainPanelNative).Assert(); + SwapChainPanelNativeMarshaller.GetNativeObject(owner, swapChainPanelNative); } // Get the underlying ID3D12Device in use diff --git a/src/ComputeSharp.UI/Helpers/SwapChainPanelNativeMarshaller.cs b/src/ComputeSharp.UI/Helpers/SwapChainPanelNativeMarshaller.cs new file mode 100644 index 000000000..12315dc89 --- /dev/null +++ b/src/ComputeSharp.UI/Helpers/SwapChainPanelNativeMarshaller.cs @@ -0,0 +1,68 @@ +using System; +using System.Runtime.CompilerServices; +using ComputeSharp.Core.Extensions; +using ComputeSharp.Win32; +#if !WINDOWS_UWP +using Microsoft.UI.Xaml.Controls; +#endif +using System.Runtime.InteropServices; +#if WINDOWS_UWP +using Windows.UI.Xaml.Controls; +#endif +using WinRT; + +#if WINDOWS_UWP +namespace ComputeSharp.Uwp.Helpers; +#else +namespace ComputeSharp.WinUI.Helpers; +#endif + +/// +/// A helper type to handle marshalling objects for native interop. +/// +internal static unsafe class SwapChainPanelNativeMarshaller +{ + /// + /// Retrieves the underlying object for an input instance. + /// + /// The input instance to unwrap. + /// A pointer to the resulting object to retrieve. + public static void GetNativeObject(SwapChainPanel managedObject, ISwapChainPanelNative** nativeObject) + { +#if WINDOWS_UWP + ReadOnlySpan data = + [ + 0xD2, 0x19, 0x2F, 0xF9, + 0xDE, 0x3A, + 0xA6, 0x45, + 0xA2, + 0x0C, + 0xF6, + 0xF1, + 0xEA, + 0x90, + 0x55, + 0x4B + ]; +#else + ReadOnlySpan data = + [ + 0xB8, 0xD0, 0xAA, 0x63, + 0x24, 0x7C, + 0xFF, 0x40, + 0x85, + 0xA8, + 0x64, + 0x0D, + 0x94, + 0x4C, + 0xC3, + 0x25 + ]; +#endif + // Unwrap and get the 'ISwapChainPanelNative' interface with the right IID depending on the UI framework + ((IWinRTObject)managedObject).NativeObject.TryAs( + iid: Unsafe.As(ref MemoryMarshal.GetReference(data)), + ppv: out *(nint*)nativeObject).Assert(); + } +} \ No newline at end of file