From 8ec138399af7e5a703a1e9b8b946f26dcd0c8473 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 11 Jun 2025 18:40:35 +0200 Subject: [PATCH] Add higher level streaming time budget in frame based on idle time --- Source/Engine/Engine/Engine.cpp | 5 +++++ Source/Engine/Level/Level.cpp | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Engine/Engine.cpp b/Source/Engine/Engine/Engine.cpp index 5607432c2..4ea18a85c 100644 --- a/Source/Engine/Engine/Engine.cpp +++ b/Source/Engine/Engine/Engine.cpp @@ -76,6 +76,7 @@ FatalErrorType Engine::FatalError = FatalErrorType::None; bool Engine::IsRequestingExit = false; int32 Engine::ExitCode = 0; Window* Engine::MainWindow = nullptr; +double EngineIdleTime = 0; int32 Engine::Main(const Char* cmdLine) { @@ -190,7 +191,10 @@ int32 Engine::Main(const Char* cmdLine) if (timeToTick > 0.002) { PROFILE_CPU_NAMED("Idle"); + auto sleepStart = Platform::GetTimeSeconds(); Platform::Sleep(1); + auto sleepEnd = Platform::GetTimeSeconds(); + EngineIdleTime += sleepEnd - sleepStart; } } @@ -227,6 +231,7 @@ int32 Engine::Main(const Char* cmdLine) OnUpdate(); OnLateUpdate(); Time::OnEndUpdate(); + EngineIdleTime = 0; } // Start physics simulation diff --git a/Source/Engine/Level/Level.cpp b/Source/Engine/Level/Level.cpp index 678eada1f..5516e6d53 100644 --- a/Source/Engine/Level/Level.cpp +++ b/Source/Engine/Level/Level.cpp @@ -251,6 +251,7 @@ public: }; LevelService LevelServiceInstanceService; +extern double EngineIdleTime; CriticalSection Level::ScenesLock; Array Level::Scenes; @@ -863,6 +864,8 @@ void LevelImpl::flushActions() else if (Engine::GetFramesPerSecond() > 0) targetFps = (float)Engine::GetFramesPerSecond(); context.TimeBudget = Level::StreamingFrameBudget / targetFps; + if (EngineIdleTime > 0.001) + context.TimeBudget += (float)(EngineIdleTime * 0.5); // Increase time budget if engine has some idle time for spare #if USE_EDITOR // Throttle up in Editor context.TimeBudget *= Editor::IsPlayMode ? 1.2f : 2.0f; @@ -871,8 +874,9 @@ void LevelImpl::flushActions() // Throttle up in Debug context.TimeBudget *= 1.2f; #endif - if (context.TimeBudget <= ZeroTolerance) - context.TimeBudget = MAX_float; + if (context.TimeBudget <= 0.0f) + context.TimeBudget = MAX_float; // Unlimited if 0 + context.TimeBudget = Math::Max(context.TimeBudget, 0.001f); // Minimum 1ms // Runs actions in order ScopeLock lock(_sceneActionsLocker);