New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an alternative backend parallel compilation queue for macOS #5291
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,7 +134,7 @@ internal class AppHost | |
_inputManager = inputManager; | ||
_accountManager = accountManager; | ||
_userChannelPersistence = userChannelPersistence; | ||
_renderingThread = new Thread(RenderLoop, 1 * 1024 * 1024) { Name = "GUI.RenderThread" }; | ||
_renderingThread = new Thread(RenderLoop, 2 * 1024 * 1024) { Name = "GUI.RenderThread" }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the sake of readability, it might be nice to make it a named constant - either the whole stack size or the multiplier |
||
_lastCursorMoveTime = Stopwatch.GetTimestamp(); | ||
_glLogLevel = ConfigurationState.Instance.Logger.GraphicsDebugLevel; | ||
_topLevel = topLevel; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Threading; | ||
|
||
namespace Ryujinx.Graphics.Vulkan | ||
{ | ||
class ShaderCompilationQueue | ||
{ | ||
private const int MaxParallelCompilations = 8; | ||
private const int MaxThreadStackSize = 2 * 1024 * 1024; // MB | ||
|
||
private struct Request | ||
{ | ||
public readonly ulong Id; | ||
public readonly Action Callback; | ||
|
||
public Request(ulong id, Action callback) | ||
{ | ||
Id = id; | ||
Callback = callback; | ||
} | ||
} | ||
|
||
private readonly Thread[] _workerThreads; | ||
private readonly CancellationTokenSource _cts; | ||
private readonly BlockingCollection<Request>[] _queues; | ||
private readonly ulong[] _finishedIds; | ||
private ulong _currentId; | ||
private int _currentQueueIndex; | ||
|
||
public ShaderCompilationQueue() | ||
{ | ||
_workerThreads = new Thread[MaxParallelCompilations]; | ||
_queues = new BlockingCollection<Request>[MaxParallelCompilations]; | ||
_finishedIds = new ulong[MaxParallelCompilations]; | ||
|
||
_cts = new CancellationTokenSource(); | ||
|
||
for (int i = 0; i < MaxParallelCompilations; i++) | ||
{ | ||
_queues[i] = new BlockingCollection<Request>(); | ||
|
||
Thread thread = new Thread(DoWork, MaxThreadStackSize) { Name = $"BackgroundShaderCompiler.{i}" }; | ||
thread.IsBackground = true; | ||
thread.Start(i); | ||
|
||
_workerThreads[i] = thread; | ||
} | ||
} | ||
|
||
private void DoWork(object threadId) | ||
{ | ||
int queueIndex = (int)threadId; | ||
|
||
try | ||
{ | ||
var queue = _queues[queueIndex]; | ||
|
||
foreach (var request in queue.GetConsumingEnumerable(_cts.Token)) | ||
{ | ||
request.Callback(); | ||
|
||
lock (queue) | ||
{ | ||
_finishedIds[queueIndex] = request.Id; | ||
|
||
Monitor.PulseAll(queue); | ||
} | ||
} | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
} | ||
} | ||
|
||
public ShaderCompilationRequest Add(Action callback) | ||
{ | ||
ulong newId = Interlocked.Increment(ref _currentId); | ||
|
||
// Let's keep rotating between the queues to increase the chances | ||
// that the selected queue thread is currently idle. | ||
int queueIndex = Interlocked.Increment(ref _currentQueueIndex) % MaxParallelCompilations; | ||
|
||
_queues[queueIndex].Add(new Request(newId, callback)); | ||
|
||
return new ShaderCompilationRequest(this, queueIndex, newId); | ||
} | ||
|
||
public void Wait(int queueIndex, ulong id) | ||
{ | ||
var queue = _queues[queueIndex]; | ||
|
||
lock (queue) | ||
{ | ||
while (_finishedIds[queueIndex] < id) | ||
{ | ||
Monitor.Wait(queue); | ||
} | ||
} | ||
} | ||
|
||
public bool IsCompleted(int queueIndex, ulong id) | ||
{ | ||
var queue = _queues[queueIndex]; | ||
|
||
lock (queue) | ||
{ | ||
return _finishedIds[queueIndex] >= id; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
for (int i = 0; i < MaxParallelCompilations; i++) | ||
{ | ||
_queues[i].CompleteAdding(); | ||
} | ||
|
||
_cts.Cancel(); | ||
|
||
for (int i = 0; i < MaxParallelCompilations; i++) | ||
{ | ||
_workerThreads[i].Join(); | ||
|
||
_queues[i].Dispose(); | ||
} | ||
|
||
_cts.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Ryujinx.Graphics.Vulkan | ||
{ | ||
struct ShaderCompilationRequest | ||
{ | ||
private readonly Task _task; | ||
private readonly ShaderCompilationQueue _queue; | ||
private readonly int _queueIndex; | ||
private readonly ulong _requestId; | ||
|
||
public bool IsCompleted | ||
{ | ||
get | ||
{ | ||
if (_task != null) | ||
{ | ||
return _task.IsCompleted; | ||
} | ||
else | ||
{ | ||
return _queue.IsCompleted(_queueIndex, _requestId); | ||
} | ||
} | ||
} | ||
|
||
public ShaderCompilationRequest(Task task) | ||
{ | ||
_task = task; | ||
_queue = null; | ||
_queueIndex = 0; | ||
_requestId = 0; | ||
} | ||
|
||
public ShaderCompilationRequest(ShaderCompilationQueue queue, int queueIndex, ulong requestId) | ||
{ | ||
_task = null; | ||
_queue = queue; | ||
_queueIndex = queueIndex; | ||
_requestId = requestId; | ||
} | ||
|
||
public void Wait() | ||
{ | ||
if (_task != null) | ||
{ | ||
_task.Wait(); | ||
} | ||
else | ||
{ | ||
_queue.Wait(_queueIndex, _requestId); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do the same change in SDL2 headless and GTK for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be in a common place, but I'm not sure where to put it. It will be annoying to change 4 files every time we want to change the stack size.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess maybe some helper in the common project to create a thread with a given name and stack size would be nice?
Then we would have other helper around for the common thread that are duplicated for all projects that have the right values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, the render thread should be created by the
ThreadedRenderer
and the GPU thread should be created on theGpuContext
. But it's not that simple since the OpenGL context is tied to the thread it is created on, and there's also some coupling with the UI and a few places that needs to know if the current thread is the "render" thread. Not something that I will change here though, so I guess I will just increase the stack size of the other threads for now.