Why does Win2D include three different sets of vector and matrix types?


tl;dr:

In more detail:

Win2D includes the C# math types in System.Numerics, a C++ version in Windows::Foundation::Numerics, and a Windows Runtime (WinRT) version in Microsoft.Graphics.Canvas.Numerics.  Why so many?

The C# and C++ namespaces provide identical functionality, but are tuned differently to fit the style and conventions of each language.  System.Numerics is almost identical to the established XNA Game Studio math API:

    using System.Numerics;

    Vector3 bounce = Vector3.Reflect(velocity, Vector3.Normalize(normal));

Windows::Foundation::Numerics rearranges this same functionality to match HLSL and C++ standard naming conventions, using lower_case and free function overloads instead of static methods:

    #include <WindowsNumerics.h>

    using namespace Windows::Foundation::Numerics;

    float3 bounce = reflect(velocity, normalize(normal));

Neither version is SIMD optimized yet, but eventually both will be.  This won’t change the API, but internally we’ll take advantage of RyuJIT for .NET and DirectXMath for the C++ implementation.

The Microsoft.Graphics.Canvas.Numerics namespace is an implementation wart that needs to exist for technical reasons I won’t go into here  (the details are relevant to people building WinRT components, but not to those who just want to use the API surface – I may write more about this later if there is interest).  Eventually we would like to hide this namespace entirely, but until we manage to do that please just pretend like it didn’t exist.  Any time you see it mentioned, use the equivalent type from System.Numerics (if you are a C# developer) or Windows::Foundation::Numerics (if you are working in C++) instead.  For instance the ICanvasBrush.Transform property is of type Microsoft.Graphics.Canvas.Numerics.Matrix3x2, but to set this property from C# you can write:

    using System.Numerics;

    brush.Transform = Matrix3x2.CreateRotation(angle) *
                      Matrix3x2.CreateScale(2);


Comments (1)

  1. MtVernonCannibisFarms says:

    %50 of desktop is still win7 , any chance of seeing this in vNext for backward compatibility

Skip to main content