Premiere Post! WPF w/ Direct3D Shader Interop and simple databinding

Based on some questions I’ve seen on the forums, with regards to WPF on the issue of Shader use and technologies, I have decided to prepare a sample which illustrates one approach by which DirectX content can be embedded in a WPF app. In particular, I render a single Managed DirectX surface containing a simple mesh rendered with shaders,  in a control embedded in a WPF app, and databound to a set of sliders to manipulate a color shift implemented via a pixel shader. This form can be helpful for teams that want to incorporate WPF as the overall UI technology(in place of GDI, MFC, winforms, etc) of their 3D heavy applications, for which a fair bit of DirectX code is often present.

In this example, I used Managed DirectX API for primary language consistency in this sample, but the HwndHost technology for interop can similarly be used to embed unmanaged DX 9/10. For this overview, I assume an working knowledge of Direct3D, in conjunction with some familiarity with WPF.


1-The application references a custom control containing the Manged DX Device in the XAML layout(For non XAML junkies, keep in mind, this can just as easily be done in code):

Custom Namespaces:


< MDXControl:ManagedDirectXControl>
< MDXControl:ManagedDirectXControl.RenderContent >
< local:AdjustableShader x:Name=“AdjustableShader“ TextureFilename=“Content\gradient1.bmp“/>

2-Here’s the code for the custom control - this is a WindowsFormsHost which allows us to contain Winforms content in WPF. The Xaml visible RenderContent field is used here for encapsulating the rendering behavior we want to execute in DirectX:

public class ManagedDirectXControl : WindowsFormsHost
  public ManagedDirectXControl()
Child = new ManagedDirectXDeviceWrapper();
  public Renderable RenderContent
    get { return ((ManagedDirectXDeviceWrapper)Child).RenderContent; }
    set { ((ManagedDirectXDeviceWrapper)Child).RenderContent = value; }

3-The ManagedDirectXDeviceWrapper code which is contained in the project attached below, is pretty routine DirectX code for initialization and a rendering loop tied to repaint events on the container control. There are many fine tutorials on that subject, so I won't reinvent that wheel of guidance. The wrapper will check for HW PS & VS shaders V1.1, and will silently fall back to software mode. If you would like active notification of the fallback, look at the “_alreadyNagged” field in that class. This class contains hooks which depend on the user supplied Renderable object.

4-Finally, the implementation of “ShaderRender” supplies a generic Quad mesh which gets textured and processed by user supplied shaders. The derived class “AdjustableShader” pre-defines a reference to a particular shader file, and exposes R,G,B properties for data binding use as targets, which provide parameters which are passed on to the pixel shader. Again, the mechanics of this is familiar territory in the DirectX world, so I'm not delving into that aspect here. You can get the VS Solution with projects for the Managed DX wrapper project, and a WPF sample app containing simple data binding to drive the pixel shader inputs for shifting the texture color. That code is available from here.

To work on the project you will need the following installed(Combined pre-req's for DX and WPF):

  1. Visual Studio 2005 
  2. DirectX SDK
  3. WPF Runtime or be running on Vista
  4. Orcas CTP for Visual Studio 2005(to support WPF VS project)

I would be glad to hear any questions, suggestions or other feedback...