Now that Windows 8 Consumer Preview is out we are eager to explore the new framework and the features it offers. While part of it is the good old XAML, there are plenty of other unique features, one of them being the DirectX integration at XAML level. While XAML is a great technology and offers literally unlimited features regarding layout and UI richness, sometimes there are cases where we need to push the framework beyond its limits in order to achieve highly-performing components with responsive and fluid user interaction. Although in most cases writing efficient algorithms and using the proper data structures will be enough to achieve the desired performance, there are scenarios where extensive UI scenes are rendered frequently and the density of the updated pixels is high. So I was most excited by the opportunity to present Direct2D content directly in XAM and started a research on how it actually works and how the performance is affected.
WinRT is a brand new framework and there is little to no information over the Internet about Direct2D and XAML integration, so I decided to summarize my efforts and to hopefully help other people who are doing the same tests. In this post I will show you a simple C++ demo that uses Direct2D and a SurfaceImageSource instance to render a Polyline with 5000 points. Microsoft has done a great job with introducing three different entry points where DirectX content may be directly embedded in a XAML application. The demo also compares the same scenario implemented with a XAML Polyline shape.
Disclaimer: I am not an experienced C++ developer (still :)), so probably there are areas in the code that might be written in a better way. In fact it is @Tim Heuer and a colleague of his that helped us with a flickering issue we had while performing this test – thanks a lot guys!
We will create a SurfaceImageSource that will be passed as a filling of a Rectangle XAML shape.
The sample code is taken directly from the MSDN article about DirectX and XAML Interop and modified to fit the demo:
We will perform the drawing upon each CompositionTarget.Rendering callback so that we can measure the frames-per-second rate:
The dimensions of the updated surface are 1024 by 768 pixels. Point count is 5000. Here is a comparison table that summarizes the results:
|Frames per second (FPS)|
Test are performed on my desktop machine – quad core, 16 GB RAM.
Pretty impressive - outputting a polyline using Direct2D is about 20 times faster. There are some things however that might need additional attention if you decide to output Direct2D content in your applications. Taking the rendering in our hands implies that we will also take care about the user interaction and hit-testing. We will need to facilitate the ID2D1Geometry interface, track input events and perform some manual hit-testing. Indeed, there will be some more code to be written but the most important thing is that it is doable and if you really need better performance you can achieve it with DirectX.
I really love the option to mix XAML and DirectX content. There are many performance-critical areas in a XAML-based application where you can benefit a lot by using DirectX to present some content. Such areas for example are image rendering and image effects, text rendering and text measuring (you can even measure text on a background thread!) plus much more. The code from the demo application can be easily compiled as a WinMD component that might be accessed from C#. The Windows 8 Runtime (WinRT) is really a powerful framework that provides myriad of unique new features. There are so many new things I’ve learned so far that I can’t wait to share them with you - stay tuned for more posts on WinRT and Windows 8 to come :)
Georgi has 15 years of experience in multiple software technologies. Ten years ago, he joined Telerik, later acquired by Progress, and he has set up and led multiple products and teams since. His latest endeavor is building a product that adds the Progress value into the augmented and virtual reality development workflow.
Subscribe to be the first to get our expert-written articles and tutorials for developers!