From 5a82df216dc1a59443eba5fd3655d30ea1d6d724 Mon Sep 17 00:00:00 2001 From: eoineoineoin Date: Sat, 19 Oct 2024 15:13:29 +0100 Subject: [PATCH] Fixes for rendering in multiple windows (#5497) * Fix race condition when swapping buffers of secondary windows * Avoid creating opengl 3.3 windows, to avoid Steam overlay bug * Revert "Avoid creating opengl 3.3 windows, to avoid Steam overlay bug" This reverts commit 97b5e7f4610d44083215f55d52359942f3363206. * Add CVar to perform unlocking test --- .../Graphics/Clyde/GLContext/GLContextWindow.cs | 13 +++++++++++-- Robust.Shared/CVars.cs | 8 ++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Robust.Client/Graphics/Clyde/GLContext/GLContextWindow.cs b/Robust.Client/Graphics/Clyde/GLContext/GLContextWindow.cs index d5dc1938480..887b954129f 100644 --- a/Robust.Client/Graphics/Clyde/GLContext/GLContextWindow.cs +++ b/Robust.Client/Graphics/Clyde/GLContext/GLContextWindow.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Channels; @@ -176,6 +176,7 @@ private void BlitSecondaryWindows() window.BlitDoneEvent!.Reset(); window.BlitStartEvent!.Set(); window.BlitDoneEvent.Wait(); + window.UnlockBeforeSwap = Clyde._cfg.GetCVar(CVars.DisplayThreadUnlockBeforeSwap); } } else @@ -212,8 +213,15 @@ private void BlitThreadDoSecondaryWindowBlit(WindowData window) GL.DrawArrays(PrimitiveType.TriangleStrip, 0, 4); Clyde.CheckGlError(); - window.BlitDoneEvent?.Set(); + if (window.UnlockBeforeSwap) + { + window.BlitDoneEvent?.Set(); + } Clyde._windowing!.WindowSwapBuffers(window.Reg); + if (!window.UnlockBeforeSwap) + { + window.BlitDoneEvent?.Set(); + } } private unsafe void BlitThreadInit(WindowData reg) @@ -336,6 +344,7 @@ private sealed class WindowData public Thread? BlitThread; public ManualResetEventSlim? BlitStartEvent; public ManualResetEventSlim? BlitDoneEvent; + public bool UnlockBeforeSwap; } } } diff --git a/Robust.Shared/CVars.cs b/Robust.Shared/CVars.cs index c57b3ea6909..d276d0fea89 100644 --- a/Robust.Shared/CVars.cs +++ b/Robust.Shared/CVars.cs @@ -1105,6 +1105,14 @@ protected CVars() public static readonly CVarDef DisplayThreadWindowBlit = CVarDef.Create("display.thread_window_blit", true, CVar.CLIENTONLY); + /// + /// Diagnostic flag for testing. When using a separate thread for multi-window blitting, + /// should the worker be unblocked before the SwapBuffers(). Setting to true may improve + /// performance but may cause crashes or rendering errors. + /// + public static readonly CVarDef DisplayThreadUnlockBeforeSwap = + CVarDef.Create("display.thread_unlock_before_swap", false, CVar.CLIENTONLY); + /// /// Buffer size of input command channel from windowing thread to main game thread. ///