DirectX immediate mode rendering in your C#/XAML app using the SharpDx library.

Last week we had a huge Microsoft representation at Eurogamer ranging  from Xbox360, XboxOne, Windows Phone and Windows 8 demonstrating our devcies, services and latest titles. During the event I had a lots of interesting discussions with game devs, educators and students, one of the key themes of discussion was what is the best technology to build my games with?

To address this I think you need to look at the industry and some best practices, hands down Unity is now by far the most popular middleware and what we are seeing is right now some real platform and service differentiators.

One of the cool factor about Win 8 is XAML and its support for D3D11. However one of the key debates from existing Windows Silverlight developers is which skills do I need to learn, my simply comment is if we are given a choice I would suggest that you with go with XAML/C# and this where some great open source project come in such as MonoGame which I have spoken a lot about in the past on this blog and SharpDx.

I know for many games devs building for Windows 8 they have spent a lot of time seeing if there was a way to combine D3D11 and XAML.  I would simply say stop searching and start focusing on doing XAML with some DirectX immediate mode rendering.

The following is a simply walkthrough of how to do some DirectX immediate mode rendering in your C#/XAML app using the SharpDx library. This isnt a new subject and one the original MSDN Blog was published on exactly this, Combining XAML and DirectX. The blog is an excellent starting point and talks about what is possible with DirectX and XAML.

So what is SharpDx

SharpDx is a very thin library that exposes the DirectX api’s for use in C# WinRT apps. SharpDx exposes the DirectX API’s nearly 1-to-1 with no higher level abstraction (like what XNA is).

SharpDX is FREE

You can simply use the SharpDx library to render DirectX content in your XAML/C# apps via the controls mentioned in Combining XAML and DirectX (SurfaceImageHost & SwapChainBackgroundPanel) nt.

I’ll spend this post walking you through how to get this all setup.

Step 1 : Get SharpDx

Current stable version of SharpDX is 2.5.0.

SharpDX binaries are directly downloadable from this web site or available from nuget packages.

SharpDX is now bundled into 2 separate packages, for all platforms and .NET Framework:

  • A Full installer package, Download FullSharpDX-Full-2.5.0.exe that contains binaries and samples. This is the recommanded way to install SharpDX. This installer adds the environement variable $(SharpDXSdkDir) to the system.
  • A Binary only package, Download BinSharpDX-Bin-2.5.0.exe that contains only binaries necessary to developed with SharpDX.
  • Or via Nuget Packages FullNuget Packages, providing binaries for .NET 2.0-4.0 and WinRT, with debugging information and source code stepping.

There is also a Visual Studio Extension available with Project Templates for the Toolkit. Search for SharpDX in Extension Manager in VisualStudio 2012 and install it from there

Step 3 : Build SharpDx code

You can also build directly from the sources.

  1. Get a local copy of the sharpdx repository from the SharpDX gihub repository with this command:
    git clone   // use if you don't have a github account
  2. .NET 3.5 Framework is required to build SharpDX (This is for example not installed by default on Windows 8)
  3. Download and install the DirectX SDK June 2010. SharpDX build is expecting the DXSDK_DIR environment variable to point to the DirectX SDK directory. This variable is setup automatically when installing the DirectX SDK.
  4. Download and install Microsoft Windows 7.1 SDK to compile Net20/Net40 release under Windows 8 / Visual Studio 2012
  5. To build SharpDX for Windows Desktop or Windows RT, open the solution SharpDX.sln and select the target you want to build:
    1. Net20Debug/Net20Release: To build the standard desktop SharpDX for .NET 2.0
    2. Net40Debug/Net40Release: To build the standard desktop SharpDX for .NET 4.0
    3. Win8Debug/Win8Release: To Build SharpDX for Windows 8 Metro (Requires Visual Studio 2012)
    4. Direct3D11_1Debug/Direct3D11_1Release: To Build SharpDX for Windows 7/8 Desktop (Requires Visual Studio 2012)
  6. To build SharpDX for Windows Phone 8, select SHarpDXWP8.sln solution

Step 4 : Using these DirectX-C# libraries to render islands of DirectX in your XAML (SurfaceImageSource)

The SharpDx team have 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,


This rendered surface is just bitmap content so you can mix XAML all over or under it at your convenience!

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.


Let me know how you get on and what interesting games or apps you end up building with SharpDx.

Comments (0)

Skip to main content