Add a Multithreading layer for the GAL, multi-thread shader compilation at runtime (#2501)
* Initial Implementation
About as fast as nvidia GL multithreading, can be improved with faster command queuing.
* Struct based command list
Speeds up a bit. Still a lot of time lost to resource copy.
* Do shader init while the render thread is active.
* Introduce circular span pool V1
Ideally should be able to use structs instead of references for storing these spans on commands. Will try that next.
* Refactor SpanRef some more
Use a struct to represent SpanRef, rather than a reference.
* Flush buffers on background thread
* Use a span for UpdateRenderScale.
Much faster than copying the array.
* Calculate command size using reflection
* WIP parallel shaders
* Some minor optimisation
* Only 2 max refs per command now.
The command with 3 refs is gone. :relieved:
* Don't cast on the GPU side
* Remove redundant casts, force sync on window present
* Fix Shader Cache
* Fix host shader save.
* Fixup to work with new renderer stuff
* Make command Run static, use array of delegates as lookup
Profile says this takes less time than the previous way.
* Bring up to date
* Add settings toggle. Fix Muiltithreading Off mode.
* Fix warning.
* Release tracking lock for flushes
* Fix Conditional Render fast path with threaded gal
* Make handle iteration safe when releasing the lock
This is mostly temporary.
* Attempt to set backend threading on driver
Only really works on nvidia before launching a game.
* Fix race condition with BufferModifiedRangeList, exceptions in tracking actions
* Update buffer set commands
* Some cleanup
* Only use stutter workaround when using opengl renderer non-threaded
* Add host-conditional reservation of counter events
There has always been the possibility that conditional rendering could use a query object just as it is disposed by the counter queue. This change makes it so that when the host decides to use host conditional rendering, the query object is reserved so that it cannot be deleted. Counter events can optionally start reserved, as the threaded implementation can reserve them before the backend creates them, and there would otherwise be a short amount of time where the counter queue could dispose the event before a call to reserve it could be made.
* Address Feedback
* Make counter flush tracked again.
Hopefully does not cause any issues this time.
* Wait for FlushTo on the main queue thread.
Currently assumes only one thread will want to FlushTo (in this case, the GPU thread)
* Add SDL2 headless integration
* Add HLE macro commands.
Co-authored-by: Mary <mary@mary.zone>
2021-08-27 00:31:29 +02:00
|
|
|
|
using Gdk;
|
2021-05-04 18:19:04 +02:00
|
|
|
|
using Gtk;
|
2020-02-12 01:56:19 +01:00
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
2020-08-02 16:41:24 +02:00
|
|
|
|
using Ryujinx.Common.Configuration;
|
2020-02-12 01:56:19 +01:00
|
|
|
|
using Ryujinx.Graphics.OpenGL;
|
2021-04-14 12:28:43 +02:00
|
|
|
|
using Ryujinx.Input.HLE;
|
|
|
|
|
using SPB.Graphics;
|
|
|
|
|
using SPB.Graphics.OpenGL;
|
2021-05-04 18:19:04 +02:00
|
|
|
|
using SPB.Platform;
|
|
|
|
|
using SPB.Platform.GLX;
|
|
|
|
|
using SPB.Platform.WGL;
|
|
|
|
|
using SPB.Windowing;
|
2020-02-12 01:56:19 +01:00
|
|
|
|
using System;
|
2021-05-04 18:19:04 +02:00
|
|
|
|
using System.Runtime.InteropServices;
|
2021-04-14 12:28:43 +02:00
|
|
|
|
|
2020-02-12 01:56:19 +01:00
|
|
|
|
namespace Ryujinx.Ui
|
|
|
|
|
{
|
2021-05-04 18:19:04 +02:00
|
|
|
|
public class GlRenderer : RendererWidgetBase
|
2020-02-12 01:56:19 +01:00
|
|
|
|
{
|
2020-08-02 16:41:24 +02:00
|
|
|
|
private GraphicsDebugLevel _glLogLevel;
|
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
private bool _initializedOpenGL;
|
2020-12-02 00:23:43 +01:00
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
private OpenGLContextBase _openGLContext;
|
|
|
|
|
private SwappableNativeWindowBase _nativeWindow;
|
2020-08-02 16:41:24 +02:00
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
public GlRenderer(InputManager inputManager, GraphicsDebugLevel glLogLevel) : base(inputManager, glLogLevel)
|
|
|
|
|
{
|
2020-08-02 16:41:24 +02:00
|
|
|
|
_glLogLevel = glLogLevel;
|
2021-03-02 23:45:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
protected override bool OnDrawn(Cairo.Context cr)
|
2021-03-02 23:45:33 +01:00
|
|
|
|
{
|
2021-05-04 18:19:04 +02:00
|
|
|
|
if (!_initializedOpenGL)
|
2021-03-02 23:45:33 +01:00
|
|
|
|
{
|
2021-05-04 18:19:04 +02:00
|
|
|
|
IntializeOpenGL();
|
|
|
|
|
}
|
2020-02-12 01:56:19 +01:00
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
return true;
|
2020-02-12 01:56:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
private void IntializeOpenGL()
|
2020-02-12 01:56:19 +01:00
|
|
|
|
{
|
2021-05-04 18:19:04 +02:00
|
|
|
|
_nativeWindow = RetrieveNativeWindow();
|
2020-02-13 18:43:29 +01:00
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
Window.EnsureNative();
|
2020-11-19 00:15:44 +01:00
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
_openGLContext = PlatformHelper.CreateOpenGLContext(GetGraphicsMode(), 3, 3, _glLogLevel == GraphicsDebugLevel.None ? OpenGLContextFlags.Compat : OpenGLContextFlags.Compat | OpenGLContextFlags.Debug);
|
|
|
|
|
_openGLContext.Initialize(_nativeWindow);
|
|
|
|
|
_openGLContext.MakeCurrent(_nativeWindow);
|
2020-02-12 01:56:19 +01:00
|
|
|
|
|
2021-04-14 12:28:43 +02:00
|
|
|
|
// Release the GL exclusivity that SPB gave us as we aren't going to use it in GTK Thread.
|
2021-05-04 18:19:04 +02:00
|
|
|
|
_openGLContext.MakeCurrent(null);
|
2020-02-12 01:56:19 +01:00
|
|
|
|
|
|
|
|
|
WaitEvent.Set();
|
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
_initializedOpenGL = true;
|
2020-02-12 01:56:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
private SwappableNativeWindowBase RetrieveNativeWindow()
|
2020-12-09 23:36:08 +01:00
|
|
|
|
{
|
2021-12-05 00:02:30 +01:00
|
|
|
|
if (OperatingSystem.IsWindows())
|
2020-12-09 23:36:08 +01:00
|
|
|
|
{
|
2021-05-04 18:19:04 +02:00
|
|
|
|
IntPtr windowHandle = gdk_win32_window_get_handle(Window.Handle);
|
2020-12-09 23:36:08 +01:00
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
return new WGLWindow(new NativeHandle(windowHandle));
|
2020-12-09 23:36:08 +01:00
|
|
|
|
}
|
2021-12-05 00:02:30 +01:00
|
|
|
|
else if (OperatingSystem.IsLinux())
|
2020-02-12 01:56:19 +01:00
|
|
|
|
{
|
2021-05-04 18:19:04 +02:00
|
|
|
|
IntPtr displayHandle = gdk_x11_display_get_xdisplay(Display.Handle);
|
|
|
|
|
IntPtr windowHandle = gdk_x11_window_get_xid(Window.Handle);
|
2020-02-12 01:56:19 +01:00
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
return new GLXWindow(new NativeHandle(displayHandle), new NativeHandle(windowHandle));
|
2020-02-18 12:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
throw new NotImplementedException();
|
2020-02-18 12:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
[DllImport("libgdk-3-0.dll")]
|
|
|
|
|
private static extern IntPtr gdk_win32_window_get_handle(IntPtr d);
|
2020-02-12 01:56:19 +01:00
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
[DllImport("libgdk-3.so.0")]
|
|
|
|
|
private static extern IntPtr gdk_x11_display_get_xdisplay(IntPtr gdkDisplay);
|
2020-12-02 00:23:43 +01:00
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
[DllImport("libgdk-3.so.0")]
|
|
|
|
|
private static extern IntPtr gdk_x11_window_get_xid(IntPtr gdkWindow);
|
2020-02-12 01:56:19 +01:00
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
private static FramebufferFormat GetGraphicsMode()
|
2020-02-12 01:56:19 +01:00
|
|
|
|
{
|
2021-05-04 18:19:04 +02:00
|
|
|
|
return Environment.OSVersion.Platform == PlatformID.Unix ? new FramebufferFormat(new ColorFormat(8, 8, 8, 0), 16, 0, ColorFormat.Zero, 0, 2, false) : FramebufferFormat.Default;
|
2020-02-12 01:56:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
public override void InitializeRenderer()
|
2020-02-12 01:56:19 +01:00
|
|
|
|
{
|
2020-02-14 11:52:34 +01:00
|
|
|
|
// First take exclusivity on the OpenGL context.
|
2021-05-04 18:19:04 +02:00
|
|
|
|
((Renderer)Renderer).InitializeBackgroundContext(SPBOpenGLContext.CreateBackgroundContext(_openGLContext));
|
2021-04-14 12:28:43 +02:00
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
_openGLContext.MakeCurrent(_nativeWindow);
|
2020-02-12 01:56:19 +01:00
|
|
|
|
|
2021-04-14 12:28:43 +02:00
|
|
|
|
GL.ClearColor(0, 0, 0, 1.0f);
|
2020-02-14 11:52:34 +01:00
|
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit);
|
|
|
|
|
SwapBuffers();
|
2020-02-12 01:56:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
public override void SwapBuffers()
|
2020-02-12 01:56:19 +01:00
|
|
|
|
{
|
2021-05-04 18:19:04 +02:00
|
|
|
|
_nativeWindow.SwapBuffers();
|
2020-02-12 01:56:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
public override string GetGpuVendorName()
|
2020-02-12 01:56:19 +01:00
|
|
|
|
{
|
2021-05-04 18:19:04 +02:00
|
|
|
|
return ((Renderer)Renderer).GpuVendor;
|
2020-02-12 01:56:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
protected override void Dispose(bool disposing)
|
2020-02-12 01:56:19 +01:00
|
|
|
|
{
|
2021-05-04 18:19:04 +02:00
|
|
|
|
// Try to bind the OpenGL context before calling the shutdown event
|
|
|
|
|
try
|
2020-02-12 01:56:19 +01:00
|
|
|
|
{
|
2021-05-04 18:19:04 +02:00
|
|
|
|
_openGLContext?.MakeCurrent(_nativeWindow);
|
2020-02-12 01:56:19 +01:00
|
|
|
|
}
|
2021-05-04 18:19:04 +02:00
|
|
|
|
catch (Exception) { }
|
2020-02-12 01:56:19 +01:00
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
Device.DisposeGpu();
|
|
|
|
|
NpadManager.Dispose();
|
2021-04-14 12:28:43 +02:00
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
// Unbind context and destroy everything
|
|
|
|
|
try
|
2021-04-14 12:28:43 +02:00
|
|
|
|
{
|
2021-05-04 18:19:04 +02:00
|
|
|
|
_openGLContext?.MakeCurrent(null);
|
2021-04-14 12:28:43 +02:00
|
|
|
|
}
|
2021-05-04 18:19:04 +02:00
|
|
|
|
catch (Exception) { }
|
2021-04-14 12:28:43 +02:00
|
|
|
|
|
2021-05-04 18:19:04 +02:00
|
|
|
|
_openGLContext.Dispose();
|
2021-04-14 12:28:43 +02:00
|
|
|
|
}
|
2020-02-12 01:56:19 +01:00
|
|
|
|
}
|
|
|
|
|
}
|