So you want to do some DirectX immediate mode rendering in your C#/XAML app ? Let me walk you through how to do that using the SharpDx library
Combining XAML and DirectX is an excellent msdn article that talks about what is possible with DirectX and XAML. Unfortunately there are no examples and what information there is available is targeting the XAML/C++ world. I wanted to do this for us managed programmers!
SharpDx is a very thin library that exposes the DirectX api’s for use in C# WinRT apps . It exposes the DirectX API’s nearly 1-to-1 with no higher level abstraction (like what XNA is). We will use this library to render DirectX content in our XAML/C# apps via the new controls mentioned in the above article (SurfaceImageHost & SwapChainBackgroundPanel)
Alexandre Mutel is the brilliant guy who owns SharpDx, he’s done a lot of the hard work for us BUT help is very scarce with getting these bits working on your Win8 development environment.
I’ll spend this post walking you through how to get this all setup. It’s a simple post that will set the foundation for some awesome UI/UX rich future posts!
Step 1 : Get SharpDx
I used CygWin with the GIT bits installed.
The below statement clones the source into a local WinRT003 directory.
“git clone https://code.google.com/p/sharpdx WinRT003”
Once it finishes cloning locally this is what the structure should look like
Step 2 : Fix up permissions for the local cloned source
When the source clones locally it will be given the permission of the logged in user which is probably your live account.
The problem here is when you try to do VS related things like compiling or deploying source code it needs higher elevated permissions.
The first thing we need to fix are these files
1. “\External\gccxml\bin\gccxml.exe”
2. “\External\gccxml\bin\gccxml_cc1plus.exe”
These are EXE’s that needs “Execute” permission for your logged in account. This file is used to help create the DirectX metadata that will be used to generate the C# DirectX library, which I’ll mention in the 3rd step below.
Step 3 : Build SharpDx code
In the root folder there is a solution file (SharpDX.sln), load this up.
When the solution loads up it will have a build type of “Default” . We need to change this to “Win8Debug”
The SharpDX project is the default project and it has a Pre-Build event
“”$(SolutionDir)Source\Tools\SharpGen\RunGenerator.bat” $(ConfigurationName)”
SharpGen is a tool that basically generates documentation metadata for the DirectX api which will intern be used to create the thin DirectX C# wrapper
I’m not going to go into how this metadata actually works BUT I do suggest spending the couple of hours to work it our for yourself, it’s actually really cool
If all permissions are set correctly, when you build the solution the SharpGen tool will start generating the metadata, then the C# DirectX libraries will get generated from that.
If all goes well then all the Bins for each library should be filled with goodness .. You should have a C# consumable library of DirectX api’s !!!!
For example the picture below shows you the C# generated dll’s that give you access to the Direct3D api’s
So now we can call DirectX api’s via C# !!!
Step 4 : Using these DirectX-C# libraries to render islands of DirectX in your XAML (SurfaceImageSource)
Alexander has some great samples that show how to use these libraries to render DirectX content in your XAML surface via the SurfaceImageSource element.
The 2 “Rectangle” elements below are “Filled” with the SurfaceImageSource DirectX rendered content.
The d3dRectangle will load the SurfaceImageSource content generated via the Direct3D pipeline
The d2dRectangle will load the SurfaceImageSource content generated via the Direct2D pipeline.
This demo shows both pipelines for rendering content, and this will be very important later on in future posts. Of interest to me is the d2d bits which have the EffectGraph stuff
This rendered surface is just bitmap content so you can mix XAML all over or under it at your convenience!
Here is a video of it running
Step 5 : Using these DirectX-C# libraries to render full page DirectX under your XAML (SwapChainBackgroundPanel)
This second sample shows how to render a FULL screen DirectX surface underneath XAML using the SwapChainBackgroundPanel.
Without going into too much detail the SwapChainBackgroundPanel lets you render DirectX content underneath your XAML content, because the XAML content sits within the SwapChainBackgroundPanel as a child.
Here’s a video of it running
Conclusion
For convenience I’ve bundled my locally rendered SharpDx libraries and the Alex’s samples to get you quickly up and running, cause I know all you want is the demos
I’m really excited that we can use managed skills and render immediate mode DirectX graphics within XAML.
We may not have gotten XNA BUT I honestly believe we have something better with DirectX …
Incidently if you do want an XNA like framework for WinRT XAML, then you should look at ANX (which is XNA backwards). It’s written ontop of SharpDx and has some higher level abstractions like SpriteBatch, ResourceManager, Pipeline etc.
In future posts I will use these SharpDx libraries to render some cool animations that play nicely with XAML!!
Pingback: DirectX in your WinRT-XAML/C# apps (SharpDx) | Silverlight and other cool things …
Pingback: DirectX in your WinRT-XAML/C# apps (SharpDx)
Fantastic post! I’m interested to know what are the caveats of building applications using C#/Xaml vs. C++/Xaml? I have a high-performacne chart component (see http://www.scichart.com) I’d like to port to Windows8 in the near future.
This is written in C#/Xaml (WPF/Silverlight) and uses mostly immediate mode rendering via WriteableBitmapEx to achieve high performance. If I converted to C#/Xaml Win8 and mix a little SharpDX, it would be an easy port, but could the component then be used in Metro and desktop apps? Or only if it was C++/Xaml?
Great article, i just downladed your samples and im getting this error any sugestion?
An exception of type ‘SharpDX.SharpDXException’ occurred in SharpDX.DLL but was not handled in user code
Additional information: HRESULT: [0x80070057], Module: [Unknown], ApiCode: [Unknown/Unknown], Message: The parameter is incorrect.
To mention im doing this on a Virtual machine windows 8 consumer preview.
Francisco : The folder you downloaded the source too, check the security tab and add the “Administrators” group to it. You may also need to click on the advanced button and make sure the “Administrators” permissions flows down to all the child subfolders/files too!
This sounds like you may have a permission problem running the app ?!
AndyB : I’ll reply to you in a separate post, my thoughts on this are too big for a comment. Stay tuned 🙂 p.s. I like your controls alot, I have you on my twitter and we’ve discussed things before.
advertboy: cool thanks! If you want drop me an email, andrew (at) abtsoftware (dot) co (dot) uk. I’d be interested to chat and pick up some Win8/Xaml/DX knowledge!
Francisco : what you might also want to keep an eye on is go to the start menu tile screen and make sure that the app hasn’t installed itself and that install is causing issues. Just go to the tile and uninstall it if it is there!
How about posting this tutorials source to github? 🙂
🙂 i will definetely, i just need to familiarize myself with GIT ..
Hi i have changed the permisions
but still the exeption is happening can you help me to run this.
https://skydrive.live.com/redir.aspx?cid=3a9d6c68cf6de5ba&resid=3A9D6C68CF6DE5BA!570&parid=root
Hi i have resolved this, it happens that in the virtual machine it crash, so i install on pc new partition and work just fine.
Thats great to hear … You might want to start using the SharpDx source code now instead of the dlls’ i’ve built 🙂 Live dangerously, and blog everything !!
Pingback: Interesting Windows 8 Links – 2012-04-13 | Dan Rigby
Pingback: There’s nothing to see here | Windows Live space
Hello, I’ve looked at the samples, but I’m having a hard time drawing a simple rectangle with a radial gradient on it. More exactly, I don’t know where to get the RenderTarget for the brush from. I’ve worked a bit with OpenGL in the past, but that doesn’t seem to be of any help. Any advice?
Hello, Will apps written using SharpDX pass certification requirements for Windows 8 metro apps due to Microsoft API requirements?
Yes i already passed certification on a game using SharpDX