From ed44c3695c76c2471b6a1f3b1d4c4bb9de7ac64b Mon Sep 17 00:00:00 2001 From: scorpioblood <77296181+scorpioblood@users.noreply.github.com> Date: Wed, 12 Jun 2024 23:26:39 +0200 Subject: [PATCH] Update Matrix3. --- .../Core/public/Math/Detail/Vector2Decl.inl | 78 +++-- .../Core/public/Math/Detail/Vector3Decl.inl | 14 + .../Source/Runtime/Core/public/Math/MathFwd.h | 2 +- .../Runtime/Core/public/Math/Matrix2.hpp | 32 +- .../Runtime/Core/public/Math/Matrix3.hpp | 279 +++++++++--------- .../public/Math/SIMD/PhanesVectorMathFPU.hpp | 66 +++++ .../public/Math/SIMD/PhanesVectorMathSSE.hpp | 22 ++ .../Runtime/Core/public/Math/Vector3.hpp | 20 +- .../Runtime/Core/public/Math/Vector3.inl | 17 ++ 9 files changed, 306 insertions(+), 224 deletions(-) diff --git a/Engine/Source/Runtime/Core/public/Math/Detail/Vector2Decl.inl b/Engine/Source/Runtime/Core/public/Math/Detail/Vector2Decl.inl index e530c9e..c19a769 100644 --- a/Engine/Source/Runtime/Core/public/Math/Detail/Vector2Decl.inl +++ b/Engine/Source/Runtime/Core/public/Math/Detail/Vector2Decl.inl @@ -21,12 +21,6 @@ namespace Phanes::Core::Math::Detail template struct compute_vec2_div {}; - template - struct compute_vec2_eq {}; - - template - struct compute_vec2_ieq {}; - template struct compute_vec2_inc {}; @@ -38,29 +32,29 @@ namespace Phanes::Core::Math::Detail template struct construct_vec2 { - template - static constexpr void map(Phanes::Core::Math::TVector2& v1, const TVector2& v2) + + static constexpr void map(Phanes::Core::Math::TVector2& v1, const TVector2& v2) { v1.x = v2.x; v1.y = v2.y; } - template - static constexpr void map(Phanes::Core::Math::TVector2& v1, T s) + + static constexpr void map(Phanes::Core::Math::TVector2& v1, T s) { v1.x = s; v1.y = s; } - template - static constexpr void map(Phanes::Core::Math::TVector2& v1, T x, T y) + + static constexpr void map(Phanes::Core::Math::TVector2& v1, T x, T y) { v1.x = x; v1.y = y; } - template - static constexpr void map(Phanes::Core::Math::TVector2& v1, const T* comp) + + static constexpr void map(Phanes::Core::Math::TVector2& v1, const T* comp) { v1.x = comp[0]; v1.y = comp[1]; @@ -71,15 +65,15 @@ namespace Phanes::Core::Math::Detail template struct compute_vec2_add { - template - static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, const Phanes::Core::Math::TVector2& v2) + + static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, const Phanes::Core::Math::TVector2& v2) { r.x = v1.x + v2.x; r.y = v1.y + v2.y; } - template - static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, T s) + + static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, T s) { r.x = v1.x + s; r.y = v1.y + s; @@ -90,15 +84,15 @@ namespace Phanes::Core::Math::Detail template struct compute_vec2_sub { - template - static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, const Phanes::Core::Math::TVector2& v2) + + static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, const Phanes::Core::Math::TVector2& v2) { r.x = v1.x - v2.x; r.y = v1.y - v2.y; } - template - static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, T s) + + static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, T s) { r.x = v1.x - s; r.y = v1.y - s; @@ -109,15 +103,15 @@ namespace Phanes::Core::Math::Detail template struct compute_vec2_mul { - template - static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, const Phanes::Core::Math::TVector2& v2) + + static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, const Phanes::Core::Math::TVector2& v2) { r.x = v1.x * v2.x; r.y = v1.y * v2.y; } - template - static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, T s) + + static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, T s) { r.x = v1.x * s; r.y = v1.y * s; @@ -128,15 +122,15 @@ namespace Phanes::Core::Math::Detail template struct compute_vec2_div { - template - static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, const Phanes::Core::Math::TVector2& v2) + + static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, const Phanes::Core::Math::TVector2& v2) { r.x = v1.x / v2.x; r.y = v1.y / v2.y; } - template - static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, T s) + + static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1, T s) { s = (T)1.0 / s; @@ -145,22 +139,22 @@ namespace Phanes::Core::Math::Detail } }; - template - struct compute_vec2_eq + template + struct compute_vec2_eq { - template - static constexpr bool map(const Phanes::Core::Math::TVector2& v1, const Phanes::Core::Math::TVector2& v2) + + static constexpr bool map(const Phanes::Core::Math::TVector2& v1, const Phanes::Core::Math::TVector2& v2) { return (Phanes::Core::Math::Abs(v1.x - v2.x) < P_FLT_INAC && Phanes::Core::Math::Abs(v1.y - v2.y) < P_FLT_INAC); } }; - template - struct compute_vec2_ieq + template + struct compute_vec2_ieq { - template - static constexpr bool map(const Phanes::Core::Math::TVector2& v1, const Phanes::Core::Math::TVector2& v2) + + static constexpr bool map(const Phanes::Core::Math::TVector2& v1, const Phanes::Core::Math::TVector2& v2) { return (Phanes::Core::Math::Abs(v1.x - v2.x) > P_FLT_INAC || Phanes::Core::Math::Abs(v1.y - v2.y) > P_FLT_INAC); @@ -170,8 +164,8 @@ namespace Phanes::Core::Math::Detail template struct compute_vec2_inc { - template - static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1) + + static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1) { r.x = v1.x + 1; r.y = v1.y + 1; @@ -181,8 +175,8 @@ namespace Phanes::Core::Math::Detail template struct compute_vec2_dec { - template - static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1) + + static constexpr void map(Phanes::Core::Math::TVector2& r, const Phanes::Core::Math::TVector2& v1) { r.x = v1.x - 1; r.y = v1.y - 1; diff --git a/Engine/Source/Runtime/Core/public/Math/Detail/Vector3Decl.inl b/Engine/Source/Runtime/Core/public/Math/Detail/Vector3Decl.inl index 6151cb6..5612c25 100644 --- a/Engine/Source/Runtime/Core/public/Math/Detail/Vector3Decl.inl +++ b/Engine/Source/Runtime/Core/public/Math/Detail/Vector3Decl.inl @@ -31,6 +31,9 @@ namespace Phanes::Core::Math::Detail template struct compute_vec3_dec {}; + template + struct compute_vec3_cross_p {}; + template @@ -199,5 +202,16 @@ namespace Phanes::Core::Math::Detail r.z = v1.z - 1; } }; + + template + struct compute_vec3_cross_p + { + static constexpr void map(Phanes::Core::Math::TVector3& r, const Phanes::Core::Math::TVector3& v1, const Phanes::Core::Math::TVector3& v2) + { + r.x = (v1.y * v2.z) - (v1.z * v2.y); + r.y = (v1.z * v2.x) - (v1.x * v2.z); + r.z = (v1.x * v2.y) - (v1.y * v2.x); + } + }; } diff --git a/Engine/Source/Runtime/Core/public/Math/MathFwd.h b/Engine/Source/Runtime/Core/public/Math/MathFwd.h index 439942a..36529b5 100644 --- a/Engine/Source/Runtime/Core/public/Math/MathFwd.h +++ b/Engine/Source/Runtime/Core/public/Math/MathFwd.h @@ -29,7 +29,6 @@ namespace Phanes::Core::Math { template struct TRay; template struct TLine; template struct TPlane; - template struct TMatrix3; template struct TMatrix4; template struct TQuaternion; template struct TTransform; @@ -40,6 +39,7 @@ namespace Phanes::Core::Math { template struct TIntPoint3; template struct TIntPoint4; template struct TMatrix2; + template struct TMatrix3; template struct TVector2; template struct TVector3; template struct TVector4; diff --git a/Engine/Source/Runtime/Core/public/Math/Matrix2.hpp b/Engine/Source/Runtime/Core/public/Math/Matrix2.hpp index e6106aa..4f8165d 100644 --- a/Engine/Source/Runtime/Core/public/Math/Matrix2.hpp +++ b/Engine/Source/Runtime/Core/public/Math/Matrix2.hpp @@ -94,30 +94,24 @@ namespace Phanes::Core::Math { } public: - - constexpr GetCol(int n) - { - switch (n) - { - case 0: - return this->c0; - case 1: - return this->c1; - default: - break; - } - } + FORCEINLINE T operator() (int n, int m) const { this->data[m][n]; } - FORCEINLINE TVector2& operator[] (int m) const + FORCEINLINE TVector2 operator[] (int m) const { - static_assert(m > -1 && m < 2, "(PHANES_CORE::MATH [Matrix2.hpp]): m must be between 0 or 1."); + switch (m) + { + case 0: + return this->c0; + case 1: + return this->c1; + } - return GetCol(m); + throw std::invalid_argument("m is outside valid range."); } }; @@ -247,15 +241,13 @@ namespace Phanes::Core::Math { template bool operator== (const TMatrix2& m1, const TMatrix2& m2) { - return (abs(m1(0, 0) - m2(0, 0)) < P_FLT_INAC && abs(m1(0, 1) - m2(0, 1)) < P_FLT_INAC && - abs(m1(1, 0) - m2(1, 0)) < P_FLT_INAC && abs(m1(1, 1) - m2(1, 1)) < P_FLT_INAC); + return m1[0] == m2[0] && m1[1] == m2[1]; } template bool operator!= (const TMatrix2& m1, const TMatrix2& m2) { - return (abs(m1(0, 0) - m2(0, 0)) > P_FLT_INAC || abs(m1(0, 1) - m2(0, 1)) > P_FLT_INAC || - abs(m1(1, 0) - m2(1, 0)) > P_FLT_INAC || abs(m1(1, 1) - m2(1, 1)) > P_FLT_INAC); + return m1[0] != m2[0] || m1[1] != m2[1]; } diff --git a/Engine/Source/Runtime/Core/public/Math/Matrix3.hpp b/Engine/Source/Runtime/Core/public/Math/Matrix3.hpp index f1f85ef..a4d9d17 100644 --- a/Engine/Source/Runtime/Core/public/Math/Matrix3.hpp +++ b/Engine/Source/Runtime/Core/public/Math/Matrix3.hpp @@ -3,6 +3,7 @@ #include "Core/public/Math/Boilerplate.h" #include "Core/public/Math/MathAbstractTypes.h" +#include "Core/public/Math/MathFwd.h" #include "Core/public/Math/Vector3.hpp" #ifndef MATRIX3_H @@ -14,12 +15,33 @@ namespace Phanes::Core::Math { // 3x3 Matrix defined in column-major order. // Accessed by M[Row][Col]. - template + template struct TMatrix3 { public: - T m[3][3]; + union + { + struct + { + /// + /// Column one. + /// + TVector3 c0; + + /// + /// Column two. + /// + TVector3 c1; + + /// + /// Column three. + /// + TVector3 c2; + }; + }; + + T data[3][3]; public: @@ -30,22 +52,24 @@ namespace Phanes::Core::Math { * Copy constructor. */ - TMatrix3(const TMatrix3& m1) + TMatrix3(const TMatrix3& m1) { - memcpy(this->m, m1.m, sizeof(T) * 9); + this->c0 = TVector3(m1.c0); + this->c1 = TVector3(m1.c1); + this->c2 = TVector3(m1.c2); } /** * Construct Matrix from 2d array. * - * @param(fields) 2D Array with column major order. + * @param(fields) 2D Array with row major order. */ - TMatrix3(T fields[2][2]) + TMatrix3(T fields[3][3]) { - this->m[0][0] = fields[0][0]; this->m[1][0] = fields[1][0]; this->m[2][0] = fields[2][0]; - this->m[0][1] = fields[0][1]; this->m[1][1] = fields[1][1]; this->m[2][1] = fields[2][1]; - this->m[0][2] = fields[0][2]; this->m[1][2] = fields[1][2]; this->m[2][2] = fields[2][2]; + this->c0 = TVector3(fields[0][0], fields[1][0], fields[2][0]); + this->c1 = TVector3(fields[0][1], fields[1][1], fields[2][1]); + this->c2 = TVector3(fields[0][2], fields[1][2], fields[2][2]); } /** @@ -55,6 +79,7 @@ namespace Phanes::Core::Math { * @param(n10) M[1][0] * @param(n01) M[0][1] * @param(n11) M[1][1] + * ... * * @note nXY = n[Row][Col] */ @@ -63,9 +88,9 @@ namespace Phanes::Core::Math { T n10, T n11, T n12, T n20, T n21, T n22) { - this->m[0][0] = n00; this->m[1][0] = n01; this->m[2][0] = n02; - this->m[1][0] = n10; this->m[1][1] = n11; this->m[2][1] = n12; - this->m[1][2] = n20; this->m[1][2] = n21; this->m[2][2] = n22; + this->c0 = TVector3(n00,n10,n20); + this->c1 = TVector3(n01,n11,n21); + this->c2 = TVector3(n02,n12,n22); } /** @@ -75,11 +100,11 @@ namespace Phanes::Core::Math { * @param(v2) Column one */ - TMatrix3(const TVector3& v1, const TVector3& v2, const TVector3 v3) + TMatrix3(const TVector3& v1, const TVector3& v2, const TVector3 v3) { - this->m[0][0] = v1.x; this->m[1][0] = v2.x; this->m[2][0] = v3.x; - this->m[0][1] = v1.y; this->m[1][1] = v2.y; this->m[2][1] = v3.y; - this->m[0][2] = v1.z; this->m[1][2] = v2.z; this->m[2][2] = v3.z; + this->c0 = v1; + this->c1 = v2; + this->c2 = v3; } public: @@ -99,9 +124,9 @@ namespace Phanes::Core::Math { return this->m[m][n]; } - FORCEINLINE const TVector3& operator[] (int m) const + FORCEINLINE const TVector3& operator[] (int m) const { - return (*reinterpret_cast*>(this->m[m])); + return (*reinterpret_cast*>(this->m[m])); } }; @@ -118,12 +143,12 @@ namespace Phanes::Core::Math { * @param(s) Scalar */ - template - TMatrix3 operator+= (TMatrix3& m1, T s) + template + TMatrix3 operator+= (TMatrix3& m1, T s) { - m1(0, 0) += s; m1(0, 1) += s; m1(0, 2) += s; - m1(1, 0) += s; m1(1, 1) += s; m1(1, 2) += s; - m1(2, 0) += s; m1(2, 1) += s; m1(2, 2) += s; + m1.c0 += s; + m1.c1 += s; + m1.c2 += s; return m1; } @@ -135,12 +160,12 @@ namespace Phanes::Core::Math { * @param(m2) Matrix */ - template - TMatrix3 operator+= (TMatrix3& m1, const TMatrix3& m2) + template + TMatrix3 operator+= (TMatrix3& m1, const TMatrix3& m2) { - m1(0, 0) += m2(0, 0); m1(0, 1) += m2(0, 1); m1(0, 2) += m2(0, 2); - m1(1, 0) += m2(1, 0); m1(1, 1) += m2(1, 1); m1(1, 2) += m2(1, 2); - m1(2, 0) += m2(2, 0); m1(2, 1) += m2(2, 1); m1(2, 2) += m2(2, 2); + m1.c0 += m2.c0; + m1.c1 += m2.c1; + m1.c2 += m2.c2; return m1; } @@ -152,12 +177,12 @@ namespace Phanes::Core::Math { * @param(s) Scalar */ - template - TMatrix3 operator-= (TMatrix3& m1, T s) + template + TMatrix3 operator-= (TMatrix3& m1, T s) { - m1(0, 0) -= s; m1(0, 1) -= s; m1(0, 2) -= s; - m1(1, 0) -= s; m1(1, 1) -= s; m1(1, 2) -= s; - m1(2, 0) -= s; m1(2, 1) -= s; m1(2, 2) -= s; + m1.c0 -= s; + m1.c1 -= s; + m1.c2 -= s; return m1; } @@ -169,12 +194,12 @@ namespace Phanes::Core::Math { * @param(m2) Matrix */ - template - TMatrix3 operator-= (TMatrix3& m1, const TMatrix3& m2) + template + TMatrix3 operator-= (TMatrix3& m1, const TMatrix3& m2) { - m1(0, 0) -= m2(0, 0); m1(0, 1) -= m2(0, 1); m1(0, 2) -= m2(0, 2); - m1(1, 0) -= m2(1, 0); m1(1, 1) -= m2(1, 1); m1(1, 2) -= m2(1, 2); - m1(2, 0) -= m2(2, 0); m1(2, 1) -= m2(2, 1); m1(2, 2) -= m2(2, 2); + m1.c0 -= m2.c0; + m1.c1 -= m2.c1; + m1.c2 -= m2.c2; return m1; } @@ -186,38 +211,29 @@ namespace Phanes::Core::Math { * @param(s) Scalar */ - template - TMatrix3 operator*= (TMatrix3& m1, T s) + template + TMatrix3 operator*= (TMatrix3& m1, T s) { - m1(0, 0) *= s; m1(0, 1) *= s; m1(0, 2) *= s; - m1(1, 0) *= s; m1(1, 1) *= s; m1(1, 2) *= s; - m1(2, 0) *= s; m1(2, 1) *= s; m1(2, 2) *= s; + m1.c0 *= ss; + m1.c1 *= ss; + m1.c2 *= ss; return m1; } /** - * Matrix on matrix multiplication + * Matrix on matrix (componentwise) * * @param(m1) Matrix * @param(m2) Matrix */ - template - TMatrix3 operator*= (TMatrix3& m1, const TMatrix3& m2) + template + TMatrix3 operator*= (TMatrix3& m1, const TMatrix3& m2) { - TMatrix3 c = m1; - m1(0, 0) = c(0, 0) * m2(0, 0) + c(0, 1) * m2(1, 0) + c(0, 2) * m2(2, 0); - m1(0, 1) = c(0, 0) * m2(0, 1) + c(0, 1) * m2(1, 1) + c(0, 2) * m2(2, 1); - m1(0, 2) = c(0, 0) * m2(0, 2) + c(0, 1) * m2(1, 2) + c(0, 2) * m2(2, 2); - - m1(1, 0) = c(1, 0) * m2(0, 0) + c(1, 1) * m2(1, 0) + c(1, 2) * m2(2, 0); - m1(1, 1) = c(1, 0) * m2(0, 1) + c(1, 1) * m2(1, 1) + c(1, 2) * m2(2, 1); - m1(1, 2) = c(1, 0) * m2(0, 2) + c(1, 1) * m2(1, 2) + c(1, 2) * m2(2, 2); - - m1(2, 0) = c(2, 0) * m2(0, 0) + c(2, 1) * m2(1, 0) + c(2, 2) * m2(2, 0); - m1(2, 1) = c(2, 0) * m2(0, 1) + c(2, 1) * m2(1, 1) + c(2, 2) * m2(2, 1); - m1(2, 2) = c(2, 0) * m2(0, 2) + c(2, 1) * m2(1, 2) + c(2, 2) * m2(2, 2); + m1.c0 *= m2.c0; + m1.c1 *= m2.c1; + m1.c2 *= m2.c2; return m1; } @@ -229,12 +245,12 @@ namespace Phanes::Core::Math { * @param(s) Scalar */ - template - TMatrix3 operator+ (const TMatrix3& m, T s) + template + TMatrix3 operator+ (const TMatrix3& m, T s) { - return TMatrix3(m(0, 0) + s, m(0, 1) + s, m(0, 2) + s, - m(1, 0) + s, m(1, 1) + s, m(1, 2) + s, - m(2, 0) + s, m(2, 1) + s, m(2, 2) + s); + return TMatrix3(m.c0 + s, + m.c1 + s, + m.c2 + s); } /** @@ -244,12 +260,12 @@ namespace Phanes::Core::Math { * @param(m2) Matrix */ - template - TMatrix3 operator+ (const TMatrix3& m1, const TMatrix3& m2) + template + TMatrix3 operator+ (const TMatrix3& m1, const TMatrix3& m2) { - return TMatrix2(m1(0, 0) + m2(0, 0), m1(0, 1) + m2(0, 1), m1(0, 2) + m2(0, 2), - m1(1, 0) + m2(1, 0), m1(1, 1) + m2(1, 1), m1(1, 2) + m2(1, 2), - m1(2, 0) + m2(2, 0), m1(2, 1) + m2(2, 1), m1(2, 2) + m2(2, 2)); + return TMatrix2(m1.c0 + m2.c0, + m1.c1 + m2.c1, + m1.c2 + m2.c2); } /** @@ -259,12 +275,12 @@ namespace Phanes::Core::Math { * @param(s) Scalar */ - template - TMatrix3 operator- (const TMatrix3& m, T s) + template + TMatrix3 operator- (const TMatrix3& m, T s) { - return TMatrix3(m(0, 0) - s, m(0, 1) - s, m(0, 2) - s, - m(1, 0) - s, m(1, 1) - s, m(1, 2) - s, - m(2, 0) - s, m(2, 1) - s, m(2, 2) - s); + return TMatrix3(m1.c0 - s, + m1.c1 - s, + m1.c2 - s); } /** @@ -274,12 +290,12 @@ namespace Phanes::Core::Math { * @param(s) Scalar */ - template - TMatrix3 operator- (const TMatrix3& m1, const TMatrix3& m2) + template + TMatrix3 operator- (const TMatrix3& m1, const TMatrix3& m2) { - return TMatrix3(m1(0, 0) - m2(0, 0), m1(0, 1) - m2(0, 1), m1(0, 2) - m2(0, 2), - m1(1, 0) - m2(1, 0), m1(1, 1) - m2(1, 1), m1(1, 2) - m2(1, 2), - m1(2, 0) - m2(2, 0), m1(2, 1) - m2(2, 1), m1(2, 2) - m2(2, 2)); + return TMatrix3(m1.c0 - m2.c0, + m1.c1 - m2.c1, + m1.c2 - m2.c2); } /** @@ -289,50 +305,27 @@ namespace Phanes::Core::Math { * @param(s) Scalar */ - template - TMatrix3 operator* (const TMatrix3& m, float s) + template + TMatrix3 operator* (const TMatrix3& m, float s) { - return TMatrix3(m(0, 0) * s, m(0, 1) * s, m(0, 2) * s, - m(1, 0) * s, m(1, 1) * s, m(1, 2) * s, - m(2, 0) * s, m(2, 1) * s, m(2, 2) * s); + return TMatrix3(m.c0 * s, + m.c1 * s, + m.c2 * s); } /** - * Multiplay matrix by matrix + * Multiplay matrix by matrix (componentwise) * * @param(m1) Matrix * @param(m2) Matrix */ - template - TMatrix3 operator* (const TMatrix3& m1, const TMatrix3& m2) + template + TMatrix3 operator* (const TMatrix3& m1, const TMatrix3& m2) { - return TMatrix3(m1(0, 0) * m2(0, 0) + m1(0, 1) * m2(1, 0) + m1(0, 2) * m2(2, 0), - m1(0, 0) * m2(0, 1) + m1(0, 1) * m2(1, 1) + m1(0, 2) * m2(2, 1), - m1(0, 0) * m2(0, 2) + m1(0, 1) * m2(1, 2) + m1(0, 2) * m2(2, 2), - - m1(1, 0) * m2(0, 0) + m1(1, 1) * m2(1, 0) + m1(1, 2) * m2(2, 0), - m1(1, 0) * m2(0, 1) + m1(1, 1) * m2(1, 1) + m1(1, 2) * m2(2, 1), - m1(1, 0) * m2(0, 2) + m1(1, 1) * m2(1, 2) + m1(1, 2) * m2(2, 2), - - m1(2, 0) * m2(0, 0) + m1(2, 1) * m2(1, 0) + m1(2, 2) * m2(2, 0), - m1(2, 0) * m2(0, 1) + m1(2, 1) * m2(1, 1) + m1(2, 2) * m2(2, 1), - m1(2, 0) * m2(0, 2) + m1(2, 1) * m2(1, 2) + m1(2, 2) * m2(2, 2)); - } - - /** - * Add matrix to matrix componentwise - * - * @param(m1) Matrix - * @param(m2) Matrix - */ - - template - TVector3 operator* (const TMatrix3& m1, const TVector3& v) - { - return TVector3(m1(0, 0) * v.x + m1(0, 1) * v.y + m1(0, 2) * v.z, - m1(1, 0) * v.x + m1(1, 1) * v.y + m1(1, 2) * v.z, - m1(2, 0) * v.x + m1(2, 1) * v.y + m1(2, 2) * v.z); + return TMatrix3(m1.c0 + m2.c0, + m1.c1 + m2.c1, + m1.c2 + m2.c2); } /** @@ -342,8 +335,8 @@ namespace Phanes::Core::Math { * @param(m2) Matrix */ - template - bool operator== (const TMatrix3& m1, const TMatrix3& m2) + template + bool operator== (const TMatrix3& m1, const TMatrix3& m2) { return (m1[0] == m2[0] && m1[1] == m2[1] && m1[2] == m2[2]); } @@ -355,8 +348,8 @@ namespace Phanes::Core::Math { * @param(m2) Matrix */ - template - bool operator!= (const TMatrix3& m1, const TMatrix3& m2) + template + bool operator!= (const TMatrix3& m1, const TMatrix3& m2) { return (m1[0] != m2[0] || m1[1] != m2[1] || m1[2] != m2[2]); } @@ -372,8 +365,8 @@ namespace Phanes::Core::Math { * @param(m1) Matrix */ - template - T Determinant(const TMatrix3& m1) + template + T Determinant(const TMatrix3& m1) { return m1(0, 0) * (m1(1, 1) * m1(2, 2) - m1(1, 2) * m1(2, 1)) - m1(0, 1) * (m1(1, 0) * m1(2, 2) - m1(1, 2) * m1(2, 0)) @@ -390,22 +383,22 @@ namespace Phanes::Core::Math { * @note Stores result in m1. */ - template - TMatrix3 InverseV(TMatrix3& m1) + template + TMatrix3 InverseV(TMatrix3& m1) { - const TVector3& v0 = m1[0]; - const TVector3& v1 = m1[1]; - const TVector3& v2 = m1[2]; + const TVector3& v0 = m1[0]; + const TVector3& v1 = m1[1]; + const TVector3& v2 = m1[2]; - TVector3 r0 = CrossP(v1, v2); - TVector3 r1 = CrossP(v2, v0); - TVector3 r2 = CrossP(v0, v1); + TVector3 r0 = CrossP(v1, v2); + TVector3 r1 = CrossP(v2, v0); + TVector3 r2 = CrossP(v0, v1); - T _1_det = (T)1.0 / determinant(m1); + T _1_det = (T)1.0 / Determinant(m1); - m1 = TMatrix3(r0.x, r0.y, r0.z, - r1.x, r1.y, r1.z, - r2.x, r2.y, r2.z); + m1 = TMatrix3(r0.x, r0.y, r0.z, + r1.x, r1.y, r1.z, + r2.x, r2.y, r2.z); m1 *= _1_det; @@ -420,8 +413,8 @@ namespace Phanes::Core::Math { * @note Result is stored in m1; */ - template - TMatrix3 TransposeV(TMatrix3& m1) + template + TMatrix3 TransposeV(TMatrix3& m1) { Swap(m1(0, 1), m1(1, 0)); Swap(m1(0, 2), m1(2, 0)); @@ -441,8 +434,8 @@ namespace Phanes::Core::Math { * @param(m1) Matrix */ - template - TMatrix3 Inverse(TMatrix3& m1) + template + TMatrix3 Inverse(TMatrix3& m1) { const TVector3& v0 = m1[0]; const TVector3& v1 = m1[1]; @@ -454,7 +447,7 @@ namespace Phanes::Core::Math { T _1_det = (T)1.0 / Determinant(m1); - TMatrix3 inverse(r0.x, r0.y, r0.z, + TMatrix3 inverse(r0.x, r0.y, r0.z, r1.x, r1.y, r1.z, r2.x, r2.y, r2.z); @@ -471,10 +464,10 @@ namespace Phanes::Core::Math { * @note Result is stored in m1; */ - template - TMatrix3 Transpose(const TMatrix3& m1) + template + TMatrix3 Transpose(const TMatrix3& m1) { - return TMatrix3(m1(0, 0), m1(1, 0), m1(2, 0), + return 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)); } @@ -483,12 +476,12 @@ namespace Phanes::Core::Math { * Checks if matrix is an identity matrix. */ - template - bool IsIndentityMatrix(const TMatrix3& m1) + template + bool IsIdentityMatrix(const TMatrix3& m1) { - return (abs(m1(0, 0) - (T)1.0) < P_FLT_INAC && abs(m1(0, 1) - (T)1.0) < P_FLT_INAC && abs(m1(0, 2) - (T)1.0) < P_FLT_INAC && - abs(m1(1, 0) - (T)1.0) < P_FLT_INAC && abs(m1(1, 1) - (T)1.0) < P_FLT_INAC && abs(m1(1, 2) - (T)1.0) < P_FLT_INAC && - abs(m1(2, 0) - (T)1.0) < P_FLT_INAC && abs(m1(2, 1) - (T)1.0) < P_FLT_INAC && abs(m1(2, 2) - (T)1.0) < P_FLT_INAC); + return (abs(m1(0, 0) - (T)1.0) < P_FLT_INAC && abs(m1(0, 1) - (T)0.0) < P_FLT_INAC && abs(m1(0, 2) - (T)0.0) < P_FLT_INAC && + abs(m1(1, 0) - (T)0.0) < P_FLT_INAC && abs(m1(1, 1) - (T)1.0) < P_FLT_INAC && abs(m1(1, 2) - (T)0.0) < P_FLT_INAC && + abs(m1(2, 0) - (T)0.0) < P_FLT_INAC && abs(m1(2, 1) - (T)0.0) < P_FLT_INAC && abs(m1(2, 2) - (T)1.0) < P_FLT_INAC); } } // Phanes::Core::Math diff --git a/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathFPU.hpp b/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathFPU.hpp index e69de29..3c6bd20 100644 --- a/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathFPU.hpp +++ b/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathFPU.hpp @@ -0,0 +1,66 @@ +#pragma once + +#include "Core/public/Math/SIMD/PhanesSIMDTypes.h" +#include "Core/public/Math/MathCommon.hpp" + + +namespace Phanes::Core::Math::SIMD +{ + /// + /// Adds all scalars of the vector. + /// + /// Vector + /// Sum stored in v[0:31]. + Phanes::Core::Types::Vec4f32Reg vec4_hadd(const Phanes::Core::Types::Vec4f32Reg v) + { + Phanes::Core::Types::Vec4f32Reg r; + r.data[0] = v.data[0] + v.data[1] + v.data[2] + v.data[3]; + } + + /// + /// Adds all scalars of the vector. + /// + /// Vector + /// Sum of components. + float vec4_hadd_cvtf32(const Phanes::Core::Types::Vec4f32Reg v) + { + return v.data[0] + v.data[1] + v.data[2] + v.data[3]; + } + + /// + /// Gets the absolute value of each scalar in the vector. + /// + /// Vector + /// Vector with all components positive. + Phanes::Core::Types::Vec4f32Reg vec4_abs(const Phanes::Core::Types::Vec4f32Reg v) + { + Phanes::Core::Types::Vec4f32Reg r; + + r.data[0] = Abs(v.data[0]); + } + + /// + /// Gets the dot product of the + /// + /// + /// + /// + Phanes::Core::Types::Vec4f32Reg vec4_dot(const Phanes::Core::Types::Vec4f32Reg v1, const Phanes::Core::Types::Vec4f32Reg v2) + { + Phanes::Core::Types::Vec4f32Reg r; + r.data[0] = v1.data[0] * v1.data[0] + v1.data[1] * v2.data[1] + v1.data[2] * v2.data[2] + v1.data[3] * v2.data[3]; + + return r; + } + + /// + /// Gets the dot product of the + /// + /// + /// + /// + float vec4_dot_cvtf32(const Phanes::Core::Types::Vec4f32Reg v1, const Phanes::Core::Types::Vec4f32Reg v2) + { + return v1.data[0] * v1.data[0] + v1.data[1] * v2.data[1] + v1.data[2] * v2.data[2] + v1.data[3] * v2.data[3]; + } +} \ No newline at end of file diff --git a/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathSSE.hpp b/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathSSE.hpp index c3ddc84..eeb20d6 100644 --- a/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathSSE.hpp +++ b/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesVectorMathSSE.hpp @@ -22,6 +22,19 @@ namespace Phanes::Core::Math::SIMD { + Phanes::Core::Types::Vec4f32Reg vec4_cross_p(const Phanes::Core::Types::Vec4f32Reg v1, const Phanes::Core::Types::Vec4f32Reg v2) + { + __m128 tmp0 = _mm_shuffle_ps(v1, v1, _MM_SHUFFLE(3, 0, 2, 1)); + __m128 tmp1 = _mm_shuffle_ps(v2, v2, _MM_SHUFFLE(3, 1, 0, 2)); + __m128 tmp2 = _mm_shuffle_ps(v1, v1, _MM_SHUFFLE(3, 1, 0, 2)); + __m128 tmp3 = _mm_shuffle_ps(v2, v2, _MM_SHUFFLE(3, 0, 2, 1)); + return _mm_sub_ps( + _mm_mul_ps(tmp0, tmp1), + _mm_mul_ps(tmp2, tmp3) + ); + } + + /// /// Adds all scalars of the vector. /// @@ -247,6 +260,15 @@ namespace Phanes::Core::Math::Detail template<> struct compute_vec3_inc : public compute_vec4_inc {}; template<> struct compute_vec3_dec : public compute_vec4_dec {}; + template<> + struct compute_vec3_cross_p + { + static FORCEINLINE void map(Phanes::Core::Math::TVector3& r, const Phanes::Core::Math::TVector3& v1, const Phanes::Core::Math::TVector3& v2) + { + r.data = Phanes::Core::Math::SIMD::vec4_cross_p(v1.data, v2.data); + } + }; + // ============ // // TVector2 // // ============ // diff --git a/Engine/Source/Runtime/Core/public/Math/Vector3.hpp b/Engine/Source/Runtime/Core/public/Math/Vector3.hpp index e3cf5c2..b7ed19b 100644 --- a/Engine/Source/Runtime/Core/public/Math/Vector3.hpp +++ b/Engine/Source/Runtime/Core/public/Math/Vector3.hpp @@ -504,18 +504,7 @@ namespace Phanes::Core::Math { */ template - TVector3 CrossPV(TVector3& v1, const TVector3& v2) - { - float x = v1.x; - float y = v1.y; - float z = v1.z; - - v1.x = (y * v2.z) - (z * v2.y); - v1.y = (z * v2.x) - (x * v2.z); - v1.z = (x * v2.y) - (y * v2.x); - - return v1; - } + TVector3 CrossPV(TVector3& v1, const TVector3& v2); /** * Gets the componentwise max of both vectors. @@ -991,12 +980,7 @@ namespace Phanes::Core::Math { */ template - TVector3 CrossP(const TVector3& v1, const TVector3& v2) - { - return TVector3((v1.y * v2.z) - (v1.z * v2.y), - (v1.z * v2.x) - (v1.x * v2.z), - (v1.x * v2.y) - (v1.y * v2.x)); - } + TVector3 CrossP(const TVector3& v1, const TVector3& v2); /** * Linearly interpolates between two vectors. diff --git a/Engine/Source/Runtime/Core/public/Math/Vector3.inl b/Engine/Source/Runtime/Core/public/Math/Vector3.inl index 6702ac1..f40271a 100644 --- a/Engine/Source/Runtime/Core/public/Math/Vector3.inl +++ b/Engine/Source/Runtime/Core/public/Math/Vector3.inl @@ -208,4 +208,21 @@ namespace Phanes::Core::Math { return --v1; } + + + // Other + + template + TVector3 CrossP(const TVector3& v1, const TVector3& v2) + { + TVector3 r; + Detail::compute_vec3_cross_p::map(r, v1, v2); + return r; + } + + template + TVector3 CrossPV(TVector3& v1, const TVector3& v2) + { + Detail::compute_vec3_cross_p::map(v1, v1, v2); + } } \ No newline at end of file