From c32e5b3f1a5eb515cc58383ab8a6fe53e84cec24 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 4 Feb 2022 01:40:29 +0100 Subject: [PATCH] Improve CsWinRT native IUnknown object retrieval --- .../Helpers/SwapChainManager.cs | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs index 1c97e3c6..835faaca 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs @@ -1,5 +1,10 @@ using System; using System.Diagnostics; +#if WINDOWS_UWP +using System.Runtime.InteropServices; +#else +using System.Runtime.CompilerServices; +#endif using System.Threading; using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Helpers; @@ -17,7 +22,6 @@ using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using TerraFX.Interop.WinRT; #if WINDOWS_UWP -using System.Runtime.InteropServices; using Windows.Foundation; using Windows.UI.Xaml.Controls; using Windows.System; @@ -217,23 +221,20 @@ internal sealed unsafe partial class SwapChainManager : NativeObject // IDXGISwapChain reference just created and set that as the swap chain panel to use. using ComPtr swapChainPanelNative = default; -#if WINDOWS_UWP using (ComPtr swapChainPanel = default) { +#if WINDOWS_UWP swapChainPanel.Attach((IUnknown*)Marshal.GetIUnknownForObject(owner)); swapChainPanel.CopyTo(swapChainPanelNative.GetAddressOf()).Assert(); - } #else - IUnknown* swapChainPanel = (IUnknown*)((IWinRTObject)owner).NativeObject.ThisPtr; - Guid iSwapChainPanelNativeUuid = new(0x63AAD0B8, 0x7C24, 0x40FF, 0x85, 0xA8, 0x64, 0x0D, 0x94, 0x4C, 0xC3, 0x25); + swapChainPanel.Attach((IUnknown*)((IWinRTObject)owner).NativeObject.GetRef()); - swapChainPanel->QueryInterface( - &iSwapChainPanelNativeUuid, - (void**)&swapChainPanelNative).Assert(); - - GC.KeepAlive(owner); + swapChainPanel.CopyTo( + (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(new Guid(0x63AAD0B8, 0x7C24, 0x40FF, 0x85, 0xA8, 0x64, 0x0D, 0x94, 0x4C, 0xC3, 0x25))), + (void**)swapChainPanelNative.GetAddressOf()).Assert(); #endif + } // Get the underlying ID3D12Device in use fixed (ID3D12Device** d3D12Device = this.d3D12Device)