1

I bought this book "Introduction to 3D Programming with DirectX 11" hoping I could port everything to C# using SharpDX, because I don't know C++. Well, I can't seem to find SharpDX's wrapper classes for XNA Math and this book deals extensively with that library. Is there something else I could get, some XNA Math C# wrapper I could download so I could keep studying?

FinnTheHuman
  • 1,035
  • 10
  • 28
  • Wait, a DirectX book uses XNA libraries? – Cameron Jul 31 '14 at 17:54
  • Yes. It's called XNA Math, and it does a lot more than XNA Game Studio's MathHelper. This lib even uses special registers and istructions, something called SIMD that's supposed to perform really fast. – FinnTheHuman Jul 31 '14 at 19:32
  • Fascinating. I always thought XNA was just a managed thing, but it seems the underlying math library has a separate life of its own. The new version is apparently called DirectXMath. And yes, SIMD is a must for any decent matrix/vector heavy library (e.g. it allows you to add two vectors together in a single instruction). Since it's an unmanaged library, it looks like you'll have to wrap it yourself, unless someone else has already done so. – Cameron Jul 31 '14 at 20:10
  • XNA was unfortunately a monstrously confusing brand. In theory it was supposed to have broader meaning, but in the end it only ever ended up meaning "XNA Game Studio" i.e. C# game development you can deploy to the Xbox 360. – Chuck Walbourn Jul 31 '14 at 21:22

1 Answers1

1

Both MonoGame and SharpDX have math structures equivalent to the those in the original XNA Game Studio math library. For example, see Vector2 in the SharpDX namespace.

UPDATED

Note that Frank Luna's book is for C++ development. The reference to "XNAMath" here is actually to the C++ SIMD math library that shipped in the DirectX SDK. The latest version of which is DirectXMath in the Windows 8.x SDK.

In general it's pretty easy to change C++ code written for XNAMath to use DirectXMath. See MSDN for the details.

The old D3DXMath math library that shipped in D3DX9 was just replicated in D3DX10 and was never brought forward to D3DX11 at all. You can find some suggestions on moving from D3DXMath to DirectXMath here.

None of which really has anything to do with SharpDX which is a C# wrapper for Direct3D. Exactly like XNA Game Studio, SharpDX and MonoGame both contain their own C# math library because marshalling for each vector operation is exceptionally inefficient.

Note that there is a C++ wrapper for DirectXMath called SimpleMath which is included in the DirectX Tool Kit which provides "XNA Game Studio" style math types such as Vector2, Vector3, Matrix, etc. It is implemented using DirectXMath and trivially interops with DirectXMath.

Chuck Walbourn
  • 28,931
  • 1
  • 45
  • 72
  • I don't think this xnamath.h thing is XNA Game Studio's MathHelper. It's a C++ lib with typedefs, pointers and all. It's meant to take advantage on hardware to perform really fast – FinnTheHuman Jul 31 '14 at 19:38
  • Here's a quote from the book: "The D3DX library for version 11 no longer ships with 3D math code. Instead it ships with the XNA Math library, which is a vector math library developed separately from Direct3D that takes advantages of special hardware registers on Windows and XBox 360. On Windows, the library uses the SSE2 (Streaming SIMD Extensions 2) instruction set. With 128-bit wide SIMD (single instruction multiple data) registers, SIMD instructions can operate on four 32-bit floats or ints with one instruction." – FinnTheHuman Jul 31 '14 at 19:48
  • "XNA Math" here does not refer to the C# XNA Game Studio math library. XNAMath is a C++ library that shipped in the DirectX SDK, and is also known as "Xboxmath version 2". The same library is now known as [DirectXMath](http://blogs.msdn.com/b/chuckw/archive/2012/03/27/introducing-directxmath.aspx) and is included in the Windows 8.x SDK. Again, this is for C++ and is not related to C# development. I had assumed you were asking about C# since you were mentioning SharpDX. – Chuck Walbourn Jul 31 '14 at 21:20
  • I was asking about C#, if there was a way to use those special instructions that perform fast vector calculations using C# instead of C++, because I don't know C++ and I'd like to learn directX but I'd like to use C# instead. I understand XNA Math is something different from XNA Game Studio and I undrestand it is not implemented in C# but I was hoping maybe there was a way around it, since there's SharpDX that wrapps DirectX, maybe there's something that lets you use all the perks of fast vector instructions without the need to delve into C++ – FinnTheHuman Aug 01 '14 at 02:18
  • Math is a bit of a special case. In C# if you switched to native code for say "Vector3.Normalize" and returned, it would spend most of that time just doing the switch out of and back to the managed code (called marshalling). As such, it makes much more sense to just write Vector3.Normalize in pure C#. This means it is up to the JIT to make good floating-point code which unfortunately is a bit of a weak area for C# JIT. There are a few projects to get SIMD code-gen support in C# (see [RyuJIT](http://blogs.msdn.com/b/clrcodegeneration/archive/2014/04/03/ryujit-ctp3-how-to-use-simd.aspx)]). – Chuck Walbourn Aug 01 '14 at 03:08
  • I see now. Thank you very much! :) – FinnTheHuman Aug 01 '14 at 09:13