From 337b054042ef1cc3afa0156205730eb6f0f05d55 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Sat, 13 Feb 2021 23:54:47 +1000 Subject: [PATCH] "Inline" some vector and matrix operations --- .../GDIPlusGuiRenderer.cs | 2 +- src/BizHawk.Bizware.BizwareGL/RetroShader.cs | 3 +- src/BizHawk.Bizware.DirectX/IGL_SlimDX9.cs | 10 ++-- .../SlimDXExtensions.cs | 50 ++++++------------- .../DisplayManager/Filters/Gui.cs | 7 +-- .../DisplayManager/DisplayManager.cs | 4 +- .../GraphicsImplementations/IGL_TK.cs | 14 +++--- 7 files changed, 31 insertions(+), 59 deletions(-) diff --git a/src/BizHawk.Bizware.BizwareGL/GDIPlusGuiRenderer.cs b/src/BizHawk.Bizware.BizwareGL/GDIPlusGuiRenderer.cs index 2241985478..defe3d9b1e 100644 --- a/src/BizHawk.Bizware.BizwareGL/GDIPlusGuiRenderer.cs +++ b/src/BizHawk.Bizware.BizwareGL/GDIPlusGuiRenderer.cs @@ -233,7 +233,7 @@ namespace BizHawk.Bizware.BizwareGL //projection is always identity, so who cares i guess //Matrix4 mat = Projection.Top * Modelview.Top; Matrix4 mat = Modelview.Top; - g.Transform = new sd.Drawing2D.Matrix(mat.M11, mat.M12, mat.M21, mat.M22, mat.M41, mat.M42); + g.Transform = new sd.Drawing2D.Matrix(mat.Row0.X, mat.Row0.Y, mat.Row1.X, mat.Row1.Y, mat.Row3.X, mat.Row3.Y); } private unsafe void DrawInternal(Art art, float x, float y, float w, float h) diff --git a/src/BizHawk.Bizware.BizwareGL/RetroShader.cs b/src/BizHawk.Bizware.BizwareGL/RetroShader.cs index ae905ad129..a9da440d05 100644 --- a/src/BizHawk.Bizware.BizwareGL/RetroShader.cs +++ b/src/BizHawk.Bizware.BizwareGL/RetroShader.cs @@ -92,8 +92,7 @@ namespace BizHawk.Bizware.BizwareGL var Projection = Owner.CreateGuiProjectionMatrix(OutputSize); var Modelview = Owner.CreateGuiViewMatrix(OutputSize); - var mat = Modelview * Projection; - mat.Transpose(); + var mat = Matrix4.Transpose(Modelview * Projection); Pipeline["modelViewProj"].Set(mat, true); Owner.SetTextureWrapMode(tex, true); diff --git a/src/BizHawk.Bizware.DirectX/IGL_SlimDX9.cs b/src/BizHawk.Bizware.DirectX/IGL_SlimDX9.cs index 5077b0ff3e..8979ec2e23 100644 --- a/src/BizHawk.Bizware.DirectX/IGL_SlimDX9.cs +++ b/src/BizHawk.Bizware.DirectX/IGL_SlimDX9.cs @@ -754,17 +754,17 @@ namespace BizHawk.Bizware.DirectX public Matrix4 CreateGuiProjectionMatrix(Size dims) { Matrix4 ret = Matrix4.Identity; - ret.M11 = 2.0f / (float)dims.Width; - ret.M22 = 2.0f / (float)dims.Height; + ret.Row0.X = 2.0f / (float)dims.Width; + ret.Row1.Y = 2.0f / (float)dims.Height; return ret; } public Matrix4 CreateGuiViewMatrix(Size dims, bool autoFlip) { Matrix4 ret = Matrix4.Identity; - ret.M22 = -1.0f; - ret.M41 = -(float)dims.Width * 0.5f - 0.5f; - ret.M42 = (float)dims.Height * 0.5f + 0.5f; + ret.Row1.Y = -1.0f; + ret.Row3.X = -(float)dims.Width * 0.5f - 0.5f; + ret.Row3.Y = (float)dims.Height * 0.5f + 0.5f; // auto-flipping isn't needed on d3d return ret; diff --git a/src/BizHawk.Bizware.DirectX/SlimDXExtensions.cs b/src/BizHawk.Bizware.DirectX/SlimDXExtensions.cs index da2f90212a..fac57625f8 100644 --- a/src/BizHawk.Bizware.DirectX/SlimDXExtensions.cs +++ b/src/BizHawk.Bizware.DirectX/SlimDXExtensions.cs @@ -1,46 +1,24 @@ -namespace BizHawk.Bizware.DirectX +using SlimDX; + +namespace BizHawk.Bizware.DirectX { internal static class Extensions { - public static global::SlimDX.Matrix ToSlimDXMatrix(this OpenTK.Matrix4 m, bool transpose) + public static Matrix ToSlimDXMatrix(this OpenTK.Matrix4 m, bool transpose) { - global::SlimDX.Matrix ret = new global::SlimDX.Matrix + Matrix ret = new() { - M11 = m.M11, - M12 = m.M12, - M13 = m.M13, - M14 = m.M14, - M21 = m.M21, - M22 = m.M22, - M23 = m.M23, - M24 = m.M24, - M31 = m.M31, - M32 = m.M32, - M33 = m.M33, - M34 = m.M34, - M41 = m.M41, - M42 = m.M42, - M43 = m.M43, - M44 = m.M44 + M11 = m.Row0.X, M12 = m.Row0.Y, M13 = m.Row0.Z, M14 = m.Row0.W, + M21 = m.Row1.X, M22 = m.Row1.Y, M23 = m.Row1.Z, M24 = m.Row1.W, + M31 = m.Row2.X, M32 = m.Row2.Y, M33 = m.Row2.Z, M34 = m.Row2.W, + M41 = m.Row3.X, M42 = m.Row3.Y, M43 = m.Row3.Z, M44 = m.Row3.W }; - - //could be optimized later into the above copies - if (transpose) - { - ret = global::SlimDX.Matrix.Transpose(ret); - } - - return ret; + // Transpose call could be inlined to reduce 2 sets of copies to 1 + return transpose ? Matrix.Transpose(ret) : ret; } - public static global::SlimDX.Vector4 ToSlimDXVector4(this OpenTK.Vector4 v) - { - return new global::SlimDX.Vector4(v.X, v.Y, v.Z, v.W); - } + public static Vector2 ToSlimDXVector2(this OpenTK.Vector2 v) => new(v.X, v.Y); - public static global::SlimDX.Vector2 ToSlimDXVector2(this OpenTK.Vector2 v) - { - return new global::SlimDX.Vector2(v.X, v.Y); - } + public static Vector4 ToSlimDXVector4(this OpenTK.Vector4 v) => new(v.X, v.Y, v.Z, v.W); } -} \ No newline at end of file +} diff --git a/src/BizHawk.Client.Common/DisplayManager/Filters/Gui.cs b/src/BizHawk.Client.Common/DisplayManager/Filters/Gui.cs index 0231cf7260..7121d52e61 100644 --- a/src/BizHawk.Client.Common/DisplayManager/Filters/Gui.cs +++ b/src/BizHawk.Client.Common/DisplayManager/Filters/Gui.cs @@ -67,9 +67,7 @@ namespace BizHawk.Client.Common.Filters // apply the zooming algorithm (pasted and reworked, for now) // ALERT COPYPASTE LAUNDROMAT - Vector2 VS = new Vector2(virtualWidth, virtualHeight); - Vector2 BS = new Vector2(textureWidth, textureHeight); - Vector2 AR = Vector2.Divide(VS, BS); + Vector2 AR = new(virtualWidth / (float) textureWidth, virtualHeight / (float) textureHeight); float targetPar = (AR.X / AR.Y); Vector2 PS = new Vector2(1, 1); //this would malfunction for AR <= 0.5 or AR >= 2.0 @@ -352,8 +350,7 @@ namespace BizHawk.Client.Common.Filters //in case we're in this layout, we get confused, so fix it var settings = nds.GetSettings(); - if (settings.ScreenLayout == MelonDS.ScreenLayoutKind.Top) - point = Vector2.Zero; + if (settings.ScreenLayout == MelonDS.ScreenLayoutKind.Top) point = new(0.0f, 0.0f); //TODO: we probably need more subtle logic here. //some capability to return -1,-1 perhaps in case the cursor is nowhere. diff --git a/src/BizHawk.Client.EmuHawk/DisplayManager/DisplayManager.cs b/src/BizHawk.Client.EmuHawk/DisplayManager/DisplayManager.cs index f56787b502..fa412c7d48 100644 --- a/src/BizHawk.Client.EmuHawk/DisplayManager/DisplayManager.cs +++ b/src/BizHawk.Client.EmuHawk/DisplayManager/DisplayManager.cs @@ -668,9 +668,7 @@ namespace BizHawk.Client.EmuHawk if (arInteger) { // ALERT COPYPASTE LAUNDROMAT - Vector2 VS = new Vector2(virtualWidth, virtualHeight); - Vector2 BS = new Vector2(bufferWidth, bufferHeight); - Vector2 AR = Vector2.Divide(VS, BS); + Vector2 AR = new(virtualWidth / (float) bufferWidth, virtualHeight / (float) bufferHeight); float targetPar = AR.X / AR.Y; // this would malfunction for AR <= 0.5 or AR >= 2.0 diff --git a/src/BizHawk.Client.EmuHawk/GraphicsImplementations/IGL_TK.cs b/src/BizHawk.Client.EmuHawk/GraphicsImplementations/IGL_TK.cs index 0de7c20f3b..6c75edbb8d 100644 --- a/src/BizHawk.Client.EmuHawk/GraphicsImplementations/IGL_TK.cs +++ b/src/BizHawk.Client.EmuHawk/GraphicsImplementations/IGL_TK.cs @@ -604,25 +604,25 @@ namespace BizHawk.Client.EmuHawk public Matrix4 CreateGuiProjectionMatrix(sd.Size dims) { Matrix4 ret = Matrix4.Identity; - ret.M11 = 2.0f / (float)dims.Width; - ret.M22 = 2.0f / (float)dims.Height; + ret.Row0.X = 2.0f / (float)dims.Width; + ret.Row1.Y = 2.0f / (float)dims.Height; return ret; } public Matrix4 CreateGuiViewMatrix(sd.Size dims, bool autoflip) { Matrix4 ret = Matrix4.Identity; - ret.M22 = -1.0f; - ret.M41 = -(float)dims.Width * 0.5f; - ret.M42 = (float)dims.Height * 0.5f; + ret.Row1.Y = -1.0f; + ret.Row3.X = -(float)dims.Width * 0.5f; + ret.Row3.Y = (float)dims.Height * 0.5f; if (autoflip) { if (_currRenderTarget == null) { } else { //flip as long as we're not a final render target - ret.M22 = 1.0f; - ret.M42 *= -1; + ret.Row1.Y = 1.0f; + ret.Row3.Y *= -1; } } return ret;