I was looking for quite some time find a way to debug my GPU code in WPF applications. WPF is quite nice because of scalable, transparent and easy to build user interfaces. At the moment there are a few drawbacks to make it a “perfect” solution (at least for me).
E.g. the XAML Syntax of Windows WPFs and Windows8 (formerly Metro-Apps) is slightly different (the control base classes differ and so the namespace but this is not the problem). My point is the different syntax for including namespaces! And the absence of preprocessor macros. So one needs to create two nearly the same XAML Files or completely define the UI in code if both targets should be addressed. (If you know something I missed please leave a notice ;)
The problem this thread is about is the missing debugging capability. The common graphics debuggers have problems to attach to WPF Applications because of the way you create your “backbuffer” for render panels. The only way I know is to create a texture that is shared between DirectX11 and 9. The DX9 texture is bound as ImageSource to an image that is then drawn by the window management (using Direct2D?). As you may notice this concept makes no use of a SwapChain and so there is now call to present this swap chain what I believe is the reason why PIX and other debuggers can’t capture a frame.
If you like to use WinForms instead of WPF there is no problem. You create your SwapChain with a handle of the window or of a panel and everything works fine.
After some time trying different approaches I found in the net or ideas of friends like these:
Embed the mentioned WinForms panel via Host into the WPF Application. Or the opposite direction to embed the WPF UI via Host into a WinForms Application. Both work but there is no way to get Transparency working so the UI is completely opaque. Because I would like to define complete pages for my UI there is no pixel left to see of the rendered scene I’m not sure at the moment but I think the debugging problem was still there in both approaches.
Now to my solution that works (at the moment):
Starting from the standard WPF solution with a shared Texture bound as ImageSource.
Then create a hidden WinForms Form before starting the WPF application (this requires an own application entry point: static main). Use this form to initialize your graphics device and create a SwapChain. Then create and open you WPF Window and listen to CompositionTarget.Rendering. When this event is fired, draw all your content to your render panels and then present the SwapChain of the hidden window (this will allow the graphics debugger to capture the frame). Finally we need to start the main loop. This is not the one of the WPF application! Instead start the RenderLoop of SharpDX (or the message pump equivalent of SlimDX). The function you define as main loop then performs nothing. It just keeps our application running and all the render passes are initiated by the composition target.
I hope you could follow me. A Sample can be found in the attachments and here http://sdrv.ms/Q6k8bE. I worked this out on Windows 8 and Visual Studio 2012 and used the build in Graphics Debugger. I tested it with PIX, too. There the capturing works, but the scene isn’t visible while the application runs. I also tried the Intel GPA but unfortunately it crashes during startup. If you have ideas to fix this you are very welcome ;)
One point I left out in this Sample. One should use this approach not for release builds because the hidden window is very dirty
I would like you to give me some feedback and discuss about improvements.
Edited by derKai, 05 September 2012 - 08:03 AM.