From b9a00a40a9a61c3df5c774fd1cc9d04f57994a0c Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Tue, 29 Jan 2019 16:05:51 -0600 Subject: [PATCH 1/2] Common: Move Matrix classes out of MathUtil into their own files and make their interface more friendly. --- Source/Core/Common/CMakeLists.txt | 1 + Source/Core/Common/Common.vcxproj | 4 +- Source/Core/Common/Common.vcxproj.filters | 4 +- Source/Core/Common/MathUtil.cpp | 126 +------------- Source/Core/Common/MathUtil.h | 37 ---- Source/Core/Common/Matrix.cpp | 158 ++++++++++++++++++ Source/Core/Common/Matrix.h | 88 ++++++++++ .../Core/VideoCommon/VertexShaderManager.cpp | 47 ++---- 8 files changed, 272 insertions(+), 193 deletions(-) create mode 100644 Source/Core/Common/Matrix.cpp create mode 100644 Source/Core/Common/Matrix.h diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt index b75c88d013..dbc1ea15e8 100644 --- a/Source/Core/Common/CMakeLists.txt +++ b/Source/Core/Common/CMakeLists.txt @@ -24,6 +24,7 @@ add_library(common JitRegister.cpp Logging/LogManager.cpp MathUtil.cpp + Matrix.cpp MD5.cpp MemArena.cpp MemoryUtil.cpp diff --git a/Source/Core/Common/Common.vcxproj b/Source/Core/Common/Common.vcxproj index 409546e43b..75b1563876 100644 --- a/Source/Core/Common/Common.vcxproj +++ b/Source/Core/Common/Common.vcxproj @@ -134,6 +134,7 @@ + @@ -201,6 +202,7 @@ + @@ -256,4 +258,4 @@ - \ No newline at end of file + diff --git a/Source/Core/Common/Common.vcxproj.filters b/Source/Core/Common/Common.vcxproj.filters index 63a131a8dc..47c0b42053 100644 --- a/Source/Core/Common/Common.vcxproj.filters +++ b/Source/Core/Common/Common.vcxproj.filters @@ -56,6 +56,7 @@ + @@ -291,6 +292,7 @@ + @@ -362,4 +364,4 @@ - \ No newline at end of file + diff --git a/Source/Core/Common/MathUtil.cpp b/Source/Core/Common/MathUtil.cpp index c1be813cc1..f745eae45a 100644 --- a/Source/Core/Common/MathUtil.cpp +++ b/Source/Core/Common/MathUtil.cpp @@ -2,136 +2,12 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. -#include -#include -#include -#include - -#include "Common/CommonTypes.h" #include "Common/MathUtil.h" -inline void MatrixMul(int n, const float* a, const float* b, float* result) -{ - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < n; ++j) - { - float temp = 0; - for (int k = 0; k < n; ++k) - { - temp += a[i * n + k] * b[k * n + j]; - } - result[i * n + j] = temp; - } - } -} +#include // Calculate sum of a float list float MathFloatVectorSum(const std::vector& Vec) { return std::accumulate(Vec.begin(), Vec.end(), 0.0f); } - -void Matrix33::LoadIdentity(Matrix33& mtx) -{ - memset(mtx.data, 0, sizeof(mtx.data)); - mtx.data[0] = 1.0f; - mtx.data[4] = 1.0f; - mtx.data[8] = 1.0f; -} - -void Matrix33::RotateX(Matrix33& mtx, float rad) -{ - float s = sin(rad); - float c = cos(rad); - memset(mtx.data, 0, sizeof(mtx.data)); - mtx.data[0] = 1; - mtx.data[4] = c; - mtx.data[5] = -s; - mtx.data[7] = s; - mtx.data[8] = c; -} -void Matrix33::RotateY(Matrix33& mtx, float rad) -{ - float s = sin(rad); - float c = cos(rad); - memset(mtx.data, 0, sizeof(mtx.data)); - mtx.data[0] = c; - mtx.data[2] = s; - mtx.data[4] = 1; - mtx.data[6] = -s; - mtx.data[8] = c; -} - -void Matrix33::Multiply(const Matrix33& a, const Matrix33& b, Matrix33& result) -{ - MatrixMul(3, a.data, b.data, result.data); -} - -void Matrix33::Multiply(const Matrix33& a, const float vec[3], float result[3]) -{ - for (int i = 0; i < 3; ++i) - { - result[i] = 0; - - for (int k = 0; k < 3; ++k) - { - result[i] += a.data[i * 3 + k] * vec[k]; - } - } -} - -void Matrix44::LoadIdentity(Matrix44& mtx) -{ - memset(mtx.data, 0, sizeof(mtx.data)); - mtx.data[0] = 1.0f; - mtx.data[5] = 1.0f; - mtx.data[10] = 1.0f; - mtx.data[15] = 1.0f; -} - -void Matrix44::LoadMatrix33(Matrix44& mtx, const Matrix33& m33) -{ - for (int i = 0; i < 3; ++i) - { - for (int j = 0; j < 3; ++j) - { - mtx.data[i * 4 + j] = m33.data[i * 3 + j]; - } - } - - for (int i = 0; i < 3; ++i) - { - mtx.data[i * 4 + 3] = 0; - mtx.data[i + 12] = 0; - } - mtx.data[15] = 1.0f; -} - -void Matrix44::Set(Matrix44& mtx, const float mtxArray[16]) -{ - for (int i = 0; i < 16; ++i) - { - mtx.data[i] = mtxArray[i]; - } -} - -void Matrix44::Translate(Matrix44& mtx, const float vec[3]) -{ - LoadIdentity(mtx); - mtx.data[3] = vec[0]; - mtx.data[7] = vec[1]; - mtx.data[11] = vec[2]; -} - -void Matrix44::Shear(Matrix44& mtx, const float a, const float b) -{ - LoadIdentity(mtx); - mtx.data[2] = a; - mtx.data[6] = b; -} - -void Matrix44::Multiply(const Matrix44& a, const Matrix44& b, Matrix44& result) -{ - MatrixMul(4, a.data, b.data, result.data); -} diff --git a/Source/Core/Common/MathUtil.h b/Source/Core/Common/MathUtil.h index 01a39db5b1..bfbf0a8a77 100644 --- a/Source/Core/Common/MathUtil.h +++ b/Source/Core/Common/MathUtil.h @@ -5,8 +5,6 @@ #pragma once #include -#include -#include #include #include "Common/CommonTypes.h" @@ -113,38 +111,3 @@ inline int IntLog2(u64 val) return result; #endif } - -// Tiny matrix/vector library. -// Used for things like Free-Look in the gfx backend. - -class Matrix33 -{ -public: - static void LoadIdentity(Matrix33& mtx); - - // set mtx to be a rotation matrix around the x axis - static void RotateX(Matrix33& mtx, float rad); - // set mtx to be a rotation matrix around the y axis - static void RotateY(Matrix33& mtx, float rad); - - // set result = a x b - static void Multiply(const Matrix33& a, const Matrix33& b, Matrix33& result); - static void Multiply(const Matrix33& a, const float vec[3], float result[3]); - - float data[9]; -}; - -class Matrix44 -{ -public: - static void LoadIdentity(Matrix44& mtx); - static void LoadMatrix33(Matrix44& mtx, const Matrix33& m33); - static void Set(Matrix44& mtx, const float mtxArray[16]); - - static void Translate(Matrix44& mtx, const float vec[3]); - static void Shear(Matrix44& mtx, const float a, const float b = 0); - - static void Multiply(const Matrix44& a, const Matrix44& b, Matrix44& result); - - float data[16]; -}; diff --git a/Source/Core/Common/Matrix.cpp b/Source/Core/Common/Matrix.cpp new file mode 100644 index 0000000000..97f8471945 --- /dev/null +++ b/Source/Core/Common/Matrix.cpp @@ -0,0 +1,158 @@ +// Copyright 2019 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "Common/Matrix.h" + +#include +#include + +inline void MatrixMul(int n, const float* a, const float* b, float* result) +{ + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < n; ++j) + { + float temp = 0; + for (int k = 0; k < n; ++k) + { + temp += a[i * n + k] * b[k * n + j]; + } + result[i * n + j] = temp; + } + } +} + +Matrix33 Matrix33::Identity() +{ + Matrix33 mtx = {}; + mtx.data[0] = 1.0f; + mtx.data[4] = 1.0f; + mtx.data[8] = 1.0f; + return mtx; +} + +Matrix33 Matrix33::RotateX(float rad) +{ + float s = sin(rad); + float c = cos(rad); + Matrix33 mtx = {}; + mtx.data[0] = 1; + mtx.data[4] = c; + mtx.data[5] = -s; + mtx.data[7] = s; + mtx.data[8] = c; + return mtx; +} + +Matrix33 Matrix33::RotateY(float rad) +{ + float s = sin(rad); + float c = cos(rad); + Matrix33 mtx = {}; + mtx.data[0] = c; + mtx.data[2] = s; + mtx.data[4] = 1; + mtx.data[6] = -s; + mtx.data[8] = c; + return mtx; +} + +Matrix33 Matrix33::RotateZ(float rad) +{ + float s = sin(rad); + float c = cos(rad); + Matrix33 mtx = {}; + mtx.data[0] = c; + mtx.data[1] = -s; + mtx.data[3] = s; + mtx.data[4] = c; + mtx.data[8] = 1; + return mtx; +} + +Matrix33 Matrix33::Scale(const Vec3& vec) +{ + Matrix33 mtx = {}; + mtx.data[0] = vec.x; + mtx.data[4] = vec.y; + mtx.data[8] = vec.z; + return mtx; +} + +void Matrix33::Multiply(const Matrix33& a, const Matrix33& b, Matrix33* result) +{ + MatrixMul(3, a.data.data(), b.data.data(), result->data.data()); +} + +void Matrix33::Multiply(const Matrix33& a, const Vec3& vec, Vec3* result) +{ + for (int i = 0; i < 3; ++i) + { + result->data[i] = 0; + + for (int k = 0; k < 3; ++k) + { + result->data[i] += a.data[i * 3 + k] * vec.data[k]; + } + } +} + +Matrix44 Matrix44::Identity() +{ + Matrix44 mtx = {}; + mtx.data[0] = 1.0f; + mtx.data[5] = 1.0f; + mtx.data[10] = 1.0f; + mtx.data[15] = 1.0f; + return mtx; +} + +Matrix44 Matrix44::FromMatrix33(const Matrix33& m33) +{ + Matrix44 mtx; + for (int i = 0; i < 3; ++i) + { + for (int j = 0; j < 3; ++j) + { + mtx.data[i * 4 + j] = m33.data[i * 3 + j]; + } + } + + for (int i = 0; i < 3; ++i) + { + mtx.data[i * 4 + 3] = 0; + mtx.data[i + 12] = 0; + } + mtx.data[15] = 1.0f; + return mtx; +} + +Matrix44 Matrix44::FromArray(const float mtxArray[16]) +{ + Matrix44 mtx; + std::copy_n(mtxArray, 16, std::begin(mtx.data)); + return mtx; +} + +Matrix44 Matrix44::Translate(const Vec3& vec) +{ + Matrix44 mtx = Matrix44::Identity(); + mtx.data[3] = vec.x; + mtx.data[7] = vec.y; + mtx.data[11] = vec.y; + return mtx; +} + +Matrix44 Matrix44::Shear(const float a, const float b) +{ + Matrix44 mtx = Matrix44::Identity(); + mtx.data[2] = a; + mtx.data[6] = b; + return mtx; +} + +void Matrix44::Multiply(const Matrix44& a, const Matrix44& b, Matrix44& result) +{ + MatrixMul(4, a.data.data(), b.data.data(), result.data.data()); +} diff --git a/Source/Core/Common/Matrix.h b/Source/Core/Common/Matrix.h new file mode 100644 index 0000000000..e9c25fd026 --- /dev/null +++ b/Source/Core/Common/Matrix.h @@ -0,0 +1,88 @@ +// Copyright 2019 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include + +// Tiny matrix/vector library. +// Used for things like Free-Look in the gfx backend. + +union Vec3 +{ + Vec3() {} + Vec3(float _x, float _y, float _z) : data{_x, _y, _z} {} + + std::array data = {}; + + struct + { + float x; + float y; + float z; + }; +}; + +class Matrix33 +{ +public: + static Matrix33 Identity(); + + // Return a rotation matrix around the x,y,z axis + static Matrix33 RotateX(float rad); + static Matrix33 RotateY(float rad); + static Matrix33 RotateZ(float rad); + + static Matrix33 Scale(const Vec3& vec); + + // set result = a x b + static void Multiply(const Matrix33& a, const Matrix33& b, Matrix33* result); + static void Multiply(const Matrix33& a, const Vec3& vec, Vec3* result); + + Matrix33& operator*=(const Matrix33& rhs) + { + Multiply(Matrix33(*this), rhs, this); + return *this; + } + + std::array data; +}; + +inline Matrix33 operator*(Matrix33 lhs, const Matrix33& rhs) +{ + return lhs *= rhs; +} + +inline Vec3 operator*(const Matrix33& lhs, const Vec3& rhs) +{ + Vec3 result; + Matrix33::Multiply(lhs, rhs, &result); + return result; +} + +class Matrix44 +{ +public: + static Matrix44 Identity(); + static Matrix44 FromMatrix33(const Matrix33& m33); + static Matrix44 FromArray(const float mtxArray[16]); + + static Matrix44 Translate(const Vec3& vec); + static Matrix44 Shear(const float a, const float b = 0); + + static void Multiply(const Matrix44& a, const Matrix44& b, Matrix44& result); + + Matrix44& operator*=(const Matrix44& rhs) + { + Multiply(Matrix44(*this), rhs, *this); + return *this; + } + + std::array data; +}; + +inline Matrix44 operator*(Matrix44 lhs, const Matrix44& rhs) +{ + return lhs *= rhs; +} \ No newline at end of file diff --git a/Source/Core/VideoCommon/VertexShaderManager.cpp b/Source/Core/VideoCommon/VertexShaderManager.cpp index fca6a420c5..b68534aa8e 100644 --- a/Source/Core/VideoCommon/VertexShaderManager.cpp +++ b/Source/Core/VideoCommon/VertexShaderManager.cpp @@ -13,7 +13,7 @@ #include "Common/CommonFuncs.h" #include "Common/CommonTypes.h" #include "Common/Logging/Log.h" -#include "Common/MathUtil.h" +#include "Common/Matrix.h" #include "Core/ConfigManager.h" #include "Core/Core.h" #include "VideoCommon/BPFunctions.h" @@ -86,7 +86,7 @@ static void ViewportCorrectionMatrix(Matrix44& result) float Wd = (X + intendedWd <= EFB_WIDTH) ? intendedWd : (EFB_WIDTH - X); float Ht = (Y + intendedHt <= EFB_HEIGHT) ? intendedHt : (EFB_HEIGHT - Y); - Matrix44::LoadIdentity(result); + result = Matrix44::Identity(); if (Wd == 0 || Ht == 0) return; @@ -121,7 +121,7 @@ void VertexShaderManager::Init() ResetView(); // TODO: should these go inside ResetView()? - Matrix44::LoadIdentity(s_viewportCorrection); + s_viewportCorrection = Matrix44::Identity(); memset(g_fProjectionMatrix, 0, sizeof(g_fProjectionMatrix)); for (int i = 0; i < 4; ++i) g_fProjectionMatrix[i * 5] = 1.0f; @@ -454,26 +454,19 @@ void VertexShaderManager::SetConstants() if (g_ActiveConfig.bFreeLook && xfmem.projection.type == GX_PERSPECTIVE) { - Matrix44 mtxA; - Matrix44 mtxB; - Matrix44 viewMtx; + auto mtxA = Matrix44::Translate(s_fViewTranslationVector); + auto mtxB = Matrix44::FromMatrix33(s_viewRotationMatrix); + const auto viewMtx = mtxB * mtxA; // view = rotation x translation - Matrix44::Translate(mtxA, s_fViewTranslationVector); - Matrix44::LoadMatrix33(mtxB, s_viewRotationMatrix); - Matrix44::Multiply(mtxB, mtxA, viewMtx); // view = rotation x translation - Matrix44::Set(mtxB, g_fProjectionMatrix); - Matrix44::Multiply(mtxB, viewMtx, mtxA); // mtxA = projection x view - Matrix44::Multiply(s_viewportCorrection, mtxA, mtxB); // mtxB = viewportCorrection x mtxA - memcpy(constants.projection.data(), mtxB.data, 4 * sizeof(float4)); + mtxA = Matrix44::FromArray(g_fProjectionMatrix) * viewMtx; // mtxA = projection x view + mtxB = s_viewportCorrection * mtxA; // mtxB = viewportCorrection x mtxA + memcpy(constants.projection.data(), mtxB.data.data(), 4 * sizeof(float4)); } else { - Matrix44 projMtx; - Matrix44::Set(projMtx, g_fProjectionMatrix); - - Matrix44 correctedMtx; - Matrix44::Multiply(s_viewportCorrection, projMtx, correctedMtx); - memcpy(constants.projection.data(), correctedMtx.data, 4 * sizeof(float4)); + const auto projMtx = Matrix44::FromArray(g_fProjectionMatrix); + const auto correctedMtx = s_viewportCorrection * projMtx; + memcpy(constants.projection.data(), correctedMtx.data.data(), 4 * sizeof(float4)); } dirty = true; @@ -677,16 +670,12 @@ void VertexShaderManager::RotateView(float x, float y) s_fViewRotation[0] += x; s_fViewRotation[1] += y; - Matrix33 mx; - Matrix33 my; - Matrix33::RotateX(mx, s_fViewRotation[1]); - Matrix33::RotateY(my, s_fViewRotation[0]); - Matrix33::Multiply(mx, my, s_viewRotationMatrix); + s_viewRotationMatrix = + Matrix33::RotateX(s_fViewRotation[1]) * Matrix33::RotateY(s_fViewRotation[0]); // reverse rotation - Matrix33::RotateX(mx, -s_fViewRotation[1]); - Matrix33::RotateY(my, -s_fViewRotation[0]); - Matrix33::Multiply(my, mx, s_viewInvRotationMatrix); + s_viewInvRotationMatrix = + Matrix33::RotateY(-s_fViewRotation[0]) * Matrix33::RotateX(-s_fViewRotation[1]); bProjectionChanged = true; } @@ -694,8 +683,8 @@ void VertexShaderManager::RotateView(float x, float y) void VertexShaderManager::ResetView() { memset(s_fViewTranslationVector, 0, sizeof(s_fViewTranslationVector)); - Matrix33::LoadIdentity(s_viewRotationMatrix); - Matrix33::LoadIdentity(s_viewInvRotationMatrix); + s_viewRotationMatrix = Matrix33::Identity(); + s_viewInvRotationMatrix = Matrix33::Identity(); s_fViewRotation[0] = s_fViewRotation[1] = 0.0f; bProjectionChanged = true; From 69b617ce766e5b1b8956a29f7a8caeed21f6024d Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Tue, 29 Jan 2019 17:12:06 -0600 Subject: [PATCH 2/2] WiimoteEmu: Remove redundant Matrix library and use the one in Common. --- Source/Core/Common/Matrix.cpp | 32 ++-- Source/Core/Common/Matrix.h | 26 +++- Source/Core/Core/Core.vcxproj | 3 +- Source/Core/Core/Core.vcxproj.filters | 3 - Source/Core/Core/HW/WiimoteEmu/Camera.cpp | 29 ++-- Source/Core/Core/HW/WiimoteEmu/MatrixMath.h | 141 ------------------ .../VideoBackends/Vulkan/VulkanContext.cpp | 3 +- .../Core/VideoCommon/VertexShaderManager.cpp | 38 +++-- 8 files changed, 71 insertions(+), 204 deletions(-) delete mode 100644 Source/Core/Core/HW/WiimoteEmu/MatrixMath.h diff --git a/Source/Core/Common/Matrix.cpp b/Source/Core/Common/Matrix.cpp index 97f8471945..dd394b7be5 100644 --- a/Source/Core/Common/Matrix.cpp +++ b/Source/Core/Common/Matrix.cpp @@ -4,10 +4,12 @@ #include "Common/Matrix.h" -#include #include +#include -inline void MatrixMul(int n, const float* a, const float* b, float* result) +namespace +{ +void MatrixMul(int n, const float* a, const float* b, float* result) { for (int i = 0; i < n; ++i) { @@ -22,7 +24,10 @@ inline void MatrixMul(int n, const float* a, const float* b, float* result) } } } +} // namespace +namespace Common +{ Matrix33 Matrix33::Identity() { Matrix33 mtx = {}; @@ -34,8 +39,8 @@ Matrix33 Matrix33::Identity() Matrix33 Matrix33::RotateX(float rad) { - float s = sin(rad); - float c = cos(rad); + const float s = sin(rad); + const float c = cos(rad); Matrix33 mtx = {}; mtx.data[0] = 1; mtx.data[4] = c; @@ -47,8 +52,8 @@ Matrix33 Matrix33::RotateX(float rad) Matrix33 Matrix33::RotateY(float rad) { - float s = sin(rad); - float c = cos(rad); + const float s = sin(rad); + const float c = cos(rad); Matrix33 mtx = {}; mtx.data[0] = c; mtx.data[2] = s; @@ -60,8 +65,8 @@ Matrix33 Matrix33::RotateY(float rad) Matrix33 Matrix33::RotateZ(float rad) { - float s = sin(rad); - float c = cos(rad); + const float s = sin(rad); + const float c = cos(rad); Matrix33 mtx = {}; mtx.data[0] = c; mtx.data[1] = -s; @@ -128,10 +133,10 @@ Matrix44 Matrix44::FromMatrix33(const Matrix33& m33) return mtx; } -Matrix44 Matrix44::FromArray(const float mtxArray[16]) +Matrix44 Matrix44::FromArray(const std::array& arr) { Matrix44 mtx; - std::copy_n(mtxArray, 16, std::begin(mtx.data)); + mtx.data = arr; return mtx; } @@ -140,7 +145,7 @@ Matrix44 Matrix44::Translate(const Vec3& vec) Matrix44 mtx = Matrix44::Identity(); mtx.data[3] = vec.x; mtx.data[7] = vec.y; - mtx.data[11] = vec.y; + mtx.data[11] = vec.z; return mtx; } @@ -152,7 +157,8 @@ Matrix44 Matrix44::Shear(const float a, const float b) return mtx; } -void Matrix44::Multiply(const Matrix44& a, const Matrix44& b, Matrix44& result) +void Matrix44::Multiply(const Matrix44& a, const Matrix44& b, Matrix44* result) { - MatrixMul(4, a.data.data(), b.data.data(), result.data.data()); + MatrixMul(4, a.data.data(), b.data.data(), result->data.data()); } +} // namespace Common diff --git a/Source/Core/Common/Matrix.h b/Source/Core/Common/Matrix.h index e9c25fd026..f5d38dc7da 100644 --- a/Source/Core/Common/Matrix.h +++ b/Source/Core/Common/Matrix.h @@ -9,11 +9,21 @@ // Tiny matrix/vector library. // Used for things like Free-Look in the gfx backend. +namespace Common +{ union Vec3 { - Vec3() {} + Vec3() = default; Vec3(float _x, float _y, float _z) : data{_x, _y, _z} {} + Vec3& operator+=(const Vec3& rhs) + { + x += rhs.x; + y += rhs.y; + z += rhs.z; + return *this; + } + std::array data = {}; struct @@ -24,6 +34,11 @@ union Vec3 }; }; +inline Vec3 operator+(Vec3 lhs, const Vec3& rhs) +{ + return lhs += rhs; +} + class Matrix33 { public: @@ -66,16 +81,16 @@ class Matrix44 public: static Matrix44 Identity(); static Matrix44 FromMatrix33(const Matrix33& m33); - static Matrix44 FromArray(const float mtxArray[16]); + static Matrix44 FromArray(const std::array& arr); static Matrix44 Translate(const Vec3& vec); static Matrix44 Shear(const float a, const float b = 0); - static void Multiply(const Matrix44& a, const Matrix44& b, Matrix44& result); + static void Multiply(const Matrix44& a, const Matrix44& b, Matrix44* result); Matrix44& operator*=(const Matrix44& rhs) { - Multiply(Matrix44(*this), rhs, *this); + Multiply(Matrix44(*this), rhs, this); return *this; } @@ -85,4 +100,5 @@ public: inline Matrix44 operator*(Matrix44 lhs, const Matrix44& rhs) { return lhs *= rhs; -} \ No newline at end of file +} +} // namespace Common diff --git a/Source/Core/Core/Core.vcxproj b/Source/Core/Core/Core.vcxproj index 837fbfdd15..bff81652df 100644 --- a/Source/Core/Core/Core.vcxproj +++ b/Source/Core/Core/Core.vcxproj @@ -451,7 +451,6 @@ - @@ -616,4 +615,4 @@ - \ No newline at end of file + diff --git a/Source/Core/Core/Core.vcxproj.filters b/Source/Core/Core/Core.vcxproj.filters index 114b9a61ac..7955ded024 100644 --- a/Source/Core/Core/Core.vcxproj.filters +++ b/Source/Core/Core/Core.vcxproj.filters @@ -1221,9 +1221,6 @@ HW %28Flipper/Hollywood%29\Wiimote\Emu - - HW %28Flipper/Hollywood%29\Wiimote\Emu - HW %28Flipper/Hollywood%29\Wiimote\Emu diff --git a/Source/Core/Core/HW/WiimoteEmu/Camera.cpp b/Source/Core/Core/HW/WiimoteEmu/Camera.cpp index b2807e7122..93b587c0be 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Camera.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Camera.cpp @@ -4,10 +4,12 @@ #include "Core/HW/WiimoteEmu/Camera.h" +#include + #include "Common/BitUtils.h" #include "Common/ChunkFile.h" +#include "Common/Matrix.h" #include "Core/HW/WiimoteCommon/WiimoteReport.h" -#include "Core/HW/WiimoteEmu/MatrixMath.h" namespace WiimoteEmu { @@ -40,30 +42,25 @@ int CameraLogic::BusWrite(u8 slave_addr, u8 addr, int count, const u8* data_in) void CameraLogic::Update(const ControllerEmu::Cursor::StateData& cursor, const NormalizedAccelData& accel, bool sensor_bar_on_top) { - double nsin, ncos; + double nsin; // Ugly code to figure out the wiimote's current angle. // TODO: Kill this. double ax = accel.x; double az = accel.z; - const double len = sqrt(ax * ax + az * az); + const double len = std::sqrt(ax * ax + az * az); if (len) { ax /= len; az /= len; // normalizing the vector nsin = ax; - ncos = az; } else { nsin = 0; - ncos = 1; } - const double ir_sin = nsin; - const double ir_cos = ncos; - static constexpr int camWidth = 1024; static constexpr int camHeight = 768; static constexpr double bndleft = 0.78820266; @@ -73,7 +70,7 @@ void CameraLogic::Update(const ControllerEmu::Cursor::StateData& cursor, constexpr int NUM_POINTS = 4; - std::array v; + std::array v; for (auto& vtx : v) { @@ -95,15 +92,9 @@ void CameraLogic::Update(const ControllerEmu::Cursor::StateData& cursor, v[2].x -= (cursor.z * 0.5 + 1) * dist2; v[3].x += (cursor.z * 0.5 + 1) * dist2; -#define printmatrix(m) \ - PanicAlert("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n", m[0][0], m[0][1], m[0][2], \ - m[0][3], m[1][0], m[1][1], m[1][2], m[1][3], m[2][0], m[2][1], m[2][2], m[2][3], \ - m[3][0], m[3][1], m[3][2], m[3][3]) - Matrix rot, tot; - static Matrix scale; - MatrixScale(scale, 1, camWidth / camHeight, 1); - MatrixRotationByZ(rot, ir_sin, ir_cos); - MatrixMultiply(tot, scale, rot); + const auto scale = Common::Matrix33::Scale({1, camWidth / camHeight, 1}); + const auto rot = Common::Matrix33::RotateZ(std::asin(nsin)); + const auto tot = scale * rot; u16 x[NUM_POINTS], y[NUM_POINTS]; memset(x, 0xFF, sizeof(x)); @@ -111,7 +102,7 @@ void CameraLogic::Update(const ControllerEmu::Cursor::StateData& cursor, for (std::size_t i = 0; i < v.size(); i++) { - MatrixTransformVertex(tot, v[i]); + v[i] = tot * v[i]; if ((v[i].x < -1) || (v[i].x > 1) || (v[i].y < -1) || (v[i].y > 1)) continue; diff --git a/Source/Core/Core/HW/WiimoteEmu/MatrixMath.h b/Source/Core/Core/HW/WiimoteEmu/MatrixMath.h deleted file mode 100644 index c3fa0d498d..0000000000 --- a/Source/Core/Core/HW/WiimoteEmu/MatrixMath.h +++ /dev/null @@ -1,141 +0,0 @@ -// Copyright 2010 Dolphin Emulator Project -// Licensed under GPLv2+ -// Refer to the license.txt file included. - -#pragma once - -#include - -// TODO: kill this whole file, we have Matrix functions in MathUtil.h - -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - -typedef double Matrix[4][4]; -struct Vertex -{ - double x, y, z; -}; - -inline void MatrixIdentity(Matrix& m) -{ - m[0][0] = 1; - m[0][1] = 0; - m[0][2] = 0; - m[0][3] = 0; - m[1][0] = 0; - m[1][1] = 1; - m[1][2] = 0; - m[1][3] = 0; - m[2][0] = 0; - m[2][1] = 0; - m[2][2] = 1; - m[2][3] = 0; - m[3][0] = 0; - m[3][1] = 0; - m[3][2] = 0; - m[3][3] = 1; -} - -inline void MatrixFrustum(Matrix& m, double l, double r, double b, double t, double n, double f) -{ - m[0][0] = 2 * n / (r - l); - m[0][1] = 0; - m[0][2] = 0; - m[0][3] = 0; - m[1][0] = 0; - m[1][1] = 2 * n / (t - b); - m[1][2] = 0; - m[1][3] = 0; - m[2][0] = (r + l) / (r - l); - m[2][1] = (t + b) / (t - b); - m[2][2] = (f + n) / (f - n); - m[2][3] = -1; - m[3][0] = 0; - m[3][1] = 0; - m[3][2] = 2 * f * n / (f - n); - m[3][3] = 0; -} - -inline void MatrixPerspective(Matrix& m, double fovy, double aspect, double nplane, double fplane) -{ - double xmin, xmax, ymin, ymax; - - ymax = nplane * tan(fovy * M_PI / 360.0); - ymin = -ymax; - xmin = ymin * aspect; - xmax = ymax * aspect; - - MatrixFrustum(m, xmin, xmax, ymin, ymax, nplane, fplane); -} - -inline void MatrixRotationByZ(Matrix& m, double sin, double cos) -{ - m[0][0] = cos; - m[0][1] = -sin; - m[0][2] = 0; - m[0][3] = 0; - m[1][0] = sin; - m[1][1] = cos; - m[1][2] = 0; - m[1][3] = 0; - m[2][0] = 0; - m[2][1] = 0; - m[2][2] = 1; - m[2][3] = 0; - m[3][0] = 0; - m[3][1] = 0; - m[3][2] = 0; - m[3][3] = 1; -} - -inline void MatrixScale(Matrix& m, double xfact, double yfact, double zfact) -{ - m[0][0] = xfact; - m[0][1] = 0; - m[0][2] = 0; - m[0][3] = 0; - m[1][0] = 0; - m[1][1] = yfact; - m[1][2] = 0; - m[1][3] = 0; - m[2][0] = 0; - m[2][1] = 0; - m[2][2] = zfact; - m[2][3] = 0; - m[3][0] = 0; - m[3][1] = 0; - m[3][2] = 0; - m[3][3] = 1; -} - -inline void MatrixMultiply(Matrix& r, const Matrix& a, const Matrix& b) -{ - for (int i = 0; i < 16; i++) - r[i >> 2][i & 3] = 0.0f; - - for (int i = 0; i < 4; i++) - for (int j = 0; j < 4; j++) - for (int k = 0; k < 4; k++) - r[i][j] += a[i][k] * b[k][j]; -} - -inline void MatrixTransformVertex(Matrix const& m, Vertex& v) -{ - Vertex ov; - double w; - ov.x = v.x; - ov.y = v.y; - ov.z = v.z; - v.x = m[0][0] * ov.x + m[0][1] * ov.y + m[0][2] * ov.z + m[0][3]; - v.y = m[1][0] * ov.x + m[1][1] * ov.y + m[1][2] * ov.z + m[1][3]; - v.z = m[2][0] * ov.x + m[2][1] * ov.y + m[2][2] * ov.z + m[2][3]; - w = m[3][0] * ov.x + m[3][1] * ov.y + m[3][2] * ov.z + m[3][3]; - if (w != 0) - { - v.x /= w; - v.y /= w; - v.z /= w; - } -} diff --git a/Source/Core/VideoBackends/Vulkan/VulkanContext.cpp b/Source/Core/VideoBackends/Vulkan/VulkanContext.cpp index a1f093ccf1..e38c47eefc 100644 --- a/Source/Core/VideoBackends/Vulkan/VulkanContext.cpp +++ b/Source/Core/VideoBackends/Vulkan/VulkanContext.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include +#include #include #include "Common/Assert.h" @@ -847,4 +848,4 @@ void VulkanContext::InitDriverDetails() static_cast(m_device_properties.driverVersion), DriverDetails::Family::UNKNOWN); } -} +} // namespace Vulkan diff --git a/Source/Core/VideoCommon/VertexShaderManager.cpp b/Source/Core/VideoCommon/VertexShaderManager.cpp index b68534aa8e..b0821df950 100644 --- a/Source/Core/VideoCommon/VertexShaderManager.cpp +++ b/Source/Core/VideoCommon/VertexShaderManager.cpp @@ -27,7 +27,7 @@ #include "VideoCommon/VideoConfig.h" #include "VideoCommon/XFMemory.h" -alignas(16) static float g_fProjectionMatrix[16]; +alignas(16) static std::array g_fProjectionMatrix; // track changes static bool bTexMatricesChanged[2], bPosNormalMatrixChanged, bProjectionChanged, bViewportChanged; @@ -38,10 +38,10 @@ static int nNormalMatricesChanged[2]; // min,max static int nPostTransformMatricesChanged[2]; // min,max static int nLightsChanged[2]; // min,max -static Matrix44 s_viewportCorrection; -static Matrix33 s_viewRotationMatrix; -static Matrix33 s_viewInvRotationMatrix; -static float s_fViewTranslationVector[3]; +static Common::Matrix44 s_viewportCorrection; +static Common::Matrix33 s_viewRotationMatrix; +static Common::Matrix33 s_viewInvRotationMatrix; +static Common::Vec3 s_fViewTranslationVector; static float s_fViewRotation[2]; VertexShaderConstants VertexShaderManager::constants; @@ -57,7 +57,7 @@ bool VertexShaderManager::dirty; // [ 0 (ih/ah) 0 ((-ih + 2*(ay-iy)) / ah + 1) ] // [ 0 0 1 0 ] // [ 0 0 0 1 ] -static void ViewportCorrectionMatrix(Matrix44& result) +static void ViewportCorrectionMatrix(Common::Matrix44& result) { int scissorXOff = bpmem.scissorOffset.x * 2; int scissorYOff = bpmem.scissorOffset.y * 2; @@ -86,7 +86,7 @@ static void ViewportCorrectionMatrix(Matrix44& result) float Wd = (X + intendedWd <= EFB_WIDTH) ? intendedWd : (EFB_WIDTH - X); float Ht = (Y + intendedHt <= EFB_HEIGHT) ? intendedHt : (EFB_HEIGHT - Y); - result = Matrix44::Identity(); + result = Common::Matrix44::Identity(); if (Wd == 0 || Ht == 0) return; @@ -121,10 +121,8 @@ void VertexShaderManager::Init() ResetView(); // TODO: should these go inside ResetView()? - s_viewportCorrection = Matrix44::Identity(); - memset(g_fProjectionMatrix, 0, sizeof(g_fProjectionMatrix)); - for (int i = 0; i < 4; ++i) - g_fProjectionMatrix[i * 5] = 1.0f; + s_viewportCorrection = Common::Matrix44::Identity(); + g_fProjectionMatrix = Common::Matrix44::Identity().data; dirty = true; } @@ -454,6 +452,8 @@ void VertexShaderManager::SetConstants() if (g_ActiveConfig.bFreeLook && xfmem.projection.type == GX_PERSPECTIVE) { + using Common::Matrix44; + auto mtxA = Matrix44::Translate(s_fViewTranslationVector); auto mtxB = Matrix44::FromMatrix33(s_viewRotationMatrix); const auto viewMtx = mtxB * mtxA; // view = rotation x translation @@ -464,7 +464,7 @@ void VertexShaderManager::SetConstants() } else { - const auto projMtx = Matrix44::FromArray(g_fProjectionMatrix); + const auto projMtx = Common::Matrix44::FromArray(g_fProjectionMatrix); const auto correctedMtx = s_viewportCorrection * projMtx; memcpy(constants.projection.data(), correctedMtx.data.data(), 4 * sizeof(float4)); } @@ -654,19 +654,15 @@ void VertexShaderManager::SetMaterialColorChanged(int index) void VertexShaderManager::TranslateView(float x, float y, float z) { - float result[3]; - float vector[3] = {x, z, y}; - - Matrix33::Multiply(s_viewInvRotationMatrix, vector, result); - - for (size_t i = 0; i < ArraySize(result); i++) - s_fViewTranslationVector[i] += result[i]; + s_fViewTranslationVector += s_viewInvRotationMatrix * Common::Vec3{x, z, y}; bProjectionChanged = true; } void VertexShaderManager::RotateView(float x, float y) { + using Common::Matrix33; + s_fViewRotation[0] += x; s_fViewRotation[1] += y; @@ -682,7 +678,9 @@ void VertexShaderManager::RotateView(float x, float y) void VertexShaderManager::ResetView() { - memset(s_fViewTranslationVector, 0, sizeof(s_fViewTranslationVector)); + using Common::Matrix33; + + s_fViewTranslationVector = {}; s_viewRotationMatrix = Matrix33::Identity(); s_viewInvRotationMatrix = Matrix33::Identity(); s_fViewRotation[0] = s_fViewRotation[1] = 0.0f;