One important feature in Windows Phone 8 is ability to develop high performance applications using Direct3D graphics and C++. While this feature clearly targets game developers, many applications could also gain performance combining native code with C++ components. Moreover, even when developing XAML/C# applications, it is possible to fully leverage modern GPU in Windows Phone 8 device by mixing XAML flexibility with Direct3D graphics.
Note: Pure native development using Direct3D are not covered in this post.
Let’s overview two other options: first is to use Direct3D graphics is XAML/C# application. Windows Phone 8 SDK provides very good starting point for such application:
Application created with this template combines standard Windows Phone 8 XAML application and WinRT native component. To integrate Direct3D surface into managed application, use new DrawingSurface XAML element. As standard XAML element it supports all standard XAML properties incl. Projection and RenderTransform and plays nicely with other XAML elements in layout. The following code snippet, for example, produce image below:
The whole idea of this “mix” is very similar to hybrid Silverlight & XNA applications introduces back in Windows Phone 7.5.
The DrawingSurface is initialized with Windows Phone Runtime object that implements the IDrawingSurfaceProvider interface. The default templates provides sample of such component which implements 3D cube render. For more info please refer to documentation.
Second option available for developers is to integrate/reuse native code containing business logic with Windows 8 other other platforms. Also, in many cases integrating native code (especially for complex algorithmic manipulations) could add noticeable performance boost to application. Let’s see how to achieve this.
My sample application uses “heavy” math calculations as managed component:
As you see, this code calculates factorial for given number.
Note: the factorial calculation was not implemented as recursive function to be fully compatible with native code presented below.
The managed library used to calculate the factorials – let’s pretend it is a part of application’s business logic – as follows:
This code snippet invokes factorial calclation for fandom number given number of runs (maxRuns) for known number of trials (numOfTrials) and caluclates average runtime (in ticks).
Let’s have exact same functionality as managed WinRT component. To do it, we need to add new project and reference it from the applcation:
The header file for this component looks super simple:
And implemntation class is also very simple:
Note, that factorial code is exact the same as in managed code.
Once native component is ready it could be referenced from main app:
and used as standard managed component:
Note, that again, only difference is the type of created object.
Runnig this application reveals interesting facts – the bigger number we are trying to calculate factorial for, the bigger gain in performance we are getting with native code.
With maxFactorialOf = 10 the results are not in favor of native code:
If setting the maxFactorialOf = 100, the results are different and while still comparable, the native code clearly wins:
Setting maxFactorialOf = 1000 shows, that native is clearly faster – in this case almost twice faster than managed code:
Setting maxFactorialOf = 10000 allwos native code shine and beat native code completely:
Does it means, that all calculations (even smallest one) should be moved to managed code, just because it is faster? Not necessary. If you are migrating from another platform and have logic already written using native code then the answer probably yes (giving the fact, that native development not as easy as managed one). If you already using managed code your probably not necessary will gain major performance boost just by rewriting some of the business logic as native library. You could try, but “your mileage may vary”. One thing is for sure – it could introduce more bugs related to new native component.
That’s it for this post. Next time I will blog about location services and new map functionality.