Add higher level streaming time budget in frame based on idle time

This commit is contained in:
Wojtek Figat
2025-06-11 18:40:35 +02:00
parent 5b6859a66f
commit 8ec138399a
2 changed files with 11 additions and 2 deletions

View File

@@ -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

View File

@@ -251,6 +251,7 @@ public:
};
LevelService LevelServiceInstanceService;
extern double EngineIdleTime;
CriticalSection Level::ScenesLock;
Array<Scene*> 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);