Search code examples
c#xamldirectxvsixsharpdx

Exception when trying to obtain a reference to the native COM object of the SwapChainPanel in Windows App SDK C# using SharpDX and DXD11


I am trying to obtain a reference to the native COM object of the SwapChainPanel created in Windows App SDK C# using SharpDX.

I migrated my project from UWP to the newer Windows App SDK with WinUI 3 and .Net 6 and everything works except the creation of the SwapChainPael.

UWP: https://github.com/CanTalat-Yakan/UWP-GameEngineEditor/blob/main/Editor/Views/View_Port.xaml.cs https://github.com/CanTalat-Yakan/UWP-GameEngineEditor/blob/main/Editor/Assets/Engine/Utilities/Engine_Renderer.cs

New Windows App SDK: https://github.com/CanTalat-Yakan/3D_Engine-WinUI3/blob/main/WinUI3DEngine/WinUI3DEngine/WinUI3DEngine/UserControls/ViewPort.xaml.cs https://github.com/CanTalat-Yakan/3D_Engine-WinUI3/blob/main/WinUI3DEngine/WinUI3DEngine/WinUI3DEngine/Assets/Engine/Utilities/CRenderer.cs

The part with said Error is this CRenderer:71(ctor)

        // Obtain a reference to the native COM object of the SwapChainPanel.
        using (var nativeObject = ComObject.As<DXGI.ISwapChainPanelNative2>(m_SwapChainPanel))
            nativeObject.SwapChain = m_SwapChain;

Exception Details:

SharpDX.SharpDXException HResult=0x80004002 Message=HRESULT: [0x80004002], Module: [General], ApiCode: [E_NOINTERFACE/No such interface supported], Message: No such interface supported

Source=SharpDX StackTrace: at SharpDX.Result.CheckError() at SharpDX.ComObject.QueryInterface(Guid guid, IntPtr& outPtr) at SharpDX.ComObject.QueryInterfaceT at SharpDX.ComObject.As[T](Object comObject) at WinUI3DEngine.Assets.Engine.Utilities.CRenderer..ctor(SwapChainPanel _swapChainPanel) in D:\Misc\Sourcetree\Engine\WinUI3DEngine\WinUI3DEngine\WinUI3DEngine\Assets\Engine\Utilities\CRenderer.cs:line 71 at WinUI3DEngine.Assets.Engine.CEngine..ctor(SwapChainPanel _swapChainPanel, TextBlock _textBlock) in D:\Misc\Sourcetree\Engine\WinUI3DEngine\WinUI3DEngine\WinUI3DEngine\Assets\Engine\CEngine.cs:line 18 at WinUI3DEngine.UserControls.ViewPort.Initialize(Object sender, RoutedEventArgs e) in D:\Misc\Sourcetree\Engine\WinUI3DEngine\WinUI3DEngine\WinUI3DEngine\UserControls\ViewPort.xaml.cs:line 41 at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.b__1_0(Object sender, RoutedEventArgs e) at ABI.Microsoft.UI.Xaml.RoutedEventHandler.<>c__DisplayClass10_0.<Do_Abi_Invoke>b__0(RoutedEventHandler invoke) at WinRT.ComWrappersSupport.MarshalDelegateInvoke[T](IntPtr thisPtr, Action`1 invoke) at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)

I tried debuging from the beginning of the process and everthing is similar to the UWP version. With the new Windows App SDK the namespace of Windows.UI.Xaml.Controls changed to Microsoft.UI.Xaml.Controls I don't know much about the SharpDX wrapper, but could that be the problem? Maybe SharpDX simply doesn't support .Net 5 or 6 only 4 and below? Do I need to setup the creation of the Device -Contex and lastly the SwapchainPanel differently?

If I get a solution, if it was my mistake or another, I will post them here below


Solution

  • Basically

    windows.ui.xaml.media.dxinterop::ISwapChainPanelNative which works with the UWP Windows.UI.Xaml.Controls.SwapChainPanel control

    doesnt work with the

    WinUI3 Microsoft.UI.Xaml.Controls.SwapChainPanel control, which implements a different interface which has the same name but a new GUID (63aad0b8-7c24-40ff-85a8-640d944cc325) and namespace: microsoft.ui.xaml.media.dxinterop::ISwapChainPanelNative

    Notice the namespace difference of Windows vs Microsoft https://githubhot.com/repo/amerkoleci/Vortice.Windows/issues/91

    here would be a workaround: https://github.com/amerkoleci/Vortice.Windows/discussions/164

    but as SharpDX is depracted, I switched to an updated wrapper Vortice https://github.com/amerkoleci/Vortice.Windows https://github.com/amerkoleci/Vortice.Windows/tree/main/src

    which supports the WinUI 3 and the new Windows App SDK (new project reunion)