• Create Account

## Using Directx11 without using a Microsoft math library

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1tragic  Members

943
Like
0Likes
Like

Posted 04 May 2014 - 10:30 PM

So as the title states I want to try to use my own math lib (please don't tell me not to reinvent the wheel i enjoy learning and thats what im doing) with directx11 in place of DirectXMath. It would be greatly appreciated if someone could point me in the right direction or even show how this done.

### #2Nik02  Members

4072
Like
4Likes
Like

Posted 04 May 2014 - 10:50 PM

Direct3D has no special requirements about math libraries you happen to use with it.

In general, when you have calculated something on the CPU side, you send the result to the device in a constant buffer or a texture (or some other buffer types). The system does not care what data goes in there; it is the responsibility of the shaders to load the data and do something meaningful with it.

The shader stages are optimized to work with single-precision floating point numbers (float) so it would be beneficial if a CPU-side math library should be able to output them.

Note that internally, DirectXMath matrices and vectors are just arrays of float values. The library does impose some alignment restrictions to the types in order to leverage accelerated vector processing such as SSE2, but other than that, it is just basic math.

Niko Suni

### #3Hodgman  Moderators

49386
Like
6Likes
Like

Posted 04 May 2014 - 10:57 PM

POPULAR

Yeah D3D doesn't come with a math library so has no requirements really.

The way that you construct your matrices will have an effect on your HLSL code.

By default, matrices in HLSL are stored in column-major element order - e.g. for [column,row], the floats are stored in the order [0,0], [0,1], [0,2], [0,3], [1,0], [1,1], etc....

You can explicitly state the storage convention you want to  use by using the appropriate keyword in front of your declaration, like this:

column_major float4x4 myMat;

or

row_major float4x4 myMat;

If you use the column-vector convention in your maths, then you'll write mul(myMat, myVec), otherwise if you're using the row-vector convention you'll write mul(myVec, myMat).

Edited by Hodgman, 05 May 2014 - 12:01 AM.

### #4BarrySkellern  Members

1492
Like
3Likes
Like

Posted 05 May 2014 - 02:16 AM

You can also change the default to row-major storage by specifying the /Zpr flag when compiling your shaders. It's probably a good idea to use the same convention for everything across your project.

Stroids, a retro style mini-game for Windows PC. http://barryskellern.itch.io/stroids

### #5NightCreature83  Members

4779
Like
2Likes
Like

Posted 05 May 2014 - 03:56 AM

Yeah D3D doesn't come with a math library so has no requirements really.

The way that you construct your matrices will have an effect on your HLSL code.

By default, matrices in HLSL are stored in column-major element order - e.g. for [column,row], the floats are stored in the order [0,0], [0,1], [0,2], [0,3], [1,0], [1,1], etc....

You can explicitly state the storage convention you want to  use by using the appropriate keyword in front of your declaration, like this:

column_major float4x4 myMat;

or

row_major float4x4 myMat;

If you use the column-vector convention in your maths, then you'll write mul(myMat, myVec), otherwise if you're using the row-vector convention you'll write mul(myVec, myMat).

You can also just tell the runtime when compiling your shaders which order to use with a compile flag.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max

### #6Mona2000  Members

1945
Like
2Likes
Like

Posted 05 May 2014 - 04:03 AM

Or you can write #pragma pack_matrix(row_major/column_major) to set the default storage convention for everything that comes after.

### #7Jason Z  Members

6411
Like
0Likes
Like

Posted 06 May 2014 - 11:41 AM

As far as examples, my own engine doesn't use DirectXMath.  I based my math classes on the Wild Magic engine by Dave Eberly long ago, and they have evolved a little since then.  If you have any specific questions, please be sure to post them and I'm sure there are answers around here!

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

### #8tragic  Members

943
Like
1Likes
Like

Posted 08 May 2014 - 07:31 PM

Sorry for late reply but i have been rather busy recently but anyways thanks for the advice. Good to know that microsoft doesn't lock you into using their stuff and I do plan on using consistent conventions for making changes and use easier. I browsed through a little bit of your engine Jason and looks like a solid place to start learning .

### #9Jason Z  Members

6411
Like
0Likes
Like

Posted 09 May 2014 - 09:22 AM

Thanks - if you have any questions about the engine, feel free to post here or in the codeplex discussion forum.  I check both regularly, and I'm always happy to discuss the various design options.

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

### #10tragic  Members

943
Like
0Likes
Like

Posted 09 May 2014 - 05:54 PM

Thanks - if you have any questions about the engine, feel free to post here or in the codeplex discussion forum.  I check both regularly, and I'm always happy to discuss the various design options.

For sure thank you!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.