# Matrix Multiplication

I was wondering if it would be worthwhile adding an optimized matrix multiplication for cases when you’re not doing perspective projections. i.e. 99.9% of the time
If you aren’t doing a perspective project, then you’re pretty much guaranteed that:
M14, M24, M34, M44 = (0, 0, 0, 1)

Maybe something along the lines of:
public static Matrix MultiplyNoPerspective(Matrix matrix1, Matrix matrix2)
{
Matrix ret;
MultiplyNoPerspective(ref matrix1, ref matrix2, out ret);
return ret;
}

``````    public static void MultiplyNoPerspective(ref Matrix matrix1, ref Matrix matrix2, out Matrix result)
{
result.M11 = matrix1.M11 * matrix2.M11 + matrix1.M12 * matrix2.M21 + matrix1.M13 * matrix2.M31; // + matrix1.M14 * matrix2.M41;
result.M12 = matrix1.M11 * matrix2.M12 + matrix1.M12 * matrix2.M22 + matrix1.M13 * matrix2.M32; // + matrix1.M14 * matrix2.M42;
result.M13 = matrix1.M11 * matrix2.M13 + matrix1.M12 * matrix2.M23 + matrix1.M13 * matrix2.M33; // + matrix1.M14 * matrix2.M43;
result.M14 = 0f; //matrix1.M11 * matrix2.M14 + matrix1.M12 * matrix2.M24 + matrix1.M13 * matrix2.M34 + matrix1.M14 * matrix2.M44;

result.M21 = matrix1.M21 * matrix2.M11 + matrix1.M22 * matrix2.M21 + matrix1.M23 * matrix2.M31; // + matrix1.M24 * matrix2.M41;
result.M22 = matrix1.M21 * matrix2.M12 + matrix1.M22 * matrix2.M22 + matrix1.M23 * matrix2.M32; // + matrix1.M24 * matrix2.M42;
result.M23 = matrix1.M21 * matrix2.M13 + matrix1.M22 * matrix2.M23 + matrix1.M23 * matrix2.M33; // + matrix1.M24 * matrix2.M43;
result.M24 = 0f; //matrix1.M21 * matrix2.M14 + matrix1.M22 * matrix2.M24 + matrix1.M23 * matrix2.M34 + matrix1.M24 * matrix2.M44;

result.M31 = matrix1.M31 * matrix2.M11 + matrix1.M32 * matrix2.M21 + matrix1.M33 * matrix2.M31; // + matrix1.M34 * matrix2.M41;
result.M32 = matrix1.M31 * matrix2.M12 + matrix1.M32 * matrix2.M22 + matrix1.M33 * matrix2.M32; // + matrix1.M34 * matrix2.M42;
result.M33 = matrix1.M31 * matrix2.M13 + matrix1.M32 * matrix2.M23 + matrix1.M33 * matrix2.M33; // + matrix1.M34 * matrix2.M43;
result.M34 = 0f; //matrix1.M31 * matrix2.M14 + matrix1.M32 * matrix2.M24 + matrix1.M33 * matrix2.M34 + matrix1.M34 * matrix2.M44;

result.M41 = matrix1.M41 * matrix2.M11 + matrix1.M42 * matrix2.M21 + matrix1.M43 * matrix2.M31 + matrix2.M41; //matrix1.M44 * matrix2.M41;
result.M42 = matrix1.M41 * matrix2.M12 + matrix1.M42 * matrix2.M22 + matrix1.M43 * matrix2.M32 + matrix2.M42; //matrix1.M44 * matrix2.M42;
result.M43 = matrix1.M41 * matrix2.M13 + matrix1.M42 * matrix2.M23 + matrix1.M43 * matrix2.M33 + matrix2.M43; //matrix1.M44 * matrix2.M43;
result.M44 = 1f; //matrix1.M41 * matrix2.M14 + matrix1.M42 * matrix2.M24 + matrix1.M43 * matrix2.M34 + matrix1.M44 * matrix2.M44;
}``````

If you mean for internal calculations were you transform a world matrix by say a rotation matrix… maybe.

You can write your own extension methods and shaders that use them.
I suppose you could squeeze out a few less calculations that way.

However, the m14 m24 m34 elements are skewing elements, they could be thought of as a skewing vector for the x y z axis in screen space.
So that wouldn’t be a proper name for your multiply as its not really a matrix multiply at all now, It might cause you confusion later.

Thanks.

I’ve also done some performance testing and the small speed improvement of the method itself becomes negligible across an entire frame, with everything else that’s happening.

A more interesting improvement would be to take advantage of SIMD.