Sharpening your Metro C#/XAML projects with DirectX and SharpDX
- Posted: Apr 09, 2012 at 6:00AM
- 16,215 views
- 2 comments
Loading user information from Channel 9
Something went wrong getting user information from Channel 9
Loading user information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
I've been keeping on eye on the SharpDX project for a while, waiting on a killer demo. I think I've found it, and in a way I wasn't really expecting. These post details not only how to use SharpDX, but how to use them in a Windows 8 Metro Style app too!
First a snip about SharpDX;
SharpDX is a full DirectX framework for the .NET Platform, including the newest DirectX technology. The top three features of SharpDX:
- SharpDX is providing the latest DirectX and Multimedia API to .Net with true AnyCpu assemblies, running on .Net and Mono.
- SharpDX is ready for next generation DirectX 11.1 technology and Windows 8 Platform. You can start to develop DirectX Metro style App in C# under Win8
- SharpDX is the fastest Managed-DirectX implementation.
SharpDX is using a custom code generator from C++ headers in order to generate an efficient .Net interoperability with C++ DirectX API.
The key features and benefits of this API are:
- Support for the following DirectX API:
- Direct3D9 & Ex
- Direct3D11 and Effects11
- Direct3D11.1 (Win8 only)
- Direct2D1 (including custom rendering, tessellation callbacks)
- Direct2D1.1 (Win8 only)
- DirectWrite (including custom client callbacks)
- DXGI 1.1
- DXGI 1.2 (Win8 only)
- DirectInput 8
- DirectSound 8
- API is generated from DirectX SDK headers : meaning a complete and reliable API and an easy support for future API.
- Managed platform independent .NET API : assemblies are compiled with AnyCpu target. You can run your code on a x64 or a x86 machine with the same assemblies, without recompiling your project.
- Lightweight individual assemblies : a core assembly - SharpDX - containing common classes and an assembly for each subgroup API (Direct3D10, Direct3D11, DXGI, D3DCompiler...etc.). Assemblies are also lightweight.
- Fast Interop : the framework is using a genuine way to avoid any C++/CLI while still achieving better performance than existing managed API. Check this benchmark.
- API naming convention mostly compatible with SlimDX API.
- Raw DirectX object life management : No overhead of ObjectTable or RCW mechanism, the API is using direct native management with classic COM method "Release".
- Easily mergeable / obfuscatable : If you need to obfuscate SharpDX assemblies, they are easily obfusctable due to the fact the framework is not using any mixed assemblies. You can also merge SharpDX assemblies into a single exe using a tool like ILMerge.
- Easily deployable as SharpDX are plain .NET assemblies, there is no need to install VC runtime or .NET GAC install.
- An integrated math API directly ported from SlimMath
An optional assembly SharDX.Diagnostics is delivered and can be added to your project in order to have an explicit error messages when there is a DirectX functions returning an error code. This assembly can be used in development but is not mandatory.
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
Step 2 : Fix up permissions for the local cloned source
Step 3 : Build SharpDx code
Step 4 : Using these DirectX-C# libraries to render islands of DirectX in your XAML (SurfaceImageSource)
Step 5 : Using these DirectX-C# libraries to render full page DirectX under your XAML (SwapChainBackgroundPanel)
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!!
If you click through the sample project is available for download too. And the series doesn't end there...
Thanks to SharpDx we can now create complex effect graphs (in code) in our WinRT-XAML/C# apps.
I took the Xaml/Cx D2DBasicEffectGraph sample from the consumer preview and pretty much redid it to work with SharpDx for us managed programmers
1. Creating the Effect Graph
2. Rendering the Graph on a Surface
3. Here’s a quick video of it in action
4. Here’s the source of the sample:
Effect graphs are very powerful and it’s the most exciting feature of Dx11.1 in my opinion. I can’t wait to show you guys more stuff that I’ve been dreaming up with WinRT/XAML/DirectX/C# and of course SharpDx
If you thought XNA not currently being available to help build Windows 8 Metro Style app's meant that you had to jump to C++ to take advantage of DirectX, you should check this project out...