diff --git a/Engine/Source/Runtime/Core/public/Math/Detail/Matrix3Decl.inl b/Engine/Source/Runtime/Core/public/Math/Detail/Matrix3Decl.inl index 49a453e..c2ec5c4 100644 --- a/Engine/Source/Runtime/Core/public/Math/Detail/Matrix3Decl.inl +++ b/Engine/Source/Runtime/Core/public/Math/Detail/Matrix3Decl.inl @@ -8,17 +8,46 @@ namespace Phanes::Core::Math::Detail template struct compute_mat3_transpose {}; + template + struct compute_mat3_mul {}; + + + template struct compute_mat3_transpose { static constexpr void map(Phanes::Core::Math::TMatrix3& r, const TMatrix3& m1) { - r = TMatrix4(m1(0, 0), m1(1, 0), m1(2, 0), + r = TMatrix3(m1(0, 0), m1(1, 0), m1(2, 0), m1(0, 1), m1(1, 1), m1(2, 1), m1(0, 2), m1(1, 2), m1(2, 2) ); } + }; + template + struct compute_mat3_mul + { + static constexpr void map(Phanes::Core::Math::TMatrix3& r, const TMatrix3& m1, const TMatrix3& m2) + { + r(0, 0) = m1(0, 0) * m2(0, 0) + m1(0, 1) * m2(1, 0) + m1(0, 2) * m2(2, 0); + r(0, 1) = m1(0, 0) * m2(0, 1) + m1(0, 1) * m2(1, 1) + m1(0, 2) * m2(2, 1); + r(0, 2) = m1(0, 0) * m2(0, 2) + m1(0, 1) * m2(1, 2) + m1(0, 2) * m2(2, 2); + r(1, 0) = m1(1, 0) * m2(0, 0) + m1(1, 1) * m2(1, 0) + m1(1, 2) * m2(2, 0); + r(1, 1) = m1(1, 0) * m2(0, 1) + m1(1, 1) * m2(1, 1) + m1(1, 2) * m2(2, 1); + r(1, 2) = m1(1, 0) * m2(0, 2) + m1(1, 1) * m2(1, 2) + m1(1, 2) * m2(2, 2); + + r(2, 0) = m1(2, 0) * m2(0, 0) + m1(2, 1) * m2(1, 0) + m1(2, 2) * m2(2, 0); + r(2, 1) = m1(2, 0) * m2(0, 1) + m1(2, 1) * m2(1, 1) + m1(2, 2) * m2(2, 1); + r(2, 2) = m1(2, 0) * m2(0, 2) + m1(2, 1) * m2(1, 2) + m1(2, 2) * m2(2, 2); + } + + static constexpr void map(Phanes::Core::Math::TVector3& r, const TMatrix3& m1, const TVector3& v) + { + r.x = m1(0, 0) * v.x + m1(0, 1) * v.y + m1(0, 2) * v.z; + r.y = m1(1, 0) * v.x + m1(1, 1) * v.y + m1(1, 2) * v.z; + r.z = m1(2, 0) * v.x + m1(2, 1) * v.y + m1(2, 2) * v.z; + } }; } \ No newline at end of file diff --git a/Engine/Source/Runtime/Core/public/Math/MathCommon.hpp b/Engine/Source/Runtime/Core/public/Math/MathCommon.hpp index d61a1db..2a1984b 100644 --- a/Engine/Source/Runtime/Core/public/Math/MathCommon.hpp +++ b/Engine/Source/Runtime/Core/public/Math/MathCommon.hpp @@ -134,7 +134,7 @@ namespace Phanes::Core::Math { template<> FORCEINLINE float Abs(float s) { - return fabs(s); + return (float)fabs(s); }; template<> diff --git a/Engine/Source/Runtime/Core/public/Math/MathFwd.h b/Engine/Source/Runtime/Core/public/Math/MathFwd.h index 775b673..912b22c 100644 --- a/Engine/Source/Runtime/Core/public/Math/MathFwd.h +++ b/Engine/Source/Runtime/Core/public/Math/MathFwd.h @@ -88,9 +88,31 @@ namespace Phanes::Core::Math { // Matrix2 - typedef TMatrix2 Matrix2; - typedef TMatrix2 Matrix2f; - typedef TMatrix2 Matrix2d; + typedef TMatrix2 Matrix2; + typedef TMatrix2 Matrix2f; + typedef TMatrix2 Matrix2d; + + // Matrix3 + + typedef TMatrix3 Matrix3; + typedef TMatrix3 Matrix3f; + typedef TMatrix3 Matrix3d; + + typedef TMatrix3::value> Matrix3Reg; + typedef TMatrix3::value> Matrix3Regf; + typedef TMatrix3::value> Matrix3Regd; + typedef TMatrix3::value> Matrix3Regf64; + + // Matrix4 + + typedef TMatrix4 Matrix4; + typedef TMatrix4 Matrix4f; + typedef TMatrix4 Matrix4d; + + typedef TMatrix3::value> Matrix4Reg; + typedef TMatrix3::value> Matrix4Regf; + typedef TMatrix3::value> Matrix4Regd; + typedef TMatrix3::value> Matrix4Regf64; } // Phanes::Core::Math::coretypes diff --git a/Engine/Source/Runtime/Core/public/Math/MathTypeConversion.hpp b/Engine/Source/Runtime/Core/public/Math/MathTypeConversion.hpp index f66aca4..1cd2b80 100644 --- a/Engine/Source/Runtime/Core/public/Math/MathTypeConversion.hpp +++ b/Engine/Source/Runtime/Core/public/Math/MathTypeConversion.hpp @@ -104,6 +104,12 @@ namespace Phanes::Core::Math { return "([" + ToString(m(0, 0)) + ", " + ToString(m(0, 1)) + "], [" + ToString(m(1, 0)) + ", " + ToString(m(1, 1)) + "])"; } + template + std::string ToString(const TMatrix3& m) + { + return "([" + ToString(m(0, 0)) + ", " + ToString(m(0, 1)) + ", " + ToString(m(0, 2)) + "], [" + ToString(m(1, 0)) + ", " + ToString(m(1, 1)) + ", " + ToString(m(1, 2)) + "], [" + ToString(m(2, 0)) + ", " + ToString(m(2, 1)) + ", " + ToString(m(2, 2)) + "])"; + } + //std::string toString(const Matrix3& v); diff --git a/Engine/Source/Runtime/Core/public/Math/Matrix3.hpp b/Engine/Source/Runtime/Core/public/Math/Matrix3.hpp index 47d1aef..a96c707 100644 --- a/Engine/Source/Runtime/Core/public/Math/Matrix3.hpp +++ b/Engine/Source/Runtime/Core/public/Math/Matrix3.hpp @@ -15,6 +15,7 @@ namespace Phanes::Core::Math { // 3x3 Matrix defined in column-major order. // Accessed by M[Row][Col]. + template struct TMatrix3 { @@ -39,9 +40,10 @@ namespace Phanes::Core::Math { /// TVector3 c2; }; + + T data[3][4]; }; - T data[3][3]; public: @@ -76,9 +78,9 @@ namespace Phanes::Core::Math { * Construct Matrix from parameters. * * @param(n00) M[0][0] - * @param(n10) M[1][0] - * @param(n01) M[0][1] - * @param(n11) M[1][1] + * @param(n10) M[0][1] + * @param(n20) M[0][2] + * @param(n01) M[1][0] * ... * * @note nXY = n[Row][Col] @@ -109,6 +111,11 @@ namespace Phanes::Core::Math { public: + FORCEINLINE T operator() (int n, int m) const + { + return this->data[m][n]; + } + FORCEINLINE T& operator() (int n, int m) { return this->data[m][n]; @@ -119,12 +126,7 @@ namespace Phanes::Core::Math { return (*reinterpret_cast*>(this->m[m])); } - FORCEINLINE const T& operator() (int n, int m) const - { - return this->data[m][n]; - } - - FORCEINLINE const TVector3& operator[] (int m) const + FORCEINLINE const TVector3 operator[] (int m) const { return (*reinterpret_cast*>(this->m[m])); } @@ -229,14 +231,7 @@ namespace Phanes::Core::Math { */ template - TMatrix3 operator*= (TMatrix3& m1, const TMatrix3& m2) - { - m1.c0 *= m2.c0; - m1.c1 *= m2.c1; - m1.c2 *= m2.c2; - - return m1; - } + TMatrix3 operator*= (TMatrix3& m1, const TMatrix3& m2); /** * Multiply matrix with scalar @@ -298,9 +293,9 @@ namespace Phanes::Core::Math { template TMatrix3 operator+ (const TMatrix3& m1, const TMatrix3& m2) { - return TMatrix2(m1.c0 + m2.c0, - m1.c1 + m2.c1, - m1.c2 + m2.c2); + return TMatrix3(m1.c0 + m2.c0, + m1.c1 + m2.c1, + m1.c2 + m2.c2); } /** @@ -348,21 +343,6 @@ namespace Phanes::Core::Math { m.c2 * s); } - /** - * Multiplay matrix by matrix (componentwise) - * - * @param(m1) Matrix - * @param(m2) Matrix - */ - - template - TMatrix3 operator/ (const TMatrix3& m1, const TMatrix3& m2) - { - return TMatrix3(m1.c0 / m2.c0, - m1.c1 / m2.c1, - m1.c2 / m2.c2); - } - /** * Multiply scalar with matrix * @@ -387,12 +367,10 @@ namespace Phanes::Core::Math { */ template - TMatrix3 operator* (const TMatrix3& m1, const TMatrix3& m2) - { - return TMatrix3(m1.c0 * m2.c0, - m1.c1 * m2.c1, - m1.c2 * m2.c2); - } + TMatrix3 operator* (const TMatrix3& m1, const TMatrix3& m2); + + template + TVector3 operator* (const TMatrix3& m1, const TVector3& v); /** * Compare matrix with other matrix. @@ -495,7 +473,7 @@ namespace Phanes::Core::Math { */ template - bool Inverse(TMatrix3& r, const TMatrix3& m1) + bool Inverse(const TMatrix3& m1, Ref> r) { TVector3 r0 = CrossP(m1.c1, m1.c2); TVector3 r1 = CrossP(m1.c2, m1.c0); diff --git a/Engine/Source/Runtime/Core/public/Math/Matrix3.inl b/Engine/Source/Runtime/Core/public/Math/Matrix3.inl index 3383d12..bd6b239 100644 --- a/Engine/Source/Runtime/Core/public/Math/Matrix3.inl +++ b/Engine/Source/Runtime/Core/public/Math/Matrix3.inl @@ -24,4 +24,28 @@ namespace Phanes::Core::Math return r; } + + template + TMatrix3 operator*= (TMatrix3& m1, const TMatrix3& m2) + { + TMatrix3 r; + Detail::compute_mat3_mul::map(r, m1, m2); + return (m1 = r); + } + + template + TMatrix3 operator* (const TMatrix3& m1, const TMatrix3& m2) + { + TMatrix3 r; + Detail::compute_mat3_mul::map(r, m1, m2); + return r; + } + + template + TVector3 operator* (const TMatrix3& m1, const TVector3& v) + { + TVector3 r; + Detail::compute_mat3_mul::map(r, m1, v); + return r; + } } \ No newline at end of file diff --git a/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesSIMDTypes.h b/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesSIMDTypes.h index d0ae2aa..5347b9e 100644 --- a/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesSIMDTypes.h +++ b/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesSIMDTypes.h @@ -26,7 +26,6 @@ namespace Phanes::Core::Math::SIMD /// /// Type of vector /// Length of vector - /// Whether SIMD intrinsics exist, that support the vector type and length. /// Whether the vector is aligned for simd usage. template struct use_simd diff --git a/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathSSE.hpp b/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathSSE.hpp index 3e554c4..747be76 100644 --- a/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathSSE.hpp +++ b/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathSSE.hpp @@ -770,6 +770,45 @@ namespace Phanes::Core::Math::Detail } }; + template<> + struct compute_mat3_mul + { + static FORCEINLINE void map(Phanes::Core::Math::TMatrix3& r, const TMatrix3& m1, const TMatrix3& m2) + { + + // First column + __m128 tmp0 = _mm_mul_ps(m1.c0.data, m2.c0.data); + __m128 tmp1 = _mm_mul_ps(m1.c1.data, m2.c0.data); + __m128 tmp2 = _mm_mul_ps(m1.c2.data, m2.c0.data); + + r.c0.data = _mm_add_ps(_mm_add_ps(tmp0, tmp1), tmp2); + + + // Second column + __m128 tmp0 = _mm_mul_ps(m1.c0.data, m2.c1.data); + __m128 tmp1 = _mm_mul_ps(m1.c1.data, m2.c1.data); + __m128 tmp2 = _mm_mul_ps(m1.c2.data, m2.c1.data); + + r.c1.data = _mm_add_ps(_mm_add_ps(tmp0, tmp1), tmp2); + + // Third column + __m128 tmp0 = _mm_mul_ps(m1.c0.data, m2.c2.data); + __m128 tmp1 = _mm_mul_ps(m1.c1.data, m2.c2.data); + __m128 tmp2 = _mm_mul_ps(m1.c2.data, m2.c2.data); + + r.c2.data = _mm_add_ps(_mm_add_ps(tmp0, tmp1), tmp2); + } + + static FORCEINLINE void map(Phanes::Core::Math::TVector3& r, const TMatrix3& m1, const TVector3& v) + { + __m128 tmp0 = _mm_mul_ps(m1.c0.data, v.data); + __m128 tmp1 = _mm_mul_ps(m1.c1.data, v.data); + __m128 tmp2 = _mm_mul_ps(m1.c2.data, v.data); + + r.data = _mm_add_ps(_mm_add_ps(tmp0, tmp1), tmp2); + } + }; + // =========== // // Matrix4 // // =========== // diff --git a/Engine/Source/Runtime/Core/public/Math/SIMD/Storage.h b/Engine/Source/Runtime/Core/public/Math/SIMD/Storage.h index 2ea8f17..f7116cd 100644 --- a/Engine/Source/Runtime/Core/public/Math/SIMD/Storage.h +++ b/Engine/Source/Runtime/Core/public/Math/SIMD/Storage.h @@ -14,17 +14,13 @@ namespace Phanes::Core::Math::SIMD template struct Storage { - typedef struct type { - T data[L]; - } type; + typedef T type[L]; }; template struct Storage<3, T, false> { - typedef struct type { - T data[4]; - } type; + typedef T type[4]; };