From 13921bc1b06bd90bb4ea954f37ae09aca1065c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tur=C3=A1nszki=20J=C3=A1nos?= Date: Thu, 16 Jan 2025 09:59:43 +0100 Subject: [PATCH] wait for job system threads to shut down before exiting --- Editor/main_SDL2.cpp | 2 ++ Editor/main_Windows.cpp | 2 ++ WickedEngine/wiJobSystem.cpp | 7 +++++++ WickedEngine/wiJobSystem.h | 4 ++++ WickedEngine/wiPlatform.h | 1 - WickedEngine/wiRenderer.cpp | 6 +++--- WickedEngine/wiVersion.cpp | 2 +- 7 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Editor/main_SDL2.cpp b/Editor/main_SDL2.cpp index 38491a855..8f5c31559 100644 --- a/Editor/main_SDL2.cpp +++ b/Editor/main_SDL2.cpp @@ -214,5 +214,7 @@ int main(int argc, char *argv[]) int ret = sdl_loop(); + wi::jobsystem::ShutDown(); + return ret; } diff --git a/Editor/main_Windows.cpp b/Editor/main_Windows.cpp index bc413e95b..053e3af1a 100644 --- a/Editor/main_Windows.cpp +++ b/Editor/main_Windows.cpp @@ -72,6 +72,8 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, } } + wi::jobsystem::ShutDown(); + return (int) msg.wParam; } diff --git a/WickedEngine/wiJobSystem.cpp b/WickedEngine/wiJobSystem.cpp index c4eb5b2a8..ac8fbbb24 100644 --- a/WickedEngine/wiJobSystem.cpp +++ b/WickedEngine/wiJobSystem.cpp @@ -113,6 +113,8 @@ namespace wi::jobsystem std::atomic_bool alive{ true }; void ShutDown() { + if (IsShuttingDown()) + return; alive.store(false); // indicate that new jobs cannot be started from this point bool wake_loop = true; std::thread waker([&] { @@ -322,6 +324,11 @@ namespace wi::jobsystem internal_state.ShutDown(); } + bool IsShuttingDown() + { + return internal_state.alive.load() == false; + } + uint32_t GetThreadCount(Priority priority) { return internal_state.resources[int(priority)].numThreads; diff --git a/WickedEngine/wiJobSystem.h b/WickedEngine/wiJobSystem.h index 63303fa17..464ce2a2f 100644 --- a/WickedEngine/wiJobSystem.h +++ b/WickedEngine/wiJobSystem.h @@ -8,6 +8,10 @@ namespace wi::jobsystem void Initialize(uint32_t maxThreadCount = ~0u); void ShutDown(); + // Returns true if the job system is shutting down + // Long-running (multi-frame) jobs should ideally check this and exit themselves if true + bool IsShuttingDown(); + struct JobArgs { uint32_t jobIndex; // job index relative to dispatch (like SV_DispatchThreadID in HLSL) diff --git a/WickedEngine/wiPlatform.h b/WickedEngine/wiPlatform.h index 3acbfe32c..94928c255 100644 --- a/WickedEngine/wiPlatform.h +++ b/WickedEngine/wiPlatform.h @@ -11,7 +11,6 @@ #endif #include #include -#include #if WINAPI_FAMILY == WINAPI_FAMILY_GAMES #define PLATFORM_XBOX diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index c9a568d41..a9a0555ca 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -1803,11 +1803,8 @@ void LoadShaders() RegisterCustomShader(customShader); } - wi::jobsystem::Wait(ctx); - - for (uint32_t renderPass = 0; renderPass < RENDERPASS_COUNT; ++renderPass) { for (uint32_t mesh_shader = 0; mesh_shader <= (device->CheckCapability(GraphicsDeviceCapability::MESH_SHADER) ? 1u : 0u); ++mesh_shader) @@ -1976,6 +1973,9 @@ void LoadShaders() wi::jobsystem::Wait(mesh_shader_ctx); } + if (wi::jobsystem::IsShuttingDown()) + return; + ObjectRenderingVariant variant = {}; variant.bits.renderpass = renderPass; variant.bits.shadertype = shaderType; diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index f7f3a63c1..b05b926ca 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wi::version // minor features, major updates, breaking compatibility changes const int minor = 71; // minor bug fixes, alterations, refactors, updates - const int revision = 656; + const int revision = 657; const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);