Advertisement

SharpDX (DX12) and WPF?

Started by May 28, 2018 12:59 PM
7 comments, last by ChuckNovice 6 years, 8 months ago

I have a winforms project that uses SharpDX (DirectX 12). The SharpDX library provides a RenderForm (based on a System.Windows.Forms.Form). 

Now I need to convert the project to WPF instead. What is the best way to do this?

I have seen someone pointing to a library, SharpDX.WPF at Codeplex, but according to their info it only provides support up to DX11.

(Sorry if this has been asked before. The search function seems to be down at the moment)

Hello,

 

You may use the SwapChainPanel that is provided for WPF/UWP. I haven't used it yet but it seems like you only have to hook to the existing swapchain provided by that control. DX12 should work as I've seen people developing DX12 UWP applications for xbox with it. Doc here https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.swapchainpanel

Advertisement

Thanks, but sharpDX (at least how I use it) needs to render to a RenderForm (a winforms control). Is there a tiny example out there somewhere using SharpDX and WPF?

Hello,

 

The RenderForm that you are talking about is just a little utility class used to make the SharpDX demos simpler to read. Ideally you'd want to drop that and handle it yourself so you have full control over everything. Actually if you're moving to WPF you WILL have to drop that RenderForm class. So before hooking to an already existing swapchain I'd suggest turning your code around to drop RenderForm and create your own swapchain so you at least get familiar with it. A swapchain is nothing more than a surface used to present an image to the screen and is internally using one or many textures as buffer.

 

Unfortunately I can't find any SharpDX demo that cover exactly this but there is some C++ examples. SharpDX expose exactly the same functionalities so it should not be too hard to translate a C++ sample. https://code.msdn.microsoft.com/XAML-SwapChainPanel-00cb688b/sourcecode?fileId=99187&pathId=40359581

 

EDIT: It may be even simpler than I thought. Look at this part of the C++ example :

 


//Get backing native interface for SwapChainPanel. 
            ComPtr<ISwapChainPanelNative> panelNative; 
            ThrowIfFailed( 
                reinterpret_cast<IUnknown*>(this)->QueryInterface(IID_PPV_ARGS(&panelNative)) 
                ); 
 
            // Associate swap chain with SwapChainPanel.  This must be done on the UI thread. 
            ThrowIfFailed( 
                panelNative->SetSwapChain(m_swapChain.Get()) 
                ); 

Where "this" is actually the current instance of a SwapChainPanel. It is being queried to retrieve the ISwapChainPanelNative on which you can call SetSwapChain(IntPtr). So basically it sounds like you can create everything normally in SharpDX and pass your swapchain's IntPtr to this method. The IntPtr of a SharpDX swapchain is simply the "NativePointer" property.

Thanks for explaining! I will begin with a small SharpDX example before moving on to my "beast" of code :)

Ah... The problem seems to be now that SwapChainPanel is not available in WPF, Desktop. It's for UWP applications (Windows Store apps, Xbox). So, I still don't see a way forward to run DirectX 12 (with or without SharpDX) in a WPF application. Back to square one again :/ .

Advertisement

Sorry for answering myself again (I seem to be rather lonely in this quest)... The only way forward, as I see it, is to share a surface between DX12 and DX9 (or DX11). Render with DX12 and present with DX9 (D3DImage in WPF). Sounds tricky enough. Any little code example on how to do this would be more than welcome.

1 hour ago, lubbe75 said:

Sorry for answering myself again (I seem to be rather lonely in this quest)... The only way forward, as I see it, is to share a surface between DX12 and DX9 (or DX11). Render with DX12 and present with DX9 (D3DImage in WPF). Sounds tricky enough. Any little code example on how to do this would be more than welcome.

Sorry for the mislead, they sold the SwapChainPanel as the XAML solution to host a swapchain and I assumed that would be both WPF/UWP. Indeed it sounds a bit more complicated now as you found out but their C++ example still should not be too hard to translate to SharpDX since we have the same functions / structures only sometimes with a slightly different name convention https://msdn.microsoft.com/en-us/library/windows/desktop/ee913554.aspx 

This topic is closed to new replies.

Advertisement