• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
panicq

DirectXMath Calling Convention

8 posts in this topic

Hello,

 

I'm new to directX and I'm reading a book where the author talks about "Calling conventions", and it's quite obscure to me even with the doc. Could you explain to me why in functions like

 

void XMStoreFloat2(XMFLOAT* dest, FXMVECTOR V);

 

...we don't pass the XMVECTOR as a reference or a pointer ? And why are we using alias like FXMVECTOR, GXMVECTOR, or CXMMATRIX  ? And why is there an order of "input parameters", and why we always need to return a pointer and a reference of XMVECTOR ?

 

I understood that there is a link with the "Single Instruction Multiple Datas" way of computing instructions in the CPU but I dont cleary understand what the link is.

 

Thanks in advance.

 

 

 

0

Share this post


Link to post
Share on other sites

Hi.

DirectXMath offers lots of opportunities in efficiently using SIMD instructions and different sorts of intrinsics.

It depends a bit on your skills/ knowledge how much of this you want to use, manual memory alignments etc. (__M128).

 

But.. I am however, not that skilled to user everything, so here's a way you can use it without too much hassle and with a good balance in needed time/ gained effort:

 

- for all vector and matrix members of classes, I use XMFLOAT (2/3/4X4 etc.), so you don't need to do manual memory alignment

- in all functions with their own scope, including member functions, I use XMVECTOR or XMMATRIX

- using the XMStoreFloat and XMLoadFloat functions, you can easily read out your 'members' and 'save' them back.

This way for the calculations you will get the avantage of the intrinsics, memory efficiency etc.

 

Two examples:

void CD3dmeshInst::Transform(const DirectX::XMFLOAT4X4 &pMatrix)
{
	XMMATRIX xmWorldOld = XMLoadFloat4x4(&mMatWorld);
	XMMATRIX xmTransform = XMLoadFloat4x4(&pMatrix);

	XMMATRIX xmWorldNew = XMMatrixMultiply(xmWorldOld, xmTransform);
	XMStoreFloat4x4(&mMatWorld, xmWorldNew);

	mTransformed = true;
}
// transforming a vector with a matrix

	XMVECTOR xmCenterOld = XMLoadFloat3(&mSphereCenterModel);
	XMMATRIX xmMatWorld = XMLoadFloat4x4(&mMatWorld);
	XMVECTOR xmCenterNew = XMVector3TransformCoord(xmCenterOld, xmMatWorld);
	XMStoreFloat3(&mBoundingSphere.WorldCenter, xmCenterNew);

Good luck.

0

Share this post


Link to post
Share on other sites

The FXMVECTOR,CXMVECTOR ect existance is explined by different function call types. there is one specal function call called __vectorcall. The special thing about it is that first 3(if i remember correcltly) arguments of the function are passed via xmm registers. however this is not avaiable for all platforms(for example when compiling on ARM ach).

 

EDIT: I forgot..... Passing __m128 (aka. XMVECTOR) has some special rules. For example reference arg wont work for __vectorcall functions. Correct me if Im wrong.

Personally I love the D3D API, but I detest DirectXMath (in terms of usage). It won't give you any performance just by using it. The library will only add complexity to your code. The real performance gains are when you oprate on a big chunk of coninious data that is highly vertor friendly(most operations are not vector friendly). Try to avoid that library if possible, use it only for really really *slow/frequent* and vertor friendly code, don't use it as a general 
purpose math library.

 

Why you should not use it as a general purpouse math lib:
 

1. The default alignment and the new operator(new operator is not aware of the alignment)

2. Becase of 1 you have to implement new by yourself for every aligned structure(ak. that has XMVECTOR/MATRIX as a member)

3. Additional structures are introduced for math computation and data storage.

 

you can use glm (just add your own perspective/view matrices) or watever else you can find. Im currently writing a math library but it is not ready yet.

http://msdn.microsoft.com/en-us/library/dn375768.aspx

Edited by imoogiBG
0

Share this post


Link to post
Share on other sites

Thx for your answers,

 

But, it is still not as easy to understand for me. You told me that FXMVECTOR call a __vectorcall function, but FXMVECTOR is a typedef of XMVECTOR wich is a datatype, so when it calls a function ? huh.png . What I really dont understand is that XMVECTOR is a 16 bytes chunk of memory, which represent a mathematical vector, so why should we use FXMVECTOR on an other platform, and WHY (beacause it's heavy) we don't pass it as a reference in many functions such as:

 

XMVECTOR XMVector3Lenght(FMXVECTOR V);

 

I'm lost. And if I'm using DirectXMath is to advance in my book (Real-Time 3D Rendering with directX and HLSL) happy.png

 

Thx

Edited by panicq
0

Share this post


Link to post
Share on other sites

XMVECTOR DOES NOT represent a mathmeatical vector (it is a coincidence that is could be used to represent a 4D vector).
When you call 
XMVector3Lenght for example, the functions will assume that its argument is 3D vector(in terms of mathematics). In terms of program it is a special SIMD type.
 

FXMVECTOR is a typedef of XMVECTOR

On some platform. Open DIrectXMath.h and see for yourself

// Fix-up for (1st-3rd) XMVECTOR parameters that are pass-in-register for x86, ARM, and Xbox 360; by reference otherwise
#if ( defined(_M_IX86) || defined(_M_ARM) || defined(_XM_VMX128_INTRINSICS_) ) && !defined(_XM_NO_INTRINSICS_)
typedef const XMVECTOR FXMVECTOR;
#else
typedef const XMVECTOR& FXMVECTOR;
#endif

On platform A we should use XMVECTOR& because this is how its SIMD workflow works.

On platform B we should use XMVECTOR because this is how its SIMD workflow works(windows for example). There is a specific __vectorcall functions modifyer what will help you in that specific scenario.

Edited by imoogiBG
1

Share this post


Link to post
Share on other sites

Okay so in fact when we pass a XMVECTOR as a parameter to a function it is passed directly through registers (but is it copied or passed as a reference ?) and we should use the corresponding typedef on the platform we use. And __vectorcall or __fastcall are functions that are automatically called when we argument is read, and this functions choose wich way the arguement should be send to register ?

0

Share this post


Link to post
Share on other sites
I believe so.
And honestly I don't believe you should stay away from the library, you can use it relatively easy (as explained above), without pretty much no cons and a big number of useful math functions.
0

Share this post


Link to post
Share on other sites

I also think the library is fine.  Although I did write some functions to do 3d math on the XMFLOAT variations when I just have to do a one line math operation and converting to XMVECTOR would be a little annoying.  The library probably does shine more if you have a lot of CPU calculations to do (collision detection) and software skinning, lots of ray/triangle intersections.

0

Share this post


Link to post
Share on other sites
MVECTOR as a parameter to a function it is passed directly through registers (but is it copied or passed as a reference ?) 

That means that the variables VALUES are already in xmm[N] registers and they are ready to be used.

 

I think you're not comfortable with calling conventions http://msdn.microsoft.com/en-us/library/zxk0tw93.aspx

1)CPU calculations to do (collision detection). 
2) lots of ray/triangle intersections.

Can't agree. There are scalar operations that are not vector friendly. 

 

EDIT:

I've missed a line in your prev comments :
 

 And if I'm using DirectXMath is to advance in my book (Real-Time 3D Rendering with directX and HLSL) 

Well if you're following a book you should use the same tools.
 

Edited by imoogiBG
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0