From 2f49a551c13934b9dc815bbda67a45098f0482a7 Mon Sep 17 00:00:00 2001 From: Jesse Talavera-Greenberg Date: Thu, 26 Oct 2023 09:37:39 -0400 Subject: [PATCH] Lock the GPU3D state when rendering on the render thread or serializing it --- src/GPU3D.cpp | 13 +++++++++++++ src/GPU3D_Soft.cpp | 3 +++ 2 files changed, 16 insertions(+) diff --git a/src/GPU3D.cpp b/src/GPU3D.cpp index 41229424..662a3287 100644 --- a/src/GPU3D.cpp +++ b/src/GPU3D.cpp @@ -275,6 +275,7 @@ u32 RenderNumPolygons; u32 FlushRequest; u32 FlushAttributes; +Platform::Mutex* StateLock; std::unique_ptr CurrentRenderer = {}; bool AbortFrame; @@ -294,12 +295,22 @@ void Vertex::DoSavestate(Savestate* file) noexcept bool Init() { + if (StateLock) + { + Platform::Mutex_Free(StateLock); + } + StateLock = Platform::Mutex_Create(); return true; } void DeInit() { CurrentRenderer = nullptr; + if (StateLock) + { + Platform::Mutex_Free(StateLock); + StateLock = nullptr; + } } void ResetRenderingState() @@ -410,6 +421,7 @@ void DoSavestate(Savestate* file) if (softRenderer && softRenderer->IsThreaded()) { softRenderer->SetupRenderThread(); + Platform::Mutex_Lock(StateLock); } CmdFIFO.DoSavestate(file); @@ -643,6 +655,7 @@ void DoSavestate(Savestate* file) file->Var32(&TexPalette); if (softRenderer && softRenderer->IsThreaded()) { + Platform::Mutex_Unlock(StateLock); softRenderer->EnableRenderThread(); } } diff --git a/src/GPU3D_Soft.cpp b/src/GPU3D_Soft.cpp index 33b278be..905ede88 100644 --- a/src/GPU3D_Soft.cpp +++ b/src/GPU3D_Soft.cpp @@ -1736,6 +1736,7 @@ void SoftRenderer::RestartFrame() EnableRenderThread(); } +extern Platform::Mutex* StateLock; void SoftRenderer::RenderThreadFunc() { for (;;) @@ -1750,8 +1751,10 @@ void SoftRenderer::RenderThreadFunc() } else { + Platform::Mutex_Lock(StateLock); ClearBuffers(); RenderPolygons(true, &RenderPolygonRAM[0], RenderNumPolygons); + Platform::Mutex_Unlock(StateLock); } Platform::Semaphore_Post(Sema_RenderDone);