• 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
Gondolin

D3DXMatrixMultiply implimentation?

6 posts in this topic

Does anyone know how D3DXMatrixMultiply is implimented? I can't find any information on what alrogrithm it uses, but it is definitely faster than just a naive matrix multiplication in C++. Could it even be implimented in assembly?
0

Share this post


Link to post
Share on other sites
I'm fairly certain that the matrix multiplication, along with a number of other functions, is written to use SSE when available. You're not likely to beat it any time soon, as it's tuned by guys who know processors like whoa.
0

Share this post


Link to post
Share on other sites
Wow, you were right, that took a long time, but I did some heavy research on the SSE and 3DNow! SIMD instruction sets and I've finally got an implimentation that equals D3DMatrixMultiply's performance. I'm developing on an AMD processor so this is in 3DNow! MMX assembly. I don't know if it would work on an Intel or not. DX looks up your processor internally and probably chooses an SSE or 3DNow! implimentation depending on what your processor supports. I will probably do something similar if I can figure out how to test it on an Intel (though supposedly AMD processors now support SSE instructions, but I'm not sure if there is a large performance difference compared to 3DNow! on these machines).

Anyway, for anyone who wishes to use this in a math library, feel free.



enum MPARTS { MP_11=0, MP_12=4, MP_13=8, MP_14=12,
MP_21=16, MP_22=20, MP_23=24, MP_24=28,
MP_31=32, MP_32=36, MP_33=40, MP_34=44,
MP_41=48, MP_42=52, MP_43=56, MP_44=60 };

enum MROWS { MR_1=0, MR_2=16, MR_3=32, MR_4=48 };


inline Matrix& Matrix::operator *=(const Matrix& rhs)
{
__asm
{
FEMMS

mov edi, this
mov esi, rhs
MOVQ MM1, [esi+MR_1]
MOVQ MM2, [esi+MR_1+8]
MOVQ MM3, [esi+MR_2]


PSHUFW MM6, [edi+MP_11], 0x44
MOVQ MM7, MM6
PFMUL MM6, MM1
PFMUL MM7, MM2

PSHUFW MM4, [edi+MP_12], 0x44
MOVQ MM5, MM4
PFMUL MM4, MM3
MOVQ MM0, [esi+MR_2+8]
PFMUL MM5, MM0
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_13], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_3]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_3+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_14], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_4]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_4+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

MOVQ [edi+MR_1], MM6
MOVQ [edi+MR_1+8], MM7


PSHUFW MM6, [edi+MP_21], 0x44
MOVQ MM7, MM6
PFMUL MM6, MM1
PFMUL MM7, MM2

PSHUFW MM4, [edi+MP_22], 0x44
MOVQ MM5, MM4
PFMUL MM4, MM3
MOVQ MM0, [esi+MR_2+8]
PFMUL MM5, MM0
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_23], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_3]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_3+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_24], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_4]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_4+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

MOVQ [edi+MR_2], MM6
MOVQ [edi+MR_2+8], MM7


PSHUFW MM6, [edi+MP_31], 0x44
MOVQ MM7, MM6
PFMUL MM6, MM1
PFMUL MM7, MM2

PSHUFW MM4, [edi+MP_32], 0x44
MOVQ MM5, MM4
PFMUL MM4, MM3
MOVQ MM0, [esi+MR_2+8]
PFMUL MM5, MM0
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_33], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_3]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_3+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_34], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_4]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_4+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

MOVQ [edi+MR_3], MM6
MOVQ [edi+MR_3+8], MM7


PSHUFW MM6, [edi+MP_41], 0x44
MOVQ MM7, MM6
PFMUL MM6, MM1
PFMUL MM7, MM2

PSHUFW MM4, [edi+MP_42], 0x44
MOVQ MM5, MM4
PFMUL MM4, MM3
MOVQ MM0, [esi+MR_2+8]
PFMUL MM5, MM0
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_43], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_3]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_3+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

PSHUFW MM4, [edi+MP_44], 0x44
MOVQ MM5, MM4
MOVQ MM0, [esi+MR_4]
PFMUL MM4, MM0
MOVQ MM0, [esi+MR_4+8]
PFMUL MM5, MM2
PFADD MM6, MM4
PFADD MM7, MM5

MOVQ [edi+MR_4], MM6
MOVQ [edi+MR_4+8], MM7

FEMMS
}
return *this;
}



Matrix's data here is just an array of 16 floats. This is the *= version, a generic multimatrix(m1, m2, out) would be simply to make from this.

Please post any improvements and optimizations, I'm only a beginnner in assembly. If anyone wants me to post the complete matrix class to test the code let me know. I tested this against D3DXMatrixMultiply and even doing 10000 multiplications per frame they performed extremely close (sometimes over, sometimes under, averaging nearly identical).


1

Share this post


Link to post
Share on other sites
Only thing is, this won't compile on a x64 platform. Microsoft pulled asm support for x64 platforms. This has forced me to not use asm, if I want to build a x64 application.

Edit: Not to sound like a real downer[smile].

[Edited by - exorcist_bob on January 3, 2007 8:19:44 PM]
0

Share this post


Link to post
Share on other sites
1. Unless you have good reason to (e.g. for self-education on how to implement such a thing, or for use on Linux PCs), for single operations like matrix multiplication and inversion I'd still advise using the D3DX maths functions: they're well tested, extremely optimal for a general purpose library, somebody else fixes the bugs, less memory is used for code if any other running application has the same D3DX DLL loaded.


2. To use Gondolin's code on x64, you'll either have to use a standalone assembler and put a function call to the ASM function inside the inline C++ or use intrinsics.

Incidentally, when you want to mix ASM and C/C++ in the same function, intrinsics can actually provide better performance because you leave the choice of register assignment up to the compiler, so it can do a better job of assigning C/C++ variables to registers that your low-level code uses.


3. writing, and hand-tuning ASM can be a fun activity, and it's often useful to understand ASM for debugging purposes, but do remember that the CPU manufacturers are more 'l33t' at ASM than you are, so it's always worth checking whether they've already done the hard work for you, e.g.:
http://www.intel.com/design/PentiumIII/sml/

http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/22007.pdf (See chapter 10, the principles behind the Matrix-Vector multiply can be expanded to a Matrix-Matrix multiply. Also check out AMD's ACML library).
0

Share this post


Link to post
Share on other sites
Quote:
Unless you have good reason to (e.g. for self-education on how to implement such a thing


Yep, this is more of a self-education implimentation. You're right, its a lot of fun tuning in ASM and when you're done you have a much better understanding of the general algorithm along with how to optimize it.

My original idea was that a game server would not have to load any DirectX DLLs...as from a design standpoint every component of DirectX is aimed for client presentation, not CPU calculations such as physics and scene management that the server does. Unfortunately including the D3DX math header seems to load the whole D3D dll which seems a waste. There are lots of math libraries out there but I was attempting to write my own that essentially impliments DX math. Even though its a lot of work and probably pointless, I'm finding that there are a few things that DX doesn't fully optimize, so its interesting to see what works better and what doesn't, even if I don't end up using this.
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