So, my issue is that I do not know how to get an 'IntPtr' or something of the sort from a 'SwapChainPanel' in UWP. I am trying to make a DirectX 11 application using the popular 'SharpDX' binding set, but I cannot figure out how to solve this problem.
Here's my code:
public static void Initialize(SwapChainPanel panel)
{
FeatureLevel[] featureLevels =
[
FeatureLevel.Level_11_1,
FeatureLevel.Level_11_0
];
SwapChainDescription swapChainDescription = new()
{
BufferCount = 1,
ModeDescription = new((int)panel.ActualWidth, (int)panel.ActualHeight, new Rational(60, 1), Format.R8G8B8A8_UNorm),
Usage = Usage.RenderTargetOutput,
SampleDescription = new SampleDescription(1, 0),
IsWindowed = true,
SwapEffect = SwapEffect.Discard,
Flags = SwapChainFlags.AllowModeSwitch
};
SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, featureLevels, swapChainDescription, out device, out swapChain);
using (var dxgiDevice2 = device.QueryInterface<SharpDX.DXGI.Device2>())
{
using Adapter dxgiAdapter = dxgiDevice2.Adapter;
using Factory2 dxgiFactory2 = dxgiAdapter.GetParent<Factory2>();
SwapChain swapChain1 = new(dxgiFactory2, device, swapChainDescription);
panel.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
panel.SwapChain = swapChain1.QueryInterface<ISwapChainPanelNative>(); //<-- Error here
}).AsTask().Wait();
}
using (var backBuffer = swapChain.GetBackBuffer<Texture2D>(0))
{
renderTargetView = new RenderTargetView(device, backBuffer);
}
device.ImmediateContext.OutputMerger.SetRenderTargets(renderTargetView);
device.ImmediateContext.Rasterizer.SetViewport(new Viewport(0, 0, (int)panel.ActualWidth, (int)panel.ActualHeight));
}
And now trying a different approach, If I were to try and get a window handle:
private static IntPtr GetInteropWindowHandle(this SwapChainPanel panel)
{
IInspectable interop = (IInspectable)panel;
object interopObj = Marshal.GetObjectForIUnknown(interop.NativePointer); //<-- Error here
IntPtr windowHandle = IntPtr.Zero;
if (interopObj is PropertySet propertySet)
{
if (propertySet.TryGetValue("SwapChainInteropHandle", out object value))
windowHandle = (IntPtr)value;
}
return windowHandle;
}
And the way I would use the above:
SwapChainDescription swapChainDescription = new()
{
BufferCount = 1,
ModeDescription = new((int)panel.ActualWidth, (int)panel.ActualHeight, new Rational(60, 1), Format.R8G8B8A8_UNorm),
Usage = Usage.RenderTargetOutput,
OutputHandle = panel.GetInteropWindowHandle()
SampleDescription = new SampleDescription(1, 0),
IsWindowed = true,
SwapEffect = SwapEffect.Discard,
Flags = SwapChainFlags.AllowModeSwitch
};
I tried using ChatGPT 4, and looking on the SharpDX forums, and looking at Microsoft Learn, but alas, I could not find a way to do this.