diff --git a/src/swrenderer/r_renderthread.cpp b/src/swrenderer/r_renderthread.cpp index fa20da456..bb2816a94 100644 --- a/src/swrenderer/r_renderthread.cpp +++ b/src/swrenderer/r_renderthread.cpp @@ -103,23 +103,28 @@ namespace swrenderer // calls to GetPixels for this to work. static std::mutex loadmutex; - loadmutex.lock(); - try + + std::unique_lock lock(loadmutex); + + texture->GetPixels(); + const FTexture::Span *spans; + texture->GetColumn(0, &spans); + if (Viewport->RenderTarget->IsBgra()) { - texture->GetPixels(); - const FTexture::Span *spans; - texture->GetColumn(0, &spans); - if (Viewport->RenderTarget->IsBgra()) - { - texture->GetPixelsBgra(); - texture->GetColumnBgra(0, &spans); - } - loadmutex.unlock(); + texture->GetPixelsBgra(); + texture->GetColumnBgra(0, &spans); } - catch (...) + } + + void RenderThread::PreparePolyObject(subsector_t *sub) + { + static std::mutex polyobjmutex; + + std::unique_lock lock(polyobjmutex); + + if (sub->BSP == nullptr || sub->BSP->bDirty) { - loadmutex.unlock(); - throw; + sub->BuildPolyBSP(); } } } diff --git a/src/swrenderer/r_renderthread.h b/src/swrenderer/r_renderthread.h index 3a439421c..6bc23bba5 100644 --- a/src/swrenderer/r_renderthread.h +++ b/src/swrenderer/r_renderthread.h @@ -83,6 +83,9 @@ namespace swrenderer // Make sure texture can accessed safely void PrepareTexture(FTexture *texture); + + // Setup poly object in a threadsafe manner + void PreparePolyObject(subsector_t *sub); private: std::unique_ptr tc_drawers; diff --git a/src/swrenderer/scene/r_opaque_pass.cpp b/src/swrenderer/scene/r_opaque_pass.cpp index 7d6d6b461..da820d7d2 100644 --- a/src/swrenderer/scene/r_opaque_pass.cpp +++ b/src/swrenderer/scene/r_opaque_pass.cpp @@ -390,10 +390,8 @@ namespace swrenderer void RenderOpaquePass::AddPolyobjs(subsector_t *sub) { - if (sub->BSP == nullptr || sub->BSP->bDirty) - { - sub->BuildPolyBSP(); - } + Thread->PreparePolyObject(sub); + if (sub->BSP->Nodes.Size() == 0) { RenderSubsector(&sub->BSP->Subsectors[0]);