Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IID for 'ISwapChainPanelNative' on UWP #857

Merged
merged 2 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ComputeSharp.UI/ComputeSharp.UI.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Extensions\IDXGIAdapterExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SemaphoreSlimExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\DynamicResolutionManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SwapChainPanelNativeMarshaller.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SwapChainManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SwapChainManager.Rendering.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IFrameRequestQueue.cs" />
Expand Down
5 changes: 1 addition & 4 deletions src/ComputeSharp.UI/Helpers/SwapChainManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using Windows.System;
using Windows.UI.Xaml.Controls;
#endif
using WinRT;

#if WINDOWS_UWP
namespace ComputeSharp.Uwp.Helpers;
Expand Down Expand Up @@ -233,9 +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)
{
Guid guid = new(0x63AAD0B8, 0x7C24, 0x40FF, 0x85, 0xA8, 0x64, 0x0D, 0x94, 0x4C, 0xC3, 0x25);

((IWinRTObject)owner).NativeObject.TryAs(guid, out *(nint*)swapChainPanelNative).Assert();
SwapChainPanelNativeMarshaller.GetNativeObject(owner, swapChainPanelNative);
}

// Get the underlying ID3D12Device in use
Expand Down
68 changes: 68 additions & 0 deletions src/ComputeSharp.UI/Helpers/SwapChainPanelNativeMarshaller.cs
Original file line number Diff line number Diff line change
@@ -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

/// <summary>
/// A helper type to handle marshalling <see cref="SwapChainPanel"/> objects for native interop.
/// </summary>
internal static unsafe class SwapChainPanelNativeMarshaller
{
/// <summary>
/// Retrieves the underlying <see cref="ISwapChainPanelNative"/> object for an input <see cref="SwapChainPanel"/> instance.
/// </summary>
/// <param name="managedObject">The input <see cref="SwapChainPanel"/> instance to unwrap.</param>
/// <param name="nativeObject">A pointer to the resulting <see cref="ISwapChainPanelNative"/> object to retrieve.</param>
public static void GetNativeObject(SwapChainPanel managedObject, ISwapChainPanelNative** nativeObject)
{
#if WINDOWS_UWP
ReadOnlySpan<byte> data =
[
0xD2, 0x19, 0x2F, 0xF9,
0xDE, 0x3A,
0xA6, 0x45,
0xA2,
0x0C,
0xF6,
0xF1,
0xEA,
0x90,
0x55,
0x4B
];
#else
ReadOnlySpan<byte> 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<byte, Guid>(ref MemoryMarshal.GetReference(data)),
ppv: out *(nint*)nativeObject).Assert();
}
}