diff --git a/Common/interface/BasicMath.hpp b/Common/interface/BasicMath.hpp index 2e37e637d..955bc65e4 100644 --- a/Common/interface/BasicMath.hpp +++ b/Common/interface/BasicMath.hpp @@ -823,6 +823,24 @@ template struct Matrix2x2 return *this; } + Matrix2x2& operator+=(const Matrix2x2& right) + { + for (int i = 0; i < 4; ++i) + Data()[i] += right.Data()[i]; + return *this; + } + + Matrix2x2 operator+(const Matrix2x2& right) const + { + // clang-format off + return Matrix2x2 + { + _11 + right._11, _12 + right._12, + _21 + right._21, _22 + right._22 + }; + // clang-format on + } + constexpr Matrix2x2 Transpose() const { return Matrix2x2{ @@ -1020,6 +1038,25 @@ template struct Matrix3x3 return *this; } + Matrix3x3& operator+=(const Matrix3x3& right) + { + for (int i = 0; i < 9; ++i) + Data()[i] += right.Data()[i]; + return *this; + } + + Matrix3x3 operator+(const Matrix3x3& right) const + { + // clang-format off + return Matrix3x3 + { + _11 + right._11, _12 + right._12, _13 + right._13, + _21 + right._21, _22 + right._22, _23 + right._23, + _31 + right._31, _32 + right._32, _33 + right._33 + }; + // clang-format on + } + Matrix3x3& operator*=(const Matrix3x3& right) { *this = Mul(*this, right); @@ -1347,6 +1384,26 @@ template struct Matrix4x4 return *this; } + Matrix4x4& operator+=(const Matrix4x4& right) + { + for (int i = 0; i < 16; ++i) + Data()[i] += right.Data()[i]; + return *this; + } + + Matrix4x4 operator+(const Matrix4x4& right) const + { + // clang-format off + return Matrix4x4 + { + _11 + right._11, _12 + right._12, _13 + right._13, _14 + right._14, + _21 + right._21, _22 + right._22, _23 + right._23, _24 + right._24, + _31 + right._31, _32 + right._32, _33 + right._33, _34 + right._34, + _41 + right._41, _42 + right._42, _43 + right._43, _44 + right._44 + }; + // clang-format on + } + constexpr Matrix4x4 Transpose() const { return Matrix4x4 // diff --git a/Tests/DiligentCoreTest/src/Common/MathLibTest.cpp b/Tests/DiligentCoreTest/src/Common/MathLibTest.cpp index 508b24bdd..5f7a3b9e3 100644 --- a/Tests/DiligentCoreTest/src/Common/MathLibTest.cpp +++ b/Tests/DiligentCoreTest/src/Common/MathLibTest.cpp @@ -1373,6 +1373,87 @@ TEST(Common_BasicMath, ScalarMatrixMultiply) // clang-format on } +TEST(Common_BasicMath, ScalarMatrixOpeartorPlus) +{ + // clang-format off + EXPECT_EQ(float2x2(1, 2, + 3, 4) + + float2x2(5, 6, + 7, 8), + float2x2( 6, 8, + 10, 12) + ); + EXPECT_EQ(float3x3(1, 2, 3, + 4, 5, 6, + 7, 8, 9) + + float3x3(10, 11, 12, + 13, 14, 15, + 16, 17, 18), + float3x3(11, 13, 15, + 17, 19, 21, + 23, 25, 27) + ); + EXPECT_EQ(float4x4( 1, 2, 3, 4, + 5, 6, 7, 8, + 9, 10 ,11, 12, + 13, 14, 15, 16) + + float4x4(17, 18, 19, 20, + 21, 22, 23, 24, + 25, 26, 27, 28, + 29, 30, 31, 32), + float4x4(18, 20, 22, 24, + 26, 28, 30, 32, + 34, 36, 38, 40, + 42, 44, 46, 48) + ); + // clang-format on +} + +TEST(Common_BasicMath, ScalarMatrixOpeartorPlusEqual) +{ + // clang-format off + { + auto Mat = float2x2{1, 2, + 3, 4}; + Mat += float2x2{5, 6, + 7, 8}; + EXPECT_EQ(Mat, + float2x2( 6, 8, + 10, 12) + ); + } + { + auto Mat = float3x3{1, 2, 3, + 4, 5, 6, + 7, 8, 9}; + Mat += float3x3{10, 11, 12, + 13, 14, 15, + 16, 17, 18}; + EXPECT_EQ(Mat, + float3x3(11, 13, 15, + 17, 19, 21, + 23, 25, 27) + ); + } + { + auto Mat = float4x4{ 1, 2, 3, 4, + 5, 6, 7, 8, + 9, 10 ,11, 12, + 13, 14, 15, 16}; + Mat += float4x4{17, 18, 19, 20, + 21, 22, 23, 24, + 25, 26, 27, 28, + 29, 30, 31, 32}; + EXPECT_EQ(Mat, + float4x4(18, 20, 22, 24, + 26, 28, 30, 32, + 34, 36, 38, 40, + 42, 44, 46, 48) + ); + } + // clang-format on +} + TEST(Common_AdvancedMath, Planes) { Plane3D plane = {};