From b9a00a40a9a61c3df5c774fd1cc9d04f57994a0c Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Tue, 29 Jan 2019 16:05:51 -0600 Subject: Common: Move Matrix classes out of MathUtil into their own files and make their interface more friendly. --- Source/Core/VideoCommon/VertexShaderManager.cpp | 49 ++++++++++--------------- 1 file changed, 19 insertions(+), 30 deletions(-) (limited to 'Source/Core/VideoCommon/VertexShaderManager.cpp') 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; - - 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)); + auto mtxA = Matrix44::Translate(s_fViewTranslationVector); + auto mtxB = Matrix44::FromMatrix33(s_viewRotationMatrix); + const auto viewMtx = mtxB * mtxA; // view = rotation x translation + + 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; -- cgit v1.2.3 From 69b617ce766e5b1b8956a29f7a8caeed21f6024d Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Tue, 29 Jan 2019 17:12:06 -0600 Subject: WiimoteEmu: Remove redundant Matrix library and use the one in Common. --- Source/Core/VideoCommon/VertexShaderManager.cpp | 38 ++++++++++++------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'Source/Core/VideoCommon/VertexShaderManager.cpp') 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; -- cgit v1.2.3