Some bug fixes

Changelog:

- Reapply some changes that got lost while rebasing from #904
- Make sure to guarantee exclusivity on the GL context (fixing multiple
possible race conditions on Windows)
- Avoid making GLRenderer disposed multiple time
This commit is contained in:
Thog 2020-02-06 14:36:12 +01:00
commit d54080c64e
3 changed files with 87 additions and 38 deletions

View file

@ -3,6 +3,7 @@ using OpenTK;
using OpenTK.Graphics; using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL; using OpenTK.Graphics.OpenGL;
using OpenTK.Input; using OpenTK.Input;
using OpenTK.Platform;
using Ryujinx.Configuration; using Ryujinx.Configuration;
using Ryujinx.Graphics.OpenGL; using Ryujinx.Graphics.OpenGL;
using Ryujinx.HLE; using Ryujinx.HLE;
@ -88,7 +89,9 @@ namespace Ryujinx.Ui
private void GLRenderer_Initialized(object sender, EventArgs e) private void GLRenderer_Initialized(object sender, EventArgs e)
{ {
// Release the GL exclusivity that OpenTK gave us.
GraphicsContext.MakeCurrent(null); GraphicsContext.MakeCurrent(null);
WaitEvent.Set(); WaitEvent.Set();
} }
@ -168,11 +171,19 @@ namespace Ryujinx.Ui
public void Exit() public void Exit()
{ {
_device.DisposeGpu(); if (IsStopped)
{
return;
}
IsStopped = true; IsStopped = true;
IsActive = false; IsActive = false;
using (ScopedGLContext scopedGLContext = new ScopedGLContext(WindowInfo, GraphicsContext))
{
_device.DisposeGpu();
}
WaitEvent.Set(); WaitEvent.Set();
} }
@ -188,8 +199,10 @@ namespace Ryujinx.Ui
public void Render() public void Render()
{ {
GraphicsContext.MakeCurrent(WindowInfo); using (ScopedGLContext scopedGLContext = new ScopedGLContext(WindowInfo, GraphicsContext))
_renderer.Initialize(); {
_renderer.Initialize();
}
while (IsActive) while (IsActive)
{ {
@ -198,44 +211,45 @@ namespace Ryujinx.Ui
return; return;
} }
GraphicsContext.MakeCurrent(WindowInfo); using (ScopedGLContext scopedGLContext = new ScopedGLContext(WindowInfo, GraphicsContext))
GL.ClearColor(Color4.Black);
_ticks += _chrono.ElapsedTicks;
_chrono.Restart();
if (_device.WaitFifo())
{ {
_device.ProcessFrame(); GL.ClearColor(Color4.Black);
}
if (_ticks >= _ticksPerFrame) _ticks += _chrono.ElapsedTicks;
{
_device.PresentFrame(SwapBuffers);
_device.Statistics.RecordSystemFrameTime(); _chrono.Restart();
double hostFps = _device.Statistics.GetSystemFrameRate(); if (_device.WaitFifo())
double gameFps = _device.Statistics.GetGameFrameRate(); {
_device.ProcessFrame();
}
string titleNameSection = string.IsNullOrWhiteSpace(_device.System.TitleName) ? string.Empty if (_ticks >= _ticksPerFrame)
: " | " + _device.System.TitleName; {
_device.PresentFrame(SwapBuffers);
string titleIdSection = string.IsNullOrWhiteSpace(_device.System.TitleIdText) ? string.Empty _device.Statistics.RecordSystemFrameTime();
: " | " + _device.System.TitleIdText.ToUpper();
_newTitle = $"Ryujinx{titleNameSection}{titleIdSection} | Host FPS: {hostFps:0.0} | Game FPS: {gameFps:0.0} | " + double hostFps = _device.Statistics.GetSystemFrameRate();
$"Game Vsync: {(_device.EnableDeviceVsync ? "On" : "Off")}"; double gameFps = _device.Statistics.GetGameFrameRate();
_titleEvent = true; string titleNameSection = string.IsNullOrWhiteSpace(_device.System.TitleName) ? string.Empty
: " | " + _device.System.TitleName;
_device.System.SignalVsync(); string titleIdSection = string.IsNullOrWhiteSpace(_device.System.TitleIdText) ? string.Empty
: " | " + _device.System.TitleIdText.ToUpper();
_device.VsyncEvent.Set(); _newTitle = $"Ryujinx{titleNameSection}{titleIdSection} | Host FPS: {hostFps:0.0} | Game FPS: {gameFps:0.0} | " +
$"Game Vsync: {(_device.EnableDeviceVsync ? "On" : "Off")}";
_ticks = Math.Min(_ticks - _ticksPerFrame, _ticksPerFrame); _titleEvent = true;
_device.System.SignalVsync();
_device.VsyncEvent.Set();
_ticks = Math.Min(_ticks - _ticksPerFrame, _ticksPerFrame);
}
} }
} }
} }
@ -245,7 +259,6 @@ namespace Ryujinx.Ui
OpenTK.Graphics.GraphicsContext.CurrentContext.SwapBuffers(); OpenTK.Graphics.GraphicsContext.CurrentContext.SwapBuffers();
} }
public void MainLoop() public void MainLoop()
{ {
while (IsActive) while (IsActive)

View file

@ -433,14 +433,15 @@ namespace Ryujinx.Ui
DiscordIntegrationModule.SwitchToMainMenu(); DiscordIntegrationModule.SwitchToMainMenu();
_screenExitStatus.Set();
Application.Invoke(delegate Application.Invoke(delegate
{ {
_stopEmulation.Sensitive = false; _stopEmulation.Sensitive = false;
_firmwareInstallFile.Sensitive = true; _firmwareInstallFile.Sensitive = true;
_firmwareInstallDirectory.Sensitive = true; _firmwareInstallDirectory.Sensitive = true;
}); });
_screenExitStatus.Set();
} }
private static void UpdateGameMetadata(string titleId) private static void UpdateGameMetadata(string titleId)
@ -475,7 +476,11 @@ namespace Ryujinx.Ui
{ {
UpdateGameMetadata(device.System.TitleIdText); UpdateGameMetadata(device.System.TitleIdText);
_gLWigdet?.Exit(); if (_gLWigdet != null)
{
_gLWigdet.Exit();
_screenExitStatus.WaitOne();
}
} }
Dispose(); Dispose();
@ -627,15 +632,11 @@ namespace Ryujinx.Ui
private void Exit_Pressed(object sender, EventArgs args) private void Exit_Pressed(object sender, EventArgs args)
{ {
_gLWigdet?.Exit();
End(_emulationContext); End(_emulationContext);
} }
private void Window_Close(object sender, DeleteEventArgs args) private void Window_Close(object sender, DeleteEventArgs args)
{ {
_gLWigdet?.Exit();
End(_emulationContext); End(_emulationContext);
} }

View file

@ -0,0 +1,35 @@
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);
}
}
}