Sign in to follow this  
Xargo

The common practices of a DirectX Engine

Recommended Posts

Xargo    122
Hi ho I'm currently writing a DirectX engine for educational purposes. Sometimes I have to pass a certain value to the application using my dll. But when that value is for example a Vector3 I start wondering. Should I pass the Vector3 or should I pass three ints with the x, y and z? This is quite important, because if I pass a Vector3 to the application, he will be obliged to set a reference to DirectX, and then add a using command. Now I've got three questions about this: 1. First of all, does it matter anyway when the application itself doesn't have a reference to DirectX but the dll it uses has? (Can this be compared to the application itself having a reference to DirectX?) 2. Does this matter at all? Are references performance expensive? 3. Is it common practice with DirectX Engines, eg. XNA, to oblige the application using XNA to reference to DirectX as well? Do they make their own structs? Thanks Xargo

Share this post


Link to post
Share on other sites
mattnewport    1038
Most games / engines written in C++ will use their own math libraries for things like vectors and matrices and most code will use those rather than having a dependency on DirectX. Only the parts of the code that use DirectX directly (usually the platform specific parts of the rendering engine) will use DirectX types and will convert from the math library types to the equivalent DirectX types only when necessary.

Share this post


Link to post
Share on other sites
TheAdmiral    1122
Performance is only a small consideration, here. And even if it were, far more important is code interface correctness - don't shoot yourself in the foot for the sake of some memory that doesn't even matter. Here are the facts:

-= The declaration for a D3DVECTOR/D3DXVECTOR3 resides in a header file - #includeing this will add nothing to the compiled code.
-= Forcing client code to link against a DLL will introduce some overhead, but this is negated if any other module in the process links to it. That is to say; if your DLL requires a reference to Direct3D 9 then it makes no difference if it forces the project to link to it, since the DLL and the executable will be loaded into the same address-space, which will contain d3d9.dll anyway.
-= Passing a struct (with an appropriate declaration in place) is tantamount to passing the components - the resulting code is exactly the same. The concept of structs only concerns C++. Everything reduces to a soup of bytes, words and dwords at the assembler's level.
-= Passing a reference to a struct is optimal. This equates to one dword on the stack, as opposed to sizeof(<the structure>) when passing by value. The difference is negligible in most cases for structures as small as a 3-vector, but it's a good idea to get into the habit of passing by const reference.

So, to answer your questions.

1. This doesn't matter if the application uses the header files (i.e. compiler-level), but it does matter if any library code is referenced (i.e. linker-level). If the application is linking to DirectX anyway, there's nothing to worry about either way.

2. This does matter if the target application needs a small memory footprint. On current PCs, it's not a huge sin to link to a DLL that you don't use, but there's no need to waste resources if it isn't necessary. This one's a judgement call. And references are cheap - as cheap as function arguments come.

3. You can find examples for both sides. A close-to-the-metal helper library will work alongside the API and will probably require you to link to it, whereas API-independent middleware will define its own interfaces.

Admiral

Share this post


Link to post
Share on other sites
Kimani    104
Quote:
Original post by TheAdmiral-= Passing a reference to a struct is optimal. This equates to one dword on the stack, as opposed to sizeof(<the structure>) when passing by value. The difference is negligible in most cases for structures as small as a 3-vector, but it's a good idea to get into the habit of passing by const reference.

Is it? You're passing in what is basically a dereferenced pointer to the function. That memory may be on the stack, it may be on the heap, wherever. So it still needs the overhead of pointer traversal, as opposed to simple stack arithmetic for accessing things on the stack. I imagine that if you're using the reference object often enough, and it's something relatively small like a vector, it would be better to just copy it onto the stack rather than suffer all that pointer traversal.

I wonder what the cutoff point where one is better than the other for something like a matrix.

Share this post


Link to post
Share on other sites
Rompa    307
Yep, passing by const reference also reduces potential variable/memory aliasing issues.
Oh and #include-ing will add something to your code - delays and dependencies! We avoid #includes where we can and forward declare where possible. Our compile times are excellent and stay that way with a bit of diligence.

Share this post


Link to post
Share on other sites
Xargo    122
Actually I'm using C#, I forgot to mention that, I'm sorry. :p

Anyway, I've got enough information from your replies already.

Thank you very much for answering. :D

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