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. 😌

* 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>
This commit is contained in:
riperiperi 2021-08-26 23:31:29 +01:00 committed by GitHub
parent 501c3d5cea
commit ec3e848d79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
143 changed files with 4491 additions and 200 deletions

View file

@ -33,6 +33,8 @@ namespace Ryujinx.Graphics.OpenGL
private int _boundDrawFramebuffer;
private int _boundReadFramebuffer;
private CounterQueueEvent _activeConditionalRender;
private struct Vector4<T>
{
public T X;
@ -1246,7 +1248,7 @@ namespace Ryujinx.Graphics.OpenGL
return (_boundDrawFramebuffer, _boundReadFramebuffer);
}
public void UpdateRenderScale(ShaderStage stage, float[] scales, int textureCount, int imageCount)
public void UpdateRenderScale(ShaderStage stage, ReadOnlySpan<float> scales, int textureCount, int imageCount)
{
if (stage != ShaderStage.Compute && stage != ShaderStage.Fragment)
{
@ -1352,16 +1354,18 @@ namespace Ryujinx.Graphics.OpenGL
// - Comparing against 0.
// - Event has not already been flushed.
if (evt.Disposed)
{
// If the event has been flushed, then just use the values on the CPU.
// The query object may already be repurposed for another draw (eg. begin + end).
return false;
}
if (compare == 0 && evt.Type == QueryTarget.SamplesPassed && evt.ClearCounter)
{
if (!value.ReserveForHostAccess())
{
// If the event has been flushed, then just use the values on the CPU.
// The query object may already be repurposed for another draw (eg. begin + end).
return false;
}
GL.BeginConditionalRender(evt.Query, isEqual ? ConditionalRenderType.QueryNoWaitInverted : ConditionalRenderType.QueryNoWait);
_activeConditionalRender = evt;
return true;
}
}
@ -1381,6 +1385,9 @@ namespace Ryujinx.Graphics.OpenGL
public void EndHostConditionalRendering()
{
GL.EndConditionalRender();
_activeConditionalRender?.ReleaseHostAccess();
_activeConditionalRender = null;
}
public void Dispose()
@ -1400,6 +1407,7 @@ namespace Ryujinx.Graphics.OpenGL
}
}
_activeConditionalRender?.ReleaseHostAccess();
_framebuffer?.Dispose();
_vertexArray?.Dispose();
}

View file

@ -54,6 +54,7 @@ namespace Ryujinx.Graphics.OpenGL.Queries
Marshal.WriteInt64(_bufferMap, -1L);
GL.GetQueryObject(Query, GetQueryObjectParam.QueryResult, (long*)0);
GL.MemoryBarrier(MemoryBarrierFlags.QueryBufferBarrierBit | MemoryBarrierFlags.ClientMappedBufferBarrierBit);
}
}

View file

@ -17,12 +17,14 @@ namespace Ryujinx.Graphics.OpenGL.Queries
private CounterQueueEvent _current;
private ulong _accumulatedCounter;
private int _waiterCount;
private object _lock = new object();
private Queue<BufferedQuery> _queryPool;
private AutoResetEvent _queuedEvent = new AutoResetEvent(false);
private AutoResetEvent _wakeSignal = new AutoResetEvent(false);
private AutoResetEvent _eventConsumed = new AutoResetEvent(false);
private Thread _consumerThread;
@ -63,7 +65,13 @@ namespace Ryujinx.Graphics.OpenGL.Queries
}
else
{
evt.TryConsume(ref _accumulatedCounter, true, _wakeSignal);
// Spin-wait rather than sleeping if there are any waiters, by passing null instead of the wake signal.
evt.TryConsume(ref _accumulatedCounter, true, _waiterCount == 0 ? _wakeSignal : null);
}
if (_waiterCount > 0)
{
_eventConsumed.Set();
}
}
}
@ -95,7 +103,7 @@ namespace Ryujinx.Graphics.OpenGL.Queries
}
}
public CounterQueueEvent QueueReport(EventHandler<ulong> resultHandler, ulong lastDrawIndex)
public CounterQueueEvent QueueReport(EventHandler<ulong> resultHandler, ulong lastDrawIndex, bool hostReserved)
{
CounterQueueEvent result;
ulong draws = lastDrawIndex - _current.DrawIndex;
@ -105,6 +113,12 @@ namespace Ryujinx.Graphics.OpenGL.Queries
// A query's result only matters if more than one draw was performed during it.
// Otherwise, dummy it out and return 0 immediately.
if (hostReserved)
{
// This counter event is guaranteed to be available for host conditional rendering.
_current.ReserveForHostAccess();
}
if (draws > 0)
{
_current.Complete(true);
@ -175,25 +189,18 @@ namespace Ryujinx.Graphics.OpenGL.Queries
public void FlushTo(CounterQueueEvent evt)
{
lock (_lock)
// Flush the counter queue on the main thread.
Interlocked.Increment(ref _waiterCount);
_wakeSignal.Set();
while (!evt.Disposed)
{
if (evt.Disposed)
{
return;
}
// Tell the queue to process all events up to this one.
while (_events.Count > 0)
{
CounterQueueEvent flush = _events.Dequeue();
flush.TryConsume(ref _accumulatedCounter, true);
if (flush == evt)
{
return;
}
}
_eventConsumed.WaitOne(1);
}
Interlocked.Decrement(ref _waiterCount);
}
public void Dispose()
@ -218,6 +225,10 @@ namespace Ryujinx.Graphics.OpenGL.Queries
{
query.Dispose();
}
_queuedEvent.Dispose();
_wakeSignal.Dispose();
_eventConsumed.Dispose();
}
}
}

