# 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.

## 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;
}



##### Share on other sites
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 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 on other sites
Quote:
 Original post by devroniousMatrix.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 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.

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 9
• 23
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633383
• Total Posts
3011589
• ### Who's Online (See full list)

There are no registered users currently online

×