Jump to content
  • Advertisement
Sign in to follow this  
devronious

Matrix multiplication in directx

This topic is 4678 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to multiply several matrices together in one shot. But apparently I can't index from a directx Matrix. is there a solution to this? Here's my code so far...
[source lang=c#]
        public static Matrix MultipleMatrixMultiply(Matrix[] mats)
        {
            Matrix b;
            Matrix o;
            foreach (Matrix m in mats)
            {
                if (b == null)
                    b = m;
                else
                {
                    for (int i = 0; i < 4; i++)
                        for (int j = 0; j < 4; j++)
                        {
                            o[j] = 0;
                            for (int k = 0; k < 4; k++)
                                o[j] += b[k] * m[k][j];
                        }
                    b = o;
                }
            }
            return b;
        }

Thanks in advance, Devin

Share this post


Link to post
Share on other sites
Advertisement
If you are willing to use unsafe code indexing the elements of a matrix is not that hard. If you want an example I can give you one. The big question is why? It is unlikely that you will get any preformance increase from doing what you are doing (managed or unmanaged). The built-in DirectX matrix multiplication is probably as fast as you are going to get -- with Matrix.Multiply performing sligltly better than matrix1 * matrix2.

Also, I am not sure you are doing it right (though you may be). Look here and here for more details.

Share this post


Link to post
Share on other sites
Matrix.Multiply() only accepts two matrices. I don't want to call the method 8 times to multiply 9 matrices if I have to. Is there a way around that?

-Devin

Share this post


Link to post
Share on other sites
Quote:
Original post by devronious
Matrix.Multiply() only accepts two matrices. I don't want to call the method 8 times to multiply 9 matrices if I have to. Is there a way around that?


Just do what you are already doing, but use Matrix.Multiply instead of your loop through the elements.

Matrix res = Matrix.Identity;
foreach(Matrix m in mats){
res = Matrix.Multiply(res,m);
}


The same number of float by float muls will happen either way. You can either let directx do it for you fast and easy or do it yourself hard and slow. This is really an area where you will have a hard time beating the performance of directx esp. If you are only worried about 9 mats.

If you are really set on indexing the data inside your mats you can pin your array with the fixed statement and cast the Matrix* to a float* and walk through the pointer managing your offsets and counts and bounds. I speculate that even going to the trouble of doing it this way will preform worse than the above code.

Share this post


Link to post
Share on other sites
Turnpast gave you an awesome answer and I second that. You might also want to look at MatrixStack. Or another way of doing what turnpast has suggested is using the "*" operator which has been overloaded to your advantage.

public Matrix MultiplyMatrices(Matrix[] matrices)
{
Matrix matResult = new Matrix();
foreach (Matrix matrix in matrices)
{
matResult *= matrix;
}
return matResult;
}

I hope this helps.
Take care.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!