View file

@ -1,4 +1,5 @@
using OpenTK.Graphics.OpenGL;
using Ryujinx.Common.Logging;
using Ryujinx.Graphics.GAL;
using System;
using System.Threading;
@ -21,7 +22,11 @@ namespace Ryujinx.Graphics.OpenGL.Queries
private CounterQueue _queue;
private BufferedQuery _counter;
private bool _hostAccessReserved = false;
private int _refCount = 1; // Starts with a reference from the counter queue.
private object _lock = new object();
private ulong _result = ulong.MaxValue;
public CounterQueueEvent(CounterQueue queue, QueryTarget type, ulong drawIndex)
{
@ -76,6 +81,8 @@ namespace Ryujinx.Graphics.OpenGL.Queries
result += (ulong)queryResult;
_result = result;
OnResult?.Invoke(this, result);
Dispose(); // Return the our resources to the pool.
@ -95,10 +102,60 @@ namespace Ryujinx.Graphics.OpenGL.Queries
_queue.FlushTo(this);
}
public void DecrementRefCount()
{
if (Interlocked.Decrement(ref _refCount) == 0)
{
DisposeInternal();
}
}
public bool ReserveForHostAccess()
{
if (_hostAccessReserved)
{
return true;
}
if (IsValueAvailable())
{
return false;
}
if (Interlocked.Increment(ref _refCount) == 1)
{
Interlocked.Decrement(ref _refCount);
return false;
}
_hostAccessReserved = true;
return true;
}
public void ReleaseHostAccess()
{
_hostAccessReserved = false;
DecrementRefCount();
}
private void DisposeInternal()
{
_queue.ReturnQueryObject(_counter);
}
private bool IsValueAvailable()
{
return _result != ulong.MaxValue || _counter.TryGetResult(out _);
}
public void Dispose()
{
Disposed = true;
_queue.ReturnQueryObject(_counter);
DecrementRefCount();
}
}
}

View file

@ -23,9 +23,9 @@ namespace Ryujinx.Graphics.OpenGL.Queries
}
}
public CounterQueueEvent QueueReport(CounterType type, EventHandler<ulong> resultHandler, ulong lastDrawIndex)
public CounterQueueEvent QueueReport(CounterType type, EventHandler<ulong> resultHandler, ulong lastDrawIndex, bool hostReserved)
{
return _counterQueues[(int)type].QueueReport(resultHandler, lastDrawIndex);
return _counterQueues[(int)type].QueueReport(resultHandler, lastDrawIndex, hostReserved);
}
public void QueueReset(CounterType type)

View file

@ -40,6 +40,8 @@ namespace Ryujinx.Graphics.OpenGL
public string GpuRenderer { get; private set; }
public string GpuVersion { get; private set; }
public bool PreferThreading => true;
public Renderer()
{
_pipeline = new Pipeline();
@ -129,9 +131,9 @@ namespace Ryujinx.Graphics.OpenGL
ResourcePool.Tick();
}
public ICounterEvent ReportCounter(CounterType type, EventHandler<ulong> resultHandler)
public ICounterEvent ReportCounter(CounterType type, EventHandler<ulong> resultHandler, bool hostReserved)
{
return _counters.QueueReport(type, resultHandler, _pipeline.DrawCount);
return _counters.QueueReport(type, resultHandler, _pipeline.DrawCount, hostReserved);
}
public void Initialize(GraphicsDebugLevel glLogLevel)
@ -163,8 +165,10 @@ namespace Ryujinx.Graphics.OpenGL
_counters.QueueReset(type);
}
public void BackgroundContextAction(Action action)
public void BackgroundContextAction(Action action, bool alwaysBackground = false)
{
// alwaysBackground is ignored, since we cannot switch from the current context.
if (IOpenGLContext.HasContext())
{
action(); // We have a context already - use that (assuming it is the main one).

View file

@ -23,13 +23,15 @@ namespace Ryujinx.Graphics.OpenGL
_renderer = renderer;
}
public void Present(ITexture texture, ImageCrop crop)
public void Present(ITexture texture, ImageCrop crop, Action swapBuffersCallback)
{
GL.Disable(EnableCap.FramebufferSrgb);
CopyTextureToFrameBufferRGB(0, GetCopyFramebufferHandleLazy(), (TextureView)texture, crop);
GL.Enable(EnableCap.FramebufferSrgb);
swapBuffersCallback();
}
public void SetSize(int width, int height)