From dbe3f938c7be920ff40761bb03ff2f385f0327d2 Mon Sep 17 00:00:00 2001 From: Thog Date: Fri, 14 Feb 2020 11:52:34 +0100 Subject: [PATCH] Remove ScopedGlContext (#933) As MakeCurrent can be slow to execute, this PR changes GLRenderer to not have to reexecute MakeCurrent when processing frames. --- Ryujinx/Ui/GLRenderer.cs | 75 ++++++++++++++++------------------- Ryujinx/Ui/ScopedGlContext.cs | 35 ---------------- 2 files changed, 34 insertions(+), 76 deletions(-) delete mode 100644 Ryujinx/Ui/ScopedGlContext.cs diff --git a/Ryujinx/Ui/GLRenderer.cs b/Ryujinx/Ui/GLRenderer.cs index 737a1fdd..6feb3ed4 100644 --- a/Ryujinx/Ui/GLRenderer.cs +++ b/Ryujinx/Ui/GLRenderer.cs @@ -84,7 +84,7 @@ namespace Ryujinx.Ui private void GLRenderer_ShuttingDown(object sender, EventArgs args) { - Exit(); + _device.DisposeGpu(); } private void Parent_FocusOutEvent(object o, Gtk.FocusOutEventArgs args) @@ -146,7 +146,7 @@ namespace Ryujinx.Ui private void GLRenderer_Initialized(object sender, EventArgs e) { - // Release the GL exclusivity that OpenTK gave us. + // Release the GL exclusivity that OpenTK gave us as we aren't going to use it in GTK Thread. GraphicsContext.MakeCurrent(null); WaitEvent.Set(); @@ -234,14 +234,7 @@ namespace Ryujinx.Ui } IsStopped = true; - IsActive = false; - - using (ScopedGlContext scopedGLContext = new ScopedGlContext(WindowInfo, GraphicsContext)) - { - _device.DisposeGpu(); - } - - WaitEvent.Set(); + IsActive = false; } public void Initialize() @@ -256,12 +249,15 @@ namespace Ryujinx.Ui public void Render() { - using (ScopedGlContext scopedGLContext = new ScopedGlContext(WindowInfo, GraphicsContext)) - { - _renderer.Initialize(); + // First take exclusivity on the OpenGL context. + GraphicsContext.MakeCurrent(WindowInfo); - SwapBuffers(); - } + _renderer.Initialize(); + + // Make sure the first frame is not transparent. + GL.ClearColor(OpenTK.Color.Black); + GL.Clear(ClearBufferMask.ColorBufferBit); + SwapBuffers(); while (IsActive) { @@ -270,43 +266,40 @@ namespace Ryujinx.Ui return; } - using (ScopedGlContext scopedGLContext = new ScopedGlContext(WindowInfo, GraphicsContext)) + _ticks += _chrono.ElapsedTicks; + + _chrono.Restart(); + + if (_device.WaitFifo()) { - _ticks += _chrono.ElapsedTicks; + _device.ProcessFrame(); + } - _chrono.Restart(); + if (_ticks >= _ticksPerFrame) + { + _device.PresentFrame(SwapBuffers); - if (_device.WaitFifo()) - { - _device.ProcessFrame(); - } + _device.Statistics.RecordSystemFrameTime(); - if (_ticks >= _ticksPerFrame) - { - _device.PresentFrame(SwapBuffers); + double hostFps = _device.Statistics.GetSystemFrameRate(); + double gameFps = _device.Statistics.GetGameFrameRate(); - _device.Statistics.RecordSystemFrameTime(); + string titleNameSection = string.IsNullOrWhiteSpace(_device.System.TitleName) ? string.Empty + : " | " + _device.System.TitleName; - double hostFps = _device.Statistics.GetSystemFrameRate(); - double gameFps = _device.Statistics.GetGameFrameRate(); + string titleIdSection = string.IsNullOrWhiteSpace(_device.System.TitleIdText) ? string.Empty + : " | " + _device.System.TitleIdText.ToUpper(); - string titleNameSection = string.IsNullOrWhiteSpace(_device.System.TitleName) ? string.Empty - : " | " + _device.System.TitleName; + _newTitle = $"Ryujinx {Program.Version}{titleNameSection}{titleIdSection} | Host FPS: {hostFps:0.0} | Game FPS: {gameFps:0.0} | " + + $"Game Vsync: {(_device.EnableDeviceVsync ? "On" : "Off")}"; - string titleIdSection = string.IsNullOrWhiteSpace(_device.System.TitleIdText) ? string.Empty - : " | " + _device.System.TitleIdText.ToUpper(); + _titleEvent = true; - _newTitle = $"Ryujinx {Program.Version}{titleNameSection}{titleIdSection} | Host FPS: {hostFps:0.0} | Game FPS: {gameFps:0.0} | " + - $"Game Vsync: {(_device.EnableDeviceVsync ? "On" : "Off")}"; + _device.System.SignalVsync(); - _titleEvent = true; + _device.VsyncEvent.Set(); - _device.System.SignalVsync(); - - _device.VsyncEvent.Set(); - - _ticks = Math.Min(_ticks - _ticksPerFrame, _ticksPerFrame); - } + _ticks = Math.Min(_ticks - _ticksPerFrame, _ticksPerFrame); } } } diff --git a/Ryujinx/Ui/ScopedGlContext.cs b/Ryujinx/Ui/ScopedGlContext.cs deleted file mode 100644 index 8dc9246e..00000000 --- a/Ryujinx/Ui/ScopedGlContext.cs +++ /dev/null @@ -1,35 +0,0 @@ -using OpenTK.Graphics; -using OpenTK.Platform; -using System; -using System.Threading; - -namespace Ryujinx.Ui -{ - class ScopedGlContext : IDisposable - { - private IGraphicsContext _graphicsContext; - - private static readonly object _lock = new object(); - - public ScopedGlContext(IWindowInfo windowInfo, IGraphicsContext graphicsContext) - { - _graphicsContext = graphicsContext; - - Monitor.Enter(_lock); - - MakeCurrent(windowInfo); - } - - private void MakeCurrent(IWindowInfo windowInfo) - { - _graphicsContext.MakeCurrent(windowInfo); - } - - public void Dispose() - { - MakeCurrent(null); - - Monitor.Exit(_lock); - } - } -}